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