mirror of
https://github.com/zaerl/mojibake.git
synced 2026-11-30 16:48:26 +01:00
* Fix: wrong mjb_confusable_skeleton count Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com> * Remove mjb_test_ from the map Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com> --------- Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>
644 lines
27 KiB
C
644 lines
27 KiB
C
/**
|
||
* The Mojibake library
|
||
*
|
||
* UTS#39 Unicode Security Mechanisms - confusable detection tests.
|
||
*
|
||
* This file is distributed under the MIT License. See LICENSE for details.
|
||
*/
|
||
|
||
#include "../src/mojibake-internal.h"
|
||
#if !defined(MJB_SHARED)
|
||
#include "../src/unicode-tables.h"
|
||
#endif
|
||
#include "test.h"
|
||
|
||
#if MJB_FEATURE_SECURITY
|
||
|
||
#define MJB_TEST_COUNT_OF(array) (sizeof(array) / sizeof((array)[0]))
|
||
|
||
static char *trim_ascii(char *text) {
|
||
while(*text == ' ' || *text == '\t' || *text == '\r' || *text == '\n') {
|
||
++text;
|
||
}
|
||
|
||
char *end = text + strlen(text);
|
||
|
||
while(end > text && (end[-1] == ' ' || end[-1] == '\t' || end[-1] == '\r' || end[-1] == '\n')) {
|
||
*--end = '\0';
|
||
}
|
||
|
||
return text;
|
||
}
|
||
|
||
static size_t codepoint_field_to_utf8(char *field, char *output, size_t output_size) {
|
||
char *p = trim_ascii(field);
|
||
size_t output_len = 0;
|
||
|
||
while(*p) {
|
||
char *end;
|
||
unsigned long cp = strtoul(p, &end, 16);
|
||
|
||
if(end == p || cp > MJB_CODEPOINT_MAX) {
|
||
return 0;
|
||
}
|
||
|
||
unsigned int encoded = mjb_codepoint_encode((mjb_codepoint)cp, output + output_len,
|
||
output_size - output_len - 1, MJB_ENC_UTF_8);
|
||
|
||
if(encoded == 0) {
|
||
return 0;
|
||
}
|
||
|
||
output_len += encoded;
|
||
p = trim_ascii(end);
|
||
}
|
||
|
||
output[output_len] = '\0';
|
||
|
||
return output_len;
|
||
}
|
||
|
||
static bool parse_intentional_line(char *line, char *left, size_t *left_len, char *right,
|
||
size_t *right_len) {
|
||
char *comment = strchr(line, '#');
|
||
|
||
if(comment) {
|
||
*comment = '\0';
|
||
}
|
||
|
||
char *semi = strchr(line, ';');
|
||
|
||
if(!semi) {
|
||
return false;
|
||
}
|
||
|
||
*semi = '\0';
|
||
|
||
*left_len = codepoint_field_to_utf8(line, left, 64);
|
||
*right_len = codepoint_field_to_utf8(semi + 1, right, 64);
|
||
|
||
return *left_len > 0 && *right_len > 0;
|
||
}
|
||
|
||
static bool test_are_confusable(const char *s1, size_t s1_byte_length, mjb_encoding s1_encoding,
|
||
const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding) {
|
||
bool confusable = false;
|
||
mjb_text left = mjb_text_make(s1, s1_byte_length, s1_encoding);
|
||
mjb_text right = mjb_text_make(s2, s2_byte_length, s2_encoding);
|
||
mjb_status status = mjb_confusable_match(&left, &right, MJB_MALFORMED_STOP, &confusable, NULL,
|
||
NULL);
|
||
MJB_TEST_COVERAGE(mjb_confusable_match);
|
||
ATT_ASSERT_STATUS(status, MJB_STATUS_OK, "Confusable comparison succeeds");
|
||
|
||
return confusable;
|
||
}
|
||
|
||
static void run_intentional_confusable_file(const char *filename) {
|
||
FILE *file = fopen(filename, "r");
|
||
|
||
if(!file) {
|
||
ATT_ASSERT("Not opened", "Opened file", "intentional.txt");
|
||
|
||
return;
|
||
}
|
||
|
||
char line[1024];
|
||
char left[64];
|
||
char right[64];
|
||
unsigned int current_line = 0;
|
||
unsigned int tested = 0;
|
||
unsigned int failures = 0;
|
||
|
||
while(fgets(line, (int)sizeof(line), file)) {
|
||
++current_line;
|
||
|
||
size_t left_len = 0;
|
||
size_t right_len = 0;
|
||
|
||
if(!parse_intentional_line(line, left, &left_len, right, &right_len)) {
|
||
continue;
|
||
}
|
||
|
||
bool ok = test_are_confusable(left, left_len, MJB_ENC_UTF_8, right, right_len,
|
||
MJB_ENC_UTF_8);
|
||
|
||
if(!ok) {
|
||
++failures;
|
||
char test_name[128];
|
||
snprintf(test_name, sizeof(test_name), "intentional.txt line %u", current_line);
|
||
ATT_ASSERT(0, 1, test_name);
|
||
|
||
if(is_exit_on_error()) {
|
||
break;
|
||
}
|
||
} else {
|
||
ATT_ASSERT(0, 0, "intentional.txt confusable pair");
|
||
}
|
||
|
||
++tested;
|
||
}
|
||
|
||
fclose(file);
|
||
|
||
char summary[128];
|
||
snprintf(summary, sizeof(summary), "intentional.txt: %u/%u pairs passed", tested - failures,
|
||
tested);
|
||
ATT_ASSERT(tested > 0, true, "intentional.txt has confusable pairs");
|
||
ATT_ASSERT(failures, 0u, summary);
|
||
}
|
||
|
||
static void check_skeleton(const char *input, size_t input_size, const char *expected,
|
||
size_t expected_size, const char *name) {
|
||
mjb_result skeleton;
|
||
|
||
MJB_TEST_COVERAGE(mjb_confusable_skeleton);
|
||
mjb_text text = mjb_text_utf8(input, input_size);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, MJB_ENC_UTF_8,
|
||
&skeleton), MJB_STATUS_OK, name);
|
||
ATT_ASSERT(skeleton.output_size, expected_size, name);
|
||
ATT_ASSERT((int)memcmp(skeleton.output, expected, expected_size), 0, name);
|
||
|
||
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
||
MJB_TEST_COVERAGE(mjb_confusable_skeleton_into);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK, name);
|
||
ATT_ASSERT(into.size, expected_size, name);
|
||
|
||
char output[128];
|
||
ATT_ASSERT((int)(into.size < sizeof(output)), true, name);
|
||
|
||
if(into.size < sizeof(output)) {
|
||
memset(output, '#', sizeof(output));
|
||
into = mjb_buffer_make(output, into.size, MJB_ENC_UTF_8);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK, name);
|
||
ATT_ASSERT(into.size, expected_size, name);
|
||
ATT_ASSERT((int)memcmp(output, expected, expected_size), 0, name);
|
||
ATT_ASSERT(output[into.size], '#', name);
|
||
}
|
||
|
||
ATT_ASSERT_STATUS(mjb_result_free(&skeleton), MJB_STATUS_OK, name);
|
||
}
|
||
|
||
#if !defined(MJB_SHARED)
|
||
static void run_confusables_file(const char *filename) {
|
||
FILE *file = fopen(filename, "r");
|
||
|
||
if(file == NULL) {
|
||
ATT_ASSERT("Not opened", "Opened file", "confusables.txt");
|
||
return;
|
||
}
|
||
|
||
char line[2048];
|
||
unsigned int current_line = 0;
|
||
unsigned int tested = 0;
|
||
|
||
while(fgets(line, sizeof(line), file) != NULL) {
|
||
++current_line;
|
||
char *comment = strchr(line, '#');
|
||
|
||
if(comment != NULL) {
|
||
*comment = '\0';
|
||
}
|
||
|
||
char *first_semicolon = strchr(line, ';');
|
||
|
||
if(first_semicolon == NULL) {
|
||
continue;
|
||
}
|
||
|
||
*first_semicolon = '\0';
|
||
|
||
char *second_semicolon = strchr(first_semicolon + 1, ';');
|
||
|
||
if(second_semicolon == NULL) {
|
||
continue;
|
||
}
|
||
|
||
*second_semicolon = '\0';
|
||
|
||
char source[128];
|
||
char target[128];
|
||
size_t source_size = codepoint_field_to_utf8(line, source, sizeof(source));
|
||
size_t target_size = codepoint_field_to_utf8(first_semicolon + 1, target, sizeof(target));
|
||
|
||
if(source_size == 0 || target_size == 0) {
|
||
continue;
|
||
}
|
||
|
||
char test_name[128];
|
||
snprintf(test_name, sizeof(test_name), "confusables.txt line %u", current_line);
|
||
mjb_codepoint source_cp = (mjb_codepoint)strtoul(trim_ascii(line), NULL, 16);
|
||
|
||
mjb_codepoint mapping[MJB_UNICODE_SEQUENCE_MAX];
|
||
uint8_t mapping_length = 0;
|
||
bool found = mjb_unicode_confusable_lookup(source_cp, mapping, &mapping_length);
|
||
|
||
// The table is validated directly, count it under its public consumer.
|
||
MJB_TEST_COVERAGE(mjb_confusable_skeleton);
|
||
ATT_ASSERT(found, true, test_name);
|
||
|
||
char actual[128];
|
||
size_t actual_size = 0;
|
||
|
||
for(uint8_t i = 0; i < mapping_length; ++i) {
|
||
actual_size += mjb_codepoint_encode(mapping[i], actual + actual_size,
|
||
sizeof(actual) - actual_size, MJB_ENC_UTF_8);
|
||
}
|
||
|
||
ATT_ASSERT(actual_size, target_size, test_name);
|
||
ATT_ASSERT((int)memcmp(actual, target, target_size), 0, test_name);
|
||
|
||
++tested;
|
||
}
|
||
|
||
fclose(file);
|
||
|
||
ATT_ASSERT(tested > 0, true, "confusables.txt has skeleton mappings");
|
||
}
|
||
#endif
|
||
|
||
static void check_resolved_script_set(const char *input, size_t input_size, mjb_encoding encoding,
|
||
mjb_script_set_kind expected_kind, const mjb_script *expected, size_t expected_count,
|
||
const char *name) {
|
||
size_t count = 0;
|
||
mjb_script_set_kind kind = MJB_SCRIPT_SET_EMPTY;
|
||
|
||
mjb_text text = mjb_text_make(input, input_size, encoding);
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(&text, NULL, &count, &kind),
|
||
MJB_STATUS_OK, name);
|
||
ATT_ASSERT((int)kind, expected_kind, name);
|
||
ATT_ASSERT(count, expected_count, name);
|
||
|
||
if(expected_count == 0) {
|
||
return;
|
||
}
|
||
|
||
mjb_script scripts[8];
|
||
ATT_ASSERT(expected_count <= MJB_TEST_COUNT_OF(scripts), true, name);
|
||
|
||
if(expected_count > MJB_TEST_COUNT_OF(scripts)) {
|
||
return;
|
||
}
|
||
|
||
count = MJB_TEST_COUNT_OF(scripts);
|
||
text = mjb_text_make(input, input_size, encoding);
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(&text, scripts, &count, &kind),
|
||
MJB_STATUS_OK, name);
|
||
ATT_ASSERT((int)kind, expected_kind, name);
|
||
ATT_ASSERT(count, expected_count, name);
|
||
ATT_ASSERT((int)memcmp(scripts, expected, expected_count * sizeof(mjb_script)), 0, name);
|
||
}
|
||
|
||
ATT_TEST(security) {
|
||
{
|
||
size_t count = 9;
|
||
mjb_script_set_kind kind = MJB_SCRIPT_SET_ALL;
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(NULL, NULL, &count, &kind),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Resolved scripts reject NULL text");
|
||
ATT_ASSERT(count, (size_t)0, "Resolved scripts clear count after NULL text");
|
||
ATT_ASSERT((int)kind, (int)MJB_SCRIPT_SET_EMPTY,
|
||
"Resolved scripts clear kind after NULL text");
|
||
}
|
||
|
||
mjb_encoding enc = MJB_ENC_UTF_8;
|
||
|
||
MJB_TEST_COVERAGE(mjb_resolved_script_set);
|
||
|
||
const mjb_script latin[] = { MJB_SC_LATN };
|
||
check_resolved_script_set("Circle", 6, enc, MJB_SCRIPT_SET_RESOLVED, latin,
|
||
MJB_TEST_COUNT_OF(latin), "Resolved Latin script");
|
||
check_resolved_script_set("Circ1e", 6, enc, MJB_SCRIPT_SET_RESOLVED, latin,
|
||
MJB_TEST_COUNT_OF(latin), "Common characters do not restrict Latin");
|
||
check_resolved_script_set("h\xD0\xB5llo", 6, enc, MJB_SCRIPT_SET_EMPTY, NULL, 0,
|
||
"Mixed Latin and Cyrillic has an empty resolved set");
|
||
check_resolved_script_set("123!", 4, enc, MJB_SCRIPT_SET_ALL, NULL, 0,
|
||
"Common-only string resolves to ALL");
|
||
check_resolved_script_set("", 0, enc, MJB_SCRIPT_SET_ALL, NULL, 0,
|
||
"Empty string resolves to ALL");
|
||
|
||
const mjb_script prolonged[] = { MJB_SC_HIRA, MJB_SC_KANA, MJB_SC_JPAN };
|
||
check_resolved_script_set("\xE3\x83\xBC", 3, enc, MJB_SCRIPT_SET_RESOLVED, prolonged,
|
||
MJB_TEST_COUNT_OF(prolonged), "Hiragana-Katakana mark adds Jpan");
|
||
|
||
const mjb_script japanese[] = { MJB_SC_JPAN };
|
||
check_resolved_script_set("\xE3\x81\xAD\xE3\x82\xAC", 6, enc, MJB_SCRIPT_SET_RESOLVED, japanese,
|
||
MJB_TEST_COUNT_OF(japanese), "Hiragana and Katakana resolve to Jpan");
|
||
|
||
const mjb_script han[] = { MJB_SC_HANI, MJB_SC_HANB, MJB_SC_JPAN, MJB_SC_KORE };
|
||
check_resolved_script_set("\xE3\x80\x86\xE5\x88\x87", 6, enc, MJB_SCRIPT_SET_RESOLVED, han,
|
||
MJB_TEST_COUNT_OF(han), "Han text receives writing-system augmentations");
|
||
|
||
const mjb_script unknown[] = { MJB_SC_ZZZZ };
|
||
check_resolved_script_set("\xCD\xB8", 2, enc, MJB_SCRIPT_SET_RESOLVED, unknown,
|
||
MJB_TEST_COUNT_OF(unknown), "Unassigned codepoint resolves to Unknown");
|
||
|
||
const char embedded_nul[] = { 'A', '\0', '1' };
|
||
check_resolved_script_set(embedded_nul, sizeof(embedded_nul), enc, MJB_SCRIPT_SET_RESOLVED,
|
||
latin, MJB_TEST_COUNT_OF(latin), "Resolved scripts preserve embedded NUL");
|
||
check_resolved_script_set("A", MJB_NUL_TERMINATED, enc, MJB_SCRIPT_SET_RESOLVED, latin,
|
||
MJB_TEST_COUNT_OF(latin), "Resolved scripts support NUL-terminated input");
|
||
|
||
const char utf16be_latin[] = { '\0', 'A', '\0', '1' };
|
||
check_resolved_script_set(utf16be_latin, sizeof(utf16be_latin), MJB_ENC_UTF_16BE,
|
||
MJB_SCRIPT_SET_RESOLVED, latin, MJB_TEST_COUNT_OF(latin),
|
||
"Resolved scripts support UTF-16");
|
||
|
||
size_t script_count = 9;
|
||
mjb_script_set_kind script_kind = MJB_SCRIPT_SET_ALL;
|
||
mjb_text text = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(&text, NULL, NULL, &script_kind),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Resolved scripts reject NULL count");
|
||
ATT_ASSERT((int)script_kind, MJB_SCRIPT_SET_EMPTY,
|
||
"Resolved scripts clear kind after NULL count");
|
||
|
||
text = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(&text, NULL, &script_count, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Resolved scripts reject NULL kind");
|
||
ATT_ASSERT(script_count, 0u, "Resolved scripts clear count after NULL kind");
|
||
|
||
script_count = 9;
|
||
script_kind = MJB_SCRIPT_SET_ALL;
|
||
text = mjb_text_make(NULL, 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(&text, NULL, &script_count, &script_kind),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Resolved scripts reject NULL input");
|
||
ATT_ASSERT(script_count, 0u, "Resolved scripts clear count after invalid input");
|
||
ATT_ASSERT((int)script_kind, MJB_SCRIPT_SET_EMPTY,
|
||
"Resolved scripts clear kind after invalid input");
|
||
|
||
script_count = 9;
|
||
text = mjb_text_make("A", 1, MJB_ENC_UNKNOWN);
|
||
ATT_ASSERT_STATUS(
|
||
mjb_resolved_script_set(&text, NULL, &script_count, &script_kind),
|
||
MJB_STATUS_INVALID_ENCODING, "Resolved scripts reject invalid encoding");
|
||
ATT_ASSERT(script_count, 0u, "Resolved scripts clear count after invalid encoding");
|
||
|
||
const char malformed_script_input[] = { '\x80' };
|
||
script_count = 9;
|
||
text = mjb_text_make(malformed_script_input, sizeof(malformed_script_input), enc);
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(&text, NULL, &script_count, &script_kind),
|
||
MJB_STATUS_MALFORMED_INPUT, "Resolved scripts reject malformed input");
|
||
ATT_ASSERT(script_count, 0u, "Resolved scripts clear count after malformed input");
|
||
|
||
mjb_script script_output = MJB_SC_ZZZZ;
|
||
script_count = 0;
|
||
text = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(
|
||
mjb_resolved_script_set(&text, &script_output, &script_count, &script_kind),
|
||
MJB_STATUS_OUTPUT_TOO_SMALL, "Resolved scripts report a small output buffer");
|
||
ATT_ASSERT(script_count, 1u, "Resolved scripts report required count");
|
||
ATT_ASSERT((int)script_kind, MJB_SCRIPT_SET_RESOLVED,
|
||
"Resolved scripts preserve kind after a small output buffer");
|
||
ATT_ASSERT((int)script_output, MJB_SC_ZZZZ,
|
||
"Resolved scripts leave a small output buffer untouched");
|
||
|
||
mjb_result skeleton;
|
||
text = mjb_text_make(NULL, 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, &skeleton),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Skeleton rejects NULL input");
|
||
text = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Skeleton rejects NULL result");
|
||
text = mjb_text_make("A", 1, MJB_ENC_UNKNOWN);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, &skeleton),
|
||
MJB_STATUS_INVALID_ENCODING, "Skeleton rejects invalid input encoding");
|
||
|
||
text = mjb_text_make(NULL, 1, enc);
|
||
mjb_buffer into = mjb_buffer_make(NULL, 9, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Skeleton into rejects NULL input");
|
||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into clears size after invalid input");
|
||
into = mjb_buffer_make(NULL, 9, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(NULL, &into), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Skeleton into rejects NULL text");
|
||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into clears size after NULL text");
|
||
text = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Skeleton into rejects NULL output");
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(NULL, enc, &skeleton), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Skeleton rejects NULL text");
|
||
|
||
text = mjb_text_make("", 0, enc);
|
||
into = mjb_buffer_make(NULL, 9, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||
"Skeleton into measures empty input");
|
||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into empty size");
|
||
|
||
const char *into_input = "h\xD0\xB5llo";
|
||
text = mjb_text_make(into_input, 6, enc);
|
||
into = mjb_buffer_make(NULL, 0, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||
"Skeleton into queries required size");
|
||
ATT_ASSERT(into.size, (size_t)5, "Skeleton into required size");
|
||
|
||
char into_output[6] = { '#', '#', '#', '#', '#', '#' };
|
||
into = mjb_buffer_make(into_output, 4, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OUTPUT_TOO_SMALL,
|
||
"Skeleton into reports a small output buffer");
|
||
ATT_ASSERT(into.size, (size_t)5, "Skeleton into preserves required size");
|
||
ATT_ASSERT((int)memcmp(into_output + 4, "##", 2), 0,
|
||
"Skeleton into is not written past its capacity");
|
||
|
||
into = mjb_buffer_make(into_output, 5, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||
"Skeleton into writes into exact capacity");
|
||
ATT_ASSERT((int)memcmp(into_output, "hello", 5), 0, "Skeleton into output");
|
||
ATT_ASSERT(into_output[5], '#', "Skeleton into does not write a terminator");
|
||
|
||
check_skeleton("h\xD0\xB5llo", 6, "hello", 5, "Skeleton maps Cyrillic e");
|
||
check_skeleton("a\xE2\x80\x8D"
|
||
"b",
|
||
5, "ab", 2, "Skeleton removes default-ignorables");
|
||
check_skeleton("A1<\xD7\xA9\xD7\x82", 7, "Al<\xD7\xA9\xCC\x87", 7,
|
||
"Skeleton applies LTR bidi processing");
|
||
check_skeleton("\xEF\xB7\xBA", 3,
|
||
"\xD8\xB5\xD9\x84\xD9\x89 l\xD9\x84\xD9\x84o \xD8\xB9\xD9\x84\xD9\x89o "
|
||
"\xD9\x88\xD8\xB3\xD9\x84\xD9\x85",
|
||
30, "Skeleton preserves full 18-codepoint expansion");
|
||
|
||
text = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, MJB_ENC_UTF_16LE, &skeleton),
|
||
MJB_STATUS_OK, "Skeleton supports UTF-16 output");
|
||
ATT_ASSERT(skeleton.output_size, (size_t)2, "Skeleton UTF-16 output size");
|
||
ATT_ASSERT((int)memcmp(skeleton.output, "A\0", 2), 0, "Skeleton UTF-16 output");
|
||
ATT_ASSERT_STATUS(mjb_result_free(&skeleton), MJB_STATUS_OK, "Free UTF-16 skeleton");
|
||
|
||
char utf16_output[3] = { '#', '#', '#' };
|
||
text = mjb_text_make("A", 1, enc);
|
||
into = mjb_buffer_make(utf16_output, sizeof(utf16_output) - 1, MJB_ENC_UTF_16LE);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_OK,
|
||
"Skeleton into supports UTF-16 output");
|
||
ATT_ASSERT(into.size, (size_t)2, "Skeleton into UTF-16 output size");
|
||
ATT_ASSERT((int)memcmp(utf16_output, "A\0", 2), 0, "Skeleton into UTF-16 output");
|
||
ATT_ASSERT(utf16_output[2], '#', "Skeleton into UTF-16 output has no terminator");
|
||
|
||
bool confusable = true;
|
||
mjb_text left = mjb_text_make(NULL, 1, enc);
|
||
mjb_text right = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_STOP, &confusable, NULL, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Confusable rejects NULL left string");
|
||
ATT_ASSERT(confusable, false, "Confusable clears output before failure");
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(NULL, &right, MJB_MALFORMED_STOP, &confusable, NULL, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Confusable rejects a NULL first text");
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&right, NULL, MJB_MALFORMED_STOP, &confusable, NULL, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Confusable rejects a NULL second text");
|
||
|
||
// Malformed input: the policy applies to both texts, each with its own diagnostic
|
||
mjb_diagnostic first_diagnostic;
|
||
mjb_diagnostic second_diagnostic;
|
||
left = mjb_text_make("A", 1, enc);
|
||
right = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, (mjb_malformed_policy)99, &confusable,
|
||
NULL, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Confusable rejects an invalid malformed policy");
|
||
|
||
left = mjb_text_make("hello", 5, enc);
|
||
right = mjb_text_make("h\xD0\xB5l\x80lo", 7, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_STOP, &confusable,
|
||
&first_diagnostic, &second_diagnostic),
|
||
MJB_STATUS_MALFORMED_INPUT, "Confusable stops on a malformed second text");
|
||
ATT_ASSERT((unsigned int)first_diagnostic.error, (unsigned int)MJB_TEXT_ERROR_NONE,
|
||
"Confusable leaves the well-formed first diagnostic clear");
|
||
ATT_ASSERT(second_diagnostic.byte_offset, (size_t)4,
|
||
"Confusable second diagnostic is relative to the second text");
|
||
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_SKIP, &confusable,
|
||
&first_diagnostic, &second_diagnostic),
|
||
MJB_STATUS_OK, "Confusable skips malformed input");
|
||
ATT_ASSERT(confusable, true, "A skipped byte does not hide a confusable");
|
||
ATT_ASSERT(second_diagnostic.byte_offset, (size_t)4, "Confusable skip diagnostic offset");
|
||
|
||
right = mjb_text_make("h\x80llo", 5, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_REPLACE, &confusable,
|
||
NULL, NULL),
|
||
MJB_STATUS_OK, "Confusable replaces malformed input");
|
||
ATT_ASSERT(confusable, false, "A replaced byte is not confusable with a letter");
|
||
left = mjb_text_make("A", 1, enc);
|
||
right = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_STOP, NULL, NULL, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Confusable rejects NULL output");
|
||
|
||
const unsigned char malformed_utf8[] = { 0x80 };
|
||
const char *malformed_utf8_bytes = (const char *)malformed_utf8;
|
||
text = mjb_text_make(malformed_utf8_bytes, sizeof(malformed_utf8), enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, enc, &skeleton),
|
||
MJB_STATUS_MALFORMED_INPUT, "Skeleton reports malformed input");
|
||
into = mjb_buffer_make(NULL, 9, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into), MJB_STATUS_MALFORMED_INPUT,
|
||
"Skeleton into reports malformed input");
|
||
ATT_ASSERT(into.size, (size_t)0, "Skeleton into clears size after malformed input");
|
||
left = mjb_text_make(malformed_utf8_bytes, sizeof(malformed_utf8), enc);
|
||
right = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_STOP, &confusable, NULL, NULL),
|
||
MJB_STATUS_MALFORMED_INPUT, "Confusable reports malformed input");
|
||
left = mjb_text_make("A", 1, MJB_ENC_UNKNOWN);
|
||
right = mjb_text_make("A", 1, enc);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_STOP, &confusable, NULL, NULL),
|
||
MJB_STATUS_INVALID_ENCODING, "Confusable rejects invalid encoding");
|
||
|
||
// Cyrillic "А" (U+0410, UTF-8: 0xD0 0x90) is confusable with Latin "A"
|
||
// Both have skeleton "A": skeleton("А")="A", skeleton("A")="A"
|
||
ATT_ASSERT(test_are_confusable("\xD0\x90", 2, enc, "A", 1, enc), true,
|
||
"Cyrillic A confusable with Latin A");
|
||
|
||
// Cyrillic "а" (U+0430) is confusable with Latin "a" (U+0061)
|
||
// skeleton("а")="a", skeleton("a")="a"
|
||
ATT_ASSERT(test_are_confusable("\xD0\xB0", 2, enc, "a", 1, enc), true,
|
||
"Cyrillic a confusable with Latin a");
|
||
|
||
// "A" (Latin capital) is NOT confusable with "a" (Latin lowercase)
|
||
// skeleton("A")="A", skeleton("a")="a" → different
|
||
ATT_ASSERT(test_are_confusable("A", 1, enc, "a", 1, enc), false, "A not confusable with a");
|
||
|
||
// "a" is not confusable with "b"
|
||
ATT_ASSERT(test_are_confusable("a", 1, enc, "b", 1, enc), false, "a not confusable with b");
|
||
|
||
// A string is confusable with itself
|
||
ATT_ASSERT(test_are_confusable("hello", 5, enc, "hello", 5, enc), true,
|
||
"hello confusable with itself");
|
||
|
||
// "hello" vs "hеllo" (second 'e' is Cyrillic U+0435, UTF-8: 0xD0 0xB5)
|
||
// skeleton("hello")="hello", skeleton("hеllo")="hello" → confusable
|
||
ATT_ASSERT(test_are_confusable("hello", 5, enc, "h\xD0\xB5llo", 6, enc), true,
|
||
"hello confusable with h(Cyrillic e)llo");
|
||
|
||
// Empty strings: both have empty skeleton (output_size==0), returns false
|
||
ATT_ASSERT(test_are_confusable("", 0, enc, "", 0, enc), false,
|
||
"empty strings not confusable");
|
||
|
||
// Different lengths with no possible match
|
||
ATT_ASSERT(test_are_confusable("a", 1, enc, "ab", 2, enc), false,
|
||
"a not confusable with ab");
|
||
|
||
// Digit '1', capital 'I', and pipe '|' all map to skeleton 'l'
|
||
ATT_ASSERT(test_are_confusable("1", 1, enc, "l", 1, enc), true, "1 confusable with l");
|
||
ATT_ASSERT(test_are_confusable("I", 1, enc, "l", 1, enc), true, "I confusable with l");
|
||
ATT_ASSERT(test_are_confusable("|", 1, enc, "l", 1, enc), true, "| confusable with l");
|
||
ATT_ASSERT(test_are_confusable("1", 1, enc, "I", 1, enc), true, "1 confusable with I");
|
||
|
||
// Digit '0' maps to skeleton 'O' (letter)
|
||
ATT_ASSERT(test_are_confusable("0", 1, enc, "O", 1, enc), true, "0 confusable with O");
|
||
ATT_ASSERT(test_are_confusable("0", 1, enc, "o", 1, enc), false,
|
||
"0 not confusable with o (O != o)");
|
||
|
||
// 'm' maps to a two-codepoint skeleton "rn": skeleton("m") == skeleton("rn")
|
||
ATT_ASSERT(test_are_confusable("m", 1, enc, "rn", 2, enc), true,
|
||
"m confusable with rn (multi-codepoint skeleton)");
|
||
ATT_ASSERT(test_are_confusable("mm", 2, enc, "rnrn", 4, enc), true,
|
||
"mm confusable with rnrn");
|
||
|
||
// Cyrillic р (U+0440, UTF-8: 0xD1 0x80) maps to Latin p
|
||
// "рal" (with Cyrillic р) is confusable with "pal"
|
||
ATT_ASSERT(test_are_confusable("рal", 4, enc, "pal", 3, enc), true,
|
||
"Cyrillic рal confusable with pal");
|
||
|
||
// Cyrillic С (U+0421, UTF-8: 0xD0 0xA1) maps to Latin C
|
||
ATT_ASSERT(test_are_confusable("\xD0\xA1" "at", 4, enc, "Cat", 3, enc), true,
|
||
"Cyrillic С + at confusable with Cat");
|
||
|
||
// "gооd" (Cyrillic о U+043E, UTF-8: 0xD0 0xBE) confusable with "good"
|
||
// skeleton(Cyrillic о) = Latin o -> both strings have skeleton "good"
|
||
ATT_ASSERT(test_are_confusable("gооd", 6, enc, "good", 4, enc), true,
|
||
"g(Cyrillic o)(Cyrillic o)d confusable with good");
|
||
|
||
ATT_ASSERT(test_are_confusable("gооd", 6, enc, "\0g\0o\0o\0d", 8, MJB_ENC_UTF_16BE), true,
|
||
"Confusable with different encodings");
|
||
|
||
// Confusability is symmetric
|
||
ATT_ASSERT(test_are_confusable("pal", 3, enc, "\xD1\x80" "al", 4, enc), true,
|
||
"confusability is symmetric");
|
||
|
||
run_intentional_confusable_file("./unicode-data/security/intentional.txt");
|
||
#if !defined(MJB_SHARED)
|
||
run_confusables_file("./unicode-data/security/confusables.txt");
|
||
#endif
|
||
}
|
||
|
||
#else
|
||
|
||
ATT_TEST(security) {
|
||
mjb_script script = MJB_SC_NOT_SET;
|
||
size_t count = 1;
|
||
mjb_script_set_kind kind = MJB_SCRIPT_SET_EMPTY;
|
||
mjb_result result = { NULL, 0, 0, false };
|
||
unsigned char output[16] = { 0 };
|
||
mjb_buffer into = mjb_buffer_make(output, sizeof(output), MJB_ENC_UTF_8);
|
||
bool confusable = false;
|
||
|
||
MJB_TEST_COVERAGE(mjb_resolved_script_set);
|
||
mjb_text text = mjb_text_utf8("a", 1);
|
||
ATT_ASSERT_STATUS(mjb_resolved_script_set(&text, &script, &count, &kind),
|
||
MJB_STATUS_FEATURE_NOT_ENABLED, "Disabled resolved scripts report feature status");
|
||
MJB_TEST_COVERAGE(mjb_confusable_skeleton);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton(&text, MJB_ENC_UTF_8, &result),
|
||
MJB_STATUS_FEATURE_NOT_ENABLED, "Disabled skeleton reports feature status");
|
||
MJB_TEST_COVERAGE(mjb_confusable_skeleton_into);
|
||
ATT_ASSERT_STATUS(mjb_confusable_skeleton_into(&text, &into),
|
||
MJB_STATUS_FEATURE_NOT_ENABLED, "Disabled skeleton into reports feature status");
|
||
MJB_TEST_COVERAGE(mjb_confusable_match);
|
||
mjb_text left = mjb_text_utf8("a", 1);
|
||
mjb_text right = mjb_text_utf8("b", 1);
|
||
ATT_ASSERT_STATUS(mjb_confusable_match(&left, &right, MJB_MALFORMED_STOP, &confusable, NULL, NULL),
|
||
MJB_STATUS_FEATURE_NOT_ENABLED, "Disabled confusable check reports feature status");
|
||
}
|
||
|
||
#endif // MJB_FEATURE_SECURITY
|