]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_archive_write_set_format_by_name.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_archive_write_set_format_by_name.c
1 /*-
2  * Copyright (c) 2012 Michihiro NAKAJIMA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #include "test.h"
28 __FBSDID("$FreeBSD$");
29
30 static void
31 test_format_by_name(const char *format_name, const char *compression_type,
32     int format_id, int dot_stored, const void *image, size_t image_size)
33 {
34         struct archive_entry *ae;
35         struct archive *a;
36         size_t used;
37         size_t buffsize = 1024 * 1024;
38         char *buff;
39         int r;
40
41         assert((buff = malloc(buffsize)) != NULL);
42         if (buff == NULL)
43                 return;
44
45         /* Create a new archive in memory. */
46         assert((a = archive_write_new()) != NULL);
47         r = archive_write_set_format_by_name(a, format_name);
48         if (r == ARCHIVE_WARN) {
49                 skipping("%s format not fully supported on this platform",
50                    compression_type);
51                 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
52                 free(buff);
53                 return;
54         }
55         assertEqualIntA(a, ARCHIVE_OK, r);
56         if (compression_type != NULL &&
57             ARCHIVE_OK != archive_write_set_format_option(a, format_name,
58             "compression", compression_type)) {
59                 skipping("%s writing not fully supported on this platform",
60                    compression_type);
61                 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
62                 free(buff);
63                 return;
64         }
65         assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
66         assertEqualIntA(a, ARCHIVE_OK,
67             archive_write_open_memory(a, buff, buffsize, &used));
68
69         /*
70          * Write a file to it.
71          */
72         assert((ae = archive_entry_new()) != NULL);
73         archive_entry_set_mtime(ae, 1, 0);
74         assertEqualInt(1, archive_entry_mtime(ae));
75         archive_entry_set_ctime(ae, 1, 0);
76         assertEqualInt(1, archive_entry_ctime(ae));
77         archive_entry_set_atime(ae, 1, 0);
78         assertEqualInt(1, archive_entry_atime(ae));
79         archive_entry_copy_pathname(ae, "file");
80         assertEqualString("file", archive_entry_pathname(ae));
81         archive_entry_set_mode(ae, AE_IFREG | 0755);
82         assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
83         archive_entry_set_size(ae, 8);
84         assertEqualInt(0, archive_write_header(a, ae));
85         archive_entry_free(ae);
86         assertEqualInt(8, archive_write_data(a, "12345678", 8));
87
88         /* Close out the archive. */
89         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
90         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
91
92         if (image && image_size > 0) {
93                 assertEqualMem(buff, image, image_size);
94         }
95         if (format_id > 0) {
96                 /*
97                  * Now, read the data back.
98                  */
99                 /* With the test memory reader -- seeking mode. */
100                 assert((a = archive_read_new()) != NULL);
101                 assertEqualIntA(a, ARCHIVE_OK,
102                     archive_read_support_format_all(a));
103                 assertEqualIntA(a, ARCHIVE_OK,
104                     archive_read_support_filter_all(a));
105                 assertEqualIntA(a, ARCHIVE_OK,
106                     read_open_memory_seek(a, buff, used, 7));
107
108                 if (dot_stored & 1) {
109                         assertEqualIntA(a, ARCHIVE_OK,
110                             archive_read_next_header(a, &ae));
111                         assertEqualString(".", archive_entry_pathname(ae));
112                         assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
113                 }
114                 /*
115                  * Read and verify the file.
116                  */
117                 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
118                 assertEqualInt(1, archive_entry_mtime(ae));
119                 if (dot_stored & 2) {
120                         assertEqualString("./file", archive_entry_pathname(ae));
121                 } else {
122                         assertEqualString("file", archive_entry_pathname(ae));
123                 }
124                 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
125                 assertEqualInt(8, archive_entry_size(ae));
126
127                 /* Verify the end of the archive. */
128                 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
129
130                 /* Verify archive format. */
131                 assertEqualIntA(a, ARCHIVE_FILTER_NONE,
132                     archive_filter_code(a, 0));
133                 assertEqualIntA(a, format_id, archive_format(a));
134
135                 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
136                 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
137         }
138         free(buff);
139 }
140
141 DEFINE_TEST(test_archive_write_set_format_by_name_7zip)
142 {
143         test_format_by_name("7zip", "copy", ARCHIVE_FORMAT_7ZIP, 0,
144             "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8);
145 }
146
147 DEFINE_TEST(test_archive_write_set_format_by_name_ar)
148 {
149         test_format_by_name("ar", NULL, ARCHIVE_FORMAT_AR, 0, NULL, 0);
150 }
151
152 DEFINE_TEST(test_archive_write_set_format_by_name_arbsd)
153 {
154         test_format_by_name("arbsd", NULL, ARCHIVE_FORMAT_AR, 0, NULL, 0);
155 }
156
157 DEFINE_TEST(test_archive_write_set_format_by_name_argnu)
158 {
159         test_format_by_name("argnu", NULL, ARCHIVE_FORMAT_AR_GNU, 0, NULL, 0);
160 }
161
162 DEFINE_TEST(test_archive_write_set_format_by_name_arsvr4)
163 {
164         test_format_by_name("arsvr4", NULL, ARCHIVE_FORMAT_AR_GNU, 0, NULL, 0);
165 }
166
167 DEFINE_TEST(test_archive_write_set_format_by_name_bsdtar)
168 {
169         test_format_by_name("bsdtar", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
170 }
171
172 DEFINE_TEST(test_archive_write_set_format_by_name_cd9660)
173 {
174         test_format_by_name("cd9660", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1,
175             NULL, 0);
176 }
177
178 DEFINE_TEST(test_archive_write_set_format_by_name_cpio)
179 {
180         test_format_by_name("cpio", NULL, ARCHIVE_FORMAT_CPIO_POSIX, 0, NULL, 0);
181 }
182
183 DEFINE_TEST(test_archive_write_set_format_by_name_gnutar)
184 {
185         test_format_by_name("gnutar", NULL, ARCHIVE_FORMAT_TAR_GNUTAR, 0,
186             NULL, 0);
187 }
188
189 DEFINE_TEST(test_archive_write_set_format_by_name_iso)
190 {
191         test_format_by_name("iso", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1,
192             NULL, 0);
193 }
194
195 DEFINE_TEST(test_archive_write_set_format_by_name_iso9660)
196 {
197         test_format_by_name("iso9660", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1,
198             NULL, 0);
199 }
200
201 DEFINE_TEST(test_archive_write_set_format_by_name_mtree)
202 {
203         test_format_by_name("mtree", NULL, ARCHIVE_FORMAT_MTREE, 2, NULL, 0);
204 }
205
206 DEFINE_TEST(test_archive_write_set_format_by_name_mtree_classic)
207 {
208         test_format_by_name("mtree-classic", NULL, ARCHIVE_FORMAT_MTREE, 1,
209             NULL, 0);
210 }
211
212 DEFINE_TEST(test_archive_write_set_format_by_name_newc)
213 {
214         test_format_by_name("newc", NULL, ARCHIVE_FORMAT_CPIO_SVR4_NOCRC, 0,
215             NULL, 0);
216 }
217
218 DEFINE_TEST(test_archive_write_set_format_by_name_odc)
219 {
220         test_format_by_name("odc", NULL, ARCHIVE_FORMAT_CPIO_POSIX, 0, NULL, 0);
221 }
222
223 DEFINE_TEST(test_archive_write_set_format_by_name_oldtar)
224 {
225         test_format_by_name("oldtar", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0);
226 }
227
228 DEFINE_TEST(test_archive_write_set_format_by_name_pax)
229 {
230         test_format_by_name("pax", NULL, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE, 0,
231             NULL, 0);
232 }
233
234 DEFINE_TEST(test_archive_write_set_format_by_name_paxr)
235 {
236         test_format_by_name("paxr", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
237 }
238
239 DEFINE_TEST(test_archive_write_set_format_by_name_posix)
240 {
241         test_format_by_name("posix", NULL, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE, 0,
242             NULL, 0);
243 }
244
245 DEFINE_TEST(test_archive_write_set_format_by_name_rpax)
246 {
247         test_format_by_name("rpax", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
248 }
249
250 DEFINE_TEST(test_archive_write_set_format_by_name_shar)
251 {
252         test_format_by_name("shar", NULL, -1, 0,
253             "#!/bin/sh\n# This is a shell archive\n", 36);
254 }
255
256 DEFINE_TEST(test_archive_write_set_format_by_name_shardump)
257 {
258         test_format_by_name("shardump", NULL, -1, 0,
259             "#!/bin/sh\n# This is a shell archive\n", 36);
260 }
261
262 DEFINE_TEST(test_archive_write_set_format_by_name_ustar)
263 {
264         test_format_by_name("ustar", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
265 }
266
267 DEFINE_TEST(test_archive_write_set_format_by_name_v7tar)
268 {
269         test_format_by_name("v7tar", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0);
270 }
271
272 DEFINE_TEST(test_archive_write_set_format_by_name_v7)
273 {
274         test_format_by_name("v7", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0);
275 }
276
277 DEFINE_TEST(test_archive_write_set_format_by_name_warc)
278 {
279         test_format_by_name("warc", NULL, ARCHIVE_FORMAT_WARC, 0, NULL, 0);
280 }
281
282 DEFINE_TEST(test_archive_write_set_format_by_name_xar)
283 {
284         test_format_by_name("xar", "gzip", ARCHIVE_FORMAT_XAR, 0, NULL, 0);
285 }
286
287 DEFINE_TEST(test_archive_write_set_format_by_name_zip)
288 {
289         test_format_by_name("zip", "store", ARCHIVE_FORMAT_ZIP, 0, NULL, 0);
290 }