xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/map_ops.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1*830154cdSJP Kobryn // SPDX-License-Identifier: GPL-2.0
2*830154cdSJP Kobryn /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3*830154cdSJP Kobryn 
4*830154cdSJP Kobryn #include <errno.h>
5*830154cdSJP Kobryn #include <sys/syscall.h>
6*830154cdSJP Kobryn #include <unistd.h>
7*830154cdSJP Kobryn 
8*830154cdSJP Kobryn #include "test_map_ops.skel.h"
9*830154cdSJP Kobryn #include "test_progs.h"
10*830154cdSJP Kobryn 
map_update(void)11*830154cdSJP Kobryn static void map_update(void)
12*830154cdSJP Kobryn {
13*830154cdSJP Kobryn 	(void)syscall(__NR_getpid);
14*830154cdSJP Kobryn }
15*830154cdSJP Kobryn 
map_delete(void)16*830154cdSJP Kobryn static void map_delete(void)
17*830154cdSJP Kobryn {
18*830154cdSJP Kobryn 	(void)syscall(__NR_getppid);
19*830154cdSJP Kobryn }
20*830154cdSJP Kobryn 
map_push(void)21*830154cdSJP Kobryn static void map_push(void)
22*830154cdSJP Kobryn {
23*830154cdSJP Kobryn 	(void)syscall(__NR_getuid);
24*830154cdSJP Kobryn }
25*830154cdSJP Kobryn 
map_pop(void)26*830154cdSJP Kobryn static void map_pop(void)
27*830154cdSJP Kobryn {
28*830154cdSJP Kobryn 	(void)syscall(__NR_geteuid);
29*830154cdSJP Kobryn }
30*830154cdSJP Kobryn 
map_peek(void)31*830154cdSJP Kobryn static void map_peek(void)
32*830154cdSJP Kobryn {
33*830154cdSJP Kobryn 	(void)syscall(__NR_getgid);
34*830154cdSJP Kobryn }
35*830154cdSJP Kobryn 
map_for_each_pass(void)36*830154cdSJP Kobryn static void map_for_each_pass(void)
37*830154cdSJP Kobryn {
38*830154cdSJP Kobryn 	(void)syscall(__NR_gettid);
39*830154cdSJP Kobryn }
40*830154cdSJP Kobryn 
map_for_each_fail(void)41*830154cdSJP Kobryn static void map_for_each_fail(void)
42*830154cdSJP Kobryn {
43*830154cdSJP Kobryn 	(void)syscall(__NR_getpgid);
44*830154cdSJP Kobryn }
45*830154cdSJP Kobryn 
setup(struct test_map_ops ** skel)46*830154cdSJP Kobryn static int setup(struct test_map_ops **skel)
47*830154cdSJP Kobryn {
48*830154cdSJP Kobryn 	int err = 0;
49*830154cdSJP Kobryn 
50*830154cdSJP Kobryn 	if (!skel)
51*830154cdSJP Kobryn 		return -1;
52*830154cdSJP Kobryn 
53*830154cdSJP Kobryn 	*skel = test_map_ops__open();
54*830154cdSJP Kobryn 	if (!ASSERT_OK_PTR(*skel, "test_map_ops__open"))
55*830154cdSJP Kobryn 		return -1;
56*830154cdSJP Kobryn 
57*830154cdSJP Kobryn 	(*skel)->rodata->pid = getpid();
58*830154cdSJP Kobryn 
59*830154cdSJP Kobryn 	err = test_map_ops__load(*skel);
60*830154cdSJP Kobryn 	if (!ASSERT_OK(err, "test_map_ops__load"))
61*830154cdSJP Kobryn 		return err;
62*830154cdSJP Kobryn 
63*830154cdSJP Kobryn 	err = test_map_ops__attach(*skel);
64*830154cdSJP Kobryn 	if (!ASSERT_OK(err, "test_map_ops__attach"))
65*830154cdSJP Kobryn 		return err;
66*830154cdSJP Kobryn 
67*830154cdSJP Kobryn 	return err;
68*830154cdSJP Kobryn }
69*830154cdSJP Kobryn 
teardown(struct test_map_ops ** skel)70*830154cdSJP Kobryn static void teardown(struct test_map_ops **skel)
71*830154cdSJP Kobryn {
72*830154cdSJP Kobryn 	if (skel && *skel)
73*830154cdSJP Kobryn 		test_map_ops__destroy(*skel);
74*830154cdSJP Kobryn }
75*830154cdSJP Kobryn 
map_ops_update_delete_subtest(void)76*830154cdSJP Kobryn static void map_ops_update_delete_subtest(void)
77*830154cdSJP Kobryn {
78*830154cdSJP Kobryn 	struct test_map_ops *skel;
79*830154cdSJP Kobryn 
80*830154cdSJP Kobryn 	if (setup(&skel))
81*830154cdSJP Kobryn 		goto teardown;
82*830154cdSJP Kobryn 
83*830154cdSJP Kobryn 	map_update();
84*830154cdSJP Kobryn 	ASSERT_OK(skel->bss->err, "map_update_initial");
85*830154cdSJP Kobryn 
86*830154cdSJP Kobryn 	map_update();
87*830154cdSJP Kobryn 	ASSERT_LT(skel->bss->err, 0, "map_update_existing");
88*830154cdSJP Kobryn 	ASSERT_EQ(skel->bss->err, -EEXIST, "map_update_existing");
89*830154cdSJP Kobryn 
90*830154cdSJP Kobryn 	map_delete();
91*830154cdSJP Kobryn 	ASSERT_OK(skel->bss->err, "map_delete_existing");
92*830154cdSJP Kobryn 
93*830154cdSJP Kobryn 	map_delete();
94*830154cdSJP Kobryn 	ASSERT_LT(skel->bss->err, 0, "map_delete_non_existing");
95*830154cdSJP Kobryn 	ASSERT_EQ(skel->bss->err, -ENOENT, "map_delete_non_existing");
96*830154cdSJP Kobryn 
97*830154cdSJP Kobryn teardown:
98*830154cdSJP Kobryn 	teardown(&skel);
99*830154cdSJP Kobryn }
100*830154cdSJP Kobryn 
map_ops_push_peek_pop_subtest(void)101*830154cdSJP Kobryn static void map_ops_push_peek_pop_subtest(void)
102*830154cdSJP Kobryn {
103*830154cdSJP Kobryn 	struct test_map_ops *skel;
104*830154cdSJP Kobryn 
105*830154cdSJP Kobryn 	if (setup(&skel))
106*830154cdSJP Kobryn 		goto teardown;
107*830154cdSJP Kobryn 
108*830154cdSJP Kobryn 	map_push();
109*830154cdSJP Kobryn 	ASSERT_OK(skel->bss->err, "map_push_initial");
110*830154cdSJP Kobryn 
111*830154cdSJP Kobryn 	map_push();
112*830154cdSJP Kobryn 	ASSERT_LT(skel->bss->err, 0, "map_push_when_full");
113*830154cdSJP Kobryn 	ASSERT_EQ(skel->bss->err, -E2BIG, "map_push_when_full");
114*830154cdSJP Kobryn 
115*830154cdSJP Kobryn 	map_peek();
116*830154cdSJP Kobryn 	ASSERT_OK(skel->bss->err, "map_peek");
117*830154cdSJP Kobryn 
118*830154cdSJP Kobryn 	map_pop();
119*830154cdSJP Kobryn 	ASSERT_OK(skel->bss->err, "map_pop");
120*830154cdSJP Kobryn 
121*830154cdSJP Kobryn 	map_peek();
122*830154cdSJP Kobryn 	ASSERT_LT(skel->bss->err, 0, "map_peek_when_empty");
123*830154cdSJP Kobryn 	ASSERT_EQ(skel->bss->err, -ENOENT, "map_peek_when_empty");
124*830154cdSJP Kobryn 
125*830154cdSJP Kobryn 	map_pop();
126*830154cdSJP Kobryn 	ASSERT_LT(skel->bss->err, 0, "map_pop_when_empty");
127*830154cdSJP Kobryn 	ASSERT_EQ(skel->bss->err, -ENOENT, "map_pop_when_empty");
128*830154cdSJP Kobryn 
129*830154cdSJP Kobryn teardown:
130*830154cdSJP Kobryn 	teardown(&skel);
131*830154cdSJP Kobryn }
132*830154cdSJP Kobryn 
map_ops_for_each_subtest(void)133*830154cdSJP Kobryn static void map_ops_for_each_subtest(void)
134*830154cdSJP Kobryn {
135*830154cdSJP Kobryn 	struct test_map_ops *skel;
136*830154cdSJP Kobryn 
137*830154cdSJP Kobryn 	if (setup(&skel))
138*830154cdSJP Kobryn 		goto teardown;
139*830154cdSJP Kobryn 
140*830154cdSJP Kobryn 	map_for_each_pass();
141*830154cdSJP Kobryn 	/* expect to iterate over 1 element */
142*830154cdSJP Kobryn 	ASSERT_EQ(skel->bss->err, 1, "map_for_each_no_flags");
143*830154cdSJP Kobryn 
144*830154cdSJP Kobryn 	map_for_each_fail();
145*830154cdSJP Kobryn 	ASSERT_LT(skel->bss->err, 0, "map_for_each_with_flags");
146*830154cdSJP Kobryn 	ASSERT_EQ(skel->bss->err, -EINVAL, "map_for_each_with_flags");
147*830154cdSJP Kobryn 
148*830154cdSJP Kobryn teardown:
149*830154cdSJP Kobryn 	teardown(&skel);
150*830154cdSJP Kobryn }
151*830154cdSJP Kobryn 
test_map_ops(void)152*830154cdSJP Kobryn void test_map_ops(void)
153*830154cdSJP Kobryn {
154*830154cdSJP Kobryn 	if (test__start_subtest("map_ops_update_delete"))
155*830154cdSJP Kobryn 		map_ops_update_delete_subtest();
156*830154cdSJP Kobryn 
157*830154cdSJP Kobryn 	if (test__start_subtest("map_ops_push_peek_pop"))
158*830154cdSJP Kobryn 		map_ops_push_peek_pop_subtest();
159*830154cdSJP Kobryn 
160*830154cdSJP Kobryn 	if (test__start_subtest("map_ops_for_each"))
161*830154cdSJP Kobryn 		map_ops_for_each_subtest();
162*830154cdSJP Kobryn }
163