Commit 2fe34a11 authored by Alexis Lothoré (eBPF Foundation)'s avatar Alexis Lothoré (eBPF Foundation) Committed by Alexei Starovoitov
Browse files

selftests/bpf: add a macro to compare raw memory



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
The output generated by this new macro looks like the following:
[...]
run_tests_skb_less:FAIL:returned flow keys unexpected memory mismatch
actual:
	00 00 00 00 00 00 00 00 	00 00 00 00 00 00 00 00
	00 00 00 00 00 00 00 00 	00 00 00 00 00 00 00 00
	00 00 00 00 00 00 00 00 	00 00 00 00 00 00 00 00
	00 00 00 00 00 00 00 00
expected:
	0E 00 3E 00 DD 86 01 01 	00 06 86 DD 50 00 90 1F
	00 00 00 00 00 00 00 00 	00 00 00 00 00 00 00 00
	00 00 00 00 00 00 00 00 	00 00 00 00 00 00 00 00
	01 00 00 00 00 00 00 00
[...]

Acked-by: default avatarStanislav Fomichev <sdf@fomichev.me>
Signed-off-by: default avatarAlexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
Link: https://lore.kernel.org/r/20241120-flow_dissector-v3-1-45b46494f937@bootlin.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent e70140ba
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -1282,6 +1282,21 @@ void crash_handler(int signum)
	backtrace_symbols_fd(bt, sz, STDERR_FILENO);
}

void hexdump(const char *prefix, const void *buf, size_t len)
{
	for (int i = 0; i < len; i++) {
		if (!(i % 16)) {
			if (i)
				fprintf(stdout, "\n");
			fprintf(stdout, "%s", prefix);
		}
		if (i && !(i % 8) && (i % 16))
			fprintf(stdout, "\t");
		fprintf(stdout, "%02X ", ((uint8_t *)(buf))[i]);
	}
	fprintf(stdout, "\n");
}

static void sigint_handler(int signum)
{
	int i;
+15 −0
Original line number Diff line number Diff line
@@ -185,6 +185,7 @@ void test__end_subtest(void);
void test__skip(void);
void test__fail(void);
int test__join_cgroup(const char *path);
void hexdump(const char *prefix, const void *buf, size_t len);

#define PRINT_FAIL(format...)                                                  \
	({                                                                     \
@@ -344,6 +345,20 @@ 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");		\
	fprintf(stdout, "actual:\n");					\
	hexdump("\t", __act, __len);					\
	fprintf(stdout, "expected:\n");					\
	hexdump("\t", __exp, __len);					\
	___ok;								\
})

#define ASSERT_OK(res, name) ({						\
	static int duration = 0;					\
	long long ___res = (res);					\