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