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_k)
29231200Smm{
30231200Smm	/*
31231200Smm	 * Create an archive with a couple of different versions of the
32231200Smm	 * same file.
33231200Smm	 */
34231200Smm
35231200Smm	assertMakeFile("foo", 0644, "foo1");
36231200Smm
37231200Smm	assertEqualInt(0, systemf("%s -cf archive.tar foo", testprog));
38231200Smm
39231200Smm	assertMakeFile("foo", 0644, "foo2");
40231200Smm
41231200Smm	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
42231200Smm
43231200Smm	assertMakeFile("bar", 0644, "bar1");
44231200Smm
45231200Smm	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
46231200Smm
47231200Smm	assertMakeFile("foo", 0644, "foo3");
48231200Smm
49231200Smm	assertEqualInt(0, systemf("%s -rf archive.tar foo", testprog));
50231200Smm
51231200Smm	assertMakeFile("bar", 0644, "bar2");
52231200Smm
53231200Smm	assertEqualInt(0, systemf("%s -rf archive.tar bar", testprog));
54231200Smm
55231200Smm	/*
56231200Smm	 * Now, try extracting from the test archive with various
57231200Smm	 * combinations of -k
58231200Smm	 */
59231200Smm
60231200Smm	/* Test 1: No option */
61231200Smm	assertMakeDir("test1", 0755);
62231200Smm	assertChdir("test1");
63231200Smm	assertEqualInt(0,
64231200Smm	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
65231200Smm	assertFileContents("foo3", 4, "foo");
66231200Smm	assertFileContents("bar2", 4, "bar");
67231200Smm	assertEmptyFile("test.out");
68231200Smm	assertEmptyFile("test.err");
69231200Smm	assertChdir("..");
70231200Smm
71231200Smm	/* Test 2: With -k, we should just get the first versions. */
72231200Smm	assertMakeDir("test2", 0755);
73231200Smm	assertChdir("test2");
74231200Smm	assertEqualInt(0,
75231200Smm	    systemf("%s -xf ../archive.tar -k >test.out 2>test.err", testprog));
76231200Smm	assertFileContents("foo1", 4, "foo");
77231200Smm	assertFileContents("bar1", 4, "bar");
78231200Smm	assertEmptyFile("test.out");
79231200Smm	assertEmptyFile("test.err");
80231200Smm	assertChdir("..");
81231200Smm
82231200Smm	/* Test 3: Without -k, existing files should get overwritten */
83231200Smm	assertMakeDir("test3", 0755);
84231200Smm	assertChdir("test3");
85231200Smm	assertMakeFile("bar", 0644, "bar0");
86231200Smm	assertMakeFile("foo", 0644, "foo0");
87231200Smm	assertEqualInt(0,
88231200Smm	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
89231200Smm	assertFileContents("foo3", 4, "foo");
90231200Smm	assertFileContents("bar2", 4, "bar");
91231200Smm	assertEmptyFile("test.out");
92231200Smm	assertEmptyFile("test.err");
93231200Smm	assertChdir("..");
94231200Smm
95231200Smm	/* Test 4: With -k, existing files should not get overwritten */
96231200Smm	assertMakeDir("test4", 0755);
97231200Smm	assertChdir("test4");
98231200Smm	assertMakeFile("bar", 0644, "bar0");
99231200Smm	assertMakeFile("foo", 0644, "foo0");
100231200Smm	assertEqualInt(0,
101231200Smm	    systemf("%s -xf ../archive.tar -k >test.out 2>test.err", testprog));
102231200Smm	assertFileContents("foo0", 4, "foo");
103231200Smm	assertFileContents("bar0", 4, "bar");
104231200Smm	assertEmptyFile("test.out");
105231200Smm	assertEmptyFile("test.err");
106231200Smm	assertChdir("..");
107231200Smm}
108