etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / idn / idnkit-1.0-src / lib / tests / mapper.tsy
blob5f7ca716e7e73145b304db12fe1ccf2fee3e3111
1 #ifndef lint
2 static char *rcsid = "Id: mapper.tsy,v 1.1 2003/06/04 00:26:54 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 <string.h>
52 #include <idn/mapper.h>
53 #include <idn/log.h>
54 #include <idn/ucs4.h>
55 #include "testutil.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
62 #define BUF_SIZE        128
63 #define ARRAY_SIZE      9
64 #define CONF_FILENAME   "test.map"
66 #define LINEBUF_SIZE    2001
69  * Sample string for `from' argument of map(),
70  * and its expected outputs.
71  */
72 static const unsigned long from[] = {
73         0x0041,         /* latin capital letter a */
74         0x0042,         /* latin capital letter b */
75         UCS4_NAME,
76         0x0000
79 static const unsigned long expected_default[] = {
80         0x0061,         /* latin small letter a */
81         0x0062,         /* latin small letter b */
82         UCS4_NAME,
83         0x0000
86 idn_result_t
87 test_create(const char *parameter, void **ctxp)
89         return (idn_success);
92 void
93 test_destroy(void *ctxp)
97 idn_result_t
98 test_map(void *ctx, const unsigned long *from, unsigned long *to,
99          size_t tolen)
101         if (tolen > idn_ucs4_strlen(from)) {
102                 idn_ucs4_strcpy(to, from);
103         } else {
104                 return (idn_buffer_overflow);
105         }
107         return (idn_success);
110 //--------------------------------------------------------------------
111 // Setups and Teardowns.
112 //--------------------------------------------------------------------
114 //# SETUP
115 //      group: noinit
116 //--
117 //      Do nothing
119         idn_result_t r;
120         const char *name;
123 //# SETUP
124 //      group: generic
125 //--
126 //      Initialize the module and create context.
128         idn_result_t r;
129         idn_mapper_t ctx = NULL;
131         r = idn_mapper_initialize();
132         ASSERT_RESULT(r, idn_success);
133         r = idn_mapper_create(&ctx);
134         ASSERT_RESULT(r, idn_success);
137 //# TEARDOWN
138 //      group: generic
139 //--
140 //      Destroy context.
142         idn_mapper_destroy(ctx);
145 //# SETUP
146 //      group: addall
147 //--
148 //      Initialize the module and create context.
150         idn_result_t r;
151         idn_mapper_t ctx = NULL;
152         char *names[ARRAY_SIZE];
153         int i;
154         unsigned long to[4];
156         for (i = 0; i < ARRAY_SIZE; i++) {
157                 names[i] = malloc(BUF_SIZE);
158                 if (names[i] == NULL) {
159                         ASSERT("malloc failed\n");
160                 }
161         }
163         r = idn_mapper_initialize();
164         ASSERT_RESULT(r, idn_success);
165         r = idn_mapper_create(&ctx);
166         ASSERT_RESULT(r, idn_success);
169 //# TEARDOWN
170 //      group: addall
171 //--
172 //      Destroy context and free some blocks.
174         idn_mapper_destroy(ctx);
175         for (i = 0; i < ARRAY_SIZE; i++) {
176                 free(names[i]);
177         }
180 //# SETUP
181 //      group: quiet
182 //--
183 //      Set log level to `fatal' to supress log messages.
185         int saved_log_level;
187         saved_log_level = idn_log_getlevel();
188         idn_log_setlevel(idn_log_level_fatal);
191 //# TEARDOWN
192 //      group: quiet
193 //--
194 //      Restore log level.
196         idn_log_setlevel(saved_log_level);
199 //--------------------------------------------------------------------
200 // Testcases.
201 //--------------------------------------------------------------------
203 //# TESTCASE
204 //      title: idn_mapper_add() - boundary condition
205 //      group: generic quiet
207         r = idn_mapper_add(ctx, "");
208         ASSERT_RESULT(r, idn_invalid_name);
211 //# TESTCASE
212 //      title: idn_mapper_add() - builtin schemes
213 //      group: generic quiet
215         r = idn_mapper_add(ctx, "RFC3491");
216         ASSERT_RESULT(r, idn_success);
218         r = idn_mapper_add(ctx, "nameprep-01");
219         ASSERT_RESULT(r, idn_invalid_name);
222 //# TESTCASE
223 //      title: idn_mapper_add() - boundary condition
224 //      group: generic quiet
226         r = idn_mapper_add(ctx, "");
227         ASSERT_RESULT(r, idn_invalid_name);
228         r = idn_mapper_add(ctx, "filemap:");
229         ASSERT_RESULT(r, idn_nofile);
230         r = idn_mapper_add(ctx, "filemap:notfound.map");
231         ASSERT_RESULT(r, idn_nofile);
234 //# TESTCASE
235 //      title: idn_mapper_add() - file
236 //      group: generic quiet
238         char name[BUF_SIZE];
239         unsigned long to[4];
241         create_conf_file(CONF_FILENAME, 0,
242                          "0041; 0061;",
243                          "0042; 0062;",
244                          NULL);
245         sprintf(name, "filemap:%s", CONF_FILENAME);
246         
247         r = idn_mapper_add(ctx, name);
248         ASSERT_RESULT(r, idn_success);
249         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
250         ASSERT_RESULT(r, idn_success);
251         ASSERT_UCS4STRING(to, expected_default);
254 //# TESTCASE
255 //      title: idn_mapper_add() - file - long line
256 //      group: generic quiet
258         char line[LINEBUF_SIZE];
259         char name[BUF_SIZE];
260         const char *first_entry = "0041;";
261         const char *other_entry = " 0061";
262         int i;
263         int len;
265         memcpy(line, first_entry, strlen(first_entry));
266         len = strlen(other_entry);      
267         for (i = len; i < LINEBUF_SIZE - len; i += len) {
268                 memcpy(line + i, other_entry, len);
269         }
270         *(line + i) = '\0';
272         create_conf_file(CONF_FILENAME, 0, line, NULL);
273         sprintf(name, "filemap:%s", CONF_FILENAME);
274         r = idn_mapper_add(ctx, name);
275         ASSERT_RESULT(r, idn_invalid_syntax);
278 //# TESTCASE
279 //      title: idn_mapper_add() - file - no new line at end of file
280 //      group: generic quiet
282         char name[BUF_SIZE];
283         unsigned long to[4];
285         create_conf_file(CONF_FILENAME, CONF_NO_EOF_NEWLINE,
286                          "0041; 0061;",
287                          "0042; 0062;",
288                          NULL);
289         sprintf(name, "filemap:%s", CONF_FILENAME);
290         r = idn_mapper_add(ctx, name);
291         ASSERT_RESULT(r, idn_success);
292         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
293         ASSERT_RESULT(r, idn_success);
294         ASSERT_UCS4STRING(to, expected_default);
297 //# TESTCASE
298 //      title: idn_mapper_add() - file - one item in one line #1
299 //      group: generic quiet
301         char name[BUF_SIZE];
302         unsigned long to[3];
303         unsigned long expected[] = {
304                 0x0061,
305                 UCS4_NAME,
306                 0x0000
307         };
309         create_conf_file(CONF_FILENAME, 0,
310                          "0041; 0061;",
311                          "0042;",
312                          NULL);
313         sprintf(name, "filemap:%s", CONF_FILENAME);
314         r = idn_mapper_add(ctx, name);
315         ASSERT_RESULT(r, idn_success);
316         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
317         ASSERT_RESULT(r, idn_success);
318         ASSERT_UCS4STRING(to, expected);
321 //# TESTCASE
322 //      title: idn_mapper_add() - file - one item in one line #2
323 //      group: generic quiet
325         char name[BUF_SIZE];
326         unsigned long to[3];
327         unsigned long expected[] = {
328                 0x0061,
329                 UCS4_NAME,
330                 0x0000
331         };
333         create_conf_file(CONF_FILENAME, 0,
334                          "0041; 0061;",
335                          "0042; ;",
336                          NULL);
337         sprintf(name, "filemap:%s", CONF_FILENAME);
338         r = idn_mapper_add(ctx, name);
339         ASSERT_RESULT(r, idn_success);
340         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
341         ASSERT_RESULT(r, idn_success);
342         ASSERT_UCS4STRING(to, expected);
345 //# TESTCASE
346 //      title: idn_mapper_add() - file -  more then two items in one line #1
347 //      group: generic quiet
349         char name[BUF_SIZE];
350         unsigned long to[4];
352         create_conf_file(CONF_FILENAME, 0,
353                          "0041; 0061; 0062;",
354                          "0042; 0062; 0063;",
355                          NULL);
356         sprintf(name, "filemap:%s", CONF_FILENAME);
357         r = idn_mapper_add(ctx, name);
358         ASSERT_RESULT(r, idn_success);
360         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
361         ASSERT_RESULT(r, idn_success);
362         ASSERT_UCS4STRING(to, expected_default);
365 //# TESTCASE
366 //      title: idn_mapper_add() - file -  more then two items in one line #2
367 //      group: generic quiet
369         char name[BUF_SIZE];
370         unsigned long to[6];
371         unsigned long expected_to[] = {
372                 0x0061,
373                 0x0062,
374                 0x0062,
375                 0x0063,
376                 UCS4_NAME,
377                 0x0000
378         };
380         create_conf_file(CONF_FILENAME, 0,
381                          "0041; 0061 0062;",
382                          "0042; 0062 0063;",
383                          NULL);
384         sprintf(name, "filemap:%s", CONF_FILENAME);
385         r = idn_mapper_add(ctx, name);
386         ASSERT_RESULT(r, idn_success);
388         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
389         ASSERT_RESULT(r, idn_success);
390         ASSERT_UCS4STRING(to, expected_to);
393 //# TESTCASE
394 //      title: idn_mapper_add() - file -  more then two items in one line #3
395 //      group: generic quiet
397         char name[BUF_SIZE];
398         unsigned long to[3];
400         create_conf_file(CONF_FILENAME, 0,
401                          "0041 0042; 0063;",
402                          NULL);
403         sprintf(name, "filemap:%s", CONF_FILENAME);
404         r = idn_mapper_add(ctx, name);
405         ASSERT_RESULT(r, idn_invalid_syntax);
408 //# TESTCASE
409 //      title: idn_mapper_addall() - add all builtin schemes
410 //      group: addall quiet
412         strcpy(names[0], "RFC3491");
413         strcpy(names[1], "RFC3491");
414         strcpy(names[2], "RFC3491");
415         strcpy(names[3], "RFC3491");
416         strcpy(names[4], "RFC3491");
417         strcpy(names[5], "RFC3491");
418         strcpy(names[6], "RFC3491");
419         strcpy(names[7], "RFC3491");
420         strcpy(names[8], "RFC3491");
422         r = idn_mapper_addall(ctx, (const char **)names, 9);
423         ASSERT_RESULT(r, idn_success);
424         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
425         ASSERT_RESULT(r, idn_success);
426         ASSERT_UCS4STRING(to, expected_default);
429 //# TESTCASE
430 //      title: idn_mapper_addall() - add same scheme repetedly
431 //      group: addall quiet
433         for (i = 0; i < ARRAY_SIZE; i++) {
434                 strcpy(names[i], "RFC3491");
435         }
436         r = idn_mapper_addall(ctx, (const char **)names, 3);
437         ASSERT_RESULT(r, idn_success);
438         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
439         ASSERT_RESULT(r, idn_success);
440         ASSERT_UCS4STRING(to, expected_default);
443 //# TESTCASE
444 //      title: idn_mapper_map() - builtin schemes check - RFC3491
445 //      group: generic quiet
447         unsigned long to[4];
449         r = idn_mapper_add(ctx, "RFC3491");
450         ASSERT_RESULT(r, idn_success);
451         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
452         ASSERT_RESULT(r, idn_success);
453         ASSERT_UCS4STRING(to, expected_default);
456 //# TESTCASE
457 //      title: idn_mapper_map() - context without procedure
458 //      group: generic
460         unsigned long to[4];
462         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
463         ASSERT_RESULT(r, idn_success);
464         ASSERT_UCS4STRING(to, from);
467 //# TESTCASE
468 //      title: idn_mapper_destroy(), idn_mapper_incrref()
469 //      group:
471         idn_result_t r;
472         idn_mapper_t ctx = NULL;
474         r = idn_mapper_initialize();
475         ASSERT_RESULT(r, idn_success);
476         r = idn_mapper_create(&ctx);
477         ASSERT_RESULT(r, idn_success);
478         idn_mapper_incrref(ctx);
479         idn_mapper_destroy(ctx);
480         idn_mapper_destroy(ctx);
483 //# TESTCASE
484 //      title: idn_mapper_register()
485 //      group: generic quiet
487         unsigned long to[10];
489         r = idn_mapper_register("test", test_create, test_destroy, test_map);
490         ASSERT_RESULT(r, idn_success);
491         r = idn_mapper_add(ctx, "test");
492         ASSERT_RESULT(r, idn_success);
494         r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
495         ASSERT_RESULT(r, idn_success);
496         ASSERT_UCS4STRING(to, from);