]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_write_filter_zstd.c
MFH r324148:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_write_filter_zstd.c
1 /*-
2  * Copyright (c) 2017 Sean Purcell
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 DEFINE_TEST(test_write_filter_zstd)
31 {
32         struct archive_entry *ae;
33         struct archive *a;
34         char *buff, *data;
35         size_t buffsize, datasize;
36         char path[16];
37         size_t used1, used2;
38         int i, r;
39
40         buffsize = 2000000;
41         assert(NULL != (buff = (char *)malloc(buffsize)));
42         if (buff == NULL)
43                 return;
44
45         datasize = 10000;
46         assert(NULL != (data = (char *)malloc(datasize)));
47         if (data == NULL) {
48                 free(buff);
49                 return;
50         }
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         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
58         r = archive_write_add_filter_zstd(a);
59         if (r != ARCHIVE_OK) {
60                 skipping("zstd writing not supported on this platform");
61                 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
62                 free(buff);
63                 free(data);
64                 return;
65         }
66         assertEqualIntA(a, ARCHIVE_OK,
67             archive_write_set_bytes_per_block(a, 10));
68         assertEqualInt(ARCHIVE_FILTER_ZSTD, archive_filter_code(a, 0));
69         assertEqualString("zstd", archive_filter_name(a, 0));
70         assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used1));
71         assertEqualInt(ARCHIVE_FILTER_ZSTD, archive_filter_code(a, 0));
72         assertEqualString("zstd", archive_filter_name(a, 0));
73         assert((ae = archive_entry_new()) != NULL);
74         archive_entry_set_filetype(ae, AE_IFREG);
75         archive_entry_set_size(ae, datasize);
76         for (i = 0; i < 100; i++) {
77                 sprintf(path, "file%03d", i);
78                 archive_entry_copy_pathname(ae, path);
79                 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
80                 assertA(datasize
81                     == (size_t)archive_write_data(a, data, datasize));
82         }
83         archive_entry_free(ae);
84         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
85         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
86
87         assert((a = archive_read_new()) != NULL);
88         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
89         r = archive_read_support_filter_zstd(a);
90         if (r == ARCHIVE_WARN) {
91                 skipping("Can't verify zstd writing by reading back;"
92                     " zstd reading not fully supported on this platform");
93         } else {
94                 assertEqualIntA(a, ARCHIVE_OK,
95                     archive_read_support_filter_all(a));
96                 assertEqualIntA(a, ARCHIVE_OK,
97                     archive_read_open_memory(a, buff, used1));
98                 for (i = 0; i < 100; i++) {
99                         sprintf(path, "file%03d", i);
100                         if (!assertEqualInt(ARCHIVE_OK,
101                                 archive_read_next_header(a, &ae)))
102                                 break;
103                         assertEqualString(path, archive_entry_pathname(ae));
104                         assertEqualInt((int)datasize, archive_entry_size(ae));
105                 }
106                 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
107         }
108         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
109
110         /*
111          * Repeat the cycle again, this time setting some compression
112          * options.
113          */
114         assert((a = archive_write_new()) != NULL);
115         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
116         assertEqualIntA(a, ARCHIVE_OK,
117             archive_write_set_bytes_per_block(a, 10));
118         assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_zstd(a));
119         assertEqualIntA(a, ARCHIVE_FAILED,
120             archive_write_set_filter_option(a, NULL, "nonexistent-option", "0"));
121         assertEqualIntA(a, ARCHIVE_FAILED,
122             archive_write_set_filter_option(a, NULL, "compression-level", "abc"));
123         assertEqualIntA(a, ARCHIVE_FAILED,
124             archive_write_set_filter_option(a, NULL, "compression-level", "25")); /* too big */
125         assertEqualIntA(a, ARCHIVE_OK,
126             archive_write_set_filter_option(a, NULL, "compression-level", "9"));
127         assertEqualIntA(a, ARCHIVE_OK,
128             archive_write_set_filter_option(a, NULL, "compression-level", "15"));
129         assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2));
130         for (i = 0; i < 100; i++) {
131                 sprintf(path, "file%03d", i);
132                 assert((ae = archive_entry_new()) != NULL);
133                 archive_entry_copy_pathname(ae, path);
134                 archive_entry_set_size(ae, datasize);
135                 archive_entry_set_filetype(ae, AE_IFREG);
136                 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
137                 assertA(datasize == (size_t)archive_write_data(a, data, datasize));
138                 archive_entry_free(ae);
139         }
140         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
141         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
142
143         failure("compression-level=15 wrote %d bytes, default wrote %d bytes",
144             (int)used2, (int)used1);
145         assert(used2 < used1);
146
147         assert((a = archive_read_new()) != NULL);
148         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
149         r = archive_read_support_filter_zstd(a);
150         if (r == ARCHIVE_WARN) {
151                 skipping("zstd reading not fully supported on this platform");
152         } else {
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)))
162                                 break;
163                         assertEqualString(path, archive_entry_pathname(ae));
164                         assertEqualInt((int)datasize, archive_entry_size(ae));
165                 }
166                 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
167         }
168         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
169
170         /*
171          * Test various premature shutdown scenarios to make sure we
172          * don't crash or leak memory.
173          */
174         assert((a = archive_write_new()) != NULL);
175         assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_zstd(a));
176         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
177
178         assert((a = archive_write_new()) != NULL);
179         assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_zstd(a));
180         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
181         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
182
183         assert((a = archive_write_new()) != NULL);
184         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
185         assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_zstd(a));
186         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
187         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
188
189         assert((a = archive_write_new()) != NULL);
190         assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
191         assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_zstd(a));
192         assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2));
193         assertEqualInt(ARCHIVE_OK, archive_write_close(a));
194         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
195
196         /*
197          * Clean up.
198          */
199         free(data);
200         free(buff);
201 }