Search in sources :

Example 1 with SpoofChecker

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

the class SpoofCheckerTest method TestAreConfusable.

@Test
public void TestAreConfusable() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CONFUSABLE).build();
    String s1 = "A long string that will overflow stack buffers.  A long string that will overflow stack buffers. " + "A long string that will overflow stack buffers.  A long string that will overflow stack buffers. ";
    String s2 = "A long string that wi11 overflow stack buffers.  A long string that will overflow stack buffers. " + "A long string that wi11 overflow stack buffers.  A long string that will overflow stack buffers. ";
    assertEquals("", SpoofChecker.SINGLE_SCRIPT_CONFUSABLE, sc.areConfusable(s1, s2));
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) Test(org.junit.Test)

Example 2 with SpoofChecker

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

the class SpoofCheckerTest method TestGetSetAllowedChars.

/*
     * get & setAllowedChars
     */
@Test
public void TestGetSetAllowedChars() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).build();
    UnicodeSet us;
    UnicodeSet uset;
    uset = sc.getAllowedChars();
    assertTrue("", uset.isFrozen());
    us = new UnicodeSet(0x41, 0x5A);
    /* [A-Z] */
    sc = new SpoofChecker.Builder().setChecks(SpoofChecker.CHAR_LIMIT).setAllowedChars(us).build();
    assertEquals("", us, sc.getAllowedChars());
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 3 with SpoofChecker

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

the class SpoofCheckerTest method TestCheckResultToString11447.

@Test
public void TestCheckResultToString11447() {
    CheckResult checkResult = new CheckResult();
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.MIXED_NUMBERS).build();
    sc.failsChecks("1рез", checkResult);
    assertTrue("CheckResult: ", checkResult.toString().contains("MIXED_NUMBERS"));
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) CheckResult(android.icu.text.SpoofChecker.CheckResult) Test(org.junit.Test)

Example 4 with SpoofChecker

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

the class SpoofCheckerTest method TestGetSetChecks1.

/*
     * Set & Get Check Flags
     */
@Test
public void TestGetSetChecks1() {
    SpoofChecker sc = new SpoofChecker.Builder().setChecks(SpoofChecker.ALL_CHECKS).build();
    int t;
    t = sc.getChecks();
    assertEquals("", SpoofChecker.ALL_CHECKS, t);
    sc = new SpoofChecker.Builder().setChecks(0).build();
    t = sc.getChecks();
    assertEquals("", 0, t);
    int checks = SpoofChecker.WHOLE_SCRIPT_CONFUSABLE | SpoofChecker.MIXED_SCRIPT_CONFUSABLE | SpoofChecker.ANY_CASE;
    sc = new SpoofChecker.Builder().setChecks(checks).build();
    t = sc.getChecks();
    assertEquals("", checks, t);
}
Also used : SpoofChecker(android.icu.text.SpoofChecker) Test(org.junit.Test)

Example 5 with SpoofChecker

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

the class SpoofCheckerTest method testConfData.

// Verify that each item from the Unicode confusables.txt file
// transforms into the expected skeleton.
@Test
public void testConfData() {
    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;
    }
    try {
        // Read in the confusables.txt file. (Distributed by Unicode.org)
        String fileName = "unicode/confusables.txt";
        BufferedReader confusablesRdr = TestUtil.getDataReader(fileName, "UTF-8");
        // Create a default spoof checker to use in this test.
        SpoofChecker sc = new SpoofChecker.Builder().build();
        // Parse lines from the confusables.txt file. Example Line:
        // FF44 ; 0064 ; SL # ( d -> d ) FULLWIDTH ....
        // Lines have three fields. The hex fields can contain more than one character,
        // and each character may be more than 4 digits (for supplemntals)
        // This regular expression matches lines and splits the fields into capture groups.
        // Capture group 1: map from chars
        // 2: map to chars
        // 3: table type, SL, ML, SA or MA (deprecated)
        // 4: Comment Lines Only
        // 5: Error Lines Only
        Matcher parseLine = Pattern.compile("\\ufeff?" + "(?:([0-9A-F\\s]+);([0-9A-F\\s]+);\\s*(SL|ML|SA|MA)\\s*(?:#.*?)?$)" + // Comment line
        "|\\ufeff?(\\s*(?:#.*)?)").matcher("");
        Normalizer2 normalizer = Normalizer2.getNFDInstance();
        int lineNum = 0;
        String inputLine;
        while ((inputLine = confusablesRdr.readLine()) != null) {
            lineNum++;
            parseLine.reset(inputLine);
            if (!parseLine.matches()) {
                errln("Syntax error in confusable data file at line " + lineNum);
                errln(inputLine);
                break;
            }
            if (parseLine.group(4) != null) {
                // comment line
                continue;
            }
            String from = parseHex(parseLine.group(1));
            if (!normalizer.isNormalized(from)) {
                // so the mapping in this line of confusables.txt will never be applied.
                continue;
            }
            String rawExpected = parseHex(parseLine.group(2));
            String expected = normalizer.normalize(rawExpected);
            String actual;
            actual = sc.getSkeleton(from);
            if (!actual.equals(expected)) {
                errln("confusables.txt: " + lineNum + ": " + parseLine.group(0));
                errln("Actual: " + escapeString(actual));
            }
        }
        confusablesRdr.close();
    } catch (IOException e) {
        errln(e.toString());
    }
}
Also used : Matcher(java.util.regex.Matcher) Normalizer2(android.icu.text.Normalizer2) SpoofChecker(android.icu.text.SpoofChecker) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) 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