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 29/* 30 * Test writing an empty archive. 31 */ 32DEFINE_TEST(test_write_format_7zip_empty_archive) 33{ 34 struct archive *a; 35 size_t buffsize = 1000; 36 char *buff; 37 size_t used; 38 39 buff = malloc(buffsize); 40 41 /* Create a new archive in memory. */ 42 assert((a = archive_write_new()) != NULL); 43 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a)); 44 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 45 assertEqualIntA(a, ARCHIVE_OK, 46 archive_write_open_memory(a, buff, buffsize, &used)); 47 48 /* Close out the archive. */ 49 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 50 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 51 52 /* Verify the archive file size. */ 53 assertEqualInt(32, used); 54 55 /* Verify the initial header. */ 56 assertEqualMem(buff, 57 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03" 58 "\x8d\x9b\xd5\x0f\x00\x00\x00\x00" 59 "\x00\x00\x00\x00\x00\x00\x00\x00" 60 "\x00\x00\x00\x00\x00\x00\x00\x00", 32); 61 62 free(buff); 63} 64 65/* 66 * Test writing an empty file. 67 */ 68static void 69test_only_empty_file(void) 70{ 71 struct archive *a; 72 struct archive_entry *ae; 73 size_t buffsize = 1000; 74 char *buff; 75 size_t used; 76 77 buff = malloc(buffsize); 78 79 /* Create a new archive in memory. */ 80 assert((a = archive_write_new()) != NULL); 81 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a)); 82 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 83 assertEqualIntA(a, ARCHIVE_OK, 84 archive_write_open_memory(a, buff, buffsize, &used)); 85 86 /* 87 * Write an empty file to it. 88 */ 89 assert((ae = archive_entry_new()) != NULL); 90 archive_entry_set_mtime(ae, 1, 10); 91 assertEqualInt(1, archive_entry_mtime(ae)); 92 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 93 archive_entry_set_atime(ae, 2, 20); 94 assertEqualInt(2, archive_entry_atime(ae)); 95 assertEqualInt(20, archive_entry_atime_nsec(ae)); 96 archive_entry_set_ctime(ae, 0, 100); 97 assertEqualInt(0, archive_entry_ctime(ae)); 98 assertEqualInt(100, archive_entry_ctime_nsec(ae)); 99 archive_entry_copy_pathname(ae, "empty"); 100 assertEqualString("empty", archive_entry_pathname(ae)); 101 archive_entry_set_mode(ae, AE_IFREG | 0755); 102 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae)); 103 104 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 105 archive_entry_free(ae); 106 107 /* Close out the archive. */ 108 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 109 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 110 111 /* Verify the archive file size. */ 112 assertEqualInt(102, used); 113 114 /* Verify the initial header. */ 115 assertEqualMem(buff, 116 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03" 117 "\x00\x5b\x58\x25\x00\x00\x00\x00" 118 "\x00\x00\x00\x00\x46\x00\x00\x00" 119 "\x00\x00\x00\x00\x8f\xce\x1d\xf3", 32); 120 121 /* 122 * Now, read the data back. 123 */ 124 /* With the test memory reader -- seeking mode. */ 125 assert((a = archive_read_new()) != NULL); 126 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 127 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 128 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 129 130 /* 131 * Read and verify an empty file. 132 */ 133 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 134 assertEqualInt(1, archive_entry_mtime(ae)); 135 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 136 assertEqualInt(2, archive_entry_atime(ae)); 137 assertEqualInt(0, archive_entry_atime_nsec(ae)); 138 assertEqualInt(0, archive_entry_ctime(ae)); 139 assertEqualInt(100, archive_entry_ctime_nsec(ae)); 140 assertEqualString("empty", archive_entry_pathname(ae)); 141 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 142 assertEqualInt(0, archive_entry_size(ae)); 143 144 /* Verify the end of the archive. */ 145 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 146 147 /* Verify archive format. */ 148 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); 149 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a)); 150 151 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 152 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 153 154 free(buff); 155} 156 157static void 158test_only_empty_files(void) 159{ 160 struct archive *a; 161 struct archive_entry *ae; 162 size_t buffsize = 1000; 163 char *buff; 164 size_t used; 165 166 buff = malloc(buffsize); 167 168 /* Create a new archive in memory. */ 169 assert((a = archive_write_new()) != NULL); 170 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_7zip(a)); 171 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 172 assertEqualIntA(a, ARCHIVE_OK, 173 archive_write_open_memory(a, buff, buffsize, &used)); 174 175 /* 176 * Write an empty file to it. 177 */ 178 assert((ae = archive_entry_new()) != NULL); 179 archive_entry_set_mtime(ae, 1, 10); 180 assertEqualInt(1, archive_entry_mtime(ae)); 181 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 182 archive_entry_set_atime(ae, 2, 20); 183 assertEqualInt(2, archive_entry_atime(ae)); 184 assertEqualInt(20, archive_entry_atime_nsec(ae)); 185 archive_entry_copy_pathname(ae, "empty"); 186 assertEqualString("empty", archive_entry_pathname(ae)); 187 archive_entry_set_mode(ae, AE_IFREG | 0755); 188 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae)); 189 190 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 191 archive_entry_free(ae); 192 193 /* 194 * Write second empty file to it. 195 */ 196 assert((ae = archive_entry_new()) != NULL); 197 archive_entry_set_mtime(ae, 2, 10); 198 assertEqualInt(2, archive_entry_mtime(ae)); 199 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 200 archive_entry_set_ctime(ae, 2, 10); 201 assertEqualInt(2, archive_entry_ctime(ae)); 202 assertEqualInt(10, archive_entry_ctime_nsec(ae)); 203 archive_entry_copy_pathname(ae, "empty2"); 204 assertEqualString("empty2", archive_entry_pathname(ae)); 205 archive_entry_set_mode(ae, AE_IFREG | 0644); 206 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae)); 207 208 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 209 archive_entry_free(ae); 210 211 /* 212 * Write third empty file to it. 213 */ 214 assert((ae = archive_entry_new()) != NULL); 215 archive_entry_set_mtime(ae, 3, 10); 216 assertEqualInt(3, archive_entry_mtime(ae)); 217 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 218 archive_entry_copy_pathname(ae, "empty3"); 219 assertEqualString("empty3", archive_entry_pathname(ae)); 220 archive_entry_set_mode(ae, AE_IFREG | 0644); 221 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae)); 222 223 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 224 archive_entry_free(ae); 225 226 /* Close out the archive. */ 227 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 228 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 229 230 /* Verify the initial header. */ 231 assertEqualMem(buff, "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8); 232 233 /* 234 * Now, read the data back. 235 */ 236 /* With the test memory reader -- seeking mode. */ 237 assert((a = archive_read_new()) != NULL); 238 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 239 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 240 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 241 242 /* 243 * Read and verify an empty file. 244 */ 245 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 246 assertEqualInt(1, archive_entry_mtime(ae)); 247 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 248 assertEqualInt(2, archive_entry_atime(ae)); 249 assertEqualInt(0, archive_entry_atime_nsec(ae)); 250 assertEqualInt(0, archive_entry_ctime(ae)); 251 assertEqualString("empty", archive_entry_pathname(ae)); 252 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 253 assertEqualInt(0, archive_entry_size(ae)); 254 255 /* 256 * Read and verify second empty file. 257 */ 258 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 259 assertEqualInt(2, archive_entry_mtime(ae)); 260 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 261 assertEqualInt(0, archive_entry_atime(ae)); 262 assertEqualInt(2, archive_entry_ctime(ae)); 263 assertEqualInt(0, archive_entry_ctime_nsec(ae)); 264 assertEqualString("empty2", archive_entry_pathname(ae)); 265 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae)); 266 assertEqualInt(0, archive_entry_size(ae)); 267 268 /* 269 * Read and verify third empty file. 270 */ 271 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 272 assertEqualInt(3, archive_entry_mtime(ae)); 273 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 274 assertEqualInt(0, archive_entry_atime(ae)); 275 assertEqualInt(0, archive_entry_ctime(ae)); 276 assertEqualString("empty3", archive_entry_pathname(ae)); 277 assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae)); 278 assertEqualInt(0, archive_entry_size(ae)); 279 280 /* Verify the end of the archive. */ 281 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 282 283 /* Verify archive format. */ 284 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0)); 285 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a)); 286 287 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 288 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 289 290 free(buff); 291} 292 293DEFINE_TEST(test_write_format_7zip_empty_files) 294{ 295 /* Test that write an empty file. */ 296 test_only_empty_file(); 297 test_only_empty_files(); 298} 299