|
| 1 | +//===-- Unittests for mbsrtowcs -------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "hdr/types/wchar_t.h" |
| 10 | +#include "src/__support/libc_errno.h" |
| 11 | +#include "src/string/memset.h" |
| 12 | +#include "src/wchar/mbsrtowcs.h" |
| 13 | +#include "test/UnitTest/ErrnoCheckingTest.h" |
| 14 | +#include "test/UnitTest/Test.h" |
| 15 | + |
| 16 | +using LlvmLibcMBSRToWCSTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 17 | + |
| 18 | +TEST_F(LlvmLibcMBSRToWCSTest, OneByteOneCharacter) { |
| 19 | + mbstate_t *mb; |
| 20 | + LIBC_NAMESPACE::memset(&mb, 0, sizeof(mbstate_t)); |
| 21 | + const char *ch = "A"; |
| 22 | + wchar_t dest[2]; |
| 23 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &ch, 2, mb); |
| 24 | + ASSERT_TRUE(dest[0] == L'A'); |
| 25 | + ASSERT_TRUE(dest[1] == L'\0'); |
| 26 | + // Should not count null terminator in number |
| 27 | + ASSERT_EQ(static_cast<int>(n), 1); |
| 28 | + // Should set ch to nullptr after reading null terminator |
| 29 | + ASSERT_EQ(ch, nullptr); |
| 30 | +} |
| 31 | + |
| 32 | +TEST_F(LlvmLibcMBSRToWCSTest, MultiByteOneCharacter) { |
| 33 | + const char *src = "\xf0\x9f\x98\xb9"; // laughing cat emoji 😹 |
| 34 | + wchar_t dest[2]; |
| 35 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 2, nullptr); |
| 36 | + ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 37 | + ASSERT_TRUE(dest[1] == L'\0'); |
| 38 | + // Should not count null terminator in number |
| 39 | + ASSERT_EQ(static_cast<int>(n), 1); |
| 40 | + // Should set ch to nullptr after reading null terminator |
| 41 | + ASSERT_EQ(src, nullptr); |
| 42 | +} |
| 43 | + |
| 44 | +TEST_F(LlvmLibcMBSRToWCSTest, MultiByteTwoCharacters) { |
| 45 | + // Two laughing cat emojis "😹😹" |
| 46 | + const char *src = "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 47 | + wchar_t dest[3]; |
| 48 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 3, nullptr); |
| 49 | + ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 50 | + ASSERT_EQ(static_cast<int>(dest[1]), 128569); |
| 51 | + ASSERT_TRUE(dest[2] == L'\0'); |
| 52 | + // Should not count null terminator in number |
| 53 | + ASSERT_EQ(static_cast<int>(n), 2); |
| 54 | + // Should set ch to nullptr after reading null terminator |
| 55 | + ASSERT_EQ(src, nullptr); |
| 56 | +} |
| 57 | + |
| 58 | +TEST_F(LlvmLibcMBSRToWCSTest, ReadLessThanStringLength) { |
| 59 | + // Four laughing cat emojis "😹😹😹😹" |
| 60 | + const char *src = |
| 61 | + "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 62 | + const char *check = src; |
| 63 | + wchar_t dest[3]; |
| 64 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 3, nullptr); |
| 65 | + // Should have read 3 emojis |
| 66 | + ASSERT_EQ(static_cast<int>(n), 3); |
| 67 | + ASSERT_EQ(static_cast<int>(dest[0]), 128569); |
| 68 | + ASSERT_EQ(static_cast<int>(dest[1]), 128569); |
| 69 | + ASSERT_EQ(static_cast<int>(dest[2]), 128569); |
| 70 | + // src should now point to the 4th cat emoji aka 13th byte |
| 71 | + ASSERT_EQ((check + 12), src); |
| 72 | +} |
| 73 | + |
| 74 | +TEST_F(LlvmLibcMBSRToWCSTest, InvalidFirstByte) { |
| 75 | + // 0x80 is invalid first byte of mb character |
| 76 | + const char *src = |
| 77 | + "\x80\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 78 | + wchar_t dest[3]; |
| 79 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 3, nullptr); |
| 80 | + // Should return error and set errno |
| 81 | + ASSERT_EQ(static_cast<int>(n), -1); |
| 82 | + ASSERT_ERRNO_EQ(EILSEQ); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(LlvmLibcMBSRToWCSTest, InvalidMiddleByte) { |
| 86 | + // The 7th byte is invalid for a 4 byte character |
| 87 | + const char *src = |
| 88 | + "\xf0\x9f\x98\xb9\xf0\x9f\xf0\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 89 | + wchar_t dest[3]; |
| 90 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(dest, &src, 5, nullptr); |
| 91 | + // Should return error and set errno |
| 92 | + ASSERT_EQ(static_cast<int>(n), -1); |
| 93 | + ASSERT_ERRNO_EQ(EILSEQ); |
| 94 | +} |
| 95 | + |
| 96 | +TEST_F(LlvmLibcMBSRToWCSTest, NullDestination) { |
| 97 | + // Four laughing cat emojis "😹😹😹😹" |
| 98 | + const char *src = |
| 99 | + "\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9\xf0\x9f\x98\xb9"; |
| 100 | + size_t n = LIBC_NAMESPACE::mbsrtowcs(nullptr, &src, 5, nullptr); |
| 101 | + // Null destination should still return correct number of read chars |
| 102 | + ASSERT_EQ(static_cast<int>(n), 4); |
| 103 | +} |
| 104 | + |
| 105 | +#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 106 | +TEST_F(LlvmLibcMBSRToWCSTest, NullSource) { |
| 107 | + // Passing in a nullptr source should crash the program |
| 108 | + EXPECT_DEATH([] { LIBC_NAMESPACE::mbsrtowcs(nullptr, nullptr, 1, nullptr); }, |
| 109 | + WITH_SIGNAL(-1)); |
| 110 | +} |
| 111 | +#endif // LIBC_HAS_ADDRESS_SANITIZER |
0 commit comments