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