test_read_format_lha_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
30static void
31test_read_format_lha_filename_CP932_eucJP(const char *refname)
32{
33	struct archive *a;
34	struct archive_entry *ae;
35
36	/*
37	 * Read LHA filename in ja_JP.eucJP.
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	/*
45	 * Create a read object only for a test that platform support
46	 * a character-set conversion because we can read a character-set
47	 * of filenames from the header of an lha archive file and so we
48	 * want to test that it works well.
49	 */
50	assert((a = archive_read_new()) != NULL);
51	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
52	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP932")) {
53		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
54		skipping("This system cannot convert character-set"
55		    " from CP932 to eucJP.");
56		return;
57	}
58	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
59
60	assert((a = archive_read_new()) != NULL);
61	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
62	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
63	assertEqualIntA(a, ARCHIVE_OK,
64	    archive_read_open_filename(a, refname, 10240));
65
66	/* Verify regular file. */
67	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
68	assertEqualString("\xB4\xC1\xBB\xFA\x2E\x74\x78\x74",
69	    archive_entry_pathname(ae));
70	assertEqualInt(8, archive_entry_size(ae));
71	assertEqualInt(archive_entry_is_encrypted(ae), 0);
72	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
73
74	/* Verify regular file. */
75	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
76	assertEqualString("\xC9\xBD\x2E\x74\x78\x74", archive_entry_pathname(ae));
77	assertEqualInt(4, archive_entry_size(ae));
78	assertEqualInt(archive_entry_is_encrypted(ae), 0);
79	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
80
81	/* End of archive. */
82	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
83
84	/* Verify archive format. */
85	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
86	assertEqualIntA(a, ARCHIVE_FORMAT_LHA, archive_format(a));
87
88	/* Close the archive. */
89	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
90	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
91}
92
93static void
94test_read_format_lha_filename_CP932_UTF8(const char *refname)
95{
96	struct archive *a;
97	struct archive_entry *ae;
98
99	/*
100	 * Read LHA filename in en_US.UTF-8.
101	 */
102	if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
103		skipping("en_US.UTF-8 locale not available on this system.");
104		return;
105	}
106	/*
107	 * Create a read object only for a test that platform support
108	 * a character-set conversion because we can read a character-set
109	 * of filenames from the header of an lha archive file and so we
110	 * want to test that it works well.
111	 */
112	assert((a = archive_read_new()) != NULL);
113	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
114	if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=CP932")) {
115		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
116		skipping("This system cannot convert character-set"
117		    " from CP932 to UTF-8.");
118		return;
119	}
120	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
121
122	assert((a = archive_read_new()) != NULL);
123	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
124	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
125	assertEqualIntA(a, ARCHIVE_OK,
126	    archive_read_open_filename(a, refname, 10240));
127
128	/* Verify regular file. */
129	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
130	assertEqualString("\xE6\xBC\xA2\xE5\xAD\x97\x2E\x74\x78\x74",
131	    archive_entry_pathname(ae));
132	assertEqualInt(8, archive_entry_size(ae));
133
134	/* Verify regular file. */
135	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
136	assertEqualString("\xE8\xA1\xA8\x2E\x74\x78\x74",
137	    archive_entry_pathname(ae));
138	assertEqualInt(4, archive_entry_size(ae));
139
140
141	/* End of archive. */
142	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
143
144	/* Verify archive format. */
145	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
146	assertEqualIntA(a, ARCHIVE_FORMAT_LHA, archive_format(a));
147
148	/* Close the archive. */
149	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
150	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
151}
152
153#if defined(_WIN32) && !defined(__CYGWIN__)
154static void
155test_read_format_lha_filename_CP932_Windows(const char *refname)
156{
157	struct archive *a;
158	struct archive_entry *ae;
159
160	/*
161	 * Read LHA filename in jpn on Windows.
162	 */
163	if (NULL == setlocale(LC_ALL, "jpn")) {
164		skipping("jpn locale not available on this system.");
165		return;
166	}
167	/*
168	 * Create a read object only for a test that platform support
169	 * a character-set conversion because we can read a character-set
170	 * of filenames from the header of an lha archive file and so we
171	 * want to test that it works well.
172	 */
173	assert((a = archive_read_new()) != NULL);
174	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
175	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
176	assertEqualIntA(a, ARCHIVE_OK,
177	    archive_read_open_filename(a, refname, 10240));
178
179	/* Verify regular file. */
180	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
181	assertEqualString("\x8A\xBF\x8E\x9A\x2E\x74\x78\x74",
182	    archive_entry_pathname(ae));
183	assertEqualInt(8, archive_entry_size(ae));
184
185	/* Verify regular file. */
186	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
187	assertEqualString("\x95\x5C\x2E\x74\x78\x74", archive_entry_pathname(ae));
188	assertEqualInt(4, archive_entry_size(ae));
189
190
191	/* End of archive. */
192	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
193
194	/* Verify archive format. */
195	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
196	assertEqualIntA(a, ARCHIVE_FORMAT_LHA, archive_format(a));
197
198	/* Close the archive. */
199	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
200	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
201}
202#else
203/* Stub */
204static void
205test_read_format_lha_filename_CP932_Windows(const char *refname)
206{
207	(void)refname; /* UNUSED */
208}
209#endif
210
211DEFINE_TEST(test_read_format_lha_filename)
212{
213	/* A sample file was created with LHA32.EXE through UNLHA.DLL. */
214	const char *refname = "test_read_format_lha_filename_cp932.lzh";
215
216	extract_reference_file(refname);
217
218	test_read_format_lha_filename_CP932_eucJP(refname);
219	test_read_format_lha_filename_CP932_UTF8(refname);
220	test_read_format_lha_filename_CP932_Windows(refname);
221}
222