Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __TEST_PROGS_H
3#define __TEST_PROGS_H
4
5#include <stdio.h>
6#include <unistd.h>
7#include <errno.h>
8#include <string.h>
9#include <assert.h>
10#include <stdlib.h>
11#include <stdarg.h>
12#include <time.h>
13#include <signal.h>
14
15#include <linux/types.h>
16typedef __u16 __sum16;
17#include <arpa/inet.h>
18#include <linux/if_ether.h>
19#include <linux/if_packet.h>
20#include <linux/ip.h>
21#include <linux/ipv6.h>
22#include <linux/filter.h>
23#include <linux/perf_event.h>
24#include <linux/socket.h>
25#include <linux/unistd.h>
26
27#include <sys/ioctl.h>
28#include <sys/wait.h>
29#include <sys/types.h>
30#include <sys/time.h>
31#include <sys/param.h>
32#include <fcntl.h>
33#include <pthread.h>
34#include <linux/bpf.h>
35#include <linux/err.h>
36#include <bpf/bpf.h>
37#include <bpf/libbpf.h>
38
39#include "test_iptunnel_common.h"
40#include "bpf_util.h"
41#include <bpf/bpf_endian.h>
42#include "trace_helpers.h"
43#include "testing_helpers.h"
44
45enum verbosity {
46 VERBOSE_NONE,
47 VERBOSE_NORMAL,
48 VERBOSE_VERY,
49 VERBOSE_SUPER,
50};
51
52struct test_filter {
53 char *name;
54 char **subtests;
55 int subtest_cnt;
56};
57
58struct test_filter_set {
59 struct test_filter *tests;
60 int cnt;
61};
62
63struct test_selector {
64 struct test_filter_set whitelist;
65 struct test_filter_set blacklist;
66 bool *num_set;
67 int num_set_len;
68};
69
70struct subtest_state {
71 char *name;
72 size_t log_cnt;
73 char *log_buf;
74 int error_cnt;
75 bool skipped;
76 bool filtered;
77 bool should_tmon;
78
79 FILE *stdout_saved;
80};
81
82struct test_state {
83 bool tested;
84 bool force_log;
85
86 int error_cnt;
87 int skip_cnt;
88 int sub_succ_cnt;
89
90 struct subtest_state *subtest_states;
91 int subtest_num;
92
93 size_t log_cnt;
94 char *log_buf;
95
96 FILE *stdout_saved;
97};
98
99extern int env_verbosity;
100
101struct test_env {
102 struct test_selector test_selector;
103 struct test_selector subtest_selector;
104 struct test_selector tmon_selector;
105 bool verifier_stats;
106 bool debug;
107 enum verbosity verbosity;
108
109 bool jit_enabled;
110 bool has_testmod;
111 bool get_test_cnt;
112 bool list_test_names;
113
114 struct prog_test_def *test; /* current running test */
115 struct test_state *test_state; /* current running test state */
116 struct subtest_state *subtest_state; /* current running subtest state */
117
118 FILE *stdout_saved;
119 FILE *stderr_saved;
120 int nr_cpus;
121 FILE *json;
122
123 int succ_cnt; /* successful tests */
124 int sub_succ_cnt; /* successful sub-tests */
125 int fail_cnt; /* total failed tests + sub-tests */
126 int skip_cnt; /* skipped tests */
127
128 int saved_netns_fd;
129 int workers; /* number of worker process */
130 int worker_id; /* id number of current worker, main process is -1 */
131 pid_t *worker_pids; /* array of worker pids */
132 int *worker_socks; /* array of worker socks */
133 int *worker_current_test; /* array of current running test for each worker */
134
135 pthread_t main_thread;
136 int secs_till_notify;
137 int secs_till_kill;
138 timer_t watchdog; /* watch for stalled tests/subtests */
139 enum { WD_NOTIFY, WD_KILL } watchdog_state;
140};
141
142#define MAX_LOG_TRUNK_SIZE 8192
143#define MAX_SUBTEST_NAME 1024
144enum msg_type {
145 MSG_DO_TEST = 0,
146 MSG_TEST_DONE = 1,
147 MSG_TEST_LOG = 2,
148 MSG_SUBTEST_DONE = 3,
149 MSG_EXIT = 255,
150};
151struct msg {
152 enum msg_type type;
153 union {
154 struct {
155 int num;
156 } do_test;
157 struct {
158 int num;
159 int sub_succ_cnt;
160 int error_cnt;
161 int skip_cnt;
162 bool have_log;
163 int subtest_num;
164 } test_done;
165 struct {
166 char log_buf[MAX_LOG_TRUNK_SIZE + 1];
167 bool is_last;
168 } test_log;
169 struct {
170 int num;
171 char name[MAX_SUBTEST_NAME + 1];
172 int error_cnt;
173 bool skipped;
174 bool filtered;
175 bool have_log;
176 } subtest_done;
177 };
178};
179
180extern struct test_env env;
181
182void test__force_log(void);
183bool test__start_subtest(const char *name);
184void test__end_subtest(void);
185void test__skip(void);
186void test__fail(void);
187int test__join_cgroup(const char *path);
188
189#define PRINT_FAIL(format...) \
190 ({ \
191 test__fail(); \
192 fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__); \
193 fprintf(stdout, ##format); \
194 })
195
196#define _CHECK(condition, tag, duration, format...) ({ \
197 int __ret = !!(condition); \
198 int __save_errno = errno; \
199 if (__ret) { \
200 test__fail(); \
201 fprintf(stdout, "%s:FAIL:%s ", __func__, tag); \
202 fprintf(stdout, ##format); \
203 } else { \
204 fprintf(stdout, "%s:PASS:%s %d nsec\n", \
205 __func__, tag, duration); \
206 } \
207 errno = __save_errno; \
208 __ret; \
209})
210
211#define CHECK_FAIL(condition) ({ \
212 int __ret = !!(condition); \
213 int __save_errno = errno; \
214 if (__ret) { \
215 test__fail(); \
216 fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__); \
217 } \
218 errno = __save_errno; \
219 __ret; \
220})
221
222#define CHECK(condition, tag, format...) \
223 _CHECK(condition, tag, duration, format)
224#define CHECK_ATTR(condition, tag, format...) \
225 _CHECK(condition, tag, tattr.duration, format)
226
227#define ASSERT_FAIL(fmt, args...) ({ \
228 static int duration = 0; \
229 CHECK(false, "", fmt"\n", ##args); \
230 false; \
231})
232
233#define ASSERT_TRUE(actual, name) ({ \
234 static int duration = 0; \
235 bool ___ok = (actual); \
236 CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \
237 ___ok; \
238})
239
240#define ASSERT_FALSE(actual, name) ({ \
241 static int duration = 0; \
242 bool ___ok = !(actual); \
243 CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \
244 ___ok; \
245})
246
247#define ASSERT_EQ(actual, expected, name) ({ \
248 static int duration = 0; \
249 typeof(actual) ___act = (actual); \
250 typeof(expected) ___exp = (expected); \
251 bool ___ok = ___act == ___exp; \
252 CHECK(!___ok, (name), \
253 "unexpected %s: actual %lld != expected %lld\n", \
254 (name), (long long)(___act), (long long)(___exp)); \
255 ___ok; \
256})
257
258#define ASSERT_NEQ(actual, expected, name) ({ \
259 static int duration = 0; \
260 typeof(actual) ___act = (actual); \
261 typeof(expected) ___exp = (expected); \
262 bool ___ok = ___act != ___exp; \
263 CHECK(!___ok, (name), \
264 "unexpected %s: actual %lld == expected %lld\n", \
265 (name), (long long)(___act), (long long)(___exp)); \
266 ___ok; \
267})
268
269#define ASSERT_LT(actual, expected, name) ({ \
270 static int duration = 0; \
271 typeof(actual) ___act = (actual); \
272 typeof(expected) ___exp = (expected); \
273 bool ___ok = ___act < ___exp; \
274 CHECK(!___ok, (name), \
275 "unexpected %s: actual %lld >= expected %lld\n", \
276 (name), (long long)(___act), (long long)(___exp)); \
277 ___ok; \
278})
279
280#define ASSERT_LE(actual, expected, name) ({ \
281 static int duration = 0; \
282 typeof(actual) ___act = (actual); \
283 typeof(expected) ___exp = (expected); \
284 bool ___ok = ___act <= ___exp; \
285 CHECK(!___ok, (name), \
286 "unexpected %s: actual %lld > expected %lld\n", \
287 (name), (long long)(___act), (long long)(___exp)); \
288 ___ok; \
289})
290
291#define ASSERT_GT(actual, expected, name) ({ \
292 static int duration = 0; \
293 typeof(actual) ___act = (actual); \
294 typeof(expected) ___exp = (expected); \
295 bool ___ok = ___act > ___exp; \
296 CHECK(!___ok, (name), \
297 "unexpected %s: actual %lld <= expected %lld\n", \
298 (name), (long long)(___act), (long long)(___exp)); \
299 ___ok; \
300})
301
302#define ASSERT_GE(actual, expected, name) ({ \
303 static int duration = 0; \
304 typeof(actual) ___act = (actual); \
305 typeof(expected) ___exp = (expected); \
306 bool ___ok = ___act >= ___exp; \
307 CHECK(!___ok, (name), \
308 "unexpected %s: actual %lld < expected %lld\n", \
309 (name), (long long)(___act), (long long)(___exp)); \
310 ___ok; \
311})
312
313#define ASSERT_STREQ(actual, expected, name) ({ \
314 static int duration = 0; \
315 const char *___act = actual; \
316 const char *___exp = expected; \
317 bool ___ok = strcmp(___act, ___exp) == 0; \
318 CHECK(!___ok, (name), \
319 "unexpected %s: actual '%s' != expected '%s'\n", \
320 (name), ___act, ___exp); \
321 ___ok; \
322})
323
324#define ASSERT_STRNEQ(actual, expected, len, name) ({ \
325 static int duration = 0; \
326 const char *___act = actual; \
327 const char *___exp = expected; \
328 int ___len = len; \
329 bool ___ok = strncmp(___act, ___exp, ___len) == 0; \
330 CHECK(!___ok, (name), \
331 "unexpected %s: actual '%.*s' != expected '%.*s'\n", \
332 (name), ___len, ___act, ___len, ___exp); \
333 ___ok; \
334})
335
336#define ASSERT_HAS_SUBSTR(str, substr, name) ({ \
337 static int duration = 0; \
338 const char *___str = str; \
339 const char *___substr = substr; \
340 bool ___ok = strstr(___str, ___substr) != NULL; \
341 CHECK(!___ok, (name), \
342 "unexpected %s: '%s' is not a substring of '%s'\n", \
343 (name), ___substr, ___str); \
344 ___ok; \
345})
346
347#define ASSERT_OK(res, name) ({ \
348 static int duration = 0; \
349 long long ___res = (res); \
350 bool ___ok = ___res == 0; \
351 CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \
352 ___res, errno); \
353 ___ok; \
354})
355
356#define ASSERT_ERR(res, name) ({ \
357 static int duration = 0; \
358 long long ___res = (res); \
359 bool ___ok = ___res < 0; \
360 CHECK(!___ok, (name), "unexpected success: %lld\n", ___res); \
361 ___ok; \
362})
363
364#define ASSERT_NULL(ptr, name) ({ \
365 static int duration = 0; \
366 const void *___res = (ptr); \
367 bool ___ok = !___res; \
368 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
369 ___ok; \
370})
371
372#define ASSERT_OK_PTR(ptr, name) ({ \
373 static int duration = 0; \
374 const void *___res = (ptr); \
375 int ___err = libbpf_get_error(___res); \
376 bool ___ok = ___err == 0; \
377 CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \
378 ___ok; \
379})
380
381#define ASSERT_ERR_PTR(ptr, name) ({ \
382 static int duration = 0; \
383 const void *___res = (ptr); \
384 int ___err = libbpf_get_error(___res); \
385 bool ___ok = ___err != 0; \
386 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
387 ___ok; \
388})
389
390#define ASSERT_OK_FD(fd, name) ({ \
391 static int duration = 0; \
392 int ___fd = (fd); \
393 bool ___ok = ___fd >= 0; \
394 CHECK(!___ok, (name), "unexpected fd: %d (errno %d)\n", \
395 ___fd, errno); \
396 ___ok; \
397})
398
399#define ASSERT_ERR_FD(fd, name) ({ \
400 static int duration = 0; \
401 int ___fd = (fd); \
402 bool ___ok = ___fd < 0; \
403 CHECK(!___ok, (name), "unexpected fd: %d\n", ___fd); \
404 ___ok; \
405})
406
407#define SYS(goto_label, fmt, ...) \
408 ({ \
409 char cmd[1024]; \
410 snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
411 if (!ASSERT_OK(system(cmd), cmd)) \
412 goto goto_label; \
413 })
414
415#define ALL_TO_DEV_NULL " >/dev/null 2>&1"
416
417#define SYS_NOFAIL(fmt, ...) \
418 ({ \
419 char cmd[1024]; \
420 int n; \
421 n = snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
422 if (n < sizeof(cmd) && sizeof(cmd) - n >= sizeof(ALL_TO_DEV_NULL)) \
423 strcat(cmd, ALL_TO_DEV_NULL); \
424 system(cmd); \
425 })
426
427int start_libbpf_log_capture(void);
428char *stop_libbpf_log_capture(void);
429
430static inline __u64 ptr_to_u64(const void *ptr)
431{
432 return (__u64) (unsigned long) ptr;
433}
434
435static inline void *u64_to_ptr(__u64 ptr)
436{
437 return (void *) (unsigned long) ptr;
438}
439
440int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
441int compare_map_keys(int map1_fd, int map2_fd);
442int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
443int trigger_module_test_read(int read_sz);
444int trigger_module_test_write(int write_sz);
445int write_sysctl(const char *sysctl, const char *value);
446int get_bpf_max_tramp_links_from(struct btf *btf);
447int get_bpf_max_tramp_links(void);
448
449struct netns_obj;
450struct netns_obj *netns_new(const char *name, bool open);
451void netns_free(struct netns_obj *netns);
452
453#ifdef __x86_64__
454#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
455#elif defined(__s390x__)
456#define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
457#elif defined(__aarch64__)
458#define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
459#elif defined(__riscv)
460#define SYS_NANOSLEEP_KPROBE_NAME "__riscv_sys_nanosleep"
461#else
462#define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
463#endif
464
465#define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
466
467typedef int (*pre_execution_cb)(struct bpf_object *obj);
468
469struct test_loader {
470 char *log_buf;
471 size_t log_buf_sz;
472 pre_execution_cb pre_execution_cb;
473
474 struct bpf_object *obj;
475};
476
477static inline void test_loader__set_pre_execution_cb(struct test_loader *tester,
478 pre_execution_cb cb)
479{
480 tester->pre_execution_cb = cb;
481}
482
483typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
484
485extern void test_loader__run_subtests(struct test_loader *tester,
486 const char *skel_name,
487 skel_elf_bytes_fn elf_bytes_factory);
488
489extern void test_loader_fini(struct test_loader *tester);
490
491#define RUN_TESTS(skel) ({ \
492 struct test_loader tester = {}; \
493 \
494 test_loader__run_subtests(&tester, #skel, skel##__elf_bytes); \
495 test_loader_fini(&tester); \
496})
497
498#endif /* __TEST_PROGS_H */
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __TEST_PROGS_H
3#define __TEST_PROGS_H
4
5#include <stdio.h>
6#include <unistd.h>
7#include <errno.h>
8#include <string.h>
9#include <assert.h>
10#include <stdlib.h>
11#include <stdarg.h>
12#include <time.h>
13#include <signal.h>
14
15#include <linux/types.h>
16typedef __u16 __sum16;
17#include <arpa/inet.h>
18#include <linux/if_ether.h>
19#include <linux/if_packet.h>
20#include <linux/ip.h>
21#include <linux/ipv6.h>
22#include <linux/filter.h>
23#include <linux/perf_event.h>
24#include <linux/socket.h>
25#include <linux/unistd.h>
26
27#include <sys/ioctl.h>
28#include <sys/wait.h>
29#include <sys/types.h>
30#include <sys/time.h>
31#include <sys/param.h>
32#include <fcntl.h>
33#include <pthread.h>
34#include <linux/bpf.h>
35#include <linux/err.h>
36#include <bpf/bpf.h>
37#include <bpf/libbpf.h>
38
39#include "test_iptunnel_common.h"
40#include "bpf_util.h"
41#include <bpf/bpf_endian.h>
42#include "trace_helpers.h"
43#include "testing_helpers.h"
44
45enum verbosity {
46 VERBOSE_NONE,
47 VERBOSE_NORMAL,
48 VERBOSE_VERY,
49 VERBOSE_SUPER,
50};
51
52struct test_filter {
53 char *name;
54 char **subtests;
55 int subtest_cnt;
56};
57
58struct test_filter_set {
59 struct test_filter *tests;
60 int cnt;
61};
62
63struct test_selector {
64 struct test_filter_set whitelist;
65 struct test_filter_set blacklist;
66 bool *num_set;
67 int num_set_len;
68};
69
70struct subtest_state {
71 char *name;
72 size_t log_cnt;
73 char *log_buf;
74 int error_cnt;
75 bool skipped;
76 bool filtered;
77
78 FILE *stdout;
79};
80
81struct test_state {
82 bool tested;
83 bool force_log;
84
85 int error_cnt;
86 int skip_cnt;
87 int sub_succ_cnt;
88
89 struct subtest_state *subtest_states;
90 int subtest_num;
91
92 size_t log_cnt;
93 char *log_buf;
94
95 FILE *stdout;
96};
97
98struct test_env {
99 struct test_selector test_selector;
100 struct test_selector subtest_selector;
101 bool verifier_stats;
102 bool debug;
103 enum verbosity verbosity;
104
105 bool jit_enabled;
106 bool has_testmod;
107 bool get_test_cnt;
108 bool list_test_names;
109
110 struct prog_test_def *test; /* current running test */
111 struct test_state *test_state; /* current running test state */
112 struct subtest_state *subtest_state; /* current running subtest state */
113
114 FILE *stdout;
115 FILE *stderr;
116 int nr_cpus;
117 FILE *json;
118
119 int succ_cnt; /* successful tests */
120 int sub_succ_cnt; /* successful sub-tests */
121 int fail_cnt; /* total failed tests + sub-tests */
122 int skip_cnt; /* skipped tests */
123
124 int saved_netns_fd;
125 int workers; /* number of worker process */
126 int worker_id; /* id number of current worker, main process is -1 */
127 pid_t *worker_pids; /* array of worker pids */
128 int *worker_socks; /* array of worker socks */
129 int *worker_current_test; /* array of current running test for each worker */
130};
131
132#define MAX_LOG_TRUNK_SIZE 8192
133#define MAX_SUBTEST_NAME 1024
134enum msg_type {
135 MSG_DO_TEST = 0,
136 MSG_TEST_DONE = 1,
137 MSG_TEST_LOG = 2,
138 MSG_SUBTEST_DONE = 3,
139 MSG_EXIT = 255,
140};
141struct msg {
142 enum msg_type type;
143 union {
144 struct {
145 int num;
146 } do_test;
147 struct {
148 int num;
149 int sub_succ_cnt;
150 int error_cnt;
151 int skip_cnt;
152 bool have_log;
153 int subtest_num;
154 } test_done;
155 struct {
156 char log_buf[MAX_LOG_TRUNK_SIZE + 1];
157 bool is_last;
158 } test_log;
159 struct {
160 int num;
161 char name[MAX_SUBTEST_NAME + 1];
162 int error_cnt;
163 bool skipped;
164 bool filtered;
165 bool have_log;
166 } subtest_done;
167 };
168};
169
170extern struct test_env env;
171
172void test__force_log(void);
173bool test__start_subtest(const char *name);
174void test__end_subtest(void);
175void test__skip(void);
176void test__fail(void);
177int test__join_cgroup(const char *path);
178
179#define PRINT_FAIL(format...) \
180 ({ \
181 test__fail(); \
182 fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__); \
183 fprintf(stdout, ##format); \
184 })
185
186#define _CHECK(condition, tag, duration, format...) ({ \
187 int __ret = !!(condition); \
188 int __save_errno = errno; \
189 if (__ret) { \
190 test__fail(); \
191 fprintf(stdout, "%s:FAIL:%s ", __func__, tag); \
192 fprintf(stdout, ##format); \
193 } else { \
194 fprintf(stdout, "%s:PASS:%s %d nsec\n", \
195 __func__, tag, duration); \
196 } \
197 errno = __save_errno; \
198 __ret; \
199})
200
201#define CHECK_FAIL(condition) ({ \
202 int __ret = !!(condition); \
203 int __save_errno = errno; \
204 if (__ret) { \
205 test__fail(); \
206 fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__); \
207 } \
208 errno = __save_errno; \
209 __ret; \
210})
211
212#define CHECK(condition, tag, format...) \
213 _CHECK(condition, tag, duration, format)
214#define CHECK_ATTR(condition, tag, format...) \
215 _CHECK(condition, tag, tattr.duration, format)
216
217#define ASSERT_FAIL(fmt, args...) ({ \
218 static int duration = 0; \
219 CHECK(false, "", fmt"\n", ##args); \
220 false; \
221})
222
223#define ASSERT_TRUE(actual, name) ({ \
224 static int duration = 0; \
225 bool ___ok = (actual); \
226 CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \
227 ___ok; \
228})
229
230#define ASSERT_FALSE(actual, name) ({ \
231 static int duration = 0; \
232 bool ___ok = !(actual); \
233 CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \
234 ___ok; \
235})
236
237#define ASSERT_EQ(actual, expected, name) ({ \
238 static int duration = 0; \
239 typeof(actual) ___act = (actual); \
240 typeof(expected) ___exp = (expected); \
241 bool ___ok = ___act == ___exp; \
242 CHECK(!___ok, (name), \
243 "unexpected %s: actual %lld != expected %lld\n", \
244 (name), (long long)(___act), (long long)(___exp)); \
245 ___ok; \
246})
247
248#define ASSERT_NEQ(actual, expected, name) ({ \
249 static int duration = 0; \
250 typeof(actual) ___act = (actual); \
251 typeof(expected) ___exp = (expected); \
252 bool ___ok = ___act != ___exp; \
253 CHECK(!___ok, (name), \
254 "unexpected %s: actual %lld == expected %lld\n", \
255 (name), (long long)(___act), (long long)(___exp)); \
256 ___ok; \
257})
258
259#define ASSERT_LT(actual, expected, name) ({ \
260 static int duration = 0; \
261 typeof(actual) ___act = (actual); \
262 typeof(expected) ___exp = (expected); \
263 bool ___ok = ___act < ___exp; \
264 CHECK(!___ok, (name), \
265 "unexpected %s: actual %lld >= expected %lld\n", \
266 (name), (long long)(___act), (long long)(___exp)); \
267 ___ok; \
268})
269
270#define ASSERT_LE(actual, expected, name) ({ \
271 static int duration = 0; \
272 typeof(actual) ___act = (actual); \
273 typeof(expected) ___exp = (expected); \
274 bool ___ok = ___act <= ___exp; \
275 CHECK(!___ok, (name), \
276 "unexpected %s: actual %lld > expected %lld\n", \
277 (name), (long long)(___act), (long long)(___exp)); \
278 ___ok; \
279})
280
281#define ASSERT_GT(actual, expected, name) ({ \
282 static int duration = 0; \
283 typeof(actual) ___act = (actual); \
284 typeof(expected) ___exp = (expected); \
285 bool ___ok = ___act > ___exp; \
286 CHECK(!___ok, (name), \
287 "unexpected %s: actual %lld <= expected %lld\n", \
288 (name), (long long)(___act), (long long)(___exp)); \
289 ___ok; \
290})
291
292#define ASSERT_GE(actual, expected, name) ({ \
293 static int duration = 0; \
294 typeof(actual) ___act = (actual); \
295 typeof(expected) ___exp = (expected); \
296 bool ___ok = ___act >= ___exp; \
297 CHECK(!___ok, (name), \
298 "unexpected %s: actual %lld < expected %lld\n", \
299 (name), (long long)(___act), (long long)(___exp)); \
300 ___ok; \
301})
302
303#define ASSERT_STREQ(actual, expected, name) ({ \
304 static int duration = 0; \
305 const char *___act = actual; \
306 const char *___exp = expected; \
307 bool ___ok = strcmp(___act, ___exp) == 0; \
308 CHECK(!___ok, (name), \
309 "unexpected %s: actual '%s' != expected '%s'\n", \
310 (name), ___act, ___exp); \
311 ___ok; \
312})
313
314#define ASSERT_STRNEQ(actual, expected, len, name) ({ \
315 static int duration = 0; \
316 const char *___act = actual; \
317 const char *___exp = expected; \
318 int ___len = len; \
319 bool ___ok = strncmp(___act, ___exp, ___len) == 0; \
320 CHECK(!___ok, (name), \
321 "unexpected %s: actual '%.*s' != expected '%.*s'\n", \
322 (name), ___len, ___act, ___len, ___exp); \
323 ___ok; \
324})
325
326#define ASSERT_HAS_SUBSTR(str, substr, name) ({ \
327 static int duration = 0; \
328 const char *___str = str; \
329 const char *___substr = substr; \
330 bool ___ok = strstr(___str, ___substr) != NULL; \
331 CHECK(!___ok, (name), \
332 "unexpected %s: '%s' is not a substring of '%s'\n", \
333 (name), ___substr, ___str); \
334 ___ok; \
335})
336
337#define ASSERT_OK(res, name) ({ \
338 static int duration = 0; \
339 long long ___res = (res); \
340 bool ___ok = ___res == 0; \
341 CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \
342 ___res, errno); \
343 ___ok; \
344})
345
346#define ASSERT_ERR(res, name) ({ \
347 static int duration = 0; \
348 long long ___res = (res); \
349 bool ___ok = ___res < 0; \
350 CHECK(!___ok, (name), "unexpected success: %lld\n", ___res); \
351 ___ok; \
352})
353
354#define ASSERT_NULL(ptr, name) ({ \
355 static int duration = 0; \
356 const void *___res = (ptr); \
357 bool ___ok = !___res; \
358 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
359 ___ok; \
360})
361
362#define ASSERT_OK_PTR(ptr, name) ({ \
363 static int duration = 0; \
364 const void *___res = (ptr); \
365 int ___err = libbpf_get_error(___res); \
366 bool ___ok = ___err == 0; \
367 CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \
368 ___ok; \
369})
370
371#define ASSERT_ERR_PTR(ptr, name) ({ \
372 static int duration = 0; \
373 const void *___res = (ptr); \
374 int ___err = libbpf_get_error(___res); \
375 bool ___ok = ___err != 0; \
376 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
377 ___ok; \
378})
379
380#define SYS(goto_label, fmt, ...) \
381 ({ \
382 char cmd[1024]; \
383 snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
384 if (!ASSERT_OK(system(cmd), cmd)) \
385 goto goto_label; \
386 })
387
388#define SYS_NOFAIL(fmt, ...) \
389 ({ \
390 char cmd[1024]; \
391 snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
392 system(cmd); \
393 })
394
395static inline __u64 ptr_to_u64(const void *ptr)
396{
397 return (__u64) (unsigned long) ptr;
398}
399
400static inline void *u64_to_ptr(__u64 ptr)
401{
402 return (void *) (unsigned long) ptr;
403}
404
405int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
406int compare_map_keys(int map1_fd, int map2_fd);
407int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
408int trigger_module_test_read(int read_sz);
409int trigger_module_test_write(int write_sz);
410int write_sysctl(const char *sysctl, const char *value);
411int get_bpf_max_tramp_links_from(struct btf *btf);
412int get_bpf_max_tramp_links(void);
413
414#ifdef __x86_64__
415#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
416#elif defined(__s390x__)
417#define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
418#elif defined(__aarch64__)
419#define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
420#elif defined(__riscv)
421#define SYS_NANOSLEEP_KPROBE_NAME "__riscv_sys_nanosleep"
422#else
423#define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
424#endif
425
426#define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
427
428typedef int (*pre_execution_cb)(struct bpf_object *obj);
429
430struct test_loader {
431 char *log_buf;
432 size_t log_buf_sz;
433 size_t next_match_pos;
434 pre_execution_cb pre_execution_cb;
435
436 struct bpf_object *obj;
437};
438
439static inline void test_loader__set_pre_execution_cb(struct test_loader *tester,
440 pre_execution_cb cb)
441{
442 tester->pre_execution_cb = cb;
443}
444
445typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
446
447extern void test_loader__run_subtests(struct test_loader *tester,
448 const char *skel_name,
449 skel_elf_bytes_fn elf_bytes_factory);
450
451extern void test_loader_fini(struct test_loader *tester);
452
453#define RUN_TESTS(skel) ({ \
454 struct test_loader tester = {}; \
455 \
456 test_loader__run_subtests(&tester, #skel, skel##__elf_bytes); \
457 test_loader_fini(&tester); \
458})
459
460#endif /* __TEST_PROGS_H */