2 * Copyright (c) 2012 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 __FBSDID("$FreeBSD$");
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)
34 struct archive_entry *ae;
37 size_t buffsize = 1024 * 1024;
41 assert((buff = malloc(buffsize)) != NULL);
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",
51 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
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",
61 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
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));
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));
88 /* Close out the archive. */
89 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
90 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
92 if (image && image_size > 0) {
93 assertEqualMem(buff, image, image_size);
97 * Now, read the data back.
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));
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));
115 * Read and verify the file.
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));
122 assertEqualString("file", archive_entry_pathname(ae));
124 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
125 assertEqualInt(8, archive_entry_size(ae));
127 /* Verify the end of the archive. */
128 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
130 /* Verify archive format. */
131 assertEqualIntA(a, ARCHIVE_FILTER_NONE,
132 archive_filter_code(a, 0));
133 assertEqualIntA(a, format_id, archive_format(a));
135 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
136 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
141 DEFINE_TEST(test_archive_write_set_format_by_name_7zip)
143 test_format_by_name("7zip", "copy", ARCHIVE_FORMAT_7ZIP, 0,
144 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8);
147 DEFINE_TEST(test_archive_write_set_format_by_name_ar)
149 test_format_by_name("ar", NULL, ARCHIVE_FORMAT_AR, 0, NULL, 0);
152 DEFINE_TEST(test_archive_write_set_format_by_name_arbsd)
154 test_format_by_name("arbsd", NULL, ARCHIVE_FORMAT_AR, 0, NULL, 0);
157 DEFINE_TEST(test_archive_write_set_format_by_name_argnu)
159 test_format_by_name("argnu", NULL, ARCHIVE_FORMAT_AR_GNU, 0, NULL, 0);
162 DEFINE_TEST(test_archive_write_set_format_by_name_arsvr4)
164 test_format_by_name("arsvr4", NULL, ARCHIVE_FORMAT_AR_GNU, 0, NULL, 0);
167 DEFINE_TEST(test_archive_write_set_format_by_name_bsdtar)
169 test_format_by_name("bsdtar", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
172 DEFINE_TEST(test_archive_write_set_format_by_name_cd9660)
174 test_format_by_name("cd9660", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1,
178 DEFINE_TEST(test_archive_write_set_format_by_name_cpio)
180 test_format_by_name("cpio", NULL, ARCHIVE_FORMAT_CPIO_POSIX, 0, NULL, 0);
183 DEFINE_TEST(test_archive_write_set_format_by_name_gnutar)
185 test_format_by_name("gnutar", NULL, ARCHIVE_FORMAT_TAR_GNUTAR, 0,
189 DEFINE_TEST(test_archive_write_set_format_by_name_iso)
191 test_format_by_name("iso", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1,
195 DEFINE_TEST(test_archive_write_set_format_by_name_iso9660)
197 test_format_by_name("iso9660", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1,
201 DEFINE_TEST(test_archive_write_set_format_by_name_mtree)
203 test_format_by_name("mtree", NULL, ARCHIVE_FORMAT_MTREE, 2, NULL, 0);
206 DEFINE_TEST(test_archive_write_set_format_by_name_mtree_classic)
208 test_format_by_name("mtree-classic", NULL, ARCHIVE_FORMAT_MTREE, 1,
212 DEFINE_TEST(test_archive_write_set_format_by_name_newc)
214 test_format_by_name("newc", NULL, ARCHIVE_FORMAT_CPIO_SVR4_NOCRC, 0,
218 DEFINE_TEST(test_archive_write_set_format_by_name_odc)
220 test_format_by_name("odc", NULL, ARCHIVE_FORMAT_CPIO_POSIX, 0, NULL, 0);
223 DEFINE_TEST(test_archive_write_set_format_by_name_oldtar)
225 test_format_by_name("oldtar", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0);
228 DEFINE_TEST(test_archive_write_set_format_by_name_pax)
230 test_format_by_name("pax", NULL, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE, 0,
234 DEFINE_TEST(test_archive_write_set_format_by_name_paxr)
236 test_format_by_name("paxr", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
239 DEFINE_TEST(test_archive_write_set_format_by_name_posix)
241 test_format_by_name("posix", NULL, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE, 0,
245 DEFINE_TEST(test_archive_write_set_format_by_name_rpax)
247 test_format_by_name("rpax", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
250 DEFINE_TEST(test_archive_write_set_format_by_name_shar)
252 test_format_by_name("shar", NULL, -1, 0,
253 "#!/bin/sh\n# This is a shell archive\n", 36);
256 DEFINE_TEST(test_archive_write_set_format_by_name_shardump)
258 test_format_by_name("shardump", NULL, -1, 0,
259 "#!/bin/sh\n# This is a shell archive\n", 36);
262 DEFINE_TEST(test_archive_write_set_format_by_name_ustar)
264 test_format_by_name("ustar", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0);
267 DEFINE_TEST(test_archive_write_set_format_by_name_v7tar)
269 test_format_by_name("v7tar", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0);
272 DEFINE_TEST(test_archive_write_set_format_by_name_v7)
274 test_format_by_name("v7", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0);
277 DEFINE_TEST(test_archive_write_set_format_by_name_xar)
279 test_format_by_name("xar", "gzip", ARCHIVE_FORMAT_XAR, 0, NULL, 0);
282 DEFINE_TEST(test_archive_write_set_format_by_name_zip)
284 test_format_by_name("zip", "store", ARCHIVE_FORMAT_ZIP, 0, NULL, 0);