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