1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 
28 #ifdef HAVE_SYS_ACL_H
29 #include <sys/acl.h>
30 #endif
31 #ifdef HAVE_SYS_XATTR_H
32 #include <sys/xattr.h>
33 #endif
34 
35 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
36 	&& defined(HAVE_ZLIB_H)
37 
38 //
39 // The test ACL used here is sometimes assigned to the 'Guest' user
40 // This changes the text and breaks the test.  This function simply
41 // strips the 'Guest' information from the string to help ensure
42 // consistent results on different machines.
43 //
44 static char _acl_temp[256];
45 static const char *
46 clean_acl(const char *acl) {
47 	char *p, *q;
48 	if (strlen(acl) >= sizeof(_acl_temp))
49 		return acl;
50 
51 	strcpy(_acl_temp, acl);
52 	p = strstr(_acl_temp, ":Guest:");
53 	if (p != NULL) {
54 		fprintf(stderr, "Shortening: %s\n", p + 1);
55 		memmove(p + 1, p + 6, strlen(p + 6) + 1);
56 		q = strstr(p + 2, ":");
57 		fprintf(stderr, "Shortening: %s\n", q);
58 		memmove(p + 2, q, strlen(q) + 1);
59 		return _acl_temp;
60 	}
61 	return _acl_temp;
62 }
63 
64 static int
65 has_xattr(const char *filename, const char *xattrname)
66 {
67 	char *nl, *nlp;
68 	ssize_t r;
69 	int existing;
70 
71 	r = listxattr(filename, NULL, 0, XATTR_SHOWCOMPRESSION);
72 	if (r < 0)
73 		return (0);
74 	if (r == 0)
75 		return (0);
76 
77 	assert((nl = malloc(r)) != NULL);
78 	if (nl == NULL)
79 		return (0);
80 
81 	r = listxattr(filename, nl, r, XATTR_SHOWCOMPRESSION);
82 	if (r < 0) {
83 		free(nl);
84 		return (0);
85 	}
86 
87 	existing = 0;
88 	for (nlp = nl; nlp < nl + r; nlp += strlen(nlp) + 1) {
89 		if (strcmp(nlp, xattrname) == 0) {
90 			existing = 1;
91 			break;
92 		}
93 	}
94 	free(nl);
95 	return (existing);
96 }
97 
98 #endif
99 
100 /*
101  * Exercise HFS+ Compression.
102  */
103 DEFINE_TEST(test_write_disk_appledouble)
104 {
105 #if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
106 	|| !defined(HAVE_ZLIB_H)
107 	skipping("MacOS-specific AppleDouble test");
108 #else
109 	const char *refname = "test_write_disk_appledouble.cpio.gz";
110 	struct archive *ad, *a;
111 	struct archive_entry *ae;
112 	struct stat st;
113 	acl_t acl;
114 
115 	extract_reference_file(refname);
116 
117 	/*
118 	 * Extract an archive to disk with HFS+ Compression.
119 	 */
120 	assert((ad = archive_write_disk_new()) != NULL);
121 	assertEqualIntA(ad, ARCHIVE_OK,
122 	    archive_write_disk_set_standard_lookup(ad));
123 	assertEqualIntA(ad, ARCHIVE_OK,
124 	    archive_write_disk_set_options(ad,
125 		ARCHIVE_EXTRACT_TIME |
126 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
127 		ARCHIVE_EXTRACT_SECURE_NODOTDOT |
128 		ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED));
129 
130 	assert((a = archive_read_new()) != NULL);
131 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
132 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
133 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
134 	    refname, 512 * 20));
135 
136 	assertMakeDir("hfscmp", 0755);
137 	assertChdir("hfscmp");
138 
139 	/* Skip "." */
140 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
141 	assertEqualString(".", archive_entry_pathname(ae));
142 	/* Extract file3. */
143 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
144 	assertEqualString("./file3", archive_entry_pathname(ae));
145 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
146 	/* Extract ._file3 which will be merged into file3 as metadata. */
147 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
148 	assertEqualString("./._file3", archive_entry_pathname(ae));
149 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
150 
151 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
152 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
153 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
154 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
155 
156 	/* Test file3. */
157 	assertEqualInt(0, stat("file3", &st));
158 	assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
159 	assertFileSize("file3", 8);
160 	failure("'%s' should not have Resource Fork", "file3");
161 	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
162 	failure("'%s' should have decompfs xattr", "file3");
163 	assertEqualInt(1, has_xattr("file3", "com.apple.decmpfs"));
164 	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
165 	assertEqualString(clean_acl(acl_to_text(acl, NULL)),
166 	    "!#acl 1\n"
167 	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
168 	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
169 	);
170 	if (acl) acl_free(acl);
171 	/* Test ._file3. */
172 	failure("'file3' should be merged and removed");
173 	assertFileNotExists("._file3");
174 
175 	assertChdir("..");
176 
177 	/*
178 	 * Extract an archive to disk without HFS+ Compression.
179 	 */
180 	assert((ad = archive_write_disk_new()) != NULL);
181 	assertEqualIntA(ad, ARCHIVE_OK,
182 	    archive_write_disk_set_standard_lookup(ad));
183 	assertEqualIntA(ad, ARCHIVE_OK,
184 	    archive_write_disk_set_options(ad,
185 		ARCHIVE_EXTRACT_TIME |
186 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
187 		ARCHIVE_EXTRACT_SECURE_NODOTDOT));
188 
189 	assert((a = archive_read_new()) != NULL);
190 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
191 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
192 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
193 	    refname, 512 * 20));
194 
195 	assertMakeDir("nocmp", 0755);
196 	assertChdir("nocmp");
197 
198 	/* Skip "." */
199 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
200 	assertEqualString(".", archive_entry_pathname(ae));
201 	/* Extract file3. */
202 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
203 	assertEqualString("./file3", archive_entry_pathname(ae));
204 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
205 	/* Extract ._file3 which will be merged into file3 as metadata. */
206 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
207 	assertEqualString("./._file3", archive_entry_pathname(ae));
208 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
209 
210 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
211 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
212 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
213 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
214 
215 	/* Test file3. */
216 	assertEqualInt(0, stat("file3", &st));
217 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
218 	assertFileSize("file3", 8);
219 	failure("'%s' should not have Resource Fork", "file3");
220 	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
221 	failure("'%s' should not have decmpfs", "file3");
222 	assertEqualInt(0, has_xattr("file3", "com.apple.decmpfs"));
223 	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
224 	assertEqualString(clean_acl(acl_to_text(acl, NULL)),
225 	    "!#acl 1\n"
226 	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
227 	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
228 	);
229 	if (acl) acl_free(acl);
230 	/* Test ._file3. */
231 	failure("'file3' should be merged and removed");
232 	assertFileNotExists("._file3");
233 
234 	assertChdir("..");
235 
236 	assertEqualFile("hfscmp/file3", "nocmp/file3");
237 #endif
238 }
239 
240 /* Test writing apple doubles to disk from zip format */
241 DEFINE_TEST(test_write_disk_appledouble_zip)
242 {
243 #if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
244 	|| !defined(HAVE_ZLIB_H)
245 	skipping("MacOS-specific AppleDouble test");
246 #else
247 	const char *refname = "test_write_disk_appledouble_zip.zip";
248 	struct archive *ad, *a;
249 	struct archive_entry *ae;
250 	struct stat st;
251 
252 	extract_reference_file(refname);
253 
254 	/*
255 	 * Extract an archive to disk.
256 	 */
257 	assert((ad = archive_write_disk_new()) != NULL);
258 	assertEqualIntA(ad, ARCHIVE_OK,
259 	    archive_write_disk_set_standard_lookup(ad));
260 	assertEqualIntA(ad, ARCHIVE_OK,
261 	    archive_write_disk_set_options(ad,
262 		ARCHIVE_EXTRACT_TIME |
263 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
264 		ARCHIVE_EXTRACT_SECURE_NODOTDOT));
265 
266 	assert((a = archive_read_new()) != NULL);
267 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
268 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
269 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
270 	    refname, 512 * 20));
271 
272 	/* Skip The top level directory */
273 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
274 	assertEqualString("apple_double_dir/", archive_entry_pathname(ae));
275 
276 	/* Extract apple_double_test */
277 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
278 	assertEqualString("apple_double_dir/apple_double_dir_test/", archive_entry_pathname(ae));
279 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
280 
281 	/* Extract ._apple_double_dir_test which will be merged into apple_double_dir_test as metadata. */
282 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
283 	assertEqualString("apple_double_dir/._apple_double_dir_test", archive_entry_pathname(ae));
284 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
285 
286 	/* Extract test_file */
287 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
288 	assertEqualString("apple_double_dir/test_file", archive_entry_pathname(ae));
289 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
290 
291 	/* Extract ._test_file which will be merged into test_file as metadata. */
292 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
293 	assertEqualString("apple_double_dir/._test_file", archive_entry_pathname(ae));
294 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
295 
296 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
297 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
298 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
299 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
300 
301 	/* Test test_file */
302 	assertEqualInt(0, stat("apple_double_dir/test_file", &st));
303 	assertFileSize("apple_double_dir/test_file", 5);
304 	failure("'%s' should have Resource Fork", "test_file");
305 	assertEqualInt(1, has_xattr("apple_double_dir/test_file", "com.apple.ResourceFork"));
306 
307 	/* Test apple_double_dir_test */
308 	failure("'%s' should have quarantine xattr", "apple_double_dir_test");
309 	assertEqualInt(1, has_xattr("apple_double_dir/apple_double_dir_test", "com.apple.quarantine"));
310 
311 	/* Test ._test_file. */
312 	failure("'apple_double_dir/._test_file' should be merged and removed");
313 	assertFileNotExists("apple_double_dir/._test_file");
314 
315 	/* Test ._apple_double_dir_test */
316 	failure("'apple_double_dir/._._apple_double_dir_test' should be merged and removed");
317 	assertFileNotExists("apple_double_dir/._apple_double_dir_test");
318 
319 	assertChdir("..");
320 
321 #endif
322 }
323