Skip to content

Commit

Permalink
selftests/bpf: Convert tcp_hdr_options test to ASSERT_* macros
Browse files Browse the repository at this point in the history
Convert the selftest to use the preferred ASSERT_* macros instead of the
deprecated CHECK().

Signed-off-by: Wang Yufen <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
wangyufen316 authored and anakryiko committed Sep 29, 2022
1 parent a0a1729 commit 3082f8c
Showing 1 changed file with 28 additions and 52 deletions.
80 changes: 28 additions & 52 deletions tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ struct sk_fds {

static int create_netns(void)
{
if (CHECK(unshare(CLONE_NEWNET), "create netns",
"unshare(CLONE_NEWNET): %s (%d)",
strerror(errno), errno))
if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns"))
return -1;

if (CHECK(system("ip link set dev lo up"), "run ip cmd",
"failed to bring lo link up\n"))
if (!ASSERT_OK(system("ip link set dev lo up"), "run ip cmd"))
return -1;

return 0;
Expand Down Expand Up @@ -80,16 +77,12 @@ static int sk_fds_shutdown(struct sk_fds *sk_fds)

shutdown(sk_fds->active_fd, SHUT_WR);
ret = read(sk_fds->passive_fd, &abyte, sizeof(abyte));
if (CHECK(ret != 0, "read-after-shutdown(passive_fd):",
"ret:%d %s (%d)\n",
ret, strerror(errno), errno))
if (!ASSERT_EQ(ret, 0, "read-after-shutdown(passive_fd):"))
return -1;

shutdown(sk_fds->passive_fd, SHUT_WR);
ret = read(sk_fds->active_fd, &abyte, sizeof(abyte));
if (CHECK(ret != 0, "read-after-shutdown(active_fd):",
"ret:%d %s (%d)\n",
ret, strerror(errno), errno))
if (!ASSERT_EQ(ret, 0, "read-after-shutdown(active_fd):"))
return -1;

return 0;
Expand All @@ -102,8 +95,7 @@ static int sk_fds_connect(struct sk_fds *sk_fds, bool fast_open)
socklen_t len;

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",
strerror(errno), errno))
if (!ASSERT_NEQ(sk_fds->srv_fd, -1, "start_server"))
goto error;

if (fast_open)
Expand All @@ -112,37 +104,33 @@ static int sk_fds_connect(struct sk_fds *sk_fds, bool fast_open)
else
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);
goto error;
}

len = sizeof(addr6);
if (CHECK(getsockname(sk_fds->srv_fd, (struct sockaddr *)&addr6,
&len), "getsockname(srv_fd)", "%s (%d)\n",
strerror(errno), errno))
if (!ASSERT_OK(getsockname(sk_fds->srv_fd, (struct sockaddr *)&addr6,
&len), "getsockname(srv_fd)"))
goto error_close;
sk_fds->passive_lport = ntohs(addr6.sin6_port);

len = sizeof(addr6);
if (CHECK(getsockname(sk_fds->active_fd, (struct sockaddr *)&addr6,
&len), "getsockname(active_fd)", "%s (%d)\n",
strerror(errno), errno))
if (!ASSERT_OK(getsockname(sk_fds->active_fd, (struct sockaddr *)&addr6,
&len), "getsockname(active_fd)"))
goto error_close;
sk_fds->active_lport = ntohs(addr6.sin6_port);

sk_fds->passive_fd = accept(sk_fds->srv_fd, NULL, 0);
if (CHECK(sk_fds->passive_fd == -1, "accept(srv_fd)", "%s (%d)\n",
strerror(errno), errno))
if (!ASSERT_NEQ(sk_fds->passive_fd, -1, "accept(srv_fd)"))
goto error_close;

