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