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 __FBSDID("$FreeBSD$");
28 
29 #ifdef HAVE_SYS_ACL_H
30 #include <sys/acl.h>
31 #endif
32 #ifdef HAVE_SYS_XATTR_H
33 #include <sys/xattr.h>
34 #endif
35 
36 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
37 	&& defined(HAVE_ZLIB_H)
38 
39 //
40 // The test ACL used here is sometimes assigned to the 'Guest' user
41 // This changes the text and breaks the test.  This function simply
42 // strips the 'Guest' information from the string to help ensure
43 // consistent results on different machines.
44 //
45 static char _acl_temp[256];
46 static const char *
47 clean_acl(const char *acl) {
48 	char *p, *q;
49 	if (strlen(acl) >= sizeof(_acl_temp))
50 		return acl;
51 
52 	strcpy(_acl_temp, acl);
53 	p = strstr(_acl_temp, ":Guest:");
54 	if (p != NULL) {
55 		fprintf(stderr, "Shortening: %s\n", p + 1);
56 		memmove(p + 1, p + 6, strlen(p + 6) + 1);
57 		q = strstr(p + 2, ":");
58 		fprintf(stderr, "Shortening: %s\n", q);
59 		memmove(p + 2, q, strlen(q) + 1);
60 		return _acl_temp;
61 	}
62 	return _acl_temp;
63 }
64 
65 static int
66 has_xattr(const char *filename, const char *xattrname)
67 {
68 	char *nl, *nlp;
69 	ssize_t r;
70 	int existing;
71 
72 	r = listxattr(filename, NULL, 0, XATTR_SHOWCOMPRESSION);
73 	if (r < 0)
74 		return (0);
75 	if (r == 0)
76 		return (0);
77 
78 	assert((nl = malloc(r)) != NULL);
79 	if (nl == NULL)
80 		return (0);
81 
82 	r = listxattr(filename, nl, r, XATTR_SHOWCOMPRESSION);
83 	if (r < 0) {
84 		free(nl);
85 		return (0);
86 	}
87 
88 	existing = 0;
89 	for (nlp = nl; nlp < nl + r; nlp += strlen(nlp) + 1) {
90 		if (strcmp(nlp, xattrname) == 0) {
91 			existing = 1;
92 			break;
93 		}
94 	}
95 	free(nl);
96 	return (existing);
97 }
98 
99 #endif
100 
101 /*
102  * Exercise HFS+ Compression.
103  */
104 DEFINE_TEST(test_write_disk_appledouble)
105 {
106 #if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
107 	|| !defined(HAVE_ZLIB_H)
108 	skipping("MacOS-specific AppleDouble test");
109 #else
110 	const char *refname = "test_write_disk_appledouble.cpio.gz";
111 	struct archive *ad, *a;
112 	struct archive_entry *ae;
113 	struct stat st;
114 	acl_t acl;
115 
116 	extract_reference_file(refname);
117 
118 	/*
119 	 * Extract an archive to disk with HFS+ Compression.
120 	 */
121 	assert((ad = archive_write_disk_new()) != NULL);
122 	assertEqualIntA(ad, ARCHIVE_OK,
123 	    archive_write_disk_set_standard_lookup(ad));
124 	assertEqualIntA(ad, ARCHIVE_OK,
125 	    archive_write_disk_set_options(ad,
126 		ARCHIVE_EXTRACT_TIME |
127 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
128 		ARCHIVE_EXTRACT_SECURE_NODOTDOT |
129 		ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED));
130 
131 	assert((a = archive_read_new()) != NULL);
132 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
133 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
134 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
135 	    refname, 512 * 20));
136 
137 	assertMakeDir("hfscmp", 0755);
138 	assertChdir("hfscmp");
139 
140 	/* Skip "." */
141 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
142 	assertEqualString(".", archive_entry_pathname(ae));
143 	/* Extract file3. */
144 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
145 	assertEqualString("./file3", archive_entry_pathname(ae));
146 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
147 	/* Extract ._file3 which will be merged into file3 as metadata. */
148 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
149 	assertEqualString("./._file3", archive_entry_pathname(ae));
150 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
151 
152 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
153 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
154 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
155 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
156 
157 	/* Test file3. */
158 	assertEqualInt(0, stat("file3", &st));
159 	assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
160 	assertFileSize("file3", 8);
161 	failure("'%s' should not have Resource Fork", "file3");
162 	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
163 	failure("'%s' should have decompfs xattr", "file3");
164 	assertEqualInt(1, has_xattr("file3", "com.apple.decmpfs"));
165 	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
166 	assertEqualString(clean_acl(acl_to_text(acl, NULL)),
167 	    "!#acl 1\n"
168 	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
169 	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
170 	);
171 	if (acl) acl_free(acl);
172 	/* Test ._file3. */
173 	failure("'file3' should be merged and removed");
174 	assertFileNotExists("._file3");
175 
176 	assertChdir("..");
177 
178 	/*
179 	 * Extract an archive to disk without HFS+ Compression.
180 	 */
181 	assert((ad = archive_write_disk_new()) != NULL);
182 	assertEqualIntA(ad, ARCHIVE_OK,
183 	    archive_write_disk_set_standard_lookup(ad));
184 	assertEqualIntA(ad, ARCHIVE_OK,
185 	    archive_write_disk_set_options(ad,
186 		ARCHIVE_EXTRACT_TIME |
187 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
188 		ARCHIVE_EXTRACT_SECURE_NODOTDOT));
189 
190 	assert((a = archive_read_new()) != NULL);
191 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
192 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
193 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
194 	    refname, 512 * 20));
195 
196 	assertMakeDir("nocmp", 0755);
197 	assertChdir("nocmp");
198 
199 	/* Skip "." */
200 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
201 	assertEqualString(".", archive_entry_pathname(ae));
202 	/* Extract file3. */
203 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
204 	assertEqualString("./file3", archive_entry_pathname(ae));
205 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
206 	/* Extract ._file3 which will be merged into file3 as metadata. */
207 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
208 	assertEqualString("./._file3", archive_entry_pathname(ae));
209 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
210 
211 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
212 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
213 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
214 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
215 
216 	/* Test file3. */
217 	assertEqualInt(0, stat("file3", &st));
218 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
219 	assertFileSize("file3", 8);
220 	failure("'%s' should not have Resource Fork", "file3");
221 	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
222 	failure("'%s' should not have decmpfs", "file3");
223 	assertEqualInt(0, has_xattr("file3", "com.apple.decmpfs"));
224 	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
225 	assertEqualString(clean_acl(acl_to_text(acl, NULL)),
226 	    "!#acl 1\n"
227 	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
228 	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
229 	);
230 	if (acl) acl_free(acl);
231 	/* Test ._file3. */
232 	failure("'file3' should be merged and removed");
233 	assertFileNotExists("._file3");
234 
235 	assertChdir("..");
236 
237 	assertEqualFile("hfscmp/file3", "nocmp/file3");
238 #endif
239 }
240