2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011 Michihiro NAKAJIMA
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.
27 __FBSDID("$FreeBSD$");
30 static const int libz_enabled = 1;
32 static const int libz_enabled = 0;
36 * The reference file for this has been manually tweaked so that:
37 * * file2 has length-at-end but file1 does not
38 * * file2 has an invalid CRC
41 verify_basic(struct archive *a, int seek_checks)
43 struct archive_entry *ae;
49 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
50 assertEqualString("dir/", archive_entry_pathname(ae));
51 assertEqualInt(1179604249, archive_entry_mtime(ae));
52 assertEqualInt(0, archive_entry_size(ae));
54 assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae));
55 assertEqualIntA(a, ARCHIVE_EOF,
56 archive_read_data_block(a, &pv, &s, &o));
57 assertEqualInt((int)s, 0);
59 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
60 assertEqualString("file1", archive_entry_pathname(ae));
61 assertEqualInt(1179604289, archive_entry_mtime(ae));
63 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
64 assertEqualInt(18, archive_entry_size(ae));
65 failure("archive_read_data() returns number of bytes read");
67 assertEqualInt(18, archive_read_data(a, buff, 19));
68 assertEqualMem(buff, "hello\nhello\nhello\n", 18);
70 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
71 assertEqualString(archive_error_string(a),
72 "Unsupported ZIP compression method (deflation)");
73 assert(archive_errno(a) != 0);
76 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
77 assertEqualString("file2", archive_entry_pathname(ae));
78 assertEqualInt(1179605932, archive_entry_mtime(ae));
80 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
81 assertEqualInt(64, archive_entry_size_is_set(ae));
83 failure("file2 has length-at-end, so we shouldn't see a valid size when streaming");
84 assertEqualInt(0, archive_entry_size_is_set(ae));
87 failure("file2 has a bad CRC, so read should fail and not change buff");
88 memset(buff, 'a', 19);
89 assertEqualInt(ARCHIVE_WARN, archive_read_data(a, buff, 19));
90 assertEqualMem(buff, "aaaaaaaaaaaaaaaaaaa", 19);
92 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
93 assertEqualString(archive_error_string(a),
94 "Unsupported ZIP compression method (deflation)");
95 assert(archive_errno(a) != 0);
97 assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &ae));
98 /* Verify the number of files read. */
99 failure("the archive file has three files");
100 assertEqualInt(3, archive_file_count(a));
101 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
102 assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
103 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
104 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
110 const char *refname = "test_read_format_zip.zip";
115 extract_reference_file(refname);
117 /* Verify with seeking reader. */
118 assert((a = archive_read_new()) != NULL);
119 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
120 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
121 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
124 /* Verify with streaming reader. */
125 p = slurpfile(&s, refname);
126 assert((a = archive_read_new()) != NULL);
127 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
128 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
129 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 31));
134 * Read Info-ZIP New Unix Extra Field 0x7875 "ux".
135 * Currently stores Unix UID/GID up to 32 bits.
138 verify_info_zip_ux(struct archive *a, int seek_checks)
140 struct archive_entry *ae;
143 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
144 assertEqualString("file1", archive_entry_pathname(ae));
145 assertEqualInt(1300668680, archive_entry_mtime(ae));
146 assertEqualInt(18, archive_entry_size(ae));
148 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae));
149 failure("zip reader should read Info-ZIP New Unix Extra Field");
150 assertEqualInt(1001, archive_entry_uid(ae));
151 assertEqualInt(1001, archive_entry_gid(ae));
153 failure("archive_read_data() returns number of bytes read");
154 assertEqualInt(18, archive_read_data(a, buff, 19));
155 assertEqualMem(buff, "hello\nhello\nhello\n", 18);
157 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
158 assertEqualString(archive_error_string(a),
159 "Unsupported ZIP compression method (deflation)");
160 assert(archive_errno(a) != 0);
162 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
164 /* Verify the number of files read. */
165 failure("the archive file has just one file");
166 assertEqualInt(1, archive_file_count(a));
168 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
169 assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
170 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
171 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
175 test_info_zip_ux(void)
177 const char *refname = "test_read_format_zip_ux.zip";
182 extract_reference_file(refname);
184 /* Verify with seeking reader. */
185 assert((a = archive_read_new()) != NULL);
186 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
187 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
188 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
189 verify_info_zip_ux(a, 1);
191 /* Verify with streaming reader. */
192 p = slurpfile(&s, refname);
193 assert((a = archive_read_new()) != NULL);
194 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
195 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
196 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 108));
197 verify_info_zip_ux(a, 0);
201 * Verify that test_read_extract correctly works with
202 * Zip entries that use length-at-end.
205 verify_extract_length_at_end(struct archive *a, int seek_checks)
207 struct archive_entry *ae;
209 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
211 assertEqualString("hello.txt", archive_entry_pathname(ae));
213 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae));
214 assert(archive_entry_size_is_set(ae));
215 assertEqualInt(6, archive_entry_size(ae));
217 assert(!archive_entry_size_is_set(ae));
218 assertEqualInt(0, archive_entry_size(ae));
222 assertEqualIntA(a, ARCHIVE_OK, archive_read_extract(a, ae, 0));
223 assertFileContents("hello\x0A", 6, "hello.txt");
225 assertEqualIntA(a, ARCHIVE_FAILED, archive_read_extract(a, ae, 0));
226 assertEqualString(archive_error_string(a),
227 "Unsupported ZIP compression method (deflation)");
228 assert(archive_errno(a) != 0);
231 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
232 assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
236 test_extract_length_at_end(void)
238 const char *refname = "test_read_format_zip_length_at_end.zip";
243 extract_reference_file(refname);
245 /* Verify extraction with seeking reader. */
246 assert((a = archive_read_new()) != NULL);
247 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
248 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
249 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
250 verify_extract_length_at_end(a, 1);
252 /* Verify extraction with streaming reader. */
253 p = slurpfile(&s, refname);
254 assert((a = archive_read_new()) != NULL);
255 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
256 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
257 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 108));
258 verify_extract_length_at_end(a, 0);
264 const char *refname = "test_read_format_zip_symlink.zip";
268 struct archive_entry *ae;
270 extract_reference_file(refname);
271 p = slurpfile(&s, refname);
273 /* Symlinks can only be extracted with the seeking reader. */
274 assert((a = archive_read_new()) != NULL);
275 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
276 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));
278 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
279 assertEqualString("file", archive_entry_pathname(ae));
280 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
282 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
283 assertEqualString("symlink", archive_entry_pathname(ae));
284 assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
285 assertEqualInt(0, archive_entry_size(ae));
286 assertEqualString("file", archive_entry_symlink(ae));
288 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
289 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
290 assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
293 DEFINE_TEST(test_read_format_zip)
297 test_extract_length_at_end();