2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 __FBSDID("$FreeBSD$");
32 #ifdef HAVE_SYS_XATTR_H
33 #include <sys/xattr.h>
37 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
38 && defined(HAVE_ZLIB_H)
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.
45 static char _acl_temp[256];
47 clean_acl(const char *acl) {
49 if (strlen(acl) >= sizeof(_acl_temp))
52 strcpy(_acl_temp, acl);
53 p = strstr(_acl_temp, ":Guest:");
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);
66 has_xattr(const char *filename, const char *xattrname)
72 r = listxattr(filename, NULL, 0, XATTR_SHOWCOMPRESSION);
78 assert((nl = malloc(r)) != NULL);
82 r = listxattr(filename, nl, r, XATTR_SHOWCOMPRESSION);
89 for (nlp = nl; nlp < nl + r; nlp += strlen(nlp) + 1) {
90 if (strcmp(nlp, xattrname) == 0) {
102 * Exercise HFS+ Compression.
104 DEFINE_TEST(test_write_disk_mac_metadata)
106 #if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
107 || !defined(HAVE_ZLIB_H)
108 skipping("MacOS-specific Mac Metadata test");
110 const char *refname = "test_write_disk_mac_metadata.tar.gz";
111 struct archive *ad, *a;
112 struct archive_entry *ae;
116 extract_reference_file(refname);
119 * Extract an archive to disk with HFS+ Compression.
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_MAC_METADATA));
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,
137 assertMakeDir("hfscmp", 0755);
138 assertChdir("hfscmp");
141 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
142 assertEqualString("file3", archive_entry_pathname(ae));
143 assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
145 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
146 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
147 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
148 assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
151 assertEqualInt(0, stat("file3", &st));
152 assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
153 assertFileSize("file3", 8);
154 failure("'%s' should not have Resource Fork", "file3");
155 assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
156 failure("'%s' should have decompfs xattr", "file3");
157 assertEqualInt(1, has_xattr("file3", "com.apple.decmpfs"));
158 assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
159 assertEqualString(clean_acl(acl_to_text(acl, NULL)),
161 "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
162 "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
164 if (acl) acl_free(acl);
169 * Extract an archive to disk without HFS+ Compression.
171 assert((ad = archive_write_disk_new()) != NULL);
172 assertEqualIntA(ad, ARCHIVE_OK,
173 archive_write_disk_set_standard_lookup(ad));
174 assertEqualIntA(ad, ARCHIVE_OK,
175 archive_write_disk_set_options(ad,
176 ARCHIVE_EXTRACT_TIME |
177 ARCHIVE_EXTRACT_SECURE_SYMLINKS |
178 ARCHIVE_EXTRACT_SECURE_NODOTDOT |
179 ARCHIVE_EXTRACT_MAC_METADATA |
180 ARCHIVE_EXTRACT_NO_HFS_COMPRESSION));
182 assert((a = archive_read_new()) != NULL);
183 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
184 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
185 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
188 assertMakeDir("nocmp", 0755);
189 assertChdir("nocmp");
192 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
193 assertEqualString("file3", archive_entry_pathname(ae));
194 assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
196 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
197 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
198 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
199 assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
202 assertEqualInt(0, stat("file3", &st));
203 assertEqualInt(0, st.st_flags & UF_COMPRESSED);
204 assertFileSize("file3", 8);
205 failure("'%s' should not have Resource Fork", "file3");
206 assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
207 failure("'%s' should not have decmpfs", "file3");
208 assertEqualInt(0, has_xattr("file3", "com.apple.decmpfs"));
209 assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
210 assertEqualString(clean_acl(acl_to_text(acl, NULL)),
212 "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
213 "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
215 if (acl) acl_free(acl);
219 assertEqualFile("hfscmp/file3", "nocmp/file3");