2 * Copyright (c) 2011 Michihiro NAKAJIMA
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.
31 * The sample tar file was made in LANG=KOI8-R and it contains two
32 * files the charset of which are different.
33 * - the filename of first file is stored in BINARY mode.
34 * - the filename of second file is stored in UTF-8.
36 * Whenever hdrcharset option is specified, we will correctly read the
37 * filename of second file, which is stored in UTF-8 by default.
41 test_read_format_tar_filename_KOI8R_CP866(const char *refname)
44 struct archive_entry *ae;
47 * Read filename in ru_RU.CP866 with "hdrcharset=KOI8-R" option.
48 * We should correctly read two filenames.
50 if (NULL == setlocale(LC_ALL, "Russian_Russia.866") &&
51 NULL == setlocale(LC_ALL, "ru_RU.CP866")) {
52 skipping("ru_RU.CP866 locale not available on this system.");
56 /* Test if the platform can convert from UTF-8. */
57 assert((a = archive_read_new()) != NULL);
58 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_tar(a));
59 if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=UTF-8")) {
60 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
61 skipping("This system cannot convert character-set"
62 " from UTF-8 to CP866.");
65 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
67 assert((a = archive_read_new()) != NULL);
68 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
69 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
70 if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
71 skipping("This system cannot convert character-set"
72 " from KOI8-R to CP866.");
75 assertEqualIntA(a, ARCHIVE_OK,
76 archive_read_open_filename(a, refname, 10240));
78 /* Verify regular first file. */
79 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
80 assertEqualString("\x8f\x90\x88\x82\x85\x92",
81 archive_entry_pathname(ae));
82 assertEqualInt(6, archive_entry_size(ae));
84 /* Verify regular second file. */
85 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
86 assertEqualString("\xaf\xe0\xa8\xa2\xa5\xe2",
87 archive_entry_pathname(ae));
88 assertEqualInt(6, archive_entry_size(ae));
92 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
94 /* Verify archive format. */
95 assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
96 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
99 /* Close the archive. */
100 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
102 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
106 * Read filename in ru_RU.CP866 without "hdrcharset=KOI8-R" option.
107 * The filename we can properly read is only second file.
110 assert((a = archive_read_new()) != NULL);
111 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
112 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
113 assertEqualIntA(a, ARCHIVE_OK,
114 archive_read_open_filename(a, refname, 10240));
117 * Verify regular first file.
118 * The filename is not translated to CP866 because hdrcharset
119 * attribute is BINARY and there is not way to know its charset.
121 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
122 /* A filename is in KOI8-R. */
123 assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
124 archive_entry_pathname(ae));
125 assertEqualInt(6, archive_entry_size(ae));
128 * Verify regular second file.
129 * The filename is translated from UTF-8 to CP866
131 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
132 assertEqualString("\xaf\xe0\xa8\xa2\xa5\xe2",
133 archive_entry_pathname(ae));
134 assertEqualInt(6, archive_entry_size(ae));
137 /* End of archive. */
138 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
140 /* Verify archive format. */
141 assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
142 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
145 /* Close the archive. */
146 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
147 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
151 test_read_format_tar_filename_KOI8R_UTF8(const char *refname)
154 struct archive_entry *ae;
157 * Read filename in en_US.UTF-8 with "hdrcharset=KOI8-R" option.
158 * We should correctly read two filenames.
160 if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
161 skipping("en_US.UTF-8 locale not available on this system.");
165 assert((a = archive_read_new()) != NULL);
166 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
167 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
168 if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
169 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
170 skipping("This system cannot convert character-set"
171 " from KOI8-R to UTF-8.");
174 assertEqualIntA(a, ARCHIVE_OK,
175 archive_read_open_filename(a, refname, 10240));
177 /* Verify regular file. */
178 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
179 assertEqualString("\xd0\x9f\xd0\xa0\xd0\x98\xd0\x92\xd0\x95\xd0\xa2",
180 archive_entry_pathname(ae));
181 assertEqualInt(6, archive_entry_size(ae));
183 /* Verify regular file. */
184 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
185 assertEqualString("\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82",
186 archive_entry_pathname(ae));
187 assertEqualInt(6, archive_entry_size(ae));
190 /* End of archive. */
191 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
193 /* Verify archive format. */
194 assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
195 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
198 /* Close the archive. */
199 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
200 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
203 * Read filename in en_US.UTF-8 without "hdrcharset=KOI8-R" option.
204 * The filename we can properly read is only second file.
207 assert((a = archive_read_new()) != NULL);
208 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
209 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
210 assertEqualIntA(a, ARCHIVE_OK,
211 archive_read_open_filename(a, refname, 10240));
214 * Verify regular first file.
215 * The filename is not translated to UTF-8 because hdrcharset
216 * attribute is BINARY and there is not way to know its charset.
218 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
219 /* A filename is in KOI8-R. */
220 assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
221 archive_entry_pathname(ae));
222 assertEqualInt(6, archive_entry_size(ae));
225 * Verify regular second file.
227 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
228 assertEqualString("\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82",
229 archive_entry_pathname(ae));
230 assertEqualInt(6, archive_entry_size(ae));
233 /* End of archive. */
234 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
236 /* Verify archive format. */
237 assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
238 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
241 /* Close the archive. */
242 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
243 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
247 test_read_format_tar_filename_KOI8R_CP1251(const char *refname)
250 struct archive_entry *ae;
253 * Read filename in CP1251 with "hdrcharset=KOI8-R" option.
254 * We should correctly read two filenames.
256 if (NULL == setlocale(LC_ALL, "Russian_Russia") &&
257 NULL == setlocale(LC_ALL, "ru_RU.CP1251")) {
258 skipping("CP1251 locale not available on this system.");
262 /* Test if the platform can convert from UTF-8. */
263 assert((a = archive_read_new()) != NULL);
264 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_tar(a));
265 if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=UTF-8")) {
266 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
267 skipping("This system cannot convert character-set"
268 " from UTF-8 to CP1251.");
271 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
273 assert((a = archive_read_new()) != NULL);
274 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
275 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
276 if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
277 skipping("This system cannot convert character-set"
278 " from KOI8-R to CP1251.");
281 assertEqualIntA(a, ARCHIVE_OK,
282 archive_read_open_filename(a, refname, 10240));
284 /* Verify regular first file. */
285 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
286 assertEqualString("\xcf\xd0\xc8\xc2\xc5\xd2",
287 archive_entry_pathname(ae));
288 assertEqualInt(6, archive_entry_size(ae));
290 /* Verify regular second file. */
291 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
292 assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2",
293 archive_entry_pathname(ae));
294 assertEqualInt(6, archive_entry_size(ae));
297 /* End of archive. */
298 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
300 /* Verify archive format. */
301 assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
302 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
305 /* Close the archive. */
306 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
308 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
311 * Read filename in CP1251 without "hdrcharset=KOI8-R" option.
312 * The filename we can properly read is only second file.
315 assert((a = archive_read_new()) != NULL);
316 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
317 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
318 assertEqualIntA(a, ARCHIVE_OK,
319 archive_read_open_filename(a, refname, 10240));
322 * Verify regular first file.
323 * The filename is not translated to CP1251 because hdrcharset
324 * attribute is BINARY and there is not way to know its charset.
326 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
327 /* A filename is in KOI8-R. */
328 assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
329 archive_entry_pathname(ae));
330 assertEqualInt(6, archive_entry_size(ae));
333 * Verify regular second file.
335 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
336 assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2",
337 archive_entry_pathname(ae));
338 assertEqualInt(6, archive_entry_size(ae));
341 /* End of archive. */
342 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
344 /* Verify archive format. */
345 assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
346 assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
349 /* Close the archive. */
350 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
351 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
355 DEFINE_TEST(test_read_format_tar_filename)
357 const char *refname = "test_read_format_tar_filename_koi8r.tar.Z";
359 extract_reference_file(refname);
360 test_read_format_tar_filename_KOI8R_CP866(refname);
361 test_read_format_tar_filename_KOI8R_UTF8(refname);
362 test_read_format_tar_filename_KOI8R_CP1251(refname);