]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_read_format_zip_winzip_aes.c
MFC r299529,r299540,r299576,r299896:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_read_format_zip_winzip_aes.c
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$");
28
29 static void
30 test_winzip_aes(const char *refname, int need_libz)
31 {
32         struct archive_entry *ae;
33         struct archive *a;
34         char buff[512];
35
36         /* Check if running system has cryptographic functionarity. */
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 liberary");
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 "README" */
64         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
65         assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
66         assertEqualString("README", archive_entry_pathname(ae));
67         assertEqualInt(6818, 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         assertEqualInt(1, archive_file_count(a));
73
74         /* End of archive. */
75         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
76
77         /* Verify archive format. */
78         assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
79         assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
80
81         /* Close the archive. */
82         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
83         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
84
85
86         /*
87          * Extract a zip file with password.
88          */
89         assert((a = archive_read_new()) != NULL);
90         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
91         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
92         /* Pass three passphrases to decrypt a file content. */
93         assertEqualIntA(a, ARCHIVE_OK,
94                 archive_read_add_passphrase(a, "invalid_pass"));
95         assertEqualIntA(a, ARCHIVE_OK,
96                 archive_read_add_passphrase(a, "invalid_phrase"));
97         assertEqualIntA(a, ARCHIVE_OK,
98                 archive_read_add_passphrase(a, "password"));
99         assertEqualIntA(a, ARCHIVE_OK, 
100                 archive_read_open_filename(a, refname, 10240));
101
102         assertEqualIntA(a, ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW,
103                 archive_read_has_encrypted_entries(a));
104
105         /* Verify encrypted file "README" */
106         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
107         assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
108         assertEqualString("README", archive_entry_pathname(ae));
109         assertEqualInt(6818, archive_entry_size(ae));
110         assertEqualInt(1, archive_entry_is_data_encrypted(ae));
111         assertEqualInt(0, archive_entry_is_metadata_encrypted(ae));
112         assertEqualIntA(a, 1, archive_read_has_encrypted_entries(a));
113         if (!need_libz || archive_zlib_version() != NULL) {
114                 assertEqualInt(512, archive_read_data(a, buff, sizeof(buff)));
115         } else {
116                 assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
117                 assertEqualString(archive_error_string(a),
118                     "Unsupported ZIP compression method (deflation)");
119                 assert(archive_errno(a) != 0);
120         }
121         
122         assertEqualInt(1, archive_file_count(a));
123
124         /* End of archive. */
125         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
126
127         /* Verify archive format. */
128         assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
129         assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
130
131         /* Close the archive. */
132         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
133         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
134 }
135
136 DEFINE_TEST(test_read_format_zip_winzip_aes128)
137 {
138         /* WinZip AES-128 encryption. */
139         test_winzip_aes("test_read_format_zip_winzip_aes128.zip", 1);
140 }
141
142 DEFINE_TEST(test_read_format_zip_winzip_aes256)
143 {
144         /* WinZip AES-256 encryption. */
145         test_winzip_aes("test_read_format_zip_winzip_aes256.zip", 1);
146 }
147
148 DEFINE_TEST(test_read_format_zip_winzip_aes256_stored)
149 {
150         /* WinZip AES-256 encryption with stored data. */
151         test_winzip_aes("test_read_format_zip_winzip_aes256_stored.zip", 0);
152 }