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
30static void
31test_read_format_cab_filename_CP932_eucJP(const char *refname)
32{
33	struct archive *a;
34	struct archive_entry *ae;
35
36	/*
37	 * Read CAB filename in ja_JP.eucJP with "hdrcharset=CP932" option.
38	 */
39	if (NULL == setlocale(LC_ALL, "ja_JP.eucJP")) {
40		skipping("ja_JP.eucJP locale not available on this system.");
41		return;
42	}
43
44	assert((a = archive_read_new()) != NULL);
45	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
46	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
47	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP932")) {
48		skipping("This system cannot convert character-set"
49		    " from CP932 to eucJP.");
50		goto cleanup;
51	}
52	assertEqualIntA(a, ARCHIVE_OK,
53	    archive_read_open_filename(a, refname, 10240));
54
55	/* Verify regular file. */
56	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
57	assertEqualString(
58	    "\xc9\xbd\xa4\xc0\xa4\xe8\x2f\xb4\xc1\xbb\xfa\x2e\x74\x78\x74",
59	    archive_entry_pathname(ae));
60	assertEqualInt(5, archive_entry_size(ae));
61	assertEqualInt(archive_entry_is_encrypted(ae), 0);
62	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
63
64	/* Verify regular file. */
65	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
66	assertEqualString(
67	    "\xc9\xbd\xa4\xc0\xa4\xe8\x2f\xb0\xec\xcd\xf7\xc9\xbd\x2e\x74\x78\x74",
68	    archive_entry_pathname(ae));
69	assertEqualInt(5, archive_entry_size(ae));
70	assertEqualInt(archive_entry_is_encrypted(ae), 0);
71	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
72
73
74	/* End of archive. */
75	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
76
77	/* Verify archive format. */
78	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
79	assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));
80
81	/* Close the archive. */
82cleanup:
83	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
84	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
85}
86
87static void
88test_read_format_cab_filename_CP932_UTF8(const char *refname)
89{
90	struct archive *a;
91	struct archive_entry *ae;
92
93	/*
94	 * Read CAB filename in en_US.UTF-8 with "hdrcharset=CP932" option.
95	 */
96	if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
97		skipping("en_US.UTF-8 locale not available on this system.");
98		return;
99	}
100
101	assert((a = archive_read_new()) != NULL);
102	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
103	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
104	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP932")) {
105		skipping("This system cannot convert character-set"
106		    " from CP932 to UTF-8.");
107		goto cleanup;
108	}
109	assertEqualIntA(a, ARCHIVE_OK,
110	    archive_read_open_filename(a, refname, 10240));
111
112	/* Verify regular file. */
113	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
114#if defined(__APPLE__)
115	/* Compare NFD string. */
116	assertEqualUTF8String(
117	    "\xe8\xa1\xa8\xe3\x81\x9f\xe3\x82\x99\xe3\x82\x88\x2f"
118	    "\xe6\xbc\xa2\xe5\xad\x97\x2e\x74\x78\x74",
119	    archive_entry_pathname(ae));
120	assertEqualInt(5, archive_entry_size(ae));
121#else
122	/* Compare NFC string. */
123	assertEqualUTF8String(
124	    "\xe8\xa1\xa8\xe3\x81\xa0\xe3\x82\x88\x2f"
125	    "\xe6\xbc\xa2\xe5\xad\x97\x2e\x74\x78\x74",
126	    archive_entry_pathname(ae));
127	assertEqualInt(5, archive_entry_size(ae));
128#endif
129	assertEqualInt(archive_entry_is_encrypted(ae), 0);
130	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
131
132	/* Verify regular file. */
133	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
134#if defined(__APPLE__)
135	/* Compare NFD string. */
136	assertEqualUTF8String(
137	    "\xe8\xa1\xa8\xe3\x81\x9f\xe3\x82\x99\xe3\x82\x88\x2f"
138	    "\xe4\xb8\x80\xe8\xa6\xa7\xe8\xa1\xa8\x2e\x74\x78\x74",
139	    archive_entry_pathname(ae));
140#else
141	/* Compare NFC string. */
142	assertEqualUTF8String(
143	    "\xe8\xa1\xa8\xe3\x81\xa0\xe3\x82\x88\x2f"
144	    "\xe4\xb8\x80\xe8\xa6\xa7\xe8\xa1\xa8\x2e\x74\x78\x74",
145	    archive_entry_pathname(ae));
146#endif
147	assertEqualInt(5, archive_entry_size(ae));
148	assertEqualInt(archive_entry_is_encrypted(ae), 0);
149	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
150
151
152	/* End of archive. */
153	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
154
155	/* Verify archive format. */
156	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
157	assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));
158
159	/* Close the archive. */
160cleanup:
161	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
162	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
163}
164
165DEFINE_TEST(test_read_format_cab_filename)
166{
167	const char *refname = "test_read_format_cab_filename_cp932.cab";
168
169	extract_reference_file(refname);
170	test_read_format_cab_filename_CP932_eucJP(refname);
171	test_read_format_cab_filename_CP932_UTF8(refname);
172}
173