1231200Smm/*-
2231200Smm * Copyright (c) 2010 Tim Kientzle
3231200Smm * All rights reserved.
4231200Smm *
5231200Smm * Redistribution and use in source and binary forms, with or without
6231200Smm * modification, are permitted provided that the following conditions
7231200Smm * are met:
8231200Smm * 1. Redistributions of source code must retain the above copyright
9231200Smm *    notice, this list of conditions and the following disclaimer.
10231200Smm * 2. Redistributions in binary form must reproduce the above copyright
11231200Smm *    notice, this list of conditions and the following disclaimer in the
12231200Smm *    documentation and/or other materials provided with the distribution.
13231200Smm *
14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24231200Smm */
25231200Smm#include "test.h"
26231200Smm__FBSDID("$FreeBSD$");
27231200Smm
28231200SmmDEFINE_TEST(test_option_C_upper)
29231200Smm{
30231200Smm	int r;
31231200Smm
32231200Smm	assertMakeDir("d1", 0755);
33231200Smm	assertMakeDir("d2", 0755);
34231200Smm	assertMakeFile("d1/file1", 0644, "d1/file1");
35231200Smm	assertMakeFile("d1/file2", 0644, "d1/file2");
36231200Smm	assertMakeFile("d2/file1", 0644, "d2/file1");
37231200Smm	assertMakeFile("d2/file2", 0644, "d2/file2");
38231200Smm
39231200Smm	/*
40231200Smm	 * Test 1: Basic use of -C
41231200Smm	 */
42231200Smm	assertMakeDir("test1", 0755);
43231200Smm	assertChdir("test1");
44231200Smm	assertEqualInt(0, systemf("%s -cf archive.tar -C ../d1 file1 -C ../d2 file2", testprog));
45231200Smm	assertEqualInt(0,
46231200Smm	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
47231200Smm	assertFileContents("d1/file1", 8, "file1");
48231200Smm	assertFileContents("d2/file2", 8, "file2");
49231200Smm	assertEmptyFile("test.out");
50231200Smm	assertEmptyFile("test.err");
51231200Smm	assertChdir("..");
52231200Smm
53231200Smm
54231200Smm	/*
55231200Smm	 * Test 2: Multiple -C
56231200Smm	 */
57231200Smm	assertMakeDir("test2", 0755);
58231200Smm	assertChdir("test2");
59231200Smm	assertEqualInt(0, systemf("%s -cf archive.tar -C .. -C d1 file1 -C .. -C d2 file2", testprog));
60231200Smm	assertEqualInt(0,
61231200Smm	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
62231200Smm	assertFileContents("d1/file1", 8, "file1");
63231200Smm	assertFileContents("d2/file2", 8, "file2");
64231200Smm	assertEmptyFile("test.out");
65231200Smm	assertEmptyFile("test.err");
66231200Smm	assertChdir("..");
67231200Smm
68231200Smm	/*
69231200Smm	 * Test 3: -C fail
70231200Smm	 */
71231200Smm	assertMakeDir("test3", 0755);
72231200Smm	assertChdir("test3");
73231200Smm	r = systemf("%s -cf archive.tar -C ../XXX file1 -C ../d2 file2 2>write.err", testprog);
74231200Smm	assert(r != 0);
75231200Smm	assertNonEmptyFile("write.err");
76231200Smm	assertEqualInt(0,
77231200Smm	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
78231200Smm	assertFileNotExists("file1");
79231200Smm	assertFileNotExists("file2");
80231200Smm	assertEmptyFile("test.out");
81231200Smm	assertEmptyFile("test.err");
82231200Smm	assertChdir("..");
83231200Smm
84231200Smm	/*
85231200Smm	 * Test 4: Absolute -C
86231200Smm	 */
87231200Smm	assertMakeDir("test4", 0755);
88231200Smm	assertChdir("test4");
89231200Smm	assertEqualInt(0,
90231200Smm	    systemf("%s -cf archive.tar -C %s/d1 file1",
91231200Smm		testprog, testworkdir));
92231200Smm	assertEqualInt(0,
93231200Smm	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
94231200Smm	assertFileContents("d1/file1", 8, "file1");
95231200Smm	assertEmptyFile("test.out");
96231200Smm	assertEmptyFile("test.err");
97231200Smm	assertChdir("..");
98231200Smm
99231200Smm	/*
100231200Smm	 * Test 5: Unnecessary -C ignored even if directory named doesn't exist
101231200Smm	 */
102231200Smm	assertMakeDir("test5", 0755);
103231200Smm	assertChdir("test5");
104231200Smm	assertEqualInt(0,
105231200Smm	    systemf("%s -cf archive.tar -C XXX -C %s/d1 file1",
106231200Smm		testprog, testworkdir));
107231200Smm	assertEqualInt(0,
108231200Smm	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
109231200Smm	assertFileContents("d1/file1", 8, "file1");
110231200Smm	assertEmptyFile("test.out");
111231200Smm	assertEmptyFile("test.err");
112231200Smm	assertChdir("..");
113231200Smm
114231200Smm	/*
115231200Smm	 * Test 6: Necessary -C not ignored if directory doesn't exist
116231200Smm	 */
117231200Smm	assertMakeDir("test6", 0755);
118231200Smm	assertChdir("test6");
119231200Smm	r = systemf("%s -cf archive.tar -C XXX -C ../d1 file1 2>write.err",
120358088Smm	    testprog);
121231200Smm	assert(r != 0);
122231200Smm	assertNonEmptyFile("write.err");
123231200Smm	assertEqualInt(0,
124231200Smm	    systemf("%s -xf archive.tar >test.out 2>test.err", testprog));
125231200Smm	assertEmptyFile("test.out");
126231200Smm	assertEmptyFile("test.err");
127231200Smm	assertChdir("..");
128231200Smm
129231200Smm	/*
130231200Smm	 * Test 7: -C used without specifying directory
131231200Smm	 */
132231200Smm	assertMakeDir("test7", 0755);
133231200Smm	assertChdir("test7");
134231200Smm	r = systemf("%s -cf archive.tar ../d1/file1 -C 2>write.err", testprog);
135231200Smm	assert(r != 0);
136231200Smm	assertNonEmptyFile("write.err");
137231200Smm	assertChdir("..");
138231200Smm
139231200Smm	/*
140231200Smm	 * Test 8: -C used with meaningless option ''
141231200Smm	 */
142231200Smm	assertMakeDir("test8", 0755);
143231200Smm	assertChdir("test8");
144231200Smm	r = systemf("%s -cf archive.tar ../d1/file1 -C \"\" 2>write.err",
145231200Smm	    testprog);
146231200Smm	assert(r != 0);
147231200Smm	assertNonEmptyFile("write.err");
148231200Smm	assertChdir("..");
149231200Smm}
150