test_read_format_zip_traditional_encryption_data.c revision 353376
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_traditional_encryption_data.c 353376 2019-10-09 22:19:06Z mm $");
28
29DEFINE_TEST(test_read_format_zip_traditional_encryption_data)
30{
31	/* This file is password protected (Traditional PKWARE Encrypted).
32	   The headers are NOT encrypted. Password is "12345678". */
33	const char *refname =
34		"test_read_format_zip_traditional_encryption_data.zip";
35	struct archive_entry *ae;
36	struct archive *a;
37	char buff[512];
38
39	/* Check if running system has cryptographic functionality. */
40	assert((a = archive_write_new()) != NULL);
41	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
42	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
43	if (ARCHIVE_OK != archive_write_set_options(a,
44				"zip:encryption=traditional")) {
45		skipping("This system does not have cryptographic liberary");
46		archive_write_free(a);
47		return;
48	}
49	archive_write_free(a);
50
51
52	extract_reference_file(refname);
53
54	/*
55	 * Extract a zip file without password.
56	 */
57	assert((a = archive_read_new()) != NULL);
58	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
59	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
60	assertEqualIntA(a, ARCHIVE_OK,
61               archive_read_open_filename(a, refname, 10240));
62
63	assertEqualIntA(a, ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW,
64		archive_read_has_encrypted_entries(a));
65
66	/* Verify encrypted file "bar.txt" */
67	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
68	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
69	assertEqualString("bar.txt", archive_entry_pathname(ae));
70	assertEqualInt(495, archive_entry_size(ae));
71	assertEqualInt(1, archive_entry_is_data_encrypted(ae));
72	assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
73	assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
74	assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, sizeof(buff)));
75
76	/* Verify encrypted file "foo.txt" */
77	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
78	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
79	assertEqualString("foo.txt", archive_entry_pathname(ae));
80	assertEqualInt(495, archive_entry_size(ae));
81	assertEqualInt(1, archive_entry_is_data_encrypted(ae));
82	assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
83	assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
84	assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, sizeof(buff)));
85
86	assertEqualInt(2, archive_file_count(a));
87
88	/* End of archive. */
89	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
90
91	/* Verify archive format. */
92	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
93	assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
94
95	/* Close the archive. */
96	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
97	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
98
99
100	/*
101	 * Extract a zip file with password.
102	 */
103	assert((a = archive_read_new()) != NULL);
104	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
105	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
106	/* Pass three passphrases to decrypt a file content. */
107	assertEqualIntA(a, ARCHIVE_OK,
108		archive_read_add_passphrase(a, "invalid_pass"));
109	assertEqualIntA(a, ARCHIVE_OK,
110		archive_read_add_passphrase(a, "invalid_phrase"));
111	assertEqualIntA(a, ARCHIVE_OK,
112		archive_read_add_passphrase(a, "12345678"));
113	assertEqualIntA(a, ARCHIVE_OK,
114		archive_read_open_filename(a, refname, 10240));
115
116	assertEqualIntA(a, ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW,
117		archive_read_has_encrypted_entries(a));
118
119	/* Verify encrypted file "bar.txt" */
120	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
121	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
122	assertEqualString("bar.txt", archive_entry_pathname(ae));
123	assertEqualInt(495, archive_entry_size(ae));
124	assertEqualInt(1, archive_entry_is_data_encrypted(ae));
125	assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
126	assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
127	if (archive_zlib_version() != NULL) {
128		assertEqualInt(495, archive_read_data(a, buff, sizeof(buff)));
129	} else {
130		assertEqualInt(ARCHIVE_FAILED,
131		    archive_read_data(a, buff, sizeof(buff)));
132		assertEqualString(archive_error_string(a),
133		    "Unsupported ZIP compression method (8: deflation)");
134		assert(archive_errno(a) != 0);
135	}
136
137	/* Verify encrypted file "foo.txt" */
138	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
139	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
140	assertEqualString("foo.txt", archive_entry_pathname(ae));
141	assertEqualInt(495, archive_entry_size(ae));
142	assertEqualInt(1, archive_entry_is_data_encrypted(ae));
143	assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
144	assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
145	if (archive_zlib_version() != NULL) {
146		assertEqualInt(495, archive_read_data(a, buff, sizeof(buff)));
147	} else {
148		assertEqualInt(ARCHIVE_FAILED,
149		    archive_read_data(a, buff, sizeof(buff)));
150		assertEqualString(archive_error_string(a),
151		    "Unsupported ZIP compression method (8: deflation)");
152		assert(archive_errno(a) != 0);
153	}
154
155	assertEqualInt(2, archive_file_count(a));
156
157	/* End of archive. */
158	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
159
160	/* Verify archive format. */
161	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
162	assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
163
164	/* Close the archive. */
165	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
166	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
167}
168
169