1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2020 Facebook */
3
4#include <test_progs.h>
5#include <time.h>
6#include "test_varlen.skel.h"
7
8#define CHECK_VAL(got, exp) \
9 CHECK((got) != (exp), "check", "got %ld != exp %ld\n", \
10 (long)(got), (long)(exp))
11
12void test_varlen(void)
13{
14 int duration = 0, err;
15 struct test_varlen* skel;
16 struct test_varlen__bss *bss;
17 struct test_varlen__data *data;
18 const char str1[] = "Hello, ";
19 const char str2[] = "World!";
20 const char exp_str[] = "Hello, \0World!\0";
21 const int size1 = sizeof(str1);
22 const int size2 = sizeof(str2);
23
24 skel = test_varlen__open_and_load();
25 if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
26 return;
27 bss = skel->bss;
28 data = skel->data;
29
30 err = test_varlen__attach(skel);
31 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
32 goto cleanup;
33
34 bss->test_pid = getpid();
35
36 /* trigger everything */
37 memcpy(bss->buf_in1, str1, size1);
38 memcpy(bss->buf_in2, str2, size2);
39 bss->capture = true;
40 usleep(1);
41 bss->capture = false;
42
43 CHECK_VAL(bss->payload1_len1, size1);
44 CHECK_VAL(bss->payload1_len2, size2);
45 CHECK_VAL(bss->total1, size1 + size2);
46 CHECK(memcmp(bss->payload1, exp_str, size1 + size2), "content_check",
47 "doesn't match!\n");
48
49 CHECK_VAL(data->payload2_len1, size1);
50 CHECK_VAL(data->payload2_len2, size2);
51 CHECK_VAL(data->total2, size1 + size2);
52 CHECK(memcmp(data->payload2, exp_str, size1 + size2), "content_check",
53 "doesn't match!\n");
54
55 CHECK_VAL(data->payload3_len1, size1);
56 CHECK_VAL(data->payload3_len2, size2);
57 CHECK_VAL(data->total3, size1 + size2);
58 CHECK(memcmp(data->payload3, exp_str, size1 + size2), "content_check",
59 "doesn't match!\n");
60
61 CHECK_VAL(data->payload4_len1, size1);
62 CHECK_VAL(data->payload4_len2, size2);
63 CHECK_VAL(data->total4, size1 + size2);
64 CHECK(memcmp(data->payload4, exp_str, size1 + size2), "content_check",
65 "doesn't match!\n");
66
67 CHECK_VAL(bss->ret_bad_read, -EFAULT);
68 CHECK_VAL(data->payload_bad[0], 0x42);
69 CHECK_VAL(data->payload_bad[1], 0x42);
70 CHECK_VAL(data->payload_bad[2], 0);
71 CHECK_VAL(data->payload_bad[3], 0x42);
72 CHECK_VAL(data->payload_bad[4], 0x42);
73cleanup:
74 test_varlen__destroy(skel);
75}
76

source code of linux/tools/testing/selftests/bpf/prog_tests/varlen.c