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