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 __FBSDID("$FreeBSD$");
27 
28 static void
29 unpack_test(const char *from, const char *options, const char *se)
30 {
31 	int r;
32 
33 	/* Create a work dir named after the file we're unpacking. */
34 	assertMakeDir(from, 0775);
35 	assertChdir(from);
36 
37 	/*
38 	 * Use cpio to unpack the sample archive
39 	 */
40 	extract_reference_file(from);
41 	r = systemf("%s -i %s < %s >unpack.out 2>unpack.err",
42 	    testprog, options, from);
43 	failure("Error invoking %s -i %s < %s",
44 	    testprog, options, from);
45 	assertEqualInt(r, 0);
46 
47 	/* Verify that nothing went to stderr. */
48 	if (canSymlink()) {
49 		failure("Error invoking %s -i %s < %s",
50 		    testprog, options, from);
51 		assertTextFileContents(se, "unpack.err");
52 	}
53 
54 	/*
55 	 * Verify unpacked files.
56 	 */
57 
58 	/* Regular file with 2 links. */
59 	assertIsReg("file", 0644);
60 	failure("%s", from);
61 	assertFileSize("file", 10);
62 	assertFileSize("linkfile", 10);
63 	failure("%s", from);
64 	assertFileNLinks("file", 2);
65 
66 	/* Another name for the same file. */
67 	failure("%s", from);
68 	assertIsHardlink("linkfile", "file");
69 	assertFileSize("file", 10);
70 	assertFileSize("linkfile", 10);
71 
72 	/* Symlink */
73 	if (canSymlink())
74 		assertIsSymlink("symlink", "file", 0);
75 
76 	/* dir */
77 	assertIsDir("dir", 0775);
78 
79 	assertChdir("..");
80 }
81 
82 DEFINE_TEST(test_gcpio_compat)
83 {
84 	assertUmask(0);
85 
86 	/* Dearchive sample files with a variety of options. */
87 	if (canSymlink()) {
88 		unpack_test("test_gcpio_compat_ref.bin",
89 		    "--no-preserve-owner", "1 block\n");
90 		unpack_test("test_gcpio_compat_ref.crc",
91 		    "--no-preserve-owner", "2 blocks\n");
92 		unpack_test("test_gcpio_compat_ref.newc",
93 		    "--no-preserve-owner", "2 blocks\n");
94 		/* gcpio-2.9 only reads 6 blocks here */
95 		unpack_test("test_gcpio_compat_ref.ustar",
96 		    "--no-preserve-owner", "7 blocks\n");
97 	} else {
98 		unpack_test("test_gcpio_compat_ref_nosym.bin",
99 		    "--no-preserve-owner", "1 block\n");
100 		unpack_test("test_gcpio_compat_ref_nosym.crc",
101 		    "--no-preserve-owner", "2 blocks\n");
102 		unpack_test("test_gcpio_compat_ref_nosym.newc",
103 		    "--no-preserve-owner", "2 blocks\n");
104 		/* gcpio-2.9 only reads 6 blocks here */
105 		unpack_test("test_gcpio_compat_ref_nosym.ustar",
106 		    "--no-preserve-owner", "7 blocks\n");
107 	}
108 }
109