]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_write_compress_lzma.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / libarchive / test / test_write_compress_lzma.c
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$");
29
30 /*
31  * A basic exercise of lzma reading and writing.
32  *
33  */
34
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         /* 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
191          * being used. */
192         /* 
193         failure("Compression-level=0 wrote %d bytes; default wrote %d bytes",
194             (int)used2, (int)used1);
195         assert(used2 > used1);
196         */
197
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");
204         } else {
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)))
211                                 break;
212                         assertEqualString(path, archive_entry_pathname(ae));
213                         assertEqualInt((int)datasize, archive_entry_size(ae));
214                 }
215                 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
216         }
217         assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
218
219         /*
220          * Test various premature shutdown scenarios to make sure we
221          * don't crash or leak memory.
222          */
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));
226
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));
231
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));
237
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));
244
245         /*
246          * Clean up.
247          */
248         free(data);
249         free(buff);
250 }