selftests/bpf: Add ASSERT_OK_FD macro

Add a new dedicated ASSERT macro ASSERT_OK_FD to test whether a socket
FD is valid or not. It can be used to replace macros ASSERT_GT(fd, 0, ""),
ASSERT_NEQ(fd, -1, "") or statements (fd < 0), (fd != -1).

Suggested-by: Martin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/ded75be86ac630a3a5099739431854c1ec33f0ea.1720515893.git.tanggeliang@kylinos.cn
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
This commit is contained in:
Geliang Tang 2024-07-09 17:16:18 +08:00 committed by Martin KaFai Lau
parent a3016a27ce
commit 7046345d48

View file

@ -377,6 +377,15 @@ int test__join_cgroup(const char *path);
___ok; \
})
#define ASSERT_OK_FD(fd, name) ({ \
static int duration = 0; \
int ___fd = (fd); \
bool ___ok = ___fd >= 0; \
CHECK(!___ok, (name), "unexpected fd: %d (errno %d)\n", \
___fd, errno); \
___ok; \
})
#define SYS(goto_label, fmt, ...) \
({ \
char cmd[1024]; \