From: Michihiro NAKAJIMA Date: Fri, 13 Mar 2009 11:05:58 +0000 (-0400) Subject: Add assertFileTextContents to bsdcpio_test. X-Git-Tag: v2.7.0~166 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4f5e6d46cdc709755c5a720a55ba572ca5c802bd;p=thirdparty%2Flibarchive.git Add assertFileTextContents to bsdcpio_test. It asserts the contents of a text file and pass over different new line characters. SVN-Revision: 763 --- diff --git a/cpio/test/main.c b/cpio/test/main.c index 46d9f4d79..1e641ffca 100644 --- a/cpio/test/main.c +++ b/cpio/test/main.c @@ -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. diff --git a/cpio/test/test.h b/cpio/test/test.h index a91adf091..6d1899411 100644 --- a/cpio/test/test.h +++ b/cpio/test/test.h @@ -114,6 +114,8 @@ /* 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 *, ...); diff --git a/cpio/test/test_basic.c b/cpio/test/test_basic.c index 5140abcb5..d94f17007 100644 --- a/cpio/test/test_basic.c +++ b/cpio/test/test_basic.c @@ -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); } diff --git a/cpio/test/test_format_newc.c b/cpio/test/test_format_newc.c index 71e096e76..34cccaa5f 100644 --- a/cpio/test/test_format_newc.c +++ b/cpio/test/test_format_newc.c @@ -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"); diff --git a/cpio/test/test_gcpio_compat.c b/cpio/test/test_gcpio_compat.c index d4acf0d7e..ffe09f897 100644 --- a/cpio/test/test_gcpio_compat.c +++ b/cpio/test/test_gcpio_compat.c @@ -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); } diff --git a/cpio/test/test_option_B.c b/cpio/test/test_option_B.c index 5bf5cdcbe..6f67d0d28 100644 --- a/cpio/test/test_option_B.c +++ b/cpio/test/test_option_B.c @@ -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); } diff --git a/cpio/test/test_option_L.c b/cpio/test/test_option_L.c index 1f5e4e052..e9928e198 100644 --- a/cpio/test/test_option_L.c +++ b/cpio/test/test_option_L.c @@ -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)); diff --git a/cpio/test/test_option_a.c b/cpio/test/test_option_a.c index 154cc3a87..b8216a90f 100644 --- a/cpio/test/test_option_a.c +++ b/cpio/test/test_option_a.c @@ -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); diff --git a/cpio/test/test_option_c.c b/cpio/test/test_option_c.c index 248da9dfd..55fa68922 100644 --- a/cpio/test/test_option_c.c +++ b/cpio/test/test_option_c.c @@ -90,8 +90,7 @@ DEFINE_TEST(test_option_c) close(filelist); r = systemf("%s -oc 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);