selftests/bpf: Convert tcp_hdr_options test to ASSERT_* macros

Convert the selftest to use the preferred ASSERT_* macros instead of the
deprecated CHECK().

Signed-off-by: Wang Yufen <wangyufen@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/1664169131-32405-9-git-send-email-wangyufen@huawei.com
This commit is contained in:
Wang Yufen 2022-09-26 13:12:08 +08:00 коммит произвёл Andrii Nakryiko
Родитель a0a1729671
Коммит 3082f8cd4b
1 изменённых файлов: 28 добавлений и 52 удалений

Просмотреть файл

@ -42,13 +42,10 @@ struct sk_fds {
static int create_netns(void) static int create_netns(void)
{ {
if (CHECK(unshare(CLONE_NEWNET), "create netns", if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns"))
"unshare(CLONE_NEWNET): %s (%d)",
strerror(errno), errno))
return -1; return -1;
if (CHECK(system("ip link set dev lo up"), "run ip cmd", if (!ASSERT_OK(system("ip link set dev lo up"), "run ip cmd"))
"failed to bring lo link up\n"))
return -1; return -1;
return 0; return 0;
@ -80,16 +77,12 @@ static int sk_fds_shutdown(struct sk_fds *sk_fds)
shutdown(sk_fds->active_fd, SHUT_WR); shutdown(sk_fds->active_fd, SHUT_WR);
ret = read(sk_fds->passive_fd, &abyte, sizeof(abyte)); ret = read(sk_fds->passive_fd, &abyte, sizeof(abyte));
if (CHECK(ret != 0, "read-after-shutdown(passive_fd):", if (!ASSERT_EQ(ret, 0, "read-after-shutdown(passive_fd):"))
"ret:%d %s (%d)\n",
ret, strerror(errno), errno))
return -1; return -1;
shutdown(sk_fds->passive_fd, SHUT_WR); shutdown(sk_fds->passive_fd, SHUT_WR);
ret = read(sk_fds->active_fd, &abyte, sizeof(abyte)); ret = read(sk_fds->active_fd, &abyte, sizeof(abyte));
if (CHECK(ret != 0, "read-after-shutdown(active_fd):", if (!ASSERT_EQ(ret, 0, "read-after-shutdown(active_fd):"))
"ret:%d %s (%d)\n",
ret, strerror(errno), errno))
return -1; return -1;
return 0; return 0;
@ -102,8 +95,7 @@ static int sk_fds_connect(struct sk_fds *sk_fds, bool fast_open)
socklen_t len; socklen_t len;
sk_fds->srv_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0); sk_fds->srv_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
if (CHECK(sk_fds->srv_fd == -1, "start_server", "%s (%d)\n", if (!ASSERT_NEQ(sk_fds->srv_fd, -1, "start_server"))
strerror(errno), errno))
goto error; goto error;
if (fast_open) if (fast_open)
@ -112,28 +104,25 @@ static int sk_fds_connect(struct sk_fds *sk_fds, bool fast_open)
else else
sk_fds->active_fd = connect_to_fd(sk_fds->srv_fd, 0); sk_fds->active_fd = connect_to_fd(sk_fds->srv_fd, 0);
if (CHECK_FAIL(sk_fds->active_fd == -1)) { if (!ASSERT_NEQ(sk_fds->active_fd, -1, "")) {
close(sk_fds->srv_fd); close(sk_fds->srv_fd);
goto error; goto error;
} }
len = sizeof(addr6); len = sizeof(addr6);
if (CHECK(getsockname(sk_fds->srv_fd, (struct sockaddr *)&addr6, if (!ASSERT_OK(getsockname(sk_fds->srv_fd, (struct sockaddr *)&addr6,
&len), "getsockname(srv_fd)", "%s (%d)\n", &len), "getsockname(srv_fd)"))
strerror(errno), errno))
goto error_close; goto error_close;
sk_fds->passive_lport = ntohs(addr6.sin6_port); sk_fds->passive_lport = ntohs(addr6.sin6_port);
len = sizeof(addr6); len = sizeof(addr6);
if (CHECK(getsockname(sk_fds->active_fd, (struct sockaddr *)&addr6, if (!ASSERT_OK(getsockname(sk_fds->active_fd, (struct sockaddr *)&addr6,
&len), "getsockname(active_fd)", "%s (%d)\n", &len), "getsockname(active_fd)"))
strerror(errno), errno))
goto error_close; goto error_close;
sk_fds->active_lport = ntohs(addr6.sin6_port); sk_fds->active_lport = ntohs(addr6.sin6_port);
sk_fds->passive_fd = accept(sk_fds->srv_fd, NULL, 0); sk_fds->passive_fd = accept(sk_fds->srv_fd, NULL, 0);
if (CHECK(sk_fds->passive_fd == -1, "accept(srv_fd)", "%s (%d)\n", if (!ASSERT_NEQ(sk_fds->passive_fd, -1, "accept(srv_fd)"))
strerror(errno), errno))
goto error_close; goto error_close;
if (fast_open) { if (fast_open) {
@ -141,8 +130,7 @@ static int sk_fds_connect(struct sk_fds *sk_fds, bool fast_open)
int ret; int ret;
ret = read(sk_fds->passive_fd, bytes_in, sizeof(bytes_in)); ret = read(sk_fds->passive_fd, bytes_in, sizeof(bytes_in));
if (CHECK(ret != sizeof(fast), "read fastopen syn data", if (!ASSERT_EQ(ret, sizeof(fast), "read fastopen syn data")) {
"expected=%lu actual=%d\n", sizeof(fast), ret)) {
close(sk_fds->passive_fd); close(sk_fds->passive_fd);
goto error_close; goto error_close;
} }
@ -163,8 +151,7 @@ static int check_hdr_opt(const struct bpf_test_option *exp,
const struct bpf_test_option *act, const struct bpf_test_option *act,
const char *hdr_desc) const char *hdr_desc)
{ {
if (CHECK(memcmp(exp, act, sizeof(*exp)), if (!ASSERT_OK(memcmp(exp, act, sizeof(*exp)), hdr_desc)) {
"expected-vs-actual", "unexpected %s\n", hdr_desc)) {
print_option(exp, "expected: "); print_option(exp, "expected: ");
print_option(act, " actual: "); print_option(act, " actual: ");
return -1; return -1;
@ -178,13 +165,11 @@ static int check_hdr_stg(const struct hdr_stg *exp, int fd,
{ {
struct hdr_stg act; struct hdr_stg act;
if (CHECK(bpf_map_lookup_elem(hdr_stg_map_fd, &fd, &act), if (!ASSERT_OK(bpf_map_lookup_elem(hdr_stg_map_fd, &fd, &act),
"map_lookup(hdr_stg_map_fd)", "%s %s (%d)\n", "map_lookup(hdr_stg_map_fd)"))
stg_desc, strerror(errno), errno))
return -1; return -1;
if (CHECK(memcmp(exp, &act, sizeof(*exp)), if (!ASSERT_OK(memcmp(exp, &act, sizeof(*exp)), stg_desc)) {
"expected-vs-actual", "unexpected %s\n", stg_desc)) {
print_hdr_stg(exp, "expected: "); print_hdr_stg(exp, "expected: ");
print_hdr_stg(&act, " actual: "); print_hdr_stg(&act, " actual: ");
return -1; return -1;
@ -228,9 +213,8 @@ static void check_hdr_and_close_fds(struct sk_fds *sk_fds)
if (sk_fds_shutdown(sk_fds)) if (sk_fds_shutdown(sk_fds))
goto check_linum; goto check_linum;
if (CHECK(expected_inherit_cb_flags != skel->bss->inherit_cb_flags, if (!ASSERT_EQ(expected_inherit_cb_flags, skel->bss->inherit_cb_flags,
"Unexpected inherit_cb_flags", "0x%x != 0x%x\n", "inherit_cb_flags"))
skel->bss->inherit_cb_flags, expected_inherit_cb_flags))
goto check_linum; goto check_linum;
if (check_hdr_stg(&exp_passive_hdr_stg, sk_fds->passive_fd, if (check_hdr_stg(&exp_passive_hdr_stg, sk_fds->passive_fd,
@ -257,7 +241,7 @@ static void check_hdr_and_close_fds(struct sk_fds *sk_fds)
"active_fin_in"); "active_fin_in");
check_linum: check_linum:
CHECK_FAIL(check_error_linum(sk_fds)); ASSERT_FALSE(check_error_linum(sk_fds), "check_error_linum");
sk_fds_close(sk_fds); sk_fds_close(sk_fds);
} }
@ -497,26 +481,20 @@ static void misc(void)
/* MSG_EOR to ensure skb will not be combined */ /* MSG_EOR to ensure skb will not be combined */
ret = send(sk_fds.active_fd, send_msg, sizeof(send_msg), ret = send(sk_fds.active_fd, send_msg, sizeof(send_msg),
MSG_EOR); MSG_EOR);
if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n", if (!ASSERT_EQ(ret, sizeof(send_msg), "send(msg)"))
ret))
goto check_linum; goto check_linum;
ret = read(sk_fds.passive_fd, recv_msg, sizeof(recv_msg)); ret = read(sk_fds.passive_fd, recv_msg, sizeof(recv_msg));
if (CHECK(ret != sizeof(send_msg), "read(msg)", "ret:%d\n", if (ASSERT_EQ(ret, sizeof(send_msg), "read(msg)"))
ret))
goto check_linum; goto check_linum;
} }
if (sk_fds_shutdown(&sk_fds)) if (sk_fds_shutdown(&sk_fds))
goto check_linum; goto check_linum;
CHECK(misc_skel->bss->nr_syn != 1, "unexpected nr_syn", ASSERT_EQ(misc_skel->bss->nr_syn, 1, "unexpected nr_syn");
"expected (1) != actual (%u)\n",
misc_skel->bss->nr_syn);
CHECK(misc_skel->bss->nr_data != nr_data, "unexpected nr_data", ASSERT_EQ(misc_skel->bss->nr_data, nr_data, "unexpected nr_data");
"expected (%u) != actual (%u)\n",
nr_data, misc_skel->bss->nr_data);
/* The last ACK may have been delayed, so it is either 1 or 2. */ /* The last ACK may have been delayed, so it is either 1 or 2. */
CHECK(misc_skel->bss->nr_pure_ack != 1 && CHECK(misc_skel->bss->nr_pure_ack != 1 &&
@ -525,12 +503,10 @@ static void misc(void)
"expected (1 or 2) != actual (%u)\n", "expected (1 or 2) != actual (%u)\n",
misc_skel->bss->nr_pure_ack); misc_skel->bss->nr_pure_ack);
CHECK(misc_skel->bss->nr_fin != 1, "unexpected nr_fin", ASSERT_EQ(misc_skel->bss->nr_fin, 1, "unexpected nr_fin");
"expected (1) != actual (%u)\n",
misc_skel->bss->nr_fin);
check_linum: check_linum:
CHECK_FAIL(check_error_linum(&sk_fds)); ASSERT_FALSE(check_error_linum(&sk_fds), "check_error_linum");
sk_fds_close(&sk_fds); sk_fds_close(&sk_fds);
bpf_link__destroy(link); bpf_link__destroy(link);
} }
@ -555,15 +531,15 @@ void test_tcp_hdr_options(void)
int i; int i;
skel = test_tcp_hdr_options__open_and_load(); skel = test_tcp_hdr_options__open_and_load();
if (CHECK(!skel, "open and load skel", "failed")) if (!ASSERT_OK_PTR(skel, "open and load skel"))
return; return;
misc_skel = test_misc_tcp_hdr_options__open_and_load(); misc_skel = test_misc_tcp_hdr_options__open_and_load();
if (CHECK(!misc_skel, "open and load misc test skel", "failed")) if (!ASSERT_OK_PTR(misc_skel, "open and load misc test skel"))
goto skel_destroy; goto skel_destroy;
cg_fd = test__join_cgroup(CG_NAME); cg_fd = test__join_cgroup(CG_NAME);
if (CHECK_FAIL(cg_fd < 0)) if (ASSERT_GE(cg_fd, 0, "join_cgroup"))
goto skel_destroy; goto skel_destroy;
for (i = 0; i < ARRAY_SIZE(tests); i++) { for (i = 0; i < ARRAY_SIZE(tests); i++) {