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