xref: /freebsd/contrib/libarchive/tar/test/test_option_k.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
DEFINE_TEST(test_option_k)9 DEFINE_TEST(test_option_k)
10 {
11 	/*
12 	 * Create an archive with a couple of different versions of the
13 	 * same file.
14 	 */
15 
16 	assertMakeFile("foo", 0644, "foo1");
17 
18 	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
19 
20 	assertMakeFile("foo", 0644, "foo2");
21 
22 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
23 
24 	assertMakeFile("bar", 0644, "bar1");
25 
26 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
27 
28 	assertMakeFile("foo", 0644, "foo3");
29 
30 	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
31 
32 	assertMakeFile("bar", 0644, "bar2");
33 
34 	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
35 
36 	/*
37 	 * Now, try extracting from the test archive with various
38 	 * combinations of -k
39 	 */
40 
41 	/* Test 1: No option */
42 	assertMakeDir("test1", 0755);
43 	assertChdir("test1");
44 	assertEqualInt(0,
45 	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
46 	assertFileContents("foo3", 4, "foo");
47 	assertFileContents("bar2", 4, "bar");
48 	assertEmptyFile("test.out");
49 	assertEmptyFile("test.err");
50 	assertChdir("..");
51 
52 	/* Test 2: With -k, we should just get the first versions. */
53 	assertMakeDir("test2", 0755);
54 	assertChdir("test2");
55 	assertEqualInt(0,
56 	    systemf("%s -xf ../archive.tar -k >test.out 2>test.err", testprog));
57 	assertFileContents("foo1", 4, "foo");
58 	assertFileContents("bar1", 4, "bar");
59 	assertEmptyFile("test.out");
60 	assertEmptyFile("test.err");
61 	assertChdir("..");
62 
63 	/* Test 3: Without -k, existing files should get overwritten */
64 	assertMakeDir("test3", 0755);
65 	assertChdir("test3");
66 	assertMakeFile("bar", 0644, "bar0");
67 	assertMakeFile("foo", 0644, "foo0");
68 	assertEqualInt(0,
69 	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
70 	assertFileContents("foo3", 4, "foo");
71 	assertFileContents("bar2", 4, "bar");
72 	assertEmptyFile("test.out");
73 	assertEmptyFile("test.err");
74 	assertChdir("..");
75 
76 	/* Test 4: With -k, existing files should not get overwritten */
77 	assertMakeDir("test4", 0755);
78 	assertChdir("test4");
79 	assertMakeFile("bar", 0644, "bar0");
80 	assertMakeFile("foo", 0644, "foo0");
81 	assertEqualInt(0,
82 	    systemf("%s -xf ../archive.tar -k >test.out 2>test.err", testprog));
83 	assertFileContents("foo0", 4, "foo");
84 	assertFileContents("bar0", 4, "bar");
85 	assertEmptyFile("test.out");
86 	assertEmptyFile("test.err");
87 	assertChdir("..");
88 }
89