]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add assertFileTextContents to bsdcpio_test.
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Fri, 13 Mar 2009 11:05:58 +0000 (07:05 -0400)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Fri, 13 Mar 2009 11:05:58 +0000 (07:05 -0400)
It asserts the contents of a text file and pass over
different new line characters.

SVN-Revision: 763

cpio/test/main.c
cpio/test/test.h
cpio/test/test_basic.c
cpio/test/test_format_newc.c
cpio/test/test_gcpio_compat.c
cpio/test/test_option_B.c
cpio/test/test_option_L.c
cpio/test/test_option_a.c
cpio/test/test_option_c.c

index 46d9f4d795a26239e183de0a0418382a8743a5b5..1e641ffca487db42812f56e6bb691e2c6874bb9b 100644 (file)
@@ -632,6 +632,60 @@ test_assert_file_contents(const void *buff, int s, const char *fpattern, ...)
        return (0);
 }
 
+/* assertTextFileContents() asserts the contents of a text file. */
+int
+test_assert_text_file_contents(const char *buff, const char *f)
+{
+       char *contents;
+       const char *btxt, *ftxt;
+       int fd;
+       int n, s;
+
+       fd = open(f, O_RDONLY);
+       s = strlen(buff);
+       contents = malloc(s * 2 + 128);
+       n = read(fd, contents, s * 2 + 128 -1);
+       if (n >= 0)
+               contents[n] = '\0';
+       close(fd);
+       /* Compare texts. */
+       btxt = buff;
+       ftxt = (const char *)contents;
+       while (*btxt != '\0' && *ftxt != '\0') {
+               if (*btxt == *ftxt) {
+                       ++btxt;
+                       ++ftxt;
+                       continue;
+               }
+               if (btxt[0] == '\n' && ftxt[0] == '\r' && ftxt[1] == '\n') {
+                       /* Pass over different new line characters. */
+                       ++btxt;
+                       ftxt += 2;
+                       continue;
+               }
+               break;
+       }
+       if (*btxt == '\0' && *ftxt == '\0') {
+               free(contents);
+               return (1);
+       }
+       failures ++;
+       if (!previous_failures(test_filename, test_line)) {
+               fprintf(stderr, "%s:%d: File contents don't match\n",
+                   test_filename, test_line);
+               fprintf(stderr, "  file=\"%s\"\n", f);
+               if (n > 0)
+                       hexdump(contents, buff, n, 0);
+               else {
+                       fprintf(stderr, "  File empty, contents should be:\n");
+                       hexdump(buff, NULL, s, 0);
+               }
+               report_failure(test_extra);
+       }
+       free(contents);
+       return (0);
+}
+
 /*
  * Call standard system() call, but build up the command line using
  * sprintf() conventions.
index a91adf091983f191c43947026b336fc0876296f3..6d1899411ba445f3168c94fc2fa07958c946db10 100644 (file)
 /* Assert that file contents match a string; supports printf-style arguments. */
 #define assertFileContents             \
   test_setup(__FILE__, __LINE__);test_assert_file_contents
+#define assertTextFileContents         \
+  test_setup(__FILE__, __LINE__);test_assert_text_file_contents
 
 /*
  * This would be simple with C99 variadic macros, but I don't want to
@@ -136,6 +138,7 @@ int test_assert_equal_string(const char *, int, const char *v1, const char *, co
 int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
 int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *);
 int test_assert_file_contents(const void *, int, const char *, ...);
+int test_assert_text_file_contents(const char *buff, const char *f);
 int test_assert_file_exists(const char *, ...);
 int test_assert_file_not_exists(const char *, ...);
 
index 5140abcb5c6db284b5bd4cdfc0f6570e35cc4686..d94f17007a1adea1bab990fb4408d45a323e1e80 100644 (file)
@@ -149,7 +149,7 @@ basic_cpio(const char *target,
 
        /* Verify stderr. */
        failure("Expected: %s, options=%s", se, pack_options);
-       assertFileContents(se, strlen(se), "pack.err");
+       assertTextFileContents(se, "pack.err");
 
        /*
         * Use cpio to unpack the archive into another directory.
@@ -161,7 +161,7 @@ basic_cpio(const char *target,
 
        /* Verify stderr. */
        failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
-       assertFileContents(se, strlen(se), "unpack.err");
+       assertTextFileContents(se, "unpack.err");
 
        verify_files(target);
 
@@ -169,7 +169,7 @@ basic_cpio(const char *target,
 }
 
 static void
