2 static char *rcsid = "Id: mapselector.tsy,v 1.1 2003/06/04 00:26:55 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/mapselector.h>
58 #define CONF_FILENAME "test.conf"
59 #define MAP_FILENAME "test.map"
64 static const char *utf8_tlds_jp[] = {"jp", ".jp", "JP", ".JP"};
65 static const char *utf8_tlds_tw[] = {"tw", ".tw", "TW", ".TW"};
67 static const unsigned long ucs4_tlds_jp[][4] = {{'j', 'p', '\0', '\0'},
68 {'.', 'j', 'p', '\0'},
69 {'J', 'P', '\0', '\0'},
70 {'.', 'J', 'P', '\0'}};
72 static const unsigned long ucs4_tlds_tw[][4] = {{'t', 'w', '\0', '\0'},
73 {'.', 't', 'w', '\0'},
74 {'T', 'W', '\0', '\0'},
75 {'.', 'T', 'W', '\0'}};
77 /* How many elements in `utf8_tlds_{jp|tw}[]' and `ucs4_tlds_{jp|tw}[]'. */
78 #define TLD_NVARIANTS 4
81 * Sample input string for mapping. (UCS4)
83 static const unsigned long in_string[] = {0x00C0, 0x2212, 0};
86 * Sample mapping results of IN_STRING.
88 * OUT_STRING_FILEMAP is the result of file-mapping (U+2212 -> U+002D).
89 * OUT_STRING_NAMEPREP is the result of the latest nameprep
90 * OUT_STRING_BOTH is the result of both file-mapping and nameprep.
92 static const unsigned long out_string_filemap[] = {0x00C0, 0x002D, 0};
93 static const unsigned long out_string_nameprep[] = {0x00E0, 0x2212, 0};
94 static const unsigned long out_string_both[] = {0x00E0, 0x002D, 0};
96 #define MAP_FILENAME "test.map"
98 //--------------------------------------------------------------------
99 // Setups and Teardowns.
100 //--------------------------------------------------------------------
103 // group: generic-init
106 idn_mapselector_t ctxs[TLD_NVARIANTS];
107 unsigned long to[256];
111 for (i = 0; i < TLD_NVARIANTS; i++)
115 r = idn_mapselector_initialize();
116 ASSERT_RESULT(r, idn_success);
120 for (i = 0; i < TLD_NVARIANTS; i++) {
121 r = idn_mapselector_create(&ctxs[i]);
122 ASSERT_RESULT(r, idn_success);
128 // group: generic-init
133 for (i = 0; i < TLD_NVARIANTS; i++) {
135 idn_mapselector_destroy(ctxs[i]);
136 remove(CONF_FILENAME);
146 saved_log_level = idn_log_getlevel();
147 idn_log_setlevel(idn_log_level_fatal);
153 idn_log_setlevel(saved_log_level);
157 // group: generic-filemap
159 create_conf_file(MAP_FILENAME, 0,
165 // group: generic-filemap
167 remove(MAP_FILENAME);
170 //--------------------------------------------------------------------
172 //--------------------------------------------------------------------
175 // title: call initialize() twice.
180 r = idn_mapselector_initialize();
181 ASSERT_RESULT(r, idn_success);
183 r = idn_mapselector_initialize();
184 ASSERT_RESULT(r, idn_success);
188 // title: call create()
191 idn_mapselector_t ctx;
193 r = idn_mapselector_initialize();
194 ASSERT_RESULT(r, idn_success);
196 r = idn_mapselector_create(&ctx);
197 ASSERT_RESULT(r, idn_success);
199 idn_mapselector_destroy(ctx);
203 // title: call add(filemap) and map()
204 // group: generic-init generic-filemap
208 for (i = 0; i < TLD_NVARIANTS; i++) {
209 r = idn_mapselector_add(ctxs[i], utf8_tlds_jp[i],
210 "filemap:" MAP_FILENAME);
211 ASSERT_RESULT(r, idn_success);
214 for (i = 0; i < TLD_NVARIANTS; i++) {
215 for (j = 0; j < TLD_NVARIANTS; j++) {
216 r = idn_mapselector_map(ctxs[i], in_string,
218 sizeof(to) / sizeof(*to));
219 ASSERT_RESULT(r, idn_success);
220 ASSERT_UCS4STRING(to, out_string_filemap);
222 r = idn_mapselector_map(ctxs[i], in_string,
224 sizeof(to) / sizeof(*to));
225 ASSERT_RESULT(r, idn_success);
226 ASSERT_UCS4STRING(to, in_string);
232 // title: call add(nameprep) and map()
233 // group: generic-init generic-filemap
237 for (i = 0; i < TLD_NVARIANTS; i++) {
238 r = idn_mapselector_add(ctxs[i], utf8_tlds_jp[i],
239 IDN_NAMEPREP_CURRENT);
240 ASSERT_RESULT(r, idn_success);
243 for (i = 0; i < TLD_NVARIANTS; i++) {
244 for (j = 0; j < TLD_NVARIANTS; j++) {
245 r = idn_mapselector_map(ctxs[i], in_string,
247 sizeof(to) / sizeof(*to));
248 ASSERT_RESULT(r, idn_success);
249 ASSERT_UCS4STRING(to, out_string_nameprep);
251 r = idn_mapselector_map(ctxs[i], in_string,
253 sizeof(to) / sizeof(*to));
254 ASSERT_RESULT(r, idn_success);
255 ASSERT_UCS4STRING(to, in_string);
261 // title: call add(filemap) and map2()
262 // group: generic-init generic-filemap
266 for (i = 0; i < TLD_NVARIANTS; i++) {
267 r = idn_mapselector_add(ctxs[i], utf8_tlds_jp[i],
268 "filemap:" MAP_FILENAME);
269 ASSERT_RESULT(r, idn_success);
272 for (i = 0; i < TLD_NVARIANTS; i++) {
273 for (j = 0; j < TLD_NVARIANTS; j++) {
274 r = idn_mapselector_map2(ctxs[i], in_string,
276 sizeof(to) / sizeof(*to));
277 ASSERT_RESULT(r, idn_success);
278 ASSERT_UCS4STRING(to, out_string_filemap);
280 r = idn_mapselector_map2(ctxs[i], in_string,
282 sizeof(to) / sizeof(*to));
283 ASSERT_RESULT(r, idn_success);
284 ASSERT_UCS4STRING(to, in_string);
290 // title: call add(nameprep) and map2()
291 // group: generic-init generic-filemap
295 for (i = 0; i < TLD_NVARIANTS; i++) {
296 r = idn_mapselector_add(ctxs[i], utf8_tlds_jp[i],
297 IDN_NAMEPREP_CURRENT);
298 ASSERT_RESULT(r, idn_success);
301 for (i = 0; i < TLD_NVARIANTS; i++) {
302 for (j = 0; j < TLD_NVARIANTS; j++) {
303 r = idn_mapselector_map2(ctxs[i], in_string,
305 sizeof(to) / sizeof(*to));
306 ASSERT_RESULT(r, idn_success);
307 ASSERT_UCS4STRING(to, out_string_nameprep);
309 r = idn_mapselector_map2(ctxs[i], in_string,
311 sizeof(to) / sizeof(*to));
312 ASSERT_RESULT(r, idn_success);
313 ASSERT_UCS4STRING(to, in_string);
319 // title: call add(filemap) and map()
320 // group: generic-init generic-filemap
324 for (i = 0; i < TLD_NVARIANTS; i++) {
325 r = idn_mapselector_add(ctxs[i], utf8_tlds_jp[i],
326 "filemap:" MAP_FILENAME);
327 ASSERT_RESULT(r, idn_success);
330 for (i = 0; i < TLD_NVARIANTS; i++) {
331 for (j = 0; j < TLD_NVARIANTS; j++) {
332 r = idn_mapselector_map(ctxs[i], in_string,
334 sizeof(to) / sizeof(*to));
335 ASSERT_RESULT(r, idn_success);
336 ASSERT_UCS4STRING(to, out_string_filemap);
338 r = idn_mapselector_map(ctxs[i], in_string,
340 sizeof(to) / sizeof(*to));
341 ASSERT_RESULT(r, idn_success);
342 ASSERT_UCS4STRING(to, in_string);
348 // title: call add(nameprep) and map()
349 // group: generic-init generic-filemap
353 for (i = 0; i < TLD_NVARIANTS; i++) {
354 r = idn_mapselector_add(ctxs[i], utf8_tlds_jp[i],
355 IDN_NAMEPREP_CURRENT);
356 ASSERT_RESULT(r, idn_success);
359 for (i = 0; i < TLD_NVARIANTS; i++) {
360 for (j = 0; j < TLD_NVARIANTS; j++) {
361 r = idn_mapselector_map(ctxs[i], in_string,
363 sizeof(to) / sizeof(*to));
364 ASSERT_RESULT(r, idn_success);
365 ASSERT_UCS4STRING(to, out_string_nameprep);
367 r = idn_mapselector_map(ctxs[i], in_string,
369 sizeof(to) / sizeof(*to));
370 ASSERT_RESULT(r, idn_success);
371 ASSERT_UCS4STRING(to, in_string);
377 // title: call addall()
378 // group: generic-init generic-filemap
380 static const char *names[] = {
381 "filemap:" MAP_FILENAME,
386 for (i = 0; i < TLD_NVARIANTS; i++) {
387 r = idn_mapselector_addall(ctxs[i], utf8_tlds_jp[i], names,
388 sizeof(names) / sizeof(*names));
389 ASSERT_RESULT(r, idn_success);
392 for (i = 0; i < TLD_NVARIANTS; i++) {
393 for (j = 0; j < TLD_NVARIANTS; j++) {
394 r = idn_mapselector_map2(ctxs[i], in_string,
396 sizeof(to) / sizeof(*to));
397 ASSERT_RESULT(r, idn_success);
398 ASSERT_UCS4STRING(to, out_string_both);
400 r = idn_mapselector_map2(ctxs[i], in_string,
402 sizeof(to) / sizeof(*to));
403 ASSERT_RESULT(r, idn_success);
404 ASSERT_UCS4STRING(to, in_string);
410 // title: call addall() with nnames=0
411 // group: generic-init
413 static const char *names[] = {IDN_NAMEPREP_CURRENT};
415 r = idn_mapselector_addall(ctxs[0], ".", names, 0);
416 ASSERT_RESULT(r, idn_success);
420 // title: call add() with empty tld
421 // group: generic-init
423 r = idn_mapselector_add(ctxs[0], "", IDN_NAMEPREP_CURRENT);
424 ASSERT_RESULT(r, idn_success);
428 // title: call addall() with empty tld
429 // group: generic-init
431 static const char *names[] = {IDN_NAMEPREP_CURRENT};
433 r = idn_mapselector_addall(ctxs[0], "", names, 1);
434 ASSERT_RESULT(r, idn_success);
438 // title: call add() with too long tld
439 // group: generic-init quiet
441 r = idn_mapselector_add(ctxs[0],
442 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
443 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
445 IDN_NAMEPREP_CURRENT);
446 ASSERT_RESULT(r, idn_success);
448 r = idn_mapselector_add(ctxs[0],
450 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
451 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
453 IDN_NAMEPREP_CURRENT);
454 ASSERT_RESULT(r, idn_success);
456 r = idn_mapselector_add(ctxs[0],
457 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
458 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
460 IDN_NAMEPREP_CURRENT);
461 ASSERT_RESULT(r, idn_invalid_name);
463 r = idn_mapselector_add(ctxs[0],
465 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
466 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
468 IDN_NAMEPREP_CURRENT);
469 ASSERT_RESULT(r, idn_invalid_name);
473 // title: call addall() with too long tld
474 // group: generic-init quiet
476 static const char *names[] = {IDN_NAMEPREP_CURRENT};
478 r = idn_mapselector_addall(ctxs[0],
479 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
480 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
483 ASSERT_RESULT(r, idn_success);
485 r = idn_mapselector_addall(ctxs[0],
487 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
488 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
491 ASSERT_RESULT(r, idn_success);
493 r = idn_mapselector_addall(ctxs[0],
494 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
495 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
498 ASSERT_RESULT(r, idn_invalid_name);
500 r = idn_mapselector_addall(ctxs[0],
502 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
503 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
506 ASSERT_RESULT(r, idn_invalid_name);
510 // title: overrun test for arg `to' of map()
511 // group: generic-init
513 r = idn_mapselector_add(ctxs[0], utf8_tlds_jp[0],
514 IDN_NAMEPREP_CURRENT);
515 ASSERT_RESULT(r, idn_success);
517 r = idn_mapselector_map(ctxs[0], in_string, utf8_tlds_jp[0], to,
518 idn_ucs4_strlen(out_string_nameprep) + 1);
519 ASSERT_RESULT(r, idn_success);
520 ASSERT_UCS4STRING(to, out_string_nameprep);
522 r = idn_mapselector_map(ctxs[0], in_string, utf8_tlds_tw[0], to,
523 idn_ucs4_strlen(in_string) + 1);
524 ASSERT_RESULT(r, idn_success);
525 ASSERT_UCS4STRING(to, in_string);
527 r = idn_mapselector_map(ctxs[0], in_string, utf8_tlds_jp[0], to,
528 idn_ucs4_strlen(out_string_nameprep));
529 ASSERT_RESULT(r, idn_buffer_overflow);
531 r = idn_mapselector_map(ctxs[0], in_string, utf8_tlds_tw[0], to,
532 idn_ucs4_strlen(in_string));
533 ASSERT_RESULT(r, idn_buffer_overflow);
537 // title: overrun test for arg `to' of map2()
538 // group: generic-init
540 r = idn_mapselector_add(ctxs[0], utf8_tlds_jp[0],
541 IDN_NAMEPREP_CURRENT);
542 ASSERT_RESULT(r, idn_success);
544 r = idn_mapselector_map2(ctxs[0], in_string, ucs4_tlds_jp[0], to,
545 idn_ucs4_strlen(out_string_nameprep) + 1);
546 ASSERT_RESULT(r, idn_success);
547 ASSERT_UCS4STRING(to, out_string_nameprep);
549 r = idn_mapselector_map2(ctxs[0], in_string, ucs4_tlds_tw[0], to,
550 idn_ucs4_strlen(in_string) + 1);
551 ASSERT_RESULT(r, idn_success);
552 ASSERT_UCS4STRING(to, in_string);
554 r = idn_mapselector_map2(ctxs[0], in_string, ucs4_tlds_jp[0], to,
555 idn_ucs4_strlen(out_string_nameprep));
556 ASSERT_RESULT(r, idn_buffer_overflow);
558 r = idn_mapselector_map2(ctxs[0], in_string, ucs4_tlds_tw[0], to,
559 idn_ucs4_strlen(in_string));
560 ASSERT_RESULT(r, idn_buffer_overflow);
564 // title: call map() with tolen=0
565 // group: generic-init
567 r = idn_mapselector_add(ctxs[0], utf8_tlds_jp[0],
568 IDN_NAMEPREP_CURRENT);
569 ASSERT_RESULT(r, idn_success);
571 r = idn_mapselector_map(ctxs[0], in_string, utf8_tlds_jp[0], to, 0);
572 ASSERT_RESULT(r, idn_buffer_overflow);
574 r = idn_mapselector_map(ctxs[0], in_string, utf8_tlds_tw[0], to, 0);
575 ASSERT_RESULT(r, idn_buffer_overflow);
579 // title: call map2() with tolen=0
580 // group: generic-init
582 r = idn_mapselector_add(ctxs[0], utf8_tlds_jp[0],
583 IDN_NAMEPREP_CURRENT);
584 ASSERT_RESULT(r, idn_success);
586 r = idn_mapselector_map2(ctxs[0], in_string, ucs4_tlds_jp[0], to, 0);
587 ASSERT_RESULT(r, idn_buffer_overflow);
589 r = idn_mapselector_map2(ctxs[0], in_string, ucs4_tlds_tw[0], to, 0);
590 ASSERT_RESULT(r, idn_buffer_overflow);