xref: /linux/tools/testing/selftests/bpf/prog_tests/iters.c (revision 1e0731c05c985deb68a97fa44c1adcd3305dda90)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3 
4 #include <test_progs.h>
5 
6 #include "iters.skel.h"
7 #include "iters_state_safety.skel.h"
8 #include "iters_looping.skel.h"
9 #include "iters_num.skel.h"
10 #include "iters_testmod_seq.skel.h"
11 
12 static void subtest_num_iters(void)
13 {
14 	struct iters_num *skel;
15 	int err;
16 
17 	skel = iters_num__open_and_load();
18 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
19 		return;
20 
21 	err = iters_num__attach(skel);
22 	if (!ASSERT_OK(err, "skel_attach"))
23 		goto cleanup;
24 
25 	usleep(1);
26 	iters_num__detach(skel);
27 
28 #define VALIDATE_CASE(case_name)					\
29 	ASSERT_EQ(skel->bss->res_##case_name,				\
30 		  skel->rodata->exp_##case_name,			\
31 		  #case_name)
32 
33 	VALIDATE_CASE(empty_zero);
34 	VALIDATE_CASE(empty_int_min);
35 	VALIDATE_CASE(empty_int_max);
36 	VALIDATE_CASE(empty_minus_one);
37 
38 	VALIDATE_CASE(simple_sum);
39 	VALIDATE_CASE(neg_sum);
40 	VALIDATE_CASE(very_neg_sum);
41 	VALIDATE_CASE(neg_pos_sum);
42 
43 	VALIDATE_CASE(invalid_range);
44 	VALIDATE_CASE(max_range);
45 	VALIDATE_CASE(e2big_range);
46 
47 	VALIDATE_CASE(succ_elem_cnt);
48 	VALIDATE_CASE(overfetched_elem_cnt);
49 	VALIDATE_CASE(fail_elem_cnt);
50 
51 #undef VALIDATE_CASE
52 
53 cleanup:
54 	iters_num__destroy(skel);
55 }
56 
57 static void subtest_testmod_seq_iters(void)
58 {
59 	struct iters_testmod_seq *skel;
60 	int err;
61 
62 	if (!env.has_testmod) {
63 		test__skip();
64 		return;
65 	}
66 
67 	skel = iters_testmod_seq__open_and_load();
68 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
69 		return;
70 
71 	err = iters_testmod_seq__attach(skel);
72 	if (!ASSERT_OK(err, "skel_attach"))
73 		goto cleanup;
74 
75 	usleep(1);
76 	iters_testmod_seq__detach(skel);
77 
78 #define VALIDATE_CASE(case_name)					\
79 	ASSERT_EQ(skel->bss->res_##case_name,				\
80 		  skel->rodata->exp_##case_name,			\
81 		  #case_name)
82 
83 	VALIDATE_CASE(empty);
84 	VALIDATE_CASE(full);
85 	VALIDATE_CASE(truncated);
86 
87 #undef VALIDATE_CASE
88 
89 cleanup:
90 	iters_testmod_seq__destroy(skel);
91 }
92 
93 void test_iters(void)
94 {
95 	RUN_TESTS(iters_state_safety);
96 	RUN_TESTS(iters_looping);
97 	RUN_TESTS(iters);
98 
99 	if (env.has_testmod)
100 		RUN_TESTS(iters_testmod_seq);
101 
102 	if (test__start_subtest("num"))
103 		subtest_num_iters();
104 	if (test__start_subtest("testmod_seq"))
105 		subtest_testmod_seq_iters();
106 }
107