-passthrough(const char *target, const char *se)
+passthrough(const char *target)
 {
        int r;
 
@@ -189,7 +189,7 @@ passthrough(const char *target, const char *se)
        /* Verify stderr. */
        failure("Error invoking %s -p in dir %s",
            testprog, target);
-       assertFileContents(se, strlen(se), "stderr");
+       assertTextFileContents("1 block\n", "stderr");
 
        verify_files(target);
        chdir("..");
@@ -239,22 +239,22 @@ DEFINE_TEST(test_basic)
        umask(022);
 
        /* Archive/dearchive with a variety of options. */
-       basic_cpio("copy", "", "", "2 blocks" NL);
-       basic_cpio("copy_odc", "--format=odc", "", "2 blocks" NL);
-       basic_cpio("copy_newc", "-H newc", "", "2 blocks" NL);
-       basic_cpio("copy_cpio", "-H odc", "", "2 blocks" NL);
+       basic_cpio("copy", "", "", "2 blocks\n");
+       basic_cpio("copy_odc", "--format=odc", "", "2 blocks\n");
+       basic_cpio("copy_newc", "-H newc", "", "2 blocks\n");
+       basic_cpio("copy_cpio", "-H odc", "", "2 blocks\n");
 #ifdef _WIN32
        /*
         * On Windows, symbolic link does not work.
         * Currentry copying file instead. therefore block size is
         * different.
         */
-       basic_cpio("copy_ustar", "-H ustar", "", "10 blocks" NL);
+       basic_cpio("copy_ustar", "-H ustar", "", "10 blocks\n");
 #else
-       basic_cpio("copy_ustar", "-H ustar", "", "9 blocks" NL);
+       basic_cpio("copy_ustar", "-H ustar", "", "9 blocks\n");
 #endif
        /* Copy in one step using -p */
-       passthrough("passthrough", "1 block" NL);
+       passthrough("passthrough");
 
        umask(oldumask);
 }
index 71e096e76483ba4f9c1dbc98c47ed1518bd03d00..34cccaa5f96cc31281f81830305035ccdef5546d 100644 (file)
@@ -112,8 +112,7 @@ DEFINE_TEST(test_format_newc)
                return;
 
        /* Verify that nothing went to stderr. */
-       p = "2 blocks" NL;
-       assertFileContents(p, strlen(p), "newc.err");
+       assertTextFileContents("2 blocks\n", "newc.err");
 
        /* Verify that stdout is a well-formed cpio file in "newc" format. */
        p = slurpfile(&s, "newc.out");
index d4acf0d7e6e50eeaa6a1da03a946feb5c428312e..ffe09f897e7691b2d84d221a828a3f59d55dc395 100644 (file)
@@ -50,7 +50,7 @@ unpack_test(const char *from, const char *options, const char *se)
 
        /* Verify that nothing went to stderr. */
        failure("Error invoking %s -i %s < %s", testprog, options, from);
-       assertFileContents(se, strlen(se), "unpack.err");
+       assertTextFileContents(se, "unpack.err");
 
        /*
         * Verify unpacked files.
@@ -134,11 +134,11 @@ DEFINE_TEST(test_gcpio_compat)
        oldumask = umask(0);
 
        /* Dearchive sample files with a variety of options. */
-       unpack_test("test_gcpio_compat_ref.bin", "", "1 block" NL);
-       unpack_test("test_gcpio_compat_ref.crc", "", "2 blocks" NL);
-       unpack_test("test_gcpio_compat_ref.newc", "", "2 blocks" NL);
+       unpack_test("test_gcpio_compat_ref.bin", "", "1 block\n");
+       unpack_test("test_gcpio_compat_ref.crc", "", "2 blocks\n");
+       unpack_test("test_gcpio_compat_ref.newc", "", "2 blocks\n");
        /* gcpio-2.9 only reads 6 blocks here */
-       unpack_test("test_gcpio_compat_ref.ustar", "", "7 blocks" NL);
+       unpack_test("test_gcpio_compat_ref.ustar", "", "7 blocks\n");
 
        umask(oldumask);
 }
index 5bf5cdcbe8e7ac932664709d0adb193a366d89af..6f67d0d28b3782c4e3b35137c0b43a2968f6c990 100644 (file)
@@ -29,7 +29,6 @@ __FBSDID("$FreeBSD$");
 DEFINE_TEST(test_option_B)
 {
        struct stat st;
-       const char *p;
        int r, fd;
 
        /*
@@ -42,15 +41,14 @@ DEFINE_TEST(test_option_B)
        /* Create an archive without -B; this should be 512 bytes. */
        r = systemf("echo file | %s -o > small.cpio 2>small.err", testprog);
        assertEqualInt(r, 0);
-       p = "1 block" NL;
-       assertFileContents(p, strlen(p), "small.err");
+       assertTextFileContents("1 block\n", "small.err");
        assertEqualInt(0, stat("small.cpio", &st));
        assertEqualInt(512, st.st_size);
 
        /* Create an archive with -B; this should be 5120 bytes. */
        r = systemf("echo file | %s -oB > large.cpio 2>large.err", testprog);
        assertEqualInt(r, 0);
-       assertFileContents(p, strlen(p), "large.err");
+       assertTextFileContents("1 block\n", "large.err");
        assertEqualInt(0, stat("large.cpio", &st));
        assertEqualInt(5120, st.st_size);
 }
