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.
/* 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
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 *, ...);
/* 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.
/* 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);
}
static void
-passthrough(const char *target, const char *se)
+passthrough(const char *target)
{
int r;
/* 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("..");
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);
}
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");
/* 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.
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);
}
DEFINE_TEST(test_option_B)
{
struct stat st;
- const char *p;
int r, fd;
/*
/* 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);
}
DEFINE_TEST(test_option_L)
{
struct stat st;
- const char *p;
int fd, filelist;
int r;
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));
int r;
int f;
char buff[64];
- const char *p;
/* Create all of the test files. */
test_create();
/* 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.");
/* 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);
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.");
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);
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);