2 **********************************************************************
3 * Copyright (C) 2011-2013, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 **********************************************************************
8 * IntlTestSpoof tests for USpoofDetector
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_FILE_IO
17 #include "unicode/normlzr.h"
18 #include "unicode/regex.h"
19 #include "unicode/unistr.h"
20 #include "unicode/uscript.h"
21 #include "unicode/uspoof.h"
24 #include "identifier_info.h"
25 #include "scriptset.h"
31 #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \
32 errcheckln(status, "Failure at file %s, line %d, error = %s", __FILE__, __LINE__, u_errorName(status));}}
34 #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \
35 errln("Test Failure at file %s, line %d: \"%s\" is false.", __FILE__, __LINE__, #expr);};}
37 #define TEST_ASSERT_MSG(expr, msg) {if ((expr)==FALSE) { \
38 dataerrln("Test Failure at file %s, line %d, %s: \"%s\" is false.", __FILE__, __LINE__, msg, #expr);};}
40 #define TEST_ASSERT_EQ(a, b) { if ((a) != (b)) { \
41 errln("Test Failure at file %s, line %d: \"%s\" (%d) != \"%s\" (%d)", \
42 __FILE__, __LINE__, #a, (a), #b, (b)); }}
44 #define TEST_ASSERT_NE(a, b) { if ((a) == (b)) { \
45 errln("Test Failure at file %s, line %d: \"%s\" (%d) == \"%s\" (%d)", \
46 __FILE__, __LINE__, #a, (a), #b, (b)); }}
48 #define LENGTHOF(array) ((int32_t)(sizeof(array)/sizeof((array)[0])))
51 * TEST_SETUP and TEST_TEARDOWN
52 * macros to handle the boilerplate around setting up test case.
53 * Put arbitrary test code between SETUP and TEARDOWN.
54 * "sc" is the ready-to-go SpoofChecker for use in the tests.
56 #define TEST_SETUP { \
57 UErrorCode status = U_ZERO_ERROR; \
59 sc = uspoof_open(&status); \
60 TEST_ASSERT_SUCCESS(status); \
61 if (U_SUCCESS(status)){
63 #define TEST_TEARDOWN \
65 TEST_ASSERT_SUCCESS(status); \
72 void IntlTestSpoof::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
74 if (exec
) logln("TestSuite spoof: ");
77 name
= "TestSpoofAPI";
83 name
= "TestSkeleton";
89 name
= "TestAreConfusable";
95 name
= "TestInvisible";
101 name
= "testConfData";
107 name
= "testBug8654";
113 name
= "testIdentifierInfo";
115 testIdentifierInfo();
119 name
= "testScriptSet";
125 name
= "testRestrictionLevel";
127 testRestrictionLevel();
131 name
= "testMixedNumbers";
138 default: name
=""; break;
142 void IntlTestSpoof::testSpoofAPI() {
145 UnicodeString
s("xyz"); // Many latin ranges are whole-script confusable with other scripts.
146 // If this test starts failing, consult confusablesWholeScript.txt
147 int32_t position
= 666;
148 int32_t checkResults
= uspoof_checkUnicodeString(sc
, s
, &position
, &status
);
149 TEST_ASSERT_SUCCESS(status
);
150 TEST_ASSERT_EQ(0, checkResults
);
151 TEST_ASSERT_EQ(0, position
);
155 UnicodeString
s1("cxs");
156 UnicodeString s2
= UnicodeString("\\u0441\\u0445\\u0455").unescape(); // Cyrillic "cxs"
157 int32_t checkResults
= uspoof_areConfusableUnicodeString(sc
, s1
, s2
, &status
);
158 TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE
| USPOOF_WHOLE_SCRIPT_CONFUSABLE
, checkResults
);
163 UnicodeString
s("I1l0O");
165 UnicodeString
&retStr
= uspoof_getSkeletonUnicodeString(sc
, USPOOF_ANY_CASE
, s
, dest
, &status
);
166 TEST_ASSERT_SUCCESS(status
);
167 TEST_ASSERT(UnicodeString("lllOO") == dest
);
168 TEST_ASSERT(&dest
== &retStr
);
173 #define CHECK_SKELETON(type, input, expected) { \
174 checkSkeleton(sc, type, input, expected, __LINE__); \
178 // testSkeleton. Spot check a number of confusable skeleton substitutions from the
179 // Unicode data file confusables.txt
180 // Test cases chosen for substitutions of various lengths, and
181 // membership in different mapping tables.
182 void IntlTestSpoof::testSkeleton() {
183 const uint32_t ML
= 0;
184 const uint32_t SL
= USPOOF_SINGLE_SCRIPT_CONFUSABLE
;
185 const uint32_t MA
= USPOOF_ANY_CASE
;
186 const uint32_t SA
= USPOOF_SINGLE_SCRIPT_CONFUSABLE
| USPOOF_ANY_CASE
;
189 // A long "identifier" that will overflow implementation stack buffers, forcing heap allocations.
190 CHECK_SKELETON(SL
, " A 1ong \\u02b9identifier' that will overflow implementation stack buffers, forcing heap allocations."
191 " A 1ong 'identifier' that will overflow implementation stack buffers, forcing heap allocations."
192 " A 1ong 'identifier' that will overflow implementation stack buffers, forcing heap allocations."
193 " A 1ong 'identifier' that will overflow implementation stack buffers, forcing heap allocations.",
195 " A long 'identifier' that vvill overflovv irnplernentation stack buffers, forcing heap allocations."
196 " A long 'identifier' that vvill overflovv irnplernentation stack buffers, forcing heap allocations."
197 " A long 'identifier' that vvill overflovv irnplernentation stack buffers, forcing heap allocations."
198 " A long 'identifier' that vvill overflovv irnplernentation stack buffers, forcing heap allocations.")
200 CHECK_SKELETON(SL
, "nochange", "nochange");
201 CHECK_SKELETON(MA
, "love", "love");
202 CHECK_SKELETON(MA
, "1ove", "love"); // Digit 1 to letter l
203 CHECK_SKELETON(ML
, "OOPS", "OOPS");
204 CHECK_SKELETON(ML
, "00PS", "00PS"); // Digit 0 unchanged in lower case mode.
205 CHECK_SKELETON(MA
, "OOPS", "OOPS");
206 CHECK_SKELETON(MA
, "00PS", "OOPS"); // Digit 0 to letter O in any case mode only
207 CHECK_SKELETON(SL
, "\\u059c", "\\u0301");
208 CHECK_SKELETON(SL
, "\\u2A74", "\\u003A\\u003A\\u003D");
209 CHECK_SKELETON(SL
, "\\u247E", "\\u0028\\u006C\\u006C\\u0029"); // "(ll)"
210 CHECK_SKELETON(SL
, "\\uFDFB", "\\u062C\\u0644\\u0020\\u062C\\u0644\\u0627\\u0644\\u0647");
212 // This mapping exists in the ML and MA tables, does not exist in SL, SA
214 CHECK_SKELETON(SL
, "\\u0C83", "\\u0C83");
215 CHECK_SKELETON(SA
, "\\u0C83", "\\u0C83");
216 CHECK_SKELETON(ML
, "\\u0C83", "\\u0983");
217 CHECK_SKELETON(MA
, "\\u0C83", "\\u0983");
220 // This mapping exists only in the MA table.
221 CHECK_SKELETON(MA
, "\\u0391", "A");
222 CHECK_SKELETON(SA
, "\\u0391", "\\u0391");
223 CHECK_SKELETON(ML
, "\\u0391", "\\u0391");
224 CHECK_SKELETON(SL
, "\\u0391", "\\u0391");
227 // This mapping exists in the ML and MA tables
228 CHECK_SKELETON(ML
, "\\u13CF", "b");
229 CHECK_SKELETON(MA
, "\\u13CF", "b");
230 CHECK_SKELETON(SL
, "\\u13CF", "\\u13CF");
231 CHECK_SKELETON(SA
, "\\u13CF", "\\u13CF");
233 // 0022 ; 0027 0027 ;
235 CHECK_SKELETON(SL
, "\\u0022", "\\u0027\\u0027");
236 CHECK_SKELETON(SA
, "\\u0022", "\\u0027\\u0027");
237 CHECK_SKELETON(ML
, "\\u0022", "\\u0027\\u0027");
238 CHECK_SKELETON(MA
, "\\u0022", "\\u0027\\u0027");
241 // This mapping exists in the SA and MA tables
242 CHECK_SKELETON(MA
, "\\u017F", "f");
243 CHECK_SKELETON(SA
, "\\u017F", "f");
250 // Run a single confusable skeleton transformation test case.
252 void IntlTestSpoof::checkSkeleton(const USpoofChecker
*sc
, uint32_t type
,
253 const char *input
, const char *expected
, int32_t lineNum
) {
254 UnicodeString uInput
= UnicodeString(input
).unescape();
255 UnicodeString uExpected
= UnicodeString(expected
).unescape();
257 UErrorCode status
= U_ZERO_ERROR
;
258 UnicodeString actual
;
259 uspoof_getSkeletonUnicodeString(sc
, type
, uInput
, actual
, &status
);
260 if (U_FAILURE(status
)) {
261 errln("File %s, Line %d, Test case from line %d, status is %s", __FILE__
, __LINE__
, lineNum
,
262 u_errorName(status
));
265 if (uExpected
!= actual
) {
266 errln("File %s, Line %d, Test case from line %d, Actual and Expected skeletons differ.",
267 __FILE__
, __LINE__
, lineNum
);
268 errln(UnicodeString(" Actual Skeleton: \"") + actual
+ UnicodeString("\"\n") +
269 UnicodeString(" Expected Skeleton: \"") + uExpected
+ UnicodeString("\""));
273 void IntlTestSpoof::testAreConfusable() {
275 UnicodeString
s1("A long string that will overflow stack buffers. A long string that will overflow stack buffers. "
276 "A long string that will overflow stack buffers. A long string that will overflow stack buffers. ");
277 UnicodeString
s2("A long string that wi11 overflow stack buffers. A long string that will overflow stack buffers. "
278 "A long string that wi11 overflow stack buffers. A long string that will overflow stack buffers. ");
279 TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE
, uspoof_areConfusableUnicodeString(sc
, s1
, s2
, &status
));
280 TEST_ASSERT_SUCCESS(status
);
285 void IntlTestSpoof::testInvisible() {
287 UnicodeString s
= UnicodeString("abcd\\u0301ef").unescape();
288 int32_t position
= -42;
289 TEST_ASSERT_EQ(0, uspoof_checkUnicodeString(sc
, s
, &position
, &status
));
290 TEST_ASSERT_SUCCESS(status
);
291 TEST_ASSERT(0 == position
);
293 UnicodeString s2
= UnicodeString("abcd\\u0301\\u0302\\u0301ef").unescape();
294 TEST_ASSERT_EQ(USPOOF_INVISIBLE
, uspoof_checkUnicodeString(sc
, s2
, &position
, &status
));
295 TEST_ASSERT_SUCCESS(status
);
296 TEST_ASSERT_EQ(0, position
);
298 // Two acute accents, one from the composed a with acute accent, \u00e1,
301 UnicodeString s3
= UnicodeString("abcd\\u00e1\\u0301xyz").unescape();
302 TEST_ASSERT_EQ(USPOOF_INVISIBLE
, uspoof_checkUnicodeString(sc
, s3
, &position
, &status
));
303 TEST_ASSERT_SUCCESS(status
);
304 TEST_ASSERT_EQ(0, position
);
308 void IntlTestSpoof::testBug8654() {
310 UnicodeString s
= UnicodeString("B\\u00c1\\u0301").unescape();
311 int32_t position
= -42;
312 TEST_ASSERT_EQ(USPOOF_INVISIBLE
, uspoof_checkUnicodeString(sc
, s
, &position
, &status
) & USPOOF_INVISIBLE
);
313 TEST_ASSERT_SUCCESS(status
);
314 TEST_ASSERT_EQ(0, position
);
318 static UnicodeString
parseHex(const UnicodeString
&in
) {
319 // Convert a series of hex numbers in a Unicode String to a string with the
320 // corresponding characters.
321 // The conversion is _really_ annoying. There must be some function to just do it.
322 UnicodeString result
;
324 for (int32_t i
=0; i
<in
.length(); i
++) {
325 UChar c
= in
.charAt(i
);
326 if (c
== 0x20) { // Space
331 } else if (c
>=0x30 && c
<=0x39) {
332 cc
= (cc
<<4) + (c
- 0x30);
333 } else if ((c
>=0x41 && c
<=0x46) || (c
>=0x61 && c
<=0x66)) {
334 cc
= (cc
<<4) + (c
& 0x0f)+9;
336 // else do something with bad input.
346 // Append the hex form of a UChar32 to a UnicodeString.
347 // Used in formatting error messages.
348 // Match the formatting of numbers in confusables.txt
349 // Minimum of 4 digits, no leading zeroes for positions 5 and up.
351 static void appendHexUChar(UnicodeString
&dest
, UChar32 c
) {
352 UBool doZeroes
= FALSE
;
353 for (int bitNum
=28; bitNum
>=0; bitNum
-=4) {
357 int hexDigit
= (c
>>bitNum
) & 0x0f;
358 if (hexDigit
!= 0 || doZeroes
) {
360 dest
.append((UChar
)(hexDigit
<=9? hexDigit
+ 0x30: hexDigit
-10 + 0x41));
363 dest
.append((UChar
)0x20);
366 U_DEFINE_LOCAL_OPEN_POINTER(LocalStdioFilePointer
, FILE, fclose
);
368 // testConfData - Check each data item from the Unicode confusables.txt file,
369 // verify that it transforms correctly in a skeleton.
371 void IntlTestSpoof::testConfData() {
372 UErrorCode status
= U_ZERO_ERROR
;
374 const char *testDataDir
= IntlTest::getSourceTestData(status
);
375 TEST_ASSERT_SUCCESS(status
);
377 uprv_strcpy(buffer
, testDataDir
);
378 uprv_strcat(buffer
, "confusables.txt");
380 LocalStdioFilePointer
f(fopen(buffer
, "rb"));
382 errln("Skipping test spoof/testConfData. File confusables.txt not accessible.");
385 fseek(f
.getAlias(), 0, SEEK_END
);
386 int32_t fileSize
= ftell(f
.getAlias());
387 LocalArray
<char> fileBuf(new char[fileSize
]);
388 fseek(f
.getAlias(), 0, SEEK_SET
);
389 int32_t amt_read
= fread(fileBuf
.getAlias(), 1, fileSize
, f
.getAlias());
390 TEST_ASSERT_EQ(amt_read
, fileSize
);
391 TEST_ASSERT(fileSize
>0);
392 if (amt_read
!= fileSize
|| fileSize
<=0) {
395 UnicodeString confusablesTxt
= UnicodeString::fromUTF8(StringPiece(fileBuf
.getAlias(), fileSize
));
397 LocalUSpoofCheckerPointer
sc(uspoof_open(&status
));
398 TEST_ASSERT_SUCCESS(status
);
400 // Parse lines from the confusables.txt file. Example Line:
401 // FF44 ; 0064 ; SL # ( d -> d ) FULLWIDTH ....
402 // Three fields. The hex fields can contain more than one character,
403 // and each character may be more than 4 digits (for supplemntals)
404 // This regular expression matches lines and splits the fields into capture groups.
405 RegexMatcher
parseLine("(?m)^([0-9A-F]{4}[^#;]*?);([^#;]*?);([^#]*)", confusablesTxt
, 0, status
);
406 TEST_ASSERT_SUCCESS(status
);
407 while (parseLine
.find()) {
408 UnicodeString from
= parseHex(parseLine
.group(1, status
));
409 if (!Normalizer::isNormalized(from
, UNORM_NFD
, status
)) {
410 // The source character was not NFD.
411 // Skip this case; the first step in obtaining a skeleton is to NFD the input,
412 // so the mapping in this line of confusables.txt will never be applied.
416 UnicodeString rawExpected
= parseHex(parseLine
.group(2, status
));
417 UnicodeString expected
;
418 Normalizer::decompose(rawExpected
, FALSE
/*NFD*/, 0, expected
, status
);
419 TEST_ASSERT_SUCCESS(status
);
421 int32_t skeletonType
= 0;
422 UnicodeString tableType
= parseLine
.group(3, status
);
423 TEST_ASSERT_SUCCESS(status
);
424 if (tableType
.indexOf("SL") >= 0) {
425 skeletonType
= USPOOF_SINGLE_SCRIPT_CONFUSABLE
;
426 } else if (tableType
.indexOf("SA") >= 0) {
427 skeletonType
= USPOOF_SINGLE_SCRIPT_CONFUSABLE
| USPOOF_ANY_CASE
;
428 } else if (tableType
.indexOf("ML") >= 0) {
430 } else if (tableType
.indexOf("MA") >= 0) {
431 skeletonType
= USPOOF_ANY_CASE
;
434 UnicodeString actual
;
435 uspoof_getSkeletonUnicodeString(sc
.getAlias(), skeletonType
, from
, actual
, &status
);
436 TEST_ASSERT_SUCCESS(status
);
437 TEST_ASSERT(actual
== expected
);
438 if (actual
!= expected
) {
439 errln(parseLine
.group(0, status
));
440 UnicodeString line
= "Actual: ";
442 while (i
< actual
.length()) {
443 appendHexUChar(line
, actual
.char32At(i
));
444 i
= actual
.moveIndex32(i
, 1);
448 if (U_FAILURE(status
)) {
454 // testIdentifierInfo. Note that IdentifierInfo is not public ICU API at this time
455 void IntlTestSpoof::testIdentifierInfo() {
456 UErrorCode status
= U_ZERO_ERROR
;
457 ScriptSet bitset12
; bitset12
.set(USCRIPT_LATIN
, status
).set(USCRIPT_HANGUL
, status
);
458 ScriptSet bitset2
; bitset2
.set(USCRIPT_HANGUL
, status
);
459 TEST_ASSERT(bitset12
.contains(bitset2
));
460 TEST_ASSERT(bitset12
.contains(bitset12
));
461 TEST_ASSERT(!bitset2
.contains(bitset12
));
463 ScriptSet arabSet
; arabSet
.set(USCRIPT_ARABIC
, status
);
464 ScriptSet latinSet
; latinSet
.set(USCRIPT_LATIN
, status
);
465 UElement arabEl
; arabEl
.pointer
= &arabSet
;
466 UElement latinEl
; latinEl
.pointer
= &latinSet
;
467 TEST_ASSERT(uhash_compareScriptSet(arabEl
, latinEl
) < 0);
468 TEST_ASSERT(uhash_compareScriptSet(latinEl
, arabEl
) > 0);
470 UnicodeString scriptString
;
471 bitset12
.displayScripts(scriptString
);
472 TEST_ASSERT(UNICODE_STRING_SIMPLE("Hang Latn") == scriptString
);
474 status
= U_ZERO_ERROR
;
475 UHashtable
*alternates
= uhash_open(uhash_hashScriptSet
,uhash_compareScriptSet
, NULL
, &status
);
476 uhash_puti(alternates
, &bitset12
, 1, &status
);
477 uhash_puti(alternates
, &bitset2
, 1, &status
);
478 UnicodeString alternatesString
;
479 IdentifierInfo::displayAlternates(alternatesString
, alternates
, status
);
480 TEST_ASSERT(UNICODE_STRING_SIMPLE("Hang; Hang Latn") == alternatesString
);
481 TEST_ASSERT_SUCCESS(status
);
483 status
= U_ZERO_ERROR
;
484 ScriptSet tScriptSet
;
485 tScriptSet
.parseScripts(scriptString
, status
);
486 TEST_ASSERT_SUCCESS(status
);
487 TEST_ASSERT(bitset12
== tScriptSet
);
490 uhash_close(alternates
);
493 const char *fTestString
;
494 URestrictionLevel fRestrictionLevel
;
495 const char *fNumerics
;
496 const char *fScripts
;
497 const char *fAlternates
;
498 const char *fCommonAlternates
;
500 {"\\u0061\\u2665", USPOOF_UNRESTRICTIVE
, "[]", "Latn", "", ""},
501 {"\\u0061\\u3006", USPOOF_HIGHLY_RESTRICTIVE
, "[]", "Latn", "Hani Hira Kana", "Hani Hira Kana"},
502 {"\\u0061\\u30FC\\u3006", USPOOF_HIGHLY_RESTRICTIVE
, "[]", "Latn", "Hira Kana", "Hira Kana"},
503 {"\\u0061\\u30FC\\u3006\\u30A2", USPOOF_HIGHLY_RESTRICTIVE
, "[]", "Latn Kana", "", ""},
504 {"\\u30A2\\u0061\\u30FC\\u3006", USPOOF_HIGHLY_RESTRICTIVE
, "[]", "Latn Kana", "", ""},
505 {"\\u0061\\u0031\\u0661", USPOOF_UNRESTRICTIVE
, "[\\u0030\\u0660]", "Latn", "Arab Thaa", "Arab Thaa"},
506 {"\\u0061\\u0031\\u0661\\u06F1", USPOOF_UNRESTRICTIVE
, "[\\u0030\\u0660\\u06F0]", "Latn Arab", "", ""},
507 {"\\u0661\\u30FC\\u3006\\u0061\\u30A2\\u0031\\u0967\\u06F1", USPOOF_UNRESTRICTIVE
,
508 "[\\u0030\\u0660\\u06F0\\u0966]", "Latn Kana Arab Deva", "", ""},
509 {"\\u0061\\u30A2\\u30FC\\u3006\\u0031\\u0967\\u0661\\u06F1", USPOOF_UNRESTRICTIVE
,
510 "[\\u0030\\u0660\\u06F0\\u0966]", "Latn Kana Arab Deva", "", ""}
514 for (testNum
= 0; testNum
< LENGTHOF(tests
); testNum
++) {
516 sprintf(testNumStr
, "testNum = %d", testNum
);
517 Test
&test
= tests
[testNum
];
518 status
= U_ZERO_ERROR
;
519 UnicodeString
testString(test
.fTestString
); // Note: may do charset conversion.
520 testString
= testString
.unescape();
521 IdentifierInfo
idInfo(status
);
522 TEST_ASSERT_SUCCESS(status
);
523 idInfo
.setIdentifierProfile(*uspoof_getRecommendedUnicodeSet(&status
));
524 idInfo
.setIdentifier(testString
, status
);
525 TEST_ASSERT_MSG(*idInfo
.getIdentifier() == testString
, testNumStr
);
527 URestrictionLevel restrictionLevel
= test
.fRestrictionLevel
;
528 TEST_ASSERT_MSG(restrictionLevel
== idInfo
.getRestrictionLevel(status
), testNumStr
);
530 status
= U_ZERO_ERROR
;
531 UnicodeSet
numerics(UnicodeString(test
.fNumerics
).unescape(), status
);
532 TEST_ASSERT_SUCCESS(status
);
533 TEST_ASSERT_MSG(numerics
== *idInfo
.getNumerics(), testNumStr
);
536 scripts
.parseScripts(UnicodeString(test
.fScripts
), status
);
537 TEST_ASSERT_MSG(scripts
== *idInfo
.getScripts(), testNumStr
);
539 UnicodeString alternatesStr
;
540 IdentifierInfo::displayAlternates(alternatesStr
, idInfo
.getAlternates(), status
);
541 TEST_ASSERT_MSG(UnicodeString(test
.fAlternates
) == alternatesStr
, testNumStr
);
543 ScriptSet commonAlternates
;
544 commonAlternates
.parseScripts(UnicodeString(test
.fCommonAlternates
), status
);
545 TEST_ASSERT_MSG(commonAlternates
== *idInfo
.getCommonAmongAlternates(), testNumStr
);
548 // Test of getScriptCount()
549 // Script and or Script Extension for chars used in the tests
550 // \\u3013 ; Bopo Hang Hani Hira Kana # So GETA MARK
551 // \\uA838 ; Deva Gujr Guru Kthi Takr # Sc NORTH INDIC RUPEE MARK
552 // \\u0951 ; Deva Latn # Mn DEVANAGARI STRESS SIGN UDATTA
554 // \\u0370 ; Greek # L GREEK CAPITAL LETTER HETA
555 // \\u0481 ; Cyrillic # L& CYRILLIC SMALL LETTER KOPPA
556 // \\u0904 ; Devanagari # Lo DEVANAGARI LETTER SHORT A
557 // \\u3041 ; Hiragana # Lo HIRAGANA LETTER SMALL A
558 // 1234 ; Common # ascii digits
559 // \\u0300 ; Inherited # Mn COMBINING GRAVE ACCENT
562 const char *fTestString
;
563 int32_t fScriptCount
;
568 {"Hello1234\\u0300", 1}, // Common and Inherited are ignored.
572 {"\\uA838\\u0951", 1}, // Triggers commonAmongAlternates path.
573 {"\\u3013\\uA838", 2}
576 status
= U_ZERO_ERROR
;
577 IdentifierInfo
identifierInfo(status
);
578 for (testNum
=0; testNum
<LENGTHOF(scriptTests
); testNum
++) {
579 ScriptTest
&test
= scriptTests
[testNum
];
581 sprintf(msgBuf
, "testNum = %d ", testNum
);
582 UnicodeString testString
= UnicodeString(test
.fTestString
).unescape();
584 status
= U_ZERO_ERROR
;
585 identifierInfo
.setIdentifier(testString
, status
);
586 int32_t scriptCount
= identifierInfo
.getScriptCount();
587 TEST_ASSERT_MSG(test
.fScriptCount
== scriptCount
, msgBuf
);
591 void IntlTestSpoof::testScriptSet() {
594 UErrorCode status
= U_ZERO_ERROR
;
596 TEST_ASSERT(s1
== s2
);
597 s1
.set(USCRIPT_ARABIC
,status
);
598 TEST_ASSERT_SUCCESS(status
);
599 TEST_ASSERT(!(s1
== s2
));
600 TEST_ASSERT(s1
.test(USCRIPT_ARABIC
, status
));
601 TEST_ASSERT(s1
.test(USCRIPT_GREEK
, status
) == FALSE
);
603 status
= U_ZERO_ERROR
;
604 s1
.reset(USCRIPT_ARABIC
, status
);
605 TEST_ASSERT(s1
== s2
);
607 status
= U_ZERO_ERROR
;
609 TEST_ASSERT(s1
.test(USCRIPT_COMMON
, status
));
610 TEST_ASSERT(s1
.test(USCRIPT_ETHIOPIC
, status
));
611 TEST_ASSERT(s1
.test(USCRIPT_CODE_LIMIT
, status
));
613 TEST_ASSERT(!s1
.test(USCRIPT_COMMON
, status
));
614 TEST_ASSERT(!s1
.test(USCRIPT_ETHIOPIC
, status
));
615 TEST_ASSERT(!s1
.test(USCRIPT_CODE_LIMIT
, status
));
617 status
= U_ZERO_ERROR
;
618 s1
.set(USCRIPT_TAKRI
, status
);
619 s1
.set(USCRIPT_BLISSYMBOLS
, status
);
621 TEST_ASSERT(s2
.contains(s1
));
622 TEST_ASSERT(!s1
.contains(s2
));
623 TEST_ASSERT(s2
.intersects(s1
));
624 TEST_ASSERT(s1
.intersects(s2
));
625 s2
.reset(USCRIPT_TAKRI
, status
);
626 TEST_ASSERT(!s2
.contains(s1
));
627 TEST_ASSERT(!s1
.contains(s2
));
628 TEST_ASSERT(s1
.intersects(s2
));
629 TEST_ASSERT(s2
.intersects(s1
));
630 TEST_ASSERT_SUCCESS(status
);
632 status
= U_ZERO_ERROR
;
634 s1
.set(USCRIPT_NKO
, status
);
635 s1
.set(USCRIPT_COMMON
, status
);
637 TEST_ASSERT(s2
== s1
);
638 TEST_ASSERT_EQ(2, s2
.countMembers());
640 TEST_ASSERT(s2
== s1
);
642 TEST_ASSERT(!(s2
== s1
));
643 TEST_ASSERT(s2
.countMembers() >= USCRIPT_CODE_LIMIT
);
645 TEST_ASSERT(s2
== s1
);
648 s2
.reset(USCRIPT_COMMON
, status
);
650 TEST_ASSERT(s2
.countMembers() == 1);
653 s1
.set(USCRIPT_AFAKA
, status
);
654 s1
.set(USCRIPT_VAI
, status
);
655 s1
.set(USCRIPT_INHERITED
, status
);
657 for (int32_t i
=0; i
<4; i
++) {
658 n
= s1
.nextSetBit(n
+1);
660 case 0: TEST_ASSERT_EQ(USCRIPT_INHERITED
, n
); break;
661 case 1: TEST_ASSERT_EQ(USCRIPT_VAI
, n
); break;
662 case 2: TEST_ASSERT_EQ(USCRIPT_AFAKA
, n
); break;
663 case 3: TEST_ASSERT_EQ(-1, (int32_t)n
); break;
664 default: TEST_ASSERT(FALSE
);
667 TEST_ASSERT_SUCCESS(status
);
671 void IntlTestSpoof::testRestrictionLevel() {
674 URestrictionLevel fExpectedRestrictionLevel
;
676 {"\\u0061\\u03B3\\u2665", USPOOF_UNRESTRICTIVE
},
678 {"\\u03B3", USPOOF_HIGHLY_RESTRICTIVE
},
679 {"\\u0061\\u30A2\\u30FC", USPOOF_HIGHLY_RESTRICTIVE
},
680 {"\\u0061\\u0904", USPOOF_MODERATELY_RESTRICTIVE
},
681 {"\\u0061\\u03B3", USPOOF_MINIMALLY_RESTRICTIVE
}
685 URestrictionLevel restrictionLevels
[] = { USPOOF_ASCII
, USPOOF_HIGHLY_RESTRICTIVE
,
686 USPOOF_MODERATELY_RESTRICTIVE
, USPOOF_MINIMALLY_RESTRICTIVE
, USPOOF_UNRESTRICTIVE
};
688 UErrorCode status
= U_ZERO_ERROR
;
689 IdentifierInfo
idInfo(status
);
690 TEST_ASSERT_SUCCESS(status
);
691 idInfo
.setIdentifierProfile(*uspoof_getRecommendedUnicodeSet(&status
));
692 TEST_ASSERT_SUCCESS(status
);
693 for (int32_t testNum
=0; testNum
< LENGTHOF(tests
); testNum
++) {
694 status
= U_ZERO_ERROR
;
695 const Test
&test
= tests
[testNum
];
696 UnicodeString testString
= UnicodeString(test
.fId
).unescape();
697 URestrictionLevel expectedLevel
= test
.fExpectedRestrictionLevel
;
698 idInfo
.setIdentifier(testString
, status
);
699 sprintf(msgBuffer
, "testNum = %d ", testNum
);
700 TEST_ASSERT_SUCCESS(status
);
701 TEST_ASSERT_MSG(expectedLevel
== idInfo
.getRestrictionLevel(status
), msgBuffer
);
702 for (int levelIndex
=0; levelIndex
<LENGTHOF(restrictionLevels
); levelIndex
++) {
703 status
= U_ZERO_ERROR
;
704 URestrictionLevel levelSetInSpoofChecker
= restrictionLevels
[levelIndex
];
705 USpoofChecker
*sc
= uspoof_open(&status
);
706 uspoof_setChecks(sc
, USPOOF_RESTRICTION_LEVEL
, &status
);
707 uspoof_setAllowedChars(sc
, uspoof_getRecommendedSet(&status
), &status
);
708 uspoof_setRestrictionLevel(sc
, levelSetInSpoofChecker
);
709 UBool actualValue
= uspoof_checkUnicodeString(sc
, testString
, NULL
, &status
) != 0;
711 // we want to fail if the text is (say) MODERATE and the testLevel is ASCII
712 UBool expectedFailure
= expectedLevel
> levelSetInSpoofChecker
||
713 !uspoof_getRecommendedUnicodeSet(&status
)->containsAll(testString
);
714 sprintf(msgBuffer
, "testNum = %d, levelIndex = %d", testNum
, levelIndex
);
715 TEST_ASSERT_MSG(expectedFailure
== actualValue
, msgBuffer
);
716 TEST_ASSERT_SUCCESS(status
);
723 void IntlTestSpoof::testMixedNumbers() {
725 const char *fTestString
;
726 const char *fExpectedSet
;
729 {"\\u0967", "[\\u0966]"},
730 {"1\\u0967", "[0\\u0966]"},
731 {"\\u0661\\u06F1", "[\\u0660\\u06F0]"}
733 UErrorCode status
= U_ZERO_ERROR
;
734 IdentifierInfo
idInfo(status
);
735 for (int32_t testNum
=0; testNum
< LENGTHOF(tests
); testNum
++) {
737 sprintf(msgBuf
, "testNum = %d ", testNum
);
738 Test
&test
= tests
[testNum
];
740 status
= U_ZERO_ERROR
;
741 UnicodeString testString
= UnicodeString(test
.fTestString
).unescape();
742 UnicodeSet
expectedSet(UnicodeString(test
.fExpectedSet
).unescape(), status
);
743 idInfo
.setIdentifier(testString
, status
);
744 TEST_ASSERT_SUCCESS(status
);
745 TEST_ASSERT_MSG(expectedSet
== *idInfo
.getNumerics(), msgBuf
);
747 status
= U_ZERO_ERROR
;
748 USpoofChecker
*sc
= uspoof_open(&status
);
749 uspoof_setChecks(sc
, USPOOF_MIXED_NUMBERS
, &status
); // only check this
750 int32_t result
= uspoof_checkUnicodeString(sc
, testString
, NULL
, &status
);
751 UBool mixedNumberFailure
= ((result
& USPOOF_MIXED_NUMBERS
) != 0);
752 TEST_ASSERT_MSG((expectedSet
.size() > 1) == mixedNumberFailure
, msgBuf
);
757 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_FILE_IO */