Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional UTF-8 unit test corner cases. #30406

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/lib/support/tests/TestUtf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ namespace {

using namespace chip;

#define TEST_VALID_BYTES(...) \
do \
{ \
uint8_t _buff[] = { __VA_ARGS__ }; \
CharSpan _span(reinterpret_cast<const char *>(_buff), sizeof(_buff)); \
NL_TEST_ASSERT(inSuite, Utf8::IsValid(_span)); \
} while (0)

#define TEST_INVALID_BYTES(...) \
do \
{ \
uint8_t _buff[] = { __VA_ARGS__ }; \
CharSpan _span(reinterpret_cast<const char *>(_buff), sizeof(_buff)); \
NL_TEST_ASSERT(inSuite, !Utf8::IsValid(_span)); \
} while (0)

void TestValidStrings(nlTestSuite * inSuite, void * inContext)
{
NL_TEST_ASSERT(inSuite, Utf8::IsValid(CharSpan())); // empty span ok
Expand Down Expand Up @@ -80,23 +96,26 @@ void TestValidStrings(nlTestSuite * inSuite, void * inContext)
char insideZero[] = "test\0zero";
NL_TEST_ASSERT(inSuite, Utf8::IsValid(CharSpan(insideZero)));
}
}

#define TEST_INVALID_BYTES(...) \
{ \
uint8_t _buff[] = { __VA_ARGS__ }; \
CharSpan _span(reinterpret_cast<const char *>(_buff), sizeof(_buff)); \
NL_TEST_ASSERT(inSuite, !Utf8::IsValid(_span)); \
} \
(void) 0
// Test around forbidden 0xD800..0xDFFF UTF-16 surrogate pairs.
TEST_VALID_BYTES(0b1110'1101, 0b10'011111, 0b10'111111);
TEST_VALID_BYTES(0b1110'1110, 0b10'000000, 0b10'000000);
}

void TestInvalidStrings(nlTestSuite * inSuite, void * inContext)
{
// overly long representation
// Overly long sequences
TEST_INVALID_BYTES(0xc0, 0b10'111111);
TEST_INVALID_BYTES(0xc1, 0b10'111111);

TEST_INVALID_BYTES(0xe0, 0b1001'1111, 0x80); // A
TEST_INVALID_BYTES(0xed, 0b1011'0000, 0x80); // B
TEST_INVALID_BYTES(0xf0, 0b1000'1111, 0x80); // C

// Invalid 0xD800 .. 0xDFFF UTF-16 surrogates that should not appear in UTF-8.
TEST_INVALID_BYTES(0b1110'1101, 0b10'100000, 0b10'000000);
TEST_INVALID_BYTES(0b1110'1101, 0b10'111111, 0b10'111111);

// Outside codepoint
TEST_INVALID_BYTES(0xf4, 0x90, 0x80, 0x80); // D
TEST_INVALID_BYTES(0xf4, 0x91, 0x82, 0x83);
Expand Down Expand Up @@ -162,7 +181,8 @@ const nlTest sTests[] =

int TestUtf8()
{
nlTestSuite theSuite = { "CHIP UTF8 tests", &sTests[0], nullptr, nullptr };
nlTestSuite theSuite = { "UTF8 validator tests", &sTests[0], nullptr, nullptr };

nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}
Expand Down
Loading