| 1 | |
| 2 |
|
| 3 | #include <test_progs.h>
|
| 4 | #include "string_kfuncs_success.skel.h"
|
| 5 | #include "string_kfuncs_failure1.skel.h"
|
| 6 | #include "string_kfuncs_failure2.skel.h"
|
| 7 | #include <sys/mman.h>
|
| 8 |
|
| 9 | static const char * const test_cases[] = {
|
| 10 | "strcmp",
|
| 11 | "strcasecmp",
|
| 12 | "strncasecmp",
|
| 13 | "strchr",
|
| 14 | "strchrnul",
|
| 15 | "strnchr",
|
| 16 | "strrchr",
|
| 17 | "strlen",
|
| 18 | "strnlen",
|
| 19 | "strspn_str",
|
| 20 | "strspn_accept",
|
| 21 | "strcspn_str",
|
| 22 | "strcspn_reject",
|
| 23 | "strstr",
|
| 24 | "strcasestr",
|
| 25 | "strnstr",
|
| 26 | "strncasestr",
|
| 27 | };
|
| 28 |
|
| 29 | void run_too_long_tests(void)
|
| 30 | {
|
| 31 | struct string_kfuncs_failure2 *skel;
|
| 32 | struct bpf_program *prog;
|
| 33 | char test_name[256];
|
| 34 | int err, i;
|
| 35 |
|
| 36 | skel = string_kfuncs_failure2__open_and_load();
|
| 37 | if (!ASSERT_OK_PTR(skel, "string_kfuncs_failure2__open_and_load"))
|
| 38 | return;
|
| 39 |
|
| 40 | memset(skel->bss->long_str, 'a', sizeof(skel->bss->long_str));
|
| 41 |
|
| 42 | for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
|
| 43 | sprintf(test_name, "test_%s_too_long", test_cases[i]);
|
| 44 | if (!test__start_subtest(test_name))
|
| 45 | continue;
|
| 46 |
|
| 47 | prog = bpf_object__find_program_by_name(skel->obj, test_name);
|
| 48 | if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
|
| 49 | goto cleanup;
|
| 50 |
|
| 51 | LIBBPF_OPTS(bpf_test_run_opts, topts);
|
| 52 | err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
|
| 53 | if (!ASSERT_OK(err, "bpf_prog_test_run"))
|
| 54 | goto cleanup;
|
| 55 |
|
| 56 | ASSERT_EQ(topts.retval, -E2BIG, "reading too long string fails with -E2BIG");
|
| 57 | }
|
| 58 |
|
| 59 | cleanup:
|
| 60 | string_kfuncs_failure2__destroy(skel);
|
| 61 | }
|
| 62 |
|
| 63 | void test_string_kfuncs(void)
|
| 64 | {
|
| 65 | RUN_TESTS(string_kfuncs_success);
|
| 66 | RUN_TESTS(string_kfuncs_failure1);
|
| 67 |
|
| 68 | run_too_long_tests();
|
| 69 | }
|