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