etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / lib / tests / mapselector.tsy
blobcfc3bac143b3d474e95aabecd3d9c091e5cd1ec4
1 #ifndef lint
2 static char *rcsid = "Id: mapselector.tsy,v 1.1 2003/06/04 00:26:55 marka Exp ";
3 #endif
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  */
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <stdarg.h>
52 #include <idn/mapselector.h>
53 #include <idn/ucs4.h>
54 #include <idn/log.h>
56 #include "testutil.h"
58 #define CONF_FILENAME           "test.conf"
59 #define MAP_FILENAME            "test.map"
62  * Sample TLDs.
63  */
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)
82  */
83 static const unsigned long in_string[] = {0x00C0, 0x2212, 0};
86  * Sample mapping results of IN_STRING.
87  *
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.
91  */
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 //--------------------------------------------------------------------
102 //# SETUP
103 //      group: generic-init
105         idn_result_t r;
106         idn_mapselector_t ctxs[TLD_NVARIANTS];
107         unsigned long to[256];
109         {
110                 int i;
111                 for (i = 0; i < TLD_NVARIANTS; i++)
112                         ctxs[i] = NULL;
113         }
115         r = idn_mapselector_initialize();
116         ASSERT_RESULT(r, idn_success);
118         {
119                 int i;
120                 for (i = 0; i < TLD_NVARIANTS; i++) {
121                         r = idn_mapselector_create(&ctxs[i]);
122                         ASSERT_RESULT(r, idn_success);
123                 }
124         }
127 //# TEARDOWN
128 //      group: generic-init
130         {
131                 int i;
133                 for (i = 0; i < TLD_NVARIANTS; i++) {
134                         if (ctxs[i] != NULL)
135                                 idn_mapselector_destroy(ctxs[i]);
136                         remove(CONF_FILENAME);
137                 }
138         }
141 //# SETUP
142 //      group: quiet
144         int saved_log_level;
146         saved_log_level = idn_log_getlevel();
147         idn_log_setlevel(idn_log_level_fatal);
150 //# TEARDOWN
151 //      group: quiet
153         idn_log_setlevel(saved_log_level);
156 //# SETUP
157 //      group: generic-filemap
159         create_conf_file(MAP_FILENAME, 0,
160                          "U+2212; U+002D",
161                          NULL);
164 //# TEARDOWN
165 //      group: generic-filemap
167         remove(MAP_FILENAME);
170 //--------------------------------------------------------------------
171 // Testcases.
172 //--------------------------------------------------------------------
174 //# TESTCASE
175 //      title: call initialize() twice.
178         idn_result_t r;
180         r = idn_mapselector_initialize();
181         ASSERT_RESULT(r, idn_success);
183         r = idn_mapselector_initialize();
184         ASSERT_RESULT(r, idn_success);
187 //# TESTCASE
188 //      title: call create()
190         idn_result_t r;
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);
202 //# TESTCASE
203 //      title: call add(filemap) and map()
204 //      group: generic-init generic-filemap
206         int i, j;
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);
212         }
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,
217                                                 utf8_tlds_jp[j], to,
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,
223                                                 utf8_tlds_tw[j], to,
224                                                 sizeof(to) / sizeof(*to));
225                         ASSERT_RESULT(r, idn_success);
226                         ASSERT_UCS4STRING(to, in_string);
227                 }
228         }
231 //# TESTCASE
232 //      title: call add(nameprep) and map()
233 //      group: generic-init generic-filemap
235         int i, j;
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);
241         }
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,
246                                                 utf8_tlds_jp[j], to,
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,
252                                                 utf8_tlds_tw[j], to,
253                                                 sizeof(to) / sizeof(*to));
254                         ASSERT_RESULT(r, idn_success);
255                         ASSERT_UCS4STRING(to, in_string);
256                 }
257         }
260 //# TESTCASE
261 //      title: call add(filemap) and map2()
262 //      group: generic-init generic-filemap
264         int i, j;
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);
270         }
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,
275                                                  ucs4_tlds_jp[j], to,
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,
281                                                  ucs4_tlds_tw[j], to,
282                                                  sizeof(to) / sizeof(*to));
283                         ASSERT_RESULT(r, idn_success);
284                         ASSERT_UCS4STRING(to, in_string);
285                 }
286         }
289 //# TESTCASE
290 //      title: call add(nameprep) and map2()
291 //      group: generic-init generic-filemap
293         int i, j;
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);
299         }
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,
304                                                  ucs4_tlds_jp[j], to,
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,
310                                                  ucs4_tlds_tw[j], to,
311                                                  sizeof(to) / sizeof(*to));
312                         ASSERT_RESULT(r, idn_success);
313                         ASSERT_UCS4STRING(to, in_string);
314                 }
315         }
318 //# TESTCASE
319 //      title: call add(filemap) and map()
320 //      group: generic-init generic-filemap
322         int i, j;
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);
328         }
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,
333                                                 utf8_tlds_jp[j], to,
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,
339                                                 utf8_tlds_tw[j], to,
340                                                 sizeof(to) / sizeof(*to));
341                         ASSERT_RESULT(r, idn_success);
342                         ASSERT_UCS4STRING(to, in_string);
343                 }
344         }
347 //# TESTCASE
348 //      title: call add(nameprep) and map()
349 //      group: generic-init generic-filemap
351         int i, j;
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);
357         }
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,
362                                                 utf8_tlds_jp[j], to,
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,
368                                                 utf8_tlds_tw[j], to,
369                                                 sizeof(to) / sizeof(*to));
370                         ASSERT_RESULT(r, idn_success);
371                         ASSERT_UCS4STRING(to, in_string);
372                 }
373         }
376 //# TESTCASE
377 //      title: call addall()
378 //      group: generic-init generic-filemap
380         static const char *names[] = {
381                 "filemap:" MAP_FILENAME,
382                 IDN_NAMEPREP_CURRENT
383         };
384         int i, j;
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);
390         }
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,
395                                                  ucs4_tlds_jp[j], to,
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,
401                                                  ucs4_tlds_tw[j], to,
402                                                  sizeof(to) / sizeof(*to));
403                         ASSERT_RESULT(r, idn_success);
404                         ASSERT_UCS4STRING(to, in_string);
405                 }
406         }
409 //# TESTCASE
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);
419 //# TESTCASE
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);
427 //# TESTCASE
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);
437 //# TESTCASE
438 //      title: call add() with too long tld
439 //      group: generic-init quiet
441         r = idn_mapselector_add(ctxs[0],
442                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
443                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
444                                 "ABCDEFGHIJK",
445                                 IDN_NAMEPREP_CURRENT);
446         ASSERT_RESULT(r, idn_success);
448         r = idn_mapselector_add(ctxs[0],
449                                 "."
450                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
451                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
452                                 "ABCDEFGHIJK",
453                                 IDN_NAMEPREP_CURRENT);
454         ASSERT_RESULT(r, idn_success);
456         r = idn_mapselector_add(ctxs[0],
457                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
458                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
459                                 "ABCDEFGHIJKL",
460                                 IDN_NAMEPREP_CURRENT);
461         ASSERT_RESULT(r, idn_invalid_name);
463         r = idn_mapselector_add(ctxs[0],
464                                 "."
465                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
466                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
467                                 "ABCDEFGHIJKL",
468                                 IDN_NAMEPREP_CURRENT);
469         ASSERT_RESULT(r, idn_invalid_name);
472 //# TESTCASE
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"
481                                    "ABCDEFGHIJK",
482                                    names, 1);
483         ASSERT_RESULT(r, idn_success);
485         r = idn_mapselector_addall(ctxs[0],
486                                    "."
487                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
488                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
489                                    "ABCDEFGHIJK",
490                                    names, 1);
491         ASSERT_RESULT(r, idn_success);
493         r = idn_mapselector_addall(ctxs[0],
494                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
495                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
496                                    "ABCDEFGHIJKL",
497                                    names, 1);
498         ASSERT_RESULT(r, idn_invalid_name);
500         r = idn_mapselector_addall(ctxs[0],
501                                    "."
502                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
503                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
504                                    "ABCDEFGHIJKL",
505                                    names, 1);
506         ASSERT_RESULT(r, idn_invalid_name);
509 //# TESTCASE
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);
536 //# TESTCASE
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);
563 //# TESTCASE
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);
578 //# TESTCASE
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);