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