Search in sources :

Example 6 with SpoofChecker

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

the class SpoofCheckerTest method TestAreConfusable1.

@Test
public void TestAreConfusable1() {
    SpoofChecker sc = new SpoofChecker.Builder().build();
    int checkResults;
    checkResults = sc.areConfusable(scLatin, scMixed);
    assertEquals("Latin/Mixed is not MIXED_SCRIPT_CONFUSABLE", SpoofChecker.MIXED_SCRIPT_CONFUSABLE, checkResults);
    checkResults = sc.areConfusable(goodGreek, scLatin);
    assertEquals("Greek/Latin is not unconfusable", 0, checkResults);
    checkResults = sc.areConfusable(lll_Latin_a, lll_Latin_b);
    assertEquals("Latin/Latin is not SINGLE_SCRIPT_CONFUSABLE", SpoofChecker.SINGLE_SCRIPT_CONFUSABLE, checkResults);
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) Test(org.junit.Test)

Example 7 with SpoofChecker

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

the class SpoofCheckerTest method TestSpoofAPI.

/**
 * IntlTestSpoof is the top level test class for the Unicode Spoof detection tests
 */
// Test the USpoofDetector API functions that require C++
// The pure C part of the API, which is most of it, is tested in cintltst
/**
 * IntlTestSpoof tests for USpoofDetector
 */
@Test
public void TestSpoofAPI() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.ALL_CHECKS).build();
    String s = "xyz";
    SpoofChecker.CheckResult result = new SpoofChecker.CheckResult();
    result.position = 666;
    boolean checkResults = sc.failsChecks(s, result);
    assertFalse("", checkResults);
    assertEquals("", 0, result.position);
    sc = new SpoofChecker.Builder().build();
    String s1 = "cxs";
    // Cyrillic "cxs"
    String s2 = Utility.unescape("\\u0441\\u0445\\u0455");
    int checkResult = sc.areConfusable(s1, s2);
    assertEquals("", SpoofChecker.MIXED_SCRIPT_CONFUSABLE | SpoofChecker.WHOLE_SCRIPT_CONFUSABLE, checkResult);
    sc = new SpoofChecker.Builder().build();
    s = "I1l0O";
    String dest = sc.getSkeleton(SpoofChecker.ANY_CASE, s);
    assertEquals("", dest, "lllOO");
}
Also used : CheckResult(android.icu.text.SpoofChecker.CheckResult) SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) Test(org.junit.Test)

Example 8 with SpoofChecker

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

the class SpoofCheckerTest method TestMixedNumbers.

@Test
public void TestMixedNumbers() {
    Object[][] tests = { { "1", "[0]" }, { "१", "[०]" }, { "1१", "[0०]" }, { "١۱", "[٠۰]" }, { "a♥", "[]" }, { "a\u303c", "[]" }, { "aー\u303c", "[]" }, { "aー\u303cア", "[]" }, { "アaー\u303c", "[]" }, { "a1١", "[0٠]" }, { "a1١۱", "[0٠۰]" }, { "١ー\u303caア1१۱", "[0٠۰०]" }, { "aアー\u303c1१١۱", "[0٠۰०]" } };
    CheckResult checkResult = new CheckResult();
    for (Object[] test : tests) {
        String testString = (String) test[0];
        UnicodeSet expected = new UnicodeSet((String) test[1]);
        SpoofChecker sc = new SpoofChecker.Builder().setChecks(// only check this
        SpoofChecker.MIXED_NUMBERS).build();
        boolean actualValue = sc.failsChecks(testString, checkResult);
        assertEquals("", expected, checkResult.numerics);
        assertEquals("Testing spoof mixed numbers for '" + testString + "', ", expected.size() > 1, actualValue);
    }
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 9 with SpoofChecker

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

the class SpoofCheckerTest method TestGetSetChecks.

/*
     * get & set Checks
     */
@Test
public void TestGetSetChecks() {
    SpoofChecker sc = new SpoofChecker.Builder().build();
    int checks;
    int checks2;
    boolean checkResults;
    checks = sc.getChecks();
    assertEquals("", SpoofChecker.ALL_CHECKS, checks);
    checks &= ~(SpoofChecker.SINGLE_SCRIPT | SpoofChecker.MIXED_SCRIPT_CONFUSABLE);
    sc = new SpoofChecker.Builder().setChecks(checks).build();
    checks2 = sc.getChecks();
    assertEquals("", checks, checks2);
    /*
         * The checks that were disabled just above are the same ones that the "scMixed" test fails. So with those tests
         * gone checking that Identifier should now succeed
         */
    checkResults = sc.failsChecks(scMixed);
    assertFalse("", checkResults);
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) Test(org.junit.Test)

Example 10 with SpoofChecker

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

the class SpoofCheckerTest method TestAllowedLocales.

/*
     * AllowedLocales
     */
@Test
public void TestAllowedLocales() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).build();
    Set<ULocale> allowedLocales = null;
    Set<Locale> allowedJavaLocales = null;
    boolean checkResults;
    /* Default allowed locales list should be empty */
    allowedLocales = sc.getAllowedLocales();
    assertTrue("Empty allowed locales", allowedLocales.isEmpty());
    allowedJavaLocales = sc.getAllowedJavaLocales();
    assertTrue("Empty allowed Java locales", allowedJavaLocales.isEmpty());
    /* Allow en and ru, which should enable Latin and Cyrillic only to pass */
    ULocale enloc = new ULocale("en");
    ULocale ruloc = new ULocale("ru_RU");
    allowedLocales = new HashSet<ULocale>();
    allowedLocales.add(enloc);
    allowedLocales.add(ruloc);
    sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).setAllowedLocales(allowedLocales).build();
    allowedLocales = sc.getAllowedLocales();
    assertTrue("en in allowed locales", allowedLocales.contains(enloc));
    assertTrue("ru_RU in allowed locales", allowedLocales.contains(ruloc));
    Locale frlocJ = new Locale("fr");
    allowedJavaLocales = new HashSet<Locale>();
    allowedJavaLocales.add(frlocJ);
    sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).setAllowedJavaLocales(allowedJavaLocales).build();
    assertFalse("no en in allowed Java locales", allowedJavaLocales.contains(new Locale("en")));
    assertTrue("fr in allowed Java locales", allowedJavaLocales.contains(frlocJ));
    sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).setAllowedLocales(allowedLocales).build();
    SpoofChecker.CheckResult result = new SpoofChecker.CheckResult();
    checkResults = sc.failsChecks(goodLatin);
    assertFalse("", checkResults);
    checkResults = sc.failsChecks(goodGreek, result);
    assertEquals("", SpoofChecker.CHAR_LIMIT, result.checks);
    checkResults = sc.failsChecks(goodCyrl);
    assertFalse("", checkResults);
    /* Reset with an empty locale list, which should allow all characters to pass */
    allowedLocales = new LinkedHashSet<ULocale>();
    sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).setAllowedLocales(allowedLocales).build();
    checkResults = sc.failsChecks(goodGreek);
    assertFalse("", checkResults);
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) CheckResult(android.icu.text.SpoofChecker.CheckResult) ULocale(android.icu.util.ULocale) SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) 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