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