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)
38static int
39has_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 */
77DEFINE_TEST(test_write_disk_mac_metadata)
78{
79#if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
80	|| !defined(HAVE_ZLIB_H)
81	skipping("MacOS-specific Mac Metadata test");
82#else
83	const char *refname = "test_write_disk_mac_metadata.tar.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_MAC_METADATA));
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	/* Extract file3. */
114	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
115	assertEqualString("file3", archive_entry_pathname(ae));
116	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
117
118	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
119	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
120	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
121	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
122
123	/* Test file3. */
124	assertEqualInt(0, stat("file3", &st));
125	assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
126	assertFileSize("file3", 8);
127	failure("'%s' should not have Resource Fork", "file3");
128	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
129	failure("'%s' should have decompfs xattr", "file3");
130	assertEqualInt(1, has_xattr("file3", "com.apple.decmpfs"));
131	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
132	assertEqualString(acl_to_text(acl, NULL),
133	    "!#acl 1\n"
134	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:Guest:201:deny:read\n"
135	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
136	);
137	if (acl) acl_free(acl);
138
139	assertChdir("..");
140
141	/*
142	 * Extract an archive to disk without HFS+ Compression.
143	 */
144	assert((ad = archive_write_disk_new()) != NULL);
145	assertEqualIntA(ad, ARCHIVE_OK,
146	    archive_write_disk_set_standard_lookup(ad));
147	assertEqualIntA(ad, ARCHIVE_OK,
148	    archive_write_disk_set_options(ad,
149		ARCHIVE_EXTRACT_TIME |
150		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
151		ARCHIVE_EXTRACT_SECURE_NODOTDOT |
152		ARCHIVE_EXTRACT_MAC_METADATA |
153		ARCHIVE_EXTRACT_NO_HFS_COMPRESSION));
154
155	assert((a = archive_read_new()) != NULL);
156	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
157	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
158	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
159	    refname, 512 * 20));
160
161	assertMakeDir("nocmp", 0755);
162	assertChdir("nocmp");
163
164	/* Extract file3. */
165	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
166	assertEqualString("file3", archive_entry_pathname(ae));
167	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
168
169	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
170	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
171	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
172	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
173
174	/* Test file3. */
175	assertEqualInt(0, stat("file3", &st));
176	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
177	assertFileSize("file3", 8);
178	failure("'%s' should not have Resource Fork", "file3");
179	assertEqualInt(0, has_xattr("file3", "com.apple.ResourceFork"));
180	failure("'%s' should not have decmpfs", "file3");
181	assertEqualInt(0, has_xattr("file3", "com.apple.decmpfs"));
182	assert(NULL != (acl = acl_get_file("file3", ACL_TYPE_EXTENDED)));
183	assertEqualString(acl_to_text(acl, NULL),
184	    "!#acl 1\n"
185	    "user:FFFFEEEE-DDDD-CCCC-BBBB-AAAA000000C9:Guest:201:deny:read\n"
186	    "group:ABCDEFAB-CDEF-ABCD-EFAB-CDEF00000050:admin:80:allow:write\n"
187	);
188	if (acl) acl_free(acl);
189
190	assertChdir("..");
191
192	assertEqualFile("hfscmp/file3", "nocmp/file3");
193#endif
194}
195