2 * Copyright (c) 2007 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 xz reading and writing.
33 * TODO: Add a reference file and make sure we can decompress that.
36 DEFINE_TEST(test_write_compress_xz)
38 struct archive_entry *ae;
41 size_t buffsize, datasize;
47 assert(NULL != (buff = (char *)malloc(buffsize)));
50 assert(NULL != (data = (char *)malloc(datasize)));
51 memset(data, 0, datasize);
54 * Write a 100 files and read them all back.
56 assert((a = archive_write_new()) != NULL);
57 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
58 r = archive_write_set_compression_xz(a);
59 if (r == ARCHIVE_FATAL) {
60 skipping("xz writing not supported on this platform");
61 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
64 assertEqualIntA(a, ARCHIVE_OK,
65 archive_write_set_bytes_per_block(a, 10));
66 assertEqualInt(ARCHIVE_COMPRESSION_XZ, archive_compression(a));
67 assertEqualString("xz", archive_compression_name(a));
68 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used1));
69 assertEqualInt(ARCHIVE_COMPRESSION_XZ, archive_compression(a));
70 assertEqualString("xz", archive_compression_name(a));
71 assert((ae = archive_entry_new()) != NULL);
72 archive_entry_set_filetype(ae, AE_IFREG);
73 archive_entry_set_size(ae, datasize);
74 for (i = 0; i < 100; i++) {
75 sprintf(path, "file%03d", i);
76 archive_entry_copy_pathname(ae, path);
77 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
79 == (size_t)archive_write_data(a, data, datasize));
81 archive_entry_free(ae);
82 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
83 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
85 assert((a = archive_read_new()) != NULL);
86 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
87 r = archive_read_support_filter_xz(a);
88 if (r == ARCHIVE_WARN) {
89 skipping("Can't verify xz writing by reading back;"
90 " xz reading not fully supported on this platform");
92 assertEqualIntA(a, ARCHIVE_OK,
93 archive_read_support_filter_all(a));
94 assertEqualIntA(a, ARCHIVE_OK,
95 archive_read_open_memory(a, buff, used1));
96 for (i = 0; i < 100; i++) {
97 sprintf(path, "file%03d", i);
98 if (!assertEqualInt(ARCHIVE_OK,
99 archive_read_next_header(a, &ae)))
101 assertEqualString(path, archive_entry_pathname(ae));
102 assertEqualInt((int)datasize, archive_entry_size(ae));
104 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
106 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
109 * Repeat the cycle again, this time setting some compression
112 assert((a = archive_write_new()) != NULL);
113 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
114 assertEqualIntA(a, ARCHIVE_OK,
115 archive_write_set_bytes_per_block(a, 10));
116 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_xz(a));
117 assertEqualIntA(a, ARCHIVE_FAILED,
118 archive_write_set_filter_option(a, NULL, "nonexistent-option", "0"));
119 assertEqualIntA(a, ARCHIVE_FAILED,
120 archive_write_set_filter_option(a, NULL, "compression-level", "abc"));
121 assertEqualIntA(a, ARCHIVE_FAILED,
122 archive_write_set_filter_option(a, NULL, "compression-level", "99"));
123 assertEqualIntA(a, ARCHIVE_OK,
124 archive_write_set_filter_option(a, NULL, "compression-level", "9"));
125 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2));
126 for (i = 0; i < 100; i++) {
127 sprintf(path, "file%03d", i);
128 assert((ae = archive_entry_new()) != NULL);
129 archive_entry_copy_pathname(ae, path);
130 archive_entry_set_size(ae, datasize);
131 archive_entry_set_filetype(ae, AE_IFREG);
132 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
133 assertA(datasize == (size_t)archive_write_data(a, data, datasize));
134 archive_entry_free(ae);
136 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
137 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
139 /* Curiously, this test fails; the test data above compresses
140 * better at default compression than at level 9. */
142 failure("compression-level=9 wrote %d bytes, default wrote %d bytes",
143 (int)used2, (int)used1);
144 assert(used2 < used1);
147 assert((a = archive_read_new()) != NULL);
148 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
149 r = archive_read_support_filter_xz(a);
150 if (r == ARCHIVE_WARN) {
151 skipping("xz reading not fully supported on this platform");
153 assertEqualIntA(a, ARCHIVE_OK,
154 archive_read_support_filter_all(a));
155 assertEqualIntA(a, ARCHIVE_OK,
156 archive_read_open_memory(a, buff, used2));
157 for (i = 0; i < 100; i++) {
158 sprintf(path, "file%03d", i);
159 failure("Trying to read %s", path);
160 if (!assertEqualIntA(a, ARCHIVE_OK,
161 archive_read_next_header(a, &ae)))
163 assertEqualString(path, archive_entry_pathname(ae));
164 assertEqualInt((int)datasize, archive_entry_size(ae));
166 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
168 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
171 * Repeat again, with much lower compression.
173 assert((a = archive_write_new()) != NULL);
174 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
175 assertEqualIntA(a, ARCHIVE_OK,
176 archive_write_set_bytes_per_block(a, 10));
177 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_xz(a));
178 assertEqualIntA(a, ARCHIVE_OK,
179 archive_write_set_filter_option(a, NULL, "compression-level", "0"));
180 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2));
181 for (i = 0; i < 100; i++) {
182 sprintf(path, "file%03d", i);
183 assert((ae = archive_entry_new()) != NULL);
184 archive_entry_copy_pathname(ae, path);
185 archive_entry_set_size(ae, datasize);
186 archive_entry_set_filetype(ae, AE_IFREG);
187 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
188 failure("Writing file %s", path);
189 assertEqualIntA(a, datasize,
190 (size_t)archive_write_data(a, data, datasize));
191 archive_entry_free(ae);
193 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
194 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
196 /* I would like to assert that compression-level=0 results in
197 * larger data than the default compression, but that's not true
198 * for all versions of liblzma. */
200 failure("Compression-level=0 wrote %d bytes; default wrote %d bytes",
201 (int)used2, (int)used1);
202 assert(used2 > used1);
205 assert((a = archive_read_new()) != NULL);
206 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
207 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
208 r = archive_read_support_filter_xz(a);
209 if (r == ARCHIVE_WARN) {
210 skipping("xz reading not fully supported on this platform");
212 assertEqualIntA(a, ARCHIVE_OK,
213 archive_read_open_memory(a, buff, used2));
214 for (i = 0; i < 100; i++) {
215 sprintf(path, "file%03d", i);
216 if (!assertEqualInt(ARCHIVE_OK,
217 archive_read_next_header(a, &ae)))
219 assertEqualString(path, archive_entry_pathname(ae));
220 assertEqualInt((int)datasize, archive_entry_size(ae));
222 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
224 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
227 * Test various premature shutdown scenarios to make sure we
228 * don't crash or leak memory.
230 assert((a = archive_write_new()) != NULL);
231 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_xz(a));
232 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
234 assert((a = archive_write_new()) != NULL);
235 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_xz(a));
236 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
237 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
239 assert((a = archive_write_new()) != NULL);
240 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
241 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_xz(a));
242 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
243 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
245 assert((a = archive_write_new()) != NULL);
246 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
247 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_xz(a));
248 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2));
249 assertEqualInt(ARCHIVE_OK, archive_write_close(a));
250 assertEqualInt(ARCHIVE_OK, archive_write_free(a));