2 static char *rcsid = "Id: checker.tsy,v 1.1 2003/06/04 00:26:51 marka Exp ";
6 * Copyright (c) 2002 Japan Network Information Center.
9 * By using this file, you agree to the terms and conditions set forth bellow.
11 * LICENSE TERMS AND CONDITIONS
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.
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.
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.
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."
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
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.
52 #include <idn/checker.h>
57 #define SIZEOFUCS4(x) (sizeof(x) / sizeof(unsigned long))
59 #define UCS4_NAME_STR "U+304C" /* hiragana letter ga */
60 #define UCS4_NAME 0x304C
65 #define CONF_FILENAME "test.map"
67 #define LINEBUF_SIZE 2001
70 * Sample strings for `from' argument of normalize(),
71 * and its expected outputs.
73 const unsigned long from[4] = {
75 0x00A0, /* no-break space: prohibited character */
76 0x0221, /* unassigned character */
81 #define FROM_UCS4NAME_OFFSET 0
82 #define FROM_PROH_OFFSET 1
83 #define FROM_UNAS_OFFSET 2
85 const unsigned long from2[4] = {
87 0x0221, /* unassigned character */
88 0x00A0, /* no-break space: prohibited character */
92 #define FROM2_UCS4NAME_OFFSET 0
93 #define FROM2_PROH_OFFSET 2
94 #define FROM2_UNAS_OFFSET 1
96 static const unsigned long bidi_from[4] = {
97 0x05BE, /* hebrew punctuation maqaf */
98 0x0041, /* latin capital letter a */
99 0xFEFC, /* arabic ligature lam with alef final form */
102 #define BIDIFROM_OFFSET 1
105 test_createproc(const char *parameter, void **ctxp)
107 return (idn_success);
111 test_destroyproc(void *ctx)
115 #define FOUNDPTR_OFFSET 2
117 test_lookupproc(void *ctx, const unsigned long *ucs4,
118 const unsigned long **found)
120 *found = ucs4 + FOUNDPTR_OFFSET;
121 return (idn_success);
124 //--------------------------------------------------------------------
125 // Setups and Teardowns.
126 //--------------------------------------------------------------------
131 // Initialize the module and create context.
134 idn_checker_t ctx = NULL;
137 r = idn_checker_initialize();
138 ASSERT_RESULT(r, idn_success);
139 r = idn_checker_create(&ctx);
140 ASSERT_RESULT(r, idn_success);
149 idn_checker_destroy(ctx);
155 // Initialize the module and create context.
158 idn_checker_t ctx = NULL;
160 const unsigned long *ptr;
162 r = idn_checker_initialize();
163 ASSERT_RESULT(r, idn_success);
164 r = idn_checker_create(&ctx);
165 ASSERT_RESULT(r, idn_success);
174 idn_checker_destroy(ctx);
180 // Initialize the module and create context.
183 idn_checker_t ctx = NULL;
184 char *names[ARRAY_SIZE];
186 const unsigned long *ptr;
188 for (i = 0; i < ARRAY_SIZE; i++) {
189 names[i] = malloc(BUF_SIZE);
190 if (names[i] == NULL) {
191 ASSERT("malloc failed\n");
195 r = idn_checker_initialize();
196 ASSERT_RESULT(r, idn_success);
197 r = idn_checker_create(&ctx);
198 ASSERT_RESULT(r, idn_success);
204 // Destroy context and free some blocks.
207 idn_checker_destroy(ctx);
208 for (i = 0; i < ARRAY_SIZE; i++) {
216 // Set log level to `fatal' to supress log messages.
220 saved_log_level = idn_log_getlevel();
221 idn_log_setlevel(idn_log_level_fatal);
227 // Restore log level.
229 idn_log_setlevel(saved_log_level);
232 //--------------------------------------------------------------------
234 //--------------------------------------------------------------------
237 // title: idn_checker_add() - boundary condition
238 // group: generic quiet
240 r = idn_checker_add(ctx, "");
241 ASSERT_RESULT(r, idn_invalid_name);
245 // title: idn_checker_add() - builtin schemes, prohibit
246 // group: generic quiet
248 sprintf(name, "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
249 r = idn_checker_add(ctx, name);
250 ASSERT_RESULT(r, idn_success);
252 sprintf(name, "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "nameprep-01");
253 r = idn_checker_add(ctx, name);
254 ASSERT_RESULT(r, idn_invalid_name);
258 // title: idn_checker_add() - builtin schemes, unassigned
259 // group: generic quiet
261 sprintf(name, "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
262 r = idn_checker_add(ctx, name);
263 ASSERT_RESULT(r, idn_success);
265 sprintf(name, "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "nameprep-01");
266 r = idn_checker_add(ctx, name);
267 ASSERT_RESULT(r, idn_invalid_name);
271 // title: idn_checker_add() - builtin schemes, bidi
272 // group: generic quiet
274 sprintf(name, "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
275 r = idn_checker_add(ctx, name);
276 ASSERT_RESULT(r, idn_success);
278 sprintf(name, "%s%s", IDN_CHECKER_BIDI_PREFIX, "nameprep-01");
279 r = idn_checker_add(ctx, name);
280 ASSERT_RESULT(r, idn_invalid_name);
284 // title: idn_checker_add() - file - boundary condition
285 // group: generic quiet
287 sprintf(name, "%sfileset:%s", IDN_CHECKER_UNASSIGNED_PREFIX, "");
288 r = idn_checker_add(ctx, name);
289 ASSERT_RESULT(r, idn_nofile);
290 sprintf(name, "%sfileset:%s", IDN_CHECKER_PROHIBIT_PREFIX, "");
291 r = idn_checker_add(ctx, name);
292 ASSERT_RESULT(r, idn_nofile);
293 sprintf(name, "%sfileset:%s", IDN_CHECKER_BIDI_PREFIX, "");
294 r = idn_checker_add(ctx, name);
295 ASSERT_RESULT(r, idn_invalid_name);
299 // title: idn_checker_add() - file - long line
300 // group: generic quiet
302 char line[LINEBUF_SIZE];
303 const char *first_entry = "304C;";
304 const char *other_entry = " 304D";
308 memcpy(line, first_entry, strlen(first_entry));
309 len = strlen(other_entry);
310 for (i = len; i < LINEBUF_SIZE - len; i += len) {
311 memcpy(line + i, other_entry, len);
315 create_conf_file(CONF_FILENAME, 0, line, NULL);
316 sprintf(name, "%sfileset:%s", IDN_CHECKER_UNASSIGNED_PREFIX,
318 r = idn_checker_add(ctx, name);
319 ASSERT_RESULT(r, idn_invalid_syntax);
320 sprintf(name, "%sfileset:%s", IDN_CHECKER_PROHIBIT_PREFIX,
322 r = idn_checker_add(ctx, name);
323 ASSERT_RESULT(r, idn_invalid_syntax);
327 // title: idn_checker_add() - file, prohibit
330 create_conf_file(CONF_FILENAME, 0,
333 sprintf(name, "%sfileset:%s", IDN_CHECKER_PROHIBIT_PREFIX,
335 r = idn_checker_add(ctx, name);
336 ASSERT_RESULT(r, idn_success);
337 r = idn_checker_lookup(ctx, from, &ptr);
338 ASSERT_RESULT(r, idn_success);
339 ASSERT_PTR(ptr, from + FROM_UCS4NAME_OFFSET);
340 r = idn_checker_lookup(ctx, from2, &ptr);
341 ASSERT_RESULT(r, idn_success);
342 ASSERT_PTR(ptr, from2 + FROM2_UCS4NAME_OFFSET);
346 // title: idn_checker_add() - file, unassigned
349 create_conf_file(CONF_FILENAME, 0,
352 sprintf(name, "%sfileset:%s", IDN_CHECKER_UNASSIGNED_PREFIX,
354 r = idn_checker_add(ctx, name);
355 ASSERT_RESULT(r, idn_success);
356 r = idn_checker_lookup(ctx, from, &ptr);
357 ASSERT_RESULT(r, idn_success);
358 ASSERT_PTR(ptr, from + FROM_UCS4NAME_OFFSET);
359 r = idn_checker_lookup(ctx, from2, &ptr);
360 ASSERT_RESULT(r, idn_success);
361 ASSERT_PTR(ptr, from2 + FROM2_UCS4NAME_OFFSET);
365 // title: idn_checker_add() - file, bidi
366 // group: lookup quiet
368 create_conf_file(CONF_FILENAME, 0,
371 sprintf(name, "%sfileset:%s", IDN_CHECKER_BIDI_PREFIX,
373 r = idn_checker_add(ctx, name);
374 ASSERT_RESULT(r, idn_invalid_name);
378 // title: idn_checker_addall() - boundary condition - scheme name
379 // group: addall quiet
381 sprintf(names[0], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
382 sprintf(names[1], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
383 sprintf(names[2], "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
384 sprintf(names[3], "%s%s", IDN_CHECKER_BIDI_PREFIX, "");
385 r = idn_checker_addall(ctx, (const char **)names, 4);
386 ASSERT_RESULT(r, idn_invalid_name);
390 // title: idn_checker_addall() - boundary condition - nschemes = 0
391 // group: addall quiet
393 sprintf(names[0], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "nameprep-01");
394 sprintf(names[1], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "nameprep-01");
395 sprintf(names[2], "%s%s", IDN_CHECKER_BIDI_PREFIX, "");
396 sprintf(names[3], "%s%s", IDN_CHECKER_BIDI_PREFIX, "nameprep-01");
397 r = idn_checker_addall(ctx, (const char **)names, 0);
398 ASSERT_RESULT(r, idn_success);
399 r = idn_checker_lookup(ctx, from, &ptr);
400 ASSERT_RESULT(r, idn_success);
401 ASSERT_PTR(ptr, NULL);
405 // title: idn_checker_addall() - add a lot of schemes #1
408 sprintf(names[0], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
409 sprintf(names[1], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
410 sprintf(names[2], "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
411 sprintf(names[3], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
412 sprintf(names[4], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
413 sprintf(names[5], "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
414 sprintf(names[6], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
415 sprintf(names[7], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
416 sprintf(names[8], "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
418 r = idn_checker_addall(ctx, (const char **)names, 9);
419 ASSERT_RESULT(r, idn_success);
421 r = idn_checker_lookup(ctx, from, &ptr);
422 ASSERT_RESULT(r, idn_success);
423 ASSERT_PTR(ptr, from + FROM_PROH_OFFSET);
425 r = idn_checker_lookup(ctx, from2, &ptr);
426 ASSERT_RESULT(r, idn_success);
427 ASSERT_PTR(ptr, from2 + FROM2_PROH_OFFSET);
429 r = idn_checker_lookup(ctx, bidi_from, &ptr);
430 ASSERT_RESULT(r, idn_success);
431 ASSERT_PTR(ptr, bidi_from + BIDIFROM_OFFSET);
435 // title: idn_checker_addall() - add a lot of schemes #2
438 sprintf(names[0], "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
439 sprintf(names[1], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
440 sprintf(names[2], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
441 sprintf(names[3], "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
442 sprintf(names[4], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
443 sprintf(names[5], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
444 sprintf(names[6], "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
445 sprintf(names[7], "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
446 sprintf(names[8], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
448 r = idn_checker_addall(ctx, (const char **)names, 9);
449 ASSERT_RESULT(r, idn_success);
451 r = idn_checker_lookup(ctx, from, &ptr);
452 ASSERT_RESULT(r, idn_success);
453 ASSERT_PTR(ptr, from + FROM_UNAS_OFFSET);
455 r = idn_checker_lookup(ctx, from2, &ptr);
456 ASSERT_RESULT(r, idn_success);
457 ASSERT_PTR(ptr, from2 + FROM2_UNAS_OFFSET);
459 r = idn_checker_lookup(ctx, bidi_from, &ptr);
460 ASSERT_RESULT(r, idn_success);
461 ASSERT_PTR(ptr, bidi_from + BIDIFROM_OFFSET);
465 // title: idn_checker_addall() - add same scheme repetedly
470 sprintf(names[0], "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
471 for (i = 1; i < ARRAY_SIZE; i++) {
472 strcpy(names[i], names[0]);
474 r = idn_checker_addall(ctx, (const char **)names, ARRAY_SIZE);
475 ASSERT_RESULT(r, idn_success);
477 r = idn_checker_lookup(ctx, from, &ptr);
478 ASSERT_RESULT(r, idn_success);
479 ASSERT_PTR(ptr, from + FROM_PROH_OFFSET);
481 r = idn_checker_lookup(ctx, from2, &ptr);
482 ASSERT_RESULT(r, idn_success);
483 ASSERT_PTR(ptr, from2 + FROM2_PROH_OFFSET);
487 // title: idn_checker_lookup() - builtin schemes - RFC3491 prohibit
490 sprintf(name, "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
492 r = idn_checker_add(ctx, name);
493 ASSERT_RESULT(r, idn_success);
495 r = idn_checker_lookup(ctx, from, &ptr);
496 ASSERT_RESULT(r, idn_success);
497 ASSERT_PTR(ptr, from + FROM_PROH_OFFSET);
499 r = idn_checker_lookup(ctx, from2, &ptr);
500 ASSERT_RESULT(r, idn_success);
501 ASSERT_PTR(ptr, from2 + FROM2_PROH_OFFSET);
505 // title: idn_checker_lookup() - builtin schemes - RFC3491 unassigned
508 sprintf(name, "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
510 r = idn_checker_add(ctx, name);
511 ASSERT_RESULT(r, idn_success);
513 r = idn_checker_lookup(ctx, from, &ptr);
514 ASSERT_RESULT(r, idn_success);
515 ASSERT_PTR(ptr, from + FROM_UNAS_OFFSET);
517 r = idn_checker_lookup(ctx, from2, &ptr);
518 ASSERT_RESULT(r, idn_success);
519 ASSERT_PTR(ptr, from2 + FROM2_UNAS_OFFSET);
523 // title: idn_checker_lookup() - builtin schemes - RFC3491 bidi
526 sprintf(name, "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
528 r = idn_checker_add(ctx, name);
529 ASSERT_RESULT(r, idn_success);
531 r = idn_checker_lookup(ctx, bidi_from, &ptr);
532 ASSERT_RESULT(r, idn_success);
533 ASSERT_PTR(ptr, bidi_from + BIDIFROM_OFFSET);
535 r = idn_checker_lookup(ctx, from, &ptr);
536 ASSERT_RESULT(r, idn_success);
537 ASSERT_PTR(ptr, NULL);
539 r = idn_checker_lookup(ctx, from2, &ptr);
540 ASSERT_RESULT(r, idn_success);
541 ASSERT_PTR(ptr, NULL);
545 // title: idn_checker_lookup() - context without procedure
548 r = idn_checker_lookup(ctx, from, &ptr);
549 ASSERT_RESULT(r, idn_success);
550 ASSERT_PTR(ptr, NULL);
554 // title: idn_checker_lookup() - string in ascii
557 char *ascii_str = "test";
558 unsigned long ucs4_str[5];
560 r = idn_ucs4_utf8toucs4(ascii_str, ucs4_str, SIZEOFUCS4(ucs4_str));
562 sprintf(name, "%s%s", IDN_CHECKER_PROHIBIT_PREFIX, "RFC3491");
563 r = idn_checker_add(ctx, name);
564 ASSERT_RESULT(r, idn_success);
565 sprintf(name, "%s%s", IDN_CHECKER_UNASSIGNED_PREFIX, "RFC3491");
566 r = idn_checker_add(ctx, name);
567 ASSERT_RESULT(r, idn_success);
568 sprintf(name, "%s%s", IDN_CHECKER_BIDI_PREFIX, "RFC3491");
569 r = idn_checker_add(ctx, name);
570 ASSERT_RESULT(r, idn_success);
571 r = idn_checker_lookup(ctx, ucs4_str, &ptr);
572 ASSERT_RESULT(r, idn_success);
573 ASSERT_PTR(ptr, NULL);
577 // title: idn_checker_destroy(), idn_checker_incrref()
581 idn_checker_t ctx = NULL;
583 r = idn_checker_initialize();
584 ASSERT_RESULT(r, idn_success);
585 r = idn_checker_create(&ctx);
586 ASSERT_RESULT(r, idn_success);
587 idn_checker_incrref(ctx);
588 idn_checker_destroy(ctx);
589 idn_checker_destroy(ctx);
593 // title: idn_checker_register()
596 const unsigned long *ptr = NULL;
598 r = idn_checker_register("test",
602 ASSERT_RESULT(r, idn_success);
604 r = idn_checker_add(ctx, "test");
605 ASSERT_RESULT(r, idn_success);
607 r = idn_checker_lookup(ctx, from, &ptr);
608 ASSERT_RESULT(r, idn_success);
609 ASSERT_PTR(ptr, from + FOUNDPTR_OFFSET);