From 3d8156d3ec32e81fa69b5d0a655be4366d59b34b Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 5 Nov 2021 17:47:07 -0700 Subject: [PATCH] uchar.h tests: small improvements. Explicitly test an invalid 5-byte UTF-8 sequence with mbrtoc16(3); the fact that we weren't testing this was shown by coverage data. Merge the surrogate pair tests in with their fewer-byte siblings to make it clearer to a human reader that we've covered both cases. Clear errno to make assertions about errno more convincing. Test: treehugger Change-Id: I485a48cc141f3e52058e2138326f3134d41b2243 --- tests/uchar_test.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/uchar_test.cpp b/tests/uchar_test.cpp index 48c500db0..4dc631419 100644 --- a/tests/uchar_test.cpp +++ b/tests/uchar_test.cpp @@ -36,6 +36,7 @@ TEST(uchar, start_state) { // Any non-initial state is invalid when calling c32rtomb. memset(&ps, 0, sizeof(ps)); EXPECT_EQ(static_cast(-2), mbrtoc32(nullptr, "\xc2", 1, &ps)); + errno = 0; EXPECT_EQ(static_cast(-1), c32rtomb(out, 0x00a2, &ps)); EXPECT_EQ(EILSEQ, errno); @@ -87,11 +88,7 @@ TEST(uchar, c16rtomb) { EXPECT_EQ('\xe2', bytes[0]); EXPECT_EQ('\x82', bytes[1]); EXPECT_EQ('\xac', bytes[2]); -} - -TEST(uchar, c16rtomb_surrogate) { - char bytes[MB_LEN_MAX]; - + // 4-byte UTF-8 from a surrogate pair... memset(bytes, 0, sizeof(bytes)); EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr)); EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr)); @@ -143,16 +140,16 @@ TEST(uchar, mbrtoc16) { // 3-byte UTF-8. ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr)); ASSERT_EQ(static_cast(0x20ac), out); -} - -TEST(uchar, mbrtoc16_surrogate) { - char16_t out; - + // 4-byte UTF-8 will be returned as a surrogate pair... ASSERT_EQ(static_cast(-3), mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr)); ASSERT_EQ(static_cast(0xdbea), out); ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d" "ef", 6, nullptr)); ASSERT_EQ(static_cast(0xdfcd), out); + // Illegal 5-byte UTF-8. + errno = 0; + ASSERT_EQ(static_cast(-1), mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr)); + ASSERT_EQ(EILSEQ, errno); } TEST(uchar, mbrtoc16_reserved_range) { @@ -194,6 +191,7 @@ void test_mbrtoc16_incomplete(mbstate_t* ps) { // Invalid 2-byte ASSERT_EQ(static_cast(-2), mbrtoc16(&out, "\xc2", 1, ps)); + errno = 0; ASSERT_EQ(static_cast(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps)); ASSERT_EQ(EILSEQ, errno); } @@ -247,6 +245,7 @@ TEST(uchar, c32rtomb) { EXPECT_EQ('\xad', bytes[2]); EXPECT_EQ('\xa2', bytes[3]); // Invalid code point. + errno = 0; EXPECT_EQ(static_cast(-1), c32rtomb(bytes, 0xffffffff, nullptr)); EXPECT_EQ(EILSEQ, errno); } @@ -307,10 +306,12 @@ TEST(uchar, mbrtoc32) { ASSERT_EQ(static_cast(0x24b62), out[0]); #if defined(__BIONIC__) // glibc allows this. // Illegal 5-byte UTF-8. + errno = 0; ASSERT_EQ(static_cast(-1), mbrtoc32(out, "\xf8\xa1\xa2\xa3\xa4" "f", 6, nullptr)); ASSERT_EQ(EILSEQ, errno); #endif // Illegal over-long sequence. + errno = 0; ASSERT_EQ(static_cast(-1), mbrtoc32(out, "\xf0\x82\x82\xac" "ef", 6, nullptr)); ASSERT_EQ(EILSEQ, errno); } @@ -340,6 +341,7 @@ void test_mbrtoc32_incomplete(mbstate_t* ps) { // Invalid 2-byte ASSERT_EQ(static_cast(-2), mbrtoc32(&out, "\xc2", 1, ps)); + errno = 0; ASSERT_EQ(static_cast(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps)); ASSERT_EQ(EILSEQ, errno); }