summaryrefslogtreecommitdiffstats
path: root/usr.bin/cpio/test/test_format_newc.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/cpio/test/test_format_newc.c')
-rw-r--r--usr.bin/cpio/test/test_format_newc.c146
1 files changed, 76 insertions, 70 deletions
diff --git a/usr.bin/cpio/test/test_format_newc.c b/usr.bin/cpio/test/test_format_newc.c
index 816f074..eb87c31 100644
--- a/usr.bin/cpio/test/test_format_newc.c
+++ b/usr.bin/cpio/test/test_format_newc.c
@@ -25,6 +25,13 @@
#include "test.h"
__FBSDID("$FreeBSD$");
+/* Number of bytes needed to pad 'n' to multiple of 'block', assuming
+ * that 'block' is a power of two. This trick can be more easily
+ * remembered as -n & (block - 1), but many compilers quite reasonably
+ * warn about "-n" when n is an unsigned value. (~(n) + 1) is the
+ * same thing, but written in a way that won't offend anyone. */
+#define PAD(n, block) ((~(n) + 1) & ((block) - 1))
+
static int
is_hex(const char *p, size_t l)
{
@@ -63,60 +70,67 @@ from_hex(const char *p, size_t l)
DEFINE_TEST(test_format_newc)
{
- int fd, list;
+ FILE *list;
int r;
int devmajor, devminor, ino, gid;
+ int uid = -1;
time_t t, t2, now;
char *p, *e;
size_t s, fs, ns;
- mode_t oldmask;
- oldmask = umask(0);
+ assertUmask(0);
+
+#if !defined(_WIN32)
+ uid = getuid();
+#endif
/*
* Create an assortment of files.
* TODO: Extend this to cover more filetypes.
*/
- list = open("list", O_CREAT | O_WRONLY, 0644);
+ list = fopen("list", "w");
/* "file1" */
- fd = open("file1", O_CREAT | O_WRONLY, 0644);
- assert(fd >= 0);
- assertEqualInt(10, write(fd, "123456789", 10));
- close(fd);
- assertEqualInt(6, write(list, "file1\n", 6));
+ assertMakeFile("file1", 0644, "1234567890");
+ fprintf(list, "file1\n");
/* "hardlink" */
- assertEqualInt(0, link("file1", "hardlink"));
- assertEqualInt(9, write(list, "hardlink\n", 9));
+ assertMakeHardlink("hardlink", "file1");
+ fprintf(list, "hardlink\n");
/* Another hardlink, but this one won't be archived. */
- assertEqualInt(0, link("file1", "hardlink2"));
+ assertMakeHardlink("hardlink2", "file1");
/* "symlink" */
- assertEqualInt(0, symlink("file1", "symlink"));
- assertEqualInt(8, write(list, "symlink\n", 8));
+ if (canSymlink()) {
+ assertMakeSymlink("symlink", "file1");
+ fprintf(list, "symlink\n");
+ }
/* "dir" */
- assertEqualInt(0, mkdir("dir", 0775));
- assertEqualInt(4, write(list, "dir\n", 4));
+ assertMakeDir("dir", 0775);
+ fprintf(list, "dir\n");
/* Record some facts about what we just created: */
now = time(NULL); /* They were all created w/in last two seconds. */
/* Use the cpio program to create an archive. */
- close(list);
+ fclose(list);
r = systemf("%s -o --format=newc <list >newc.out 2>newc.err",
testprog);
if (!assertEqualInt(r, 0))
return;
/* Verify that nothing went to stderr. */
- assertTextFileContents("2 blocks\n", "newc.err");
+ if (canSymlink()) {
+ assertTextFileContents("2 blocks\n", "newc.err");
+ } else {
+ assertTextFileContents("1 block\n", "newc.err");
+ }
/* Verify that stdout is a well-formed cpio file in "newc" format. */
p = slurpfile(&s, "newc.out");
- assertEqualInt(s, 1024);
+ assertEqualInt(s, canSymlink() ? 1024 : 512);
e = p;
/*
@@ -133,8 +147,10 @@ DEFINE_TEST(test_format_newc)
assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */
#else
assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
-#endif
- assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
+#endif
+ if (uid < 0)
+ uid = from_hex(e + 22, 8);
+ assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
gid = from_hex(e + 30, 8); /* gid */
assertEqualMem(e + 38, "00000003", 8); /* nlink */
t = from_hex(e + 46, 8); /* mtime */
@@ -147,68 +163,60 @@ DEFINE_TEST(test_format_newc)
" field should be zero");
assertEqualInt(0, from_hex(e + 54, 8)); /* File size */
fs = from_hex(e + 54, 8);
- fs += 3 & -fs;
+ fs += PAD(fs, 4);
devmajor = from_hex(e + 62, 8); /* devmajor */
devminor = from_hex(e + 70, 8); /* devminor */
assert(is_hex(e + 78, 8)); /* rdevmajor */
assert(is_hex(e + 86, 8)); /* rdevminor */
assertEqualMem(e + 94, "00000006", 8); /* Name size */
ns = from_hex(e + 94, 8);
- ns += 3 & (-ns - 2);
+ ns += PAD(ns + 2, 4);
assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
assertEqualMem(e + 110, "file1\0", 6); /* Name contents */
/* Since there's another link, no file contents here. */
/* But add in file size so that an error here doesn't cascade. */
e += 110 + fs + ns;
- /* "symlink" pointing to "file1" */
- assert(is_hex(e, 110));
- assertEqualMem(e + 0, "070701", 6); /* Magic */
- assert(is_hex(e + 6, 8)); /* ino */
-#if !defined(_WIN32) || defined(__CYGWIN__)
- /* On Windows, symbolic link and group members bits and
- * others bits do not work. */
- assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */
-#endif
- assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
- assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
- assertEqualMem(e + 38, "00000001", 8); /* nlink */
- t2 = from_hex(e + 46, 8); /* mtime */
- failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
- assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
-#if defined(_WIN32) && !defined(__CYGWIN__)
- /* Symbolic link does not work. */
- assertEqualMem(e + 54, "0000000a", 8); /* File size */
-#else
- assertEqualMem(e + 54, "00000005", 8); /* File size */
-#endif
- fs = from_hex(e + 54, 8);
- fs += 3 & -fs;
- assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
- assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
- assert(is_hex(e + 78, 8)); /* rdevmajor */
- assert(is_hex(e + 86, 8)); /* rdevminor */
- assertEqualMem(e + 94, "00000008", 8); /* Name size */
- ns = from_hex(e + 94, 8);
- ns += 3 & (-ns - 2);
- assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
- assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
-#if !defined(_WIN32) || defined(__CYGWIN__)
- assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */
-#endif
- e += 110 + fs + ns;
+ if (canSymlink()) {
+ /* "symlink" pointing to "file1" */
+ assert(is_hex(e, 110));
+ assertEqualMem(e + 0, "070701", 6); /* Magic */
+ assert(is_hex(e + 6, 8)); /* ino */
+ assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */
+ assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
+ assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
+ assertEqualMem(e + 38, "00000001", 8); /* nlink */
+ t2 = from_hex(e + 46, 8); /* mtime */
+ failure("First entry created at t=0x%08x this entry created at t2=0x%08x", t, t2);
+ assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
+ assertEqualMem(e + 54, "00000005", 8); /* File size */
+ fs = from_hex(e + 54, 8);
+ fs += PAD(fs, 4);
+ assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
+ assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
+ assert(is_hex(e + 78, 8)); /* rdevmajor */
+ assert(is_hex(e + 86, 8)); /* rdevminor */
+ assertEqualMem(e + 94, "00000008", 8); /* Name size */
+ ns = from_hex(e + 94, 8);
+ ns += PAD(ns + 2, 4);
+ assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
+ assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
+ assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */
+ e += 110 + fs + ns;
+ }
/* "dir" */
assert(is_hex(e, 110));
assertEqualMem(e + 0, "070701", 6); /* Magic */
assert(is_hex(e + 6, 8)); /* ino */
#if defined(_WIN32) && !defined(__CYGWIN__)
- /* Group members bits and others bits do not work. */
+ /* Group members bits and others bits do not work. */
assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */
#else
- assertEqualInt(0x41fd, from_hex(e + 14, 8)); /* Mode */
+ /* Mode: sgid bit sometimes propagates from parent dirs, ignore it. */
+ assertEqualInt(040775, from_hex(e + 14, 8) & ~02000);
#endif
- assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
+ assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
#ifndef NLINKS_INACCURATE_FOR_DIRS
assertEqualMem(e + 38, "00000002", 8); /* nlink */
@@ -218,14 +226,14 @@ DEFINE_TEST(test_format_newc)
assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
assertEqualMem(e + 54, "00000000", 8); /* File size */
fs = from_hex(e + 54, 8);
- fs += 3 & -fs;
+ fs += PAD(fs, 4);
assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
assert(is_hex(e + 78, 8)); /* rdevmajor */
assert(is_hex(e + 86, 8)); /* rdevminor */
assertEqualMem(e + 94, "00000004", 8); /* Name size */
ns = from_hex(e + 94, 8);
- ns += 3 & (-ns - 2);
+ ns += PAD(ns + 2, 4);
assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
e += 110 + fs + ns;
@@ -243,7 +251,7 @@ DEFINE_TEST(test_format_newc)
#else
assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
#endif
- assertEqualInt(from_hex(e + 22, 8), getuid()); /* uid */
+ assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */
assertEqualMem(e + 38, "00000003", 8); /* nlink */
t2 = from_hex(e + 46, 8); /* mtime */
@@ -251,17 +259,17 @@ DEFINE_TEST(test_format_newc)
assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
assertEqualInt(10, from_hex(e + 54, 8)); /* File size */
fs = from_hex(e + 54, 8);
- fs += 3 & -fs;
+ fs += PAD(fs, 4);
assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
assert(is_hex(e + 78, 8)); /* rdevmajor */
assert(is_hex(e + 86, 8)); /* rdevminor */
assertEqualMem(e + 94, "00000009", 8); /* Name size */
ns = from_hex(e + 94, 8);
- ns += 3 & (-ns - 2);
+ ns += PAD(ns + 2, 4);
assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */
- assertEqualMem(e + 110 + ns, "123456789\0\0\0", 12); /* File contents */
+ assertEqualMem(e + 110 + ns, "1234567890\0\0", 12); /* File contents */
e += 110 + ns + fs;
/* Last entry is end-of-archive marker. */
@@ -283,6 +291,4 @@ DEFINE_TEST(test_format_newc)
assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */
free(p);
-
- umask(oldmask);
}
OpenPOWER on IntegriCloud