1 /*- 2 * Copyright (c) 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 * in this position and unchanged. 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 27 #include "test.h" 28 29 /* 30 * A basic exercise of xz reading and writing. 31 * 32 * TODO: Add a reference file and make sure we can decompress that. 33 */ 34 35 DEFINE_TEST(test_write_filter_xz) 36 { 37 struct archive_entry *ae; 38 struct archive* a; 39 char *buff, *data; 40 size_t buffsize, datasize; 41 char path[16]; 42 size_t used1, used2; 43 int i, r; 44 45 buffsize = 2000000; 46 assert(NULL != (buff = (char *)malloc(buffsize))); 47 if (buff == NULL) 48 return; 49 50 datasize = 10000; 51 assert(NULL != (data = (char *)malloc(datasize))); 52 if (data == NULL) { 53 free(buff); 54 return; 55 } 56 memset(data, 0, datasize); 57 58 /* 59 * Write a 100 files and read them all back. 60 */ 61 assert((a = archive_write_new()) != NULL); 62 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 63 r = archive_write_add_filter_xz(a); 64 if (r == ARCHIVE_FATAL) { 65 skipping("xz writing not supported on this platform"); 66 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 67 free(buff); 68 free(data); 69 return; 70 } 71 assertEqualIntA(a, ARCHIVE_OK, 72 archive_write_set_bytes_per_block(a, 10)); 73 assertEqualInt(ARCHIVE_FILTER_XZ, archive_filter_code(a, 0)); 74 assertEqualString("xz", archive_filter_name(a, 0)); 75 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used1)); 76 assertEqualInt(ARCHIVE_FILTER_XZ, archive_filter_code(a, 0)); 77 assertEqualString("xz", archive_filter_name(a, 0)); 78 assert((ae = archive_entry_new()) != NULL); 79 archive_entry_set_filetype(ae, AE_IFREG); 80 archive_entry_set_size(ae, datasize); 81 for (i = 0; i < 100; i++) { 82 snprintf(path, sizeof(path), "file%03d", i); 83 archive_entry_copy_pathname(ae, path); 84 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 85 assertA(datasize 86 == (size_t)archive_write_data(a, data, datasize)); 87 } 88 archive_entry_free(ae); 89 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 90 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 91 92 assert((a = archive_read_new()) != NULL); 93 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 94 r = archive_read_support_filter_xz(a); 95 if (r == ARCHIVE_WARN) { 96 skipping("Can't verify xz writing by reading back;" 97 " xz reading not fully supported on this platform"); 98 } else { 99 assertEqualIntA(a, ARCHIVE_OK, 100 archive_read_support_filter_all(a)); 101 assertEqualIntA(a, ARCHIVE_OK, 102 archive_read_open_memory(a, buff, used1)); 103 for (i = 0; i < 100; i++) { 104 snprintf(path, sizeof(path), "file%03d", i); 105 if (!assertEqualInt(ARCHIVE_OK, 106 archive_read_next_header(a, &ae))) 107 break; 108 assertEqualString(path, archive_entry_pathname(ae)); 109 assertEqualInt((int)datasize, archive_entry_size(ae)); 110 } 111 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 112 } 113 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 114 115 /* 116 * Repeat the cycle again, this time setting some compression 117 * options. 118 */ 119 assert((a = archive_write_new()) != NULL); 120 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 121 assertEqualIntA(a, ARCHIVE_OK, 122 archive_write_set_bytes_per_block(a, 10)); 123 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_xz(a)); 124 assertEqualIntA(a, ARCHIVE_FAILED, 125 archive_write_set_filter_option(a, NULL, "nonexistent-option", "0")); 126 assertEqualIntA(a, ARCHIVE_FAILED, 127 archive_write_set_filter_option(a, NULL, "compression-level", "abc")); 128 assertEqualIntA(a, ARCHIVE_FAILED, 129 archive_write_set_filter_option(a, NULL, "compression-level", "99")); 130 assertEqualIntA(a, ARCHIVE_OK, 131 archive_write_set_filter_option(a, NULL, "compression-level", "9")); 132 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2)); 133 for (i = 0; i < 100; i++) { 134 snprintf(path, sizeof(path), "file%03d", i); 135 assert((ae = archive_entry_new()) != NULL); 136 archive_entry_copy_pathname(ae, path); 137 archive_entry_set_size(ae, datasize); 138 archive_entry_set_filetype(ae, AE_IFREG); 139 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 140 assertA(datasize == (size_t)archive_write_data(a, data, datasize)); 141 archive_entry_free(ae); 142 } 143 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 144 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 145 146 /* Curiously, this test fails; the test data above compresses 147 * better at default compression than at level 9. */ 148 /* 149 failure("compression-level=9 wrote %d bytes, default wrote %d bytes", 150 (int)used2, (int)used1); 151 assert(used2 < used1); 152 */ 153 154 assert((a = archive_read_new()) != NULL); 155 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 156 r = archive_read_support_filter_xz(a); 157 if (r == ARCHIVE_WARN) { 158 skipping("xz reading not fully supported on this platform"); 159 } else { 160 assertEqualIntA(a, ARCHIVE_OK, 161 archive_read_support_filter_all(a)); 162 assertEqualIntA(a, ARCHIVE_OK, 163 archive_read_open_memory(a, buff, used2)); 164 for (i = 0; i < 100; i++) { 165 snprintf(path, sizeof(path), "file%03d", i); 166 failure("Trying to read %s", path); 167 if (!assertEqualIntA(a, ARCHIVE_OK, 168 archive_read_next_header(a, &ae))) 169 break; 170 assertEqualString(path, archive_entry_pathname(ae)); 171 assertEqualInt((int)datasize, archive_entry_size(ae)); 172 } 173 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 174 } 175 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 176 177 /* 178 * Repeat again, with much lower compression. 179 */ 180 assert((a = archive_write_new()) != NULL); 181 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 182 assertEqualIntA(a, ARCHIVE_OK, 183 archive_write_set_bytes_per_block(a, 10)); 184 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_xz(a)); 185 assertEqualIntA(a, ARCHIVE_OK, 186 archive_write_set_filter_option(a, NULL, "compression-level", "0")); 187 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2)); 188 for (i = 0; i < 100; i++) { 189 snprintf(path, sizeof(path), "file%03d", i); 190 assert((ae = archive_entry_new()) != NULL); 191 archive_entry_copy_pathname(ae, path); 192 archive_entry_set_size(ae, datasize); 193 archive_entry_set_filetype(ae, AE_IFREG); 194 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 195 failure("Writing file %s", path); 196 assertEqualIntA(a, datasize, 197 (size_t)archive_write_data(a, data, datasize)); 198 archive_entry_free(ae); 199 } 200 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 201 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 202 203 /* I would like to assert that compression-level=0 results in 204 * larger data than the default compression, but that's not true 205 * for all versions of liblzma. */ 206 /* 207 failure("Compression-level=0 wrote %d bytes; default wrote %d bytes", 208 (int)used2, (int)used1); 209 assert(used2 > used1); 210 */ 211 212 assert((a = archive_read_new()) != NULL); 213 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 214 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 215 r = archive_read_support_filter_xz(a); 216 if (r == ARCHIVE_WARN) { 217 skipping("xz reading not fully supported on this platform"); 218 } else { 219 assertEqualIntA(a, ARCHIVE_OK, 220 archive_read_open_memory(a, buff, used2)); 221 for (i = 0; i < 100; i++) { 222 snprintf(path, sizeof(path), "file%03d", i); 223 if (!assertEqualInt(ARCHIVE_OK, 224 archive_read_next_header(a, &ae))) 225 break; 226 assertEqualString(path, archive_entry_pathname(ae)); 227 assertEqualInt((int)datasize, archive_entry_size(ae)); 228 } 229 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 230 } 231 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 232 233 /* 234 * Test various premature shutdown scenarios to make sure we 235 * don't crash or leak memory. 236 */ 237 assert((a = archive_write_new()) != NULL); 238 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_xz(a)); 239 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 240 241 assert((a = archive_write_new()) != NULL); 242 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_xz(a)); 243 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 244 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 245 246 assert((a = archive_write_new()) != NULL); 247 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 248 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_xz(a)); 249 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 250 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 251 252 assert((a = archive_write_new()) != NULL); 253 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 254 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_xz(a)); 255 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2)); 256 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 257 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 258 259 /* 260 * Clean up. 261 */ 262 free(data); 263 free(buff); 264 } 265