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_U_upper)9 DEFINE_TEST(test_option_U_upper)
10 {
11           int r;
12 
13           assertMakeFile("file1", 0644, "file1");
14           assertMakeDir("d1", 0755);
15           assertMakeFile("d1/file1", 0644, "d1/file1");
16           assertEqualInt(0, systemf("%s -cf archive.tar file1 d1/file1", testprog));
17 
18           /*
19            * bsdtar's man page used to claim that -x without -U would
20            * not break hard links.  This was and is nonsense.  The first
21            * two tests here simply verify that existing hard links get
22            * broken regardless.
23            */
24 
25           /* Test 1: -x without -U */
26           assertMakeDir("test1", 0755);
27           assertChdir("test1");
28           assertMakeFile("file1", 0644, "file1new");
29           assertMakeHardlink("file2", "file1");
30           assertEqualInt(0,
31               systemf("%s -xf ../archive.tar >test.out 2>test.err", testprog));
32           assertFileContents("file1", 5, "file1");
33           assertFileContents("file1new", 8, "file2");
34           assertEmptyFile("test.out");
35           assertEmptyFile("test.err");
36           assertChdir("..");
37 
38 
39           /* Test 2: -x with -U */
40           assertMakeDir("test2", 0755);
41           assertChdir("test2");
42           assertMakeFile("file1", 0644, "file1new");
43           assertMakeHardlink("file2", "file1");
44           assertEqualInt(0,
45               systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
46           assertFileContents("file1", 5, "file1");
47           assertFileContents("file1new", 8, "file2");
48           assertEmptyFile("test.out");
49           assertEmptyFile("test.err");
50           assertChdir("..");
51 
52           /*
53            * -U does make a difference in how bsdtar handles unwanted symlinks,
54            * though.  It interacts with -P.
55            */
56           if (!canSymlink())
57                     return;
58 
59           /* Test 3: Intermediate dir symlink preserves it */
60           assertMakeDir("test3", 0755);
61           assertChdir("test3");
62           assertMakeDir("realDir", 0755);
63           assertMakeSymlink("d1", "realDir", 1);
64           r = systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog);
65           assert(r == 0);
66           assertIsSymlink("d1", "realDir", 1);
67           assertFileContents("d1/file1", 8, "d1/file1");
68           assertEmptyFile("test.out");
69           assertEmptyFile("test.err");
70           assertChdir("..");
71 
72           /* Test 4: Intermediate dir symlink gets removed with -U */
73           assertMakeDir("test4", 0755);
74           assertChdir("test4");
75           assertMakeDir("realDir", 0755);
76           assertMakeSymlink("d1", "realDir", 1);
77           assertEqualInt(0,
78               systemf("%s -xUf ../archive.tar >test.out 2>test.err", testprog));
79           assertIsDir("d1", -1);
80           assertFileContents("d1/file1", 8, "d1/file1");
81           assertEmptyFile("test.out");
82           assertEmptyFile("test.err");
83           assertChdir("..");
84 
85           /* Test 5: Intermediate dir symlink is followed with -P */
86           assertMakeDir("test5", 0755);
87           assertChdir("test5");
88           assertMakeDir("realDir", 0755);
89           assertMakeSymlink("d1", "realDir", 1);
90           assertEqualInt(0,
91               systemf("%s -xPf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
92           assertIsSymlink("d1", "realDir", 1);
93           assertFileContents("d1/file1", 8, "d1/file1");
94           assertEmptyFile("test.out");
95           assertEmptyFile("test.err");
96           assertChdir("..");
97 
98           /* Test 6: Intermediate dir symlink is followed with -PU */
99           assertMakeDir("test6", 0755);
100           assertChdir("test6");
101           assertMakeDir("realDir", 0755);
102           assertMakeSymlink("d1", "realDir", 1);
103           assertEqualInt(0,
104               systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
105           assertIsSymlink("d1", "realDir", 1);
106           assertFileContents("d1/file1", 8, "d1/file1");
107           assertEmptyFile("test.out");
108           assertEmptyFile("test.err");
109           assertChdir("..");
110 
111           /* Test 7: Final file symlink replaced by default */
112           assertMakeDir("test7", 0755);
113           assertChdir("test7");
114           assertMakeDir("d1", 0755);
115           assertMakeFile("d1/realfile1", 0644, "realfile1");
116           assertMakeSymlink("d1/file1", "d1/realfile1", 0);
117           assertEqualInt(0,
118               systemf("%s -xf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
119           assertIsReg("d1/file1", umasked(0644));
120           assertFileContents("d1/file1", 8, "d1/file1");
121           assertFileContents("realfile1", 9, "d1/realfile1");
122           assertEmptyFile("test.out");
123           assertEmptyFile("test.err");
124           assertChdir("..");
125 
126           /* Test 8: Final file symlink replaced with -PU */
127           assertMakeDir("test8", 0755);
128           assertChdir("test8");
129           assertMakeDir("d1", 0755);
130           assertMakeFile("d1/realfile1", 0644, "realfile1");
131           assertMakeSymlink("d1/file1", "d1/realfile1", 0);
132           assertEqualInt(0,
133               systemf("%s -xPUf ../archive.tar d1/file1 >test.out 2>test.err", testprog));
134           assertIsReg("d1/file1", umasked(0644));
135           assertFileContents("d1/file1", 8, "d1/file1");
136           assertFileContents("realfile1", 9, "d1/realfile1");
137           assertEmptyFile("test.out");
138           assertEmptyFile("test.err");
139           assertChdir("..");
140 }
141