[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 7 | #include "base/logging.h" |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 8 | #include "base/macros.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 9 | #include "base/strings/string_piece.h" |
[email protected] | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
[email protected] | 7ec4900 | 2013-05-06 21:32:31 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | namespace { |
| 18 | |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 19 | const wchar_t* const kConvertRoundtripCases[] = { |
| 20 | L"Google Video", |
| 21 | // "网页 图片 资讯更多 »" |
| 22 | L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb", |
| 23 | // "Παγκόσμιος Ιστός" |
| 24 | L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9" |
| 25 | L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2", |
| 26 | // "Поиск страниц на русском" |
| 27 | L"\x041f\x043e\x0438\x0441\x043a\x0020\x0441\x0442" |
| 28 | L"\x0440\x0430\x043d\x0438\x0446\x0020\x043d\x0430" |
| 29 | L"\x0020\x0440\x0443\x0441\x0441\x043a\x043e\x043c", |
| 30 | // "전체서비스" |
| 31 | L"\xc804\xccb4\xc11c\xbe44\xc2a4", |
| 32 | |
| 33 | // Test characters that take more than 16 bits. This will depend on whether |
| 34 | // wchar_t is 16 or 32 bits. |
| 35 | #if defined(WCHAR_T_IS_UTF16) |
| 36 | L"\xd800\xdf00", |
| 37 | // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) |
| 38 | L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", |
| 39 | #elif defined(WCHAR_T_IS_UTF32) |
| 40 | L"\x10300", |
| 41 | // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) |
| 42 | L"\x11d40\x11d41\x11d42\x11d43\x11d44", |
| 43 | #endif |
| 44 | }; |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | TEST(UTFStringConversionsTest, ConvertUTF8AndWide) { |
| 49 | // we round-trip all the wide strings through UTF-8 to make sure everything |
| 50 | // agrees on the conversion. This uses the stream operators to test them |
| 51 | // simultaneously. |
| 52 | for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
| 53 | std::ostringstream utf8; |
| 54 | utf8 << WideToUTF8(kConvertRoundtripCases[i]); |
| 55 | std::wostringstream wide; |
| 56 | wide << UTF8ToWide(utf8.str()); |
| 57 | |
| 58 | EXPECT_EQ(kConvertRoundtripCases[i], wide.str()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | TEST(UTFStringConversionsTest, ConvertUTF8AndWideEmptyString) { |
| 63 | // An empty std::wstring should be converted to an empty std::string, |
| 64 | // and vice versa. |
| 65 | std::wstring wempty; |
| 66 | std::string empty; |
| 67 | EXPECT_EQ(empty, WideToUTF8(wempty)); |
| 68 | EXPECT_EQ(wempty, UTF8ToWide(empty)); |
| 69 | } |
| 70 | |
| 71 | TEST(UTFStringConversionsTest, ConvertUTF8ToWide) { |
| 72 | struct UTF8ToWideCase { |
| 73 | const char* utf8; |
| 74 | const wchar_t* wide; |
| 75 | bool success; |
| 76 | } convert_cases[] = { |
| 77 | // Regular UTF-8 input. |
| 78 | {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true}, |
[email protected] | 858d4887 | 2010-01-16 17:56:08 | [diff] [blame] | 79 | // Non-character is passed through. |
| 80 | {"\xef\xbf\xbfHello", L"\xffffHello", true}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 81 | // Truncated UTF-8 sequence. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 82 | {"\xe4\xa0\xe5\xa5\xbd", L"\xfffd\x597d", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 83 | // Truncated off the end. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 84 | {"\xe5\xa5\xbd\xe4\xa0", L"\x597d\xfffd", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 85 | // Non-shortest-form UTF-8. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 86 | {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", L"\xfffd\x597d", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 87 | // This UTF-8 character decodes to a UTF-16 surrogate, which is illegal. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 88 | {"\xed\xb0\x80", L"\xfffd", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 89 | // Non-BMP characters. The second is a non-character regarded as valid. |
| 90 | // The result will either be in UTF-16 or UTF-32. |
| 91 | #if defined(WCHAR_T_IS_UTF16) |
| 92 | {"A\xF0\x90\x8C\x80z", L"A\xd800\xdf00z", true}, |
[email protected] | 858d4887 | 2010-01-16 17:56:08 | [diff] [blame] | 93 | {"A\xF4\x8F\xBF\xBEz", L"A\xdbff\xdffez", true}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 94 | #elif defined(WCHAR_T_IS_UTF32) |
| 95 | {"A\xF0\x90\x8C\x80z", L"A\x10300z", true}, |
[email protected] | 858d4887 | 2010-01-16 17:56:08 | [diff] [blame] | 96 | {"A\xF4\x8F\xBF\xBEz", L"A\x10fffez", true}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 97 | #endif |
| 98 | }; |
| 99 | |
viettrungluu | 805eabb | 2014-10-16 04:02:49 | [diff] [blame] | 100 | for (size_t i = 0; i < arraysize(convert_cases); i++) { |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 101 | std::wstring converted; |
| 102 | EXPECT_EQ(convert_cases[i].success, |
| 103 | UTF8ToWide(convert_cases[i].utf8, |
| 104 | strlen(convert_cases[i].utf8), |
| 105 | &converted)); |
| 106 | std::wstring expected(convert_cases[i].wide); |
| 107 | EXPECT_EQ(expected, converted); |
| 108 | } |
| 109 | |
| 110 | // Manually test an embedded NULL. |
| 111 | std::wstring converted; |
| 112 | EXPECT_TRUE(UTF8ToWide("\00Z\t", 3, &converted)); |
| 113 | ASSERT_EQ(3U, converted.length()); |
| 114 | EXPECT_EQ(static_cast<wchar_t>(0), converted[0]); |
| 115 | EXPECT_EQ('Z', converted[1]); |
| 116 | EXPECT_EQ('\t', converted[2]); |
| 117 | |
| 118 | // Make sure that conversion replaces, not appends. |
| 119 | EXPECT_TRUE(UTF8ToWide("B", 1, &converted)); |
| 120 | ASSERT_EQ(1U, converted.length()); |
| 121 | EXPECT_EQ('B', converted[0]); |
| 122 | } |
| 123 | |
| 124 | #if defined(WCHAR_T_IS_UTF16) |
| 125 | // This test is only valid when wchar_t == UTF-16. |
| 126 | TEST(UTFStringConversionsTest, ConvertUTF16ToUTF8) { |
| 127 | struct WideToUTF8Case { |
| 128 | const wchar_t* utf16; |
| 129 | const char* utf8; |
| 130 | bool success; |
| 131 | } convert_cases[] = { |
| 132 | // Regular UTF-16 input. |
| 133 | {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true}, |
| 134 | // Test a non-BMP character. |
| 135 | {L"\xd800\xdf00", "\xF0\x90\x8C\x80", true}, |
[email protected] | 858d4887 | 2010-01-16 17:56:08 | [diff] [blame] | 136 | // Non-characters are passed through. |
| 137 | {L"\xffffHello", "\xEF\xBF\xBFHello", true}, |
| 138 | {L"\xdbff\xdffeHello", "\xF4\x8F\xBF\xBEHello", true}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 139 | // The first character is a truncated UTF-16 character. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 140 | {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 141 | // Truncated at the end. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 142 | {L"\x597d\xd800", "\xe5\xa5\xbd\xef\xbf\xbd", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 143 | }; |
| 144 | |
thakis | 9230c1d | 2015-12-10 00:36:25 | [diff] [blame] | 145 | for (const auto& test : convert_cases) { |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 146 | std::string converted; |
thakis | 9230c1d | 2015-12-10 00:36:25 | [diff] [blame] | 147 | EXPECT_EQ(test.success, |
| 148 | WideToUTF8(test.utf16, wcslen(test.utf16), &converted)); |
| 149 | std::string expected(test.utf8); |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 150 | EXPECT_EQ(expected, converted); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #elif defined(WCHAR_T_IS_UTF32) |
| 155 | // This test is only valid when wchar_t == UTF-32. |
| 156 | TEST(UTFStringConversionsTest, ConvertUTF32ToUTF8) { |
| 157 | struct WideToUTF8Case { |
| 158 | const wchar_t* utf32; |
| 159 | const char* utf8; |
| 160 | bool success; |
| 161 | } convert_cases[] = { |
| 162 | // Regular 16-bit input. |
| 163 | {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true}, |
| 164 | // Test a non-BMP character. |
| 165 | {L"A\x10300z", "A\xF0\x90\x8C\x80z", true}, |
[email protected] | 858d4887 | 2010-01-16 17:56:08 | [diff] [blame] | 166 | // Non-characters are passed through. |
| 167 | {L"\xffffHello", "\xEF\xBF\xBFHello", true}, |
| 168 | {L"\x10fffeHello", "\xF4\x8F\xBF\xBEHello", true}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 169 | // Invalid Unicode code points. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 170 | {L"\xfffffffHello", "\xEF\xBF\xBDHello", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 171 | // The first character is a truncated UTF-16 character. |
[email protected] | d7a3e8e | 2010-01-01 22:16:38 | [diff] [blame] | 172 | {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, |
| 173 | {L"\xdc01Hello", "\xef\xbf\xbdHello", false}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 174 | }; |
| 175 | |
thakis | 9230c1d | 2015-12-10 00:36:25 | [diff] [blame] | 176 | for (const auto& test : convert_cases) { |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 177 | std::string converted; |
thakis | 9230c1d | 2015-12-10 00:36:25 | [diff] [blame] | 178 | EXPECT_EQ(test.success, |
| 179 | WideToUTF8(test.utf32, wcslen(test.utf32), &converted)); |
| 180 | std::string expected(test.utf8); |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 181 | EXPECT_EQ(expected, converted); |
| 182 | } |
| 183 | } |
| 184 | #endif // defined(WCHAR_T_IS_UTF32) |
| 185 | |
| 186 | TEST(UTFStringConversionsTest, ConvertMultiString) { |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 187 | static char16 multi16[] = { |
| 188 | 'f', 'o', 'o', '\0', |
| 189 | 'b', 'a', 'r', '\0', |
| 190 | 'b', 'a', 'z', '\0', |
| 191 | '\0' |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 192 | }; |
| 193 | static char multi[] = { |
| 194 | 'f', 'o', 'o', '\0', |
| 195 | 'b', 'a', 'r', '\0', |
| 196 | 'b', 'a', 'z', '\0', |
| 197 | '\0' |
| 198 | }; |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 199 | string16 multistring16; |
| 200 | memcpy(WriteInto(&multistring16, arraysize(multi16)), multi16, |
| 201 | sizeof(multi16)); |
| 202 | EXPECT_EQ(arraysize(multi16) - 1, multistring16.length()); |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 203 | std::string expected; |
| 204 | memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); |
| 205 | EXPECT_EQ(arraysize(multi) - 1, expected.length()); |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 206 | const std::string& converted = UTF16ToUTF8(multistring16); |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 207 | EXPECT_EQ(arraysize(multi) - 1, converted.length()); |
| 208 | EXPECT_EQ(expected, converted); |
| 209 | } |
| 210 | |
danakj | c3762b9 | 2015-03-07 01:51:42 | [diff] [blame] | 211 | } // namespace base |