2 * Copyright (c) 2012 Michihiro NAKAJIMA
3 * Copyright (c) 2015 Okhotnikov Kirill
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 __FBSDID("$FreeBSD$");
32 test_format_filter_by_ext(const char *output_file,
33 int format_id, int filter_id, int dot_stored, char * def_ext)
35 struct archive_entry *ae;
38 size_t buffsize = 1024 * 1024;
42 assert((buff = malloc(buffsize)) != NULL);
46 /* Create a new archive in memory. */
47 assert((a = archive_write_new()) != NULL);
49 r = archive_write_set_format_filter_by_ext(a, output_file);
51 r = archive_write_set_format_filter_by_ext_def(a, output_file, def_ext);
52 if (r == ARCHIVE_WARN) {
53 skipping("%s format not fully supported on this platform",
54 archive_format_name(a));
55 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
58 } else if (r == ARCHIVE_FATAL &&
59 (strcmp(archive_error_string(a),
60 "lzma compression not supported on this platform") == 0 ||
61 strcmp(archive_error_string(a),
62 "xz compression not supported on this platform") == 0)) {
63 const char *filter_name = archive_filter_name(a, 0);
64 skipping("%s filter not suported on this platform", filter_name);
65 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
69 if (!assertEqualIntA(a, ARCHIVE_OK, r)) {
70 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
76 assertEqualIntA(a, ARCHIVE_OK,
77 archive_write_open_memory(a, buff, buffsize, &used));
82 assert((ae = archive_entry_new()) != NULL);
83 archive_entry_set_mtime(ae, 1, 0);
84 assertEqualInt(1, archive_entry_mtime(ae));
85 archive_entry_set_ctime(ae, 1, 0);
86 assertEqualInt(1, archive_entry_ctime(ae));
87 archive_entry_set_atime(ae, 1, 0);
88 assertEqualInt(1, archive_entry_atime(ae));
89 archive_entry_copy_pathname(ae, "file");
90 assertEqualString("file", archive_entry_pathname(ae));
91 archive_entry_set_mode(ae, AE_IFREG | 0755);
92 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
93 archive_entry_set_size(ae, 8);
94 assertEqualInt(0, archive_write_header(a, ae));
95 archive_entry_free(ae);
96 assertEqualInt(8, archive_write_data(a, "12345678", 8));
98 /* Close out the archive. */
99 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
100 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
104 * Now, read the data back.
106 /* With the test memory reader -- seeking mode. */
107 assert((a = archive_read_new()) != NULL);
108 assertEqualIntA(a, ARCHIVE_OK,
109 archive_read_support_format_all(a));
110 assertEqualIntA(a, ARCHIVE_OK,
111 archive_read_support_filter_all(a));
112 assertEqualIntA(a, ARCHIVE_OK,
113 read_open_memory_seek(a, buff, used, 7));
115 if (dot_stored & 1) {
116 assertEqualIntA(a, ARCHIVE_OK,
117 archive_read_next_header(a, &ae));
118 assertEqualString(".", archive_entry_pathname(ae));
119 assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
122 * Read and verify the file.
124 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
125 assertEqualInt(1, archive_entry_mtime(ae));
126 if (dot_stored & 2) {
127 assertEqualString("./file", archive_entry_pathname(ae));
129 assertEqualString("file", archive_entry_pathname(ae));
131 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
132 assertEqualInt(8, archive_entry_size(ae));
134 /* Verify the end of the archive. */
135 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
137 /* Verify archive format. */
138 assertEqualIntA(a, filter_id,
139 archive_filter_code(a, 0));
140 assertEqualIntA(a, format_id, archive_format(a) & ARCHIVE_FORMAT_BASE_MASK );
142 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
143 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
148 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_7zip)
150 test_format_filter_by_ext("./data/test.7z", ARCHIVE_FORMAT_7ZIP, ARCHIVE_FILTER_NONE, 0, NULL);
153 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_zip)
155 test_format_filter_by_ext("./data/test.zip", ARCHIVE_FORMAT_ZIP, ARCHIVE_FILTER_NONE, 0, NULL);
158 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_jar)
160 test_format_filter_by_ext("./data/test.jar", ARCHIVE_FORMAT_ZIP, ARCHIVE_FILTER_NONE, 0, NULL);
163 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_a)
165 test_format_filter_by_ext("./data/test.a", ARCHIVE_FORMAT_AR, ARCHIVE_FILTER_NONE, 0, NULL);
168 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_ar)
170 test_format_filter_by_ext("./data/test.ar", ARCHIVE_FORMAT_AR, ARCHIVE_FILTER_NONE, 0, NULL);
173 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_cpio)
175 test_format_filter_by_ext("./data/test.cpio", ARCHIVE_FORMAT_CPIO, ARCHIVE_FILTER_NONE, 0, NULL);
178 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_iso)
180 test_format_filter_by_ext("./data/test.iso", ARCHIVE_FORMAT_ISO9660, ARCHIVE_FILTER_NONE, 1, NULL);
183 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_tar)
185 test_format_filter_by_ext("./data/test.tar", ARCHIVE_FORMAT_TAR, ARCHIVE_FILTER_NONE, 0, NULL);
188 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_tar_gz)
190 test_format_filter_by_ext("./data/test.tar.gz", ARCHIVE_FORMAT_TAR, ARCHIVE_FILTER_GZIP, 20, NULL);
193 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_tar_bz2)
195 test_format_filter_by_ext("./data/test.tar.bz2", ARCHIVE_FORMAT_TAR, ARCHIVE_FILTER_BZIP2, 0, NULL);
198 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_tar_xz)
200 test_format_filter_by_ext("./data/test.tar.xz", ARCHIVE_FORMAT_TAR, ARCHIVE_FILTER_XZ, 0, NULL);
203 DEFINE_TEST(test_archive_write_set_format_filter_by_no_ext_def_zip)
205 test_format_filter_by_ext("./data/test", ARCHIVE_FORMAT_ZIP, ARCHIVE_FILTER_NONE, 0, ".zip");
208 DEFINE_TEST(test_archive_write_set_format_filter_by_ext_tar_bz2_def_zip)
210 test_format_filter_by_ext("./data/test.tar.bz2", ARCHIVE_FORMAT_TAR, ARCHIVE_FILTER_BZIP2, 0, ".zip");