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