Skip to content

Commit fac85c2

Browse files
yuranpereiraAlexei Starovoitov
authored andcommitted
selftests/bpf: Convert CHECK macros to ASSERT_* macros in bpf_iter
As it was pointed out by Yonghong Song [1], in the bpf selftests the use of the ASSERT_* series of macros is preferred over the CHECK macro. This patch replaces all CHECK calls in bpf_iter with the appropriate ASSERT_* macros. [1] https://lore.kernel.org/lkml/[email protected] Suggested-by: Yonghong Song <[email protected]> Signed-off-by: Yuran Pereira <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Kui-Feng Lee <[email protected]> Link: https://lore.kernel.org/r/DB3PR10MB6835E9C8DFCA226DD6FEF914E8A3A@DB3PR10MB6835.EURPRD10.PROD.OUTLOOK.COM Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 89cdf9d commit fac85c2

File tree

1 file changed

+35
-44
lines changed

1 file changed

+35
-44
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_iter.c

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
#include "bpf_iter_ksym.skel.h"
3535
#include "bpf_iter_sockmap.skel.h"
3636

37-
static int duration;
38-
3937
static void test_btf_id_or_null(void)
4038
{
4139
struct bpf_iter_test_kern3 *skel;
@@ -64,7 +62,7 @@ static void do_dummy_read_opts(struct bpf_program *prog, struct bpf_iter_attach_
6462
/* not check contents, but ensure read() ends without error */
6563
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
6664
;
67-
CHECK(len < 0, "read", "read failed: %s\n", strerror(errno));
65+
ASSERT_GE(len, 0, "read");
6866

6967
close(iter_fd);
7068

@@ -413,7 +411,7 @@ static int do_btf_read(struct bpf_iter_task_btf *skel)
413411
goto free_link;
414412
}
415413

416-
if (CHECK(err < 0, "read", "read failed: %s\n", strerror(errno)))
414+
if (!ASSERT_GE(err, 0, "read"))
417415
goto free_link;
418416

419417
ASSERT_HAS_SUBSTR(taskbuf, "(struct task_struct)",
@@ -526,11 +524,11 @@ static int do_read_with_fd(int iter_fd, const char *expected,
526524
start = 0;
527525
while ((len = read(iter_fd, buf + start, read_buf_len)) > 0) {
528526
start += len;
529-
if (CHECK(start >= 16, "read", "read len %d\n", len))
527+
if (!ASSERT_LT(start, 16, "read"))
530528
return -1;
531529
read_buf_len = read_one_char ? 1 : 16 - start;
532530
}
533-
if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
531+
if (!ASSERT_GE(len, 0, "read"))
534532
return -1;
535533

536534
if (!ASSERT_STREQ(buf, expected, "read"))
@@ -571,8 +569,7 @@ static int do_read(const char *path, const char *expected)
571569
int err, iter_fd;
572570

573571
iter_fd = open(path, O_RDONLY);
574-
if (CHECK(iter_fd < 0, "open", "open %s failed: %s\n",
575-
path, strerror(errno)))
572+
if (!ASSERT_GE(iter_fd, 0, "open"))
576573
return -1;
577574

578575
err = do_read_with_fd(iter_fd, expected, false);
@@ -600,7 +597,7 @@ static void test_file_iter(void)
600597
unlink(path);
601598

602599
err = bpf_link__pin(link, path);
603-
if (CHECK(err, "pin_iter", "pin_iter to %s failed: %d\n", path, err))
600+
if (!ASSERT_OK(err, "pin_iter"))
604601
goto free_link;
605602

606603
err = do_read(path, "abcd");
@@ -651,12 +648,10 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
651648
* overflow and needs restart.
652649
*/
653650
map1_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
654-
if (CHECK(map1_fd < 0, "bpf_map_create",
655-
"map_creation failed: %s\n", strerror(errno)))
651+
if (!ASSERT_GE(map1_fd, 0, "bpf_map_create"))
656652
goto out;
657653
map2_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
658-
if (CHECK(map2_fd < 0, "bpf_map_create",
659-
"map_creation failed: %s\n", strerror(errno)))
654+
if (!ASSERT_GE(map2_fd, 0, "bpf_map_create"))
660655
goto free_map1;
661656

662657
/* bpf_seq_printf kernel buffer is 8 pages, so one map
@@ -685,14 +680,12 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
685680
/* setup filtering map_id in bpf program */
686681
map_info_len = sizeof(map_info);
687682
err = bpf_map_get_info_by_fd(map1_fd, &map_info, &map_info_len);
688-
if (CHECK(err, "get_map_info", "get map info failed: %s\n",
689-
strerror(errno)))
683+
if (!ASSERT_OK(err, "get_map_info"))
690684
goto free_map2;
691685
skel->bss->map1_id = map_info.id;
692686

693687
err = bpf_map_get_info_by_fd(map2_fd, &map_info, &map_info_len);
694-
if (CHECK(err, "get_map_info", "get map info failed: %s\n",
695-
strerror(errno)))
688+
if (!ASSERT_OK(err, "get_map_info"))
696689
goto free_map2;
697690
skel->bss->map2_id = map_info.id;
698691

@@ -714,16 +707,14 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
714707
while ((len = read(iter_fd, buf, expected_read_len)) > 0)
715708
total_read_len += len;
716709

717-
CHECK(len != -1 || errno != E2BIG, "read",
718-
"expected ret -1, errno E2BIG, but get ret %d, error %s\n",
719-
len, strerror(errno));
710+
ASSERT_EQ(len, -1, "read");
711+
ASSERT_EQ(errno, E2BIG, "read");
720712
goto free_buf;
721713
} else if (!ret1) {
722714
while ((len = read(iter_fd, buf, expected_read_len)) > 0)
723715
total_read_len += len;
724716

725-
if (CHECK(len < 0, "read", "read failed: %s\n",
726-
strerror(errno)))
717+
if (!ASSERT_GE(len, 0, "read"))
727718
goto free_buf;
728719
} else {
729720
do {
@@ -732,8 +723,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
732723
total_read_len += len;
733724
} while (len > 0 || len == -EAGAIN);
734725

735-
if (CHECK(len < 0, "read", "read failed: %s\n",
736-
strerror(errno)))
726+
if (!ASSERT_GE(len, 0, "read"))
737727
goto free_buf;
738728
}
739729

@@ -836,7 +826,7 @@ static void test_bpf_hash_map(void)
836826
/* do some tests */
837827
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
838828
;
839-
if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
829+
if (!ASSERT_GE(len, 0, "read"))
840830
goto close_iter;
841831

842832
/* test results */
@@ -917,7 +907,7 @@ static void test_bpf_percpu_hash_map(void)
917907
/* do some tests */
918908
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
919909
;
920-
if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
910+
if (!ASSERT_GE(len, 0, "read"))
921911
goto close_iter;
922912

923913
/* test results */
@@ -983,17 +973,14 @@ static void test_bpf_array_map(void)
983973
start = 0;
984974
while ((len = read(iter_fd, buf + start, sizeof(buf) - start)) > 0)
985975
start += len;
986-
if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
976+
if (!ASSERT_GE(len, 0, "read"))
987977
goto close_iter;
988978

989979
/* test results */
990980
res_first_key = *(__u32 *)buf;
991981
res_first_val = *(__u64 *)(buf + sizeof(__u32));
992-
if (CHECK(res_first_key != 0 || res_first_val != first_val,
993-
"bpf_seq_write",
994-
"seq_write failure: first key %u vs expected 0, "
995-
" first value %llu vs expected %llu\n",
996-
res_first_key, res_first_val, first_val))
982+
if (!ASSERT_EQ(res_first_key, 0, "bpf_seq_write") ||
983+
!ASSERT_EQ(res_first_val, first_val, "bpf_seq_write"))
997984
goto close_iter;
998985

999986
if (!ASSERT_EQ(skel->bss->key_sum, expected_key, "key_sum"))
@@ -1092,7 +1079,7 @@ static void test_bpf_percpu_array_map(void)
10921079
/* do some tests */
10931080
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
10941081
;
1095-
if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
1082+
if (!ASSERT_GE(len, 0, "read"))
10961083
goto close_iter;
10971084

10981085
/* test results */
@@ -1131,6 +1118,7 @@ static void test_bpf_sk_storage_delete(void)
11311118
sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
11321119
if (!ASSERT_GE(sock_fd, 0, "socket"))
11331120
goto out;
1121+
11341122
err = bpf_map_update_elem(map_fd, &sock_fd, &val, BPF_NOEXIST);
11351123
if (!ASSERT_OK(err, "map_update"))
11361124
goto out;
@@ -1151,14 +1139,19 @@ static void test_bpf_sk_storage_delete(void)
11511139
/* do some tests */
11521140
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
11531141
;
1154-
if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
1142+
if (!ASSERT_GE(len, 0, "read"))
11551143
goto close_iter;
11561144

11571145
/* test results */
11581146
err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
1159-
if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
1160-
"map value wasn't deleted (err=%d, errno=%d)\n", err, errno))
1161-
goto close_iter;
1147+
1148+
/* Note: The following assertions serve to ensure
1149+
* the value was deleted. It does so by asserting
1150+
* that bpf_map_lookup_elem has failed. This might
1151+
* seem counterintuitive at first.
1152+
*/
1153+
ASSERT_ERR(err, "bpf_map_lookup_elem");
1154+
ASSERT_EQ(errno, ENOENT, "bpf_map_lookup_elem");
11621155

