1 /*- 2 * Copyright (c) 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 #include "test.h" 27 28 static void 29 test_format_by_name(const char *format_name, const char *compression_type, 30 int format_id, int dot_stored, const void *image, size_t image_size) 31 { 32 struct archive_entry *ae; 33 struct archive *a; 34 size_t used; 35 size_t buffsize = 1024 * 1024; 36 char *buff; 37 int r; 38 39 assert((buff = malloc(buffsize)) != NULL); 40 if (buff == NULL) 41 return; 42 43 /* Create a new archive in memory. */ 44 assert((a = archive_write_new()) != NULL); 45 r = archive_write_set_format_by_name(a, format_name); 46 if (r == ARCHIVE_WARN) { 47 skipping("%s format not fully supported on this platform", 48 compression_type); 49 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 50 free(buff); 51 return; 52 } 53 assertEqualIntA(a, ARCHIVE_OK, r); 54 if (compression_type != NULL && 55 ARCHIVE_OK != archive_write_set_format_option(a, format_name, 56 "compression", compression_type)) { 57 skipping("%s writing not fully supported on this platform", 58 compression_type); 59 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 60 free(buff); 61 return; 62 } 63 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 64 assertEqualIntA(a, ARCHIVE_OK, 65 archive_write_open_memory(a, buff, buffsize, &used)); 66 67 /* 68 * Write a file to it. 69 */ 70 assert((ae = archive_entry_new()) != NULL); 71 archive_entry_set_mtime(ae, 1, 0); 72 assertEqualInt(1, archive_entry_mtime(ae)); 73 archive_entry_set_ctime(ae, 1, 0); 74 assertEqualInt(1, archive_entry_ctime(ae)); 75 archive_entry_set_atime(ae, 1, 0); 76 assertEqualInt(1, archive_entry_atime(ae)); 77 archive_entry_copy_pathname(ae, "file"); 78 assertEqualString("file", archive_entry_pathname(ae)); 79 archive_entry_set_mode(ae, AE_IFREG | 0755); 80 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae)); 81 archive_entry_set_size(ae, 8); 82 assertEqualInt(0, archive_write_header(a, ae)); 83 archive_entry_free(ae); 84 assertEqualInt(8, archive_write_data(a, "12345678", 8)); 85 86 /* Close out the archive. */ 87 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 88 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 89 90 if (image && image_size > 0) { 91 assertEqualMem(buff, image, image_size); 92 } 93 if (format_id > 0) { 94 /* 95 * Now, read the data back. 96 */ 97 /* With the test memory reader -- seeking mode. */ 98 assert((a = archive_read_new()) != NULL); 99 assertEqualIntA(a, ARCHIVE_OK, 100 archive_read_support_format_all(a)); 101 assertEqualIntA(a, ARCHIVE_OK, 102 archive_read_support_filter_all(a)); 103 assertEqualIntA(a, ARCHIVE_OK, 104 read_open_memory_seek(a, buff, used, 7)); 105 106 if (dot_stored & 1) { 107 assertEqualIntA(a, ARCHIVE_OK, 108 archive_read_next_header(a, &ae)); 109 assertEqualString(".", archive_entry_pathname(ae)); 110 assertEqualInt(AE_IFDIR, archive_entry_filetype(ae)); 111 } 112 /* 113 * Read and verify the file. 114 */ 115 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 116 assertEqualInt(1, archive_entry_mtime(ae)); 117 if (dot_stored & 2) { 118 assertEqualString("./file", archive_entry_pathname(ae)); 119 } else { 120 assertEqualString("file", archive_entry_pathname(ae)); 121 } 122 assertEqualInt(AE_IFREG, archive_entry_filetype(ae)); 123 assertEqualInt(8, archive_entry_size(ae)); 124 125 /* Verify the end of the archive. */ 126 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 127 128 /* Verify archive format. */ 129 assertEqualIntA(a, ARCHIVE_FILTER_NONE, 130 archive_filter_code(a, 0)); 131 assertEqualIntA(a, format_id, archive_format(a)); 132 133 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 134 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 135 } 136 free(buff); 137 } 138 139 DEFINE_TEST(test_archive_write_set_format_by_name_7zip) 140 { 141 test_format_by_name("7zip", "copy", ARCHIVE_FORMAT_7ZIP, 0, 142 "\x37\x7a\xbc\xaf\x27\x1c\x00\x03", 8); 143 } 144 145 DEFINE_TEST(test_archive_write_set_format_by_name_ar) 146 { 147 test_format_by_name("ar", NULL, ARCHIVE_FORMAT_AR, 0, NULL, 0); 148 } 149 150 DEFINE_TEST(test_archive_write_set_format_by_name_arbsd) 151 { 152 test_format_by_name("arbsd", NULL, ARCHIVE_FORMAT_AR, 0, NULL, 0); 153 } 154 155 DEFINE_TEST(test_archive_write_set_format_by_name_argnu) 156 { 157 test_format_by_name("argnu", NULL, ARCHIVE_FORMAT_AR_GNU, 0, NULL, 0); 158 } 159 160 DEFINE_TEST(test_archive_write_set_format_by_name_arsvr4) 161 { 162 test_format_by_name("arsvr4", NULL, ARCHIVE_FORMAT_AR_GNU, 0, NULL, 0); 163 } 164 165 DEFINE_TEST(test_archive_write_set_format_by_name_bsdtar) 166 { 167 test_format_by_name("bsdtar", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0); 168 } 169 170 DEFINE_TEST(test_archive_write_set_format_by_name_cd9660) 171 { 172 test_format_by_name("cd9660", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1, 173 NULL, 0); 174 } 175 176 DEFINE_TEST(test_archive_write_set_format_by_name_cpio) 177 { 178 test_format_by_name("cpio", NULL, ARCHIVE_FORMAT_CPIO_POSIX, 0, NULL, 0); 179 } 180 181 DEFINE_TEST(test_archive_write_set_format_by_name_gnutar) 182 { 183 test_format_by_name("gnutar", NULL, ARCHIVE_FORMAT_TAR_GNUTAR, 0, 184 NULL, 0); 185 } 186 187 DEFINE_TEST(test_archive_write_set_format_by_name_iso) 188 { 189 test_format_by_name("iso", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1, 190 NULL, 0); 191 } 192 193 DEFINE_TEST(test_archive_write_set_format_by_name_iso9660) 194 { 195 test_format_by_name("iso9660", NULL, ARCHIVE_FORMAT_ISO9660_ROCKRIDGE, 1, 196 NULL, 0); 197 } 198 199 DEFINE_TEST(test_archive_write_set_format_by_name_mtree) 200 { 201 test_format_by_name("mtree", NULL, ARCHIVE_FORMAT_MTREE, 2, NULL, 0); 202 } 203 204 DEFINE_TEST(test_archive_write_set_format_by_name_mtree_classic) 205 { 206 test_format_by_name("mtree-classic", NULL, ARCHIVE_FORMAT_MTREE, 1, 207 NULL, 0); 208 } 209 210 DEFINE_TEST(test_archive_write_set_format_by_name_newc) 211 { 212 test_format_by_name("newc", NULL, ARCHIVE_FORMAT_CPIO_SVR4_NOCRC, 0, 213 NULL, 0); 214 } 215 216 DEFINE_TEST(test_archive_write_set_format_by_name_odc) 217 { 218 test_format_by_name("odc", NULL, ARCHIVE_FORMAT_CPIO_POSIX, 0, NULL, 0); 219 } 220 221 DEFINE_TEST(test_archive_write_set_format_by_name_oldtar) 222 { 223 test_format_by_name("oldtar", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0); 224 } 225 226 DEFINE_TEST(test_archive_write_set_format_by_name_pax) 227 { 228 test_format_by_name("pax", NULL, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE, 0, 229 NULL, 0); 230 } 231 232 DEFINE_TEST(test_archive_write_set_format_by_name_paxr) 233 { 234 test_format_by_name("paxr", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0); 235 } 236 237 DEFINE_TEST(test_archive_write_set_format_by_name_posix) 238 { 239 test_format_by_name("posix", NULL, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE, 0, 240 NULL, 0); 241 } 242 243 DEFINE_TEST(test_archive_write_set_format_by_name_rpax) 244 { 245 test_format_by_name("rpax", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0); 246 } 247 248 DEFINE_TEST(test_archive_write_set_format_by_name_shar) 249 { 250 test_format_by_name("shar", NULL, -1, 0, 251 "#!/bin/sh\n# This is a shell archive\n", 36); 252 } 253 254 DEFINE_TEST(test_archive_write_set_format_by_name_shardump) 255 { 256 test_format_by_name("shardump", NULL, -1, 0, 257 "#!/bin/sh\n# This is a shell archive\n", 36); 258 } 259 260 DEFINE_TEST(test_archive_write_set_format_by_name_ustar) 261 { 262 test_format_by_name("ustar", NULL, ARCHIVE_FORMAT_TAR_USTAR, 0, NULL, 0); 263 } 264 265 DEFINE_TEST(test_archive_write_set_format_by_name_v7tar) 266 { 267 test_format_by_name("v7tar", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0); 268 } 269 270 DEFINE_TEST(test_archive_write_set_format_by_name_v7) 271 { 272 test_format_by_name("v7", NULL, ARCHIVE_FORMAT_TAR, 0, NULL, 0); 273 } 274 275 DEFINE_TEST(test_archive_write_set_format_by_name_warc) 276 { 277 test_format_by_name("warc", NULL, ARCHIVE_FORMAT_WARC, 0, NULL, 0); 278 } 279 280 DEFINE_TEST(test_archive_write_set_format_by_name_xar) 281 { 282 test_format_by_name("xar", "gzip", ARCHIVE_FORMAT_XAR, 0, NULL, 0); 283 } 284 285 DEFINE_TEST(test_archive_write_set_format_by_name_zip) 286 { 287 test_format_by_name("zip", "store", ARCHIVE_FORMAT_ZIP, 0, NULL, 0); 288 } 289