Skip to content

Commit e043325

Browse files
Chenbo Fengdavem330
authored andcommitted
bpf: Add tests for eBPF file mode
Two related tests are added into bpf selftest to test read only map and write only map. The tests verified the read only and write only flags are working on hash maps. Signed-off-by: Chenbo Feng <fengc@google.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6e71b04 commit e043325

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tools/testing/selftests/bpf/test_maps.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,51 @@ static void test_map_parallel(void)
10331033
assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
10341034
}
10351035

1036+
static void test_map_rdonly(void)
1037+
{
1038+
int i, fd, key = 0, value = 0;
1039+
1040+
fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1041+
MAP_SIZE, map_flags | BPF_F_RDONLY);
1042+
if (fd < 0) {
1043+
printf("Failed to create map for read only test '%s'!\n",
1044+
strerror(errno));
1045+
exit(1);
1046+
}
1047+
1048+
key = 1;
1049+
value = 1234;
1050+
/* Insert key=1 element. */
1051+
assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 &&
1052+
errno == EPERM);
1053+
1054+
/* Check that key=2 is not found. */
1055+
assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
1056+
assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT);
1057+
}
1058+
1059+
static void test_map_wronly(void)
1060+
{
1061+
int i, fd, key = 0, value = 0;
1062+
1063+
fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
1064+
MAP_SIZE, map_flags | BPF_F_WRONLY);
1065+
if (fd < 0) {
1066+
printf("Failed to create map for read only test '%s'!\n",
1067+
strerror(errno));
1068+
exit(1);
1069+
}
1070+
1071+
key = 1;
1072+
value = 1234;
1073+
/* Insert key=1 element. */
1074+
assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0)
1075+
1076+
/* Check that key=2 is not found. */
1077+
assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM);
1078+
assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM);
1079+
}
1080+
10361081
static void run_all_tests(void)
10371082
{
10381083
test_hashmap(0, NULL);
@@ -1050,6 +1095,9 @@ static void run_all_tests(void)
10501095
test_map_large();
10511096
test_map_parallel();
10521097
test_map_stress();
1098+
1099+
test_map_rdonly();
1100+
test_map_wronly();
10531101
}
10541102

10551103
int main(void)

0 commit comments

Comments
 (0)