2 static char *rcsid = "Id: mapper.tsy,v 1.1 2003/06/04 00:26:54 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/mapper.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
64 #define CONF_FILENAME "test.map"
66 #define LINEBUF_SIZE 2001
69 * Sample string for `from' argument of map(),
70 * and its expected outputs.
72 static const unsigned long from[] = {
73 0x0041, /* latin capital letter a */
74 0x0042, /* latin capital letter b */
79 static const unsigned long expected_default[] = {
80 0x0061, /* latin small letter a */
81 0x0062, /* latin small letter b */
87 test_create(const char *parameter, void **ctxp)
93 test_destroy(void *ctxp)
98 test_map(void *ctx, const unsigned long *from, unsigned long *to,
101 if (tolen > idn_ucs4_strlen(from)) {
102 idn_ucs4_strcpy(to, from);
104 return (idn_buffer_overflow);
107 return (idn_success);
110 //--------------------------------------------------------------------
111 // Setups and Teardowns.
112 //--------------------------------------------------------------------
126 // Initialize the module and create context.
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);
142 idn_mapper_destroy(ctx);
148 // Initialize the module and create context.
151 idn_mapper_t ctx = NULL;
152 char *names[ARRAY_SIZE];
156 for (i = 0; i < ARRAY_SIZE; i++) {
157 names[i] = malloc(BUF_SIZE);
158 if (names[i] == NULL) {
159 ASSERT("malloc failed\n");
163 r = idn_mapper_initialize();
164 ASSERT_RESULT(r, idn_success);
165 r = idn_mapper_create(&ctx);
166 ASSERT_RESULT(r, idn_success);
172 // Destroy context and free some blocks.
174 idn_mapper_destroy(ctx);
175 for (i = 0; i < ARRAY_SIZE; i++) {
183 // Set log level to `fatal' to supress log messages.
187 saved_log_level = idn_log_getlevel();
188 idn_log_setlevel(idn_log_level_fatal);
194 // Restore log level.
196 idn_log_setlevel(saved_log_level);
199 //--------------------------------------------------------------------
201 //--------------------------------------------------------------------
204 // title: idn_mapper_add() - boundary condition
205 // group: generic quiet
207 r = idn_mapper_add(ctx, "");
208 ASSERT_RESULT(r, idn_invalid_name);
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);
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);
235 // title: idn_mapper_add() - file
236 // group: generic quiet
241 create_conf_file(CONF_FILENAME, 0,
245 sprintf(name, "filemap:%s", CONF_FILENAME);
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);
255 // title: idn_mapper_add() - file - long line
256 // group: generic quiet
258 char line[LINEBUF_SIZE];
260 const char *first_entry = "0041;";
261 const char *other_entry = " 0061";
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);
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);
279 // title: idn_mapper_add() - file - no new line at end of file
280 // group: generic quiet
285 create_conf_file(CONF_FILENAME, CONF_NO_EOF_NEWLINE,
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);
298 // title: idn_mapper_add() - file - one item in one line #1
299 // group: generic quiet
303 unsigned long expected[] = {
309 create_conf_file(CONF_FILENAME, 0,
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);
322 // title: idn_mapper_add() - file - one item in one line #2
323 // group: generic quiet
327 unsigned long expected[] = {
333 create_conf_file(CONF_FILENAME, 0,
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);
346 // title: idn_mapper_add() - file - more then two items in one line #1
347 // group: generic quiet
352 create_conf_file(CONF_FILENAME, 0,
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);
366 // title: idn_mapper_add() - file - more then two items in one line #2
367 // group: generic quiet
371 unsigned long expected_to[] = {
380 create_conf_file(CONF_FILENAME, 0,
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);
394 // title: idn_mapper_add() - file - more then two items in one line #3
395 // group: generic quiet
400 create_conf_file(CONF_FILENAME, 0,
403 sprintf(name, "filemap:%s", CONF_FILENAME);
404 r = idn_mapper_add(ctx, name);
405 ASSERT_RESULT(r, idn_invalid_syntax);
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);
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");
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);
444 // title: idn_mapper_map() - builtin schemes check - RFC3491
445 // group: generic quiet
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);
457 // title: idn_mapper_map() - context without procedure
462 r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to));
463 ASSERT_RESULT(r, idn_success);
464 ASSERT_UCS4STRING(to, from);
468 // title: idn_mapper_destroy(), idn_mapper_incrref()
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);
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);