1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
4 #include <test_progs.h>
6 #include <sys/utsname.h>
7 #include <linux/version.h>
8 #include "test_core_extern.skel.h"
10 static uint32_t get_kernel_version(void)
12 uint32_t major, minor, patch;
16 if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
18 return KERNEL_VERSION(major, minor, patch);
21 #define CFG "CONFIG_BPF_SYSCALL=n\n"
23 static struct test_case {
27 struct test_core_extern__data data;
29 { .name = "default search path", .data = { .bpf_syscall = true } },
31 .name = "custom values",
32 .cfg = "CONFIG_BPF_SYSCALL=n\n"
36 "CONFIG_USHORT=30000\n"
38 "CONFIG_ULONG=0xDEADBEEFC0DE\n"
39 "CONFIG_STR=\"abracad\"\n"
43 .tristate_val = TRI_MODULE,
48 .ulong_val = 0xDEADBEEFC0DE,
53 { .name = "tristate (y)", .cfg = CFG"CONFIG_TRISTATE=y\n",
54 .data = { .tristate_val = TRI_YES } },
55 { .name = "tristate (n)", .cfg = CFG"CONFIG_TRISTATE=n\n",
56 .data = { .tristate_val = TRI_NO } },
57 { .name = "tristate (m)", .cfg = CFG"CONFIG_TRISTATE=m\n",
58 .data = { .tristate_val = TRI_MODULE } },
59 { .name = "tristate (int)", .fails = 1, .cfg = CFG"CONFIG_TRISTATE=1" },
60 { .name = "tristate (bad)", .fails = 1, .cfg = CFG"CONFIG_TRISTATE=M" },
62 { .name = "bool (y)", .cfg = CFG"CONFIG_BOOL=y\n",
63 .data = { .bool_val = true } },
64 { .name = "bool (n)", .cfg = CFG"CONFIG_BOOL=n\n",
65 .data = { .bool_val = false } },
66 { .name = "bool (tristate)", .fails = 1, .cfg = CFG"CONFIG_BOOL=m" },
67 { .name = "bool (int)", .fails = 1, .cfg = CFG"CONFIG_BOOL=1" },
69 { .name = "char (tristate)", .cfg = CFG"CONFIG_CHAR=m\n",
70 .data = { .char_val = 'm' } },
71 { .name = "char (bad)", .fails = 1, .cfg = CFG"CONFIG_CHAR=q\n" },
72 { .name = "char (empty)", .fails = 1, .cfg = CFG"CONFIG_CHAR=\n" },
73 { .name = "char (str)", .fails = 1, .cfg = CFG"CONFIG_CHAR=\"y\"\n" },
75 { .name = "str (empty)", .cfg = CFG"CONFIG_STR=\"\"\n",
76 .data = { .str_val = "\0\0\0\0\0\0\0" } },
77 { .name = "str (padded)", .cfg = CFG"CONFIG_STR=\"abra\"\n",
78 .data = { .str_val = "abra\0\0\0" } },
79 { .name = "str (too long)", .cfg = CFG"CONFIG_STR=\"abracada\"\n",
80 .data = { .str_val = "abracad" } },
81 { .name = "str (no value)", .fails = 1, .cfg = CFG"CONFIG_STR=\n" },
82 { .name = "str (bad value)", .fails = 1, .cfg = CFG"CONFIG_STR=bla\n" },
85 .name = "integer forms",
88 "CONFIG_USHORT=0462\n"
90 "CONFIG_ULONG=+1000000000000",
95 .ulong_val = 1000000000000,
98 { .name = "int (bad)", .fails = 1, .cfg = CFG"CONFIG_INT=abc" },
99 { .name = "int (str)", .fails = 1, .cfg = CFG"CONFIG_INT=\"abc\"" },
100 { .name = "int (empty)", .fails = 1, .cfg = CFG"CONFIG_INT=" },
101 { .name = "int (mixed)", .fails = 1, .cfg = CFG"CONFIG_INT=123abc" },
102 { .name = "int (max)", .cfg = CFG"CONFIG_INT=2147483647",
103 .data = { .int_val = 2147483647 } },
104 { .name = "int (min)", .cfg = CFG"CONFIG_INT=-2147483648",
105 .data = { .int_val = -2147483648 } },
106 { .name = "int (max+1)", .fails = 1, .cfg = CFG"CONFIG_INT=2147483648" },
107 { .name = "int (min-1)", .fails = 1, .cfg = CFG"CONFIG_INT=-2147483649" },
108 { .name = "ushort (max)", .cfg = CFG"CONFIG_USHORT=65535",
109 .data = { .ushort_val = 65535 } },
110 { .name = "ushort (min)", .cfg = CFG"CONFIG_USHORT=0",
111 .data = { .ushort_val = 0 } },
112 { .name = "ushort (max+1)", .fails = 1, .cfg = CFG"CONFIG_USHORT=65536" },
113 { .name = "ushort (min-1)", .fails = 1, .cfg = CFG"CONFIG_USHORT=-1" },
114 { .name = "u64 (max)", .cfg = CFG"CONFIG_ULONG=0xffffffffffffffff",
115 .data = { .ulong_val = 0xffffffffffffffff } },
116 { .name = "u64 (min)", .cfg = CFG"CONFIG_ULONG=0",
117 .data = { .ulong_val = 0 } },
118 { .name = "u64 (max+1)", .fails = 1, .cfg = CFG"CONFIG_ULONG=0x10000000000000000" },
121 void test_core_extern(void)
123 const uint32_t kern_ver = get_kernel_version();
124 int err, duration = 0, i, j;
125 struct test_core_extern *skel = NULL;
127 int n = sizeof(*skel->data) / sizeof(uint64_t);
129 for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
130 struct test_case *t = &test_cases[i];
131 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
135 if (!test__start_subtest(t->name))
138 skel = test_core_extern__open_opts(&opts);
139 if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
141 err = test_core_extern__load(skel);
143 CHECK(!err, "skel_load",
144 "shouldn't succeed open/load of skeleton\n");
146 } else if (CHECK(err, "skel_load",
147 "failed to open/load skeleton\n")) {
150 err = test_core_extern__attach(skel);
151 if (CHECK(err, "attach_raw_tp", "failed attach: %d\n", err))
156 t->data.kern_ver = kern_ver;
157 t->data.missing_val = 0xDEADC0DE;
158 got = (uint64_t *)skel->data;
159 exp = (uint64_t *)&t->data;
160 for (j = 0; j < n; j++) {
161 CHECK(got[j] != exp[j], "check_res",
162 "result #%d: expected %lx, but got %lx\n",
166 test_core_extern__destroy(skel);