]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/testidn.cpp
ICU-551.51.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / testidn.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3*
57a6839d 4* Copyright (C) 2003-2013, International Business Machines
b75a7d8f
A
5* Corporation and others. All Rights Reserved.
6*
7*******************************************************************************
8* file name: testidn.cpp
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2003-02-06
14* created by: Ram Viswanadha
15*
16* This program reads the rfc3454_*.txt files,
17* parses them, and extracts the data for Nameprep conformance.
18* It then preprocesses it and writes a binary file for efficient use
19* in various IDNA conversion processes.
20*/
21
b75a7d8f
A
22#include "unicode/utypes.h"
23
24#if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
25
374ca955
A
26#define USPREP_TYPE_NAMES_ARRAY
27
b75a7d8f
A
28#include "unicode/uchar.h"
29#include "unicode/putil.h"
30#include "cmemory.h"
31#include "cstring.h"
32#include "unicode/udata.h"
4388f060 33#include "unicode/utf16.h"
b75a7d8f
A
34#include "unewdata.h"
35#include "uoptions.h"
36#include "uparse.h"
37#include "utrie.h"
38#include "umutex.h"
39#include "sprpimpl.h"
40#include "testidna.h"
374ca955 41#include "punyref.h"
73c04bcf 42#include <stdlib.h>
b75a7d8f
A
43
44UBool beVerbose=FALSE, haveCopyright=TRUE;
45
46/* prototypes --------------------------------------------------------------- */
47
48
b75a7d8f 49static void
374ca955 50parseMappings(const char *filename, UBool reportError,TestIDNA& test, UErrorCode *pErrorCode);
b75a7d8f
A
51
52static void
53compareMapping(uint32_t codepoint, uint32_t* mapping, int32_t mapLength,
374ca955 54 UStringPrepType option);
b75a7d8f
A
55
56static void
374ca955 57compareFlagsForRange(uint32_t start, uint32_t end,UStringPrepType option);
b75a7d8f
A
58
59static void
60testAllCodepoints(TestIDNA& test);
61
62static TestIDNA* pTestIDNA =NULL;
63
64static const char* fileNames[] = {
729e4ab9 65 "rfc3491.txt"
374ca955 66 };
374ca955
A
67static const UTrie *idnTrie = NULL;
68static const int32_t *indexes = NULL;
69static const uint16_t *mappingData = NULL;
b75a7d8f
A
70/* -------------------------------------------------------------------------- */
71
72/* file definitions */
b75a7d8f
A
73#define DATA_TYPE "icu"
74
729e4ab9 75#define SPREP_DIR "sprep"
b75a7d8f
A
76
77extern int
78testData(TestIDNA& test) {
b75a7d8f
A
79 char *basename=NULL;
80 UErrorCode errorCode=U_ZERO_ERROR;
81 char *saveBasename =NULL;
82
729e4ab9 83 LocalUStringPrepProfilePointer profile(usprep_openByType(USPREP_RFC3491_NAMEPREP, &errorCode));
374ca955 84 if(U_FAILURE(errorCode)){
729e4ab9 85 test.errcheckln(errorCode, "Failed to load IDNA data file. " + UnicodeString(u_errorName(errorCode)));
374ca955 86 return errorCode;
b75a7d8f
A
87 }
88
46f4442e
A
89 char* filename = (char*) malloc(strlen(IntlTest::pathToDataDirectory())*1024);
90 //TODO get the srcDir dynamically
91 const char *srcDir=IntlTest::pathToDataDirectory();
92
374ca955
A
93 idnTrie = &profile->sprepTrie;
94 indexes = profile->indexes;
95 mappingData = profile->mappingData;
96
b75a7d8f
A
97 //initialize
98 pTestIDNA = &test;
99
100 /* prepare the filename beginning with the source dir */
101 if(uprv_strchr(srcDir,U_FILE_SEP_CHAR) == NULL){
102 filename[0] = 0x2E;
103 filename[1] = U_FILE_SEP_CHAR;
104 uprv_strcpy(filename+2,srcDir);
105 }else{
106 uprv_strcpy(filename, srcDir);
107 }
108 basename=filename+uprv_strlen(filename);
109 if(basename>filename && *(basename-1)!=U_FILE_SEP_CHAR) {
110 *basename++=U_FILE_SEP_CHAR;
111 }
112
113 /* process unassigned */
114 basename=filename+uprv_strlen(filename);
115 if(basename>filename && *(basename-1)!=U_FILE_SEP_CHAR) {
116 *basename++=U_FILE_SEP_CHAR;
117 }
118
119 /* first copy misc directory */
120 saveBasename = basename;
57a6839d 121 (void)saveBasename; // Suppress set but not used warning.
729e4ab9
A
122 uprv_strcpy(basename,SPREP_DIR);
123 basename = basename + uprv_strlen(SPREP_DIR);
b75a7d8f
A
124 *basename++=U_FILE_SEP_CHAR;
125
126 /* process unassigned */
127 uprv_strcpy(basename,fileNames[0]);
374ca955 128 parseMappings(filename,TRUE, test,&errorCode);
b75a7d8f
A
129 if(U_FAILURE(errorCode)) {
130 test.errln( "Could not open file %s for reading \n", filename);
131 return errorCode;
132 }
133
134 testAllCodepoints(test);
135
b75a7d8f
A
136 pTestIDNA = NULL;
137 free(filename);
138 return errorCode;
139}
140U_CDECL_BEGIN
374ca955 141
b75a7d8f 142static void U_CALLCONV
374ca955
A
143strprepProfileLineFn(void * /*context*/,
144 char *fields[][2], int32_t fieldCount,
b75a7d8f
A
145 UErrorCode *pErrorCode) {
146 uint32_t mapping[40];
374ca955 147 char *end, *map;
b75a7d8f
A
148 uint32_t code;
149 int32_t length;
374ca955
A
150 /*UBool* mapWithNorm = (UBool*) context;*/
151 const char* typeName;
152 uint32_t rangeStart=0,rangeEnd =0;
729e4ab9
A
153 const char *s;
154
155 s = u_skipWhitespace(fields[0][0]);
156 if (*s == '@') {
157 /* a special directive introduced in 4.2 */
158 return;
159 }
160
374ca955
A
161 if(fieldCount != 3){
162 *pErrorCode = U_INVALID_FORMAT_ERROR;
163 return;
164 }
b75a7d8f 165
374ca955
A
166 typeName = fields[2][0];
167 map = fields[1][0];
168
169 if(uprv_strstr(typeName, usprepTypeNames[USPREP_UNASSIGNED])!=NULL){
b75a7d8f 170
729e4ab9 171 u_parseCodePointRange(s, &rangeStart,&rangeEnd, pErrorCode);
374ca955
A
172
173 /* store the range */
174 compareFlagsForRange(rangeStart,rangeEnd,USPREP_UNASSIGNED);
175
176 }else if(uprv_strstr(typeName, usprepTypeNames[USPREP_PROHIBITED])!=NULL){
177
729e4ab9 178 u_parseCodePointRange(s, &rangeStart,&rangeEnd, pErrorCode);
b75a7d8f 179
374ca955
A
180 /* store the range */
181 compareFlagsForRange(rangeStart,rangeEnd,USPREP_PROHIBITED);
b75a7d8f 182
374ca955
A
183 }else if(uprv_strstr(typeName, usprepTypeNames[USPREP_MAP])!=NULL){
184 /* get the character code, field 0 */
729e4ab9 185 code=(uint32_t)uprv_strtoul(s, &end, 16);
374ca955
A
186
187 /* parse the mapping string */
188 length=u_parseCodePoints(map, mapping, sizeof(mapping)/4, pErrorCode);
189
190 /* store the mapping */
191 compareMapping(code,mapping, length,USPREP_MAP);
192
193 }else{
194 *pErrorCode = U_INVALID_FORMAT_ERROR;
195 }
b75a7d8f 196
b75a7d8f 197}
374ca955 198
b75a7d8f
A
199U_CDECL_END
200
201static void
374ca955 202parseMappings(const char *filename,UBool reportError, TestIDNA& test, UErrorCode *pErrorCode) {
b75a7d8f
A
203 char *fields[3][2];
204
205 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
206 return;
207 }
208
374ca955 209 u_parseDelimitedFile(filename, ';', fields, 3, strprepProfileLineFn, (void*)filename, pErrorCode);
b75a7d8f
A
210
211 //fprintf(stdout,"Number of code points that have mappings with length >1 : %i\n",len);
212
213 if(U_FAILURE(*pErrorCode) && (reportError || *pErrorCode!=U_FILE_ACCESS_ERROR)) {
214 test.errln( "testidn error: u_parseDelimitedFile(\"%s\") failed - %s\n", filename, u_errorName(*pErrorCode));
215 }
216}
217
b75a7d8f 218
374ca955
A
219static inline UStringPrepType
220getValues(uint32_t result, int32_t& value, UBool& isIndex){
b75a7d8f 221
374ca955 222 UStringPrepType type;
b75a7d8f 223
374ca955
A
224 if(result == 0){
225 /*
226 * Initial value stored in the mapping table
227 * just return USPREP_TYPE_LIMIT .. so that
228 * the source codepoint is copied to the destination
229 */
230 type = USPREP_TYPE_LIMIT;
73c04bcf
A
231 isIndex =FALSE;
232 value = 0;
374ca955
A
233 }else if(result >= _SPREP_TYPE_THRESHOLD){
234 type = (UStringPrepType) (result - _SPREP_TYPE_THRESHOLD);
73c04bcf
A
235 isIndex =FALSE;
236 value = 0;
374ca955
A
237 }else{
238 /* get the state */
239 type = USPREP_MAP;
240 /* ascertain if the value is index or delta */
241 if(result & 0x02){
242 isIndex = TRUE;
243 value = result >> 2; //mask off the lower 2 bits and shift
b75a7d8f 244
374ca955
A
245 }else{
246 isIndex = FALSE;
247 value = (int16_t)result;
248 value = (value >> 2);
b75a7d8f 249
374ca955
A
250 }
251 if((result>>2) == _SPREP_MAX_INDEX_VALUE){
252 type = USPREP_DELETE;
253 isIndex =FALSE;
254 value = 0;
255 }
256 }
257 return type;
b75a7d8f
A
258}
259
374ca955 260
b75a7d8f
A
261
262static void
374ca955
A
263testAllCodepoints(TestIDNA& test){
264 /*
265 {
266 UChar str[19] = {
267 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
268 0x070F,//prohibited
269 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74
270 };
271 uint32_t in[19] = {0};
272 UErrorCode status = U_ZERO_ERROR;
273 int32_t inLength=0, outLength=100;
274 char output[100] = {0};
275 punycode_status error;
276 u_strToUTF32((UChar32*)in,19,&inLength,str,19,&status);
277
278 error= punycode_encode(inLength, in, NULL, (uint32_t*)&outLength, output);
279 printf(output);
280
b75a7d8f 281 }
374ca955 282 */
b75a7d8f 283
374ca955
A
284 uint32_t i = 0;
285 int32_t unassigned = 0;
286 int32_t prohibited = 0;
287 int32_t mappedWithNorm = 0;
288 int32_t mapped = 0;
289 int32_t noValueInTrie = 0;
b75a7d8f 290
374ca955
A
291 UStringPrepType type;
292 int32_t value;
293 UBool isIndex = FALSE;
b75a7d8f 294
374ca955
A
295 for(i=0;i<=0x10FFFF;i++){
296 uint32_t result = 0;
297 UTRIE_GET16(idnTrie,i, result);
298 type = getValues(result,value, isIndex);
299 if(type != USPREP_TYPE_LIMIT ){
300 if(type == USPREP_UNASSIGNED){
301 unassigned++;
302 }
303 if(type == USPREP_PROHIBITED){
304 prohibited++;
305 }
306 if(type == USPREP_MAP){
307 mapped++;
308 }
309 }else{
310 noValueInTrie++;
311 if(result > 0){
312 test.errln("The return value for 0x%06X is wrong. %i\n",i,result);
b75a7d8f
A
313 }
314 }
b75a7d8f 315 }
b75a7d8f 316
374ca955
A
317 test.logln("Number of Unassinged code points : %i \n",unassigned);
318 test.logln("Number of Prohibited code points : %i \n",prohibited);
319 test.logln("Number of Mapped code points : %i \n",mapped);
320 test.logln("Number of Mapped with NFKC code points : %i \n",mappedWithNorm);
321 test.logln("Number of code points that have no value in Trie: %i \n",noValueInTrie);
322
323
b75a7d8f
A
324}
325
326static void
327compareMapping(uint32_t codepoint, uint32_t* mapping,int32_t mapLength,
374ca955
A
328 UStringPrepType type){
329 uint32_t result = 0;
330 UTRIE_GET16(idnTrie,codepoint, result);
b75a7d8f 331
374ca955
A
332 int32_t length=0;
333 UBool isIndex;
334 UStringPrepType retType;
335 int32_t value, index=0, delta=0;
336
337 retType = getValues(result,value,isIndex);
b75a7d8f
A
338
339
374ca955
A
340 if(type != retType && retType != USPREP_DELETE){
341
342 pTestIDNA->errln( "Did not get the assigned type for codepoint 0x%08X. Expected: %i Got: %i\n",codepoint, USPREP_MAP, type);
343
344 }
345
346 if(isIndex){
347 index = value;
348 if(index >= indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START] &&
349 index < indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]){
350 length = 1;
351 }else if(index >= indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START] &&
352 index < indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START]){
353 length = 2;
354 }else if(index >= indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] &&
355 index < indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]){
356 length = 3;
b75a7d8f 357 }else{
374ca955 358 length = mappingData[index++];
b75a7d8f 359 }
374ca955
A
360 }else{
361 delta = value;
362 length = (retType == USPREP_DELETE)? 0 : 1;
363 }
b75a7d8f 364
374ca955
A
365 int32_t realLength =0;
366 /* figure out the real length */
367 for(int32_t j=0; j<mapLength; j++){
368 if(mapping[j] > 0xFFFF){
369 realLength +=2;
370 }else{
371 realLength++;
372 }
373 }
b75a7d8f 374
374ca955
A
375 if(realLength != length){
376 pTestIDNA->errln( "Did not get the expected length. Expected: %i Got: %i\n", mapLength, length);
377 }
378
379 if(isIndex){
b75a7d8f
A
380 for(int8_t i =0; i< mapLength; i++){
381 if(mapping[i] <= 0xFFFF){
382 if(mappingData[index+i] != (uint16_t)mapping[i]){
383 pTestIDNA->errln("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[i], mappingData[index+i]);
384 }
385 }else{
4388f060
A
386 UChar lead = U16_LEAD(mapping[i]);
387 UChar trail = U16_TRAIL(mapping[i]);
b75a7d8f
A
388 if(mappingData[index+i] != lead ||
389 mappingData[index+i+1] != trail){
390 pTestIDNA->errln( "Did not get the expected result. Expected: 0x%04X 0x%04X Got: 0x%04X 0x%04X", lead, trail, mappingData[index+i], mappingData[index+i+1]);
391 }
392 }
393 }
374ca955
A
394 }else{
395 if(retType!=USPREP_DELETE && (codepoint-delta) != (uint16_t)mapping[0]){
396 pTestIDNA->errln("Did not get the expected result. Expected: 0x%04X Got: 0x%04X \n", mapping[0],(codepoint-delta));
397 }
b75a7d8f
A
398 }
399
400}
401
402static void
403compareFlagsForRange(uint32_t start, uint32_t end,
374ca955
A
404 UStringPrepType type){
405
406 uint32_t result =0 ;
407 UStringPrepType retType;
408 UBool isIndex=FALSE;
409 int32_t value=0;
4388f060 410/*
374ca955 411 // supplementary code point
4388f060 412 UChar __lead16=U16_LEAD(0x2323E);
374ca955
A
413 int32_t __offset;
414
415 // get data for lead surrogate
416 (result)=_UTRIE_GET_RAW((&idnTrie), index, 0, (__lead16));
417 __offset=(&idnTrie)->getFoldingOffset(result);
418
419 // get the real data from the folded lead/trail units
420 if(__offset>0) {
421 (result)=_UTRIE_GET_RAW((&idnTrie), index, __offset, (0x2323E)&0x3ff);
b75a7d8f 422 } else {
374ca955 423 (result)=(uint32_t)((&idnTrie)->initialValue);
b75a7d8f 424 }
b75a7d8f 425
374ca955
A
426 UTRIE_GET16(&idnTrie,0x2323E, result);
427*/
428 while(start < end+1){
429 UTRIE_GET16(idnTrie,start, result);
430 retType = getValues(result,value,isIndex);
431 if(result > _SPREP_TYPE_THRESHOLD){
432 if(retType != type){
433 pTestIDNA->errln( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]);
434 }
435 }else{
436 if(type == USPREP_PROHIBITED && ((result & 0x01) != 0x01)){
437 pTestIDNA->errln( "FAIL: Did not get the expected type for 0x%06X. Expected: %s Got: %s\n",start,usprepTypeNames[type], usprepTypeNames[retType]);
438 }
b75a7d8f 439 }
b75a7d8f 440
374ca955 441 start++;
b75a7d8f 442 }
374ca955 443
b75a7d8f
A
444}
445
374ca955 446
b75a7d8f
A
447#endif /* #if !UCONFIG_NO_IDNA */
448
449/*
450 * Hey, Emacs, please set the following:
451 *
452 * Local Variables:
453 * indent-tabs-mode: nil
454 * End:
455 *
456 */