1 /*- 2 * Copyright (c) 2007-2010 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$"); 29 30 /* 31 * Check that we generate an error message when reading a truncated 32 * gzip, bzip2, compress, xz, lzma, or lzip file. 33 */ 34 35 static void 36 test_truncation(const char *compression, 37 int (*set_compression)(struct archive *), int can_prog) 38 { 39 struct archive_entry *ae; 40 struct archive* a; 41 char path[16]; 42 char *buff, *data; 43 size_t buffsize, datasize, used1; 44 int i, r, use_prog; 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 bunch of files with semi-random data. 61 */ 62 assert((a = archive_write_new()) != NULL); 63 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 64 assertEqualIntA(a, ARCHIVE_OK, 65 archive_write_add_filter_compress(a)); 66 r = (*set_compression)(a); 67 if (r != ARCHIVE_OK && !can_prog) { 68 skipping("%s writing not supported on this platform", 69 compression); 70 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 71 free(buff); 72 free(data); 73 return; 74 } 75 use_prog = (r == ARCHIVE_WARN && can_prog); 76 assertEqualIntA(a, ARCHIVE_OK, 77 archive_write_set_bytes_per_block(a, 10)); 78 assertEqualIntA(a, ARCHIVE_OK, 79 archive_write_open_memory(a, buff, buffsize, &used1)); 80 assert((ae = archive_entry_new()) != NULL); 81 archive_entry_set_filetype(ae, AE_IFREG); 82 archive_entry_set_size(ae, datasize); 83 for (i = 0; i < 100; i++) { 84 snprintf(path, sizeof(path), "%s%d", compression, i); 85 archive_entry_copy_pathname(ae, path); 86 failure("%s", path); 87 if (!assertEqualIntA(a, ARCHIVE_OK, 88 archive_write_header(a, ae))) { 89 archive_write_free(a); 90 free(data); 91 free(buff); 92 return; 93 } 94 fill_with_pseudorandom_data(data, datasize); 95 failure("%s", path); 96 if (!assertEqualIntA(a, datasize, 97 archive_write_data(a, data, datasize))) { 98 archive_write_free(a); 99 free(data); 100 free(buff); 101 return; 102 } 103 } 104 archive_entry_free(ae); 105 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 106 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 107 108 assert((a = archive_read_new()) != NULL); 109 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 110 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 111 112 r = archive_read_open_memory(a, buff, used1 - used1/64); 113 if (r != ARCHIVE_OK) { 114 assertEqualStringA(a, "truncated bzip2 input", 115 archive_error_string(a)); 116 goto out; 117 } 118 119 for (i = 0; i < 100; i++) { 120 if (ARCHIVE_OK != archive_read_next_header(a, &ae)) { 121 failure("Should have non-NULL error message for %s", 122 compression); 123 assert(NULL != archive_error_string(a)); 124 break; 125 } 126 snprintf(path, sizeof(path), "%s%d", compression, i); 127 assertEqualString(path, archive_entry_pathname(ae)); 128 if (datasize != (size_t)archive_read_data(a, data, datasize)) { 129 failure("Should have non-NULL error message for %s", 130 compression); 131 assert(NULL != archive_error_string(a)); 132 break; 133 } 134 } 135 assertEqualIntA(a, (use_prog)?ARCHIVE_WARN:ARCHIVE_OK, 136 archive_read_close(a)); 137 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 138 139 out: 140 free(data); 141 free(buff); 142 } 143 144 DEFINE_TEST(test_read_truncated_filter_bzip2) 145 { 146 test_truncation("bzip2", archive_write_add_filter_bzip2, canBzip2()); 147 } 148 149 DEFINE_TEST(test_read_truncated_filter_compress) 150 { 151 test_truncation("compress", archive_write_add_filter_compress, 0); 152 } 153 154 DEFINE_TEST(test_read_truncated_filter_gzip) 155 { 156 test_truncation("gzip", archive_write_add_filter_gzip, canGzip()); 157 } 158 159 DEFINE_TEST(test_read_truncated_filter_lzip) 160 { 161 test_truncation("lzip", archive_write_add_filter_lzip, canLzip()); 162 } 163 164 DEFINE_TEST(test_read_truncated_filter_lzma) 165 { 166 test_truncation("lzma", archive_write_add_filter_lzma, canLzma()); 167 } 168 169 DEFINE_TEST(test_read_truncated_filter_lzop) 170 { 171 test_truncation("lzop", archive_write_add_filter_lzop, canLzop()); 172 } 173 174 DEFINE_TEST(test_read_truncated_filter_xz) 175 { 176 test_truncation("xz", archive_write_add_filter_xz, canXz()); 177 } 178