test_read_truncated_filter.c revision 232153
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 35static void 36test_truncation(const char *compression, int (*set_compression)(struct archive *)) 37{ 38 struct archive_entry *ae; 39 struct archive* a; 40 char path[16]; 41 char *buff, *data; 42 size_t buffsize, datasize, used1; 43 int i, j, r; 44 45 buffsize = 2000000; 46 assert(NULL != (buff = (char *)malloc(buffsize))); 47 48 datasize = 10000; 49 assert(NULL != (data = (char *)malloc(datasize))); 50 memset(data, 0, datasize); 51 52 /* 53 * Write a bunch of files with semi-random data. 54 */ 55 assert((a = archive_write_new()) != NULL); 56 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 57 assertEqualIntA(a, ARCHIVE_OK, 58 archive_write_set_compression_compress(a)); 59 r = (*set_compression)(a); 60 if (r == ARCHIVE_FATAL) { 61 skipping("%s writing not supported on this platform", compression); 62 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 63 return; 64 } 65 assertEqualIntA(a, ARCHIVE_OK, 66 archive_write_set_bytes_per_block(a, 10)); 67 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used1)); 68 assert((ae = archive_entry_new()) != NULL); 69 archive_entry_set_filetype(ae, AE_IFREG); 70 archive_entry_set_size(ae, datasize); 71 for (i = 0; i < 100; i++) { 72 sprintf(path, "%s%d", compression, i); 73 archive_entry_copy_pathname(ae, path); 74 failure(path); 75 if (!assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae))) { 76 archive_write_free(a); 77 free(data); 78 free(buff); 79 return; 80 } 81 for (j = 0; j < (int)datasize; ++j) { 82 data[j] = (char)(rand() % 256); 83 } 84 failure(path); 85 if (!assertEqualIntA(a, datasize, archive_write_data(a, data, datasize))) { 86 archive_write_free(a); 87 free(data); 88 free(buff); 89 return; 90 } 91 } 92 archive_entry_free(ae); 93 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 94 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 95 96 assert((a = archive_read_new()) != NULL); 97 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 98 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 99 100 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used1 - used1/64)); 101 for (i = 0; i < 100; i++) { 102 if (ARCHIVE_OK != archive_read_next_header(a, &ae)) { 103 failure("Should have non-NULL error message for %s", 104 compression); 105 assert(NULL != archive_error_string(a)); 106 break; 107 } 108 sprintf(path, "%s%d", compression, i); 109 assertEqualString(path, archive_entry_pathname(ae)); 110 if (datasize != (size_t)archive_read_data(a, data, datasize)) { 111 failure("Should have non-NULL error message for %s", 112 compression); 113 assert(NULL != archive_error_string(a)); 114 break; 115 } 116 } 117 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 118 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 119 120 free(data); 121 free(buff); 122} 123 124DEFINE_TEST(test_read_truncated_filter) 125{ 126 test_truncation("bzip2", archive_write_set_compression_bzip2); 127 test_truncation("compress", archive_write_set_compression_compress); 128 test_truncation("gzip", archive_write_set_compression_gzip); 129 test_truncation("lzip", archive_write_set_compression_lzip); 130 test_truncation("lzma", archive_write_set_compression_lzma); 131 test_truncation("xz", archive_write_set_compression_xz); 132} 133