1231200Smm/*-
2231200Smm * Copyright (c) 2011 Michihiro NAKAJIMA
3231200Smm * All rights reserved.
4231200Smm *
5231200Smm * Redistribution and use in source and binary forms, with or without
6231200Smm * modification, are permitted provided that the following conditions
7231200Smm * are met:
8231200Smm * 1. Redistributions of source code must retain the above copyright
9231200Smm *    notice, this list of conditions and the following disclaimer.
10231200Smm * 2. Redistributions in binary form must reproduce the above copyright
11231200Smm *    notice, this list of conditions and the following disclaimer in the
12231200Smm *    documentation and/or other materials provided with the distribution.
13231200Smm *
14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24231200Smm */
25231200Smm#include "test.h"
26231200Smm__FBSDID("$FreeBSD");
27231200Smm
28231200Smm#include <locale.h>
29231200Smm
30231200Smm/*
31231200Smm * Test "tar:compat-2x" option that enables the string conversion of
32231200Smm * libarchive 2.x, which made incorrect UTF-8 form filenames for the
33231200Smm * pax format on some platform the wchar_t of which was not Unicode form.
34231200Smm * The option is unneeded if people have been using UTF-8 locale during
35231200Smm * making tar files(in pax format).
36231200Smm *
37231200Smm * NOTE: The sample tar file was made with bsdtar 2.x in LANG=KOI8-R on
38231200Smm * FreeBSD.
39231200Smm */
40231200Smm
41231200SmmDEFINE_TEST(test_compat_pax_libarchive_2x)
42231200Smm{
43231200Smm#if (defined(_WIN32) && !defined(__CYGWIN__)) \
44231200Smm         || defined(__STDC_ISO_10646__) || defined(__APPLE__)
45231200Smm	skipping("This test only for the platform the WCS of which is "
46231200Smm	    "not Unicode.");
47231200Smm#else
48231200Smm	struct archive *a;
49231200Smm	struct archive_entry *ae;
50231200Smm	char c;
51231200Smm	wchar_t wc;
52231200Smm	const char *refname = "test_compat_pax_libarchive_2x.tar.Z";
53231200Smm
54231200Smm	/*
55231200Smm 	* Read incorrect format UTF-8 filename in ru_RU.KOI8-R with
56231200Smm	* "tar:compat-2x" option. We should correctly
57231200Smm	* read two filenames.
58231200Smm	*/
59231200Smm	if (NULL == setlocale(LC_ALL, "ru_RU.KOI8-R")) {
60231200Smm		skipping("ru_RU.KOI8-R locale not available on this system.");
61231200Smm		return;
62231200Smm	}
63231200Smm
64231200Smm	/*
65231200Smm	 * Test if wchar_t format is the same as FreeBSD wchar_t.
66231200Smm	 */
67231200Smm	assert(-1 != wctomb(NULL, L'\0'));
68231200Smm	wc = (wchar_t)0xd0;
69231200Smm	c = 0;
70231200Smm	if (wctomb(&c, wc) != 1 || (unsigned char)c != 0xd0) {
71231200Smm		skipping("wchar_t format is different on this platform.");
72231200Smm		return;
73231200Smm	}
74231200Smm
75231200Smm	extract_reference_file(refname);
76231200Smm
77231200Smm	assert((a = archive_read_new()) != NULL);
78231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
79231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
80231200Smm	assertEqualIntA(a, ARCHIVE_OK,
81231200Smm	    archive_read_set_options(a, "tar:compat-2x"));
82231200Smm	assertEqualIntA(a, ARCHIVE_OK,
83231200Smm	    archive_read_open_filename(a, refname, 10240));
84231200Smm
85231200Smm	/* Verify regular first file. */
86231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
87231200Smm	assertEqualString("\xd0\xd2\xc9\xd7\xc5\xd4",
88231200Smm	    archive_entry_pathname(ae));
89231200Smm	assertEqualInt(6, archive_entry_size(ae));
90231200Smm
91231200Smm	/* Verify regular second file. */
92231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
93231200Smm	assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
94231200Smm	    archive_entry_pathname(ae));
95231200Smm	assertEqualInt(6, archive_entry_size(ae));
96231200Smm
97231200Smm
98231200Smm	/* End of archive. */
99231200Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
100231200Smm
101231200Smm	/* Verify archive format. */
102231200Smm	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
103231200Smm	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
104231200Smm	    archive_format(a));
105231200Smm
106231200Smm	/* Close the archive. */
107231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
108231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
109231200Smm
110231200Smm	/*
111231200Smm	 * Without "tar:compat-2x" option.
112231200Smm	 * Neither first file name nor second file name can be translated
113231200Smm	 * to KOI8-R.
114231200Smm	 */
115231200Smm	assert((a = archive_read_new()) != NULL);
116231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
117231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
118231200Smm	assertEqualIntA(a, ARCHIVE_OK,
119231200Smm	    archive_read_open_filename(a, refname, 10240));
120231200Smm
121231200Smm	/* We cannot correctly read the filename. */
122231200Smm	assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
123231200Smm	assert(strcmp("\xd0\xd2\xc9\xd7\xc5\xd4",
124231200Smm	    archive_entry_pathname(ae)) != 0);
125231200Smm	assertEqualInt(6, archive_entry_size(ae));
126231200Smm
127231200Smm	/* We cannot correctly read the filename. */
128231200Smm	assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
129231200Smm	assert(strcmp("\xf0\xf2\xe9\xf7\xe5\xf4",
130231200Smm	    archive_entry_pathname(ae)) != 0);
131231200Smm	assertEqualInt(6, archive_entry_size(ae));
132231200Smm
133231200Smm
134231200Smm	/* End of archive. */
135231200Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
136231200Smm
137231200Smm	/* Verify archive format. */
138231200Smm	assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
139231200Smm	assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
140231200Smm	    archive_format(a));
141231200Smm
142231200Smm	/* Close the archive. */
143231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
144231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
145231200Smm#endif
146231200Smm}
147