~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/test_strncmp.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
  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  */
 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 
 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 
 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 
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 
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 
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 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php