diff mbox series

[bpf-next,01/10] selftests/bpf: add a macro to compare raw memory

Message ID 20241113-flow_dissector-v1-1-27c4df0592dc@bootlin.com (mailing list archive)
State New
Headers show
Series selftests/bpf: migrate test_flow_dissector.sh to test_progs | expand

Commit Message

Alexis Lothoré Nov. 13, 2024, 1:53 p.m. UTC
We sometimes need to compare whole structures in an assert. It is
possible to use the existing macros on each field, but when the whole
structure has to be checked, it is more convenient to simply compare the
whole structure memory

Add a dedicated assert macro, ASSERT_MEMEQ, to allow bare memory
comparision

Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
---
 tools/testing/selftests/bpf/test_progs.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Comments

Stanislav Fomichev Nov. 13, 2024, 5:31 p.m. UTC | #1
On 11/13, Alexis Lothoré (eBPF Foundation) wrote:
> We sometimes need to compare whole structures in an assert. It is
> possible to use the existing macros on each field, but when the whole
> structure has to be checked, it is more convenient to simply compare the
> whole structure memory
> 
> Add a dedicated assert macro, ASSERT_MEMEQ, to allow bare memory
> comparision
> 
> Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
> ---
>  tools/testing/selftests/bpf/test_progs.h | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
> index 74de33ae37e56c90646cd1e0bb58ed7e3f345ec0..bdde741543836991398daacfe5423e6af8ef9151 100644
> --- a/tools/testing/selftests/bpf/test_progs.h
> +++ b/tools/testing/selftests/bpf/test_progs.h
> @@ -186,6 +186,19 @@ void test__skip(void);
>  void test__fail(void);
>  int test__join_cgroup(const char *path);

[..]
 
> +#define DUMP_BUFFER(name, buf, len)						\
> +	({									\
> +		fprintf(stdout, "%s:\n", name);					\
> +		for (int i = 0; i < len; i++) {					\
> +			if (i && !(i % 16))					\
> +				fprintf(stdout, "\n");				\
> +			if (i && !(i % 8) && (i % 16))				\
> +				fprintf(stdout, "\t");				\
> +			fprintf(stdout, "%02X ", ((uint8_t *)(buf))[i]);	\
> +		}								\
> +		fprintf(stdout, "\n");						\
> +	})

nit: should we rewrite this as a real function?

void hexdump(const char *prefix, void *buf, size_t len)
{
..
}
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
index 74de33ae37e56c90646cd1e0bb58ed7e3f345ec0..bdde741543836991398daacfe5423e6af8ef9151 100644
--- a/tools/testing/selftests/bpf/test_progs.h
+++ b/tools/testing/selftests/bpf/test_progs.h
@@ -186,6 +186,19 @@  void test__skip(void);
 void test__fail(void);
 int test__join_cgroup(const char *path);
 
+#define DUMP_BUFFER(name, buf, len)						\
+	({									\
+		fprintf(stdout, "%s:\n", name);					\
+		for (int i = 0; i < len; i++) {					\
+			if (i && !(i % 16))					\
+				fprintf(stdout, "\n");				\
+			if (i && !(i % 8) && (i % 16))				\
+				fprintf(stdout, "\t");				\
+			fprintf(stdout, "%02X ", ((uint8_t *)(buf))[i]);	\
+		}								\
+		fprintf(stdout, "\n");						\
+	})
+
 #define PRINT_FAIL(format...)                                                  \
 	({                                                                     \
 		test__fail();                                                  \
@@ -344,6 +357,18 @@  int test__join_cgroup(const char *path);
 	___ok;								\
 })
 
+#define ASSERT_MEMEQ(actual, expected, len, name) ({			\
+	static int duration = 0;					\
+	const void *__act = actual;					\
+	const void *__exp = expected;					\
+	int __len = len;						\
+	bool ___ok = memcmp(__act, __exp, __len) == 0;			\
+	CHECK(!___ok, (name), "unexpected memory mismatch\n");		\
+	DUMP_BUFFER("actual", __act, __len);				\
+	DUMP_BUFFER("expected:", __exp, __len);				\
+	___ok;								\
+})
+
 #define ASSERT_OK(res, name) ({						\
 	static int duration = 0;					\
 	long long ___res = (res);					\