1 /*-
2 * Copyright (c) 2012 Michihiro NAKAJIMA
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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "test.h"
27
28 static void
test_filter_by_name(const char * filter_name,int filter_code,int (* can_filter_prog)(void))29 test_filter_by_name(const char *filter_name, int filter_code,
30 int (*can_filter_prog)(void))
31 {
32 struct archive_entry *ae;
33 struct archive *a;
34 size_t used;
35 size_t buffsize = 1024 * 128;
36 char *buff;
37 int r;
38
39 assert((buff = calloc(buffsize, sizeof(char))) != NULL);
40 if (buff == NULL)
41 return;
42
43 /* Create a new archive in memory. */
44 assert((a = archive_write_new()) != NULL);
45 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
46 r = archive_write_add_filter_by_name(a, filter_name);
47 if (r == ARCHIVE_WARN) {
48 if (!can_filter_prog()) {
49 skipping("%s filter not supported on this platform",
50 filter_name);
51 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
52 free(buff);
53 return;
54 }
55 } else if (r == ARCHIVE_FATAL &&
56 (strcmp(archive_error_string(a),
57 "lzma compression not supported on this platform") == 0 ||
58 strcmp(archive_error_string(a),
59 "xz compression not supported on this platform") == 0)) {
60 skipping("%s filter not supported on this platform", filter_name);
61 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
62 free(buff);
63 return;
64 } else {
65 if (!assertEqualIntA(a, ARCHIVE_OK, r)) {
66 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
67 free(buff);
68 return;
69 }
70 }
71 if (filter_code == ARCHIVE_FILTER_LRZIP)
72 {
73 /*
74 * There's a bug in lrzip (as of release 0.612) where 2nd stage
75 * compression can't be performed on smaller files. Set lrzip to
76 * use no 2nd stage compression.
77 */
78 assertEqualIntA(a, ARCHIVE_OK,
79 archive_write_set_options(a, "lrzip:compression=none"));
80 }
81 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 10));
82 assertEqualIntA(a, ARCHIVE_OK,
83 archive_write_open_memory(a, buff, buffsize, &used));
84
85 /*
86 * Write a file to it.
87 */
88 assert((ae = archive_entry_new()) != NULL);
89 archive_entry_set_mtime(ae, 1, 0);
90 assertEqualInt(1, archive_entry_mtime(ae));
91 archive_entry_set_ctime(ae, 1, 0);
92 assertEqualInt(1, archive_entry_ctime(ae));
93 archive_entry_set_atime(ae, 1, 0);
94 assertEqualInt(1, archive_entry_atime(ae));
95 archive_entry_copy_pathname(ae, "file");
96 assertEqualString("file", archive_entry_pathname(ae));
97 archive_entry_set_mode(ae, AE_IFREG | 0755);
98 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
99 archive_entry_set_size(ae, 8);
100 assertEqualInt(0, archive_write_header(a, ae));
101 archive_entry_free(ae);
102 assertEqualInt(8, archive_write_data(a, "12345678", 8));
103
104 /* Close out the archive. */
105 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
106 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
107
108 /*
109 * Now, read the data back.
110 */
111 assert((a = archive_read_new()) != NULL);
112 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
113 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
114 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
115
116 /*
117 * Read and verify the file.
118 */
119 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
120 assertEqualInt(1, archive_entry_mtime(ae));
121 assertEqualString("file", archive_entry_pathname(ae));
122 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
123 assertEqualInt(8, archive_entry_size(ae));
124
125 /* Verify the end of the archive. */
126 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
127
128 /* Verify archive format. */
129 assertEqualIntA(a, filter_code, archive_filter_code(a, 0));
130 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_USTAR, archive_format(a));
131
132 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
133 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
134 free(buff);
135 }
136
137 static int
canAlways(void)138 canAlways(void)
139 {
140 return 1;
141 }
142
DEFINE_TEST(test_archive_write_add_filter_by_name_b64encode)143 DEFINE_TEST(test_archive_write_add_filter_by_name_b64encode)
144 {
145 test_filter_by_name("b64encode", ARCHIVE_FILTER_UU, canAlways);
146 }
147
DEFINE_TEST(test_archive_write_add_filter_by_name_bzip2)148 DEFINE_TEST(test_archive_write_add_filter_by_name_bzip2)
149 {
150 test_filter_by_name("bzip2", ARCHIVE_FILTER_BZIP2, canBzip2);
151 }
152
DEFINE_TEST(test_archive_write_add_filter_by_name_compress)153 DEFINE_TEST(test_archive_write_add_filter_by_name_compress)
154 {
155 test_filter_by_name("compress", ARCHIVE_FILTER_COMPRESS, canAlways);
156 }
157
DEFINE_TEST(test_archive_write_add_filter_by_name_grzip)158 DEFINE_TEST(test_archive_write_add_filter_by_name_grzip)
159 {
160 test_filter_by_name("grzip", ARCHIVE_FILTER_GRZIP, canGrzip);
161 }
162
DEFINE_TEST(test_archive_write_add_filter_by_name_gzip)163 DEFINE_TEST(test_archive_write_add_filter_by_name_gzip)
164 {
165 test_filter_by_name("gzip", ARCHIVE_FILTER_GZIP, canGzip);
166 }
167
DEFINE_TEST(test_archive_write_add_filter_by_name_lrzip)168 DEFINE_TEST(test_archive_write_add_filter_by_name_lrzip)
169 {
170 test_filter_by_name("lrzip", ARCHIVE_FILTER_LRZIP, canLrzip);
171 }
172
DEFINE_TEST(test_archive_write_add_filter_by_name_lz4)173 DEFINE_TEST(test_archive_write_add_filter_by_name_lz4)
174 {
175 test_filter_by_name("lz4", ARCHIVE_FILTER_LZ4, canLz4);
176 }
177
DEFINE_TEST(test_archive_write_add_filter_by_name_lzip)178 DEFINE_TEST(test_archive_write_add_filter_by_name_lzip)
179 {
180 test_filter_by_name("lzip", ARCHIVE_FILTER_LZIP, canLzip);
181 }
182
DEFINE_TEST(test_archive_write_add_filter_by_name_lzma)183 DEFINE_TEST(test_archive_write_add_filter_by_name_lzma)
184 {
185 test_filter_by_name("lzma", ARCHIVE_FILTER_LZMA, canLzma);
186 }
187
DEFINE_TEST(test_archive_write_add_filter_by_name_lzop)188 DEFINE_TEST(test_archive_write_add_filter_by_name_lzop)
189 {
190 test_filter_by_name("lzop", ARCHIVE_FILTER_LZOP, canLzop);
191 }
192
DEFINE_TEST(test_archive_write_add_filter_by_name_uuencode)193 DEFINE_TEST(test_archive_write_add_filter_by_name_uuencode)
194 {
195 test_filter_by_name("uuencode", ARCHIVE_FILTER_UU, canAlways);
196 }
197
DEFINE_TEST(test_archive_write_add_filter_by_name_xz)198 DEFINE_TEST(test_archive_write_add_filter_by_name_xz)
199 {
200 test_filter_by_name("xz", ARCHIVE_FILTER_XZ, canXz);
201 }
202
DEFINE_TEST(test_archive_write_add_filter_by_name_zstd)203 DEFINE_TEST(test_archive_write_add_filter_by_name_zstd)
204 {
205 test_filter_by_name("zstd", ARCHIVE_FILTER_ZSTD, canZstd);
206 }
207