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