1 /*-
2  * Copyright (c) 2010 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 DEFINE_TEST(test_option_C_upper)
29 {
30 	int r;
31 
32 	assertMakeDir("d1", 0755);
33 	assertMakeDir("d2", 0755);
34 	assertMakeFile("d1/file1", 0644, "d1/file1");
35 	assertMakeFile("d1/file2", 0644, "d1/file2");
36 	assertMakeFile("d2/file1", 0644, "d2/file1");
37 	assertMakeFile("d2/file2", 0644, "d2/file2");
38 
39 	/*
40 	 * Test 1: Basic use of -C
41 	 */
42 	assertMakeDir("test1", 0755);
43 	assertChdir("test1");
44 	assertEqualInt(0, systemf("%s -cf archive.tar -C ../d1 file1 -C ../d2 file2", testprog));
45 	assertEqualInt(0,
46 	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
47 	assertFileContents("d1/file1", 8, "file1");
48 	assertFileContents("d2/file2", 8, "file2");
49 	assertEmptyFile("test.out");
50 	assertEmptyFile("test.err");
51 	assertChdir("..");
52 
53 
54 	/*
55 	 * Test 2: Multiple -C
56 	 */
57 	assertMakeDir("test2", 0755);
58 	assertChdir("test2");
59 	assertEqualInt(0, systemf("%s -cf archive.tar -C .. -C d1 file1 -C .. -C d2 file2", testprog));
60 	assertEqualInt(0,
61 	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
62 	assertFileContents("d1/file1", 8, "file1");
63 	assertFileContents("d2/file2", 8, "file2");
64 	assertEmptyFile("test.out");
65 	assertEmptyFile("test.err");
66 	assertChdir("..");
67 
68 	/*
69 	 * Test 3: -C fail
70 	 */
71 	assertMakeDir("test3", 0755);
72 	assertChdir("test3");
73 	r = systemf("%s -cf archive.tar -C ../XXX file1 -C ../d2 file2 2>write.err", testprog);
74 	assert(r != 0);
75 	assertNonEmptyFile("write.err");
76 	assertEqualInt(0,
77 	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
78 	assertFileNotExists("file1");
79 	assertFileNotExists("file2");
80 	assertEmptyFile("test.out");
81 	assertEmptyFile("test.err");
82 	assertChdir("..");
83 
84 	/*
85 	 * Test 4: Absolute -C
86 	 */
87 	assertMakeDir("test4", 0755);
88 	assertChdir("test4");
89 	assertEqualInt(0,
90 	    systemf("%s -cf archive.tar -C %s/d1 file1",
91 		testprog, testworkdir));
92 	assertEqualInt(0,
93 	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
94 	assertFileContents("d1/file1", 8, "file1");
95 	assertEmptyFile("test.out");
96 	assertEmptyFile("test.err");
97 	assertChdir("..");
98 
99 	/*
100 	 * Test 5: Unnecessary -C ignored even if directory named doesn't exist
101 	 */
102 	assertMakeDir("test5", 0755);
103 	assertChdir("test5");
104 	assertEqualInt(0,
105 	    systemf("%s -cf archive.tar -C XXX -C %s/d1 file1",
106 		testprog, testworkdir));
107 	assertEqualInt(0,
108 	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
109 	assertFileContents("d1/file1", 8, "file1");
110 	assertEmptyFile("test.out");
111 	assertEmptyFile("test.err");
112 	assertChdir("..");
113 
114 	/*
115 	 * Test 6: Necessary -C not ignored if directory doesn't exist
116 	 */
117 	assertMakeDir("test6", 0755);
118 	assertChdir("test6");
119 	r = systemf("%s -cf archive.tar -C XXX -C ../d1 file1 2>write.err",
120 	    testprog);
121 	assert(r != 0);
122 	assertNonEmptyFile("write.err");
123 	assertEqualInt(0,
124 	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
125 	assertEmptyFile("test.out");
126 	assertEmptyFile("test.err");
127 	assertChdir("..");
128 
129 	/*
130 	 * Test 7: -C used without specifying directory
131 	 */
132 	assertMakeDir("test7", 0755);
133 	assertChdir("test7");
134 	r = systemf("%s -cf archive.tar ../d1/file1 -C 2>write.err", testprog);
135 	assert(r != 0);
136 	assertNonEmptyFile("write.err");
137 	assertChdir("..");
138 
139 	/*
140 	 * Test 8: -C used with meaningless option ''
141 	 */
142 	assertMakeDir("test8", 0755);
143 	assertChdir("test8");
144 	r = systemf("%s -cf archive.tar ../d1/file1 -C \"\" 2>write.err",
145 	    testprog);
146 	assert(r != 0);
147 	assertNonEmptyFile("write.err");
148 	assertChdir("..");
149 }
150