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
27 #include "test.h"
28 __FBSDID("$FreeBSD$");
29
30 static void
test_filter_by_name(const char * filter_name,int filter_code,int (* can_filter_prog)(void))31 test_filter_by_name(const char *filter_name, int filter_code,
32 int (*can_filter_prog)(void))
33 {
34 struct archive_entry *ae;
35 struct archive *a;
36 size_t used;
37 size_t buffsize = 1024 * 128;
38 char *buff;
39 int r;
40
41 assert((buff = calloc(1, buffsize)) != NULL);
42 if (buff == NULL)
43 return;
44
45 /* Create a new archive in memory. */
46 assert((a = archive_write_new()) != NULL);
47 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
48 r = archive_write_add_filter_by_name(a, filter_name);
49 if (r == ARCHIVE_WARN) {
50 if (!can_filter_prog()) {
51 skipping("%s filter not suported on this platform",
52 filter_name);
53 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
54 free(buff);
55 return;
56 }
57 } else if (r == ARCHIVE_FATAL &&
58 (strcmp(archive_error_string(a),
59 "lzma compression not supported on this platform") == 0 ||
60 strcmp(archive_error_string(a),
61 "xz compression not supported on this platform") == 0)) {
62 skipping("%s filter not suported on this platform", filter_name);
63 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
64 free(buff);
65 return;
66 } else {
67 if (!assertEqualIntA(a, ARCHIVE_OK, r)) {
68 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
69 free(buff);
70 return;
71 }
72 }
73 if (filter_code == ARCHIVE_FILTER_LRZIP)
74 {
75 /*
76 * There's a bug in lrzip (as of release 0.612) where 2nd stage
77 * compression can't be performed on smaller files. Set lrzip to
78 * use no 2nd stage compression.
79 */
80 assertEqualIntA(a, ARCHIVE_OK,
81 archive_write_set_options(a, "lrzip:compression=none"));
82 }
83 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 10));
84 assertEqualIntA(a, ARCHIVE_OK,
85 archive_write_open_memory(a, buff, buffsize, &used));
86
87 /*
88 * Write a file to it.
89 */
90 assert((ae = archive_entry_new()) != NULL);
91 archive_entry_set_mtime(ae, 1, 0);
92 assertEqualInt(1, archive_entry_mtime(ae));
93 archive_entry_set_ctime(ae, 1, 0);
94 assertEqualInt(1, archive_entry_ctime(ae));
95 archive_entry_set_atime(ae, 1, 0);
96 assertEqualInt(1, archive_entry_atime(ae));
97 archive_entry_copy_pathname(ae, "file");
98 assertEqualString("file", archive_entry_pathname(ae));
99 archive_entry_set_mode(ae, AE_IFREG | 0755);
100 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
101 archive_entry_set_size(ae, 8);
102 assertEqualInt(0, archive_write_header(a, ae));
103 archive_entry_free(ae);
104 assertEqualInt(8, archive_write_data(a, "12345678", 8));
105
106 /* Close out the archive. */
107 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
108 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
109
110 /*
111 * Now, read the data back.
112 */
113 assert((a = archive_read_new()) != NULL);
114 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
115 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
116 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
117
118 /*
119 * Read and verify the file.
120 */
121 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
122 assertEqualInt(1, archive_entry_mtime(ae));
123 assertEqualString("file", archive_entry_pathname(ae));
124 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
125 assertEqualInt(8, archive_entry_size(ae));
126
127 /* Verify the end of the archive. */
128 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
129
130 /* Verify archive format. */
131 assertEqualIntA(a, filter_code, archive_filter_code(a, 0));
132 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_USTAR, archive_format(a));
133
134 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
135 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
136 free(buff);
137 }
138
139 static int
canAlways(void)140 canAlways(void)
141 {
142 return 1;
143 }
144
145 static int
cannot(void)146 cannot(void)
147 {
148 return 0;
149 }
150
DEFINE_TEST(test_archive_write_add_filter_by_name_b64encode)151 DEFINE_TEST(test_archive_write_add_filter_by_name_b64encode)
152 {
153 test_filter_by_name("b64encode", ARCHIVE_FILTER_UU, canAlways);
154 }
155
DEFINE_TEST(test_archive_write_add_filter_by_name_bzip2)156 DEFINE_TEST(test_archive_write_add_filter_by_name_bzip2)
157 {
158 test_filter_by_name("bzip2", ARCHIVE_FILTER_BZIP2, canBzip2);
159 }
160
DEFINE_TEST(test_archive_write_add_filter_by_name_compress)161 DEFINE_TEST(test_archive_write_add_filter_by_name_compress)
162 {
163 test_filter_by_name("compress", ARCHIVE_FILTER_COMPRESS, canAlways);
164 }
165
DEFINE_TEST(test_archive_write_add_filter_by_name_grzip)166 DEFINE_TEST(test_archive_write_add_filter_by_name_grzip)
167 {
168 test_filter_by_name("grzip", ARCHIVE_FILTER_GRZIP, canGrzip);
169 }
170
DEFINE_TEST(test_archive_write_add_filter_by_name_gzip)171 DEFINE_TEST(test_archive_write_add_filter_by_name_gzip)
172 {
173 test_filter_by_name("gzip", ARCHIVE_FILTER_GZIP, canGzip);
174 }
175
DEFINE_TEST(test_archive_write_add_filter_by_name_lrzip)176 DEFINE_TEST(test_archive_write_add_filter_by_name_lrzip)
177 {
178 test_filter_by_name("lrzip", ARCHIVE_FILTER_LRZIP, canLrzip);
179 }
180
DEFINE_TEST(test_archive_write_add_filter_by_name_lz4)181 DEFINE_TEST(test_archive_write_add_filter_by_name_lz4)
182 {
183 test_filter_by_name("lz4", ARCHIVE_FILTER_LZ4, canLz4);
184 }
185
DEFINE_TEST(test_archive_write_add_filter_by_name_lzip)186 DEFINE_TEST(test_archive_write_add_filter_by_name_lzip)
187 {
188 test_filter_by_name("lzip", ARCHIVE_FILTER_LZIP, cannot);
189 }
190
DEFINE_TEST(test_archive_write_add_filter_by_name_lzma)191 DEFINE_TEST(test_archive_write_add_filter_by_name_lzma)
192 {
193 test_filter_by_name("lzma", ARCHIVE_FILTER_LZMA, cannot);
194 }
195
DEFINE_TEST(test_archive_write_add_filter_by_name_lzop)196 DEFINE_TEST(test_archive_write_add_filter_by_name_lzop)
197 {
198 test_filter_by_name("lzop", ARCHIVE_FILTER_LZOP, canLzop);
199 }
200
DEFINE_TEST(test_archive_write_add_filter_by_name_uuencode)201 DEFINE_TEST(test_archive_write_add_filter_by_name_uuencode)
202 {
203 test_filter_by_name("uuencode", ARCHIVE_FILTER_UU, canAlways);
204 }
205
DEFINE_TEST(test_archive_write_add_filter_by_name_xz)206 DEFINE_TEST(test_archive_write_add_filter_by_name_xz)
207 {
208 test_filter_by_name("xz", ARCHIVE_FILTER_XZ, cannot);
209 }
210