示例#1
0
// Test open from source rules.
// Run this in isolation to verify initialization.
static void TestOpenFromSource() {
    // No TEST_SETUP because that calls uspoof_open().
    UErrorCode status = U_ZERO_ERROR;
    const char *dataSrcDir;
    char       *fileName;
    char       *confusables;
    int         confusablesLength = 0;
    char       *confusablesWholeScript;
    int         confusablesWholeScriptLength = 0;
    FILE       *f;
    UParseError pe;
    int32_t     errType;
    int32_t     checkResults;
    USpoofChecker *rsc;

    dataSrcDir = ctest_dataSrcDir();
    fileName = malloc(strlen(dataSrcDir) + 100);
    strcpy(fileName, dataSrcDir);
    strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusables.txt");
    f = fopen(fileName, "rb");
    TEST_ASSERT_NE(f, NULL);
    confusables = malloc(3000000);
    if (f != NULL) {
        confusablesLength = fread(confusables, 1, 3000000, f);
        fclose(f);
    }

    strcpy(fileName, dataSrcDir);
    strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusablesWholeScript.txt");
    f = fopen(fileName, "rb");
    TEST_ASSERT_NE(f, NULL);
    confusablesWholeScript = malloc(1000000);
    if (f != NULL) {
        confusablesWholeScriptLength = fread(confusablesWholeScript, 1, 1000000, f);
        fclose(f);
    }

    rsc = uspoof_openFromSource(confusables, confusablesLength,
                                confusablesWholeScript, confusablesWholeScriptLength,
                                &errType, &pe, &status);
    TEST_ASSERT_SUCCESS(status);

    // Ticket #11860: uspoof_openFromSource() did not initialize for use.
    // Verify that the spoof checker does not crash.
    checkResults = uspoof_check(rsc, goodLatin, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    free(confusablesWholeScript);
    free(confusables);
    free(fileName);
    uspoof_close(rsc);
    /*  printf("ParseError Line is %d\n", pe.line);  */
}
示例#2
0
/* {{{ void spoofchecker_object_destroy( Spoofchecker_object* co )
 * Clean up mem allocted by internals of Spoofchecker_object
 */
void spoofchecker_object_destroy(Spoofchecker_object* co)
{
	if (!co) {
		return;
	}

	if (co->uspoof) {
		uspoof_close(co->uspoof);
		co->uspoof = NULL;
	}

	intl_error_reset(SPOOFCHECKER_ERROR_P(co));
}
示例#3
0
c_SpoofChecker::~c_SpoofChecker() {
  uspoof_close(m_spoof_checker);
}
示例#4
0
//----------------------------------------------------------------------------
//
//  main      for gencfu
//
//----------------------------------------------------------------------------
int  main(int argc, char **argv) {
    UErrorCode  status = U_ZERO_ERROR;
    const char *confFileName;
    const char *confWSFileName;
    const char *outFileName;
    const char *outDir = NULL;
    const char *copyright = NULL;

    //
    // Pick up and check the command line arguments,
    //    using the standard ICU tool utils option handling.
    //
    U_MAIN_INIT_ARGS(argc, argv);
    progName = argv[0];
    argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
    if(argc<0) {
        // Unrecognized option
        fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]);
        usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
    }

    if(options[0].doesOccur || options[1].doesOccur) {
        //  -? or -h for help.
        usageAndDie(0);
    }

    if (!(options[3].doesOccur && options[4].doesOccur && options[5].doesOccur)) {
        fprintf(stderr, "confusables file, whole script confusables file and output file must all be specified.\n");
        usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
    }
    confFileName   = options[3].value;
    confWSFileName = options[4].value;
    outFileName    = options[5].value;

    if (options[6].doesOccur) {
        u_setDataDirectory(options[6].value);
    }

    status = U_ZERO_ERROR;

    /* Combine the directory with the file name */
    if(options[7].doesOccur) {
        outDir = options[7].value;
    }
    if (options[8].doesOccur) {
        copyright = U_COPYRIGHT_STRING;
    }

