xref: /linux/tools/testing/selftests/bpf/prog_tests/varlen.c (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
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 
12 void 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 cleanup:
67 	test_varlen__destroy(skel);
68 }
69