[CRIU] [PATCH 2/6] compel/test: add ASSERT macro

Dmitry Safonov dsafonov at virtuozzo.com
Mon May 30 05:48:47 PDT 2016


Now it looks prettier. Also deleted unnecessary parameters from
test's functions.
No functional changes.

Cc: Cyrill Gorcunov <gorcunov at openvz.org>
Signed-off-by: Dmitry Safonov <dsafonov at virtuozzo.com>
---
 test/compel/handle_binary.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/test/compel/handle_binary.c b/test/compel/handle_binary.c
index a213ed0d3151..b06c814ba7b1 100644
--- a/test/compel/handle_binary.c
+++ b/test/compel/handle_binary.c
@@ -8,6 +8,12 @@
 extern int launch_test(void *mem, int expected_ret, const char *test_fmt, ...);
 extern const size_t test_elf_buf_size;
 
+static uintptr_t elf_addr;
+static const char *test_bitness;
+#define ASSERT(expected, fmt, ...)					\
+	launch_test((void *)elf_addr, expected,				\
+		fmt " %s", ##__VA_ARGS__, test_bitness)
+
 static const unsigned int sections_nr = 1;
 
 static void set_elf_hdr_relocatable(Ehdr_t *hdr)
@@ -16,38 +22,35 @@ static void set_elf_hdr_relocatable(Ehdr_t *hdr)
 	hdr->e_version = EV_CURRENT;
 }
 
-static int test_add_strings_section(void *elf, const char *msg)
+static int test_add_strings_section(Ehdr_t *hdr)
 {
-	Ehdr_t *hdr = elf;
 	Shdr_t *sec_strings_hdr;
-	uintptr_t sections_table = (uintptr_t)elf + hdr->e_shoff;
+	uintptr_t sections_table = elf_addr + hdr->e_shoff;
 	size_t sections_table_size = sections_nr*sizeof(hdr->e_shentsize);
 
 	hdr->e_shnum = sections_nr;
 	hdr->e_shstrndx = sections_nr; /* off-by-one */
-	if (launch_test(elf, -E_NO_STR_SEC,
-			"strings section's header oob of section table %s", msg))
+	if (ASSERT(-E_NO_STR_SEC,
+			"strings section's header oob of section table"))
 		return -1;
 
 	hdr->e_shstrndx = 0;
 	sec_strings_hdr = (void *)sections_table;
 
 	sec_strings_hdr->sh_offset = (Off_t)-1;
-	if (launch_test(elf, -E_NO_STR_SEC, "strings section oob %s", msg))
+	if (ASSERT(-E_NO_STR_SEC, "strings section oob"))
 		return -1;
 
 	/* Put strings just right after sections table. */
-	sec_strings_hdr->sh_offset = sections_table - (uintptr_t)elf +
+	sec_strings_hdr->sh_offset = sections_table - elf_addr +
 						sections_table_size;
 	return 0;
 }
 
-static int test_prepare_section_table(void *elf, const char *msg)
+static int test_prepare_section_table(Ehdr_t *hdr)
 {
-	Ehdr_t *hdr = elf;
-
 	hdr->e_shoff = (Off_t)test_elf_buf_size;
-	if (launch_test(elf, -E_NO_STR_SEC, "section table start oob %s", msg))
+	if (ASSERT(-E_NO_STR_SEC, "section table start oob"))
 		return -1;
 
 	/* Lets put sections table right after ELF header. */
@@ -55,31 +58,31 @@ static int test_prepare_section_table(void *elf, const char *msg)
 	hdr->e_shentsize = (Half_t) sizeof(Shdr_t);
 
 	hdr->e_shnum = (Half_t)-1;
-	if (launch_test(elf, -E_NO_STR_SEC, "too many sections in table %s", msg))
+	if (ASSERT(-E_NO_STR_SEC, "too many sections in table"))
 		return -1;
 
-	if (test_add_strings_section(elf, msg))
+	if (test_add_strings_section(hdr))
 		return -1;
 	return 0;
 }
 
-static int test_prepare_elf_header(void *elf, const char *msg)
+static int test_prepare_elf_header(void *elf)
 {
 	memset(elf, 0, sizeof(Ehdr_t));
-	if (launch_test(elf, -E_NOT_ELF, "zero ELF header %s", msg))
+	if (ASSERT(-E_NOT_ELF, "zero ELF header"))
 		return -1;
 
 	arch_test_set_elf_hdr_ident(elf);
-	if (launch_test(elf, -E_NOT_ELF, "unsupported ELF header %s", msg))
+	if (ASSERT(-E_NOT_ELF, "unsupported ELF header"))
 		return -1;
 
 	arch_test_set_elf_hdr_machine(elf);
-	if (launch_test(elf, -E_NOT_ELF, "non-relocatable ELF header %s", msg))
+	if (ASSERT(-E_NOT_ELF, "non-relocatable ELF header"))
 		return -1;
 
 	set_elf_hdr_relocatable(elf);
 
-	if (test_prepare_section_table(elf, msg))
+	if (test_prepare_section_table(elf))
 		return -1;
 
 	return 0;
@@ -87,6 +90,9 @@ static int test_prepare_elf_header(void *elf, const char *msg)
 
 void __run_tests(void *mem, const char *msg)
 {
-	if (test_prepare_elf_header(mem, msg))
+	elf_addr = (uintptr_t)mem;
+	test_bitness = msg;
+
+	if (test_prepare_elf_header(mem))
 		return;
 }
-- 
2.8.2



More information about the CRIU mailing list