1#ifndef lint 2static char *rcsid = "$Id: delimitermap.tsy,v 1.1 2003/06/04 00:26:53 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 <stdarg.h> 52#include <idn/delimitermap.h> 53#include <idn/ucs4.h> 54#include <idn/log.h> 55#include "testutil.h" 56 57/* 58 * Codepoions to test the add() function. 59 */ 60#define ADDITIONAL_DELIMITER0 0xe0 61#define ADDITIONAL_DELIMITER1 0xe1 62 63/* 64 * Sample string for `from' argument of map(), 65 * and its expected outputs. 66 */ 67static const unsigned long from[] = { 68 0x002e, /* full stop */ 69 0x3002, /* ideographic full stop */ 70 0xff0e, /* fullwidth full stop */ 71 0xff61, /* halfwidth ideographic full stop */ 72 ADDITIONAL_DELIMITER0, 73 ADDITIONAL_DELIMITER1, 74 0x0000 75}; 76 77static const unsigned long expected_default[] = { 78 0x002e, /* full stop */ 79 0x002e, /* full stop */ 80 0x002e, /* full stop */ 81 0x002e, /* full stop */ 82 ADDITIONAL_DELIMITER0, 83 ADDITIONAL_DELIMITER1, 84 0x0000 85}; 86 87static const unsigned long expected_add[] = { 88 0x002e, /* full stop */ 89 0x002e, /* full stop */ 90 0x002e, /* full stop */ 91 0x002e, /* full stop */ 92 0x002e, /* full stop */ 93 ADDITIONAL_DELIMITER1, 94 0x0000 95}; 96 97static const unsigned long expected_addall[] = { 98 0x002e, /* full stop */ 99 0x002e, /* full stop */ 100 0x002e, /* full stop */ 101 0x002e, /* full stop */ 102 0x002e, /* full stop */ 103 0x002e, /* full stop */ 104 0x0000 105}; 106 107//-------------------------------------------------------------------- 108// Setups and Teardowns. 109//-------------------------------------------------------------------- 110 111//# SETUP 112// group: generic-init 113{ 114 idn_result_t r; 115 idn_delimitermap_t ctx; 116 unsigned long to[256]; 117 118 r = idn_delimitermap_create(&ctx); 119 ASSERT_RESULT(r, idn_success); 120} 121 122//# TEARDOWN 123// group: generic-init 124{ 125 if (ctx != NULL) 126 idn_delimitermap_destroy(ctx); 127} 128 129//# SETUP 130// group: quiet 131{ 132 int saved_log_level; 133 134 saved_log_level = idn_log_getlevel(); 135 idn_log_setlevel(idn_log_level_fatal); 136} 137 138//# TEARDOWN 139// group: quiet 140{ 141 idn_log_setlevel(saved_log_level); 142} 143 144//-------------------------------------------------------------------- 145// Testcases. 146//-------------------------------------------------------------------- 147 148//# TESTCASE 149// title: call create() 150// group: generic-init 151{ 152} 153 154//# TESTCASE 155// title: call map() without additional delimiters 156// group: generic-init 157{ 158 r = idn_delimitermap_map(ctx, from, to, sizeof(to) / sizeof(*to)); 159 ASSERT_RESULT(r, idn_success); 160 ASSERT_UCS4STRING(to, expected_default); 161} 162 163//# TESTCASE 164// title: call add() and map() 165// group: generic-init 166{ 167 r = idn_delimitermap_add(ctx, ADDITIONAL_DELIMITER0); 168 ASSERT_RESULT(r, idn_success); 169 170 r = idn_delimitermap_map(ctx, from, to, sizeof(to) / sizeof(*to)); 171 ASSERT_RESULT(r, idn_success); 172 ASSERT_UCS4STRING(to, expected_add); 173} 174 175//# TESTCASE 176// title: call addall() 177// group: generic-init 178{ 179 unsigned long delimiters[2]; 180 181 delimiters[0] = ADDITIONAL_DELIMITER0; 182 delimiters[1] = ADDITIONAL_DELIMITER1; 183 r = idn_delimitermap_addall(ctx, delimiters, 2); 184 ASSERT_RESULT(r, idn_success); 185 186 r = idn_delimitermap_map(ctx, from, to, sizeof(to) / sizeof(*to)); 187 ASSERT_RESULT(r, idn_success); 188 ASSERT_UCS4STRING(to, expected_addall); 189} 190 191//# TESTCASE 192// title: call addall() with nnames=0 193// group: generic-init 194{ 195 unsigned long delimiters[2]; 196 197 r = idn_delimitermap_addall(ctx, delimiters, 0); 198 ASSERT_RESULT(r, idn_success); 199 200 r = idn_delimitermap_map(ctx, from, to, sizeof(to) / sizeof(*to)); 201 ASSERT_RESULT(r, idn_success); 202 ASSERT_UCS4STRING(to, expected_default); 203} 204 205//# TESTCASE 206// title: call add() with invalid codepoint 207// group: generic-init quiet 208{ 209 r = idn_delimitermap_add(ctx, 0x0000); /* NUL */ 210 ASSERT_RESULT(r, idn_invalid_codepoint); 211 212 r = idn_delimitermap_add(ctx, 0xd800); /* surrogate */ 213 ASSERT_RESULT(r, idn_invalid_codepoint); 214 215 r = idn_delimitermap_add(ctx, 0x110000); /* out of range */ 216 ASSERT_RESULT(r, idn_invalid_codepoint); 217} 218 219//# TESTCASE 220// title: call addall() with invalid codepoint 221// group: generic-init quiet 222{ 223 unsigned long delimiters[1]; 224 225 delimiters[0] = 0x0000; /* NUL */ 226 r = idn_delimitermap_addall(ctx, delimiters, 1); 227 ASSERT_RESULT(r, idn_invalid_codepoint); 228 229 delimiters[0] = 0xd800; /* surrogate */ 230 r = idn_delimitermap_addall(ctx, delimiters, 1); 231 ASSERT_RESULT(r, idn_invalid_codepoint); 232 233 delimiters[0] = 0x110000; /* out of range */ 234 r = idn_delimitermap_addall(ctx, delimiters, 1); 235 ASSERT_RESULT(r, idn_invalid_codepoint); 236} 237 238//# TESTCASE 239// title: overrun test for arg `to' of map() 240// group: generic-init 241{ 242 r = idn_delimitermap_map(ctx, from, to, 243 idn_ucs4_strlen(expected_default) + 1); 244 ASSERT_RESULT(r, idn_success); 245 ASSERT_UCS4STRING(to, expected_default); 246 r = idn_delimitermap_map(ctx, from, to, 247 idn_ucs4_strlen(expected_default)); 248 ASSERT_RESULT(r, idn_buffer_overflow); 249} 250 251//# TESTCASE 252// title: call map() with tolen=0 253// group: generic-init 254{ 255 r = idn_delimitermap_map(ctx, from, to, 0); 256 ASSERT_RESULT(r, idn_buffer_overflow); 257} 258