Imported Upstream version 58.1
[platform/upstream/icu.git] / source / test / intltest / itspoof.cpp
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (C) 2011-2015, International Business Machines Corporation 
6 * and others.  All Rights Reserved.
7 **********************************************************************
8 */
9 /**
10  * IntlTestSpoof tests for USpoofDetector
11  */
12
13 #include "unicode/utypes.h"
14
15 #if !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_FILE_IO
16
17 #include "itspoof.h"
18
19 #include "unicode/normlzr.h"
20 #include "unicode/regex.h"
21 #include "unicode/unistr.h"
22 #include "unicode/uscript.h"
23 #include "unicode/uspoof.h"
24
25 #include "cstring.h"
26 #include "scriptset.h"
27 #include "uhash.h"
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \
33     errcheckln(status, "Failure at file %s, line %d, error = %s", __FILE__, __LINE__, u_errorName(status));}}
34
35 #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \
36     errln("Test Failure at file %s, line %d: \"%s\" is false.", __FILE__, __LINE__, #expr);};}
37
38 #define TEST_ASSERT_MSG(expr, msg) {if ((expr)==FALSE) { \
39     dataerrln("Test Failure at file %s, line %d, %s: \"%s\" is false.", __FILE__, __LINE__, msg, #expr);};}
40
41 #define TEST_ASSERT_EQ(a, b) { if ((a) != (b)) { \
42     errln("Test Failure at file %s, line %d: \"%s\" (%d) != \"%s\" (%d)", \
43              __FILE__, __LINE__, #a, (a), #b, (b)); }}
44
45 #define TEST_ASSERT_NE(a, b) { if ((a) == (b)) { \
46     errln("Test Failure at file %s, line %d: \"%s\" (%d) == \"%s\" (%d)", \
47              __FILE__, __LINE__, #a, (a), #b, (b)); }}
48
49 /*
50  *   TEST_SETUP and TEST_TEARDOWN
51  *         macros to handle the boilerplate around setting up test case.
52  *         Put arbitrary test code between SETUP and TEARDOWN.
53  *         "sc" is the ready-to-go  SpoofChecker for use in the tests.
54  */
55 #define TEST_SETUP {  \
56     UErrorCode status = U_ZERO_ERROR; \
57     USpoofChecker *sc;     \
58     sc = uspoof_open(&status);  \
59     TEST_ASSERT_SUCCESS(status);   \
60     USpoofCheckResult *checkResult; \
61     checkResult = uspoof_openCheckResult(&status); \
62     TEST_ASSERT_SUCCESS(status);   \
63     if (U_SUCCESS(status)){
64
65 #define TEST_TEARDOWN  \
66     }  \
67     TEST_ASSERT_SUCCESS(status);  \
68     uspoof_closeCheckResult(checkResult); \
69     uspoof_close(sc);  \
70 }
71
72
73
74
75 void IntlTestSpoof::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
76 {
77     if (exec) {
78         logln("TestSuite spoof: ");
79     }
80     TESTCASE_AUTO_BEGIN;
81     TESTCASE_AUTO(testSpoofAPI);
82     TESTCASE_AUTO(testSkeleton);
83     TESTCASE_AUTO(testAreConfusable);
84     TESTCASE_AUTO(testInvisible);
85     TESTCASE_AUTO(testConfData);
86     TESTCASE_AUTO(testBug8654);
87     TESTCASE_AUTO(testScriptSet);
88     TESTCASE_AUTO(testRestrictionLevel);
89     TESTCASE_AUTO(testMixedNumbers);
90     TESTCASE_AUTO(testBug12153);
91     TESTCASE_AUTO_END;
92 }
93
94 void IntlTestSpoof::testSpoofAPI() {
95
96     TEST_SETUP
97         UnicodeString s("xyz");  // Many latin ranges are whole-script confusable with other scripts.
98                                  // If this test starts failing, consult confusablesWholeScript.txt
99         int32_t position = 666;
100         int32_t checkResults = uspoof_checkUnicodeString(sc, s, &position, &status);
101         TEST_ASSERT_SUCCESS(status);
102         TEST_ASSERT_EQ(0, checkResults);
103         TEST_ASSERT_EQ(0, position);
104     TEST_TEARDOWN;
105     
106     TEST_SETUP
107         UnicodeString s1("cxs");
108         UnicodeString s2 = UnicodeString("\\u0441\\u0445\\u0455").unescape();  // Cyrillic "cxs"
109         int32_t checkResults = uspoof_areConfusableUnicodeString(sc, s1, s2, &status);
110         TEST_ASSERT_SUCCESS(status);
111         TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_WHOLE_SCRIPT_CONFUSABLE, checkResults);
112
113     TEST_TEARDOWN;
114
115     TEST_SETUP
116         UnicodeString s("I1l0O");
117         UnicodeString dest;
118         UnicodeString &retStr = uspoof_getSkeletonUnicodeString(sc, USPOOF_ANY_CASE, s, dest, &status);
119         TEST_ASSERT_SUCCESS(status);
120         TEST_ASSERT(UnicodeString("lllOO") == dest);
121         TEST_ASSERT(&dest == &retStr);
122     TEST_TEARDOWN;
123 }
124
125
126 #define CHECK_SKELETON(type, input, expected) { \
127     checkSkeleton(sc, type, input, expected, __LINE__); \
128     }
129
130
131 // testSkeleton.   Spot check a number of confusable skeleton substitutions from the 
132 //                 Unicode data file confusables.txt
133 //                 Test cases chosen for substitutions of various lengths, and 
134 //                 membership in different mapping tables.
135 //          Note: for ICU 55, all tables collapsed to the MA table data.
136 //          TODO: for ICU 56 with Unicode 8, revisit this test.
137 //
138 void IntlTestSpoof::testSkeleton() {
139     const uint32_t ML = 0;
140     const uint32_t SL = USPOOF_SINGLE_SCRIPT_CONFUSABLE;
141     const uint32_t MA = USPOOF_ANY_CASE;
142     const uint32_t SA = USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE;
143
144     TEST_SETUP
145         CHECK_SKELETON(SL, "nochange", "nochange");
146         CHECK_SKELETON(SA, "nochange", "nochange");
147         CHECK_SKELETON(ML, "nochange", "nochange");
148         CHECK_SKELETON(MA, "nochange", "nochange");
149         CHECK_SKELETON(MA, "love", "love"); 
150         CHECK_SKELETON(MA, "1ove", "love");   // Digit 1 to letter l
151         CHECK_SKELETON(ML, "OOPS", "OOPS");
152         CHECK_SKELETON(ML, "00PS", "OOPS");
153         CHECK_SKELETON(MA, "OOPS", "OOPS");
154         CHECK_SKELETON(MA, "00PS", "OOPS");   // Digit 0 to letter O in any case mode only
155         CHECK_SKELETON(SL, "\\u059c", "\\u0301");
156         CHECK_SKELETON(SL, "\\u2A74", "\\u003A\\u003A\\u003D");
157         CHECK_SKELETON(SL, "\\u247E", "\\u0028\\u006C\\u006C\\u0029");  // "(ll)"
158         CHECK_SKELETON(SL, "\\uFDFB", "\\u062C\\u0644\\u0020\\u062C\\u0644\\u006c\\u0644\\u006f");
159
160         // This mapping exists in the ML and MA tables, does not exist in SL, SA
161         // 0C83 ;       0983 ;  ML
162         // 0C83 ;       0983 ;  MA
163         // 
164
165         CHECK_SKELETON(SL, "\\u0C83", "\\u0983");      
166         CHECK_SKELETON(SA, "\\u0C83", "\\u0983");          
167         CHECK_SKELETON(ML, "\\u0C83", "\\u0983");
168         CHECK_SKELETON(MA, "\\u0C83", "\\u0983");
169         
170         // 0391 mappings exist only in MA and SA tables.
171         CHECK_SKELETON(MA, "\\u0391", "A");
172         CHECK_SKELETON(SA, "\\u0391", "A");
173         CHECK_SKELETON(ML, "\\u0391", "A");
174         CHECK_SKELETON(SL, "\\u0391", "A");
175
176         // 13CF Mappings in all four tables, different in MA.
177         CHECK_SKELETON(ML, "\\u13CF", "b");
178         CHECK_SKELETON(MA, "\\u13CF", "b");
179         CHECK_SKELETON(SL, "\\u13CF", "b");
180         CHECK_SKELETON(SA, "\\u13CF", "b");
181
182         // 0022 ;  0027 0027 ; 
183         // all tables.
184         CHECK_SKELETON(SL, "\\u0022", "\\u0027\\u0027");
185         CHECK_SKELETON(SA, "\\u0022", "\\u0027\\u0027");
186         CHECK_SKELETON(ML, "\\u0022", "\\u0027\\u0027");
187         CHECK_SKELETON(MA, "\\u0022", "\\u0027\\u0027");
188
189         // 017F mappings exist only in MA and SA tables.
190         CHECK_SKELETON(MA, "\\u017F", "f");
191         CHECK_SKELETON(SA, "\\u017F", "f");
192         CHECK_SKELETON(ML, "\\u017F", "f");
193         CHECK_SKELETON(SL, "\\u017F", "f");
194
195     TEST_TEARDOWN;
196 }
197
198
199 //
200 //  Run a single confusable skeleton transformation test case.
201 //
202 void IntlTestSpoof::checkSkeleton(const USpoofChecker *sc, uint32_t type, 
203                                   const char *input, const char *expected, int32_t lineNum) {
204     UnicodeString uInput = UnicodeString(input).unescape();
205     UnicodeString uExpected = UnicodeString(expected).unescape();
206     
207     UErrorCode status = U_ZERO_ERROR;
208     UnicodeString actual;
209     uspoof_getSkeletonUnicodeString(sc, type, uInput, actual, &status);
210     if (U_FAILURE(status)) {
211         errln("File %s, Line %d, Test case from line %d, status is %s", __FILE__, __LINE__, lineNum,
212               u_errorName(status));
213         return;
214     }
215     if (uExpected != actual) {
216         errln("File %s, Line %d, Test case from line %d, Actual and Expected skeletons differ.",
217                __FILE__, __LINE__, lineNum);
218         errln(UnicodeString(" Actual   Skeleton: \"") + actual + UnicodeString("\"\n") +
219               UnicodeString(" Expected Skeleton: \"") + uExpected + UnicodeString("\""));
220     }
221 }
222
223 void IntlTestSpoof::testAreConfusable() {
224     TEST_SETUP
225         UnicodeString s1("A long string that will overflow stack buffers.  A long string that will overflow stack buffers. "
226                          "A long string that will overflow stack buffers.  A long string that will overflow stack buffers. ");
227         UnicodeString s2("A long string that wi11 overflow stack buffers.  A long string that will overflow stack buffers. "
228                          "A long string that wi11 overflow stack buffers.  A long string that will overflow stack buffers. ");
229         int32_t result = uspoof_areConfusableUnicodeString(sc, s1, s2, &status);
230         TEST_ASSERT_SUCCESS(status);
231         TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE, result);
232
233     TEST_TEARDOWN;
234 }
235
236 void IntlTestSpoof::testInvisible() {
237     TEST_SETUP
238         UnicodeString  s = UnicodeString("abcd\\u0301ef").unescape();
239         int32_t position = -42;
240         TEST_ASSERT_EQ(0, uspoof_checkUnicodeString(sc, s, &position, &status));
241         TEST_ASSERT_SUCCESS(status);
242         TEST_ASSERT(0 == position);
243
244         UnicodeString  s2 = UnicodeString("abcd\\u0301\\u0302\\u0301ef").unescape();
245         TEST_ASSERT_EQ(USPOOF_INVISIBLE, uspoof_checkUnicodeString(sc, s2, &position, &status));
246         TEST_ASSERT_SUCCESS(status);
247         TEST_ASSERT_EQ(0, position);
248
249         // Two acute accents, one from the composed a with acute accent, \u00e1,
250         // and one separate.
251         position = -42;
252         UnicodeString  s3 = UnicodeString("abcd\\u00e1\\u0301xyz").unescape();
253         TEST_ASSERT_EQ(USPOOF_INVISIBLE, uspoof_checkUnicodeString(sc, s3, &position, &status));
254         TEST_ASSERT_SUCCESS(status);
255         TEST_ASSERT_EQ(0, position);
256     TEST_TEARDOWN;
257 }
258
259 void IntlTestSpoof::testBug8654() {
260     TEST_SETUP
261         UnicodeString s = UnicodeString("B\\u00c1\\u0301").unescape();
262         int32_t position = -42;
263         TEST_ASSERT_EQ(USPOOF_INVISIBLE, uspoof_checkUnicodeString(sc, s, &position, &status) & USPOOF_INVISIBLE );
264         TEST_ASSERT_SUCCESS(status);
265         TEST_ASSERT_EQ(0, position);
266     TEST_TEARDOWN;
267 }
268
269 static UnicodeString parseHex(const UnicodeString &in) {
270     // Convert a series of hex numbers in a Unicode String to a string with the
271     // corresponding characters.
272     // The conversion is _really_ annoying.  There must be some function to just do it.
273     UnicodeString result;
274     UChar32 cc = 0;
275     for (int32_t i=0; i<in.length(); i++) {
276         UChar c = in.charAt(i);
277         if (c == 0x20) {   // Space
278             if (cc > 0) {
279                result.append(cc);
280                cc = 0;
281             }
282         } else if (c>=0x30 && c<=0x39) {
283             cc = (cc<<4) + (c - 0x30);
284         } else if ((c>=0x41 && c<=0x46) || (c>=0x61 && c<=0x66)) {
285             cc = (cc<<4) + (c & 0x0f)+9;
286         }
287         // else do something with bad input.
288     }
289     if (cc > 0) {
290         result.append(cc);
291     }
292     return result;
293 }
294
295
296 //
297 // Append the hex form of a UChar32 to a UnicodeString.
298 // Used in formatting error messages.
299 // Match the formatting of numbers in confusables.txt
300 // Minimum of 4 digits, no leading zeroes for positions 5 and up.
301 //
302 static void appendHexUChar(UnicodeString &dest, UChar32 c) {
303     UBool   doZeroes = FALSE;    
304     for (int bitNum=28; bitNum>=0; bitNum-=4) {
305         if (bitNum <= 12) {
306             doZeroes = TRUE;
307         }
308         int hexDigit = (c>>bitNum) & 0x0f;
309         if (hexDigit != 0 || doZeroes) {
310             doZeroes = TRUE;
311             dest.append((UChar)(hexDigit<=9? hexDigit + 0x30: hexDigit -10 + 0x41));
312         }
313     }
314     dest.append((UChar)0x20);
315 }
316
317 U_DEFINE_LOCAL_OPEN_POINTER(LocalStdioFilePointer, FILE, fclose);
318
319 //  testConfData - Check each data item from the Unicode confusables.txt file,
320 //                 verify that it transforms correctly in a skeleton.
321 //
322 void IntlTestSpoof::testConfData() {
323     char buffer[2000];
324     if (getUnidataPath(buffer) == NULL) {
325         errln("Skipping test spoof/testConfData. Unable to find path to source/data/unidata/.");
326         return;
327     }
328     uprv_strcat(buffer, "confusables.txt");
329
330     LocalStdioFilePointer f(fopen(buffer, "rb"));
331     if (f.isNull()) {
332         errln("Skipping test spoof/testConfData.  File confusables.txt not accessible.");
333         return;
334     }
335     fseek(f.getAlias(), 0, SEEK_END);
336     int32_t  fileSize = ftell(f.getAlias());
337     LocalArray<char> fileBuf(new char[fileSize]);
338     fseek(f.getAlias(), 0, SEEK_SET);
339     int32_t amt_read = fread(fileBuf.getAlias(), 1, fileSize, f.getAlias());
340     TEST_ASSERT_EQ(amt_read, fileSize);
341     TEST_ASSERT(fileSize>0);
342     if (amt_read != fileSize || fileSize <=0) {
343         return;
344     }
345     UnicodeString confusablesTxt = UnicodeString::fromUTF8(StringPiece(fileBuf.getAlias(), fileSize));
346
347     UErrorCode status = U_ZERO_ERROR;
348     LocalUSpoofCheckerPointer sc(uspoof_open(&status));
349     TEST_ASSERT_SUCCESS(status);
350
351     // Parse lines from the confusables.txt file.  Example Line:
352     // FF44 ;   0064 ;  SL      # ( d -> d ) FULLWIDTH ....
353     // Three fields.  The hex fields can contain more than one character,
354     //                and each character may be more than 4 digits (for supplemntals)
355     // This regular expression matches lines and splits the fields into capture groups.
356     RegexMatcher parseLine("(?m)^([0-9A-F]{4}[^#;]*?);([^#;]*?);([^#]*)", confusablesTxt, 0, status);
357     TEST_ASSERT_SUCCESS(status);
358     while (parseLine.find()) {
359         UnicodeString from = parseHex(parseLine.group(1, status));
360         if (!Normalizer::isNormalized(from, UNORM_NFD, status)) {
361             // The source character was not NFD.
362             // Skip this case; the first step in obtaining a skeleton is to NFD the input,
363             //  so the mapping in this line of confusables.txt will never be applied.
364             continue;
365         }
366
367         UnicodeString rawExpected = parseHex(parseLine.group(2, status));
368         UnicodeString expected;
369         Normalizer::decompose(rawExpected, FALSE /*NFD*/, 0, expected, status);
370         TEST_ASSERT_SUCCESS(status);
371
372         int32_t skeletonType = 0;
373         UnicodeString tableType = parseLine.group(3, status);
374         TEST_ASSERT_SUCCESS(status);
375         if (tableType.indexOf("SL") >= 0) {
376             skeletonType = USPOOF_SINGLE_SCRIPT_CONFUSABLE;
377         } else if (tableType.indexOf("SA") >= 0) {
378             skeletonType = USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE;
379         } else if (tableType.indexOf("ML") >= 0) {
380             skeletonType = 0;
381         } else if (tableType.indexOf("MA") >= 0) {
382             skeletonType = USPOOF_ANY_CASE;
383         }
384
385         UnicodeString actual;
386         uspoof_getSkeletonUnicodeString(sc.getAlias(), skeletonType, from, actual, &status);
387         TEST_ASSERT_SUCCESS(status);
388         TEST_ASSERT(actual == expected);
389         if (actual != expected) {
390             errln(parseLine.group(0, status));
391             UnicodeString line = "Actual: ";
392             int i = 0;
393             while (i < actual.length()) {
394                 appendHexUChar(line, actual.char32At(i));
395                 i = actual.moveIndex32(i, 1);
396             }
397             errln(line);
398         }
399         if (U_FAILURE(status)) {
400             break;
401         }
402     }
403 }
404
405
406 void IntlTestSpoof::testScriptSet() {
407     ScriptSet s1;
408     ScriptSet s2;
409     UErrorCode status = U_ZERO_ERROR;
410
411     TEST_ASSERT(s1 == s2);
412     s1.set(USCRIPT_ARABIC,status);
413     TEST_ASSERT_SUCCESS(status);
414     TEST_ASSERT(!(s1 == s2));
415     TEST_ASSERT(s1.test(USCRIPT_ARABIC, status));
416     TEST_ASSERT(s1.test(USCRIPT_GREEK, status) == FALSE);
417
418     status = U_ZERO_ERROR;
419     s1.reset(USCRIPT_ARABIC, status);
420     TEST_ASSERT(s1 == s2);
421
422     status = U_ZERO_ERROR;
423     s1.setAll();
424     TEST_ASSERT(s1.test(USCRIPT_COMMON, status));
425     TEST_ASSERT(s1.test(USCRIPT_ETHIOPIC, status));
426     TEST_ASSERT(s1.test(USCRIPT_CODE_LIMIT, status));
427     s1.resetAll();
428     TEST_ASSERT(!s1.test(USCRIPT_COMMON, status));
429     TEST_ASSERT(!s1.test(USCRIPT_ETHIOPIC, status));
430     TEST_ASSERT(!s1.test(USCRIPT_CODE_LIMIT, status));
431
432     status = U_ZERO_ERROR;
433     s1.set(USCRIPT_TAKRI, status);
434     s1.set(USCRIPT_BLISSYMBOLS, status);
435     s2.setAll();
436     TEST_ASSERT(s2.contains(s1));
437     TEST_ASSERT(!s1.contains(s2));
438     TEST_ASSERT(s2.intersects(s1));
439     TEST_ASSERT(s1.intersects(s2));
440     s2.reset(USCRIPT_TAKRI, status);
441     TEST_ASSERT(!s2.contains(s1));
442     TEST_ASSERT(!s1.contains(s2));
443     TEST_ASSERT(s1.intersects(s2));
444     TEST_ASSERT(s2.intersects(s1));
445     TEST_ASSERT_SUCCESS(status);
446
447     status = U_ZERO_ERROR;
448     s1.resetAll();
449     s1.set(USCRIPT_NKO, status);
450     s1.set(USCRIPT_COMMON, status);
451     s2 = s1;
452     TEST_ASSERT(s2 == s1);
453     TEST_ASSERT_EQ(2, s2.countMembers());
454     s2.intersect(s1);
455     TEST_ASSERT(s2 == s1);
456     s2.setAll();
457     TEST_ASSERT(!(s2 == s1));
458     TEST_ASSERT(s2.countMembers() >= USCRIPT_CODE_LIMIT);
459     s2.intersect(s1);
460     TEST_ASSERT(s2 == s1);
461     
462     s2.setAll();
463     s2.reset(USCRIPT_COMMON, status);
464     s2.intersect(s1);
465     TEST_ASSERT(s2.countMembers() == 1);
466
467     s1.resetAll();
468     TEST_ASSERT(s1.isEmpty());
469     s1.set(USCRIPT_LATIN, status);
470     TEST_ASSERT(!s1.isEmpty());
471     s1.setAll();
472     TEST_ASSERT(!s1.isEmpty());
473     TEST_ASSERT_SUCCESS(status);
474
475     s1.resetAll();
476     s1.set(USCRIPT_AFAKA, status);
477     s1.set(USCRIPT_VAI, status);
478     s1.set(USCRIPT_INHERITED, status);
479     int32_t n = -1;
480     for (int32_t i=0; i<4; i++) {
481         n = s1.nextSetBit(n+1);
482         switch (i) {
483           case 0: TEST_ASSERT_EQ(USCRIPT_INHERITED, n); break;
484           case 1: TEST_ASSERT_EQ(USCRIPT_VAI, n); break;
485           case 2: TEST_ASSERT_EQ(USCRIPT_AFAKA, n); break;
486           case 3: TEST_ASSERT_EQ(-1, (int32_t)n); break;
487           default: TEST_ASSERT(FALSE);
488         }
489     }
490     TEST_ASSERT_SUCCESS(status);
491
492     // Script extensions.  Depends on data.
493     s1.resetAll();
494     s1.setScriptExtensions(0x67, status);
495     TEST_ASSERT(s1.countMembers() == 1);
496     TEST_ASSERT(s1.test(USCRIPT_LATIN, status));
497     TEST_ASSERT_SUCCESS(status);
498
499     s1.resetAll();
500     s1.setScriptExtensions(0x303C, status);
501     TEST_ASSERT(s1.countMembers() == 3);
502     TEST_ASSERT(s1.test(USCRIPT_HAN, status));
503     TEST_ASSERT(s1.test(USCRIPT_HIRAGANA, status));
504     TEST_ASSERT(s1.test(USCRIPT_KATAKANA, status));
505     TEST_ASSERT_SUCCESS(status);
506
507     // Additional tests
508     ScriptSet bitset12; bitset12.set(USCRIPT_LATIN, status).set(USCRIPT_HANGUL, status);
509     ScriptSet bitset2;  bitset2.set(USCRIPT_HANGUL, status);
510     TEST_ASSERT(bitset12.contains(bitset2));
511     TEST_ASSERT(bitset12.contains(bitset12));
512     TEST_ASSERT(!bitset2.contains(bitset12));
513
514     ScriptSet arabSet;  arabSet.set(USCRIPT_ARABIC, status);
515     ScriptSet latinSet; latinSet.set(USCRIPT_LATIN, status);
516     UElement arabEl;  arabEl.pointer = &arabSet;
517     UElement latinEl; latinEl.pointer = &latinSet;
518     TEST_ASSERT(uhash_compareScriptSet(arabEl, latinEl) < 0);
519     TEST_ASSERT(uhash_compareScriptSet(latinEl, arabEl) > 0);
520
521     UnicodeString scriptString;
522     bitset12.displayScripts(scriptString);
523     TEST_ASSERT(UNICODE_STRING_SIMPLE("Hang Latn") == scriptString);
524 }
525
526
527 void IntlTestSpoof::testRestrictionLevel() {
528     struct Test {
529         const char         *fId;
530         URestrictionLevel   fExpectedRestrictionLevel;
531     } tests[] = {
532         {"\\u0061\\u03B3\\u2665", USPOOF_UNRESTRICTIVE},
533         {"a",                     USPOOF_ASCII},
534         {"\\u03B3",               USPOOF_SINGLE_SCRIPT_RESTRICTIVE},
535         {"\\u0061\\u30A2\\u30FC", USPOOF_HIGHLY_RESTRICTIVE},
536         {"\\u0061\\u0904",        USPOOF_MODERATELY_RESTRICTIVE},
537         {"\\u0061\\u03B3",        USPOOF_MINIMALLY_RESTRICTIVE},
538         {"\\u0061\\u2665",                USPOOF_UNRESTRICTIVE},
539         {"\\u0061\\u303C",                USPOOF_HIGHLY_RESTRICTIVE},
540         {"\\u0061\\u30FC\\u303C",         USPOOF_HIGHLY_RESTRICTIVE},
541         {"\\u0061\\u30FC\\u303C\\u30A2",  USPOOF_HIGHLY_RESTRICTIVE},
542         {"\\u30A2\\u0061\\u30FC\\u303C",  USPOOF_HIGHLY_RESTRICTIVE},
543         {"\\u0061\\u0031\\u0661",         USPOOF_MODERATELY_RESTRICTIVE},
544         {"\\u0061\\u0031\\u0661\\u06F1",  USPOOF_MODERATELY_RESTRICTIVE},
545         {"\\u0661\\u30FC\\u303C\\u0061\\u30A2\\u0031\\u0967\\u06F1",  USPOOF_MINIMALLY_RESTRICTIVE},
546         {"\\u0061\\u30A2\\u30FC\\u303C\\u0031\\u0967\\u0661\\u06F1",  USPOOF_MINIMALLY_RESTRICTIVE}
547     };
548     char msgBuffer[100];
549     URestrictionLevel restrictionLevels[] = { USPOOF_ASCII, USPOOF_SINGLE_SCRIPT_RESTRICTIVE, 
550         USPOOF_HIGHLY_RESTRICTIVE, USPOOF_MODERATELY_RESTRICTIVE, USPOOF_MINIMALLY_RESTRICTIVE, 
551         USPOOF_UNRESTRICTIVE};
552
553     UErrorCode status = U_ZERO_ERROR;
554     UnicodeSet allowedChars;
555     // Allowed Identifier Characters. In addition to the Recommended Set,
556     //    allow u303c, which has an interesting script extension of Hani Hira Kana.
557     allowedChars.addAll(*uspoof_getRecommendedUnicodeSet(&status)).add(0x303C);
558
559     for (int32_t testNum=0; testNum < UPRV_LENGTHOF(tests); testNum++) {
560         status = U_ZERO_ERROR;
561         const Test &test = tests[testNum];
562         UnicodeString testString = UnicodeString(test.fId).unescape();
563         URestrictionLevel expectedLevel = test.fExpectedRestrictionLevel;
564         for (int levelIndex=0; levelIndex<UPRV_LENGTHOF(restrictionLevels); levelIndex++) {
565             status = U_ZERO_ERROR;
566             URestrictionLevel levelSetInSpoofChecker = restrictionLevels[levelIndex];
567             USpoofChecker *sc = uspoof_open(&status);
568             uspoof_setAllowedChars(sc, allowedChars.toUSet(), &status);
569             uspoof_setRestrictionLevel(sc, levelSetInSpoofChecker);
570             uspoof_setChecks(sc, USPOOF_RESTRICTION_LEVEL, &status);
571             int32_t actualValue = uspoof_checkUnicodeString(sc, testString, NULL, &status);
572             
573             // we want to fail if the text is (say) MODERATE and the testLevel is ASCII
574             int32_t expectedValue = 0;
575             if (expectedLevel > levelSetInSpoofChecker) {
576                 expectedValue |= USPOOF_RESTRICTION_LEVEL;
577             }
578             sprintf(msgBuffer, "testNum = %d, levelIndex = %d, expected = %#x, actual = %#x",
579                     testNum, levelIndex, expectedValue, actualValue);
580             TEST_ASSERT_MSG(expectedValue == actualValue, msgBuffer);
581             TEST_ASSERT_SUCCESS(status);
582
583             // Run the same check again, with the Spoof Checker configured to return
584             // the actual restriction level.
585             uspoof_setAllowedChars(sc, allowedChars.toUSet(), &status);
586             uspoof_setRestrictionLevel(sc, levelSetInSpoofChecker);
587             uspoof_setChecks(sc, USPOOF_AUX_INFO | USPOOF_RESTRICTION_LEVEL, &status);
588             int32_t result = uspoof_checkUnicodeString(sc, testString, NULL, &status);
589             TEST_ASSERT_SUCCESS(status);
590             if (U_SUCCESS(status)) {
591                 TEST_ASSERT_EQ(expectedLevel, result & USPOOF_RESTRICTION_LEVEL_MASK);
592                 TEST_ASSERT_EQ(expectedValue, result & USPOOF_ALL_CHECKS);
593             }
594             uspoof_close(sc);
595         }
596     }
597
598 }
599
600 void IntlTestSpoof::testMixedNumbers() {
601     struct Test {
602         const char *fTestString;
603         const char *fExpectedSet;
604     } tests[] = {
605         {"1",              "[0]"},
606         {"\\u0967",        "[\\u0966]"},
607         {"1\\u0967",       "[0\\u0966]"},
608         {"\\u0661\\u06F1", "[\\u0660\\u06F0]"},
609         {"\\u0061\\u2665", "[]"},
610         {"\\u0061\\u303C", "[]"},
611         {"\\u0061\\u30FC\\u303C", "[]"},
612         {"\\u0061\\u30FC\\u303C\\u30A2", "[]"},
613         {"\\u30A2\\u0061\\u30FC\\u303C", "[]"},
614         {"\\u0061\\u0031\\u0661", "[\\u0030\\u0660]"},
615         {"\\u0061\\u0031\\u0661\\u06F1", "[\\u0030\\u0660\\u06F0]"},
616         {"\\u0661\\u30FC\\u303C\\u0061\\u30A2\\u0031\\u0967\\u06F1", "[\\u0030\\u0660\\u06F0\\u0966]"},
617         {"\\u0061\\u30A2\\u30FC\\u303C\\u0031\\u0967\\u0661\\u06F1", "[\\u0030\\u0660\\u06F0\\u0966]"}
618     };
619     UErrorCode status = U_ZERO_ERROR;
620     for (int32_t testNum=0; testNum < UPRV_LENGTHOF(tests); testNum++) {
621         char msgBuf[100];
622         sprintf(msgBuf, "testNum = %d ", testNum);
623         Test &test = tests[testNum];
624
625         status = U_ZERO_ERROR;
626         UnicodeString testString = UnicodeString(test.fTestString).unescape();
627         UnicodeSet expectedSet(UnicodeString(test.fExpectedSet).unescape(), status);
628
629         status = U_ZERO_ERROR;
630         TEST_SETUP
631             uspoof_setChecks(sc, USPOOF_MIXED_NUMBERS, &status); // only check this
632             uspoof_check2UnicodeString(sc, testString, checkResult, &status);
633             UBool mixedNumberFailure = ((uspoof_getCheckResultChecks(checkResult, &status) & USPOOF_MIXED_NUMBERS) != 0);
634             TEST_ASSERT_MSG((expectedSet.size() > 1) == mixedNumberFailure, msgBuf);
635             const UnicodeSet* actualSet = UnicodeSet::fromUSet(uspoof_getCheckResultNumerics(checkResult, &status));
636             TEST_ASSERT_MSG(expectedSet == *actualSet, msgBuf);
637         TEST_TEARDOWN
638     }
639 }
640
641 // Bug #12153 - uspoof_setRestrictionLevel() should enable restriction level testing.
642 // 
643 void IntlTestSpoof::testBug12153() {
644     UErrorCode status = U_ZERO_ERROR;
645     LocalUSpoofCheckerPointer sc(uspoof_open(&status));
646     TEST_ASSERT_SUCCESS(status);
647     int32_t checks = uspoof_getChecks(sc.getAlias(), &status);
648     TEST_ASSERT((checks & USPOOF_RESTRICTION_LEVEL) != 0);
649     checks &= ~USPOOF_RESTRICTION_LEVEL;
650     uspoof_setChecks(sc.getAlias(), checks, &status);
651     checks = uspoof_getChecks(sc.getAlias(), &status);
652     TEST_ASSERT((checks & USPOOF_RESTRICTION_LEVEL) == 0);
653
654     uspoof_setRestrictionLevel(sc.getAlias(), USPOOF_MODERATELY_RESTRICTIVE);
655     checks = uspoof_getChecks(sc.getAlias(), &status);
656     TEST_ASSERT((checks & USPOOF_RESTRICTION_LEVEL) != 0);
657     TEST_ASSERT_SUCCESS(status);
658 }
659
660 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_NORMALIZATION && !UCONFIG_NO_FILE_IO */