1#ifndef lint
2static char *rcsid = "$Id: normalizer.tsy,v 1.1 2003/06/04 00:26:57 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 <idn/normalizer.h>
53#include <idn/log.h>
54#include <idn/ucs4.h>
55
56#define BUF_SIZE	64
57#define TOBUF_SIZE	4
58#define ARRAY_SIZE	20
59#define CONF_FILENAME	"test.map"
60
61/*
62 * Sample string for `from' argument of normalize(),
63 * and its expected outputs.
64 */
65static const unsigned long from[4] = {
66	0x304B,	/* hiragana letter ka */
67	0x3099,	/* combining katakana-hiragana voiced sound mark */
68	0x32D0,	/* circled katakana a */
69	0x0000
70};
71
72static const unsigned long normalized_kc_str[3] = {
73	0x304C,	/* hiragana letter ga */
74	0x30A2,	/* katakana letter a */
75	0x0000
76};
77
78static const unsigned long normalized_c_str[3] = {
79	0x304C,	/* hiragana letter ga */
80	0x32D0,	/* circled katakana a */
81	0x0000
82};
83
84idn_result_t
85test_proc(const unsigned long *from, unsigned long *to, size_t tolen)
86{
87	if (tolen > idn_ucs4_strlen(from)) {
88		idn_ucs4_strcpy(to, from);
89	} else {
90		return (idn_buffer_overflow);
91	}
92
93	return (idn_success);
94}
95
96//--------------------------------------------------------------------
97// Setups and Teardowns.
98//--------------------------------------------------------------------
99
100//# SETUP
101//	group: noinit
102//--
103//	Do nothing
104{
105	idn_result_t r;
106	const char *name;
107}
108
109//# SETUP
110//	group: generic
111//--
112//	Initialize the module and create context.
113{
114	idn_result_t r;
115	idn_normalizer_t ctx = NULL;
116
117	r = idn_normalizer_initialize();
118	ASSERT_RESULT(r, idn_success);
119	r = idn_normalizer_create(&ctx);
120	ASSERT_RESULT(r, idn_success);
121}
122
123//# TEARDOWN
124//	group: generic
125//--
126//	Destroy context.
127{
128	idn_normalizer_destroy(ctx);
129}
130
131//# SETUP
132//	group: addall
133//--
134//	Initialize the module and create context.
135{
136	idn_result_t r;
137	idn_normalizer_t ctx = NULL;
138	char *names[ARRAY_SIZE];
139	int i;
140
141	for (i = 0; i < ARRAY_SIZE; i++) {
142		names[i] = malloc(BUF_SIZE);
143		if (names[i] == NULL) {
144			ASSERT("malloc failed\n");
145		}
146	}
147
148	strcpy(names[0], "RFC3491");
149	strcpy(names[1], "unicode-form-kc");
150	strcpy(names[2], "unicode-form-kc/3.2.0");
151	strcpy(names[3], "RFC3491");
152	strcpy(names[4], "unicode-form-kc");
153	strcpy(names[5], "unicode-form-kc/3.2.0");
154	strcpy(names[6], "RFC3491");
155	strcpy(names[7], "unicode-form-kc");
156	strcpy(names[8], "unicode-form-kc/3.2.0");
157	strcpy(names[9], "RFC3491");
158	strcpy(names[10], "unicode-form-kc");
159	strcpy(names[11], "unicode-form-kc/3.2.0");
160	strcpy(names[12], "RFC3491");
161	strcpy(names[13], "unicode-form-kc");
162	strcpy(names[14], "unicode-form-kc/3.2.0");
163	strcpy(names[15], "RFC3491");
164	strcpy(names[16], "unicode-form-kc");
165	strcpy(names[17], "unicode-form-kc/3.2.0");
166	strcpy(names[18], "RFC3491");
167	strcpy(names[19], "unicode-form-kc");
168
169	r = idn_normalizer_initialize();
170	ASSERT_RESULT(r, idn_success);
171	r = idn_normalizer_create(&ctx);
172	ASSERT_RESULT(r, idn_success);
173}
174
175//# TEARDOWN
176//	group: addall
177//--
178//	Destroy context.
179{
180	idn_normalizer_destroy(ctx);
181	for (i = 0; i < ARRAY_SIZE; i++) {
182		free(names[i]);
183	}
184}
185
186//# SETUP
187//	group: quiet
188//--
189//	Set log level to `fatal' to supress log messages.
190{
191	int saved_log_level;
192
193	saved_log_level = idn_log_getlevel();
194	idn_log_setlevel(idn_log_level_fatal);
195}
196
197//# TEARDOWN
198//	group: quiet
199//--
200//	Restore log level.
201{
202	idn_log_setlevel(saved_log_level);
203}
204
205//--------------------------------------------------------------------
206// Testcases.
207//--------------------------------------------------------------------
208
209//# TESTCASE
210//	title: idn_normalizer_add() - boundary condition
211//	group: generic quiet
212{
213	r = idn_normalizer_add(ctx, "");
214	ASSERT_RESULT(r, idn_invalid_name);
215}
216
217//# TESTCASE
218//	title: idn_normalizer_add() - builtin schemes
219//	group: generic quiet
220{
221	r = idn_normalizer_add(ctx, "RFC3491");
222	ASSERT_RESULT(r, idn_success);
223	r = idn_normalizer_add(ctx, "unicode-form-kc");
224	ASSERT_RESULT(r, idn_success);
225	r = idn_normalizer_add(ctx, "unicode-form-kc/3.2.0");
226	ASSERT_RESULT(r, idn_success);
227
228	r = idn_normalizer_add(ctx, "nameprep-01");
229	ASSERT_RESULT(r, idn_invalid_name);
230}
231
232//# TESTCASE
233//	title: idn_normalizer_addall() - boundary condition
234//	group: addall quiet
235{
236	strcpy(names[3], "");
237	r = idn_normalizer_addall(ctx, (const char **)names, ARRAY_SIZE);
238	ASSERT_RESULT(r, idn_invalid_name);
239}
240
241//# TESTCASE
242//	title: idn_normalizer_addall() - nschemes is 0
243//	group: addall quiet
244{
245	r = idn_normalizer_addall(ctx, (const char **)names, 0);
246	ASSERT_RESULT(r, idn_success);
247}
248
249//# TESTCASE
250//	title: idn_normalizer_addall() - add a lot of schemes
251//	group: addall quiet
252{
253	unsigned long to[TOBUF_SIZE];
254
255	r = idn_normalizer_addall(ctx, (const char **)names, ARRAY_SIZE);
256	ASSERT_RESULT(r, idn_success);
257	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
258	ASSERT_RESULT(r, idn_success);
259	ASSERT_UCS4STRING(to, normalized_kc_str);
260}
261
262//# TESTCASE
263//	title: idn_normalizer_addall() - add same scheme repetedly
264//	group: addall quiet
265{
266	int i;
267	unsigned long to[TOBUF_SIZE];
268
269	for (i = 0; i < ARRAY_SIZE; i++) {
270		strcpy(names[i], "RFC3491");
271	}
272	r = idn_normalizer_addall(ctx, (const char **)names, ARRAY_SIZE);
273	ASSERT_RESULT(r, idn_success);
274	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
275	ASSERT_RESULT(r, idn_success);
276	ASSERT_UCS4STRING(to, normalized_kc_str);
277}
278
279//# TESTCASE
280//	title: idn_normalizer_normalize() - schemes check - RFC3491
281//	group: generic quiet
282{
283	unsigned long to[TOBUF_SIZE];
284
285	r = idn_normalizer_add(ctx, "RFC3491");
286	ASSERT_RESULT(r, idn_success);
287	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
288	ASSERT_RESULT(r, idn_success);
289	ASSERT_UCS4STRING(to, normalized_kc_str);
290}
291
292//# TESTCASE
293//	title: idn_normalizer_normalize() - schemes check - unicode-form-kc/3.2.0
294//	group: generic quiet
295{
296	unsigned long to[TOBUF_SIZE];
297
298	r = idn_normalizer_add(ctx, "unicode-form-kc/3.2.0");
299	ASSERT_RESULT(r, idn_success);
300	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
301	ASSERT_RESULT(r, idn_success);
302	ASSERT_UCS4STRING(to, normalized_kc_str);
303}
304
305//# TESTCASE
306//	title: idn_normalizer_normalize() - context without procedure
307//	group: generic quiet
308{
309	unsigned long to[TOBUF_SIZE];
310
311	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
312	ASSERT_RESULT(r, idn_success);
313	ASSERT_UCS4STRING(to, from);
314}
315
316//# TESTCASE
317//	title: idn_normalizer_destroy(), idn_normalizer_incrref()
318//	group:
319{
320	idn_result_t r;
321	idn_normalizer_t ctx = NULL;
322
323	r = idn_normalizer_initialize();
324	ASSERT_RESULT(r, idn_success);
325	r = idn_normalizer_create(&ctx);
326	ASSERT_RESULT(r, idn_success);
327	idn_normalizer_incrref(ctx);
328	idn_normalizer_destroy(ctx);
329	idn_normalizer_destroy(ctx);
330}
331
332//# TESTCASE
333//	title: idn_normalizer_register()
334//	group: generic quiet
335{
336	unsigned long to[TOBUF_SIZE];
337
338	r = idn_normalizer_register("test", test_proc);
339	ASSERT_RESULT(r, idn_success);
340	r = idn_normalizer_add(ctx, "test");
341	ASSERT_RESULT(r, idn_success);
342
343	r = idn_normalizer_normalize(ctx, from, to, TOBUF_SIZE);
344	ASSERT_RESULT(r, idn_success);
345	ASSERT_UCS4STRING(to, from);
346}
347