1#ifndef lint 2static char *rcsid = "$Id: mapper.tsy,v 1.1 2003/06/04 00:26:54 marka Exp $"; 3#endif 4 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 */ 47 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" 56 57#define SIZEOFUCS4(x) (sizeof(x) / sizeof(unsigned long)) 58 59#define UCS4_NAME_STR "U+304C" /* hiragana letter ga */ 60#define UCS4_NAME 0x304C 61 62#define BUF_SIZE 128 63#define ARRAY_SIZE 9 64#define CONF_FILENAME "test.map" 65 66#define LINEBUF_SIZE 2001 67 68/* 69 * Sample string for `from' argument of map(), 70 * and its expected outputs. 71 */ 72static const unsigned long from[] = { 73 0x0041, /* latin capital letter a */ 74 0x0042, /* latin capital letter b */ 75 UCS4_NAME, 76 0x0000 77}; 78 79static const unsigned long expected_default[] = { 80 0x0061, /* latin small letter a */ 81 0x0062, /* latin small letter b */ 82 UCS4_NAME, 83 0x0000 84}; 85 86idn_result_t 87test_create(const char *parameter, void **ctxp) 88{ 89 return (idn_success); 90} 91 92void 93test_destroy(void *ctxp) 94{ 95} 96 97idn_result_t 98test_map(void *ctx, const unsigned long *from, unsigned long *to, 99 size_t tolen) 100{ 101 if (tolen > idn_ucs4_strlen(from)) { 102 idn_ucs4_strcpy(to, from); 103 } else { 104 return (idn_buffer_overflow); 105 } 106 107 return (idn_success); 108} 109 110//-------------------------------------------------------------------- 111// Setups and Teardowns. 112//-------------------------------------------------------------------- 113 114//# SETUP 115// group: noinit 116//-- 117// Do nothing 118{ 119 idn_result_t r; 120 const char *name; 121} 122 123//# SETUP 124// group: generic 125//-- 126// Initialize the module and create context. 127{ 128 idn_result_t r; 129 idn_mapper_t ctx = NULL; 130 131 r = idn_mapper_initialize(); 132 ASSERT_RESULT(r, idn_success); 133 r = idn_mapper_create(&ctx); 134 ASSERT_RESULT(r, idn_success); 135} 136 137//# TEARDOWN 138// group: generic 139//-- 140// Destroy context. 141{ 142 idn_mapper_destroy(ctx); 143} 144 145//# SETUP 146// group: addall 147//-- 148// Initialize the module and create context. 149{ 150 idn_result_t r; 151 idn_mapper_t ctx = NULL; 152 char *names[ARRAY_SIZE]; 153 int i; 154 unsigned long to[4]; 155 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 } 162 163 r = idn_mapper_initialize(); 164 ASSERT_RESULT(r, idn_success); 165 r = idn_mapper_create(&ctx); 166 ASSERT_RESULT(r, idn_success); 167} 168 169//# TEARDOWN 170// group: addall 171//-- 172// Destroy context and free some blocks. 173{ 174 idn_mapper_destroy(ctx); 175 for (i = 0; i < ARRAY_SIZE; i++) { 176 free(names[i]); 177 } 178} 179 180//# SETUP 181// group: quiet 182//-- 183// Set log level to `fatal' to supress log messages. 184{ 185 int saved_log_level; 186 187 saved_log_level = idn_log_getlevel(); 188 idn_log_setlevel(idn_log_level_fatal); 189} 190 191//# TEARDOWN 192// group: quiet 193//-- 194// Restore log level. 195{ 196 idn_log_setlevel(saved_log_level); 197} 198 199//-------------------------------------------------------------------- 200// Testcases. 201//-------------------------------------------------------------------- 202 203//# TESTCASE 204// title: idn_mapper_add() - boundary condition 205// group: generic quiet 206{ 207 r = idn_mapper_add(ctx, ""); 208 ASSERT_RESULT(r, idn_invalid_name); 209} 210 211//# TESTCASE 212// title: idn_mapper_add() - builtin schemes 213// group: generic quiet 214{ 215 r = idn_mapper_add(ctx, "RFC3491"); 216 ASSERT_RESULT(r, idn_success); 217 218 r = idn_mapper_add(ctx, "nameprep-01"); 219 ASSERT_RESULT(r, idn_invalid_name); 220} 221 222//# TESTCASE 223// title: idn_mapper_add() - boundary condition 224// group: generic quiet 225{ 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); 232} 233 234//# TESTCASE 235// title: idn_mapper_add() - file 236// group: generic quiet 237{ 238 char name[BUF_SIZE]; 239 unsigned long to[4]; 240 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); 252} 253 254//# TESTCASE 255// title: idn_mapper_add() - file - long line 256// group: generic quiet 257{ 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; 264 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'; 271 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); 276} 277 278//# TESTCASE 279// title: idn_mapper_add() - file - no new line at end of file 280// group: generic quiet 281{ 282 char name[BUF_SIZE]; 283 unsigned long to[4]; 284 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); 295} 296 297//# TESTCASE 298// title: idn_mapper_add() - file - one item in one line #1 299// group: generic quiet 300{ 301 char name[BUF_SIZE]; 302 unsigned long to[3]; 303 unsigned long expected[] = { 304 0x0061, 305 UCS4_NAME, 306 0x0000 307 }; 308 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); 319} 320 321//# TESTCASE 322// title: idn_mapper_add() - file - one item in one line #2 323// group: generic quiet 324{ 325 char name[BUF_SIZE]; 326 unsigned long to[3]; 327 unsigned long expected[] = { 328 0x0061, 329 UCS4_NAME, 330 0x0000 331 }; 332 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); 343} 344 345//# TESTCASE 346// title: idn_mapper_add() - file - more then two items in one line #1 347// group: generic quiet 348{ 349 char name[BUF_SIZE]; 350 unsigned long to[4]; 351 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); 359 360 r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to)); 361 ASSERT_RESULT(r, idn_success); 362 ASSERT_UCS4STRING(to, expected_default); 363} 364 365//# TESTCASE 366// title: idn_mapper_add() - file - more then two items in one line #2 367// group: generic quiet 368{ 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 }; 379 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); 387 388 r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to)); 389 ASSERT_RESULT(r, idn_success); 390 ASSERT_UCS4STRING(to, expected_to); 391} 392 393//# TESTCASE 394// title: idn_mapper_add() - file - more then two items in one line #3 395// group: generic quiet 396{ 397 char name[BUF_SIZE]; 398 unsigned long to[3]; 399 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); 406} 407 408//# TESTCASE 409// title: idn_mapper_addall() - add all builtin schemes 410// group: addall quiet 411{ 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"); 421 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); 427} 428 429//# TESTCASE 430// title: idn_mapper_addall() - add same scheme repetedly 431// group: addall quiet 432{ 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); 441} 442 443//# TESTCASE 444// title: idn_mapper_map() - builtin schemes check - RFC3491 445// group: generic quiet 446{ 447 unsigned long to[4]; 448 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); 454} 455 456//# TESTCASE 457// title: idn_mapper_map() - context without procedure 458// group: generic 459{ 460 unsigned long to[4]; 461 462 r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to)); 463 ASSERT_RESULT(r, idn_success); 464 ASSERT_UCS4STRING(to, from); 465} 466 467//# TESTCASE 468// title: idn_mapper_destroy(), idn_mapper_incrref() 469// group: 470{ 471 idn_result_t r; 472 idn_mapper_t ctx = NULL; 473 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); 481} 482 483//# TESTCASE 484// title: idn_mapper_register() 485// group: generic quiet 486{ 487 unsigned long to[10]; 488 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); 493 494 r = idn_mapper_map(ctx, from, to, SIZEOFUCS4(to)); 495 ASSERT_RESULT(r, idn_success); 496 ASSERT_UCS4STRING(to, from); 497} 498