]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
selftests/bpf: Add ASSERT_OK_FD macro
authorGeliang Tang <tanggeliang@kylinos.cn>
Tue, 9 Jul 2024 09:16:18 +0000 (17:16 +0800)
committerMartin KaFai Lau <martin.lau@kernel.org>
Wed, 10 Jul 2024 18:56:22 +0000 (11:56 -0700)
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>
tools/testing/selftests/bpf/test_progs.h

index 0ba5a20b19ba8e8e0ec3f7ca774ba847c1cb3320..51341d50213b9dac024039257b9810055a9c8f98 100644 (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];                                         \