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