Search in sources :

Example 11 with SpoofChecker

use of android.icu.text.SpoofChecker in project j2objc by google.

the class SpoofCheckerTest method TestCheck.

@Test
public void TestCheck() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.ALL_CHECKS).build();
    SpoofChecker.CheckResult result = new SpoofChecker.CheckResult();
    boolean checkResults;
    result.position = 666;
    checkResults = sc.failsChecks(goodLatin, result);
    assertFalse("", checkResults);
    assertEquals("", 0, result.checks);
    checkResults = sc.failsChecks(goodCyrl, result);
    assertFalse("", checkResults);
    assertEquals("", 0, result.checks);
    result.position = 666;
    checkResults = sc.failsChecks(scMixed, result);
    assertTrue("", checkResults);
    assertEquals("", SpoofChecker.RESTRICTION_LEVEL, result.checks);
    result.position = 666;
    checkResults = sc.failsChecks(han_Hiragana, result);
    assertFalse("", checkResults);
    assertEquals("", 0, result.checks);
}
Also used : CheckResult(android.icu.text.SpoofChecker.CheckResult) SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) Test(org.junit.Test)

Example 12 with SpoofChecker

use of android.icu.text.SpoofChecker in project j2objc by google.

the class SpoofCheckerTest method TestInvisible.

@Test
public void TestInvisible() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.INVISIBLE).build();
    String s = Utility.unescape("abcd\\u0301ef");
    SpoofChecker.CheckResult result = new SpoofChecker.CheckResult();
    result.position = -42;
    assertFalse("", sc.failsChecks(s, result));
    assertEquals("", 0, result.checks);
    assertEquals("", result.position, 0);
    String s2 = Utility.unescape("abcd\\u0301\\u0302\\u0301ef");
    assertTrue("", sc.failsChecks(s2, result));
    assertEquals("", SpoofChecker.INVISIBLE, result.checks);
    assertEquals("", 0, result.position);
    // Two acute accents, one from the composed a with acute accent, \u00e1,
    // and one separate.
    result.position = -42;
    String s3 = Utility.unescape("abcd\\u00e1\\u0301xyz");
    assertTrue("", sc.failsChecks(s3, result));
    assertEquals("", SpoofChecker.INVISIBLE, result.checks);
    assertEquals("", 0, result.position);
}
Also used : CheckResult(android.icu.text.SpoofChecker.CheckResult) SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) Test(org.junit.Test)

Example 13 with SpoofChecker

use of android.icu.text.SpoofChecker in project j2objc by google.

the class SpoofCheckerTest method TestRestrictionLevel.

@Test
public void TestRestrictionLevel() {
    Object[][] tests = { { "aγ♥", RestrictionLevel.UNRESTRICTIVE }, { "a", RestrictionLevel.ASCII }, { "γ", RestrictionLevel.SINGLE_SCRIPT_RESTRICTIVE }, { "aアー", RestrictionLevel.HIGHLY_RESTRICTIVE }, { "aऄ", RestrictionLevel.MODERATELY_RESTRICTIVE }, { "aγ", RestrictionLevel.MINIMALLY_RESTRICTIVE }, { "a♥", RestrictionLevel.UNRESTRICTIVE }, { "a\u303c", RestrictionLevel.HIGHLY_RESTRICTIVE }, { "aー\u303c", RestrictionLevel.HIGHLY_RESTRICTIVE }, { "aー\u303cア", RestrictionLevel.HIGHLY_RESTRICTIVE }, { "アaー\u303c", RestrictionLevel.HIGHLY_RESTRICTIVE }, { "a1١", RestrictionLevel.MODERATELY_RESTRICTIVE }, { "a1١۱", RestrictionLevel.MODERATELY_RESTRICTIVE }, { "١ー\u303caア1१۱", RestrictionLevel.MINIMALLY_RESTRICTIVE }, { "aアー\u303c1१١۱", RestrictionLevel.MINIMALLY_RESTRICTIVE } };
    UnicodeSet allowedChars = new UnicodeSet();
    // Allowed Identifier Characters. In addition to the Recommended Set,
    // allow u303c, which has an interesting script extension of Hani Hira Kana.
    allowedChars.addAll(SpoofChecker.RECOMMENDED).add(0x303c);
    CheckResult checkResult = new CheckResult();
    for (Object[] test : tests) {
        String testString = (String) test[0];
        RestrictionLevel expectedLevel = (RestrictionLevel) test[1];
        for (RestrictionLevel levelSetInSpoofChecker : RestrictionLevel.values()) {
            SpoofChecker sc = new SpoofChecker.Builder().setAllowedChars(allowedChars).setRestrictionLevel(levelSetInSpoofChecker).setChecks(// only check this
            SpoofChecker.RESTRICTION_LEVEL).build();
            boolean actualValue = sc.failsChecks(testString, checkResult);
            assertEquals("Testing restriction level for '" + testString + "'", expectedLevel, checkResult.restrictionLevel);
            // we want to fail if the text is (say) MODERATE and the testLevel is ASCII
            boolean expectedFailure = expectedLevel.compareTo(levelSetInSpoofChecker) > 0;
            assertEquals("Testing spoof restriction level for '" + testString + "', " + levelSetInSpoofChecker, expectedFailure, actualValue);
            // Coverage for getRestrictionLevel
            assertEquals("Restriction level on built SpoofChecker should be same as on builder", levelSetInSpoofChecker, sc.getRestrictionLevel());
        }
    }
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) RestrictionLevel(android.icu.text.SpoofChecker.RestrictionLevel) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 14 with SpoofChecker

