1#ifndef lint
2static char *rcsid = "$Id: ucs4.tsy,v 1.1 2003/06/04 00:27:04 marka Exp $";
3#endif
4
5/*
6 * Copyright (c) 2002 Japan Network Information Center.
7 * All rights reserved.
8 *  
9 * By using this file, you agree to the terms and conditions set forth bellow.
10 * 
11 * 			LICENSE TERMS AND CONDITIONS 
12 * 
13 * The following License Terms and Conditions apply, unless a different
14 * license is obtained from Japan Network Information Center ("JPNIC"),
15 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
16 * Chiyoda-ku, Tokyo 101-0047, Japan.
17 * 
18 * 1. Use, Modification and Redistribution (including distribution of any
19 *    modified or derived work) in source and/or binary forms is permitted
20 *    under this License Terms and Conditions.
21 * 
22 * 2. Redistribution of source code must retain the copyright notices as they
23 *    appear in each source code file, this License Terms and Conditions.
24 * 
25 * 3. Redistribution in binary form must reproduce the Copyright Notice,
26 *    this License Terms and Conditions, in the documentation and/or other
27 *    materials provided with the distribution.  For the purposes of binary
28 *    distribution the "Copyright Notice" refers to the following language:
29 *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
30 * 
31 * 4. The name of JPNIC may not be used to endorse or promote products
32 *    derived from this Software without specific prior written approval of
33 *    JPNIC.
34 * 
35 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
36 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
39 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
43 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
44 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46 */
47
48#include <stddef.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <stdarg.h>
53#include <idn/ucs4.h>
54#include <idn/log.h>
55#include "testutil.h"
56
57/*
58 * Sample UTF8 and UCS4 strings.
59 */
60static const char *utf8_string =
61	"\x7f"				/* 0x0000007f */
62	"\xdf\xbf"			/* 0x000007ff */
63	"\xef\xbf\xbf"			/* 0x0000ffff */
64	"\xf7\xbf\xbf\xbf"		/* 0x001fffff */
65	"\xfb\xbf\xbf\xbf\xbf"		/* 0x03ffffff */
66	"\xfd\xbf\xbf\xbf\xbf\xbf";	/* 0x7fffffff */
67
68static const unsigned long ucs4_string[] = {
69	0x0000007f,
70	0x000007ff,
71	0x0000ffff,
72	0x001fffff,
73	0x03ffffff,
74	0x7fffffff,
75	0x00000000
76};
77
78//--------------------------------------------------------------------
79// Setups and Teardowns.
80//--------------------------------------------------------------------
81
82//# SETUP
83//      group: utf8-init
84{
85	unsigned long to[256];
86	size_t tolen = 256;
87	idn_result_t r;
88}
89
90//# SETUP
91//      group: ucs4-init
92{
93	char to[256];
94	size_t tolen = 256;
95	idn_result_t r;
96}
97
98//# SETUP
99//	group: quiet
100{
101	int saved_log_level;
102
103	saved_log_level = idn_log_getlevel();
104	idn_log_setlevel(idn_log_level_fatal);
105}
106
107//# TEARDOWN
108//	group: quiet
109{
110	idn_log_setlevel(saved_log_level);
111}
112
113//--------------------------------------------------------------------
114// Testcases.
115//--------------------------------------------------------------------
116
117//# TESTCASE
118//	title: call utf8toucs4()
119//	group: utf8-init
120{
121	r = idn_ucs4_utf8toucs4(utf8_string, to, tolen);
122	ASSERT_RESULT(r, idn_success);
123	ASSERT_UCS4STRING(to, ucs4_string);
124}
125
126//# TESTCASE
127//	title: call ucs4toutf8()
128//	group: ucs4-init
129{
130	r = idn_ucs4_ucs4toutf8(ucs4_string, to, tolen);
131	ASSERT_RESULT(r, idn_success);
132	ASSERT_STRING(to, utf8_string);
133}
134
135//# TESTCASE
136//	title: call utf8toucs4() with empty from
137//	group: utf8-init
138{
139	static unsigned long empty_ucs4_string[] = {0};
140
141	r = idn_ucs4_utf8toucs4("", to, tolen);
142	ASSERT_RESULT(r, idn_success);
143	ASSERT_UCS4STRING(to, empty_ucs4_string);
144}
145
146//# TESTCASE
147//	title: call ucs4toutf8() with empty from
148//	group: ucs4-init
149{
150	static unsigned long empty_ucs4_string[] = {0};
151
152	r = idn_ucs4_ucs4toutf8(empty_ucs4_string, to, tolen);
153	ASSERT_RESULT(r, idn_success);
154	ASSERT_STRING(to, "");
155}
156
157//# TESTCASE
158//	title: call utf8toucs4() with broken string
159//	group: utf8-init quiet
160{
161	/* "\xfe" as the 1st byte is out of range. */
162	r = idn_ucs4_utf8toucs4("\xfe\xbf\xbf\xbf\xbf\xbf\xbf", to, tolen);
163	ASSERT_RESULT(r, idn_invalid_encoding);
164
165	/* "\x7f" as the 2nd byte is out of range. */
166	r = idn_ucs4_utf8toucs4("\xdf\x7f", to, tolen);
167	ASSERT_RESULT(r, idn_invalid_encoding);
168
169	/* "\xc0" as the 2nd byte is out of range. */
170	r = idn_ucs4_utf8toucs4("\xdf\xc0", to, tolen);
171	ASSERT_RESULT(r, idn_invalid_encoding);
172
173	/* "\x7f" as the 3rd byte is out of range. */
174	r = idn_ucs4_utf8toucs4("\xef\xbf\x7f", to, tolen);
175	ASSERT_RESULT(r, idn_invalid_encoding);
176
177	/* "\xc0" as the 3rd byte is out of range. */
178	r = idn_ucs4_utf8toucs4("\xef\xbf\xc0", to, tolen);
179	ASSERT_RESULT(r, idn_invalid_encoding);
180
181	/* "\x7f" as the 4th byte is out of range. */
182	r = idn_ucs4_utf8toucs4("\xf7\xbf\xbf\x7f", to, tolen);
183	ASSERT_RESULT(r, idn_invalid_encoding);
184
185	/* "\xc0" as the 4th byte is out of range. */
186	r = idn_ucs4_utf8toucs4("\xf7\xbf\xbf\xc0", to, tolen);
187	ASSERT_RESULT(r, idn_invalid_encoding);
188
189	/* "\x7f" as the 5th byte is out of range. */
190	r = idn_ucs4_utf8toucs4("\xfb\xbf\xbf\xbf\x7f", to, tolen);
191	ASSERT_RESULT(r, idn_invalid_encoding);
192
193	/* "\xc0" as the 5th byte is out of range. */
194	r = idn_ucs4_utf8toucs4("\xfb\xbf\xbf\xbf\xc0", to, tolen);
195	ASSERT_RESULT(r, idn_invalid_encoding);
196
197	/* "\x7f" as the 6th byte is out of range. */
198	r = idn_ucs4_utf8toucs4("\xfd\xbf\xbf\xbf\xbf\x7f", to, tolen);
199	ASSERT_RESULT(r, idn_invalid_encoding);
200
201	/* "\xc0" as the 6th byte is out of range. */
202	r = idn_ucs4_utf8toucs4("\xfd\xbf\xbf\xbf\xbf\xc0", to, tolen);
203	ASSERT_RESULT(r, idn_invalid_encoding);
204
205	/* `from' contains surrogate pair */
206	r = idn_ucs4_utf8toucs4("\xed\xa0\x80\xed\xbf\xbf", to, tolen);
207	ASSERT_RESULT(r, idn_invalid_encoding);
208}
209
210//# TESTCASE
211//	title: call ucs4toutf8() with broken string
212//	group: ucs4-init quiet
213{
214	static unsigned long invalid_ucs4_string0[] = {0x80000000, 0};
215	static unsigned long invalid_ucs4_string1[] = {0xd800, 0xdfff, 0};
216
217	/* 0x80000000 is out of range */
218	r = idn_ucs4_ucs4toutf8(invalid_ucs4_string0, to, tolen);
219	ASSERT_RESULT(r, idn_invalid_encoding);
220
221	/* `from' contains surrogate pair */
222	r = idn_ucs4_ucs4toutf8(invalid_ucs4_string1, to, tolen);
223	ASSERT_RESULT(r, idn_invalid_encoding);
224}
225
226//# TESTCASE
227//	title: buffer overrun test for utf8toucs4()
228//	group: utf8-init
229{
230	r = idn_ucs4_utf8toucs4(utf8_string, to,
231				idn_ucs4_strlen(ucs4_string) + 1);
232	ASSERT_RESULT(r, idn_success);
233	ASSERT_UCS4STRING(to, ucs4_string);
234
235	r = idn_ucs4_utf8toucs4(utf8_string, to,
236				idn_ucs4_strlen(ucs4_string));
237	ASSERT_RESULT(r, idn_buffer_overflow);
238
239	r = idn_ucs4_utf8toucs4(utf8_string, to, 0);
240	ASSERT_RESULT(r, idn_buffer_overflow);
241}
242
243//# TESTCASE
244//	title: buffer overrun test for ucs4toutf8()
245//	group: ucs4-init
246{
247	r = idn_ucs4_ucs4toutf8(ucs4_string, to, strlen(utf8_string) + 1);
248	ASSERT_RESULT(r, idn_success);
249	ASSERT_STRING(to, utf8_string);
250
251	r = idn_ucs4_ucs4toutf8(ucs4_string, to, strlen(utf8_string));
252	ASSERT_RESULT(r, idn_buffer_overflow);
253
254	r = idn_ucs4_ucs4toutf8(ucs4_string, to, 0);
255	ASSERT_RESULT(r, idn_buffer_overflow);
256}
257
258