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
36#if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
37	&& defined(HAVE_ZLIB_H)
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//
44static char _acl_temp[256];
45static const char *
46clean_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
64static int
65has_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 */
103DEFINE_TEST(test_write_disk_mac_metadata)
104{
105#if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
106	|| !defined(HAVE_ZLIB_H)
107	skipping("MacOS-specific Mac Metadata test");
108#else
109	const char *refname = "test_write_disk_mac_metadata.tar.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_MAC_METADATA));
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	/* Extract file3. */
140	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
141	assertEqualString("file3", archive_entry_pathname(ae));
142	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
143
144	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
145	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
146	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
147	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
148
149	/* Test file3. */
150	assertEqualInt(0, stat("file3", &st));
151	assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
152	assertFileSize("file3", 8);
153	failure("'%s' should not have Resource Fork", "file3");
154	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
155	failure("'%s' should have decompfs xattr", "file3");
156	assertEqualInt(1, has_xattr("file3", "com.apple.decmpfs"));
157	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
158	assertEqualString(clean_acl(acl_to_text(acl, NULL)),
159	    "!#acl 1\n"
160	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
161	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
162	);
163	if (acl) acl_free(acl);
164
165	assertChdir("..");
166
167	/*
168	 * Extract an archive to disk without HFS+ Compression.
169	 */
170	assert((ad = archive_write_disk_new()) != NULL);
171	assertEqualIntA(ad, ARCHIVE_OK,
172	    archive_write_disk_set_standard_lookup(ad));
173	assertEqualIntA(ad, ARCHIVE_OK,
174	    archive_write_disk_set_options(ad,
175		ARCHIVE_EXTRACT_TIME |
176		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
177		ARCHIVE_EXTRACT_SECURE_NODOTDOT |
178		ARCHIVE_EXTRACT_MAC_METADATA |
179		ARCHIVE_EXTRACT_NO_HFS_COMPRESSION));
180
181	assert((a = archive_read_new()) != NULL);
182	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
183	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
184	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
185	    refname, 512 * 20));
186
187	assertMakeDir("nocmp", 0755);
188	assertChdir("nocmp");
189
190	/* Extract file3. */
191	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
192	assertEqualString("file3", archive_entry_pathname(ae));
193	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
194
195	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
196	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
197	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
198	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
199
200	/* Test file3. */
201	assertEqualInt(0, stat("file3", &st));
202	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
203	assertFileSize("file3", 8);
204	failure("'%s' should not have Resource Fork", "file3");
205	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
206	failure("'%s' should not have decmpfs", "file3");
207	assertEqualInt(0, has_xattr("file3", "com.apple.decmpfs"));
208	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
209	assertEqualString(clean_acl(acl_to_text(acl, NULL)),
210	    "!#acl 1\n"
211	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
212	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
213	);
214	if (acl) acl_free(acl);
215
216	assertChdir("..");
217
218	assertEqualFile("hfscmp/file3", "nocmp/file3");
219#endif
220}
221