1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
3 #include <test_progs.h>
4 #include "strncmp_test.skel.h"
5 
trigger_strncmp(const struct strncmp_test * skel)6 static int trigger_strncmp(const struct strncmp_test *skel)
7 {
8 	int cmp;
9 
10 	usleep(1);
11 
12 	cmp = skel->bss->cmp_ret;
13 	if (cmp > 0)
14 		return 1;
15 	if (cmp < 0)
16 		return -1;
17 	return 0;
18 }
19 
20 /*
21  * Compare str and target after making str[i] != target[i].
22  * When exp is -1, make str[i] < target[i] and delta = -1.
23  */
strncmp_full_str_cmp(struct strncmp_test * skel,const char * name,int exp)24 static void strncmp_full_str_cmp(struct strncmp_test *skel, const char *name,
25 				 int exp)
26 {
27 	size_t nr = sizeof(skel->bss->str);
28 	char *str = skel->bss->str;
29 	int delta = exp;
30 	int got;
31 	size_t i;
32 
33 	memcpy(str, skel->rodata->target, nr);
34 	for (i = 0; i < nr - 1; i++) {
35 		str[i] += delta;
36 
37 		got = trigger_strncmp(skel);
38 		ASSERT_EQ(got, exp, name);
39 
40 		str[i] -= delta;
41 	}
42 }
43 
test_strncmp_ret(void)44 static void test_strncmp_ret(void)
45 {
46 	struct strncmp_test *skel;
47 	int err, got;
48 
49 	skel = strncmp_test__open();
50 	if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
51 		return;
52 
53 	bpf_program__set_autoload(skel->progs.do_strncmp, true);
54 
55 	err = strncmp_test__load(skel);
56 	if (!ASSERT_EQ(err, 0, "strncmp_test load"))
57 		goto out;
58 
59 	err = strncmp_test__attach(skel);
60 	if (!ASSERT_EQ(err, 0, "strncmp_test attach"))
61 		goto out;
62 
63 	skel->bss->target_pid = getpid();
64 
65 	/* Empty str */
66 	skel->bss->str[0] = '\0';
67 	got = trigger_strncmp(skel);
68 	ASSERT_EQ(got, -1, "strncmp: empty str");
69 
70 	/* Same string */
71 	memcpy(skel->bss->str, skel->rodata->target, sizeof(skel->bss->str));
72 	got = trigger_strncmp(skel);
73 	ASSERT_EQ(got, 0, "strncmp: same str");
74 
75 	/* Not-null-termainted string  */
76 	memcpy(skel->bss->str, skel->rodata->target, sizeof(skel->bss->str));
77 	skel->bss->str[sizeof(skel->bss->str) - 1] = 'A';
78 	got = trigger_strncmp(skel);
79 	ASSERT_EQ(got, 1, "strncmp: not-null-term str");
80 
81 	strncmp_full_str_cmp(skel, "strncmp: less than", -1);
82 	strncmp_full_str_cmp(skel, "strncmp: greater than", 1);
83 out:
84 	strncmp_test__destroy(skel);
85 }
86 
test_strncmp_bad_not_const_str_size(void)87 static void test_strncmp_bad_not_const_str_size(void)
88 {
89 	struct strncmp_test *skel;
90 	int err;
91 
92 	skel = strncmp_test__open();
93 	if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
94 		return;
95 
96 	bpf_program__set_autoload(skel->progs.strncmp_bad_not_const_str_size, true);
97 
98 	err = strncmp_test__load(skel);
99 	ASSERT_ERR(err, "strncmp_test load bad_not_const_str_size");
100 
101 	strncmp_test__destroy(skel);
102 }
103 
test_strncmp_bad_writable_target(void)104 static void test_strncmp_bad_writable_target(void)
105 {
106 	struct strncmp_test *skel;
107 	int err;
108 
109 	skel = strncmp_test__open();
110 	if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
111 		return;
112 
113 	bpf_program__set_autoload(skel->progs.strncmp_bad_writable_target, true);
114 
115 	err = strncmp_test__load(skel);
116 	ASSERT_ERR(err, "strncmp_test load bad_writable_target");
117 
118 	strncmp_test__destroy(skel);
119 }
120 
test_strncmp_bad_not_null_term_target(void)121 static void test_strncmp_bad_not_null_term_target(void)
122 {
123 	struct strncmp_test *skel;
124 	int err;
125 
126 	skel = strncmp_test__open();
127 	if (!ASSERT_OK_PTR(skel, "strncmp_test open"))
128 		return;
129 
130 	bpf_program__set_autoload(skel->progs.strncmp_bad_not_null_term_target, true);
131 
132 	err = strncmp_test__load(skel);
133 	ASSERT_ERR(err, "strncmp_test load bad_not_null_term_target");
134 
135 	strncmp_test__destroy(skel);
136 }
137 
test_test_strncmp(void)138 void test_test_strncmp(void)
139 {
140 	if (test__start_subtest("strncmp_ret"))
141 		test_strncmp_ret();
142 	if (test__start_subtest("strncmp_bad_not_const_str_size"))
143 		test_strncmp_bad_not_const_str_size();
144 	if (test__start_subtest("strncmp_bad_writable_target"))
145 		test_strncmp_bad_writable_target();
146 	if (test__start_subtest("strncmp_bad_not_null_term_target"))
147 		test_strncmp_bad_not_null_term_target();
148 }
149