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