| 64 | } // namespace |
| 65 | |
| 66 | TEST(StringUtilTest, TruncateUTF8ToByteSize) { |
| 67 | std::string output; |
| 68 | |
| 69 | // Empty strings and invalid byte_size arguments |
| 70 | EXPECT_FALSE(Truncated(std::string(), 0, &output)); |
| 71 | EXPECT_EQ(output, ""); |
| 72 | EXPECT_TRUE(Truncated("\xe1\x80\xbf", 0, &output)); |
| 73 | EXPECT_EQ(output, ""); |
| 74 | EXPECT_FALSE(Truncated("\xe1\x80\xbf", (size_t)-1, &output)); |
| 75 | EXPECT_FALSE(Truncated("\xe1\x80\xbf", 4, &output)); |
| 76 | |
| 77 | // Testing the truncation of valid UTF8 correctly |
| 78 | EXPECT_TRUE(Truncated("abc", 2, &output)); |
| 79 | EXPECT_EQ(output, "ab"); |
| 80 | EXPECT_TRUE(Truncated("\xc2\x81\xc2\x81", 2, &output)); |
| 81 | EXPECT_EQ(output.compare("\xc2\x81"), 0); |
| 82 | EXPECT_TRUE(Truncated("\xc2\x81\xc2\x81", 3, &output)); |
| 83 | EXPECT_EQ(output.compare("\xc2\x81"), 0); |
| 84 | EXPECT_FALSE(Truncated("\xc2\x81\xc2\x81", 4, &output)); |
| 85 | EXPECT_EQ(output.compare("\xc2\x81\xc2\x81"), 0); |
| 86 | |
| 87 | { |
| 88 | const char array[] = "\x00\x00\xc2\x81\xc2\x81"; |
| 89 | const std::string array_string(array, arraysize(array)); |
| 90 | EXPECT_TRUE(Truncated(array_string, 4, &output)); |
| 91 | EXPECT_EQ(output.compare(std::string("\x00\x00\xc2\x81", 4)), 0); |
| 92 | } |
| 93 | |
| 94 | { |
| 95 | const char array[] = "\x00\xc2\x81\xc2\x81"; |
| 96 | const std::string array_string(array, arraysize(array)); |
| 97 | EXPECT_TRUE(Truncated(array_string, 4, &output)); |
| 98 | EXPECT_EQ(output.compare(std::string("\x00\xc2\x81", 3)), 0); |
| 99 | } |
| 100 | |
| 101 | // Testing invalid UTF8 |
| 102 | EXPECT_TRUE(Truncated("\xed\xa0\x80\xed\xbf\xbf", 6, &output)); |
| 103 | EXPECT_EQ(output.compare(""), 0); |
| 104 | EXPECT_TRUE(Truncated("\xed\xa0\x8f", 3, &output)); |
| 105 | EXPECT_EQ(output.compare(""), 0); |
| 106 | EXPECT_TRUE(Truncated("\xed\xbf\xbf", 3, &output)); |
| 107 | EXPECT_EQ(output.compare(""), 0); |
| 108 | |
| 109 | // Testing invalid UTF8 mixed with valid UTF8 |
| 110 | EXPECT_FALSE(Truncated("\xe1\x80\xbf", 3, &output)); |
| 111 | EXPECT_EQ(output.compare("\xe1\x80\xbf"), 0); |
| 112 | EXPECT_FALSE(Truncated("\xf1\x80\xa0\xbf", 4, &output)); |
| 113 | EXPECT_EQ(output.compare("\xf1\x80\xa0\xbf"), 0); |
| 114 | EXPECT_FALSE(Truncated("a\xc2\x81\xe1\x80\xbf\xf1\x80\xa0\xbf", |
| 115 | 10, &output)); |
| 116 | EXPECT_EQ(output.compare("a\xc2\x81\xe1\x80\xbf\xf1\x80\xa0\xbf"), 0); |
| 117 | EXPECT_TRUE(Truncated("a\xc2\x81\xe1\x80\xbf\xf1""a""\x80\xa0", |
| 118 | 10, &output)); |
| 119 | EXPECT_EQ(output.compare("a\xc2\x81\xe1\x80\xbf\xf1""a"), 0); |
| 120 | EXPECT_FALSE(Truncated("\xef\xbb\xbf" "abc", 6, &output)); |
| 121 | EXPECT_EQ(output.compare("\xef\xbb\xbf" "abc"), 0); |
| 122 | |
| 123 | // Overlong sequences |
nothing calls this directly
no test coverage detected