test_write_disk_mac_metadata.c revision 370535
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
37#if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
38	&& defined(HAVE_ZLIB_H)
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//
45static char _acl_temp[256];
46static const char *
47clean_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
65static int
66has_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 */
104DEFINE_TEST(test_write_disk_mac_metadata)
105{
106#if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
107	|| !defined(HAVE_ZLIB_H)
108	skipping("MacOS-specific Mac Metadata test");
109#else
110	const char *refname = "test_write_disk_mac_metadata.tar.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_MAC_METADATA));
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	/* Extract file3. */
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));
144
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));
149
150	/* Test file3. */
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)),
160	    "!#acl 1\n"
161	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
162	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
163	);
164	if (acl) acl_free(acl);
165
166	assertChdir("..");
167
168	/*
169	 * Extract an archive to disk without HFS+ Compression.
170	 */
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));
181
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,
186	    refname, 512 * 20));
187
188	assertMakeDir("nocmp", 0755);
189	assertChdir("nocmp");
190
191	/* Extract file3. */
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));
195
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));
200
201	/* Test file3. */
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)),
211	    "!#acl 1\n"
212	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:::deny:read\n"
213	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
214	);
215	if (acl) acl_free(acl);
216
217	assertChdir("..");
218
219	assertEqualFile("hfscmp/file3", "nocmp/file3");
220#endif
221}
222