1/*- 2 * Copyright (c) 2003-2007 Tim Kientzle 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#include "test.h" 26 27static void 28test_format(int (*set_format)(struct archive *)) 29{ 30 char filedata[64]; 31 struct archive_entry *ae; 32 struct archive *a; 33 char *p; 34 size_t used; 35 size_t buffsize = 1000000; 36 char *buff; 37 int damaged = 0; 38 39 buff = malloc(buffsize); 40 41 /* Create a new archive in memory. */ 42 assert((a = archive_write_new()) != NULL); 43 assertA(0 == (*set_format)(a)); 44 assertA(0 == archive_write_add_filter_none(a)); 45 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used)); 46 47 /* 48 * Write a file to it. 49 */ 50 assert((ae = archive_entry_new()) != NULL); 51 archive_entry_set_mtime(ae, 1, 10); 52 assert(1 == archive_entry_mtime(ae)); 53 assert(10 == archive_entry_mtime_nsec(ae)); 54 p = strdup("file"); 55 archive_entry_copy_pathname(ae, p); 56 strcpy(p, "XXXX"); 57 free(p); 58 assertEqualString("file", archive_entry_pathname(ae)); 59 archive_entry_set_mode(ae, S_IFREG | 0755); 60 assert((S_IFREG | 0755) == archive_entry_mode(ae)); 61 archive_entry_set_size(ae, 8); 62 63 assertA(0 == archive_write_header(a, ae)); 64 archive_entry_free(ae); 65 assertA(8 == archive_write_data(a, "12345678", 9)); 66 67 /* 68 * Write another file to it. 69 */ 70 assert((ae = archive_entry_new()) != NULL); 71 archive_entry_set_mtime(ae, 1, 10); 72 assert(1 == archive_entry_mtime(ae)); 73 assert(10 == archive_entry_mtime_nsec(ae)); 74 p = strdup("file2"); 75 archive_entry_copy_pathname(ae, p); 76 strcpy(p, "XXXX"); 77 free(p); 78 assertEqualString("file2", archive_entry_pathname(ae)); 79 archive_entry_set_mode(ae, S_IFREG | 0755); 80 assert((S_IFREG | 0755) == archive_entry_mode(ae)); 81 archive_entry_set_size(ae, 4); 82 83 assertA(0 == archive_write_header(a, ae)); 84 archive_entry_free(ae); 85 assertA(4 == archive_write_data(a, "1234", 5)); 86 87 /* 88 * Write a file with a name, filetype, and size. 89 */ 90 assert((ae = archive_entry_new()) != NULL); 91 archive_entry_copy_pathname(ae, "name"); 92 archive_entry_set_size(ae, 0); 93 archive_entry_set_filetype(ae, AE_IFREG); 94 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 95 assert(archive_error_string(a) == NULL); 96 archive_entry_free(ae); 97 98 /* 99 * Write a file with a name and filetype but no size. 100 */ 101 assert((ae = archive_entry_new()) != NULL); 102 archive_entry_copy_pathname(ae, "name"); 103 archive_entry_unset_size(ae); 104 archive_entry_set_filetype(ae, AE_IFREG); 105 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae)); 106 assert(archive_error_string(a) != NULL); 107 archive_entry_free(ae); 108 109 /* 110 * Write a file with a name and size but no filetype. 111 */ 112 assert((ae = archive_entry_new()) != NULL); 113 archive_entry_copy_pathname(ae, "name"); 114 archive_entry_set_size(ae, 0); 115 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae)); 116 assert(archive_error_string(a) != NULL); 117 archive_entry_free(ae); 118 119 /* 120 * Write a file with a size and filetype but no name. 121 */ 122 assert((ae = archive_entry_new()) != NULL); 123 archive_entry_set_size(ae, 0); 124 archive_entry_set_filetype(ae, AE_IFREG); 125 assertEqualInt(ARCHIVE_FAILED, archive_write_header(a, ae)); 126 assert(archive_error_string(a) != NULL); 127 archive_entry_free(ae); 128 129 /* 130 * Write a directory to it. 131 */ 132 assert((ae = archive_entry_new()) != NULL); 133 archive_entry_set_mtime(ae, 11, 110); 134 archive_entry_copy_pathname(ae, "dir"); 135 archive_entry_set_mode(ae, S_IFDIR | 0755); 136 archive_entry_set_size(ae, 512); 137 138 assertA(0 == archive_write_header(a, ae)); 139 assertEqualInt(0, archive_entry_size(ae)); 140 archive_entry_free(ae); 141 assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9)); 142 143 /* 144 * Write a character device to it. 145 */ 146 assert((ae = archive_entry_new()) != NULL); 147 archive_entry_copy_pathname(ae, "tty0"); 148 archive_entry_set_mode(ae, S_IFCHR | 0600); 149 archive_entry_set_size(ae, 0); 150 archive_entry_set_rdev(ae, 1024); 151 assertA(0 == archive_write_header(a, ae)); 152 archive_entry_free(ae); 153 154 155 /* Close out the archive. */ 156 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 157 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 158 159 /* 160 * Damage the second entry to test the search-ahead recovery. 161 * TODO: Move the damage-recovery checking to a separate test; 162 * it doesn't really belong in this write test. 163 */ 164 { 165 int i; 166 for (i = 80; i < 150; i++) { 167 if (memcmp(buff + i, "07070", 5) == 0) { 168 damaged = 1; 169 buff[i] = 'X'; 170 break; 171 } 172 } 173 } 174 failure("Unable to locate the second header for damage-recovery test."); 175 assert(damaged == 1); 176 177 /* 178 * Now, read the data back. 179 */ 180 assert((a = archive_read_new()) != NULL); 181 assertA(0 == archive_read_support_format_all(a)); 182 assertA(0 == archive_read_support_filter_all(a)); 183 assertA(0 == archive_read_open_memory(a, buff, used)); 184 185 if (!assertEqualIntA(a, 0, archive_read_next_header(a, &ae))) { 186 archive_read_free(a); 187 return; 188 } 189 190 assertEqualInt(1, archive_entry_mtime(ae)); 191 /* Not the same as above: cpio doesn't store hi-res times. */ 192 assert(0 == archive_entry_mtime_nsec(ae)); 193 assert(0 == archive_entry_atime(ae)); 194 assert(0 == archive_entry_ctime(ae)); 195 assertEqualString("file", archive_entry_pathname(ae)); 196 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae)); 197 assertEqualInt(8, archive_entry_size(ae)); 198 assertA(8 == archive_read_data(a, filedata, 10)); 199 assertEqualMem(filedata, "12345678", 8); 200 201 /* 202 * The second file can't be read because we damaged its header. 203 */ 204 205 /* 206 * Read the third file back. 207 * ARCHIVE_WARN here because the damaged entry was skipped. 208 */ 209 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae)); 210 assertEqualString("name", archive_entry_pathname(ae)); 211 212 /* 213 * Read the dir entry back. 214 */ 215 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 216 assertEqualInt(11, archive_entry_mtime(ae)); 217 assert(0 == archive_entry_mtime_nsec(ae)); 218 assert(0 == archive_entry_atime(ae)); 219 assert(0 == archive_entry_ctime(ae)); 220 assertEqualString("dir", archive_entry_pathname(ae)); 221 assertEqualInt((S_IFDIR | 0755), archive_entry_mode(ae)); 222 assertEqualInt(0, archive_entry_size(ae)); 223 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10)); 224 225 /* 226 * Read the character device entry back. 227 */ 228 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 229 assertEqualString("tty0", archive_entry_pathname(ae)); 230 assertEqualInt((S_IFCHR | 0600), archive_entry_mode(ae)); 231 assertEqualInt(0, archive_entry_size(ae)); 232 assertEqualInt(1024, archive_entry_rdev(ae)); 233 234 /* Verify the end of the archive. */ 235 assertEqualIntA(a, 1, archive_read_next_header(a, &ae)); 236 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 237 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 238 239 free(buff); 240} 241 242static void 243test_big_entries(int (*set_format)(struct archive *), int64_t size, int expected) 244{ 245 struct archive_entry *ae; 246 struct archive *a; 247 size_t buffsize = 1000000; 248 size_t used; 249 char *buff; 250 251 buff = malloc(buffsize); 252 253 /* Create a new archive in memory. */ 254 assert((a = archive_write_new()) != NULL); 255 assertA(0 == (*set_format)(a)); 256 assertA(0 == archive_write_add_filter_none(a)); 257 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used)); 258 259 assert((ae = archive_entry_new()) != NULL); 260 archive_entry_copy_pathname(ae, "file"); 261 archive_entry_set_size(ae, size); 262 archive_entry_set_filetype(ae, AE_IFREG); 263 assertEqualInt(expected, archive_write_header(a, ae)); 264 if (expected != ARCHIVE_OK) 265 assert(archive_error_string(a) != NULL); 266 267 archive_entry_free(ae); 268 archive_write_free(a); 269 free(buff); 270} 271 272 273DEFINE_TEST(test_write_format_cpio) 274{ 275 int64_t size_16m = ((int64_t)1) << 24; 276 int64_t size_2g = ((int64_t)1) << 31; 277 int64_t size_4g = ((int64_t)1) << 32; 278 int64_t size_8g = ((int64_t)1) << 33; 279 280 test_format(archive_write_set_format_cpio_odc); 281 test_format(archive_write_set_format_cpio_newc); 282 283 test_big_entries(archive_write_set_format_cpio_odc, 284 size_8g - 1, ARCHIVE_OK); 285 test_big_entries(archive_write_set_format_cpio_odc, 286 size_8g, ARCHIVE_FAILED); 287 test_big_entries(archive_write_set_format_cpio_newc, 288 size_4g - 1, ARCHIVE_OK); 289 test_big_entries(archive_write_set_format_cpio_newc, 290 size_4g, ARCHIVE_FAILED); 291 test_big_entries(archive_write_set_format_cpio_bin, 292 size_2g - 1, ARCHIVE_OK); 293 test_big_entries(archive_write_set_format_cpio_bin, 294 size_2g, ARCHIVE_FAILED); 295 test_big_entries(archive_write_set_format_cpio_pwb, 296 size_16m - 1, ARCHIVE_OK); 297 test_big_entries(archive_write_set_format_cpio_pwb, 298 size_16m, ARCHIVE_FAILED); 299} 300