svn commit: r189481 - head/lib/libarchive/test

Tim Kientzle kientzle at FreeBSD.org
Fri Mar 6 19:16:17 PST 2009


Author: kientzle
Date: Sat Mar  7 03:16:16 2009
New Revision: 189481
URL: http://svn.freebsd.org/changeset/base/189481

Log:
  Merge r280,281,496,595,675,712 from libarchive.googlecode.com: Various
  test improvements, including some work on Windows compatibility and an
  extra check to verify that no test leaves open file descriptors
  around.

Modified:
  head/lib/libarchive/test/main.c
  head/lib/libarchive/test/test.h
  head/lib/libarchive/test/test_read_compress_program.c
  head/lib/libarchive/test/test_read_extract.c
  head/lib/libarchive/test/test_write_compress_program.c

Modified: head/lib/libarchive/test/main.c
==============================================================================
--- head/lib/libarchive/test/main.c	Sat Mar  7 03:04:06 2009	(r189480)
+++ head/lib/libarchive/test/main.c	Sat Mar  7 03:16:16 2009	(r189481)
@@ -62,7 +62,11 @@ __FBSDID("$FreeBSD$");
  */
 #undef DEFINE_TEST
 #define	DEFINE_TEST(name) void name(void);
+#ifdef LIST_H
+#include LIST_H
+#else
 #include "list.h"
+#endif
 
 /* Interix doesn't define these in a standard header. */
 #if __INTERIX__
@@ -343,10 +347,10 @@ test_assert_equal_string(const char *fil
 	    file, line);
 	fprintf(stderr, "      %s = ", e1);
 	strdump(v1);
-	fprintf(stderr, " (length %d)\n", v1 == NULL ? 0 : strlen(v1));
+	fprintf(stderr, " (length %d)\n", v1 == NULL ? 0 : (int)strlen(v1));
 	fprintf(stderr, "      %s = ", e2);
 	strdump(v2);
-	fprintf(stderr, " (length %d)\n", v2 == NULL ? 0 : strlen(v2));
+	fprintf(stderr, " (length %d)\n", v2 == NULL ? 0 : (int)strlen(v2));
 	report_failure(extra);
 	return (0);
 }
