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_k)
29 {
30 	/*
31 	 * Create an archive with a couple of different versions of the
32 	 * same file.
33 	 */
34 
35 	assertMakeFile("foo", 0644, "foo1");
36 
37 	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
38 
39 	assertMakeFile("foo", 0644, "foo2");
40 
41 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
42 
43 	assertMakeFile("bar", 0644, "bar1");
44 
45 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
46 
47 	assertMakeFile("foo", 0644, "foo3");
48 
49 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
50 
51 	assertMakeFile("bar", 0644, "bar2");
52 
53 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
54 
55 	/*
56 	 * Now, try extracting from the test archive with various
57 	 * combinations of -k
58 	 */
59 
60 	/* Test 1: No option */
61 	assertMakeDir("test1", 0755);
62 	assertChdir("test1");
63 	assertEqualInt(0,
64 	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
65 	assertFileContents("foo3", 4, "foo");
66 	assertFileContents("bar2", 4, "bar");
67 	assertEmptyFile("test.out");
68 	assertEmptyFile("test.err");
69 	assertChdir("..");
70 
71 	/* Test 2: With -k, we should just get the first versions. */
72 	assertMakeDir("test2", 0755);
73 	assertChdir("test2");
74 	assertEqualInt(0,
75 	    systemf("%s -xf ../archive.tar -k >test.out 2>test.err", testprog));
76 	assertFileContents("foo1", 4, "foo");
77 	assertFileContents("bar1", 4, "bar");
78 	assertEmptyFile("test.out");
79 	assertEmptyFile("test.err");
80 	assertChdir("..");
81 
82 	/* Test 3: Without -k, existing files should get overwritten */
83 	assertMakeDir("test3", 0755);
84 	assertChdir("test3");
85 	assertMakeFile("bar", 0644, "bar0");
86 	assertMakeFile("foo", 0644, "foo0");
87 	assertEqualInt(0,
88 	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
89 	assertFileContents("foo3", 4, "foo");
90 	assertFileContents("bar2", 4, "bar");
91 	assertEmptyFile("test.out");
92 	assertEmptyFile("test.err");
93 	assertChdir("..");
94 
95 	/* Test 4: With -k, existing files should not get overwritten */
96 	assertMakeDir("test4", 0755);
97 	assertChdir("test4");
98 	assertMakeFile("bar", 0644, "bar0");
99 	assertMakeFile("foo", 0644, "foo0");
100 	assertEqualInt(0,
101 	    systemf("%s -xf ../archive.tar -k >test.out 2>test.err", testprog));
102 	assertFileContents("foo0", 4, "foo");
103 	assertFileContents("bar0", 4, "bar");
104 	assertEmptyFile("test.out");
105 	assertEmptyFile("test.err");
106 	assertChdir("..");
107 }
108