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_U_upper)
29231200Smm{
30231200Smm	int r;
31231200Smm
32231200Smm	assertMakeFile("file1", 0644, "file1");
33231200Smm	assertMakeDir("d1", 0755);
34231200Smm	assertMakeFile("d1/file1", 0644, "d1/file1");
35231200Smm	assertEqualInt(0, systemf("%s -cf archive.tar file1 d1/file1", testprog));
36231200Smm
37231200Smm	/*
38231200Smm	 * bsdtar's man page used to claim that -x without -U would
39231200Smm	 * not break hard links.  This was and is nonsense.  The first
40231200Smm	 * two tests here simply verify that existing hard links get
41231200Smm	 * broken regardless.
42231200Smm	 */
43231200Smm
44231200Smm	/* Test 1: -x without -U */
45231200Smm	assertMakeDir("test1", 0755);
46231200Smm	assertChdir("test1");
47231200Smm	assertMakeFile("file1", 0644, "file1new");
48231200Smm	assertMakeHardlink("file2", "file1");
49231200Smm	assertEqualInt(0,
50231200Smm	    systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
51231200Smm	assertFileContents("file1", 5, "file1");
52231200Smm	assertFileContents("file1new", 8, "file2");
53231200Smm	assertEmptyFile("test.out");
54231200Smm	assertEmptyFile("test.err");
55231200Smm	assertChdir("..");
56231200Smm
57231200Smm
58231200Smm	/* Test 2: -x with -U */
59231200Smm	assertMakeDir("test2", 0755);
60231200Smm	assertChdir("test2");
61231200Smm	assertMakeFile("file1", 0644, "file1new");
62231200Smm	assertMakeHardlink("file2", "file1");
63231200Smm	assertEqualInt(0,
64231200Smm	    systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
65231200Smm	assertFileContents("file1", 5, "file1");
66231200Smm	assertFileContents("file1new", 8, "file2");
67231200Smm	assertEmptyFile("test.out");
68231200Smm	assertEmptyFile("test.err");
69231200Smm	assertChdir("..");
70231200Smm
71231200Smm	/*
72231200Smm	 * -U does make a difference in how bsdtar handles unwanted symlinks,
73231200Smm	 * though.  It interacts with -P.
74231200Smm	 */
75231200Smm	if (!canSymlink())
76231200Smm		return;
77231200Smm
78231200Smm	/* Test 3: Intermediate dir symlink causes error by default */
79231200Smm	assertMakeDir("test3", 0755);
80231200Smm	assertChdir("test3");
81231200Smm	assertMakeDir("realDir", 0755);
82231200Smm	assertMakeSymlink("d1", "realDir");
83231200Smm	r = systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog);
84231200Smm	assert(r != 0);
85231200Smm	assertIsSymlink("d1", "realDir");
86231200Smm	assertFileNotExists("d1/file1");
87231200Smm	assertEmptyFile("test.out");
88231200Smm	assertNonEmptyFile("test.err");
89231200Smm	assertChdir("..");
90231200Smm
91231200Smm	/* Test 4: Intermediate dir symlink gets removed with -U */
92231200Smm	assertMakeDir("test4", 0755);
93231200Smm	assertChdir("test4");
94231200Smm	assertMakeDir("realDir", 0755);
95231200Smm	assertMakeSymlink("d1", "realDir");
96231200Smm	assertEqualInt(0,
97231200Smm	    systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
98231200Smm	assertIsDir("d1", -1);
99231200Smm	assertFileContents("d1/file1", 8, "d1/file1");
100231200Smm	assertEmptyFile("test.out");
101231200Smm	assertEmptyFile("test.err");
102231200Smm	assertChdir("..");
103231200Smm
104231200Smm	/* Test 5: Intermediate dir symlink is followed with -P */
105231200Smm	assertMakeDir("test5", 0755);
106231200Smm	assertChdir("test5");
107231200Smm	assertMakeDir("realDir", 0755);
108231200Smm	assertMakeSymlink("d1", "realDir");
109231200Smm	assertEqualInt(0,
110231200Smm	    systemf("%s -xPf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
111231200Smm	assertIsSymlink("d1", "realDir");
112231200Smm	assertFileContents("d1/file1", 8, "d1/file1");
113231200Smm	assertEmptyFile("test.out");
114231200Smm	assertEmptyFile("test.err");
115231200Smm	assertChdir("..");
116231200Smm
117231200Smm	/* Test 6: Intermediate dir symlink is followed with -PU */
118231200Smm	assertMakeDir("test6", 0755);
119231200Smm	assertChdir("test6");
120231200Smm	assertMakeDir("realDir", 0755);
121231200Smm	assertMakeSymlink("d1", "realDir");
122231200Smm	assertEqualInt(0,
123231200Smm	    systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
124231200Smm	assertIsSymlink("d1", "realDir");
125231200Smm	assertFileContents("d1/file1", 8, "d1/file1");
126231200Smm	assertEmptyFile("test.out");
127231200Smm	assertEmptyFile("test.err");
128231200Smm	assertChdir("..");
129231200Smm
130231200Smm	/* Test 7: Final file symlink replaced by default */
131231200Smm	assertMakeDir("test7", 0755);
132231200Smm	assertChdir("test7");
133231200Smm	assertMakeDir("d1", 0755);
134231200Smm	assertMakeFile("d1/realfile1", 0644, "realfile1");
135231200Smm	assertMakeSymlink("d1/file1", "d1/realfile1");
136231200Smm	assertEqualInt(0,
137231200Smm	    systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
138231200Smm	assertIsReg("d1/file1", 0644);
139231200Smm	assertFileContents("d1/file1", 8, "d1/file1");
140231200Smm	assertFileContents("realfile1", 9, "d1/realfile1");
141231200Smm	assertEmptyFile("test.out");
142231200Smm	assertEmptyFile("test.err");
143231200Smm	assertChdir("..");
144231200Smm
145231200Smm	/* Test 8: Final file symlink replaced with -PU */
146231200Smm	assertMakeDir("test8", 0755);
147231200Smm	assertChdir("test8");
148231200Smm	assertMakeDir("d1", 0755);
149231200Smm	assertMakeFile("d1/realfile1", 0644, "realfile1");
150231200Smm	assertMakeSymlink("d1/file1", "d1/realfile1");
151231200Smm	assertEqualInt(0,
152231200Smm	    systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
153231200Smm	assertIsReg("d1/file1", 0644);
154231200Smm	assertFileContents("d1/file1", 8, "d1/file1");
155231200Smm	assertFileContents("realfile1", 9, "d1/realfile1");
156231200Smm	assertEmptyFile("test.out");
157231200Smm	assertEmptyFile("test.err");
158231200Smm	assertChdir("..");
159231200Smm}
160