@@ -421,7 +425,7 @@ hexdump(const char *p, const char *ref, 
 	char sep;
 
 	for(i=0; i < l; i+=16) {
-		fprintf(stderr, "%04x", i + offset);
+		fprintf(stderr, "%04x", (unsigned)(i + offset));
 		sep = ' ';
 		for (j = 0; j < 16 && i + j < l; j++) {
 			if (ref != NULL && p[i + j] != ref[i + j])
@@ -718,9 +722,30 @@ slurpfile(size_t * sizep, const char *fm
 #undef DEFINE_TEST
 #define	DEFINE_TEST(n) { n, #n },
 struct { void (*func)(void); const char *name; } tests[] = {
+#ifdef LIST_H
+	#include LIST_H
+#else
 	#include "list.h"
+#endif
 };
 
+static void
+close_descriptors(int warn)
+{
+	int i;
+	int left_open = 0;
+
+	for (i = 3; i < 100; ++i) {
+		if (close(i) == 0)
+			++left_open;
+	}
+	if (warn && left_open > 0) {
+		fprintf(stderr, " ** %d descriptors unclosed\n", left_open);
+		failures += left_open;
+		report_failure(NULL);
+	}
+}
+
 /*
  * Each test is run in a private work dir.  Those work dirs
  * do have consistent and predictable names, in case a group
@@ -762,8 +787,12 @@ static int test_run(int i, const char *t
 	}
 	/* Explicitly reset the locale before each test. */
 	setlocale(LC_ALL, "C");
+	/* Make sure there are no stray descriptors going into the test. */
+	close_descriptors(0);
 	/* Run the actual test. */
 	(*tests[i].func)();
+	/* Close stray descriptors, record as errors against this test. */
+	close_descriptors(1);
 	/* Summarize the results of this test. */
 	summarize();
 	/* If there were no failures, we can remove the work dir. */
@@ -865,6 +894,32 @@ extract_reference_file(const char *name)
 	fclose(in);
 }
 
+#ifdef _WIN32
+#define DEV_NULL "NUL"
+#else
+#define DEV_NULL "/dev/null"
+#endif
+
+const char *
+external_gzip_program(int un)
+{
+	const char *extprog;
+
+	if (un) {
+		extprog = "gunzip";
+		if (systemf("%s -V >" DEV_NULL " 2>" DEV_NULL, extprog) == 0)
+			return (extprog);
+		extprog = "gzip -d";
+		if (systemf("%s -V >" DEV_NULL " 2>" DEV_NULL, extprog) == 0)
+			return (extprog);
+	} else {
+		extprog = "gzip";
+		if (systemf("%s -V >" DEV_NULL " 2>" DEV_NULL, extprog) == 0)
+			return (extprog);
+	}
+	return (NULL);
+}
+
 static char *
 get_refdir(void)
 {
@@ -873,7 +928,6 @@ get_refdir(void)
 	char *pwd, *p;
 
 	/* Get the current dir. */
-	/* XXX Visual C++ uses _getcwd() XXX */
 	pwd = getcwd(NULL, 0);
 	while (pwd[strlen(pwd) - 1] == '\n')
 		pwd[strlen(pwd) - 1] = '\0';
@@ -974,7 +1028,7 @@ int main(int argc, char **argv)
 	 * Parse options, without using getopt(), which isn't available
 	 * on all platforms.
 	 */
-	++argv; --argc;/* Skip program name */
+	++argv; /* Skip program name */
 	while (*argv != NULL) {
 		if (**argv != '-')
 			break;

Modified: head/lib/libarchive/test/test.h
==============================================================================
--- head/lib/libarchive/test/test.h	Sat Mar  7 03:04:06 2009	(r189480)
+++ head/lib/libarchive/test/test.h	Sat Mar  7 03:16:16 2009	(r189481)
@@ -157,6 +157,9 @@ char *slurpfile(size_t *, const char *fm
 /* Extracts named reference file to the current directory. */
 void extract_reference_file(const char *);
 
+/* Get external gzip program name */
+const char *external_gzip_program(int un);
+
 /*
  * Special interfaces for libarchive test harness.
  */

Modified: head/lib/libarchive/test/test_read_compress_program.c
==============================================================================
--- head/lib/libarchive/test/test_read_compress_program.c	Sat Mar  7 03:04:06 2009	(r189480)
+++ head/lib/libarchive/test/test_read_compress_program.c	Sat Mar  7 03:16:16 2009	(r189481)
@@ -41,12 +41,20 @@ DEFINE_TEST(test_read_compress_program)
 #else
 	struct archive_entry *ae;
 	struct archive *a;
+	const char *extprog;
+
+	if ((extprog = external_gzip_program(1)) == NULL) {
+		skipping("There is no gzip uncompression "
+		    "program in this platform");
+		return;
+	}
 	assert((a = archive_read_new()) != NULL);
 	assertEqualIntA(a, ARCHIVE_OK,
 	    archive_read_support_compression_none(a));
-	r = archive_read_support_compression_program(a, "gunzip");
+	r = archive_read_support_compression_program(a, extprog);
 	if (r == ARCHIVE_FATAL) {
-		skipping("archive_read_support_compression_program() unsupported on this platform");
+		skipping("archive_read_support_compression_program() "
+		    "unsupported on this platform");
 		return;
 	}
 	assertEqualIntA(a, ARCHIVE_OK, r);

Modified: head/lib/libarchive/test/test_read_extract.c
==============================================================================
--- head/lib/libarchive/test/test_read_extract.c	Sat Mar  7 03:04:06 2009	(r189480)
+++ head/lib/libarchive/test/test_read_extract.c	Sat Mar  7 03:16:16 2009	(r189481)
@@ -158,6 +158,7 @@ DEFINE_TEST(test_read_extract)
 	failure("The file on disk could not be opened.");
 	assert(fd != 0);
 	bytes_read = read(fd, buff, FILE_BUFF_SIZE);
+	close(fd);
 	failure("The file contents read from disk are the wrong size");
 	assert(bytes_read == FILE_BUFF_SIZE);
 	failure("The file contents on disk do not match the file contents that were put into the archive.");

Modified: head/lib/libarchive/test/test_write_compress_program.c
==============================================================================
--- head/lib/libarchive/test/test_write_compress_program.c	Sat Mar  7 03:04:06 2009	(r189480)
+++ head/lib/libarchive/test/test_write_compress_program.c	Sat Mar  7 03:16:16 2009	(r189481)
@@ -38,14 +38,21 @@ DEFINE_TEST(test_write_compress_program)
 	size_t used;
 	int blocksize = 1024;
 	int r;
+	const char *extprog;
 
+	if ((extprog = external_gzip_program(0)) == NULL) {
+		skipping("There is no gzip compression "
+		    "program in this platform");
+		return;
+	}
 	/* Create a new archive in memory. */
 	/* Write it through an external "gzip" program. */
 	assert((a = archive_write_new()) != NULL);
 	assertA(0 == archive_write_set_format_ustar(a));
-	r = archive_write_set_compression_program(a, "gzip");
+	r = archive_write_set_compression_program(a, extprog);
 	if (r == ARCHIVE_FATAL) {
-		skipping("Write compression via external program unsupported on this platform");
+		skipping("Write compression via external "
+		    "program unsupported on this platform");
 		archive_write_finish(a);
 		return;
 	}
@@ -84,7 +91,32 @@ DEFINE_TEST(test_write_compress_program)
 	assertA(0 == archive_read_support_compression_all(a));
 	assertA(0 == archive_read_open_memory(a, buff, used));
 
-	assertA(0 == archive_read_next_header(a, &ae));
+	r = archive_read_next_header(a, &ae);
+	if (r != ARCHIVE_OK) {
+		if (strcmp(archive_error_string(a),
+		    "Unrecognized archive format") == 0) {
+			skipping("This version of libarchive was compiled "
+			    "without gzip support");
+			assert(0 == archive_read_finish(a));
+			/*
+			 * Try using an external "gunzip","gzip -d" program
+			 */
+			if ((extprog = external_gzip_program(1)) == NULL) {
+				skipping("There is no gzip uncompression "
+				    "program in this platform");
+				return;
+			}
+			assert((a = archive_read_new()) != NULL);
+			assertEqualIntA(a, ARCHIVE_OK,
+			    archive_read_support_compression_none(a));
+			assertEqualIntA(a, ARCHIVE_OK,
+			    archive_read_support_compression_program(a, extprog));
+			assertA(0 == archive_read_support_format_all(a));
+			assertA(0 == archive_read_open_memory(a, buff, used));
+			r = archive_read_next_header(a, &ae);
+		}
+	}
+	assertA(0 == r);
 
 	assert(1 == archive_entry_mtime(ae));
 	assert(0 == archive_entry_atime(ae));


More information about the svn-src-all mailing list