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 static int
39 has_xattr(const char *filename, const char *xattrname)
40 {
41 	char *nl, *nlp;
42 	ssize_t r;
43 	int exisiting;
44 
45 	r = listxattr(filename, NULL, 0, XATTR_SHOWCOMPRESSION);
46 	if (r < 0)
47 		return (0);
48 	if (r == 0)
49 		return (0);
50 
51 	nl = malloc(r);
52 	if (!assert(nl != NULL))
53 		return (0);
54 
55 	r = listxattr(filename, nl, r, XATTR_SHOWCOMPRESSION);
56 	if (r < 0) {
57 		free(nl);
58 		return (0);
59 	}
60 
61 	exisiting = 0;
62 	for (nlp = nl; nlp < nl + r; nlp += strlen(nlp) + 1) {
63 		if (strcmp(nlp, xattrname) == 0) {
64 			exisiting = 1;
65 			break;
66 		}
67 	}
68 	free(nl);
69 	return (exisiting);
70 }
71 
72 #endif
73 
74 /*
75  * Exercise HFS+ Compression.
76  */
77 DEFINE_TEST(test_write_disk_appledouble)
78 {
79 #if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
80 	|| !defined(HAVE_ZLIB_H)
81 	skipping("MacOS-specific AppleDouble test");
82 #else
83 	const char *refname = "test_write_disk_appledouble.cpio.gz";
84 	struct archive *ad, *a;
85 	struct archive_entry *ae;
86 	struct stat st;
87 	acl_t acl;
88 
89 	extract_reference_file(refname);
90 
91 	/*
92 	 * Extract an archive to disk with HFS+ Compression.
93 	 */
94 	assert((ad = archive_write_disk_new()) != NULL);
95 	assertEqualIntA(ad, ARCHIVE_OK,
96 	    archive_write_disk_set_standard_lookup(ad));
97 	assertEqualIntA(ad, ARCHIVE_OK,
98 	    archive_write_disk_set_options(ad,
99 		ARCHIVE_EXTRACT_TIME |
100 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
101 		ARCHIVE_EXTRACT_SECURE_NODOTDOT |
102 		ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED));
103 
104 	assert((a = archive_read_new()) != NULL);
105 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
106 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
107 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
108 	    refname, 512 * 20));
109 
110 	assertMakeDir("hfscmp", 0755);
111 	assertChdir("hfscmp");
112 
113 	/* Skip "." */
114 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
115 	assertEqualString(".", archive_entry_pathname(ae));
116 	/* Extract file3. */
117 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
118 	assertEqualString("./file3", archive_entry_pathname(ae));
119 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
120 	/* Extract ._file3 which will be merged into file3 as medtadata. */
121 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
122 	assertEqualString("./._file3", archive_entry_pathname(ae));
123 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
124 
125 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
126 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
127 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
128 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
129 
130 	/* Test file3. */
131 	assertEqualInt(0, stat("file3", &st));
132 	assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
133 	assertFileSize("file3", 8);
134 	failure("'%s' should not have Resource Fork", "file3");
135 	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
136 	failure("'%s' should have decompfs xattr", "file3");
137 	assertEqualInt(1, has_xattr("file3", "com.apple.decmpfs"));
138 	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
139 	assertEqualString(acl_to_text(acl, NULL),
140 	    "!#acl 1\n"
141 	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:Guest:201:deny:read\n"
142 	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
143 	);
144 	if (acl) acl_free(acl);
145 	/* Test ._file3. */
146 	failure("'file3' should be merged and removed");
147 	assertFileNotExists("._file3");
148 
149 	assertChdir("..");
150 
151 	/*
152 	 * Extract an archive to disk without HFS+ Compression.
153 	 */
154 	assert((ad = archive_write_disk_new()) != NULL);
155 	assertEqualIntA(ad, ARCHIVE_OK,
156 	    archive_write_disk_set_standard_lookup(ad));
157 	assertEqualIntA(ad, ARCHIVE_OK,
158 	    archive_write_disk_set_options(ad,
159 		ARCHIVE_EXTRACT_TIME |
160 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
161 		ARCHIVE_EXTRACT_SECURE_NODOTDOT));
162 
163 	assert((a = archive_read_new()) != NULL);
164 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
165 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
166 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
167 	    refname, 512 * 20));
168 
169 	assertMakeDir("nocmp", 0755);
170 	assertChdir("nocmp");
171 
172 	/* Skip "." */
173 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
174 	assertEqualString(".", archive_entry_pathname(ae));
175 	/* Extract file3. */
176 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
177 	assertEqualString("./file3", archive_entry_pathname(ae));
178 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
179 	/* Extract ._file3 which will be merged into file3 as medtadata. */
180 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
181 	assertEqualString("./._file3", archive_entry_pathname(ae));
182 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
183 
184 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
185 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
186 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
187 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
188 
189 	/* Test file3. */
190 	assertEqualInt(0, stat("file3", &st));
191 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
192 	assertFileSize("file3", 8);
193 	failure("'%s' should not have Resource Fork", "file3");
194 	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
195 	failure("'%s' should not have decmpfs", "file3");
196 	assertEqualInt(0, has_xattr("file3", "com.apple.decmpfs"));
197 	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
198 	assertEqualString(acl_to_text(acl, NULL),
199 	    "!#acl 1\n"
200 	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:Guest:201:deny:read\n"
201 	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
202 	);
203 	if (acl) acl_free(acl);
204 	/* Test ._file3. */
205 	failure("'file3' should be merged and removed");
206 	assertFileNotExists("._file3");
207 
208 	assertChdir("..");
209 
210 	assertEqualFile("hfscmp/file3", "nocmp/file3");
211 #endif
212 }
213