2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
31 * Pax interchange is supposed to encode filenames into
32 * UTF-8. Of course, that's not always possible. This
33 * test is intended to verify that filenames always get
34 * stored and restored correctly, regardless of the encodings.
38 * Read a manually-created archive that has filenames that are
39 * stored in binary instead of UTF-8 and verify that we get
40 * the right filename returned and that we get a warning only
41 * if the header isn't marked as binary.
44 test_pax_filename_encoding_1(void)
46 static const char testname[] = "test_pax_filename_encoding.tar";
48 * \314\214 is a valid 2-byte UTF-8 sequence.
49 * \374 is invalid in UTF-8.
51 char filename[] = "abc\314\214mno\374xyz";
53 struct archive_entry *entry;
56 * Read an archive that has non-UTF8 pax filenames in it.
58 extract_reference_file(testname);
59 a = archive_read_new();
60 assertEqualInt(ARCHIVE_OK, archive_read_support_format_tar(a));
61 assertEqualInt(ARCHIVE_OK, archive_read_support_compression_all(a));
62 assertEqualInt(ARCHIVE_OK,
63 archive_read_open_filename(a, testname, 10240));
65 * First entry in this test archive has an invalid UTF-8 sequence
66 * in it, but the header is not marked as hdrcharset=BINARY, so that
69 failure("Invalid UTF8 in a pax archive pathname should cause a warning");
70 assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry));
71 assertEqualString(filename, archive_entry_pathname(entry));
73 * Second entry is identical except that it does have
74 * hdrcharset=BINARY, so no warning should be generated.
76 failure("A pathname with hdrcharset=BINARY can have invalid UTF8\n"
77 " characters in it without generating a warning");
78 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
79 assertEqualString(filename, archive_entry_pathname(entry));
80 archive_read_finish(a);
84 * Set the locale and write a pathname containing invalid characters.
85 * This should work; the underlying implementation should automatically
86 * fall back to storing the pathname in binary.
89 test_pax_filename_encoding_2(void)
91 char filename[] = "abc\314\214mno\374xyz";
93 struct archive_entry *entry;
95 char longname[] = "abc\314\214mno\374xyz"
96 "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz"
97 "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz"
98 "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz"
99 "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz"
100 "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz"
101 "/abc\314\214mno\374xyz/abcdefghijklmnopqrstuvwxyz"
106 * We need a starting locale which has invalid sequences.
107 * de_DE.UTF-8 seems to be commonly supported.
109 /* If it doesn't exist, just warn and return. */
110 if (NULL == setlocale(LC_ALL, LOCALE_DE)) {
111 skipping("invalid encoding tests require a suitable locale;"
112 " %s not available on this system", LOCALE_DE);
116 assert((a = archive_write_new()) != NULL);
117 assertEqualIntA(a, 0, archive_write_set_format_pax(a));
118 assertEqualIntA(a, 0, archive_write_set_compression_none(a));
119 assertEqualIntA(a, 0, archive_write_set_bytes_per_block(a, 0));
121 archive_write_open_memory(a, buff, sizeof(buff), &used));
123 assert((entry = archive_entry_new()) != NULL);
124 /* Set pathname, gname, uname, hardlink to nonconvertible values. */
125 archive_entry_copy_pathname(entry, filename);
126 archive_entry_copy_gname(entry, filename);
127 archive_entry_copy_uname(entry, filename);
128 archive_entry_copy_hardlink(entry, filename);
129 archive_entry_set_filetype(entry, AE_IFREG);
130 failure("This should generate a warning for nonconvertible names.");
131 assertEqualInt(ARCHIVE_WARN, archive_write_header(a, entry));
132 archive_entry_free(entry);
134 assert((entry = archive_entry_new()) != NULL);
135 /* Set path, gname, uname, and symlink to nonconvertible values. */
136 archive_entry_copy_pathname(entry, filename);
137 archive_entry_copy_gname(entry, filename);
138 archive_entry_copy_uname(entry, filename);
139 archive_entry_copy_symlink(entry, filename);
140 archive_entry_set_filetype(entry, AE_IFLNK);
141 failure("This should generate a warning for nonconvertible names.");
142 assertEqualInt(ARCHIVE_WARN, archive_write_header(a, entry));
143 archive_entry_free(entry);
145 assert((entry = archive_entry_new()) != NULL);
146 /* Set pathname to a very long nonconvertible value. */
147 archive_entry_copy_pathname(entry, longname);
148 archive_entry_set_filetype(entry, AE_IFREG);
149 failure("This should generate a warning for nonconvertible names.");
150 assertEqualInt(ARCHIVE_WARN, archive_write_header(a, entry));
151 archive_entry_free(entry);
153 assertEqualInt(0, archive_write_close(a));
154 #if ARCHIVE_VERSION_NUMBER < 2000000
155 archive_write_finish(a);
157 assertEqualInt(0, archive_write_finish(a));
161 * Now read the entries back.
164 assert((a = archive_read_new()) != NULL);
165 assertEqualInt(0, archive_read_support_format_tar(a));
166 assertEqualInt(0, archive_read_open_memory(a, buff, used));
168 assertEqualInt(0, archive_read_next_header(a, &entry));
169 assertEqualString(filename, archive_entry_pathname(entry));
170 assertEqualString(filename, archive_entry_gname(entry));
171 assertEqualString(filename, archive_entry_uname(entry));
172 assertEqualString(filename, archive_entry_hardlink(entry));
174 assertEqualInt(0, archive_read_next_header(a, &entry));
175 assertEqualString(filename, archive_entry_pathname(entry));
176 assertEqualString(filename, archive_entry_gname(entry));
177 assertEqualString(filename, archive_entry_uname(entry));
178 assertEqualString(filename, archive_entry_symlink(entry));
180 assertEqualInt(0, archive_read_next_header(a, &entry));
181 assertEqualString(longname, archive_entry_pathname(entry));
183 assertEqualInt(0, archive_read_close(a));
184 #if ARCHIVE_VERSION_NUMBER < 2000000
185 archive_read_finish(a);
187 assertEqualInt(0, archive_read_finish(a));
192 * Create an entry starting from a wide-character Unicode pathname,
193 * read it back into "C" locale, which doesn't support the name.
194 * TODO: Figure out the "right" behavior here.
197 test_pax_filename_encoding_3(void)
199 wchar_t badname[] = L"xxxAyyyBzzz";
200 const char badname_utf8[] = "xxx\xE1\x88\xB4yyy\xE5\x99\xB8zzz";
202 struct archive_entry *entry;
209 /* If it doesn't exist, just warn and return. */
210 if (NULL == setlocale(LC_ALL, "C")) {
211 skipping("Can't set \"C\" locale, so can't exercise "
212 "certain character-conversion failures");
216 /* If wctomb is broken, warn and return. */
217 if (wctomb(buff, 0x1234) > 0) {
218 skipping("Cannot test conversion failures because \"C\" "
219 "locale on this system has no invalid characters.");
223 /* If wctomb is broken, warn and return. */
224 if (wctomb(buff, 0x1234) > 0) {
225 skipping("Cannot test conversion failures because \"C\" "
226 "locale on this system has no invalid characters.");
230 assert((a = archive_write_new()) != NULL);
231 assertEqualIntA(a, 0, archive_write_set_format_pax(a));
232 assertEqualIntA(a, 0, archive_write_set_compression_none(a));
233 assertEqualIntA(a, 0, archive_write_set_bytes_per_block(a, 0));
235 archive_write_open_memory(a, buff, sizeof(buff), &used));
237 assert((entry = archive_entry_new()) != NULL);
238 /* Set pathname to non-convertible wide value. */
239 archive_entry_copy_pathname_w(entry, badname);
240 archive_entry_set_filetype(entry, AE_IFREG);
241 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
242 archive_entry_free(entry);
244 assert((entry = archive_entry_new()) != NULL);
245 archive_entry_copy_pathname_w(entry, L"abc");
246 /* Set gname to non-convertible wide value. */
247 archive_entry_copy_gname_w(entry, badname);
248 archive_entry_set_filetype(entry, AE_IFREG);
249 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
250 archive_entry_free(entry);
252 assert((entry = archive_entry_new()) != NULL);
253 archive_entry_copy_pathname_w(entry, L"abc");
254 /* Set uname to non-convertible wide value. */
255 archive_entry_copy_uname_w(entry, badname);
256 archive_entry_set_filetype(entry, AE_IFREG);
257 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
258 archive_entry_free(entry);
260 assert((entry = archive_entry_new()) != NULL);
261 archive_entry_copy_pathname_w(entry, L"abc");
262 /* Set hardlink to non-convertible wide value. */
263 archive_entry_copy_hardlink_w(entry, badname);
264 archive_entry_set_filetype(entry, AE_IFREG);
265 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
266 archive_entry_free(entry);
268 assert((entry = archive_entry_new()) != NULL);
269 archive_entry_copy_pathname_w(entry, L"abc");
270 /* Set symlink to non-convertible wide value. */
271 archive_entry_copy_symlink_w(entry, badname);
272 archive_entry_set_filetype(entry, AE_IFLNK);
273 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
274 archive_entry_free(entry);
276 assertEqualInt(0, archive_write_close(a));
277 #if ARCHIVE_VERSION_NUMBER < 2000000
278 archive_write_finish(a);
280 assertEqualInt(0, archive_write_finish(a));
284 * Now read the entries back.
287 assert((a = archive_read_new()) != NULL);
288 assertEqualInt(0, archive_read_support_format_tar(a));
289 assertEqualInt(0, archive_read_open_memory(a, buff, used));
291 failure("A non-convertible pathname should cause a warning.");
292 assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry));
293 assertEqualWString(badname, archive_entry_pathname_w(entry));
294 failure("If native locale can't convert, we should get UTF-8 back.");
295 assertEqualString(badname_utf8, archive_entry_pathname(entry));
297 failure("A non-convertible gname should cause a warning.");
298 assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry));
299 assertEqualWString(badname, archive_entry_gname_w(entry));
300 failure("If native locale can't convert, we should get UTF-8 back.");
301 assertEqualString(badname_utf8, archive_entry_gname(entry));
303 failure("A non-convertible uname should cause a warning.");
304 assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry));
305 assertEqualWString(badname, archive_entry_uname_w(entry));
306 failure("If native locale can't convert, we should get UTF-8 back.");
307 assertEqualString(badname_utf8, archive_entry_uname(entry));
309 failure("A non-convertible hardlink should cause a warning.");
310 assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry));
311 assertEqualWString(badname, archive_entry_hardlink_w(entry));
312 failure("If native locale can't convert, we should get UTF-8 back.");
313 assertEqualString(badname_utf8, archive_entry_hardlink(entry));
315 failure("A non-convertible symlink should cause a warning.");
316 assertEqualInt(ARCHIVE_WARN, archive_read_next_header(a, &entry));
317 assertEqualWString(badname, archive_entry_symlink_w(entry));
318 assertEqualWString(NULL, archive_entry_hardlink_w(entry));
319 failure("If native locale can't convert, we should get UTF-8 back.");
320 assertEqualString(badname_utf8, archive_entry_symlink(entry));
322 assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &entry));
324 assertEqualInt(0, archive_read_close(a));
325 #if ARCHIVE_VERSION_NUMBER < 2000000
326 archive_read_finish(a);
328 assertEqualInt(0, archive_read_finish(a));
332 DEFINE_TEST(test_pax_filename_encoding)
334 test_pax_filename_encoding_1();
335 test_pax_filename_encoding_2();
336 test_pax_filename_encoding_3();