1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 
27 static void
28 unpack_test(const char *from, const char *options, const char *se)
29 {
30 	int r;
31 
32 	/* Create a work dir named after the file we're unpacking. */
33 	assertMakeDir(from, 0775);
34 	assertChdir(from);
35 
36 	/*
37 	 * Use cpio to unpack the sample archive
38 	 */
39 	extract_reference_file(from);
40 	r = systemf("%s -i %s < %s >unpack.out 2>unpack.err",
41 	    testprog, options, from);
42 	failure("Error invoking %s -i %s < %s",
43 	    testprog, options, from);
44 	assertEqualInt(r, 0);
45 
46 	/* Verify that nothing went to stderr. */
47 	if (canSymlink()) {
48 		failure("Error invoking %s -i %s < %s",
49 		    testprog, options, from);
50 		assertTextFileContents(se, "unpack.err");
51 	}
52 
53 	/*
54 	 * Verify unpacked files.
55 	 */
56 
57 	/* Regular file with 2 links. */
58 	assertIsReg("file", 0644);
59 	failure("%s", from);
60 	assertFileSize("file", 10);
61 	assertFileSize("linkfile", 10);
62 	failure("%s", from);
63 	assertFileNLinks("file", 2);
64 
65 	/* Another name for the same file. */
66 	failure("%s", from);
67 	assertIsHardlink("linkfile", "file");
68 	assertFileSize("file", 10);
69 	assertFileSize("linkfile", 10);
70 
71 	/* Symlink */
72 	if (canSymlink())
73 		assertIsSymlink("symlink", "file", 0);
74 
75 	/* dir */
76 	assertIsDir("dir", 0775);
77 
78 	assertChdir("..");
79 }
80 
81 DEFINE_TEST(test_gcpio_compat)
82 {
83 	assertUmask(0);
84 
85 	/* Dearchive sample files with a variety of options. */
86 	if (canSymlink()) {
87 		unpack_test("test_gcpio_compat_ref.bin",
88 		    "--no-preserve-owner", "1 block\n");
89 		unpack_test("test_gcpio_compat_ref.crc",
90 		    "--no-preserve-owner", "2 blocks\n");
91 		unpack_test("test_gcpio_compat_ref.newc",
92 		    "--no-preserve-owner", "2 blocks\n");
93 		/* gcpio-2.9 only reads 6 blocks here */
94 		unpack_test("test_gcpio_compat_ref.ustar",
95 		    "--no-preserve-owner", "7 blocks\n");
96 	} else {
97 		unpack_test("test_gcpio_compat_ref_nosym.bin",
98 		    "--no-preserve-owner", "1 block\n");
99 		unpack_test("test_gcpio_compat_ref_nosym.crc",
100 		    "--no-preserve-owner", "2 blocks\n");
101 		unpack_test("test_gcpio_compat_ref_nosym.newc",
102 		    "--no-preserve-owner", "2 blocks\n");
103 		/* gcpio-2.9 only reads 6 blocks here */
104 		unpack_test("test_gcpio_compat_ref_nosym.ustar",
105 		    "--no-preserve-owner", "7 blocks\n");
106 	}
107 }
108