1#ifndef lint
2static char *rcsid = "Id: nameprep.tsy,v 1.1 2003/06/04 00:26:56 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/nameprep.h>
53#include <idn/log.h>
54#include "testutil.h"
55
56#define UCS4_NAME	0x304C	/* hiragana letter ga */
57#define BUF_SIZE	4
58/*
59 * Sample string for `from' argument for map(),
60 * and its expected outputs.
61 */
62static const unsigned long map_from[] = {
63	0x0041,		/* latin capital letter a */
64	0x0042,		/* latin capital letter b */
65	UCS4_NAME,
66	0x0000
67};
68
69static const unsigned long map_expected[] = {
70	0x0061,		/* latin small letter a */
71	0x0062,		/* latin small letter b */
72	UCS4_NAME,
73	0x0000
74};
75
76/*
77 * Sample string for `from' argument for isprohibited().
78 */
79static const unsigned long check_from[4] = {
80	UCS4_NAME,
81	0x00A0, /* no-break space: prohibited character */
82	0x0221, /* unassigned character */
83	0x0000
84};
85
86#define FROM_UCS4NAME_OFFSET	0
87#define FROM_PROH_OFFSET	1
88#define FROM_UNAS_OFFSET	2
89
90/*
91 * Sample string for `from' argument for isunassigned().
92 */
93static const unsigned long check_from2[4] = {
94	UCS4_NAME,
95	0x0221, /* unassigned character */
96	0x00A0, /* no-break space: prohibited character */
97	0x0000
98};
99
100#define FROM2_UCS4NAME_OFFSET	0
101#define FROM2_PROH_OFFSET	2
102#define FROM2_UNAS_OFFSET	1
103
104/*
105 * Sample string for `from' argument for isvalidbidi().
106 * (It is not a valid bidi label.)
107 */
108static const unsigned long bidi_from[4] = {
109	0x05BE, /* hebrew punctuation maqaf */
110	0x0041, /* latin capital letter a */
111	0xFEFC, /* arabic ligature lam with alef final form */
112	0x0000
113};
114#define BIDIFROM_OFFSET		1
115
116/*
117 * Empty string.
118 */
119static const unsigned long ucs4_nullstr[] = {
120	0x0000
121};
122
123//--------------------------------------------------------------------
124// Setups and Teardowns.
125//--------------------------------------------------------------------
126
127//# SETUP
128//	group: generic
129//--
130//	Nothing happens.
131{
132	idn_result_t r;
133	idn_nameprep_t handle = NULL;
134
135}
136
137//# SETUP
138//	group: check
139//--
140//	Initialize the module and create contexts.
141{
142	idn_result_t r;
143	idn_nameprep_t handle11 = NULL;
144
145	r = idn_nameprep_create("RFC3491", &handle11);
146	ASSERT_RESULT(r, idn_success);
147}
148
149//# TEARDOWN
150//	group: check
151//--
152//	Destroy contexts.
153{
154	if (handle11 != NULL) {
155		idn_nameprep_destroy(handle11);
156	}
157}
158
159//# SETUP
160//	group: quiet
161//--
162//	Set log level to `fatal' to supress log messages.
163{
164	int saved_log_level;
165
166	saved_log_level = idn_log_getlevel();
167	idn_log_setlevel(idn_log_level_fatal);
168}
169
170//# TEARDOWN
171//	group: quiet
172//--
173//	Restore log level.
174{
175	idn_log_setlevel(saved_log_level);
176}
177
178//--------------------------------------------------------------------
179// Testcases.
180//--------------------------------------------------------------------
181
182//# TESTCASE
183//	title: idn_nameprep_create() - boundary condition
184//	group: generic quiet
185{
186	r = idn_nameprep_create("", &handle);
187	ASSERT_RESULT(r, idn_notfound);
188}
189
190//# TESTCASE
191//	title: idn_nameprep_create() - version is NULL (current nameprep)
192//	group: generic quiet
193{
194	unsigned long to[BUF_SIZE];
195	const unsigned long *found;
196
197	r = idn_nameprep_create(NULL, &handle);
198	ASSERT_RESULT(r, idn_success);
199
200	r = idn_nameprep_map(handle, map_from, to, BUF_SIZE);
201	ASSERT_RESULT(r, idn_success);
202	ASSERT_UCS4STRING(to, map_expected);
203
204	r = idn_nameprep_isunassigned(handle, check_from, &found);
205	ASSERT_RESULT(r, idn_success);
206	ASSERT_PTR(found, check_from + FROM_UNAS_OFFSET);
207
208	r = idn_nameprep_isprohibited(handle, check_from, &found);
209	ASSERT_RESULT(r, idn_success);
210	ASSERT_PTR(found, check_from + FROM_PROH_OFFSET);
211
212	r = idn_nameprep_isvalidbidi(handle, bidi_from, &found);
213	ASSERT_RESULT(r, idn_success);
214	ASSERT_PTR(found, bidi_from + BIDIFROM_OFFSET);
215
216	idn_nameprep_destroy(handle);
217}
218
219//# TESTCASE
220//	title: idn_nameprep_create() - nameprep-01
221//	group: generic quiet
222{
223	r = idn_nameprep_create("nameprep-01", &handle);
224	ASSERT_RESULT(r, idn_notfound);
225}
226
227//# TESTCASE
228//	title: idn_nameprep_create() - RFC3491
229//	group: generic
230{
231	r = idn_nameprep_create("RFC3491", &handle);
232	ASSERT_RESULT(r, idn_success);
233	idn_nameprep_destroy(handle);
234}
235
236//# TESTCASE
237//	title: idn_nameprep_map() - boundary condition
238//	group: check
239{
240	unsigned long to[BUF_SIZE];
241
242	r = idn_nameprep_map(handle11, ucs4_nullstr, to, 0);
243	ASSERT_RESULT(r, idn_buffer_overflow);
244	r = idn_nameprep_map(handle11, ucs4_nullstr, to, 1);
245	ASSERT_RESULT(r, idn_success);
246	ASSERT_UCS4STRING(to, ucs4_nullstr);
247}
248
249//# TESTCASE
250//	title: idn_nameprep_map() - RFC3491
251//	group: check
252{
253	unsigned long to[BUF_SIZE];
254
255	r = idn_nameprep_map(handle11, map_from, to, 0);
256	ASSERT_RESULT(r, idn_buffer_overflow);
257	r = idn_nameprep_map(handle11, map_from, to, BUF_SIZE - 1);
258	ASSERT_RESULT(r, idn_buffer_overflow);
259	r = idn_nameprep_map(handle11, map_from, to, BUF_SIZE);
260	ASSERT_RESULT(r, idn_success);
261	ASSERT_UCS4STRING(to, map_expected);
262}
263
264//# TESTCASE
265//	title: idn_nameprep_isunassigned() - boundary condition
266//	group: check
267{
268	const unsigned long *found;
269
270	r = idn_nameprep_isunassigned(handle11, ucs4_nullstr, &found);
271	ASSERT_RESULT(r, idn_success);
272	ASSERT_PTR(found, NULL);
273}
274
275//# TESTCASE
276//	title: idn_nameprep_isunassigned() - RFC3491
277//	group: check
278{
279	const unsigned long *found;
280
281	r = idn_nameprep_isunassigned(handle11, check_from, &found);
282	ASSERT_RESULT(r, idn_success);
283	ASSERT_PTR(found, check_from + FROM_UNAS_OFFSET);
284
285	r = idn_nameprep_isunassigned(handle11, check_from2, &found);
286	ASSERT_RESULT(r, idn_success);
287	ASSERT_PTR(found, check_from2 + FROM2_UNAS_OFFSET);
288}
289
290//# TESTCASE
291//	title: idn_nameprep_isprohibited() - boundary condition
292//	group: check
293{
294	const unsigned long *found;
295
296	r = idn_nameprep_isprohibited(handle11, ucs4_nullstr, &found);
297	ASSERT_RESULT(r, idn_success);
298	ASSERT_PTR(found, NULL);
299}
300
301//# TESTCASE
302//	title: idn_nameprep_isprohibited() - RFC3491
303//	group: check
304{
305	const unsigned long *found;
306
307	r = idn_nameprep_isprohibited(handle11, check_from, &found);
308	ASSERT_RESULT(r, idn_success);
309	ASSERT_PTR(found, check_from + FROM_PROH_OFFSET);
310
311	r = idn_nameprep_isprohibited(handle11, check_from2, &found);
312	ASSERT_RESULT(r, idn_success);
313	ASSERT_PTR(found, check_from2 + FROM2_PROH_OFFSET);
314}
315
316//# TESTCASE
317//	title: idn_nameprep_isvalidbidi() - boundary condition
318//	group: check
319{
320	const unsigned long *found;
321
322	r = idn_nameprep_isvalidbidi(handle11, ucs4_nullstr, &found);
323	ASSERT_RESULT(r, idn_success);
324	ASSERT_PTR(found, NULL);
325}
326
327//# TESTCASE
328//	title: idn_nameprep_isvalidbidi() - RFC3491
329//	group: check
330{
331	const unsigned long *found;
332
333	r = idn_nameprep_isvalidbidi(handle11, bidi_from, &found);
334	ASSERT_RESULT(r, idn_success);
335	ASSERT_PTR(found, bidi_from + BIDIFROM_OFFSET);
336
337	r = idn_nameprep_isvalidbidi(handle11, check_from2, &found);
338	ASSERT_RESULT(r, idn_success);
339	ASSERT_PTR(found, NULL);
340}
341