test_read_format_gtar_filename.c revision 302408
1/*-
2 * Copyright (c) 2011 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#include "test.h"
26__FBSDID("$FreeBSD");
27
28#include <locale.h>
29
30DEFINE_TEST(test_read_format_gtar_filename_eucJP_UTF8)
31{
32	const char *refname = "test_read_format_gtar_filename_eucjp.tar.Z";
33	struct archive *a;
34	struct archive_entry *ae;
35
36	/*
37	 * Read eucJP filename in en_US.UTF-8 with "hdrcharset=eucJP" option.
38	 */
39	if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
40		skipping("en_US.UTF-8 locale not available on this system.");
41		return;
42	}
43	extract_reference_file(refname);
44
45	assert((a = archive_read_new()) != NULL);
46	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
47	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
48	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=eucJP")) {
49		skipping("This system cannot convert character-set"
50		    " from eucJP to UTF-8.");
51		goto cleanup;
52	}
53	assertEqualIntA(a, ARCHIVE_OK,
54	    archive_read_open_filename(a, refname, 10240));
55
56	/* Verify regular file. */
57	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
58	assertEqualString("\xe6\xbc\xa2\xe5\xad\x97.txt",
59	    archive_entry_pathname(ae));
60	assertEqualInt(8, archive_entry_size(ae));
61
62	/* Verify regular file. */
63	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
64	assertEqualString("\xe8\xa1\xa8.txt", archive_entry_pathname(ae));
65	assertEqualInt(4, archive_entry_size(ae));
66	assertEqualInt(archive_entry_is_encrypted(ae), 0);
67	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
68
69
70	/* End of archive. */
71	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
72
73	/* Verify archive format. */
74	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
75	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
76
77	/* Close the archive. */
78	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
79cleanup:
80	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
81}
82
83DEFINE_TEST(test_read_format_gtar_filename_CP866_KOI8R)
84{
85	const char *refname = "test_read_format_gtar_filename_cp866.tar.Z";
86	struct archive *a;
87	struct archive_entry *ae;
88
89	/*
90	 * Read CP866 filename in ru_RU.KOI8-R with "hdrcharset=CP866" option.
91	 */
92	if (NULL == setlocale(LC_ALL, "Russian_Russia.20866") &&
93	    NULL == setlocale(LC_ALL, "ru_RU.KOI8-R")) {
94		skipping("ru_RU.KOI8-R locale not available on this system.");
95		return;
96	}
97	extract_reference_file(refname);
98
99	assert((a = archive_read_new()) != NULL);
100	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
101	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
102	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP866")) {
103		skipping("This system cannot convert character-set"
104		    " from CP866 to KOI8-R.");
105		goto cleanup;
106	}
107	assertEqualIntA(a, ARCHIVE_OK,
108	    archive_read_open_filename(a, refname, 10240));
109
110	/* Verify regular file. */
111	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
112	assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
113	    archive_entry_pathname(ae));
114	assertEqualInt(6, archive_entry_size(ae));
115
116	/* Verify regular file. */
117	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
118	assertEqualString("\xd0\xd2\xc9\xd7\xc5\xd4",
119	    archive_entry_pathname(ae));
120	assertEqualInt(6, archive_entry_size(ae));
121
122
123	/* End of archive. */
124	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
125
126	/* Verify archive format. */
127	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
128	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
129
130	/* Close the archive. */
131	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
132cleanup:
133	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
134}
135
136DEFINE_TEST(test_read_format_gtar_filename_CP866_UTF8)
137{
138	const char *refname = "test_read_format_gtar_filename_cp866.tar.Z";
139	struct archive *a;
140	struct archive_entry *ae;
141
142	/*
143	 * Read CP866 filename in en_US.UTF-8 with "hdrcharset=CP866" option.
144	 */
145	if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
146		skipping("en_US.UTF-8 locale not available on this system.");
147		return;
148	}
149	extract_reference_file(refname);
150
151	assert((a = archive_read_new()) != NULL);
152	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
153	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
154	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP866")) {
155		skipping("This system cannot convert character-set"
156		    " from CP866 to UTF-8.");
157		goto cleanup;
158	}
159	assertEqualIntA(a, ARCHIVE_OK,
160	    archive_read_open_filename(a, refname, 10240));
161
162	/* Verify regular file. */
163	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
164	assertEqualString("\xd0\x9f\xd0\xa0\xd0\x98\xd0\x92\xd0\x95\xd0\xa2",
165	    archive_entry_pathname(ae));
166	assertEqualInt(6, archive_entry_size(ae));
167
168	/* Verify regular file. */
169	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
170	assertEqualString("\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82",
171	    archive_entry_pathname(ae));
172	assertEqualInt(6, archive_entry_size(ae));
173
174
175	/* End of archive. */
176	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
177
178	/* Verify archive format. */
179	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
180	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
181
182	/* Close the archive. */
183	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
184cleanup:
185	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
186}
187
188DEFINE_TEST(test_read_format_gtar_filename_KOI8R_CP866)
189{
190	const char *refname = "test_read_format_gtar_filename_koi8r.tar.Z";
191	struct archive *a;
192	struct archive_entry *ae;
193
194	/*
195	 * Read KOI8-R filename in ru_RU.CP866 with "hdrcharset=KOI8-R" option.
196	 */
197	if (NULL == setlocale(LC_ALL, "Russian_Russia.866") &&
198	    NULL == setlocale(LC_ALL, "ru_RU.CP866")) {
199		skipping("ru_RU.CP866 locale not available on this system.");
200		return;
201	}
202	extract_reference_file(refname);
203
204	assert((a = archive_read_new()) != NULL);
205	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
206	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
207	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
208		skipping("This system cannot convert character-set"
209		    " from KOI8-R to CP866.");
210		goto cleanup;
211	}
212	assertEqualIntA(a, ARCHIVE_OK,
213	    archive_read_open_filename(a, refname, 10240));
214
215	/* Verify regular file. */
216	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
217	assertEqualString("\xaf\xe0\xa8\xa2\xa5\xe2",
218	    archive_entry_pathname(ae));
219	assertEqualInt(6, archive_entry_size(ae));
220
221	/* Verify regular file. */
222	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
223	assertEqualString("\x8f\x90\x88\x82\x85\x92",
224	    archive_entry_pathname(ae));
225	assertEqualInt(6, archive_entry_size(ae));
226
227
228	/* End of archive. */
229	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
230
231	/* Verify archive format. */
232	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
233	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
234
235	/* Close the archive. */
236	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
237cleanup:
238	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
239}
240
241DEFINE_TEST(test_read_format_gtar_filename_KOI8R_UTF8)
242{
243	const char *refname = "test_read_format_gtar_filename_koi8r.tar.Z";
244	struct archive *a;
245	struct archive_entry *ae;
246
247	/*
248	 * Read KOI8-R filename in en_US.UTF-8 with "hdrcharset=KOI8-R" option.
249	 */
250	if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
251		skipping("en_US.UTF-8 locale not available on this system.");
252		return;
253	}
254	extract_reference_file(refname);
255
256	assert((a = archive_read_new()) != NULL);
257	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
258	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
259	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
260		skipping("This system cannot convert character-set"
261		    " from KOI8-R to UTF-8.");
262		goto cleanup;
263	}
264	assertEqualIntA(a, ARCHIVE_OK,
265	    archive_read_open_filename(a, refname, 10240));
266
267	/* Verify regular file. */
268	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
269	assertEqualString("\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82",
270	    archive_entry_pathname(ae));
271	assertEqualInt(6, archive_entry_size(ae));
272
273	/* Verify regular file. */
274	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
275	assertEqualString("\xd0\x9f\xd0\xa0\xd0\x98\xd0\x92\xd0\x95\xd0\xa2",
276	    archive_entry_pathname(ae));
277	assertEqualInt(6, archive_entry_size(ae));
278
279
280	/* End of archive. */
281	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
282
283	/* Verify archive format. */
284	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
285	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
286
287	/* Close the archive. */
288	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
289cleanup:
290	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
291}
292
293DEFINE_TEST(test_read_format_gtar_filename_eucJP_CP932)
294{
295	const char *refname = "test_read_format_gtar_filename_eucjp.tar.Z";
296	struct archive *a;
297	struct archive_entry *ae;
298
299	/*
300	 * Read eucJP filename in CP932/SJIS with "hdrcharset=eucJP" option.
301	 */
302	if (NULL == setlocale(LC_ALL, "Japanese_Japan") &&
303	    NULL == setlocale(LC_ALL, "ja_JP.SJIS")) {
304		skipping("CP932 locale not available on this system.");
305		return;
306	}
307	extract_reference_file(refname);
308
309	assert((a = archive_read_new()) != NULL);
310	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
311	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
312	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=eucJP")) {
313		skipping("This system cannot convert character-set"
314		    " from eucJP.");
315		goto cleanup;
316	}
317	assertEqualIntA(a, ARCHIVE_OK,
318	    archive_read_open_filename(a, refname, 10240));
319
320	/* Verify regular file. */
321	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
322	assertEqualString("\x8a\xbf\x8e\x9a.txt", archive_entry_pathname(ae));
323	assertEqualInt(8, archive_entry_size(ae));
324
325	/* Verify regular file. */
326	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
327	assertEqualString("\x95\x5c.txt", archive_entry_pathname(ae));
328	assertEqualInt(4, archive_entry_size(ae));
329
330
331	/* End of archive. */
332	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
333
334	/* Verify archive format. */
335	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
336	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
337
338	/* Close the archive. */
339	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
340cleanup:
341	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
342}
343
344DEFINE_TEST(test_read_format_gtar_filename_CP866_CP1251)
345{
346	const char *refname = "test_read_format_gtar_filename_cp866.tar.Z";
347	struct archive *a;
348	struct archive_entry *ae;
349
350	/*
351	 * Read CP866 filename in CP1251 with "hdrcharset=CP866" option.
352	 */
353	if (NULL == setlocale(LC_ALL, "Russian_Russia") &&
354	    NULL == setlocale(LC_ALL, "ru_RU.CP1251")) {
355		skipping("CP1251 locale not available on this system.");
356		return;
357	}
358	extract_reference_file(refname);
359
360	assert((a = archive_read_new()) != NULL);
361	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
362	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
363	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP866")) {
364		skipping("This system cannot convert character-set"
365		    " from CP866 to CP1251.");
366		goto cleanup;
367	}
368	assertEqualIntA(a, ARCHIVE_OK,
369	    archive_read_open_filename(a, refname, 10240));
370
371	/* Verify regular file. */
372	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
373	assertEqualString("\xcf\xd0\xc8\xc2\xc5\xd2",
374	    archive_entry_pathname(ae));
375	assertEqualInt(6, archive_entry_size(ae));
376
377	/* Verify regular file. */
378	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
379	assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2",
380	    archive_entry_pathname(ae));
381	assertEqualInt(6, archive_entry_size(ae));
382
383
384	/* End of archive. */
385	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
386
387	/* Verify archive format. */
388	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
389	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
390
391	/* Close the archive. */
392	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
393cleanup:
394	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
395}
396
397/*
398 * This test only for Windows platform because other archiver
399 * applications on Windows translate CP1251 filenames into CP866
400 * filenames and store it in the gtar file and so we should read
401 * it by default on Windows.
402 */
403DEFINE_TEST(test_read_format_gtar_filename_CP866_CP1251_win)
404{
405	const char *refname = "test_read_format_gtar_filename_cp866.tar.Z";
406	struct archive *a;
407	struct archive_entry *ae;
408
409	/*
410	 * Read CP866 filename in CP1251 without "hdrcharset=CP866" option.
411	 */
412	if (NULL == setlocale(LC_ALL, "Russian_Russia")) {
413		skipping("Russian_Russia locale not available on this system.");
414		return;
415	}
416	extract_reference_file(refname);
417
418	assert((a = archive_read_new()) != NULL);
419	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
420	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
421	assertEqualIntA(a, ARCHIVE_OK,
422	    archive_read_open_filename(a, refname, 10240));
423
424	/* Verify regular file. */
425	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
426	assertEqualString("\xcf\xd0\xc8\xc2\xc5\xd2",
427	    archive_entry_pathname(ae));
428	assertEqualInt(6, archive_entry_size(ae));
429
430	/* Verify regular file. */
431	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
432	assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2",
433	    archive_entry_pathname(ae));
434	assertEqualInt(6, archive_entry_size(ae));
435
436
437	/* End of archive. */
438	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
439
440	/* Verify archive format. */
441	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
442	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
443
444	/* Close the archive. */
445	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
446	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
447}
448
449DEFINE_TEST(test_read_format_gtar_filename_KOI8R_CP1251)
450{
451	const char *refname = "test_read_format_gtar_filename_koi8r.tar.Z";
452	struct archive *a;
453	struct archive_entry *ae;
454
455	/*
456	 * Read KOI8-R filename in CP1251 with "hdrcharset=KOI8-R" option.
457	 */
458	if (NULL == setlocale(LC_ALL, "Russian_Russia") &&
459	    NULL == setlocale(LC_ALL, "ru_RU.CP1251")) {
460		skipping("CP1251 locale not available on this system.");
461		return;
462	}
463	extract_reference_file(refname);
464
465	assert((a = archive_read_new()) != NULL);
466	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
467	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
468	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
469		skipping("This system cannot convert character-set"
470		    " from KOI8-R to CP1251.");
471		goto cleanup;
472	}
473	assertEqualIntA(a, ARCHIVE_OK,
474	    archive_read_open_filename(a, refname, 10240));
475
476	/* Verify regular file. */
477	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
478	assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2",
479	    archive_entry_pathname(ae));
480	assertEqualInt(6, archive_entry_size(ae));
481
482	/* Verify regular file. */
483	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
484	assertEqualString("\xcf\xd0\xc8\xc2\xc5\xd2",
485	    archive_entry_pathname(ae));
486	assertEqualInt(6, archive_entry_size(ae));
487
488
489	/* End of archive. */
490	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
491
492	/* Verify archive format. */
493	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
494	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_GNUTAR, archive_format(a));
495
496	/* Close the archive. */
497	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
498cleanup:
499	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
500}
501
502