]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
729e4ab9 | 3 | /******************************************************************** |
2ca993e8 A |
4 | * COPYRIGHT: |
5 | * Copyright (c) 2009-2016, International Business Machines Corporation and | |
729e4ab9 A |
6 | * others. All Rights Reserved. |
7 | ********************************************************************/ | |
8 | /******************************************************************************** | |
9 | * | |
10 | * File spooftest.c | |
11 | * | |
12 | *********************************************************************************/ | |
13 | /*C API TEST for the uspoof Unicode Indentifier Spoofing and Security API */ | |
14 | /** | |
15 | * This is an API test for ICU spoof detection in plain C. It doesn't test very many cases, and doesn't | |
16 | * try to test the full functionality. It just calls each function and verifies that it | |
17 | * works on a basic level. | |
18 | * | |
19 | * More complete testing of spoof detection functionality is done with the C++ tests. | |
20 | **/ | |
21 | ||
22 | #include "unicode/utypes.h" | |
23 | #if !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_NORMALIZATION | |
24 | ||
25 | #include <stdlib.h> | |
26 | #include <stdio.h> | |
27 | #include <string.h> | |
28 | #include "unicode/uspoof.h" | |
29 | #include "unicode/ustring.h" | |
30 | #include "unicode/uset.h" | |
31 | #include "cintltst.h" | |
2ca993e8 | 32 | #include "cmemory.h" |
729e4ab9 | 33 | |
340931cb A |
34 | #define TEST_ASSERT_SUCCESS(status) UPRV_BLOCK_MACRO_BEGIN { \ |
35 | if (U_FAILURE(status)) { \ | |
36 | log_err_status(status, "Failure at file %s, line %d, error = %s\n", __FILE__, __LINE__, u_errorName(status)); \ | |
37 | } \ | |
38 | } UPRV_BLOCK_MACRO_END | |
39 | ||
40 | #define TEST_ASSERT(expr) UPRV_BLOCK_MACRO_BEGIN { \ | |
41 | if ((expr)==FALSE) { \ | |
42 | log_err("Test Failure at file %s, line %d: \"%s\" is false.\n", __FILE__, __LINE__, #expr); \ | |
43 | } \ | |
44 | } UPRV_BLOCK_MACRO_END | |
45 | ||
46 | #define TEST_ASSERT_EQ(a, b) UPRV_BLOCK_MACRO_BEGIN { \ | |
47 | if ((a) != (b)) { \ | |
48 | log_err("Test Failure at file %s, line %d: \"%s\" (%d) != \"%s\" (%d) \n", \ | |
49 | __FILE__, __LINE__, #a, (a), #b, (b)); \ | |
50 | } \ | |
51 | } UPRV_BLOCK_MACRO_END | |
52 | ||
53 | #define TEST_ASSERT_NE(a, b) UPRV_BLOCK_MACRO_BEGIN { \ | |
54 | if ((a) == (b)) { \ | |
55 | log_err("Test Failure at file %s, line %d: \"%s\" (%d) == \"%s\" (%d) \n", \ | |
56 | __FILE__, __LINE__, #a, (a), #b, (b)); \ | |
57 | } \ | |
58 | } UPRV_BLOCK_MACRO_END | |
729e4ab9 A |
59 | |
60 | ||
61 | /* | |
62 | * TEST_SETUP and TEST_TEARDOWN | |
63 | * macros to handle the boilerplate around setting up test case. | |
64 | * Put arbitrary test code between SETUP and TEARDOWN. | |
65 | * "sc" is the ready-to-go SpoofChecker for use in the tests. | |
66 | */ | |
340931cb | 67 | #define TEST_SETUP UPRV_BLOCK_MACRO_BEGIN { \ |
729e4ab9 A |
68 | UErrorCode status = U_ZERO_ERROR; \ |
69 | USpoofChecker *sc; \ | |
70 | sc = uspoof_open(&status); \ | |
71 | TEST_ASSERT_SUCCESS(status); \ | |
72 | if (U_SUCCESS(status)){ | |
73 | ||
74 | #define TEST_TEARDOWN \ | |
75 | } \ | |
76 | TEST_ASSERT_SUCCESS(status); \ | |
77 | uspoof_close(sc); \ | |
340931cb | 78 | } UPRV_BLOCK_MACRO_END |
729e4ab9 | 79 | |
2ca993e8 | 80 | static void TestOpenFromSource(void); |
729e4ab9 A |
81 | static void TestUSpoofCAPI(void); |
82 | ||
83 | void addUSpoofTest(TestNode** root); | |
84 | ||
85 | void addUSpoofTest(TestNode** root) | |
86 | { | |
87 | #if !UCONFIG_NO_FILE_IO | |
2ca993e8 | 88 | addTest(root, &TestOpenFromSource, "uspoof/TestOpenFromSource"); |
729e4ab9 | 89 | #endif |
2ca993e8 | 90 | addTest(root, &TestUSpoofCAPI, "uspoof/TestUSpoofCAPI"); |
729e4ab9 A |
91 | } |
92 | ||
93 | /* | |
94 | * Identifiers for verifying that spoof checking is minimally alive and working. | |
95 | */ | |
96 | const UChar goodLatin[] = {(UChar)0x75, (UChar)0x7a, 0}; /* "uz", all ASCII */ | |
97 | /* (not confusable) */ | |
98 | const UChar scMixed[] = {(UChar)0x73, (UChar)0x0441, 0}; /* "sc", with Cyrillic 'c' */ | |
99 | /* (mixed script, confusable */ | |
100 | ||
101 | const UChar scLatin[] = {(UChar)0x73, (UChar)0x63, 0}; /* "sc", plain ascii. */ | |
102 | const UChar goodCyrl[] = {(UChar)0x438, (UChar)0x43B, 0}; /* Plain lower case Cyrillic letters, | |
103 | no latin confusables */ | |
104 | ||
105 | const UChar goodGreek[] = {(UChar)0x3c0, (UChar)0x3c6, 0}; /* Plain lower case Greek letters */ | |
106 | ||
107 | const UChar lll_Latin_a[] = {(UChar)0x6c, (UChar)0x49, (UChar)0x31, 0}; /* lI1, all ASCII */ | |
108 | ||
109 | /* Full-width I, Small Roman Numeral fifty, Latin Cap Letter IOTA*/ | |
110 | const UChar lll_Latin_b[] = {(UChar)0xff29, (UChar)0x217c, (UChar)0x196, 0}; | |
111 | ||
112 | const UChar lll_Cyrl[] = {(UChar)0x0406, (UChar)0x04C0, (UChar)0x31, 0}; | |
113 | ||
114 | /* The skeleton transform for all of thes 'lll' lookalikes is all lower case l. */ | |
115 | const UChar lll_Skel[] = {(UChar)0x6c, (UChar)0x6c, (UChar)0x6c, 0}; | |
116 | ||
4388f060 A |
117 | const UChar han_Hiragana[] = {(UChar)0x3086, (UChar)0x308A, (UChar)0x0020, (UChar)0x77F3, (UChar)0x7530, 0}; |
118 | ||
729e4ab9 A |
119 | /* Provide better code coverage */ |
120 | const char goodLatinUTF8[] = {0x75, 0x77, 0}; | |
729e4ab9 | 121 | |
2ca993e8 A |
122 | // Test open from source rules. |
123 | // Run this in isolation to verify initialization. | |
124 | static void TestOpenFromSource() { | |
125 | // No TEST_SETUP because that calls uspoof_open(). | |
126 | UErrorCode status = U_ZERO_ERROR; | |
729e4ab9 A |
127 | const char *dataSrcDir; |
128 | char *fileName; | |
129 | char *confusables; | |
57a6839d | 130 | int confusablesLength = 0; |
729e4ab9 | 131 | char *confusablesWholeScript; |
57a6839d | 132 | int confusablesWholeScriptLength = 0; |
729e4ab9 A |
133 | FILE *f; |
134 | UParseError pe; | |
135 | int32_t errType; | |
2ca993e8 | 136 | int32_t checkResults; |
729e4ab9 | 137 | USpoofChecker *rsc; |
2ca993e8 | 138 | |
729e4ab9 A |
139 | dataSrcDir = ctest_dataSrcDir(); |
140 | fileName = malloc(strlen(dataSrcDir) + 100); | |
141 | strcpy(fileName, dataSrcDir); | |
142 | strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusables.txt"); | |
4388f060 | 143 | f = fopen(fileName, "rb"); |
729e4ab9 A |
144 | TEST_ASSERT_NE(f, NULL); |
145 | confusables = malloc(3000000); | |
51004dcb | 146 | if (f != NULL) { |
3d1f044b | 147 | confusablesLength = (int)fread(confusables, 1, 3000000, f); |
57a6839d | 148 | fclose(f); |
51004dcb | 149 | } |
729e4ab9 | 150 | |
729e4ab9 A |
151 | strcpy(fileName, dataSrcDir); |
152 | strcat(fileName, U_FILE_SEP_STRING "unidata" U_FILE_SEP_STRING "confusablesWholeScript.txt"); | |
4388f060 | 153 | f = fopen(fileName, "rb"); |
729e4ab9 A |
154 | TEST_ASSERT_NE(f, NULL); |
155 | confusablesWholeScript = malloc(1000000); | |
51004dcb | 156 | if (f != NULL) { |
3d1f044b | 157 | confusablesWholeScriptLength = (int)fread(confusablesWholeScript, 1, 1000000, f); |
57a6839d | 158 | fclose(f); |
51004dcb | 159 | } |
729e4ab9 A |
160 | |
161 | rsc = uspoof_openFromSource(confusables, confusablesLength, | |
2ca993e8 A |
162 | confusablesWholeScript, confusablesWholeScriptLength, |
163 | &errType, &pe, &status); | |
729e4ab9 A |
164 | TEST_ASSERT_SUCCESS(status); |
165 | ||
2ca993e8 A |
166 | // Ticket #11860: uspoof_openFromSource() did not initialize for use. |
167 | // Verify that the spoof checker does not crash. | |
168 | checkResults = uspoof_check(rsc, goodLatin, -1, NULL, &status); | |
169 | TEST_ASSERT_SUCCESS(status); | |
170 | TEST_ASSERT_EQ(0, checkResults); | |
171 | ||
729e4ab9 A |
172 | free(confusablesWholeScript); |
173 | free(confusables); | |
174 | free(fileName); | |
175 | uspoof_close(rsc); | |
176 | /* printf("ParseError Line is %d\n", pe.line); */ | |
2ca993e8 | 177 | } |
729e4ab9 | 178 | |
2ca993e8 A |
179 | /* |
180 | * Spoof Detection C API Tests | |
181 | */ | |
182 | static void TestUSpoofCAPI(void) { | |
183 | ||
184 | /* | |
185 | * basic uspoof_open(). | |
186 | */ | |
187 | { | |
188 | USpoofChecker *sc; | |
189 | UErrorCode status = U_ZERO_ERROR; | |
190 | sc = uspoof_open(&status); | |
191 | TEST_ASSERT_SUCCESS(status); | |
192 | if (U_FAILURE(status)) { | |
193 | /* If things are so broken that we can't even open a default spoof checker, */ | |
194 | /* don't even try the rest of the tests. They would all fail. */ | |
195 | return; | |
196 | } | |
197 | uspoof_close(sc); | |
198 | } | |
729e4ab9 A |
199 | |
200 | /* | |
201 | * openFromSerialized and serialize | |
202 | */ | |
203 | TEST_SETUP | |
204 | int32_t serializedSize = 0; | |
205 | int32_t actualLength = 0; | |
206 | char *buf; | |
207 | USpoofChecker *sc2; | |
208 | int32_t checkResults; | |
209 | ||
210 | ||
211 | serializedSize = uspoof_serialize(sc, NULL, 0, &status); | |
212 | TEST_ASSERT_EQ(status, U_BUFFER_OVERFLOW_ERROR); | |
213 | TEST_ASSERT(serializedSize > 0); | |
214 | ||
215 | /* Serialize the default spoof checker */ | |
216 | status = U_ZERO_ERROR; | |
217 | buf = (char *)malloc(serializedSize + 10); | |
218 | TEST_ASSERT(buf != NULL); | |
219 | buf[serializedSize] = 42; | |
220 | uspoof_serialize(sc, buf, serializedSize, &status); | |
221 | TEST_ASSERT_SUCCESS(status); | |
222 | TEST_ASSERT_EQ(42, buf[serializedSize]); | |
223 | ||
224 | /* Create a new spoof checker from the freshly serialized data */ | |
225 | sc2 = uspoof_openFromSerialized(buf, serializedSize+10, &actualLength, &status); | |
226 | TEST_ASSERT_SUCCESS(status); | |
227 | TEST_ASSERT_NE(NULL, sc2); | |
228 | TEST_ASSERT_EQ(serializedSize, actualLength); | |
229 | ||
230 | /* Verify that the new spoof checker at least wiggles */ | |
231 | checkResults = uspoof_check(sc2, goodLatin, -1, NULL, &status); | |
232 | TEST_ASSERT_SUCCESS(status); | |
233 | TEST_ASSERT_EQ(0, checkResults); | |
234 | ||
235 | checkResults = uspoof_check(sc2, scMixed, -1, NULL, &status); | |
236 | TEST_ASSERT_SUCCESS(status); | |
f3c0d7a5 | 237 | TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT, checkResults); |
729e4ab9 A |
238 | |
239 | uspoof_close(sc2); | |
240 | free(buf); | |
241 | TEST_TEARDOWN; | |
242 | ||
243 | ||
244 | ||
245 | /* | |
246 | * Set & Get Check Flags | |
247 | */ | |
248 | TEST_SETUP | |
249 | int32_t t; | |
250 | uspoof_setChecks(sc, USPOOF_ALL_CHECKS, &status); | |
251 | TEST_ASSERT_SUCCESS(status); | |
252 | t = uspoof_getChecks(sc, &status); | |
253 | TEST_ASSERT_EQ(t, USPOOF_ALL_CHECKS); | |
254 | ||
255 | uspoof_setChecks(sc, 0, &status); | |
256 | TEST_ASSERT_SUCCESS(status); | |
257 | t = uspoof_getChecks(sc, &status); | |
258 | TEST_ASSERT_EQ(0, t); | |
259 | ||
260 | uspoof_setChecks(sc, | |
261 | USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, | |
262 | &status); | |
263 | TEST_ASSERT_SUCCESS(status); | |
264 | t = uspoof_getChecks(sc, &status); | |
265 | TEST_ASSERT_SUCCESS(status); | |
266 | TEST_ASSERT_EQ(USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE, t); | |
267 | TEST_TEARDOWN; | |
268 | ||
269 | /* | |
270 | * get & setAllowedChars | |
271 | */ | |
272 | TEST_SETUP | |
273 | USet *us; | |
274 | const USet *uset; | |
275 | ||
276 | uset = uspoof_getAllowedChars(sc, &status); | |
277 | TEST_ASSERT_SUCCESS(status); | |
278 | TEST_ASSERT(uset_isFrozen(uset)); | |
279 | us = uset_open((UChar32)0x41, (UChar32)0x5A); /* [A-Z] */ | |
280 | uspoof_setAllowedChars(sc, us, &status); | |
281 | TEST_ASSERT_SUCCESS(status); | |
282 | TEST_ASSERT_NE(us, uspoof_getAllowedChars(sc, &status)); | |
283 | TEST_ASSERT(uset_equals(us, uspoof_getAllowedChars(sc, &status))); | |
284 | TEST_ASSERT_SUCCESS(status); | |
285 | uset_close(us); | |
286 | TEST_TEARDOWN; | |
287 | ||
288 | /* | |
289 | * clone() | |
290 | */ | |
291 | ||
292 | TEST_SETUP | |
293 | USpoofChecker *clone1 = NULL; | |
294 | USpoofChecker *clone2 = NULL; | |
295 | int32_t checkResults = 0; | |
296 | ||
297 | clone1 = uspoof_clone(sc, &status); | |
298 | TEST_ASSERT_SUCCESS(status); | |
299 | TEST_ASSERT_NE(clone1, sc); | |
300 | ||
301 | clone2 = uspoof_clone(clone1, &status); | |
302 | TEST_ASSERT_SUCCESS(status); | |
303 | TEST_ASSERT_NE(clone2, clone1); | |
304 | ||
305 | uspoof_close(clone1); | |
306 | ||
307 | /* Verify that the cloned spoof checker is alive */ | |
308 | checkResults = uspoof_check(clone2, goodLatin, -1, NULL, &status); | |
309 | TEST_ASSERT_SUCCESS(status); | |
310 | TEST_ASSERT_EQ(0, checkResults); | |
311 | ||
312 | checkResults = uspoof_check(clone2, scMixed, -1, NULL, &status); | |
313 | TEST_ASSERT_SUCCESS(status); | |
f3c0d7a5 | 314 | TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT, checkResults); |
729e4ab9 A |
315 | uspoof_close(clone2); |
316 | TEST_TEARDOWN; | |
317 | ||
4388f060 A |
318 | /* |
319 | * basic uspoof_check() | |
320 | */ | |
321 | TEST_SETUP | |
322 | int32_t result; | |
323 | result = uspoof_check(sc, goodLatin, -1, NULL, &status); | |
324 | TEST_ASSERT_SUCCESS(status); | |
325 | TEST_ASSERT_EQ(0, result); | |
326 | ||
327 | result = uspoof_check(sc, han_Hiragana, -1, NULL, &status); | |
328 | TEST_ASSERT_SUCCESS(status); | |
329 | TEST_ASSERT_EQ(0, result); | |
330 | ||
331 | result = uspoof_check(sc, scMixed, -1, NULL, &status); | |
332 | TEST_ASSERT_SUCCESS(status); | |
f3c0d7a5 | 333 | TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT, result); |
340931cb | 334 | TEST_TEARDOWN; |
4388f060 A |
335 | |
336 | ||
729e4ab9 A |
337 | /* |
338 | * get & set Checks | |
339 | */ | |
340 | TEST_SETUP | |
341 | int32_t checks; | |
342 | int32_t checks2; | |
343 | int32_t checkResults; | |
344 | ||
345 | checks = uspoof_getChecks(sc, &status); | |
346 | TEST_ASSERT_SUCCESS(status); | |
347 | TEST_ASSERT_EQ(USPOOF_ALL_CHECKS, checks); | |
348 | ||
349 | checks &= ~(USPOOF_SINGLE_SCRIPT | USPOOF_MIXED_SCRIPT_CONFUSABLE); | |
350 | uspoof_setChecks(sc, checks, &status); | |
351 | TEST_ASSERT_SUCCESS(status); | |
352 | checks2 = uspoof_getChecks(sc, &status); | |
353 | TEST_ASSERT_EQ(checks, checks2); | |
354 | ||
355 | /* The checks that were disabled just above are the same ones that the "scMixed" test fails. | |
356 | So with those tests gone checking that Identifier should now succeed */ | |
357 | checkResults = uspoof_check(sc, scMixed, -1, NULL, &status); | |
358 | TEST_ASSERT_SUCCESS(status); | |
359 | TEST_ASSERT_EQ(0, checkResults); | |
360 | TEST_TEARDOWN; | |
361 | ||
362 | /* | |
363 | * AllowedLoacles | |
364 | */ | |
365 | ||
366 | TEST_SETUP | |
367 | const char *allowedLocales; | |
368 | int32_t checkResults; | |
369 | ||
370 | /* Default allowed locales list should be empty */ | |
371 | allowedLocales = uspoof_getAllowedLocales(sc, &status); | |
372 | TEST_ASSERT_SUCCESS(status); | |
340931cb | 373 | TEST_ASSERT(strcmp("", allowedLocales) == 0); |
729e4ab9 A |
374 | |
375 | /* Allow en and ru, which should enable Latin and Cyrillic only to pass */ | |
376 | uspoof_setAllowedLocales(sc, "en, ru_RU", &status); | |
377 | TEST_ASSERT_SUCCESS(status); | |
378 | allowedLocales = uspoof_getAllowedLocales(sc, &status); | |
379 | TEST_ASSERT_SUCCESS(status); | |
380 | TEST_ASSERT(strstr(allowedLocales, "en") != NULL); | |
381 | TEST_ASSERT(strstr(allowedLocales, "ru") != NULL); | |
382 | ||
383 | /* Limit checks to USPOOF_CHAR_LIMIT. Some of the test data has whole script confusables also, | |
384 | * which we don't want to see in this test. */ | |
385 | uspoof_setChecks(sc, USPOOF_CHAR_LIMIT, &status); | |
386 | TEST_ASSERT_SUCCESS(status); | |
387 | ||
388 | checkResults = uspoof_check(sc, goodLatin, -1, NULL, &status); | |
389 | TEST_ASSERT_SUCCESS(status); | |
390 | TEST_ASSERT_EQ(0, checkResults); | |
391 | ||
392 | checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status); | |
393 | TEST_ASSERT_SUCCESS(status); | |
394 | TEST_ASSERT_EQ(USPOOF_CHAR_LIMIT, checkResults); | |
395 | ||
396 | checkResults = uspoof_check(sc, goodCyrl, -1, NULL, &status); | |
397 | TEST_ASSERT_SUCCESS(status); | |
398 | TEST_ASSERT_EQ(0, checkResults); | |
399 | ||
400 | /* Reset with an empty locale list, which should allow all characters to pass */ | |
401 | uspoof_setAllowedLocales(sc, " ", &status); | |
402 | TEST_ASSERT_SUCCESS(status); | |
403 | ||
404 | checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status); | |
405 | TEST_ASSERT_SUCCESS(status); | |
406 | TEST_ASSERT_EQ(0, checkResults); | |
407 | TEST_TEARDOWN; | |
408 | ||
409 | /* | |
410 | * AllowedChars set/get the USet of allowed characters. | |
411 | */ | |
412 | TEST_SETUP | |
413 | const USet *set; | |
414 | USet *tmpSet; | |
415 | int32_t checkResults; | |
416 | ||
417 | /* By default, we should see no restriction; the USet should allow all characters. */ | |
418 | set = uspoof_getAllowedChars(sc, &status); | |
419 | TEST_ASSERT_SUCCESS(status); | |
420 | tmpSet = uset_open(0, 0x10ffff); | |
421 | TEST_ASSERT(uset_equals(tmpSet, set)); | |
422 | ||
423 | /* Setting the allowed chars should enable the check. */ | |
424 | uspoof_setChecks(sc, USPOOF_ALL_CHECKS & ~USPOOF_CHAR_LIMIT, &status); | |
425 | TEST_ASSERT_SUCCESS(status); | |
426 | ||
427 | /* Remove a character that is in our good Latin test identifier from the allowed chars set. */ | |
428 | uset_remove(tmpSet, goodLatin[1]); | |
429 | uspoof_setAllowedChars(sc, tmpSet, &status); | |
430 | TEST_ASSERT_SUCCESS(status); | |
431 | uset_close(tmpSet); | |
432 | ||
51004dcb A |
433 | /* Latin Identifier should now fail; other non-latin test cases should still be OK |
434 | * Note: fail of CHAR_LIMIT also causes the restriction level to be USPOOF_UNRESTRICTIVE | |
435 | * which will give us a USPOOF_RESTRICTION_LEVEL failure. | |
436 | */ | |
729e4ab9 A |
437 | checkResults = uspoof_check(sc, goodLatin, -1, NULL, &status); |
438 | TEST_ASSERT_SUCCESS(status); | |
51004dcb | 439 | TEST_ASSERT_EQ(USPOOF_CHAR_LIMIT | USPOOF_RESTRICTION_LEVEL, checkResults); |
729e4ab9 A |
440 | |
441 | checkResults = uspoof_check(sc, goodGreek, -1, NULL, &status); | |
442 | TEST_ASSERT_SUCCESS(status); | |
f3c0d7a5 | 443 | TEST_ASSERT_EQ(0, checkResults); |
729e4ab9 A |
444 | TEST_TEARDOWN; |
445 | ||
446 | /* | |
447 | * check UTF-8 | |
448 | */ | |
449 | TEST_SETUP | |
450 | char utf8buf[200]; | |
f3c0d7a5 | 451 | int32_t checkResults, checkResults2; |
729e4ab9 A |
452 | int32_t position; |
453 | ||
454 | u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, goodLatin, -1, &status); | |
455 | TEST_ASSERT_SUCCESS(status); | |
456 | position = 666; | |
457 | checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status); | |
458 | TEST_ASSERT_SUCCESS(status); | |
459 | TEST_ASSERT_EQ(0, checkResults); | |
51004dcb | 460 | TEST_ASSERT_EQ(0, position); |
729e4ab9 A |
461 | |
462 | u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, goodCyrl, -1, &status); | |
463 | TEST_ASSERT_SUCCESS(status); | |
464 | checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status); | |
465 | TEST_ASSERT_SUCCESS(status); | |
466 | TEST_ASSERT_EQ(0, checkResults); | |
467 | ||
468 | u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, scMixed, -1, &status); | |
469 | TEST_ASSERT_SUCCESS(status); | |
470 | position = 666; | |
471 | checkResults = uspoof_checkUTF8(sc, utf8buf, -1, &position, &status); | |
f3c0d7a5 | 472 | checkResults2 = uspoof_check(sc, scMixed, -1, NULL, &status); |
729e4ab9 | 473 | TEST_ASSERT_SUCCESS(status); |
f3c0d7a5 | 474 | TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT , checkResults); |
51004dcb | 475 | TEST_ASSERT_EQ(0, position); |
f3c0d7a5 A |
476 | TEST_ASSERT_EQ(checkResults , checkResults2); |
477 | ||
478 | TEST_TEARDOWN; | |
479 | ||
480 | /* | |
481 | * uspoof_check2 variants | |
482 | */ | |
483 | TEST_SETUP | |
484 | int32_t result1, result2; | |
485 | char utf8buf[200]; | |
486 | uspoof_setChecks(sc, USPOOF_ALL_CHECKS | USPOOF_AUX_INFO, &status); | |
487 | USpoofCheckResult* checkResult = uspoof_openCheckResult(&status); | |
488 | TEST_ASSERT_SUCCESS(status); | |
489 | ||
490 | const UChar* tests[] = { goodLatin, scMixed, scLatin, | |
491 | goodCyrl, goodGreek, lll_Latin_a, lll_Latin_b, han_Hiragana }; | |
492 | ||
493 | for (int32_t i=0; i<UPRV_LENGTHOF(tests); i++) { | |
494 | const UChar* str = tests[i]; | |
495 | ||
496 | // Basic test | |
497 | result1 = uspoof_check(sc, str, -1, NULL, &status); | |
498 | result2 = uspoof_check2(sc, str, -1, NULL, &status); | |
499 | TEST_ASSERT_SUCCESS(status); | |
500 | TEST_ASSERT_EQ(result1, result2); | |
501 | ||
502 | // With check result parameter | |
503 | result1 = uspoof_check(sc, str, -1, NULL, &status); | |
504 | result2 = uspoof_check2(sc, str, -1, checkResult, &status); | |
505 | TEST_ASSERT_SUCCESS(status); | |
506 | TEST_ASSERT_EQ(result1, result2); | |
507 | ||
508 | // Checks from checkResult should be same as those from bitmask | |
509 | TEST_ASSERT_EQ(result1 & USPOOF_ALL_CHECKS, uspoof_getCheckResultChecks(checkResult, &status)); | |
510 | ||
511 | // Restriction level from checkResult should be same as that from bitmask | |
512 | URestrictionLevel restrictionLevel = uspoof_getCheckResultRestrictionLevel(checkResult, &status); | |
513 | TEST_ASSERT_EQ(result1 & restrictionLevel, restrictionLevel); | |
514 | ||
515 | // UTF8 endpoint | |
516 | u_strToUTF8(utf8buf, sizeof(utf8buf), NULL, goodLatin, -1, &status); | |
517 | TEST_ASSERT_SUCCESS(status); | |
518 | result1 = uspoof_checkUTF8(sc, utf8buf, -1, NULL, &status); | |
519 | result2 = uspoof_check2UTF8(sc, utf8buf, -1, NULL, &status); | |
520 | TEST_ASSERT_SUCCESS(status); | |
521 | TEST_ASSERT_EQ(result1, result2); | |
522 | } | |
729e4ab9 | 523 | |
f3c0d7a5 | 524 | uspoof_closeCheckResult(checkResult); |
729e4ab9 A |
525 | TEST_TEARDOWN; |
526 | ||
527 | /* | |
528 | * uspoof_areConfusable() | |
529 | */ | |
530 | TEST_SETUP | |
531 | int32_t checkResults; | |
532 | ||
533 | checkResults = uspoof_areConfusable(sc, scLatin, -1, scMixed, -1, &status); | |
534 | TEST_ASSERT_SUCCESS(status); | |
535 | TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults); | |
536 | ||
537 | checkResults = uspoof_areConfusable(sc, goodGreek, -1, scLatin, -1, &status); | |
538 | TEST_ASSERT_SUCCESS(status); | |
539 | TEST_ASSERT_EQ(0, checkResults); | |
540 | ||
541 | checkResults = uspoof_areConfusable(sc, lll_Latin_a, -1, lll_Latin_b, -1, &status); | |
542 | TEST_ASSERT_SUCCESS(status); | |
543 | TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE, checkResults); | |
544 | ||
545 | TEST_TEARDOWN; | |
546 | ||
547 | /* | |
548 | * areConfusableUTF8 | |
549 | */ | |
550 | TEST_SETUP | |
551 | int32_t checkResults; | |
552 | char s1[200]; | |
553 | char s2[200]; | |
554 | ||
555 | ||
556 | u_strToUTF8(s1, sizeof(s1), NULL, scLatin, -1, &status); | |
557 | u_strToUTF8(s2, sizeof(s2), NULL, scMixed, -1, &status); | |
558 | TEST_ASSERT_SUCCESS(status); | |
559 | checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status); | |
560 | TEST_ASSERT_SUCCESS(status); | |
561 | TEST_ASSERT_EQ(USPOOF_MIXED_SCRIPT_CONFUSABLE, checkResults); | |
562 | ||
563 | u_strToUTF8(s1, sizeof(s1), NULL, goodGreek, -1, &status); | |
564 | u_strToUTF8(s2, sizeof(s2), NULL, scLatin, -1, &status); | |
565 | TEST_ASSERT_SUCCESS(status); | |
566 | checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status); | |
567 | TEST_ASSERT_SUCCESS(status); | |
568 | TEST_ASSERT_EQ(0, checkResults); | |
569 | ||
570 | u_strToUTF8(s1, sizeof(s1), NULL, lll_Latin_a, -1, &status); | |
571 | u_strToUTF8(s2, sizeof(s2), NULL, lll_Latin_b, -1, &status); | |
572 | TEST_ASSERT_SUCCESS(status); | |
573 | checkResults = uspoof_areConfusableUTF8(sc, s1, -1, s2, -1, &status); | |
574 | TEST_ASSERT_SUCCESS(status); | |
575 | TEST_ASSERT_EQ(USPOOF_SINGLE_SCRIPT_CONFUSABLE, checkResults); | |
576 | ||
577 | TEST_TEARDOWN; | |
578 | ||
579 | ||
580 | /* | |
581 | * getSkeleton | |
582 | */ | |
583 | ||
584 | TEST_SETUP | |
585 | UChar dest[100]; | |
586 | int32_t skelLength; | |
587 | ||
2ca993e8 | 588 | skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, dest, UPRV_LENGTHOF(dest), &status); |
729e4ab9 A |
589 | TEST_ASSERT_SUCCESS(status); |
590 | TEST_ASSERT_EQ(0, u_strcmp(lll_Skel, dest)); | |
591 | TEST_ASSERT_EQ(u_strlen(lll_Skel), skelLength); | |
592 | ||
593 | skelLength = uspoof_getSkeletonUTF8(sc, USPOOF_ANY_CASE, goodLatinUTF8, -1, (char*)dest, | |
2ca993e8 | 594 | UPRV_LENGTHOF(dest), &status); |
729e4ab9 A |
595 | TEST_ASSERT_SUCCESS(status); |
596 | ||
597 | skelLength = uspoof_getSkeleton(sc, USPOOF_ANY_CASE, lll_Latin_a, -1, NULL, 0, &status); | |
598 | TEST_ASSERT_EQ(U_BUFFER_OVERFLOW_ERROR, status); | |
599 | TEST_ASSERT_EQ(3, skelLength); | |
600 | status = U_ZERO_ERROR; | |
601 | ||
602 | TEST_TEARDOWN; | |
b331163b A |
603 | |
604 | /* | |
605 | * get Inclusion and Recommended sets | |
606 | */ | |
607 | TEST_SETUP | |
608 | const USet *inclusions = NULL; | |
609 | const USet *recommended = NULL; | |
610 | ||
611 | inclusions = uspoof_getInclusionSet(&status); | |
612 | TEST_ASSERT_SUCCESS(status); | |
613 | TEST_ASSERT_EQ(TRUE, uset_isFrozen(inclusions)); | |
614 | ||
615 | status = U_ZERO_ERROR; | |
616 | recommended = uspoof_getRecommendedSet(&status); | |
617 | TEST_ASSERT_SUCCESS(status); | |
618 | TEST_ASSERT_EQ(TRUE, uset_isFrozen(recommended)); | |
619 | TEST_TEARDOWN; | |
620 | ||
729e4ab9 A |
621 | } |
622 | ||
623 | #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ |