2 * Copyright (c) 2007-2009 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 __FBSDID("$FreeBSD$");
31 * A basic exercise of lzma reading and writing.
35 DEFINE_TEST(test_write_compress_lzma)
37 struct archive_entry *ae;
40 size_t buffsize, datasize;
46 assert(NULL != (buff = (char *)malloc(buffsize)));
49 assert(NULL != (data = (char *)malloc(datasize)));
50 memset(data, 0, datasize);
53 * Write a 100 files and read them all back.
55 assert((a = archive_write_new()) != NULL);
56 assertA(0 == archive_write_set_format_ustar(a));
57 r = archive_write_set_compression_lzma(a);
58 if (r == ARCHIVE_FATAL) {
59 skipping("lzma writing not supported on this platform");
60 assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
63 assertEqualIntA(a, ARCHIVE_OK,
64 archive_write_set_bytes_per_block(a, 10));
65 assertEqualInt(ARCHIVE_COMPRESSION_LZMA, archive_compression(a));
66 assertEqualString("lzma", archive_compression_name(a));
67 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used1));
68 assertEqualInt(ARCHIVE_COMPRESSION_LZMA, archive_compression(a));
69 assertEqualString("lzma", archive_compression_name(a));
70 assert((ae = archive_entry_new()) != NULL);
71 archive_entry_set_filetype(ae, AE_IFREG);
72 archive_entry_set_size(ae, datasize);
73 for (i = 0; i < 100; i++) {
74 sprintf(path, "file%03d", i);
75 archive_entry_copy_pathname(ae, path);
76 assertA(0 == archive_write_header(a, ae));
78 == (size_t)archive_write_data(a, data, datasize));
80 archive_entry_free(ae);
81 archive_write_close(a);
82 assert(0 == archive_write_finish(a));
84 assert((a = archive_read_new()) != NULL);
85 assertA(0 == archive_read_support_format_all(a));
86 r = archive_read_support_compression_lzma(a);
87 if (r == ARCHIVE_WARN) {
88 skipping("Can't verify lzma writing by reading back;"
89 " lzma reading not fully supported on this platform");
91 assertEqualIntA(a, ARCHIVE_OK,
92 archive_read_support_compression_all(a));
93 assertEqualIntA(a, ARCHIVE_OK,
94 archive_read_open_memory(a, buff, used1));
95 for (i = 0; i < 100; i++) {
96 sprintf(path, "file%03d", i);
97 if (!assertEqualInt(ARCHIVE_OK,
98 archive_read_next_header(a, &ae)))
100 assertEqualString(path, archive_entry_pathname(ae));
101 assertEqualInt((int)datasize, archive_entry_size(ae));
103 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
105 assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
108 * Repeat the cycle again, this time setting some compression
111 assert((a = archive_write_new()) != NULL);
112 assertA(0 == archive_write_set_format_ustar(a));
113 assertEqualIntA(a, ARCHIVE_OK,
114 archive_write_set_bytes_per_block(a, 10));
115 assertA(0 == archive_write_set_compression_lzma(a));
116 assertEqualIntA(a, ARCHIVE_WARN,
117 archive_write_set_compressor_options(a, "nonexistent-option=0"));
118 assertEqualIntA(a, ARCHIVE_WARN,
119 archive_write_set_compressor_options(a, "compression-level=abc"));
120 assertEqualIntA(a, ARCHIVE_WARN,
121 archive_write_set_compressor_options(a, "compression-level=99"));
122 assertEqualIntA(a, ARCHIVE_OK,
123 archive_write_set_compressor_options(a, "compression-level=9"));
124 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
125 for (i = 0; i < 100; i++) {
126 sprintf(path, "file%03d", i);
127 assert((ae = archive_entry_new()) != NULL);
128 archive_entry_copy_pathname(ae, path);
129 archive_entry_set_size(ae, datasize);
130 archive_entry_set_filetype(ae, AE_IFREG);
131 assertA(0 == archive_write_header(a, ae));
132 assertA(datasize == (size_t)archive_write_data(a, data, datasize));
133 archive_entry_free(ae);
135 archive_write_close(a);
136 assert(0 == archive_write_finish(a));
139 assert((a = archive_read_new()) != NULL);
140 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
141 r = archive_read_support_compression_lzma(a);
142 if (r == ARCHIVE_WARN) {
143 skipping("lzma reading not fully supported on this platform");
145 assertEqualIntA(a, ARCHIVE_OK,
146 archive_read_support_compression_all(a));
147 assertEqualIntA(a, ARCHIVE_OK,
148 archive_read_open_memory(a, buff, used2));
149 for (i = 0; i < 100; i++) {
150 sprintf(path, "file%03d", i);
151 failure("Trying to read %s", path);
152 if (!assertEqualIntA(a, ARCHIVE_OK,
153 archive_read_next_header(a, &ae)))
155 assertEqualString(path, archive_entry_pathname(ae));
156 assertEqualInt((int)datasize, archive_entry_size(ae));
158 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
160 assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
163 * Repeat again, with much lower compression.
165 assert((a = archive_write_new()) != NULL);
166 assertA(0 == archive_write_set_format_ustar(a));
167 assertEqualIntA(a, ARCHIVE_OK,
168 archive_write_set_bytes_per_block(a, 10));
169 assertA(0 == archive_write_set_compression_lzma(a));
170 assertEqualIntA(a, ARCHIVE_OK,
171 archive_write_set_compressor_options(a, "compression-level=0"));
172 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
173 for (i = 0; i < 100; i++) {
174 sprintf(path, "file%03d", i);
175 assert((ae = archive_entry_new()) != NULL);
176 archive_entry_copy_pathname(ae, path);
177 archive_entry_set_size(ae, datasize);
178 archive_entry_set_filetype(ae, AE_IFREG);
179 assertA(0 == archive_write_header(a, ae));
180 failure("Writing file %s", path);
181 assertEqualIntA(a, datasize,
182 (size_t)archive_write_data(a, data, datasize));
183 archive_entry_free(ae);
185 archive_write_close(a);
186 assert(0 == archive_write_finish(a));
188 /* It would be nice to assert that compression-level=0 produced
189 * consistently larger/smaller results than the default compression,
190 * but the results here vary a lot depending on the version of liblzma
193 failure("Compression-level=0 wrote %d bytes; default wrote %d bytes",
194 (int)used2, (int)used1);
195 assert(used2 > used1);
198 assert((a = archive_read_new()) != NULL);
199 assertA(0 == archive_read_support_format_all(a));
200 assertA(0 == archive_read_support_compression_all(a));
201 r = archive_read_support_compression_lzma(a);
202 if (r == ARCHIVE_WARN) {
203 skipping("lzma reading not fully supported on this platform");
205 assertEqualIntA(a, ARCHIVE_OK,
206 archive_read_open_memory(a, buff, used2));
207 for (i = 0; i < 100; i++) {
208 sprintf(path, "file%03d", i);
209 if (!assertEqualInt(ARCHIVE_OK,
210 archive_read_next_header(a, &ae)))
212 assertEqualString(path, archive_entry_pathname(ae));
213 assertEqualInt((int)datasize, archive_entry_size(ae));
215 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
217 assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
220 * Test various premature shutdown scenarios to make sure we
221 * don't crash or leak memory.
223 assert((a = archive_write_new()) != NULL);
224 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
225 assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
227 assert((a = archive_write_new()) != NULL);
228 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
229 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
230 assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
232 assert((a = archive_write_new()) != NULL);
233 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
234 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
235 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
236 assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
238 assert((a = archive_write_new()) != NULL);
239 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
240 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
241 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
242 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
243 assertEqualInt(ARCHIVE_OK, archive_write_finish(a));