1 /*-
2 * Copyright (c) 2013 Tim Kientzle
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 #include "test.h"
26 __FBSDID("$FreeBSD$");
27
28 /*
29 * Sample file was created with:
30 * echo file0 | zip
31 * Info-Zip uses Zip64 extensions in this case.
32 */
33 static void
verify_file0_seek(struct archive * a)34 verify_file0_seek(struct archive *a)
35 {
36 struct archive_entry *ae;
37
38 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
39 assertEqualString("-", archive_entry_pathname(ae));
40 assertEqualInt(AE_IFREG | 0660, archive_entry_mode(ae));
41 assertEqualInt(6, archive_entry_size(ae));
42 #ifdef HAVE_ZLIB_H
43 {
44 char data[16];
45 assertEqualIntA(a, 6, archive_read_data(a, data, 16));
46 assertEqualMem(data, "file0\x0a", 6);
47 }
48 #endif
49 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
50 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
51 assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
52 }
53
54 static void
verify_file0_stream(struct archive * a,int size_known)55 verify_file0_stream(struct archive *a, int size_known)
56 {
57 struct archive_entry *ae;
58
59 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
60 assertEqualString("-", archive_entry_pathname(ae));
61 assertEqualInt(AE_IFREG | 0664, archive_entry_mode(ae));
62 if (size_known) {
63 // zip64b has the uncompressed size at the beginning,
64 // plus CRC and compressed size using length-at-end.
65 assert(archive_entry_size_is_set(ae));
66 assertEqualInt(6, archive_entry_size(ae));
67 } else {
68 // zip64a does not have a size at the beginning at all.
69 assert(!archive_entry_size_is_set(ae));
70 }
71 #ifdef HAVE_ZLIB_H
72 {
73 char data[16];
74 assertEqualIntA(a, 6, archive_read_data(a, data, 16));
75 assertEqualMem(data, "file0\x0a", 6);
76 }
77 #endif
78 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
79 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
80 assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
81 }
82
DEFINE_TEST(test_read_format_zip_zip64a)83 DEFINE_TEST(test_read_format_zip_zip64a)
84 {
85 const char *refname = "test_read_format_zip_zip64a.zip";
86 struct archive *a;
87 char *p;
88 size_t s;
89
90 extract_reference_file(refname);
91 p = slurpfile(&s, refname);
92
93 /* First read with seeking. */
94 assert((a = archive_read_new()) != NULL);
95 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip_seekable(a));
96 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));
97 verify_file0_seek(a);
98
99 /* Then read streaming. */
100 assert((a = archive_read_new()) != NULL);
101 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip_streamable(a));
102 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));
103 verify_file0_stream(a, 0);
104 free(p);
105 }
106
DEFINE_TEST(test_read_format_zip_zip64b)107 DEFINE_TEST(test_read_format_zip_zip64b)
108 {
109 const char *refname = "test_read_format_zip_zip64b.zip";
110 struct archive *a;
111 char *p;
112 size_t s;
113
114 extract_reference_file(refname);
115 p = slurpfile(&s, refname);
116
117 /* First read with seeking. */
118 assert((a = archive_read_new()) != NULL);
119 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip_seekable(a));
120 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));
121 verify_file0_seek(a);
122
123 /* Then read streaming. */
124 assert((a = archive_read_new()) != NULL);
125 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip_streamable(a));
126 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));
127 verify_file0_stream(a, 1);
128 free(p);
129 }
130
131