Search in sources :

Example 16 with SpoofChecker

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

the class SpoofCheckerTest method TestBug11635.

@Test
public void TestBug11635() {
    // The bug was an error in iterating through supplementary characters in IdentifierInfo.
    // The three supplemental chars in the string are "123" from the mathematical bold digit range.
    // Common script, Nd general category, and no other restrictions on allowed characters
    // leaves "ABC123" as SINGLE_SCRIPT_RESTRICTIVE.
    String identifier = Utility.unescape("ABC\\U0001D7CF\\U0001D7D0\\U0001D7D1");
    CheckResult checkResult = new CheckResult();
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.RESTRICTION_LEVEL).build();
    sc.failsChecks(identifier, checkResult);
    assertEquals("", RestrictionLevel.SINGLE_SCRIPT_RESTRICTIVE, checkResult.restrictionLevel);
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) Test(org.junit.Test)

Example 17 with SpoofChecker

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

the class SpoofCheckerTest method TestOpenFromSourceRules.

/*
     * Test build from source rules.
     */
@Test
public void TestOpenFromSourceRules() {
    if (TestUtil.getJavaVendor() == JavaVendor.IBM && TestUtil.getJavaVersion() == 5) {
        // Note: IBM Java 5 has a bug reading a large UTF-8 text contents
        logln("Skip this test case because of the IBM Java 5 bug");
        return;
    }
    String fileName;
    Reader confusables;
    try {
        SpoofChecker rsc = null;
        fileName = "unicode/confusables.txt";
        confusables = TestUtil.getDataReader(fileName, "UTF-8");
        try {
            rsc = new SpoofChecker.Builder().setData(confusables).build();
        } finally {
            confusables.close();
        }
        if (rsc == null) {
            errln("FAIL: null SpoofChecker");
            return;
        }
        // Check that newly built-from-rules SpoofChecker is able to function.
        checkSkeleton(rsc, "TestOpenFromSourceRules");
        SpoofChecker.CheckResult result = new SpoofChecker.CheckResult();
        rsc.failsChecks("Hello", result);
        // The checker we just built from source rules should be equivalent to the
        // default checker created from prebuilt rules baked into the ICU data.
        SpoofChecker defaultChecker = new SpoofChecker.Builder().build();
        assertEquals("Checker built from rules equals default", defaultChecker, rsc);
        assertEquals("Checker built from rules has same hash code as default", defaultChecker.hashCode(), rsc.hashCode());
        SpoofChecker optionChecker = new SpoofChecker.Builder().setRestrictionLevel(RestrictionLevel.UNRESTRICTIVE).build();
        assertFalse("", optionChecker.equals(rsc));
        String stubConfusables = "# Stub confusables data\n" + "05AD ; 0596 ;  MA  # ( ֭ → ֖ ) HEBREW ACCENT DEHI → HEBREW ACCENT TIPEHA   #\n";
        // Verify that re-using a builder doesn't alter SpoofCheckers that were
        // previously created by that builder. (The builder could modify data
        // being used by the existing checker)
        SpoofChecker.Builder builder = new SpoofChecker.Builder();
        SpoofChecker testChecker1 = builder.build();
        assertTrue("", testChecker1.equals(defaultChecker));
        builder.setData(new StringReader(stubConfusables));
        builder.setRestrictionLevel(RestrictionLevel.UNRESTRICTIVE);
        builder.setChecks(SpoofChecker.SINGLE_SCRIPT_CONFUSABLE);
        Set<ULocale> allowedLocales = new HashSet<ULocale>();
        allowedLocales.add(ULocale.JAPANESE);
        allowedLocales.add(ULocale.FRENCH);
        builder.setAllowedLocales(allowedLocales);
        SpoofChecker testChecker2 = builder.build();
        SpoofChecker testChecker3 = builder.build();
        assertTrue("", testChecker1.equals(defaultChecker));
        assertFalse("", testChecker2.equals(defaultChecker));
        assertTrue("", testChecker2.equals(testChecker3));
    } catch (java.io.IOException e) {
        errln(e.toString());
    } catch (ParseException e) {
        errln(e.toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) CheckResult(android.icu.text.SpoofChecker.CheckResult) SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) StringReader(java.io.StringReader) ParseException(java.text.ParseException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 18 with SpoofChecker

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

the class SpoofCheckerTest method TestDeprecated.

@Test
public void TestDeprecated() {
    // getSkeleton
    SpoofChecker sc = new SpoofChecker.Builder().build();
    assertEquals("Deprecated version of getSkeleton method does not work", sc.getSkeleton(SpoofChecker.ANY_CASE, scMixed), sc.getSkeleton(scMixed));
    // setData
    try {
        String fileName1 = "unicode/confusables.txt";
        String fileName2 = "unicode/confusablesWholeScript.txt";
        Reader reader1 = TestUtil.getDataReader(fileName1, "UTF-8");
        Reader reader2 = TestUtil.getDataReader(fileName2, "UTF-8");
        Reader reader3 = TestUtil.getDataReader(fileName1, "UTF-8");
        try {
            SpoofChecker sc2 = new SpoofChecker.Builder().setData(reader1, reader2).build();
            SpoofChecker sc1 = new SpoofChecker.Builder().setData(reader3).build();
            assertEquals("Deprecated version of setData method does not work", sc1, sc2);
        } finally {
            reader1.close();
            reader2.close();
            reader3.close();
        }
    } catch (IOException e) {
        fail("Could not load confusables data");
    } catch (ParseException e) {
        fail("Could not parse confusables data");
    }
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 19 with SpoofChecker

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

the class SpoofCheckerTest method TestAllowedChars.

/*
     * AllowedChars set/get the UnicodeSet of allowed characters.
     */
@Test
public void TestAllowedChars() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).build();
    UnicodeSet set;
    UnicodeSet tmpSet;
    boolean checkResults;
    /* By default, we should see no restriction; the UnicodeSet should allow all characters. */
    set = sc.getAllowedChars();
    tmpSet = new UnicodeSet(0, 0x10ffff);
    assertEquals("", tmpSet, set);
    /* Remove a character that is in our good Latin test identifier from the allowed chars set. */
    tmpSet.remove(goodLatin.charAt(1));
    sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).setAllowedChars(tmpSet).build();
    /* Latin Identifier should now fail; other non-latin test cases should still be OK */
    SpoofChecker.CheckResult result = new SpoofChecker.CheckResult();
    checkResults = sc.failsChecks(goodLatin, result);
    assertTrue("", checkResults);
    assertEquals("", SpoofChecker.CHAR_LIMIT, result.checks);
}
Also used : CheckResult(android.icu.text.SpoofChecker.CheckResult) SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 20 with SpoofChecker

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

the class SpoofCheckerTest method TestSkeleton.

@Test
public void TestSkeleton() {
    SpoofChecker sc = new SpoofChecker.Builder().build();
    checkSkeleton(sc, "TestSkeleton");
}
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