]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_write_compress_bzip2.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_bzip2.c
1 /*-
2  * Copyright (c) 2007 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 bzip2 reading and writing.
32  *
33  * TODO: Add a reference file and make sure we can decompress that.
34  */
35
36 DEFINE_TEST(test_write_compress_bzip2)
37 {
38         struct archive_entry *ae;
39         struct archive* a;
40         char *buff, *data;
41         size_t buffsize, datasize;
42         char path[16];
43         size_t used1, used2;
44         int i, r;
45
46         buffsize = 2000000;
47         assert(NULL != (buff = (char *)malloc(buffsize)));
48
49         datasize = 10000;
50         assert(NULL != (data = (char *)malloc(datasize)));
51         memset(data, 0, datasize);
52
53         /*
54          * Write a 100 files and read them all back.
55          */
56         assert((a = archive_write_new()) != NULL);
57         assertA(0 == archive_write_set_format_ustar(a));
58         r = archive_write_set_compression_bzip2(a);
59         if (r == ARCHIVE_FATAL) {
60                 skipping("bzip2 writing not supported on this platform");
61                 assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
62                 return;
63         }
64         assertEqualIntA(a, ARCHIVE_OK,
65             archive_write_set_bytes_per_block(a, 10));
66         assertEqualInt(ARCHIVE_COMPRESSION_BZIP2, archive_compression(a));
67         assertEqualString("bzip2", archive_compression_name(a));
68         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used1));
69         assertEqualInt(ARCHIVE_COMPRESSION_BZIP2, archive_compression(a));
70         assertEqualString("bzip2", 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 < 999; i++) {
75                 sprintf(path, "file%03d", i);
76                 archive_entry_copy_pathname(ae, path);
77                 assertA(0 == archive_write_header(a, ae));
78                 assertA(datasize
79                     == (size_t)archive_write_data(a, data, datasize));
80         }
81         archive_entry_free(ae);
82         archive_write_close(a);
83         assert(0 == archive_write_finish(a));
84
85         assert((a = archive_read_new()) != NULL);
86         assertA(0 == archive_read_support_format_all(a));
87         assertA(0 == archive_read_support_compression_all(a));
88         assertA(0 == archive_read_open_memory(a, buff, used1));
89         for (i = 0; i < 999; i++) {
90                 sprintf(path, "file%03d", i);
91                 if (!assertEqualInt(0, archive_read_next_header(a, &ae)))
92                         break;
93                 assertEqualString(path, archive_entry_pathname(ae));
94                 assertEqualInt((int)datasize, archive_entry_size(ae));
95         }
96         assert(0 == archive_read_close(a));
97         assert(0 == archive_read_finish(a));
98
99         /*
100          * Repeat the cycle again, this time setting some compression
101          * options.
102          */
103         assert((a = archive_write_new()) != NULL);
104         assertA(0 == archive_write_set_format_ustar(a));
105         assertEqualIntA(a, ARCHIVE_OK,
106             archive_write_set_bytes_per_block(a, 10));
107         assertA(0 == archive_write_set_compression_bzip2(a));
108         assertEqualIntA(a, ARCHIVE_WARN,
109             archive_write_set_compressor_options(a, "nonexistent-option=0"));
110         assertEqualIntA(a, ARCHIVE_WARN,
111             archive_write_set_compressor_options(a, "compression-level=abc"));
112         assertEqualIntA(a, ARCHIVE_WARN,
113             archive_write_set_compressor_options(a, "compression-level=99"));
114         assertEqualIntA(a, ARCHIVE_OK,
115             archive_write_set_compressor_options(a, "compression-level=9"));
116         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
117         for (i = 0; i < 999; i++) {
118                 sprintf(path, "file%03d", i);
119                 assert((ae = archive_entry_new()) != NULL);
120                 archive_entry_copy_pathname(ae, path);
121                 archive_entry_set_size(ae, datasize);
122                 archive_entry_set_filetype(ae, AE_IFREG);
123                 assertA(0 == archive_write_header(a, ae));
124                 assertA(datasize == (size_t)archive_write_data(a, data, datasize));
125                 archive_entry_free(ae);
126         }
127         archive_write_close(a);
128         assert(0 == archive_write_finish(a));
129
130         /* Curiously, this test fails; the test data above compresses
131          * better at default compression than at level 9. */
132         /*
133         failure("compression-level=9 wrote %d bytes, default wrote %d bytes",
134             (int)used2, (int)used1);
135         assert(used2 < used1);
136         */
137
138         assert((a = archive_read_new()) != NULL);
139         assertA(0 == archive_read_support_format_all(a));
140         assertA(0 == archive_read_support_compression_all(a));
141         assertA(0 == archive_read_open_memory(a, buff, used2));
142         for (i = 0; i < 999; i++) {
143                 sprintf(path, "file%03d", i);
144                 if (!assertEqualInt(0, archive_read_next_header(a, &ae)))
145                         break;
146                 assertEqualString(path, archive_entry_pathname(ae));
147                 assertEqualInt((int)datasize, archive_entry_size(ae));
148         }
149         assert(0 == archive_read_close(a));
150         assert(0 == archive_read_finish(a));
151
152         /*
153          * Repeat again, with much lower compression.
154          */
155         assert((a = archive_write_new()) != NULL);
156         assertA(0 == archive_write_set_format_ustar(a));
157         assertEqualIntA(a, ARCHIVE_OK,
158             archive_write_set_bytes_per_block(a, 10));
159         assertA(0 == archive_write_set_compression_bzip2(a));
160         assertEqualIntA(a, ARCHIVE_OK,
161             archive_write_set_compressor_options(a, "compression-level=1"));
162         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
163         for (i = 0; i < 999; i++) {
164                 sprintf(path, "file%03d", i);
165                 assert((ae = archive_entry_new()) != NULL);
166                 archive_entry_copy_pathname(ae, path);
167                 archive_entry_set_size(ae, datasize);
168                 archive_entry_set_filetype(ae, AE_IFREG);
169                 assertA(0 == archive_write_header(a, ae));
170                 failure("Writing file %s", path);
171                 assertEqualIntA(a, datasize,
172                     (size_t)archive_write_data(a, data, datasize));
173                 archive_entry_free(ae);
174         }
175         archive_write_close(a);
176         assert(0 == archive_write_finish(a));
177
178         /* Level 0 really does result in larger data. */
179         failure("Compression-level=0 wrote %d bytes; default wrote %d bytes",
180             (int)used2, (int)used1);
181         assert(used2 > used1);
182
183         assert((a = archive_read_new()) != NULL);
184         assertA(0 == archive_read_support_format_all(a));
185         assertA(0 == archive_read_support_compression_all(a));
186         assertA(0 == archive_read_open_memory(a, buff, used2));
187         for (i = 0; i < 999; i++) {
188                 sprintf(path, "file%03d", i);
189                 if (!assertEqualInt(0, archive_read_next_header(a, &ae)))
190                         break;
191                 assertEqualString(path, archive_entry_pathname(ae));
192                 assertEqualInt((int)datasize, archive_entry_size(ae));
193         }
194         assert(0 == archive_read_close(a));
195         assert(0 == archive_read_finish(a));
196
197         /*
198          * Test various premature shutdown scenarios to make sure we
199          * don't crash or leak memory.
200          */
201         assert((a = archive_write_new()) != NULL);
202         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_bzip2(a));
203         assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
204
205         assert((a = archive_write_new()) != NULL);
206         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_bzip2(a));
207         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
208         assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
209
210         assert((a = archive_write_new()) != NULL);
211         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
212         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_bzip2(a));
213         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
214         assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
215
216         assert((a = archive_write_new()) != NULL);
217         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
218         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_bzip2(a));
219         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
220         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
221         assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
222
223         /*
224          * Clean up.
225          */
226         free(data);
227         free(buff);
228 }