use of android.icu.text.SpoofChecker in project j2objc by google.

the class SpoofCheckerTest method TestGetSkeleton.

@Test
public void TestGetSkeleton() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CONFUSABLE).build();
    String dest;
    dest = sc.getSkeleton(SpoofChecker.ANY_CASE, lll_Latin_a);
    assertEquals("", lll_Skel, dest);
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) Test(org.junit.Test)

Example 15 with SpoofChecker

use of android.icu.text.SpoofChecker in project j2objc by google.

the class SpoofCheckerTest method TestConfusableFlagVariants.

@Test
public void TestConfusableFlagVariants() {
    // The spoof checker should only return those tests that the user requested.  This test makes sure that
    // the checker doesn't return anything the user doesn't want.  This test started passing in ICU 58.
    String latn = "desordenado";
    String cyrl = "ԁеѕогԁепаԁо";
    String mixed = "dеѕогdenаdo";
    Object[][] tests = { // string 1, string 2, checks for spoof checker, expected output
    { latn, cyrl, SpoofChecker.CONFUSABLE, SpoofChecker.MIXED_SCRIPT_CONFUSABLE | SpoofChecker.WHOLE_SCRIPT_CONFUSABLE }, { latn, cyrl, SpoofChecker.MIXED_SCRIPT_CONFUSABLE | SpoofChecker.WHOLE_SCRIPT_CONFUSABLE, SpoofChecker.MIXED_SCRIPT_CONFUSABLE | SpoofChecker.WHOLE_SCRIPT_CONFUSABLE }, { latn, cyrl, SpoofChecker.MIXED_SCRIPT_CONFUSABLE, SpoofChecker.MIXED_SCRIPT_CONFUSABLE }, { latn, cyrl, SpoofChecker.WHOLE_SCRIPT_CONFUSABLE, SpoofChecker.WHOLE_SCRIPT_CONFUSABLE }, { latn, cyrl, SpoofChecker.SINGLE_SCRIPT_CONFUSABLE, 0 }, { latn, mixed, SpoofChecker.CONFUSABLE, SpoofChecker.MIXED_SCRIPT_CONFUSABLE }, { latn, mixed, SpoofChecker.MIXED_SCRIPT_CONFUSABLE, SpoofChecker.MIXED_SCRIPT_CONFUSABLE }, { latn, mixed, SpoofChecker.MIXED_SCRIPT_CONFUSABLE | SpoofChecker.WHOLE_SCRIPT_CONFUSABLE, SpoofChecker.MIXED_SCRIPT_CONFUSABLE }, { latn, mixed, SpoofChecker.WHOLE_SCRIPT_CONFUSABLE, 0 }, { latn, latn, SpoofChecker.CONFUSABLE, SpoofChecker.SINGLE_SCRIPT_CONFUSABLE } };
    for (Object[] test : tests) {
        String s1 = (String) test[0];
        String s2 = (String) test[1];
        int checks = (Integer) test[2];
        int expectedResult = (Integer) test[3];
        // Sanity check: expectedResult should be a subset of checks
        assertEquals("Invalid test case", expectedResult & checks, expectedResult);
        SpoofChecker sc = new SpoofChecker.Builder().setChecks(checks).build();
        int actualResult = sc.areConfusable(s1, s2);
        assertEquals("Comparing '" + s1 + "' and '" + s2 + "' with checks '" + checks + "'", expectedResult, actualResult);
    }
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) Test(org.junit.Test)

Aggregations

SpoofChecker (android.icu.text.SpoofChecker)21 Test (org.junit.Test)21 CheckResult (android.icu.text.SpoofChecker.CheckResult)10 UnicodeSet (android.icu.text.UnicodeSet)4 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 ULocale (android.icu.util.ULocale)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 ParseException (java.text.ParseException)2 Normalizer2 (android.icu.text.Normalizer2)1 RestrictionLevel (android.icu.text.SpoofChecker.RestrictionLevel)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Locale (java.util.Locale)1 Matcher (java.util.regex.Matcher)1