1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003-2007 Tim Kientzle
5 * All rights reserved.
6 */
7 #include "test.h"
8
9 static void
unpack_test(const char * from,const char * options,const char * se)10 unpack_test(const char *from, const char *options, const char *se)
11 {
12 int r;
13
14 /* Create a work dir named after the file we're unpacking. */
15 assertMakeDir(from, 0775);
16 assertChdir(from);
17
18 /*
19 * Use cpio to unpack the sample archive
20 */
21 extract_reference_file(from);
22 r = systemf("%s -i %s < %s >unpack.out 2>unpack.err",
23 testprog, options, from);
24 failure("Error invoking %s -i %s < %s",
25 testprog, options, from);
26 assertEqualInt(r, 0);
27
28 /* Verify that nothing went to stderr. */
29 if (canSymlink()) {
30 failure("Error invoking %s -i %s < %s",
31 testprog, options, from);
32 assertTextFileContents(se, "unpack.err");
33 }
34
35 /*
36 * Verify unpacked files.
37 */
38
39 /* Regular file with 2 links. */
40 assertIsReg("file", 0644);
41 failure("%s", from);
42 assertFileSize("file", 10);
43 assertFileSize("linkfile", 10);
44 failure("%s", from);
45 assertFileNLinks("file", 2);
46
47 /* Another name for the same file. */
48 failure("%s", from);
49 assertIsHardlink("linkfile", "file");
50 assertFileSize("file", 10);
51 assertFileSize("linkfile", 10);
52
53 /* Symlink */
54 if (canSymlink())
55 assertIsSymlink("symlink", "file", 0);
56
57 /* dir */
58 assertIsDir("dir", 0775);
59
60 assertChdir("..");
61 }
62
DEFINE_TEST(test_gcpio_compat)63 DEFINE_TEST(test_gcpio_compat)
64 {
65 assertUmask(0);
66
67 /* Dearchive sample files with a variety of options. */
68 if (canSymlink()) {
69 unpack_test("test_gcpio_compat_ref.bin",
70 "--no-preserve-owner", "1 block\n");
71 unpack_test("test_gcpio_compat_ref.crc",
72 "--no-preserve-owner", "2 blocks\n");
73 unpack_test("test_gcpio_compat_ref.newc",
74 "--no-preserve-owner", "2 blocks\n");
75 /* gcpio-2.9 only reads 6 blocks here */
76 unpack_test("test_gcpio_compat_ref.ustar",
77 "--no-preserve-owner", "7 blocks\n");
78 } else {
79 unpack_test("test_gcpio_compat_ref_nosym.bin",
80 "--no-preserve-owner", "1 block\n");
81 unpack_test("test_gcpio_compat_ref_nosym.crc",
82 "--no-preserve-owner", "2 blocks\n");
83 unpack_test("test_gcpio_compat_ref_nosym.newc",
84 "--no-preserve-owner", "2 blocks\n");
85 /* gcpio-2.9 only reads 6 blocks here */
86 unpack_test("test_gcpio_compat_ref_nosym.ustar",
87 "--no-preserve-owner", "7 blocks\n");
88 }
89 }
90