| OLD | NEW |
| 1 /******************************************************************** | 1 /******************************************************************** |
| 2 * COPYRIGHT: | 2 * COPYRIGHT: |
| 3 * Copyright (c) 2009-2013, International Business Machines Corporation and | 3 * Copyright (c) 2009-2015, International Business Machines Corporation and |
| 4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
| 5 ********************************************************************/ | 5 ********************************************************************/ |
| 6 /*******************************************************************************
* | 6 /*******************************************************************************
* |
| 7 * | 7 * |
| 8 * File spooftest.c | 8 * File spooftest.c |
| 9 * | 9 * |
| 10 ********************************************************************************
*/ | 10 ********************************************************************************
*/ |
| 11 /*C API TEST for the uspoof Unicode Indentifier Spoofing and Security API */ | 11 /*C API TEST for the uspoof Unicode Indentifier Spoofing and Security API */ |
| 12 /** | 12 /** |
| 13 * This is an API test for ICU spoof detection in plain C. It doesn't test ver
y many cases, and doesn't | 13 * This is an API test for ICU spoof detection in plain C. It doesn't test ver
y many cases, and doesn't |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 sc = uspoof_open(&status); \ | 55 sc = uspoof_open(&status); \ |
| 56 TEST_ASSERT_SUCCESS(status); \ | 56 TEST_ASSERT_SUCCESS(status); \ |
| 57 if (U_SUCCESS(status)){ | 57 if (U_SUCCESS(status)){ |
| 58 | 58 |
| 59 #define TEST_TEARDOWN \ | 59 #define TEST_TEARDOWN \ |
| 60 } \ | 60 } \ |
| 61 TEST_ASSERT_SUCCESS(status); \ | 61 TEST_ASSERT_SUCCESS(status); \ |
| 62 uspoof_close(sc); \ | 62 uspoof_close(sc); \ |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 static void TestOpenFromSource(void); |
| 66 static void TestUSpoofCAPI(void); | 66 static void TestUSpoofCAPI(void); |
| 67 | 67 |
| 68 void addUSpoofTest(TestNode** root); | 68 void addUSpoofTest(TestNode** root); |
| 69 | 69 |
| 70 void addUSpoofTest(TestNode** root) | 70 void addUSpoofTest(TestNode** root) |
| 71 { | 71 { |
| 72 #if !UCONFIG_NO_FILE_IO | 72 #if !UCONFIG_NO_FILE_IO |
| 73 addTest(root, &TestOpenFromSource, "uspoof/TestOpenFromSource"); |
| 74 #endif |
| 73 addTest(root, &TestUSpoofCAPI, "uspoof/TestUSpoofCAPI"); | 75 addTest(root, &TestUSpoofCAPI, "uspoof/TestUSpoofCAPI"); |
| 74 #endif | |
| 75 } | 76 } |
| 76 | 77 |
| 77 /* | 78 /* |
| 78 * Identifiers for verifying that spoof checking is minimally alive and working
. | 79 * Identifiers for verifying that spoof checking is minimally alive and working
. |
| 79 */ | 80 */ |
| 80 const UChar goodLatin[] = {(UChar)0x75, (UChar)0x7a, 0}; /* "uz", all ASCII
*/ | 81 const UChar goodLatin[] = {(UChar)0x75, (UChar)0x7a, 0}; /* "uz", all ASCII
*/ |
| 81 /* (not confusable
) */ | 82 /* (not confusable
) */ |
| 82 const UChar scMixed[] = {(UChar)0x73, (UChar)0x0441, 0}; /* "sc", with Cyrill
ic 'c' */ | 83 const UChar scMixed[] = {(UChar)0x73, (UChar)0x0441, 0}; /* "sc", with Cyrill
ic 'c' */ |
| 83 /* (mixed script,
confusable */ | 84 /* (mixed script,
confusable */ |
| 84 | 85 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 95 | 96 |
| 96 const UChar lll_Cyrl[] = {(UChar)0x0406, (UChar)0x04C0, (UChar)0x31, 0}; | 97 const UChar lll_Cyrl[] = {(UChar)0x0406, (UChar)0x04C0, (UChar)0x31, 0}; |
| 97 | 98 |
| 98 /* The skeleton transform for all of thes 'lll' lookalikes is all lower case l.
*/ | 99 /* The skeleton transform for all of thes 'lll' lookalikes is all lower case l.
*/ |
| 99 const UChar lll_Skel[] = {(UChar)0x6c, (UChar)0x6c, (UChar)0x6c, 0}; | 100 const UChar lll_Skel[] = {(UChar)0x6c, (UChar)0x6c, (UChar)0x6c, 0}; |
| 100 | 101 |
| 101 const UChar han_Hiragana[] = {(UChar)0x3086, (UChar)0x308A, (UChar)0x0020, (UCha
r)0x77F3, (UChar)0x7530, 0}; | 102 const UChar han_Hiragana[] = {(UChar)0x3086, (UChar)0x308A, (UChar)0x0020, (UCha
r)0x77F3, (UChar)0x7530, 0}; |
| 102 | 103 |
| 103 /* Provide better code coverage */ | 104 /* Provide better code coverage */ |
| 104 const char goodLatinUTF8[] = {0x75, 0x77, 0}; | 105 const char goodLatinUTF8[] = {0x75, 0x77, 0}; |
| 105 /* | |
| 106 * Spoof Detction C API Tests | |
| 107 */ | |
| 108 static void TestUSpoofCAPI(void) { | |
| 109 | 106 |
| 110 /* | 107 // Test open from source rules. |
| 111 * basic uspoof_open(). | 108 // Run this in isolation to verify initialization. |
| 112 */ | 109 static void TestOpenFromSource() { |
| 113 { | 110 // No TEST_SETUP because that calls uspoof_open(). |
| 114 USpoofChecker *sc; | 111 UErrorCode status = U_ZERO_ERROR; |
| 115 UErrorCode status = U_ZERO_ERROR; | |
| 116 sc = uspoof_open(&status); | |
| 117 TEST_ASSERT_SUCCESS(status); | |
| 118 if (U_FAILURE(status)) { | |
| 119 /* If things are so broken that we can't even open a default spoof c
hecker, */ | |
| 120 /* don't even try the rest of the tests. They would all fail.
*/ | |
| 121 return; | |
| 122 } | |
| 123 uspoof_close(sc); | |
| 124 } | |
| 125 | |
| 126 | |
| 127 | |
| 128 /* | |
| 129 * Test Open from source rules. | |
| 130 */ | |
| 131 TEST_SETUP | |
| 132 const char *dataSrcDir; | 112 const char *dataSrcDir; |
| 133 char *fileName; | 113 char *fileName; |
| 134 char *confusables; | 114 char *confusables; |
| 135 int confusablesLength = 0; | 115 int confusablesLength = 0; |
| 136 char *confusablesWholeScript; | 116 char *confusablesWholeScript; |
| 137 int confusablesWholeScriptLength = 0; | 117 int confusablesWholeScriptLength = 0; |
| 138 FILE *f; | 118 FILE *f; |
| 139 UParseError pe; | 119 UParseError pe; |
| 140 int32_t errType; | 120 int32_t errType; |
| 121 int32_t checkResults; |
| 141 USpoofChecker *rsc; | 122 USpoofChecker *rsc; |
| 142 | 123 |
| 143 dataSrcDir = ctest_dataSrcDir(); | 124 dataSrcDir = ctest_dataSrcDir(); |
| 144 fileName = malloc(strlen(dataSrcDir) + 100); | 125 fileName = malloc(strlen(dataSrcDir) + 100); |
| 145 strcpy(fileName, dataSrcDir); | 126 strcpy(fileName, dataSrcDir); |
| 146 strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusables.
txt"); | 127 strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusables.
txt"); |
| 147 f = fopen(fileName, "rb"); | 128 f = fopen(fileName, "rb"); |
| 148 TEST_ASSERT_NE(f, NULL); | 129 TEST_ASSERT_NE(f, NULL); |
| 149 confusables = malloc(3000000); | 130 confusables = malloc(3000000); |
| 150 if (f != NULL) { | 131 if (f != NULL) { |
| 151 confusablesLength = fread(confusables, 1, 3000000, f); | 132 confusablesLength = fread(confusables, 1, 3000000, f); |
| 152 fclose(f); | 133 fclose(f); |
| 153 } | 134 } |
| 154 | 135 |
| 155 strcpy(fileName, dataSrcDir); | 136 strcpy(fileName, dataSrcDir); |
| 156 strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusablesW
holeScript.txt"); | 137 strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusablesW
holeScript.txt"); |
| 157 f = fopen(fileName, "rb"); | 138 f = fopen(fileName, "rb"); |
| 158 TEST_ASSERT_NE(f, NULL); | 139 TEST_ASSERT_NE(f, NULL); |
| 159 confusablesWholeScript = malloc(1000000); | 140 confusablesWholeScript = malloc(1000000); |
| 160 if (f != NULL) { | 141 if (f != NULL) { |
| 161 confusablesWholeScriptLength = fread(confusablesWholeScript, 1, 1000000,
f); | 142 confusablesWholeScriptLength = fread(confusablesWholeScript, 1, 1000000,
f); |
| 162 fclose(f); | 143 fclose(f); |
| 163 } | 144 } |
| 164 | 145 |
| 165 rsc = uspoof_openFromSource(confusables, confusablesLength, | 146 rsc = uspoof_openFromSource(confusables, confusablesLength, |
| 166 confusablesWholeScript, confusable
sWholeScriptLength, | 147 confusablesWholeScript, confusablesWholeScriptLe
ngth, |
| 167 &errType, &pe, &status); | 148 &errType, &pe, &status); |
| 168 TEST_ASSERT_SUCCESS(status); | 149 TEST_ASSERT_SUCCESS(status); |
| 169 | 150 |
| 151 // Ticket #11860: uspoof_openFromSource() did not initialize for use. |
| 152 // Verify that the spoof checker does not crash. |
| 153 checkResults = uspoof_check(rsc, goodLatin, -1, NULL, &status); |
| 154 TEST_ASSERT_SUCCESS(status); |
| 155 TEST_ASSERT_EQ(0, checkResults); |
| 156 |
| 170 free(confusablesWholeScript); | 157 free(confusablesWholeScript); |
| 171 free(confusables); | 158 free(confusables); |
| 172 free(fileName); | 159 free(fileName); |
| 173 uspoof_close(rsc); | 160 uspoof_close(rsc); |
| 174 /* printf("ParseError Line is %d\n", pe.line); */ | 161 /* printf("ParseError Line is %d\n", pe.line); */ |
| 175 TEST_TEARDOWN; | 162 } |
| 176 | 163 |
| 164 /* |
| 165 * Spoof Detection C API Tests |
| 166 */ |
| 167 static void TestUSpoofCAPI(void) { |
| 168 |
| 169 /* |
| 170 * basic uspoof_open(). |
| 171 */ |
| 172 { |
| 173 USpoofChecker *sc; |
| 174 UErrorCode status = U_ZERO_ERROR; |
| 175 sc = uspoof_open(&status); |
| 176 TEST_ASSERT_SUCCESS(status); |
| 177 if (U_FAILURE(status)) { |
| 178 /* If things are so broken that we can't even open a default spoof c
hecker, */ |
| 179 /* don't even try the rest of the tests. They would all fail.
*/ |
| 180 return; |
| 181 } |
| 182 uspoof_close(sc); |
| 183 } |
| 177 | 184 |
| 178 /* | 185 /* |
| 179 * openFromSerialized and serialize | 186 * openFromSerialized and serialize |
| 180 */ | 187 */ |
| 181 TEST_SETUP | 188 TEST_SETUP |
| 182 int32_t serializedSize = 0; | 189 int32_t serializedSize = 0; |
| 183 int32_t actualLength = 0; | 190 int32_t actualLength = 0; |
| 184 char *buf; | 191 char *buf; |
| 185 USpoofChecker *sc2; | 192 USpoofChecker *sc2; |
| 186 int32_t checkResults; | 193 int32_t checkResults; |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 skelLength = uspoof_getSkeletonUTF8(sc, USPOOF_ANY_CASE, goodLatinUTF8,
-1, (char*)dest, | 529 skelLength = uspoof_getSkeletonUTF8(sc, USPOOF_ANY_CASE, goodLatinUTF8,
-1, (char*)dest, |
| 523 sizeof(dest)/sizeof(UChar), &status)
; | 530 sizeof(dest)/sizeof(UChar), &status)
; |
| 524 TEST_ASSERT_SUCCESS(status); | 531 TEST_ASSERT_SUCCESS(status); |
| 525 | 532 |
| 526 skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, NU
LL, 0, &status); | 533 skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, NU
LL, 0, &status); |
| 527 TEST_ASSERT_EQ(U_BUFFER_OVERFLOW_ERROR, status); | 534 TEST_ASSERT_EQ(U_BUFFER_OVERFLOW_ERROR, status); |
| 528 TEST_ASSERT_EQ(3, skelLength); | 535 TEST_ASSERT_EQ(3, skelLength); |
| 529 status = U_ZERO_ERROR; | 536 status = U_ZERO_ERROR; |
| 530 | 537 |
| 531 TEST_TEARDOWN; | 538 TEST_TEARDOWN; |
| 539 |
| 540 /* |
| 541 * get Inclusion and Recommended sets |
| 542 */ |
| 543 TEST_SETUP |
| 544 const USet *inclusions = NULL; |
| 545 const USet *recommended = NULL; |
| 546 |
| 547 inclusions = uspoof_getInclusionSet(&status); |
| 548 TEST_ASSERT_SUCCESS(status); |
| 549 TEST_ASSERT_EQ(TRUE, uset_isFrozen(inclusions)); |
| 550 |
| 551 status = U_ZERO_ERROR; |
| 552 recommended = uspoof_getRecommendedSet(&status); |
| 553 TEST_ASSERT_SUCCESS(status); |
| 554 TEST_ASSERT_EQ(TRUE, uset_isFrozen(recommended)); |
| 555 TEST_TEARDOWN; |
| 556 |
| 532 } | 557 } |
| 533 | 558 |
| 534 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ | 559 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ |
| OLD | NEW |