xref: /freebsd/contrib/libarchive/libarchive/test/test_write_disk_no_hfs_compression.c (revision b9128a37faafede823eb456aa65a11ac69997284)
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_XATTR_H
29 #include <sys/xattr.h>
30 #endif
31 
32 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
33 	&& defined(HAVE_ZLIB_H)
34 static int
35 has_xattr(const char *filename, const char *xattrname)
36 {
37 	char *nl, *nlp;
38 	ssize_t r;
39 	int existing;
40 
41 	r = listxattr(filename, NULL, 0, XATTR_SHOWCOMPRESSION);
42 	if (r < 0)
43 		return (0);
44 	if (r == 0)
45 		return (0);
46 
47 	assert((nl = malloc(r)) != NULL);
48 	if (nl == NULL)
49 		return (0);
50 
51 	r = listxattr(filename, nl, r, XATTR_SHOWCOMPRESSION);
52 	if (r < 0) {
53 		free(nl);
54 		return (0);
55 	}
56 
57 	existing = 0;
58 	for (nlp = nl; nlp < nl + r; nlp += strlen(nlp) + 1) {
59 		if (strcmp(nlp, xattrname) == 0) {
60 			existing = 1;
61 			break;
62 		}
63 	}
64 	free(nl);
65 	return (existing);
66 }
67 #endif
68 
69 /*
70  * Exercise HFS+ Compression.
71  */
72 DEFINE_TEST(test_write_disk_no_hfs_compression)
73 {
74 #if !defined(__APPLE__) || !defined(UF_COMPRESSED) || !defined(HAVE_SYS_XATTR_H)\
75 	|| !defined(HAVE_ZLIB_H)
76 	skipping("MacOS-specific HFS+ Compression test");
77 #else
78 	const char *refname = "test_write_disk_no_hfs_compression.tgz";
79 	struct archive *ad, *a;
80 	struct archive_entry *ae;
81 	struct stat st;
82 
83 	extract_reference_file(refname);
84 
85 	/*
86 	 * Extract an archive to disk with HFS+ Compression
87 	 * the file was compressed.
88 	 */
89 	assert((ad = archive_write_disk_new()) != NULL);
90 	assertEqualIntA(ad, ARCHIVE_OK,
91 	    archive_write_disk_set_standard_lookup(ad));
92 	assertEqualIntA(ad, ARCHIVE_OK,
93 	    archive_write_disk_set_options(ad,
94 		ARCHIVE_EXTRACT_TIME |
95 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
96 		ARCHIVE_EXTRACT_SECURE_NODOTDOT));
97 
98 	assert((a = archive_read_new()) != NULL);
99 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
100 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
101 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
102 	    refname, 512 * 20));
103 
104 	assertMakeDir("hfscmp", 0755);
105 	assertChdir("hfscmp");
106 
107 	/* Extract file1. */
108 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
109 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
110 	/* Extract README. */
111 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
112 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
113 	/* Extract NEWS. */
114 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
115 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
116 	/* Extract Makefile. */
117 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
118 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
119 
120 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
121 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
122 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
123 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
124 
125 	/* Test file1. */
126 	assertEqualInt(0, stat("file1", &st));
127 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
128 	assertFileSize("file1", 8);
129 	assertEqualInt(0, has_xattr("file1", "com.apple.ResourceFork"));
130 	assertEqualInt(0, has_xattr("file1", "com.apple.decmpfs"));
131 
132 	/* Test README. */
133 	assertEqualInt(0, stat("README", &st));
134 	assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
135 	assertFileSize("README", 6586);
136 	assertEqualInt(0, has_xattr("README", "com.apple.ResourceFork"));
137 	assertEqualInt(1, has_xattr("README", "com.apple.decmpfs"));
138 
139 	/* Test NEWS. */
140 	assertEqualInt(0, stat("NEWS", &st));
141 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
142 	assertFileSize("NEWS", 28438);
143 	assertEqualInt(0, has_xattr("NEWS", "com.apple.ResourceFork"));
144 	assertEqualInt(0, has_xattr("NEWS", "com.apple.decmpfs"));
145 
146 	/* Test Makefile. */
147 	assertEqualInt(0, stat("Makefile", &st));
148 	assertEqualInt(UF_COMPRESSED, st.st_flags & UF_COMPRESSED);
149 	assertFileSize("Makefile", 1238119);
150 	assertEqualInt(1, has_xattr("Makefile", "com.apple.ResourceFork"));
151 	assertEqualInt(1, has_xattr("Makefile", "com.apple.decmpfs"));
152 
153 	assertChdir("..");
154 
155 	/*
156 	 * Extract an archive to disk without HFS+ Compression.
157 	 */
158 	assert((ad = archive_write_disk_new()) != NULL);
159 	assertEqualIntA(ad, ARCHIVE_OK,
160 	    archive_write_disk_set_standard_lookup(ad));
161 	assertEqualIntA(ad, ARCHIVE_OK,
162 	    archive_write_disk_set_options(ad,
163 		ARCHIVE_EXTRACT_TIME |
164 		ARCHIVE_EXTRACT_SECURE_SYMLINKS |
165 		ARCHIVE_EXTRACT_SECURE_NODOTDOT |
166 		ARCHIVE_EXTRACT_NO_HFS_COMPRESSION));
167 
168 	assert((a = archive_read_new()) != NULL);
169 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
170 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
171 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a,
172 	    refname, 512 * 20));
173 
174 	assertMakeDir("nocmp", 0755);
175 	assertChdir("nocmp");
176 
177 	/* Extract file1. */
178 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
179 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
180 	/* Extract README. */
181 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
182 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
183 	/* Extract NEWS. */
184 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
185 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
186 	/* Extract Makefile. */
187 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
188 	assertEqualIntA(a, ARCHIVE_OK, archive_read_extract2(a, ae, ad));
189 
190 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
191 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
192 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
193 	assertEqualIntA(ad, ARCHIVE_OK, archive_write_free(ad));
194 
195 	/* Test file1. */
196 	assertEqualInt(0, stat("file1", &st));
197 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
198 	assertFileSize("file1", 8);
199 	assertEqualInt(0, has_xattr("file1", "com.apple.ResourceFork"));
200 	assertEqualInt(0, has_xattr("file1", "com.apple.decmpfs"));
201 
202 	/* Test README. */
203 	assertEqualInt(0, stat("README", &st));
204 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
205 	assertFileSize("README", 6586);
206 	assertEqualInt(0, has_xattr("README", "com.apple.ResourceFork"));
207 	assertEqualInt(0, has_xattr("README", "com.apple.decmpfs"));
208 
209 	/* Test NEWS. */
210 	assertEqualInt(0, stat("NEWS", &st));
211 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
212 	assertFileSize("NEWS", 28438);
213 	assertEqualInt(0, has_xattr("NEWS", "com.apple.ResourceFork"));
214 	assertEqualInt(0, has_xattr("NEWS", "com.apple.decmpfs"));
215 
216 	/* Test Makefile. */
217 	assertEqualInt(0, stat("Makefile", &st));
218 	assertEqualInt(0, st.st_flags & UF_COMPRESSED);
219 	assertFileSize("Makefile", 1238119);
220 	assertEqualInt(0, has_xattr("Makefile", "com.apple.ResourceFork"));
221 	assertEqualInt(0, has_xattr("Makefile", "com.apple.decmpfs"));
222 
223 	assertChdir("..");
224 
225 	assertEqualFile("hfscmp/file1", "nocmp/file1");
226 	assertEqualFile("hfscmp/README", "nocmp/README");
227 	assertEqualFile("hfscmp/NEWS", "nocmp/NEWS");
228 	assertEqualFile("hfscmp/Makefile", "nocmp/Makefile");
229 #endif
230 }
231