xref: /linux/tools/testing/selftests/bpf/prog_tests/map_excl.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2025 Google LLC. */
3 #define _GNU_SOURCE
4 #include <unistd.h>
5 #include <sys/syscall.h>
6 #include <test_progs.h>
7 #include <bpf/btf.h>
8 
9 #include "map_excl.skel.h"
10 
11 static void test_map_excl_allowed(void)
12 {
13 	struct map_excl *skel = map_excl__open();
14 	int err;
15 
16 	err = bpf_map__set_exclusive_program(skel->maps.excl_map, skel->progs.should_have_access);
17 	if (!ASSERT_OK(err, "bpf_map__set_exclusive_program"))
18 		goto out;
19 
20 	bpf_program__set_autoload(skel->progs.should_have_access, true);
21 	bpf_program__set_autoload(skel->progs.should_not_have_access, false);
22 
23 	err = map_excl__load(skel);
24 	ASSERT_OK(err, "map_excl__load");
25 out:
26 	map_excl__destroy(skel);
27 }
28 
29 static void test_map_excl_denied(void)
30 {
31 	struct map_excl *skel = map_excl__open();
32 	int err;
33 
34 	err = bpf_map__set_exclusive_program(skel->maps.excl_map, skel->progs.should_have_access);
35 	if (!ASSERT_OK(err, "bpf_map__make_exclusive"))
36 		goto out;
37 
38 	bpf_program__set_autoload(skel->progs.should_have_access, false);
39 	bpf_program__set_autoload(skel->progs.should_not_have_access, true);
40 
41 	err = map_excl__load(skel);
42 	ASSERT_EQ(err, -EACCES, "exclusive map access not denied\n");
43 out:
44 	map_excl__destroy(skel);
45 
46 }
47 
48 void test_map_excl(void)
49 {
50 	if (test__start_subtest("map_excl_allowed"))
51 		test_map_excl_allowed();
52 	if (test__start_subtest("map_excl_denied"))
53 		test_map_excl_denied();
54 }
55