1 /*-
2  * Copyright (c) 2007-2009 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: head/lib/libarchive/test/test_write_compress_lzma.c 191183 2009-04-17 01:06:31Z kientzle $");
29 
30 /*
31  * A basic exercise of lzma reading and writing.
32  *
33  */
34 
DEFINE_TEST(test_write_compress_lzma)35 DEFINE_TEST(test_write_compress_lzma)
36 {
37 	struct archive_entry *ae;
38 	struct archive* a;
39 	char *buff, *data;
40 	size_t buffsize, datasize;
41 	char path[16];
42 	size_t used1, used2;
43 	int i, 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 100 files and read them all back.
54 	 */
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));
61 		return;
62 	}
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));
77 		assertA(datasize
78 		    == (size_t)archive_write_data(a, data, datasize));
79 	}
80 	archive_entry_free(ae);
81 	archive_write_close(a);
82 	assert(0 == archive_write_finish(a));
83 
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");
90 	} else {
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)))
99 				break;
100 			assertEqualString(path, archive_entry_pathname(ae));
101 			assertEqualInt((int)datasize, archive_entry_size(ae));
102 		}
103 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
104 	}
105 	assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
106 
107 	/*
108 	 * Repeat the cycle again, this time setting some compression
109 	 * options.
110 	 */
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);
134 	}
135 	archive_write_close(a);
136 	assert(0 == archive_write_finish(a));
137 
138 
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");
144 	} else {
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)))
154 				break;
155 			assertEqualString(path, archive_entry_pathname(ae));
156 			assertEqualInt((int)datasize, archive_entry_size(ae));
157 		}
158 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
159 	}
160 	assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
161 
162 	/*
163 	 * Repeat again, with much lower compression.
164 	 */
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);
184 	}
185 	archive_write_close(a);
186 	assert(0 == archive_write_finish(a));
187 
188 	/* Level 0 really does result in larger data. */
189 	failure("Compression-level=0 wrote %d bytes; default wrote %d bytes",
190 	    (int)used2, (int)used1);
191 	assert(used2 > used1);
192 
193 	assert((a = archive_read_new()) != NULL);
194 	assertA(0 == archive_read_support_format_all(a));
195 	assertA(0 == archive_read_support_compression_all(a));
196 	r = archive_read_support_compression_lzma(a);
197 	if (r == ARCHIVE_WARN) {
198 		skipping("lzma reading not fully supported on this platform");
199 	} else {
200 		assertEqualIntA(a, ARCHIVE_OK,
201 		    archive_read_open_memory(a, buff, used2));
202 		for (i = 0; i < 100; i++) {
203 			sprintf(path, "file%03d", i);
204 			if (!assertEqualInt(ARCHIVE_OK,
205 				archive_read_next_header(a, &ae)))
206 				break;
207 			assertEqualString(path, archive_entry_pathname(ae));
208 			assertEqualInt((int)datasize, archive_entry_size(ae));
209 		}
210 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
211 	}
212 	assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
213 
214 	/*
215 	 * Test various premature shutdown scenarios to make sure we
216 	 * don't crash or leak memory.
217 	 */
218 	assert((a = archive_write_new()) != NULL);
219 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
220 	assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
221 
222 	assert((a = archive_write_new()) != NULL);
223 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
224 	assertEqualInt(ARCHIVE_OK, archive_write_close(a));
225 	assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
226 
227 	assert((a = archive_write_new()) != NULL);
228 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
229 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
230 	assertEqualInt(ARCHIVE_OK, archive_write_close(a));
231 	assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
232 
233 	assert((a = archive_write_new()) != NULL);
234 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
235 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_lzma(a));
236 	assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
237 	assertEqualInt(ARCHIVE_OK, archive_write_close(a));
238 	assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
239 
240 	/*
241 	 * Clean up.
242 	 */
243 	free(data);
244 	free(buff);
245 }
246