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

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/snprintf.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 Google LLC. */
  3 
  4 #include <test_progs.h>
  5 #include "test_snprintf.skel.h"
  6 #include "test_snprintf_single.skel.h"
  7 
  8 #define EXP_NUM_OUT  "-8 9 96 -424242 1337 DABBAD00"
  9 #define EXP_NUM_RET  sizeof(EXP_NUM_OUT)
 10 
 11 #define EXP_IP_OUT   "127.000.000.001 0000:0000:0000:0000:0000:0000:0000:0001"
 12 #define EXP_IP_RET   sizeof(EXP_IP_OUT)
 13 
 14 /* The third specifier, %pB, depends on compiler inlining so don't check it */
 15 #define EXP_SYM_OUT  "schedule schedule+0x0/"
 16 #define MIN_SYM_RET  sizeof(EXP_SYM_OUT)
 17 
 18 /* The third specifier, %p, is a hashed pointer which changes on every reboot */
 19 #define EXP_ADDR_OUT "0000000000000000 ffff00000add4e55 "
 20 #define EXP_ADDR_RET sizeof(EXP_ADDR_OUT "unknownhashedptr")
 21 
 22 #define EXP_STR_OUT  "str1         a  b c      d e longstr"
 23 #define EXP_STR_RET  sizeof(EXP_STR_OUT)
 24 
 25 #define EXP_OVER_OUT "%over"
 26 #define EXP_OVER_RET 10
 27 
 28 #define EXP_PAD_OUT "    4 000"
 29 #define EXP_PAD_RET 900007
 30 
 31 #define EXP_NO_ARG_OUT "simple case"
 32 #define EXP_NO_ARG_RET 12
 33 
 34 #define EXP_NO_BUF_RET 29
 35 
 36 static void test_snprintf_positive(void)
 37 {
 38         char exp_addr_out[] = EXP_ADDR_OUT;
 39         char exp_sym_out[]  = EXP_SYM_OUT;
 40         struct test_snprintf *skel;
 41 
 42         skel = test_snprintf__open_and_load();
 43         if (!ASSERT_OK_PTR(skel, "skel_open"))
 44                 return;
 45 
 46         skel->bss->pid = getpid();
 47 
 48         if (!ASSERT_OK(test_snprintf__attach(skel), "skel_attach"))
 49                 goto cleanup;
 50 
 51         /* trigger tracepoint */
 52         usleep(1);
 53 
 54         ASSERT_STREQ(skel->bss->num_out, EXP_NUM_OUT, "num_out");
 55         ASSERT_EQ(skel->bss->num_ret, EXP_NUM_RET, "num_ret");
 56 
 57         ASSERT_STREQ(skel->bss->ip_out, EXP_IP_OUT, "ip_out");
 58         ASSERT_EQ(skel->bss->ip_ret, EXP_IP_RET, "ip_ret");
 59 
 60         ASSERT_OK(memcmp(skel->bss->sym_out, exp_sym_out,
 61                          sizeof(exp_sym_out) - 1), "sym_out");
 62         ASSERT_LT(MIN_SYM_RET, skel->bss->sym_ret, "sym_ret");
 63 
 64         ASSERT_OK(memcmp(skel->bss->addr_out, exp_addr_out,
 65                          sizeof(exp_addr_out) - 1), "addr_out");
 66         ASSERT_EQ(skel->bss->addr_ret, EXP_ADDR_RET, "addr_ret");
 67 
 68         ASSERT_STREQ(skel->bss->str_out, EXP_STR_OUT, "str_out");
 69         ASSERT_EQ(skel->bss->str_ret, EXP_STR_RET, "str_ret");
 70 
 71         ASSERT_STREQ(skel->bss->over_out, EXP_OVER_OUT, "over_out");
 72         ASSERT_EQ(skel->bss->over_ret, EXP_OVER_RET, "over_ret");
 73 
 74         ASSERT_STREQ(skel->bss->pad_out, EXP_PAD_OUT, "pad_out");
 75         ASSERT_EQ(skel->bss->pad_ret, EXP_PAD_RET, "pad_ret");
 76 
 77         ASSERT_STREQ(skel->bss->noarg_out, EXP_NO_ARG_OUT, "no_arg_out");
 78         ASSERT_EQ(skel->bss->noarg_ret, EXP_NO_ARG_RET, "no_arg_ret");
 79 
 80         ASSERT_EQ(skel->bss->nobuf_ret, EXP_NO_BUF_RET, "no_buf_ret");
 81 
 82 cleanup:
 83         test_snprintf__destroy(skel);
 84 }
 85 
 86 /* Loads an eBPF object calling bpf_snprintf with up to 10 characters of fmt */
 87 static int load_single_snprintf(char *fmt)
 88 {
 89         struct test_snprintf_single *skel;
 90         int ret;
 91 
 92         skel = test_snprintf_single__open();
 93         if (!skel)
 94                 return -EINVAL;
 95 
 96         memcpy(skel->rodata->fmt, fmt, MIN(strlen(fmt) + 1, 10));
 97 
 98         ret = test_snprintf_single__load(skel);
 99         test_snprintf_single__destroy(skel);
100 
101         return ret;
102 }
103 
104 static void test_snprintf_negative(void)
105 {
106         ASSERT_OK(load_single_snprintf("valid %d"), "valid usage");
107 
108         ASSERT_ERR(load_single_snprintf("0123456789"), "no terminating zero");
109         ASSERT_ERR(load_single_snprintf("%d %d"), "too many specifiers");
110         ASSERT_ERR(load_single_snprintf("%pi5"), "invalid specifier 1");
111         ASSERT_ERR(load_single_snprintf("%a"), "invalid specifier 2");
112         ASSERT_ERR(load_single_snprintf("%"), "invalid specifier 3");
113         ASSERT_ERR(load_single_snprintf("%12345678"), "invalid specifier 4");
114         ASSERT_ERR(load_single_snprintf("%--------"), "invalid specifier 5");
115         ASSERT_ERR(load_single_snprintf("%lc"), "invalid specifier 6");
116         ASSERT_ERR(load_single_snprintf("%llc"), "invalid specifier 7");
117         ASSERT_ERR(load_single_snprintf("\x80"), "non ascii character");
118         ASSERT_ERR(load_single_snprintf("\x1"), "non printable character");
119 }
120 
121 void test_snprintf(void)
122 {
123         if (test__start_subtest("snprintf_positive"))
124                 test_snprintf_positive();
125         if (test__start_subtest("snprintf_negative"))
126                 test_snprintf_negative();
127 }
128 

~ [ 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