if (fast_open) {
char bytes_in[sizeof(fast)];
int ret;

ret = read(sk_fds->passive_fd, bytes_in, sizeof(bytes_in));
if (CHECK(ret != sizeof(fast), "read fastopen syn data",
"expected=%lu actual=%d\n", sizeof(fast), ret)) {
if (!ASSERT_EQ(ret, sizeof(fast), "read fastopen syn data")) {
close(sk_fds->passive_fd);
goto error_close;
}
Expand All @@ -163,8 +151,7 @@ static int check_hdr_opt(const struct bpf_test_option *exp,
const struct bpf_test_option *act,
const char *hdr_desc)
{
if (CHECK(memcmp(exp, act, sizeof(*exp)),
"expected-vs-actual", "unexpected %s\n", hdr_desc)) {
if (!ASSERT_OK(memcmp(exp, act, sizeof(*exp)), hdr_desc)) {
print_option(exp, "expected: ");
print_option(act, " actual: ");
return -1;
Expand All @@ -178,13 +165,11 @@ static int check_hdr_stg(const struct hdr_stg *exp, int fd,
{
struct hdr_stg act;

if (CHECK(bpf_map_lookup_elem(hdr_stg_map_fd, &fd, &act),
"map_lookup(hdr_stg_map_fd)", "%s %s (%d)\n",
stg_desc, strerror(errno), errno))
if (!ASSERT_OK(bpf_map_lookup_elem(hdr_stg_map_fd, &fd, &act),
"map_lookup(hdr_stg_map_fd)"))
return -1;

if (CHECK(memcmp(exp, &act, sizeof(*exp)),
"expected-vs-actual", "unexpected %s\n", stg_desc)) {
if (!ASSERT_OK(memcmp(exp, &act, sizeof(*exp)), stg_desc)) {
print_hdr_stg(exp, "expected: ");
print_hdr_stg(&act, " actual: ");
return -1;
Expand Down Expand Up @@ -228,9 +213,8 @@ static void check_hdr_and_close_fds(struct sk_fds *sk_fds)
if (sk_fds_shutdown(sk_fds))
goto check_linum;

if (CHECK(expected_inherit_cb_flags != skel->bss->inherit_cb_flags,
"Unexpected inherit_cb_flags", "0x%x != 0x%x\n",
skel->bss->inherit_cb_flags, expected_inherit_cb_flags))
if (!ASSERT_EQ(expected_inherit_cb_flags, skel->bss->inherit_cb_flags,
"inherit_cb_flags"))
goto check_linum;

if (check_hdr_stg(&exp_passive_hdr_stg, sk_fds->passive_fd,
Expand All @@ -257,7 +241,7 @@ static void check_hdr_and_close_fds(struct sk_fds *sk_fds)
"active_fin_in");

check_linum:
CHECK_FAIL(check_error_linum(sk_fds));
ASSERT_FALSE(check_error_linum(sk_fds), "check_error_linum");
sk_fds_close(sk_fds);
}

Expand Down Expand Up @@ -497,26 +481,20 @@ static void misc(void)
/* MSG_EOR to ensure skb will not be combined */
ret = send(sk_fds.active_fd, send_msg, sizeof(send_msg),
MSG_EOR);
if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n",
ret))
if (!ASSERT_EQ(ret, sizeof(send_msg), "send(msg)"))
goto check_linum;

ret = read(sk_fds.passive_fd, recv_msg, sizeof(recv_msg));
if (CHECK(ret != sizeof(send_msg), "read(msg)", "ret:%d\n",
ret))
if (ASSERT_EQ(ret, sizeof(send_msg), "read(msg)"))
goto check_linum;
}

if (sk_fds_shutdown(&sk_fds))
goto check_linum;

CHECK(misc_skel->bss->nr_syn != 1, "unexpected nr_syn",
"expected (1) != actual (%u)\n",
misc_skel->bss->nr_syn);
ASSERT_EQ(misc_skel->bss->nr_syn, 1, "unexpected nr_syn");

CHECK(misc_skel->bss->nr_data != nr_data, "unexpected nr_data",
"expected (%u) != actual (%u)\n",
nr_data, misc_skel->bss->nr_data);
ASSERT_EQ(misc_skel->bss->nr_data, nr_data, "unexpected nr_data");

/* The last ACK may have been delayed, so it is either 1 or 2. */
CHECK(misc_skel->bss->nr_pure_ack != 1 &&
Expand All @@ -525,12 +503,10 @@ static void misc(void)
"expected (1 or 2) != actual (%u)\n",
misc_skel->bss->nr_pure_ack);

CHECK(misc_skel->bss->nr_fin != 1, "unexpected nr_fin",
"expected (1) != actual (%u)\n",
misc_skel->bss->nr_fin);
ASSERT_EQ(misc_skel->bss->nr_fin, 1, "unexpected nr_fin");

check_linum:
CHECK_FAIL(check_error_linum(&sk_fds));
ASSERT_FALSE(check_error_linum(&sk_fds), "check_error_linum");
sk_fds_close(&sk_fds);
bpf_link__destroy(link);
}
Expand All @@ -555,15 +531,15 @@ void test_tcp_hdr_options(void)
int i;

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;

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;

cg_fd = test__join_cgroup(CG_NAME);
if (CHECK_FAIL(cg_fd < 0))
if (ASSERT_GE(cg_fd, 0, "join_cgroup"))
goto skel_destroy;

for (i = 0; i < ARRAY_SIZE(tests); i++) {
Expand Down

0 comments on commit 3082f8c

Please sign in to comment.