#if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO
    // spoof detection data file parsing is dependent on regular expressions.
    // TODO: have the tool return an error status.  Requires fixing the ICU data build
    //       so that it doesn't abort entirely on that error.

    UNewDataMemory *pData;
    char msg[1024];

    /* write message with just the name */
    sprintf(msg, "gencfu writes dummy %s because of UCONFIG_NO_REGULAR_EXPRESSIONS and/or UCONFIG_NO_NORMALIZATION and/or UCONFIG_NO_FILE_IO, see uconfig.h", outFileName);
    fprintf(stderr, "%s\n", msg);

    /* write the dummy data file */
    pData = udata_create(outDir, NULL, outFileName, &dummyDataInfo, NULL, &status);
    udata_writeBlock(pData, msg, strlen(msg));
    udata_finish(pData, &status);
    return (int)status;

#else
    /* Initialize ICU */
    u_init(&status);
    if (U_FAILURE(status)) {
        fprintf(stderr, "%s: can not initialize ICU.  status = %s\n",
            argv[0], u_errorName(status));
        exit(1);
    }
    status = U_ZERO_ERROR;

    //  Read in the confusables source file

    int32_t      confusablesLen = 0;
    const char  *confusables = readFile(confFileName, &confusablesLen);
    if (confusables == NULL) {
        printf("gencfu: error reading file  \"%s\"\n", confFileName);
        exit(-1);
    }

    int32_t     wsConfusablesLen = 0;
    const char *wsConfsables =  readFile(confWSFileName, &wsConfusablesLen);
    if (wsConfsables == NULL) {
        printf("gencfu: error reading file  \"%s\"\n", confFileName);
        exit(-1);
    }

    //
    //  Create the Spoof Detector from the source confusables files.
    //     This will compile the data.
    //
    UParseError parseError;
    parseError.line = 0;
    parseError.offset = 0;
    int32_t errType;
    USpoofChecker *sc = uspoof_openFromSource(confusables, confusablesLen,
                                              wsConfsables, wsConfusablesLen,
                                              &errType, &parseError, &status);
    if (U_FAILURE(status)) {
        const char *errFile = 
            (errType == USPOOF_WHOLE_SCRIPT_CONFUSABLE)? confWSFileName : confFileName;
        fprintf(stderr, "gencfu: uspoof_openFromSource error \"%s\"  at file %s, line %d, column %d\n",
                u_errorName(status), errFile, (int)parseError.line, (int)parseError.offset);
        exit(status);
    };


    //
    //  Get the compiled rule data from the USpoofChecker.
    //
    uint32_t        outDataSize;
    uint8_t        *outData;
    outDataSize = uspoof_serialize(sc, NULL, 0, &status);
    if (status != U_BUFFER_OVERFLOW_ERROR) {
        fprintf(stderr, "gencfu: uspoof_serialize() returned %s\n", u_errorName(status));
        exit(status);
    }
    status = U_ZERO_ERROR;
    outData = new uint8_t[outDataSize];
    uspoof_serialize(sc, outData, outDataSize, &status);

    // Copy the data format version numbers from the spoof data header into the UDataMemory header.
    
    uprv_memcpy(dh.info.formatVersion, 
                reinterpret_cast<SpoofDataHeader *>(outData)->fFormatVersion,
                sizeof(dh.info.formatVersion));

    //
    //  Create the output file
    //
    size_t bytesWritten;
    UNewDataMemory *pData;
    pData = udata_create(outDir, NULL, outFileName, &(dh.info), copyright, &status);
    if(U_FAILURE(status)) {
        fprintf(stderr, "gencfu: Could not open output file \"%s\", \"%s\"\n", 
                         outFileName, u_errorName(status));
        exit(status);
    }


    //  Write the data itself.
    udata_writeBlock(pData, outData, outDataSize);
    // finish up 
    bytesWritten = udata_finish(pData, &status);
    if(U_FAILURE(status)) {
        fprintf(stderr, "gencfu: Error %d writing the output file\n", status);
        exit(status);
    }
    
    if (bytesWritten != outDataSize) {
        fprintf(stderr, "gencfu: Error writing to output file \"%s\"\n", outFileName);
        exit(-1);
    }

    uspoof_close(sc);
    delete [] outData;
    delete [] confusables;
    delete [] wsConfsables;
    u_cleanup();
    printf("gencfu: tool completed successfully.\n");
    return 0;
