1 /*-
2 * Copyright (c) 2003-2008 Tim Kientzle
3 * Copyright (c) 2010 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 #include <locale.h>
29
30 static void
test_xar(const char * option)31 test_xar(const char *option)
32 {
33 char buff2[64];
34 size_t buffsize = 1500;
35 char *buff;
36 struct archive_entry *ae;
37 struct archive *a;
38 size_t used;
39 const char *name;
40 const void *value;
41 size_t size;
42
43 /* Create a new archive in memory. */
44 assert((a = archive_write_new()) != NULL);
45 if (archive_write_set_format_xar(a) != ARCHIVE_OK) {
46 skipping("xar is not supported on this platform");
47 assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
48 return;
49 }
50 assertA(0 == archive_write_add_filter_none(a));
51 if (option != NULL &&
52 archive_write_set_options(a, option) != ARCHIVE_OK) {
53 skipping("option `%s` is not supported on this platform", option);
54 assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
55 return;
56 }
57
58 buff = malloc(buffsize);
59 assert(buff != NULL);
60
61 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
62
63 /*
64 * "file" has a bunch of attributes and 8 bytes of data and
65 * 7 bytes of xattr data and 3 bytes of xattr.
66 */
67 assert((ae = archive_entry_new()) != NULL);
68 archive_entry_set_atime(ae, 2, 20);
69 archive_entry_set_ctime(ae, 4, 40);
70 archive_entry_set_mtime(ae, 5, 50);
71 archive_entry_copy_pathname(ae, "file");
72 archive_entry_set_mode(ae, AE_IFREG | 0755);
73 archive_entry_set_nlink(ae, 2);
74 archive_entry_set_size(ae, 8);
75 archive_entry_xattr_add_entry(ae, "user.data1", "ABCDEFG", 7);
76 archive_entry_xattr_add_entry(ae, "user.data2", "XYZ", 3);
77 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
78 archive_entry_free(ae);
79 assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
80
81 /*
82 * "file2" is symbolic link to file
83 */
84 assert((ae = archive_entry_new()) != NULL);
85 archive_entry_set_atime(ae, 2, 20);
86 archive_entry_set_ctime(ae, 4, 40);
87 archive_entry_set_mtime(ae, 5, 50);
88 archive_entry_copy_pathname(ae, "file2");
89 archive_entry_copy_symlink(ae, "file");
90 archive_entry_set_mode(ae, AE_IFLNK | 0755);
91 archive_entry_set_size(ae, 0);
92 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
93 archive_entry_free(ae);
94
95 /*
96 * "dir/file3" has a bunch of attributes and 8 bytes of data.
97 */
98 assert((ae = archive_entry_new()) != NULL);
99 archive_entry_set_atime(ae, 2, 20);
100 archive_entry_set_ctime(ae, 4, 40);
101 archive_entry_set_mtime(ae, 5, 50);
102 archive_entry_copy_pathname(ae, "dir/file");
103 archive_entry_set_mode(ae, AE_IFREG | 0755);
104 archive_entry_set_size(ae, 8);
105 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
106 archive_entry_free(ae);
107 assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
108
109 /*
110 * "dir/dir2/file4" is hard link to file
111 */
112 assert((ae = archive_entry_new()) != NULL);
113 archive_entry_set_atime(ae, 2, 20);
114 archive_entry_set_ctime(ae, 4, 40);
115 archive_entry_set_mtime(ae, 5, 50);
116 archive_entry_copy_pathname(ae, "dir/dir2/file4");
117 archive_entry_copy_hardlink(ae, "file");
118 archive_entry_set_mode(ae, AE_IFREG | 0755);
119 archive_entry_set_nlink(ae, 2);
120 archive_entry_set_size(ae, 0);
121 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
122 archive_entry_free(ae);
123
124 /*
125 * "dir/dir3" is a directory
126 */
127 assert((ae = archive_entry_new()) != NULL);
128 archive_entry_set_atime(ae, 2, 20);
129 archive_entry_set_ctime(ae, 4, 40);
130 archive_entry_set_mtime(ae, 5, 50);
131 archive_entry_copy_pathname(ae, "dir/dir3");
132 archive_entry_set_mode(ae, AE_IFDIR | 0755);
133 archive_entry_unset_size(ae);
134 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
135 archive_entry_free(ae);
136
137 /*
138 * Add a wrong path "dir/dir2/file4/wrong"
139 */
140 assert((ae = archive_entry_new()) != NULL);
141 archive_entry_set_atime(ae, 2, 20);
142 archive_entry_set_ctime(ae, 4, 40);
143 archive_entry_set_mtime(ae, 5, 50);
144 archive_entry_copy_pathname(ae, "dir/dir2/file4/wrong");
145 archive_entry_set_mode(ae, AE_IFREG | 0755);
146 archive_entry_set_nlink(ae, 1);
147 archive_entry_set_size(ae, 0);
148 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
149 archive_entry_free(ae);
150
151 /*
152 * "dir/file{UNICODE}" has a name that requires base64 encoding
153 */
154 assert((ae = archive_entry_new()) != NULL);
155 archive_entry_set_atime(ae, 2, 20);
156 archive_entry_set_ctime(ae, 4, 40);
157 archive_entry_set_mtime(ae, 5, 50);
158 archive_entry_copy_pathname_w(ae, L"dir/file\U0001F574");
159 archive_entry_set_mode(ae, AE_IFREG | 0755);
160 archive_entry_set_size(ae, 8);
161 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
162 archive_entry_free(ae);
163 assertEqualIntA(a, 8, archive_write_data(a, "ghijklmn", 9));
164
165 /*
166 * XXX TODO XXX Archive directory, other file types.
167 * Archive extended attributes, ACLs, other metadata.
168 * Verify they get read back correctly.
169 */
170
171 /* Close out the archive. */
172 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
173 assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
174
175 /*
176 *
177 * Now, read the data back.
178 *
179 */
180 assert((a = archive_read_new()) != NULL);
181 assertEqualIntA(a, 0, archive_read_support_format_all(a));
182 assertEqualIntA(a, 0, archive_read_support_filter_all(a));
183 assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
184
185 /*
186 * Read "file"
187 */
188 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
189 assertEqualInt(2, archive_entry_atime(ae));
190 assertEqualInt(0, archive_entry_atime_nsec(ae));
191 assertEqualInt(4, archive_entry_ctime(ae));
192 assertEqualInt(0, archive_entry_ctime_nsec(ae));
193 assertEqualInt(5, archive_entry_mtime(ae));
194 assertEqualInt(0, archive_entry_mtime_nsec(ae));
195 assertEqualString("file", archive_entry_pathname(ae));
196 assert((AE_IFREG | 0755) == archive_entry_mode(ae));
197 assertEqualInt(2, archive_entry_nlink(ae));
198 assertEqualInt(8, archive_entry_size(ae));
199 assertEqualInt(2, archive_entry_xattr_reset(ae));
200 assertEqualInt(ARCHIVE_OK,
201 archive_entry_xattr_next(ae, &name, &value, &size));
202 assertEqualString("user.data2", name);
203 assertEqualMem(value, "XYZ", 3);
204 assertEqualInt(ARCHIVE_OK,
205 archive_entry_xattr_next(ae, &name, &value, &size));
206 assertEqualString("user.data1", name);
207 assertEqualMem(value, "ABCDEFG", 7);
208 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
209 assertEqualMem(buff2, "12345678", 8);
210
211 /*
212 * Read "file2"
213 */
214 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
215 assert(archive_entry_atime_is_set(ae));
216 assertEqualInt(2, archive_entry_atime(ae));
217 assertEqualInt(0, archive_entry_atime_nsec(ae));
218 assert(archive_entry_ctime_is_set(ae));
219 assertEqualInt(4, archive_entry_ctime(ae));
220 assertEqualInt(0, archive_entry_ctime_nsec(ae));
221 assert(archive_entry_mtime_is_set(ae));
222 assertEqualInt(5, archive_entry_mtime(ae));
223 assertEqualInt(0, archive_entry_mtime_nsec(ae));
224 assertEqualString("file2", archive_entry_pathname(ae));
225 assertEqualString("file", archive_entry_symlink(ae));
226 assert((AE_IFLNK | 0755) == archive_entry_mode(ae));
227 assertEqualInt(0, archive_entry_size(ae));
228
229 /*
230 * Read "dir/file3"
231 */
232 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
233 assertEqualInt(2, archive_entry_atime(ae));
234 assertEqualInt(0, archive_entry_atime_nsec(ae));
235 assertEqualInt(4, archive_entry_ctime(ae));
236 assertEqualInt(0, archive_entry_ctime_nsec(ae));
237 assertEqualInt(5, archive_entry_mtime(ae));
238 assertEqualInt(0, archive_entry_mtime_nsec(ae));
239 assertEqualString("dir/file", archive_entry_pathname(ae));
240 assert((AE_IFREG | 0755) == archive_entry_mode(ae));
241 assertEqualInt(8, archive_entry_size(ae));
242 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
243 assertEqualMem(buff2, "abcdefgh", 8);
244
245 /*
246 * Read "dir/dir2/file4"
247 */
248 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
249 assert(archive_entry_atime_is_set(ae));
250 assertEqualInt(2, archive_entry_atime(ae));
251 assertEqualInt(0, archive_entry_atime_nsec(ae));
252 assert(archive_entry_ctime_is_set(ae));
253 assertEqualInt(4, archive_entry_ctime(ae));
254 assertEqualInt(0, archive_entry_ctime_nsec(ae));
255 assert(archive_entry_mtime_is_set(ae));
256 assertEqualInt(5, archive_entry_mtime(ae));
257 assertEqualInt(0, archive_entry_mtime_nsec(ae));
258 assertEqualString("dir/dir2/file4", archive_entry_pathname(ae));
259 assertEqualString("file", archive_entry_hardlink(ae));
260 assert((AE_IFREG | 0755) == archive_entry_mode(ae));
261 assertEqualInt(2, archive_entry_nlink(ae));
262 assertEqualInt(0, archive_entry_size(ae));
263
264 /*
265 * Read "dir/dir3"
266 */
267 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
268 assert(archive_entry_atime_is_set(ae));
269 assertEqualInt(2, archive_entry_atime(ae));
270 assertEqualInt(0, archive_entry_atime_nsec(ae));
271 assert(archive_entry_ctime_is_set(ae));
272 assertEqualInt(4, archive_entry_ctime(ae));
273 assertEqualInt(0, archive_entry_ctime_nsec(ae));
274 assert(archive_entry_mtime_is_set(ae));
275 assertEqualInt(5, archive_entry_mtime(ae));
276 assertEqualInt(0, archive_entry_mtime_nsec(ae));
277 assertEqualString("dir/dir3", archive_entry_pathname(ae));
278 assert((AE_IFDIR | 0755) == archive_entry_mode(ae));
279
280 /*
281 * Read "dir/file{UNICODE}"
282 */
283 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
284 assertEqualInt(2, archive_entry_atime(ae));
285 assertEqualInt(0, archive_entry_atime_nsec(ae));
286 assertEqualInt(4, archive_entry_ctime(ae));
287 assertEqualInt(0, archive_entry_ctime_nsec(ae));
288 assertEqualInt(5, archive_entry_mtime(ae));
289 assertEqualInt(0, archive_entry_mtime_nsec(ae));
290 assertEqualWString(L"dir/file\U0001F574", archive_entry_pathname_w(ae));
291 assert((AE_IFREG | 0755) == archive_entry_mode(ae));
292 assertEqualInt(8, archive_entry_size(ae));
293 assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
294 assertEqualMem(buff2, "ghijklmn", 8);
295
296 /*
297 * Verify the end of the archive.
298 */
299 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
300 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
301 assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
302
303 free(buff);
304 }
305
DEFINE_TEST(test_write_format_xar)306 DEFINE_TEST(test_write_format_xar)
307 {
308 /* xar mandates the use of UTF-8 XML; if we cannot
309 * use UTF-8, perhaps we should not write xar. */
310 if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
311 skipping("en_US.UTF-8 locale not available on this system.");
312 return;
313 }
314
315 /* Default mode. */
316 test_xar(NULL);
317
318 /* Disable TOC checksum. */
319 test_xar("!toc-checksum");
320 test_xar("toc-checksum=none");
321 /* Specify TOC checksum type to sha1. */
322 test_xar("toc-checksum=sha1");
323 /* Specify TOC checksum type to md5. */
324 test_xar("toc-checksum=md5");
325
326 /* Disable file checksum. */
327 test_xar("!checksum");
328 test_xar("checksum=none");
329 /* Specify file checksum type to sha1. */
330 test_xar("checksum=sha1");
331 /* Specify file checksum type to md5. */
332 test_xar("checksum=md5");
333
334 /* Disable compression. */
335 test_xar("!compression");
336 test_xar("compression=none");
337 /* Specify compression type to gzip. */
338 test_xar("compression=gzip");
339 test_xar("compression=gzip,compression-level=1");
340 test_xar("compression=gzip,compression-level=9");
341 /* Specify compression type to bzip2. */
342 test_xar("compression=bzip2");
343 test_xar("compression=bzip2,compression-level=1");
344 test_xar("compression=bzip2,compression-level=9");
345 /* Specify compression type to lzma. */
346 test_xar("compression=lzma");
347 test_xar("compression=lzma,compression-level=1");
348 test_xar("compression=lzma,compression-level=9");
349 /* Specify compression type to xz. */
350 test_xar("compression=xz");
351 test_xar("compression=xz,compression-level=1");
352 test_xar("compression=xz,compression-level=9");
353 }
354