Просмотр исходного кода

libdpkg: Use new test_alloc() macro to test memory allocations

Use this instead of normal pass/fail macros, as it will test_bail()
on errors, exiting with a proper exit code.
Guillem Jover лет назад: 12
Родитель
Сommit
75e991baae

+ 1 - 0
debian/changelog

@@ -77,6 +77,7 @@ dpkg (1.17.7) UNRELEASED; urgency=low
   * Test suite cleanup:
     - Do not unnecessarily shut up stdout in t-subproc.
     - Test command_exec() exit code.
+    - Test allocations with new test_alloc() instead of pass/fail macros.
 
   [ Updated dpkg translations ]
   * German (Sven Joachim).

+ 16 - 0
lib/dpkg/test.h

@@ -44,6 +44,22 @@
 		exit(99); \
 	} while (0)
 
+#define test_xstringify(str) \
+	#str
+#define test_stringify(str) \
+	test_xstringify(str)
+
+static inline void *
+test_alloc(void *ptr, const char *reason)
+{
+	if (ptr == NULL)
+		test_bail(reason);
+	return ptr;
+}
+
+#define test_alloc(ptr) \
+	test_alloc((ptr), "cannot allocate memory for " #ptr " in " __FILE__ ":" test_stringify(__LINE__))
+
 #define test_pass(a)			assert((a))
 #define test_fail(a)			assert(!(a))
 #define test_str(a, op, b)		assert(strcmp((a), (b)) op 0)

+ 1 - 1
lib/dpkg/test/t-arch.c

@@ -66,7 +66,7 @@ test_dpkg_arch_get_list(void)
 
 	/* Must never return NULL. */
 	arch = dpkg_arch_get_list();
-	test_pass(arch != NULL);
+	test_alloc(arch);
 
 	while ((arch = arch->next))
 		count++;

+ 1 - 2
lib/dpkg/test/t-buffer.c

@@ -55,8 +55,7 @@ test_fdio_hash(void)
 	char *test_file;
 	int fd;
 
-	test_file = strdup("test.XXXXXX");
-	test_pass(test_file != NULL);
+	test_file = test_alloc(strdup("test.XXXXXX"));
 	fd = mkstemp(test_file);
 	test_pass(fd >= 0);
 

+ 6 - 11
lib/dpkg/test/t-path.c

@@ -32,7 +32,7 @@
  * meaningful line numbers from assert. */
 #define test_trim_eq_ref(p, ref) \
 do { \
-	char *t = strdup((p)); \
+	char *t = test_alloc(strdup((p))); \
 	path_trim_slash_slashdot(t); \
 	test_str(t, ==, (ref)); \
 	free(t); \
@@ -129,8 +129,7 @@ test_path_quote(void)
 
 	/* Test no quoting. */
 	len = strlen(src_7_bit) + 1;
-	dst = malloc(len);
-	test_fail(dst == NULL);
+	dst = test_alloc(malloc(len));
 
 	path_quote_filename(dst, src_7_bit, len);
 	test_str(dst, ==, src_7_bit);
@@ -138,8 +137,7 @@ test_path_quote(void)
 
 	/* Test no quoting with limit. */
 	len = strlen(src_7_bit_trim) + 1;
-	dst = malloc(len);
-	test_fail(dst == NULL);
+	dst = test_alloc(malloc(len));
 
 	path_quote_filename(dst, src_7_bit, len);
 	test_str(dst, ==, src_7_bit_trim);
@@ -147,8 +145,7 @@ test_path_quote(void)
 
 	/* Test normal quoting. */
 	len = strlen(src_8_bit) * 2 + 1;
-	dst = malloc(len);
-	test_fail(dst == NULL);
+	dst = test_alloc(malloc(len));
 
 	path_quote_filename(dst, src_8_bit, len);
 	test_pass(strstr(dst, "end") != NULL);
@@ -157,8 +154,7 @@ test_path_quote(void)
 
 	/* Test normal quoting with limit. */
 	len = strlen(src_8_bit_end) + 1 + 2;
-	dst = malloc(len);
-	test_fail(dst == NULL);
+	dst = test_alloc(malloc(len));
 
 	path_quote_filename(dst, src_8_bit_end, len);
 	test_str(dst, ==, "text ");
@@ -166,8 +162,7 @@ test_path_quote(void)
 
 	/* Test backslash quoting with limit. */
 	len = strlen(src_bs_end) + 1;
-	dst = malloc(len);
-	test_fail(dst == NULL);
+	dst = test_alloc(malloc(len));
 
 	path_quote_filename(dst, src_bs_end, len);
 	test_str(dst, ==, "text ");

+ 7 - 7
lib/dpkg/test/t-pkg-list.c

@@ -32,17 +32,17 @@ test_pkg_list_new(void)
 	struct pkginfo pkg1, pkg2, pkg3;
 
 	l1 = pkg_list_new(&pkg1, NULL);
-	test_pass(l1 != NULL);
+	test_alloc(l1);
 	test_pass(l1->next == NULL);
 	test_pass(l1->pkg == &pkg1);
 
 	l2 = pkg_list_new(&pkg2, l1);
-	test_pass(l2 != NULL);
+	test_alloc(l2);
 	test_pass(l2->next == l1);
 	test_pass(l2->pkg == &pkg2);
 
 	l3 = pkg_list_new(&pkg3, l2);
-	test_pass(l3 != NULL);
+	test_alloc(l3);
 	test_pass(l3->next == l2);
 	test_pass(l3->pkg == &pkg3);
 
@@ -56,25 +56,25 @@ test_pkg_list_prepend(void)
 	struct pkginfo pkg1, pkg2, pkg3, pkg4;
 
 	pkg_list_prepend(&head, &pkg1);
-	test_pass(head != NULL);
+	test_alloc(head);
 	test_pass(head->next == NULL);
 	test_pass(head->pkg == &pkg1);
 	l1 = head;
 
 	pkg_list_prepend(&head, &pkg2);
-	test_pass(head != NULL);
+	test_alloc(head);
 	test_pass(head->next == l1);
 	test_pass(head->pkg == &pkg2);
 	l2 = head;
 
 	pkg_list_prepend(&head, &pkg3);
-	test_pass(head != NULL);
+	test_alloc(head);
 	test_pass(head->next == l2);
 	test_pass(head->pkg == &pkg3);
 	l3 = head;
 
 	pkg_list_prepend(&head, &pkg4);
-	test_pass(head != NULL);
+	test_alloc(head);
 	test_pass(head->next == l3);
 	test_pass(head->pkg == &pkg4);
 

+ 2 - 2
lib/dpkg/test/t-version.c

@@ -257,7 +257,7 @@ test_version_parse(void)
 	test_warn(err);
 
 	/* Test invalid characters in upstream version. */
-	verstr = m_strdup("0:0a-0");
+	verstr = test_alloc(strdup("0:0a-0"));
 	for (p = "!#@$%&/|\\<>()[]{};,_=*^'"; *p; p++) {
 		verstr[3] = *p;
 		test_fail(parseversion(&a, verstr, &err) == 0);
@@ -269,7 +269,7 @@ test_version_parse(void)
 	test_fail(parseversion(&a, "0:0-0:0", &err) == 0);
 	test_warn(err);
 
-	verstr = m_strdup("0:0-0");
+	verstr = test_alloc(strdup("0:0-0"));
 	for (p = "!#@$%&/|\\<>()[]{}:;,_=*^'"; *p; p++) {
 		verstr[4] = *p;
 		test_fail(parseversion(&a, verstr, &err) == 0);