xref: /freebsd/contrib/libarchive/tar/test/test_symlink_dir.c (revision 70e0bbedef95258a4dadc996d641a9bebd3f107d)
1 /*-
2  * Copyright (c) 2003-2007 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 /*
29  * tar -x -P should follow existing symlinks for dirs, but not other
30  * content.  Plain tar -x should remove symlinks when they're in the
31  * way of a dir extraction.
32  */
33 
34 static int
35 mkfile(const char *name, int mode, const char *contents, size_t size)
36 {
37 	FILE *f = fopen(name, "wb");
38 	size_t written;
39 
40 	(void)mode; /* UNUSED */
41 	if (f == NULL)
42 		return (-1);
43 	written = fwrite(contents, 1, size, f);
44 	fclose(f);
45 	if (size != written)
46 		return (-1);
47 	return (0);
48 }
49 
50 DEFINE_TEST(test_symlink_dir)
51 {
52 	assertUmask(0);
53 
54 	assertMakeDir("source", 0755);
55 	assertEqualInt(0, mkfile("source/file", 0755, "a", 1));
56 	assertEqualInt(0, mkfile("source/file2", 0755, "ab", 2));
57 	assertMakeDir("source/dir", 0755);
58 	assertMakeDir("source/dir/d", 0755);
59 	assertEqualInt(0, mkfile("source/dir/f", 0755, "abc", 3));
60 	assertMakeDir("source/dir2", 0755);
61 	assertMakeDir("source/dir2/d2", 0755);
62 	assertEqualInt(0, mkfile("source/dir2/f2", 0755, "abcd", 4));
63 	assertMakeDir("source/dir3", 0755);
64 	assertMakeDir("source/dir3/d3", 0755);
65 	assertEqualInt(0, mkfile("source/dir3/f3", 0755, "abcde", 5));
66 
67 	assertEqualInt(0,
68 	    systemf("%s -cf test.tar -C source dir dir2 dir3 file file2",
69 		testprog));
70 
71 	/*
72 	 * Extract with -x and without -P.
73 	 */
74 	assertMakeDir("dest1", 0755);
75 	/* "dir" is a symlink to an existing "dest1/real_dir" */
76 	assertMakeDir("dest1/real_dir", 0755);
77 	if (canSymlink()) {
78 		assertMakeSymlink("dest1/dir", "real_dir");
79 		/* "dir2" is a symlink to a non-existing "real_dir2" */
80 		assertMakeSymlink("dest1/dir2", "real_dir2");
81 	} else {
82 		skipping("some symlink checks");
83 	}
84 	/* "dir3" is a symlink to an existing "non_dir3" */
85 	assertEqualInt(0, mkfile("dest1/non_dir3", 0755, "abcdef", 6));
86 	if (canSymlink())
87 		assertMakeSymlink("dest1/dir3", "non_dir3");
88 	/* "file" is a symlink to existing "real_file" */
89 	assertEqualInt(0, mkfile("dest1/real_file", 0755, "abcdefg", 7));
90 	if (canSymlink()) {
91 		assertMakeSymlink("dest1/file", "real_file");
92 		/* "file2" is a symlink to non-existing "real_file2" */
93 		assertMakeSymlink("dest1/file2", "real_file2");
94 	}
95 	assertEqualInt(0, systemf("%s -xf test.tar -C dest1", testprog));
96 
97 	/* dest1/dir symlink should be replaced */
98 	failure("symlink to dir was followed when it shouldn't be");
99 	assertIsDir("dest1/dir", -1);
100 	/* dest1/dir2 symlink should be replaced */
101 	failure("Broken symlink wasn't replaced with dir");
102 	assertIsDir("dest1/dir2", -1);
103 	/* dest1/dir3 symlink should be replaced */
104 	failure("Symlink to non-dir wasn't replaced with dir");
105 	assertIsDir("dest1/dir3", -1);
106 	/* dest1/file symlink should be replaced */
107 	failure("Symlink to existing file should be replaced");
108 	assertIsReg("dest1/file", -1);
109 	/* dest1/file2 symlink should be replaced */
110 	failure("Symlink to non-existing file should be replaced");
111 	assertIsReg("dest1/file2", -1);
112 
113 	/*
114 	 * Extract with both -x and -P
115 	 */
116 	assertMakeDir("dest2", 0755);
117 	/* "dir" is a symlink to existing "real_dir" */
118 	assertMakeDir("dest2/real_dir", 0755);
119 	if (canSymlink())
120 		assertMakeSymlink("dest2/dir", "real_dir");
121 	/* "dir2" is a symlink to a non-existing "real_dir2" */
122 	if (canSymlink())
123 		assertMakeSymlink("dest2/dir2", "real_dir2");
124 	/* "dir3" is a symlink to an existing "non_dir3" */
125 	assertEqualInt(0, mkfile("dest2/non_dir3", 0755, "abcdefgh", 8));
126 	if (canSymlink())
127 		assertMakeSymlink("dest2/dir3", "non_dir3");
128 	/* "file" is a symlink to existing "real_file" */
129 	assertEqualInt(0, mkfile("dest2/real_file", 0755, "abcdefghi", 9));
130 	if (canSymlink())
131 		assertMakeSymlink("dest2/file", "real_file");
132 	/* "file2" is a symlink to non-existing "real_file2" */
133 	if (canSymlink())
134 		assertMakeSymlink("dest2/file2", "real_file2");
135 	assertEqualInt(0, systemf("%s -xPf test.tar -C dest2", testprog));
136 
137 	/* dest2/dir symlink should be followed */
138 	if (canSymlink()) {
139 		assertIsSymlink("dest2/dir", "real_dir");
140 		assertIsDir("dest2/real_dir", -1);
141 	}
142 
143 	/* Contents of 'dir' should be restored */
144 	assertIsDir("dest2/dir/d", -1);
145 	assertIsReg("dest2/dir/f", -1);
146 	assertFileSize("dest2/dir/f", 3);
147 	/* dest2/dir2 symlink should be removed */
148 	failure("Broken symlink wasn't replaced with dir");
149 	assertIsDir("dest2/dir2", -1);
150 	/* dest2/dir3 symlink should be removed */
151 	failure("Symlink to non-dir wasn't replaced with dir");
152 	assertIsDir("dest2/dir3", -1);
153 	/* dest2/file symlink should be removed;
154 	 * even -P shouldn't follow symlinks for files */
155 	failure("Symlink to existing file should be removed");
156 	assertIsReg("dest2/file", -1);
157 	/* dest2/file2 symlink should be removed */
158 	failure("Symlink to non-existing file should be removed");
159 	assertIsReg("dest2/file2", -1);
160 }
161