11631156
close_iter:
11641157
close(iter_fd);
@@ -1203,17 +1196,15 @@ static void test_bpf_sk_storage_get(void)
12031196
do_dummy_read(skel->progs.fill_socket_owner);
12041197

12051198
err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
1206-
if (CHECK(err || val != getpid(), "bpf_map_lookup_elem",
1207-
"map value wasn't set correctly (expected %d, got %d, err=%d)\n",
1208-
getpid(), val, err))
1199+
if (!ASSERT_OK(err, "bpf_map_lookup_elem") ||
1200+
!ASSERT_EQ(val, getpid(), "bpf_map_lookup_elem"))
12091201
goto close_socket;
12101202

12111203
do_dummy_read(skel->progs.negate_socket_local_storage);
12121204

12131205
err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
1214-
CHECK(err || val != -getpid(), "bpf_map_lookup_elem",
1215-
"map value wasn't set correctly (expected %d, got %d, err=%d)\n",
1216-
-getpid(), val, err);
1206+
ASSERT_OK(err, "bpf_map_lookup_elem");
1207+
ASSERT_EQ(val, -getpid(), "bpf_map_lookup_elem");
12171208

12181209
close_socket:
12191210
close(sock_fd);
@@ -1290,7 +1281,7 @@ static void test_bpf_sk_storage_map(void)
12901281
/* do some tests */
12911282
while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
12921283
;
1293-
if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
1284+
if (!ASSERT_GE(len, 0, "read"))
12941285
goto close_iter;
12951286

12961287
/* test results */

0 commit comments

Comments
 (0)