#endif   // UCONFIG_NO_REGULAR_EXPRESSIONS
}
示例#5
0
static void TestUSpoofCAPI(void) {

    /*
     *  basic uspoof_open().
     */
    {
        USpoofChecker *sc;
        UErrorCode  status = U_ZERO_ERROR;
        sc = uspoof_open(&status);
        TEST_ASSERT_SUCCESS(status);
        if (U_FAILURE(status)) {
            /* If things are so broken that we can't even open a default spoof checker,  */
            /*   don't even try the rest of the tests.  They would all fail.             */
            return;
        }
        uspoof_close(sc);
    }

    
        
    /*
     *  Test Open from source rules.
    */
    TEST_SETUP
    const char *dataSrcDir;
    char       *fileName;
    char       *confusables;
    int         confusablesLength;
    char       *confusablesWholeScript;
    int         confusablesWholeScriptLength;
    FILE       *f;
    UParseError pe;
    int32_t     errType;
    USpoofChecker *rsc;
    
    dataSrcDir = ctest_dataSrcDir();
    fileName = malloc(strlen(dataSrcDir) + 100);
    strcpy(fileName, dataSrcDir);
    strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusables.txt");
    f = fopen(fileName, "r");
    TEST_ASSERT_NE(f, NULL);
    confusables = malloc(3000000);
    confusablesLength = fread(confusables, 1, 3000000, f);
    fclose(f);

    
    strcpy(fileName, dataSrcDir);
    strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusablesWholeScript.txt");
    f = fopen(fileName, "r");
    TEST_ASSERT_NE(f, NULL);
    confusablesWholeScript = malloc(1000000);
    confusablesWholeScriptLength = fread(confusablesWholeScript, 1, 1000000, f);
    fclose(f);

    rsc = uspoof_openFromSource(confusables, confusablesLength,
                                              confusablesWholeScript, confusablesWholeScriptLength,
                                              &errType, &pe, &status);
    TEST_ASSERT_SUCCESS(status);

    free(confusablesWholeScript);
    free(confusables);
    free(fileName);
    uspoof_close(rsc);
    /*  printf("ParseError Line is %d\n", pe.line);  */
    TEST_TEARDOWN;


    /*
     * openFromSerialized and serialize
    */
    TEST_SETUP
        int32_t        serializedSize = 0;
        int32_t        actualLength = 0;
        char           *buf;
        USpoofChecker  *sc2;
        int32_t         checkResults;

        
        serializedSize = uspoof_serialize(sc, NULL, 0, &status);
        TEST_ASSERT_EQ(status, U_BUFFER_OVERFLOW_ERROR);
        TEST_ASSERT(serializedSize > 0);

        /* Serialize the default spoof checker */
        status = U_ZERO_ERROR;
        buf = (char *)malloc(serializedSize + 10);
        TEST_ASSERT(buf != NULL);
        buf[serializedSize] = 42;
        uspoof_serialize(sc, buf, serializedSize, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(42, buf[serializedSize]);

        /* Create a new spoof checker from the freshly serialized data */
        sc2 = uspoof_openFromSerialized(buf, serializedSize+10, &actualLength, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_NE(NULL, sc2);
        TEST_ASSERT_EQ(serializedSize, actualLength);

        /* Verify that the new spoof checker at least wiggles */
        checkResults = uspoof_check(sc2, goodLatin, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);

        checkResults = uspoof_check(sc2, scMixed, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);

        uspoof_close(sc2);
        free(buf);
    TEST_TEARDOWN;
        
        
        
    /*
     * Set & Get Check Flags
    */
    TEST_SETUP
        int32_t t;
        uspoof_setChecks(sc, USPOOF_ALL_CHECKS, &status);
        TEST_ASSERT_SUCCESS(status);
        t = uspoof_getChecks(sc, &status);
        TEST_ASSERT_EQ(t, USPOOF_ALL_CHECKS);
    
        uspoof_setChecks(sc, 0, &status);
        TEST_ASSERT_SUCCESS(status);
        t = uspoof_getChecks(sc, &status);
        TEST_ASSERT_EQ(0, t);
        
        uspoof_setChecks(sc,
                        USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE,
                        &status);
        TEST_ASSERT_SUCCESS(status);
        t = uspoof_getChecks(sc, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, t);
    TEST_TEARDOWN;

    /*
    * get & setAllowedChars
    */
    TEST_SETUP
        USet *us;
        const USet *uset;

        uset = uspoof_getAllowedChars(sc, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT(uset_isFrozen(uset));
        us = uset_open((UChar32)0x41, (UChar32)0x5A);   /*  [A-Z]  */
        uspoof_setAllowedChars(sc, us, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_NE(us, uspoof_getAllowedChars(sc, &status));
        TEST_ASSERT(uset_equals(us, uspoof_getAllowedChars(sc, &status)));
        TEST_ASSERT_SUCCESS(status);
        uset_close(us);
    TEST_TEARDOWN;

    /*
    *  clone()
    */

    TEST_SETUP
        USpoofChecker *clone1 = NULL;
        USpoofChecker *clone2 = NULL;
        int32_t        checkResults = 0;
        
        clone1 = uspoof_clone(sc, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_NE(clone1, sc);

        clone2 = uspoof_clone(clone1, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_NE(clone2, clone1);

        uspoof_close(clone1);
        
        /* Verify that the cloned spoof checker is alive */
        checkResults = uspoof_check(clone2, goodLatin, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);

        checkResults = uspoof_check(clone2, scMixed, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);
        uspoof_close(clone2);
    TEST_TEARDOWN;

    /*
     *  get & set Checks
    */
    TEST_SETUP
        int32_t   checks;
        int32_t   checks2;
        int32_t   checkResults;

        checks = uspoof_getChecks(sc, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_ALL_CHECKS, checks);

        checks &= ~(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE);
        uspoof_setChecks(sc, checks, &status);
        TEST_ASSERT_SUCCESS(status);
        checks2 = uspoof_getChecks(sc, &status);
        TEST_ASSERT_EQ(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 = uspoof_check(sc, scMixed, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);
    TEST_TEARDOWN;
        
    /*
     *  AllowedLoacles
     */

    TEST_SETUP
        const char  *allowedLocales;
        int32_t  checkResults;

        /* Default allowed locales list should be empty */
        allowedLocales = uspoof_getAllowedLocales(sc, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT(strcmp("", allowedLocales) == 0)

        /* Allow en and ru, which should enable Latin and Cyrillic only to pass */
        uspoof_setAllowedLocales(sc, "en, ru_RU", &status);
        TEST_ASSERT_SUCCESS(status);
        allowedLocales = uspoof_getAllowedLocales(sc, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT(strstr(allowedLocales, "en") != NULL);
        TEST_ASSERT(strstr(allowedLocales, "ru") != NULL);

        /* Limit checks to USPOOF_CHAR_LIMIT.  Some of the test data has whole script confusables also,
         * which we don't want to see in this test. */
        uspoof_setChecks(sc, USPOOF_CHAR_LIMIT, &status);
        TEST_ASSERT_SUCCESS(status);

        checkResults = uspoof_check(sc, goodLatin, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);
        
        checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_CHAR_LIMIT, checkResults);

        checkResults = uspoof_check(sc, goodCyrl, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);

        /* Reset with an empty locale list, which should allow all characters to pass */
        uspoof_setAllowedLocales(sc, " ", &status);
        TEST_ASSERT_SUCCESS(status);

        checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);
    TEST_TEARDOWN;

    /*
     * AllowedChars   set/get the USet of allowed characters.
     */
    TEST_SETUP
        const USet  *set;
        USet        *tmpSet;
        int32_t      checkResults;
        
        /* By default, we should see no restriction; the USet should allow all characters. */
        set = uspoof_getAllowedChars(sc, &status);
        TEST_ASSERT_SUCCESS(status);
        tmpSet = uset_open(0, 0x10ffff);
        TEST_ASSERT(uset_equals(tmpSet, set));

        /* Setting the allowed chars should enable the check. */
        uspoof_setChecks(sc, USPOOF_ALL_CHECKS & ~USPOOF_CHAR_LIMIT, &status);
        TEST_ASSERT_SUCCESS(status);

        /* Remove a character that is in our good Latin test identifier from the allowed chars set. */
        uset_remove(tmpSet, goodLatin[1]);
        uspoof_setAllowedChars(sc, tmpSet, &status);
        TEST_ASSERT_SUCCESS(status);
        uset_close(tmpSet);

        /* Latin Identifier should now fail; other non-latin test cases should still be OK */
        checkResults = uspoof_check(sc, goodLatin, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_CHAR_LIMIT, checkResults);

        checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_WHOLE_SCRIPT_CONFUSABLE, checkResults);
    TEST_TEARDOWN;

    /*
     * check UTF-8
     */
    TEST_SETUP
        char    utf8buf[200];
        int32_t checkResults;
        int32_t position;

        u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, goodLatin, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        position = 666;
        checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);
        TEST_ASSERT_EQ(666, position);

        u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, goodCyrl, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);

        u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, scMixed, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        position = 666;
        checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_SINGLE_SCRIPT , checkResults);
        TEST_ASSERT_EQ(2, position);

    TEST_TEARDOWN;

    /*
     * uspoof_areConfusable()
     */
    TEST_SETUP
        int32_t  checkResults;
        
        checkResults = uspoof_areConfusable(sc, scLatin, -1, scMixed, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);

        checkResults = uspoof_areConfusable(sc, goodGreek, -1, scLatin, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);

        checkResults = uspoof_areConfusable(sc, lll_Latin_a, -1, lll_Latin_b, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE, checkResults);

    TEST_TEARDOWN;

    /*
     * areConfusableUTF8
     */
    TEST_SETUP
        int32_t checkResults;
        char s1[200];
        char s2[200];


        u_strToUTF8(s1, sizeof(s1), NULL, scLatin, -1, &status);
        u_strToUTF8(s2, sizeof(s2), NULL, scMixed, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);

        u_strToUTF8(s1, sizeof(s1), NULL, goodGreek, -1, &status);
        u_strToUTF8(s2, sizeof(s2), NULL, scLatin, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, checkResults);
        
        u_strToUTF8(s1, sizeof(s1), NULL, lll_Latin_a, -1, &status);
        u_strToUTF8(s2, sizeof(s2), NULL, lll_Latin_b, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE, checkResults);

    TEST_TEARDOWN;


  /*
   * getSkeleton
   */

    TEST_SETUP
        UChar dest[100];
        int32_t   skelLength;

        skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, dest, sizeof(dest)/sizeof(UChar), &status);
        TEST_ASSERT_SUCCESS(status);
        TEST_ASSERT_EQ(0, u_strcmp(lll_Skel, dest));
        TEST_ASSERT_EQ(u_strlen(lll_Skel), skelLength);

        skelLength = uspoof_getSkeletonUTF8(sc, USPOOF_ANY_CASE, goodLatinUTF8, -1, dest, sizeof(dest)/sizeof(UChar), &status);
        TEST_ASSERT_SUCCESS(status);

        skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, NULL, 0, &status);
        TEST_ASSERT_EQ(U_BUFFER_OVERFLOW_ERROR, status);
        TEST_ASSERT_EQ(3, skelLength);
        status = U_ZERO_ERROR;

    TEST_TEARDOWN;
}
示例#6
0
/*
 *   Spoof Detection C API Tests
 */
static void TestUSpoofCAPI(void) {

    /*
     *  basic uspoof_open().
     */
    {
        USpoofChecker *sc;
        UErrorCode  status = U_ZERO_ERROR;
        sc = uspoof_open(&status);
        TEST_ASSERT_SUCCESS(status);
        if (U_FAILURE(status)) {
            /* If things are so broken that we can't even open a default spoof checker,  */
            /*   don't even try the rest of the tests.  They would all fail.             */
            return;
        }
        uspoof_close(sc);
    }

    /*
     * openFromSerialized and serialize
    */
    TEST_SETUP
    int32_t        serializedSize = 0;
    int32_t        actualLength = 0;
    char           *buf;
    USpoofChecker  *sc2;
    int32_t         checkResults;


    serializedSize = uspoof_serialize(sc, NULL, 0, &status);
    TEST_ASSERT_EQ(status, U_BUFFER_OVERFLOW_ERROR);
    TEST_ASSERT(serializedSize > 0);

    /* Serialize the default spoof checker */
    status = U_ZERO_ERROR;
    buf = (char *)malloc(serializedSize + 10);
    TEST_ASSERT(buf != NULL);
    buf[serializedSize] = 42;
    uspoof_serialize(sc, buf, serializedSize, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(42, buf[serializedSize]);

    /* Create a new spoof checker from the freshly serialized data */
    sc2 = uspoof_openFromSerialized(buf, serializedSize+10, &actualLength, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_NE(NULL, sc2);
    TEST_ASSERT_EQ(serializedSize, actualLength);

    /* Verify that the new spoof checker at least wiggles */
    checkResults = uspoof_check(sc2, goodLatin, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    checkResults = uspoof_check(sc2, scMixed, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);

    uspoof_close(sc2);
    free(buf);
    TEST_TEARDOWN;



    /*
     * Set & Get Check Flags
    */
    TEST_SETUP
    int32_t t;
    uspoof_setChecks(sc, USPOOF_ALL_CHECKS, &status);
    TEST_ASSERT_SUCCESS(status);
    t = uspoof_getChecks(sc, &status);
    TEST_ASSERT_EQ(t, USPOOF_ALL_CHECKS);

    uspoof_setChecks(sc, 0, &status);
    TEST_ASSERT_SUCCESS(status);
    t = uspoof_getChecks(sc, &status);
    TEST_ASSERT_EQ(0, t);

    uspoof_setChecks(sc,
                     USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE,
                     &status);
    TEST_ASSERT_SUCCESS(status);
    t = uspoof_getChecks(sc, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, t);
    TEST_TEARDOWN;

    /*
    * get & setAllowedChars
    */
    TEST_SETUP
    USet *us;
    const USet *uset;

    uset = uspoof_getAllowedChars(sc, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT(uset_isFrozen(uset));
    us = uset_open((UChar32)0x41, (UChar32)0x5A);   /*  [A-Z]  */
    uspoof_setAllowedChars(sc, us, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_NE(us, uspoof_getAllowedChars(sc, &status));
    TEST_ASSERT(uset_equals(us, uspoof_getAllowedChars(sc, &status)));
    TEST_ASSERT_SUCCESS(status);
    uset_close(us);
    TEST_TEARDOWN;

    /*
    *  clone()
    */

    TEST_SETUP
    USpoofChecker *clone1 = NULL;
    USpoofChecker *clone2 = NULL;
    int32_t        checkResults = 0;

    clone1 = uspoof_clone(sc, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_NE(clone1, sc);

    clone2 = uspoof_clone(clone1, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_NE(clone2, clone1);

    uspoof_close(clone1);

    /* Verify that the cloned spoof checker is alive */
    checkResults = uspoof_check(clone2, goodLatin, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    checkResults = uspoof_check(clone2, scMixed, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);
    uspoof_close(clone2);
    TEST_TEARDOWN;

    /*
    *  basic uspoof_check()
    */
    TEST_SETUP
    int32_t result;
    result = uspoof_check(sc, goodLatin, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, result);

    result = uspoof_check(sc, han_Hiragana, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, result);

    result = uspoof_check(sc, scMixed, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE, result);
    TEST_TEARDOWN


    /*
     *  get & set Checks
    */
    TEST_SETUP
    int32_t   checks;
    int32_t   checks2;
    int32_t   checkResults;

    checks = uspoof_getChecks(sc, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_ALL_CHECKS, checks);

    checks &= ~(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE);
    uspoof_setChecks(sc, checks, &status);
    TEST_ASSERT_SUCCESS(status);
    checks2 = uspoof_getChecks(sc, &status);
    TEST_ASSERT_EQ(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 = uspoof_check(sc, scMixed, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);
    TEST_TEARDOWN;

    /*
     *  AllowedLoacles
     */

    TEST_SETUP
    const char  *allowedLocales;
    int32_t  checkResults;

    /* Default allowed locales list should be empty */
    allowedLocales = uspoof_getAllowedLocales(sc, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT(strcmp("", allowedLocales) == 0)

    /* Allow en and ru, which should enable Latin and Cyrillic only to pass */
    uspoof_setAllowedLocales(sc, "en, ru_RU", &status);
    TEST_ASSERT_SUCCESS(status);
    allowedLocales = uspoof_getAllowedLocales(sc, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT(strstr(allowedLocales, "en") != NULL);
    TEST_ASSERT(strstr(allowedLocales, "ru") != NULL);

    /* Limit checks to USPOOF_CHAR_LIMIT.  Some of the test data has whole script confusables also,
     * which we don't want to see in this test. */
    uspoof_setChecks(sc, USPOOF_CHAR_LIMIT, &status);
    TEST_ASSERT_SUCCESS(status);

    checkResults = uspoof_check(sc, goodLatin, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_CHAR_LIMIT, checkResults);

    checkResults = uspoof_check(sc, goodCyrl, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    /* Reset with an empty locale list, which should allow all characters to pass */
    uspoof_setAllowedLocales(sc, " ", &status);
    TEST_ASSERT_SUCCESS(status);

    checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);
    TEST_TEARDOWN;

    /*
     * AllowedChars   set/get the USet of allowed characters.
     */
    TEST_SETUP
    const USet  *set;
    USet        *tmpSet;
    int32_t      checkResults;

    /* By default, we should see no restriction; the USet should allow all characters. */
    set = uspoof_getAllowedChars(sc, &status);
    TEST_ASSERT_SUCCESS(status);
    tmpSet = uset_open(0, 0x10ffff);
    TEST_ASSERT(uset_equals(tmpSet, set));

    /* Setting the allowed chars should enable the check. */
    uspoof_setChecks(sc, USPOOF_ALL_CHECKS & ~USPOOF_CHAR_LIMIT, &status);
    TEST_ASSERT_SUCCESS(status);

    /* Remove a character that is in our good Latin test identifier from the allowed chars set. */
    uset_remove(tmpSet, goodLatin[1]);
    uspoof_setAllowedChars(sc, tmpSet, &status);
    TEST_ASSERT_SUCCESS(status);
    uset_close(tmpSet);

    /* Latin Identifier should now fail; other non-latin test cases should still be OK
     *  Note: fail of CHAR_LIMIT also causes the restriction level to be USPOOF_UNRESTRICTIVE
     *        which will give us a USPOOF_RESTRICTION_LEVEL failure.
     */
    checkResults = uspoof_check(sc, goodLatin, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_CHAR_LIMIT | USPOOF_RESTRICTION_LEVEL, checkResults);

    checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_WHOLE_SCRIPT_CONFUSABLE, checkResults);
    TEST_TEARDOWN;

    /*
     * check UTF-8
     */
    TEST_SETUP
    char    utf8buf[200];
    int32_t checkResults;
    int32_t position;

    u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, goodLatin, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    position = 666;
    checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);
    TEST_ASSERT_EQ(0, position);

    u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, goodCyrl, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, scMixed, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    position = 666;
    checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_SINGLE_SCRIPT , checkResults);
    TEST_ASSERT_EQ(0, position);

    TEST_TEARDOWN;

    /*
     * uspoof_areConfusable()
     */
    TEST_SETUP
    int32_t  checkResults;

    checkResults = uspoof_areConfusable(sc, scLatin, -1, scMixed, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);

    checkResults = uspoof_areConfusable(sc, goodGreek, -1, scLatin, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    checkResults = uspoof_areConfusable(sc, lll_Latin_a, -1, lll_Latin_b, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE, checkResults);

    TEST_TEARDOWN;

    /*
     * areConfusableUTF8
     */
    TEST_SETUP
    int32_t checkResults;
    char s1[200];
    char s2[200];


    u_strToUTF8(s1, sizeof(s1), NULL, scLatin, -1, &status);
    u_strToUTF8(s2, sizeof(s2), NULL, scMixed, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults);

    u_strToUTF8(s1, sizeof(s1), NULL, goodGreek, -1, &status);
    u_strToUTF8(s2, sizeof(s2), NULL, scLatin, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, checkResults);

    u_strToUTF8(s1, sizeof(s1), NULL, lll_Latin_a, -1, &status);
    u_strToUTF8(s2, sizeof(s2), NULL, lll_Latin_b, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE, checkResults);

    TEST_TEARDOWN;


    /*
     * getSkeleton
     */

    TEST_SETUP
    UChar dest[100];
    int32_t   skelLength;

    skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, dest, UPRV_LENGTHOF(dest), &status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(0, u_strcmp(lll_Skel, dest));
    TEST_ASSERT_EQ(u_strlen(lll_Skel), skelLength);

    skelLength = uspoof_getSkeletonUTF8(sc, USPOOF_ANY_CASE, goodLatinUTF8, -1, (char*)dest,
                                        UPRV_LENGTHOF(dest), &status);
    TEST_ASSERT_SUCCESS(status);

    skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, NULL, 0, &status);
    TEST_ASSERT_EQ(U_BUFFER_OVERFLOW_ERROR, status);
    TEST_ASSERT_EQ(3, skelLength);
    status = U_ZERO_ERROR;

    TEST_TEARDOWN;

    /*
     * get Inclusion and Recommended sets
     */
    TEST_SETUP
    const USet *inclusions = NULL;
    const USet *recommended = NULL;

    inclusions = uspoof_getInclusionSet(&status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(TRUE, uset_isFrozen(inclusions));

    status = U_ZERO_ERROR;
    recommended = uspoof_getRecommendedSet(&status);
    TEST_ASSERT_SUCCESS(status);
    TEST_ASSERT_EQ(TRUE, uset_isFrozen(recommended));
    TEST_TEARDOWN;

}
示例#7
0
文件: text_icu.c 项目: bos/text-icu
void __hs_uspoof_close(USpoofChecker *sc)
{
    uspoof_close(sc);
}