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