diff mbox series

[bpf-next,v11,2/9] selftests/bpf: Add ASSERT_OK_FD macro

Message ID ded75be86ac630a3a5099739431854c1ec33f0ea.1720515893.git.tanggeliang@kylinos.cn
State New
Headers show
Series use network helpers, part 8 | expand

Commit Message

Geliang Tang July 9, 2024, 9:16 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

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>
---
 tools/testing/selftests/bpf/test_progs.h | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Martin KaFai Lau July 10, 2024, 7 p.m. UTC | #1
On 7/9/24 2:16 AM, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> 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>
> ---
>   tools/testing/selftests/bpf/test_progs.h | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
> index 0ba5a20b19ba..4f7b91c25b1e 100644
> --- a/tools/testing/selftests/bpf/test_progs.h
> +++ b/tools/testing/selftests/bpf/test_progs.h
> @@ -377,6 +377,14 @@ 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\n", ___fd);		\

printing errno should be useful.

> +	___ok;								\
> +})
> +
>   #define SYS(goto_label, fmt, ...)					\
>   	({								\
>   		char cmd[1024];						\
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index 0ba5a20b19ba..4f7b91c25b1e 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -377,6 +377,14 @@  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\n", ___fd);		\
+	___ok;								\
+})
+
 #define SYS(goto_label, fmt, ...)					\
 	({								\
 		char cmd[1024];						\