1 /*-
2 * Copyright (c) 2013 Konrad Kleine
3 * Copyright (c) 2014 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
25 */
26 #include "test.h"
27
DEFINE_TEST(test_read_format_zip_winzip_aes256_large)28 DEFINE_TEST(test_read_format_zip_winzip_aes256_large)
29 {
30 const char *refname = "test_read_format_zip_winzip_aes256_large.zip";
31 struct archive_entry *ae;
32 struct archive *a;
33 char buff[512];
34
35
36 /* Check if running system has cryptographic functionality. */
37 assert((a = archive_write_new()) != NULL);
38 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
39 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
40 if (ARCHIVE_OK != archive_write_set_options(a,
41 "zip:encryption=aes256")) {
42 skipping("This system does not have cryptographic library");
43 archive_write_free(a);
44 return;
45 }
46 archive_write_free(a);
47
48
49 extract_reference_file(refname);
50
51 /*
52 * Extract a zip file without password.
53 */
54 assert((a = archive_read_new()) != NULL);
55 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
56 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
57 assertEqualIntA(a, ARCHIVE_OK,
58 archive_read_open_filename(a, refname, 10240));
59
60 assertEqualIntA(a, ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW,
61 archive_read_has_encrypted_entries(a));
62
63 /* Verify encrypted file "Makefile" */
64 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
65 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
66 assertEqualString("Makefile", archive_entry_pathname(ae));
67 assertEqualInt(1456747, archive_entry_size(ae));
68 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
69 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
70 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
71 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, sizeof(buff)));
72
73 /* Verify encrypted file "NEWS" */
74 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
75 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
76 assertEqualString("NEWS", archive_entry_pathname(ae));
77 assertEqualInt(29357, archive_entry_size(ae));
78 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
79 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
80 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
81 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, sizeof(buff)));
82
83 /* Verify encrypted file "README" */
84 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
85 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
86 assertEqualString("README", archive_entry_pathname(ae));
87 assertEqualInt(6818, archive_entry_size(ae));
88 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
89 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
90 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
91 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, sizeof(buff)));
92
93 /* Verify encrypted file "config.h" */
94 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
95 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
96 assertEqualString("config.h", archive_entry_pathname(ae));
97 assertEqualInt(32667, archive_entry_size(ae));
98 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
99 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
100 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
101 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, sizeof(buff)));
102
103 assertEqualInt(4, archive_file_count(a));
104
105 /* End of archive. */
106 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
107
108 /* Verify archive format. */
109 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
110 assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
111
112 /* Close the archive. */
113 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
114 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
115
116
117 /*
118 * Extract a zip file with password.
119 */
120 assert((a = archive_read_new()) != NULL);
121 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
122 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
123 assertEqualIntA(a, ARCHIVE_OK,
124 archive_read_add_passphrase(a, "password"));
125 assertEqualIntA(a, ARCHIVE_OK,
126 archive_read_open_filename(a, refname, 10240));
127
128 assertEqualIntA(a, ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW,
129 archive_read_has_encrypted_entries(a));
130
131 /* Verify encrypted file "Makefile" */
132 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
133 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
134 assertEqualString("Makefile", archive_entry_pathname(ae));
135 assertEqualInt(1456747, archive_entry_size(ae));
136 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
137 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
138 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
139 if (archive_zlib_version() != NULL) {
140 assertEqualInt(512, archive_read_data(a, buff, sizeof(buff)));
141 } else {
142 assertEqualInt(ARCHIVE_FAILED,
143 archive_read_data(a, buff, sizeof(buff)));
144 assertEqualString(archive_error_string(a),
145 "Unsupported ZIP compression method (8: deflation)");
146 assert(archive_errno(a) != 0);
147 }
148
149 /* Verify encrypted file "NEWS" */
150 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
151 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
152 assertEqualString("NEWS", archive_entry_pathname(ae));
153 assertEqualInt(29357, archive_entry_size(ae));
154 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
155 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
156 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
157 if (archive_zlib_version() != NULL) {
158 assertEqualInt(512, archive_read_data(a, buff, sizeof(buff)));
159 } else {
160 assertEqualInt(ARCHIVE_FAILED,
161 archive_read_data(a, buff, sizeof(buff)));
162 assertEqualString(archive_error_string(a),
163 "Unsupported ZIP compression method (8: deflation)");
164 assert(archive_errno(a) != 0);
165 }
166
167 /* Verify encrypted file "README" */
168 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
169 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
170 assertEqualString("README", archive_entry_pathname(ae));
171 assertEqualInt(6818, archive_entry_size(ae));
172 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
173 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
174 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
175 if (archive_zlib_version() != NULL) {
176 assertEqualInt(512, archive_read_data(a, buff, sizeof(buff)));
177 } else {
178 assertEqualInt(ARCHIVE_FAILED,
179 archive_read_data(a, buff, sizeof(buff)));
180 assertEqualString(archive_error_string(a),
181 "Unsupported ZIP compression method (8: deflation)");
182 assert(archive_errno(a) != 0);
183 }
184
185 /* Verify encrypted file "config.h" */
186 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
187 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
188 assertEqualString("config.h", archive_entry_pathname(ae));
189 assertEqualInt(32667, archive_entry_size(ae));
190 assertEqualInt(1, archive_entry_is_data_encrypted(ae));
191 assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
192 assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
193 if (archive_zlib_version() != NULL) {
194 assertEqualInt(512, archive_read_data(a, buff, sizeof(buff)));
195 } else {
196 assertEqualInt(ARCHIVE_FAILED,
197 archive_read_data(a, buff, sizeof(buff)));
198 assertEqualString(archive_error_string(a),
199 "Unsupported ZIP compression method (8: deflation)");
200 assert(archive_errno(a) != 0);
201 }
202
203 assertEqualInt(4, archive_file_count(a));
204
205 /* End of archive. */
206 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
207
208 /* Verify archive format. */
209 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
210 assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
211
212 /* Close the archive. */
213 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
214 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
215 }
216
217