index 1f5e4e0528692fc184747b8554ec9a17ba141a95..e9928e1980b90489b228ab6b65aa7d9b4359c4e3 100644 (file)
@@ -34,7 +34,6 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_option_L.c,v 1.2 2008/08/24 06:21
 DEFINE_TEST(test_option_L)
 {
        struct stat st;
-       const char *p;
        int fd, filelist;
        int r;
 
@@ -64,8 +63,7 @@ DEFINE_TEST(test_option_L)
        r = systemf(CAT " filelist | %s -pd -L copy-L >copy-L.out 2>copy-L.err", testprog);
        assertEqualInt(r, 0);
        assertEmptyFile("copy-L.out");
-       p = "1 block" NL;
-       assertFileContents(p, strlen(p), "copy-L.err");
+       assertTextFileContents("1 block\n", "copy-L.err");
        assertEqualInt(0, lstat("copy-L/symlink", &st));
        failure("-pdL should dereference symlinks and turn them into files.");
        assert(!S_ISLNK(st.st_mode));
index 154cc3a871625b72a94668c2f10e8be8d051d53c..b8216a90f07313fc24518b3db28ba6d3915d0de5 100644 (file)
@@ -99,7 +99,6 @@ DEFINE_TEST(test_option_a)
        int r;
        int f;
        char buff[64];
-       const char *p;
 
        /* Create all of the test files. */
        test_create();
@@ -123,8 +122,7 @@ DEFINE_TEST(test_option_a)
                /* Copy the file without -a; should change the atime. */
                r = systemf("echo %s | %s -pd copy-no-a > copy-no-a.out 2>copy-no-a.err", files[1].name, testprog);
                assertEqualInt(r, 0);
-               p = "1 block" NL;
-               assertFileContents(p, strlen(p), "copy-no-a.err");
+               assertTextFileContents("1 block\n", "copy-no-a.err");
                assertEmptyFile("copy-no-a.out");
                assertEqualInt(0, stat(files[1].name, &st));
                failure("Copying file without -a should have changed atime.");
@@ -133,7 +131,7 @@ DEFINE_TEST(test_option_a)
                /* Archive the file without -a; should change the atime. */
                r = systemf("echo %s | %s -o > archive-no-a.out 2>archive-no-a.err", files[2].name, testprog);
                assertEqualInt(r, 0);
-               assertFileContents(p, strlen(p), "copy-no-a.err");
+               assertTextFileContents("1 block\n", "copy-no-a.err");
                assertEqualInt(0, stat(files[2].name, &st));
                failure("Archiving file without -a should have changed atime.");
                assert(st.st_atime != files[2].atime_sec);
@@ -148,8 +146,7 @@ DEFINE_TEST(test_option_a)
        r = systemf("echo %s | %s -pad copy-a > copy-a.out 2>copy-a.err",
            files[3].name, testprog);
        assertEqualInt(r, 0);
-       p = "1 block" NL;
-       assertFileContents(p, strlen(p), "copy-a.err");
+       assertTextFileContents("1 block\n", "copy-a.err");
        assertEmptyFile("copy-a.out");
        assertEqualInt(0, stat(files[3].name, &st));
        failure("Copying file with -a should not have changed atime.");
@@ -159,7 +156,7 @@ DEFINE_TEST(test_option_a)
        r = systemf("echo %s | %s -oa > archive-a.out 2>archive-a.err",
            files[4].name, testprog);
        assertEqualInt(r, 0);
-       assertFileContents(p, strlen(p), "copy-a.err");
+       assertTextFileContents("1 block\n", "copy-a.err");
        assertEqualInt(0, stat(files[4].name, &st));
        failure("Archiving file with -a should not have changed atime.");
        assertEqualInt(st.st_atime, files[4].atime_sec);
index 248da9dfdca79c528472f14217829e075fe809de..55fa6892298e2d610f2d74f88fab29b9698f2a0b 100644 (file)
@@ -90,8 +90,7 @@ DEFINE_TEST(test_option_c)
        close(filelist);
        r = systemf("%s -oc <filelist >basic.out 2>basic.err", testprog);
        /* Verify that nothing went to stderr. */
-       p = "1 block" NL;
-       assertFileContents(p, strlen(p), "basic.err");
+       assertTextFileContents("1 block\n", "basic.err");
 
        /* Assert that the program finished. */
        failure("%s -oc crashed", testprog);