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