/* *TheUTF-8decodertestsarebasedontheUTF-8decodercapability *andstresstest"2015-08-26byMarkusKuhn.Acopyofthatfile
* named "UTF-8-test.txt" should be in the source directory for this file */
// Abbreviate UCS_REPLACEMENT_CHARACTER for utf8_decode_sub_test arrays #define URC UCS_REPLACEMENT_CHARACTER
struct utf8_decode_sub_test
{ constchar *testref; constchar *utf8str; // This array will contain 0 values after the initialised part const char32_t expected[65];
};
// Used as the simple test in UTF-8-test.txt conststaticchar greek_kosme[] = "\xce\xba"// GREEK SMALL LETTER KAPPA "\xe1\xbd\xb9"// GREEK SMALL LETTER OMICRON WITH OXIA "\xcf\x83"// GREEK SMALL LETTER SIGMA "\xce\xbc"// GREEK SMALL LETTER MU "\xce\xb5"; // GREEK SMALL LETTER EPSILON
// See Issue #2603 conststaticchar simple_test_with_emoji[] = "Simple Test." "\xf0\x9f\x98\xa5"; // U+1F625 Disappointed But Relieved Face
/******************************************************************************/
START_TEST(test_get_next_char__section_1)
{ conststruct utf8_decode_sub_test st =
{ "1",
greek_kosme,
{ 0x03ba, // GREEK SMALL LETTER KAPPA 0x1f79, // GREEK SMALL LETTER OMICRON WITH OXIA 0x03c3, // GREEK SMALL LETTER SIGMA 0x03bc, // GREEK SMALL LETTER MU 0x03b5 // GREEK SMALL LETTER EPSILON
}
};
run_utf8_decode_sub_test(&st);
}
END_TEST
/******************************************************************************/
START_TEST(test_get_next_char__section_2)
{ struct utf8_decode_sub_test tests[] =
{ // 2.1 First possible sequence of a certain length // // (2.1.1 Is tested separately)
{ "2.1.2", "\xc2\x80", { 0x80 } },
{ "2.1.3", "\xe0\xa0\x80", { 0x800 } },
{ "2.1.4", "\xf0\x90\x80\x80", { 0x10000 } },
{ "2.1.5", "\xf8\x88\x80\x80\x80", { URC } },
{ "2.1.6", "\xfc\x84\x80\x80\x80\x80", { URC } },
// 2.2 Last possible sequence of a certain length
{ "2.2.1", "\x7f", { 0x7f } },
{ "2.2.2", "\xdf\xbf", { 0x7ff } }, // Use U+0000FFFC instead of U+0000FFFF as our decoder // treats non-characters as an input error
{ "2.2.3", "\xef\xbf\xbc", { 0xfffc } }, // U+001FFFFF is out-of-range
{ "2.2.4", "\xf7\xbf\xbf\xbf", { URC } },
{ "2.2.5", "\xfb\xbf\xbf\xbf\xbf", { URC } },
{ "2.2.6", "\xfd\xbf\xbf\xbf\xbf\xbf", { URC } },
// 3.3 Sequences with last continuation byte missing // // From UTF-8-test.txt:- // All bytes of an incomplete sequence should be signalled as // a single malformed sequence, i.e., you should see only a // single replacement character in each of the next 10 tests.
{ "3.3.1", "\xc0", { URC } },
{ "3.3.2", "\xe0\x80", { URC } },
{ "3.3.3", "\xf0\x80\x80", { URC } },
{ "3.3.4", "\xf8\x80\x80\x80", { URC } },
{ "3.3.5", "\xfc\x80\x80\x80\x80", { URC } },
/******************************************************************************/
START_TEST(test_get_next_char__section_4)
{ struct utf8_decode_sub_test tests[] =
{ // 4.1 Examples of an overlong ASCII character // // With a safe UTF-8 decoder, all of the following five // overlong representations of the ASCII character slash ("/") // should be rejected like a malformed UTF-8 sequence, for // instance by substituting it with a replacement character. If // you see a slash below, you do not have a safe UTF-8 decoder!
{ "4.1.1", "\xc0\xaf", { URC } },
{ "4.1.2", "\xe0\x80\xaf", { URC } },
{ "4.1.3", "\xf0\x80\x80\xaf", { URC } },
{ "4.1.4", "\xf8\x80\x80\x80\xaf", { URC } },
{ "4.1.5", "\xfc\x80\x80\x80\x80\xaf", { URC } },
// 4.2 Maximum overlong sequences
// Below you see the highest Unicode value that is still resulting // in an overlong sequence if represented with the given number of // bytes. This is a boundary test for safe UTF-8 decoders. All // five characters should be rejected like malformed UTF-8 // sequences.
{ "4.2.1", "\xc1\xbf", { URC } },
{ "4.2.2", "\xe0\x9f\xbf", { URC } },
{ "4.2.3", "\xf0\x8f\xbf\xbf", { URC } },
{ "4.2.4", "\xf8\x87\xbf\xbf\xbf", { URC } },
{ "4.2.5", "\xfc\x83\xbf\xbf\xbf\xbf", { URC } },
// 4.3 Overlong representation of the NUL character
// The following five sequences should also be rejected like // malformed UTF-8 sequences and should not be treated like the // ASCII NUL character.
{ "4.3.1", "\xc0\x80", { URC } },
{ "4.3.2", "\xe0\x80\x80", { URC } },
{ "4.3.3", "\xf0\x80\x80\x80", { URC } },
{ "4.3.4", "\xf8\x80\x80\x80\x80", { URC } },
{ "4.3.5", "\xfc\x80\x80\x80\x80\x80", { URC } },
// The following UTF-8 sequences should be rejected like // malformed sequences, because they never represent valid // ISO 10646 characters and a UTF-8 decoder that accepts them // might introduce security problems comparable to overlong // UTF-8 sequences.
// The following "noncharacters" are "reserved for internal // use" by applications, and according to older versions of // the Unicode Standard "should never be interchanged". Unicode // Corrigendum #9 dropped the latter restriction. Nevertheless, // their presence in incoming UTF-8 data can remain a potential // security risk, depending on what use is made of these codes // subsequently. Examples of such internal use: // // - Some file APIs with 16-bit characters may use the integer // value -1 = U+FFFF to signal an end-of-file (EOF) or error // condition. // // - In some UTF-16 receivers, code point U+FFFE might trigger // a byte-swap operation (to convert between UTF-16LE and // UTF-16BE). // With such internal use of noncharacters, it may be desirable // and safer to block those code points in UTF-8 decoders, as // they should never occur legitimately in incoming UTF-8 data, // and could trigger unsafe behaviour in subsequent processing.
/******************************************************************************/
START_TEST(test_utf8_char_count)
{ // Check function can cope with NULL argument
ck_assert_int_eq(utf8_char_count(NULL), 0);
// All characters map to two bytes except for the 'omicrom with oxia' // which maps to three
ck_assert_int_eq(kosme_strlen, 2 + 3 + 2 + 2 + 2);
ck_assert_int_eq(kosme_len, 5);
// Should have reached the buffer size by now
ck_assert_int_eq(strlen(buff), TEST_SIZE - 1);
// Check the string is what we expect
ck_assert_int_eq(strcmp(buff, simple_test_with_emoji), 0);
// Try to insert another character if (utf8_add_char_at(buff, TEST_SIZE, ' ', 0))
{
ck_abort_msg("test_utf8_add_char_at: " "Insert succeeded but should have failed");
}
#undef TEST_SIZE
}
END_TEST
/******************************************************************************/
START_TEST(test_utf8_remove_char_at)
{ #define TEST_SIZE sizeof(simple_test_with_emoji) // Type pairing a string position with a Unicode char struct pos_to_char_map
{ unsignedint pos;
char32_t c32;
};
// Buffer for deconstructing the string char buff[TEST_SIZE];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.