1 /*- 2 * Copyright (c) 2011-2012 Michihiro NAKAJIMA 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 27 #include "test.h" 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Test writing an empty archive. 32 */ 33 DEFINE_TEST(test_write_format_7zip_empty_archive) 34 { 35 struct archive *a; 36 size_t buffsize = 1000; 37 char *buff; 38 size_t used; 39 40 buff = malloc(buffsize); 41 42 /* Create a new archive in memory. */ 43 assert((a = archive_write_new()) != NULL); 44 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a)); 45 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 46 assertEqualIntA(a, ARCHIVE_OK, 47 archive_write_open_memory(a, buff, buffsize, &used)); 48 49 /* Close out the archive. */ 50 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 51 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 52 53 /* Verify the archive file size. */ 54 assertEqualInt(32, used); 55 56 /* Verify the initial header. */ 57 assertEqualMem(buff, 58 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03" 59 "\x8d\x9b\xd5\x0f\x00\x00\x00\x00" 60 "\x00\x00\x00\x00\x00\x00\x00\x00" 61 "\x00\x00\x00\x00\x00\x00\x00\x00", 32); 62 63 free(buff); 64 } 65 66 /* 67 * Test writing an empty file. 68 */ 69 static void 70 test_only_empty_file(void) 71 { 72 struct archive *a; 73 struct archive_entry *ae; 74 size_t buffsize = 1000; 75 char *buff; 76 size_t used; 77 78 buff = malloc(buffsize); 79 80 /* Create a new archive in memory. */ 81 assert((a = archive_write_new()) != NULL); 82 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a)); 83 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 84 assertEqualIntA(a, ARCHIVE_OK, 85 archive_write_open_memory(a, buff, buffsize, &used)); 86 87 /* 88 * Write an empty file to it. 89 */ 90 assert((ae = archive_entry_new()) != NULL); 91 archive_entry_set_mtime(ae, 1, 10); 92 assertEqualInt(1, archive_entry_mtime(ae)); 93 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 94 archive_entry_set_atime(ae, 2, 20); 95 assertEqualInt(2, archive_entry_atime(ae)); 96 assertEqualInt(20, archive_entry_atime_nsec(ae)); 97 archive_entry_set_ctime(ae, 0, 100); 98 assertEqualInt(0, archive_entry_ctime(ae)); 99 assertEqualInt(100, archive_entry_ctime_nsec(ae)); 100 archive_entry_copy_pathname(ae, "empty"); 101 assertEqualString("empty", archive_entry_pathname(ae)); 102 archive_entry_set_mode(ae, AE_IFREG | 0755); 103 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae)); 104 105 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 106 archive_entry_free(ae); 107 108 /* Close out the archive. */ 109 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 110 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 111 112 /* Verify the archive file size. */ 113 assertEqualInt(102, used); 114 115 /* Verify the initial header. */ 116 assertEqualMem(buff, 117 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03" 118 "\x00\x5b\x58\x25\x00\x00\x00\x00" 119 "\x00\x00\x00\x00\x46\x00\x00\x00" 120 "\x00\x00\x00\x00\x8f\xce\x1d\xf3", 32); 121 122 /* 123 * Now, read the data back. 124 */ 125 /* With the test memory reader -- seeking mode. */ 126 assert((a = archive_read_new()) != NULL); 127 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 128 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 129 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 130 131 /* 132 * Read and verify an empty file. 133 */ 134 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 135 assertEqualInt(1, archive_entry_mtime(ae)); 136 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 137 assertEqualInt(2, archive_entry_atime(ae)); 138 assertEqualInt(0, archive_entry_atime_nsec(ae)); 139 assertEqualInt(0, archive_entry_ctime(ae)); 140 assertEqualInt(100, archive_entry_ctime_nsec(ae)); 141 assertEqualString("empty", archive_entry_pathname(ae)); 142 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 143 assertEqualInt(0, archive_entry_size(ae)); 144 145 /* Verify the end of the archive. */ 146 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 147 148 /* Verify archive format. */ 149 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); 150 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a)); 151 152 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 153 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 154 155 free(buff); 156 } 157 158 static void 159 test_only_empty_files(void) 160 { 161 struct archive *a; 162 struct archive_entry *ae; 163 size_t buffsize = 1000; 164 char *buff; 165 size_t used; 166 167 buff = malloc(buffsize); 168 169 /* Create a new archive in memory. */ 170 assert((a = archive_write_new()) != NULL); 171 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a)); 172 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 173 assertEqualIntA(a, ARCHIVE_OK, 174 archive_write_open_memory(a, buff, buffsize, &used)); 175 176 /* 177 * Write an empty file to it. 178 */ 179 assert((ae = archive_entry_new()) != NULL); 180 archive_entry_set_mtime(ae, 1, 10); 181 assertEqualInt(1, archive_entry_mtime(ae)); 182 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 183 archive_entry_set_atime(ae, 2, 20); 184 assertEqualInt(2, archive_entry_atime(ae)); 185 assertEqualInt(20, archive_entry_atime_nsec(ae)); 186 archive_entry_copy_pathname(ae, "empty"); 187 assertEqualString("empty", archive_entry_pathname(ae)); 188 archive_entry_set_mode(ae, AE_IFREG | 0755); 189 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae)); 190 191 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 192 archive_entry_free(ae); 193 194 /* 195 * Write second empty file to it. 196 */ 197 assert((ae = archive_entry_new()) != NULL); 198 archive_entry_set_mtime(ae, 2, 10); 199 assertEqualInt(2, archive_entry_mtime(ae)); 200 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 201 archive_entry_set_ctime(ae, 2, 10); 202 assertEqualInt(2, archive_entry_ctime(ae)); 203 assertEqualInt(10, archive_entry_ctime_nsec(ae)); 204 archive_entry_copy_pathname(ae, "empty2"); 205 assertEqualString("empty2", archive_entry_pathname(ae)); 206 archive_entry_set_mode(ae, AE_IFREG | 0644); 207 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae)); 208 209 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 210 archive_entry_free(ae); 211 212 /* 213 * Write third empty file to it. 214 */ 215 assert((ae = archive_entry_new()) != NULL); 216 archive_entry_set_mtime(ae, 3, 10); 217 assertEqualInt(3, archive_entry_mtime(ae)); 218 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 219 archive_entry_copy_pathname(ae, "empty3"); 220 assertEqualString("empty3", archive_entry_pathname(ae)); 221 archive_entry_set_mode(ae, AE_IFREG | 0644); 222 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae)); 223 224 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 225 archive_entry_free(ae); 226 227 /* Close out the archive. */ 228 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 229 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 230 231 /* Verify the initial header. */ 232 assertEqualMem(buff, "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8); 233 234 /* 235 * Now, read the data back. 236 */ 237 /* With the test memory reader -- seeking mode. */ 238 assert((a = archive_read_new()) != NULL); 239 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 240 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 241 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 242 243 /* 244 * Read and verify an empty file. 245 */ 246 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 247 assertEqualInt(1, archive_entry_mtime(ae)); 248 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 249 assertEqualInt(2, archive_entry_atime(ae)); 250 assertEqualInt(0, archive_entry_atime_nsec(ae)); 251 assertEqualInt(0, archive_entry_ctime(ae)); 252 assertEqualString("empty", archive_entry_pathname(ae)); 253 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 254 assertEqualInt(0, archive_entry_size(ae)); 255 256 /* 257 * Read and verify second empty file. 258 */ 259 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 260 assertEqualInt(2, archive_entry_mtime(ae)); 261 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 262 assertEqualInt(0, archive_entry_atime(ae)); 263 assertEqualInt(2, archive_entry_ctime(ae)); 264 assertEqualInt(0, archive_entry_ctime_nsec(ae)); 265 assertEqualString("empty2", archive_entry_pathname(ae)); 266 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae)); 267 assertEqualInt(0, archive_entry_size(ae)); 268 269 /* 270 * Read and verify third empty file. 271 */ 272 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 273 assertEqualInt(3, archive_entry_mtime(ae)); 274 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 275 assertEqualInt(0, archive_entry_atime(ae)); 276 assertEqualInt(0, archive_entry_ctime(ae)); 277 assertEqualString("empty3", archive_entry_pathname(ae)); 278 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae)); 279 assertEqualInt(0, archive_entry_size(ae)); 280 281 /* Verify the end of the archive. */ 282 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 283 284 /* Verify archive format. */ 285 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); 286 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a)); 287 288 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 289 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 290 291 free(buff); 292 } 293 294 DEFINE_TEST(test_write_format_7zip_empty_files) 295 { 296 /* Test that write an empty file. */ 297 test_only_empty_file(); 298 test_only_empty_files(); 299 } 300