Skip to content

Commit

Permalink
selftests/bpf: Convert tcpbpf_user 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 8dda32a commit 9d0b05b
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#define LO_ADDR6 "::1"
#define CG_NAME "/tcpbpf-user-test"

static __u32 duration;

static void verify_result(struct tcpbpf_globals *result)
{
__u32 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
Expand All @@ -22,9 +20,7 @@ static void verify_result(struct tcpbpf_globals *result)
(1 << BPF_SOCK_OPS_TCP_LISTEN_CB));

/* check global map */
CHECK(expected_events != result->event_map, "event_map",
"unexpected event_map: actual 0x%08x != expected 0x%08x\n",
result->event_map, expected_events);
ASSERT_EQ(expected_events, result->event_map, "event_map");

ASSERT_EQ(result->bytes_received, 501, "bytes_received");
ASSERT_EQ(result->bytes_acked, 1002, "bytes_acked");
Expand Down Expand Up @@ -56,42 +52,39 @@ static void run_test(struct tcpbpf_globals *result)
int i, rv;

listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
if (CHECK(listen_fd == -1, "start_server", "listen_fd:%d errno:%d\n",
listen_fd, errno))
if (!ASSERT_NEQ(listen_fd, -1, "start_server"))
goto done;

cli_fd = connect_to_fd(listen_fd, 0);
if (CHECK(cli_fd == -1, "connect_to_fd(listen_fd)",
"cli_fd:%d errno:%d\n", cli_fd, errno))
if (!ASSERT_NEQ(cli_fd, -1, "connect_to_fd(listen_fd)"))
goto done;

accept_fd = accept(listen_fd, NULL, NULL);
if (CHECK(accept_fd == -1, "accept(listen_fd)",
"accept_fd:%d errno:%d\n", accept_fd, errno))
if (!ASSERT_NEQ(accept_fd, -1, "accept(listen_fd)"))
goto done;

/* Send 1000B of '+'s from cli_fd -> accept_fd */
for (i = 0; i < 1000; i++)
buf[i] = '+';

rv = send(cli_fd, buf, 1000, 0);
if (CHECK(rv != 1000, "send(cli_fd)", "rv:%d errno:%d\n", rv, errno))
if (!ASSERT_EQ(rv, 1000, "send(cli_fd)"))
goto done;

rv = recv(accept_fd, buf, 1000, 0);
if (CHECK(rv != 1000, "recv(accept_fd)", "rv:%d errno:%d\n", rv, errno))
if (!ASSERT_EQ(rv, 1000, "recv(accept_fd)"))
goto done;

/* Send 500B of '.'s from accept_fd ->cli_fd */
for (i = 0; i < 500; i++)
buf[i] = '.';

rv = send(accept_fd, buf, 500, 0);
if (CHECK(rv != 500, "send(accept_fd)", "rv:%d errno:%d\n", rv, errno))
if (!ASSERT_EQ(rv, 500, "send(accept_fd)"))
goto done;

rv = recv(cli_fd, buf, 500, 0);
if (CHECK(rv != 500, "recv(cli_fd)", "rv:%d errno:%d\n", rv, errno))
if (!ASSERT_EQ(rv, 500, "recv(cli_fd)"))
goto done;

/*
Expand All @@ -100,12 +93,12 @@ static void run_test(struct tcpbpf_globals *result)
*/
shutdown(accept_fd, SHUT_WR);
err = recv(cli_fd, buf, 1, 0);
if (CHECK(err, "recv(cli_fd) for fin", "err:%d errno:%d\n", err, errno))
if (!ASSERT_OK(err, "recv(cli_fd) for fin"))
goto done;

shutdown(cli_fd, SHUT_WR);
err = recv(accept_fd, buf, 1, 0);
CHECK(err, "recv(accept_fd) for fin", "err:%d errno:%d\n", err, errno);
ASSERT_OK(err, "recv(accept_fd) for fin");
done:
if (accept_fd != -1)
close(accept_fd);
Expand All @@ -124,12 +117,11 @@ void test_tcpbpf_user(void)
int cg_fd = -1;

skel = test_tcpbpf_kern__open_and_load();
if (CHECK(!skel, "open and load skel", "failed"))
if (!ASSERT_OK_PTR(skel, "open and load skel"))
return;

cg_fd = test__join_cgroup(CG_NAME);
if (CHECK(cg_fd < 0, "test__join_cgroup(" CG_NAME ")",
"cg_fd:%d errno:%d", cg_fd, errno))
if (!ASSERT_GE(cg_fd, 0, "test__join_cgroup(" CG_NAME ")"))
goto err;

skel->links.bpf_testcb = bpf_program__attach_cgroup(skel->progs.bpf_testcb, cg_fd);
Expand Down

0 comments on commit 9d0b05b

Please sign in to comment.