1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/strings/utf_string_conversions.h"
6
7 #include <stddef.h>
8
9 #include "base/strings/string_util.h"
10 #include "build/build_config.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace base {
14
15 namespace {
16
17 const wchar_t* const kConvertRoundtripCases[] = {
18 L"Google Video",
19 // "网页 图片 资讯更多 »"
20 L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb",
21 // "Παγκόσμιος Ιστός"
22 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
23 L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2",
24 // "Поиск страниц на русском"
25 L"\x041f\x043e\x0438\x0441\x043a\x0020\x0441\x0442"
26 L"\x0440\x0430\x043d\x0438\x0446\x0020\x043d\x0430"
27 L"\x0020\x0440\x0443\x0441\x0441\x043a\x043e\x043c",
28 // "전체서비스"
29 L"\xc804\xccb4\xc11c\xbe44\xc2a4",
30
31 // Test characters that take more than 16 bits. This will depend on whether
32 // wchar_t is 16 or 32 bits.
33 #if defined(WCHAR_T_IS_16_BIT)
34 L"\xd800\xdf00",
35 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 :
36 // A,B,C,D,E)
37 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44",
38 #elif defined(WCHAR_T_IS_32_BIT)
39 L"\x10300",
40 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 :
41 // A,B,C,D,E)
42 L"\x11d40\x11d41\x11d42\x11d43\x11d44",
43 #endif
44 };
45
46 } // namespace
47
TEST(UTFStringConversionsTest,ConvertUTF8AndWide)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 (auto* i : kConvertRoundtripCases) {
53 std::ostringstream utf8;
54 utf8 << WideToUTF8(i);
55 std::wostringstream wide;
56 wide << UTF8ToWide(utf8.str());
57
58 EXPECT_EQ(i, wide.str());
59 }
60 }
61
TEST(UTFStringConversionsTest,ConvertUTF8AndWideEmptyString)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
TEST(UTFStringConversionsTest,ConvertUTF8ToWide)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},
79 // Non-character is passed through.
80 {"\xef\xbf\xbfHello", L"\xffffHello", true},
81 // Truncated UTF-8 sequence.
82 {"\xe4\xa0\xe5\xa5\xbd", L"\xfffd\x597d", false},
83 // Truncated off the end.
84 {"\xe5\xa5\xbd\xe4\xa0", L"\x597d\xfffd", false},
85 // Non-shortest-form UTF-8.
86 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", L"\xfffd\xfffd\xfffd\xfffd\x597d", false},
87 // This UTF-8 character decodes to a UTF-16 surrogate, which is illegal.
88 {"\xed\xb0\x80", L"\xfffd\xfffd\xfffd", false},
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_16_BIT)
92 {"A\xF0\x90\x8C\x80z", L"A\xd800\xdf00z", true},
93 {"A\xF4\x8F\xBF\xBEz", L"A\xdbff\xdffez", true},
94 #elif defined(WCHAR_T_IS_32_BIT)
95 {"A\xF0\x90\x8C\x80z", L"A\x10300z", true},
96 {"A\xF4\x8F\xBF\xBEz", L"A\x10fffez", true},
97 #endif
98 };
99
100 for (const auto& i : convert_cases) {
101 std::wstring converted;
102 EXPECT_EQ(i.success, UTF8ToWide(i.utf8, strlen(i.utf8), &converted));
103 std::wstring expected(i.wide);
104 EXPECT_EQ(expected, converted);
105 }
106
107 // Manually test an embedded NULL.
108 std::wstring converted;
109 EXPECT_TRUE(UTF8ToWide("\00Z\t", 3, &converted));
110 ASSERT_EQ(3U, converted.length());
111 EXPECT_EQ(static_cast<wchar_t>(0), converted[0]);
112 EXPECT_EQ('Z', converted[1]);
113 EXPECT_EQ('\t', converted[2]);
114
115 // Make sure that conversion replaces, not appends.
116 EXPECT_TRUE(UTF8ToWide("B", 1, &converted));
117 ASSERT_EQ(1U, converted.length());
118 EXPECT_EQ('B', converted[0]);
119 }
120
121 #if defined(WCHAR_T_IS_16_BIT)
122 // This test is only valid when wchar_t == UTF-16.
TEST(UTFStringConversionsTest,ConvertUTF16ToUTF8)123 TEST(UTFStringConversionsTest, ConvertUTF16ToUTF8) {
124 struct WideToUTF8Case {
125 const wchar_t* utf16;
126 const char* utf8;
127 bool success;
128 } convert_cases[] = {
129 // Regular UTF-16 input.
130 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true},
131 // Test a non-BMP character.
132 {L"\xd800\xdf00", "\xF0\x90\x8C\x80", true},
133 // Non-characters are passed through.
134 {L"\xffffHello", "\xEF\xBF\xBFHello", true},
135 {L"\xdbff\xdffeHello", "\xF4\x8F\xBF\xBEHello", true},
136 // The first character is a truncated UTF-16 character.
137 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false},
138 // Truncated at the end.
139 {L"\x597d\xd800", "\xe5\xa5\xbd\xef\xbf\xbd", false},
140 };
141
142 for (const auto& test : convert_cases) {
143 std::string converted;
144 EXPECT_EQ(test.success,
145 WideToUTF8(test.utf16, wcslen(test.utf16), &converted));
146 std::string expected(test.utf8);
147 EXPECT_EQ(expected, converted);
148 }
149 }
150
151 #elif defined(WCHAR_T_IS_32_BIT)
152 // This test is only valid when wchar_t == UTF-32.
TEST(UTFStringConversionsTest,ConvertUTF32ToUTF8)153 TEST(UTFStringConversionsTest, ConvertUTF32ToUTF8) {
154 struct WideToUTF8Case {
155 const wchar_t* utf32;
156 const char* utf8;
157 bool success;
158 } convert_cases[] = {
159 // Regular 16-bit input.
160 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true},
161 // Test a non-BMP character.
162 {L"A\x10300z", "A\xF0\x90\x8C\x80z", true},
163 // Non-characters are passed through.
164 {L"\xffffHello", "\xEF\xBF\xBFHello", true},
165 {L"\x10fffeHello", "\xF4\x8F\xBF\xBEHello", true},
166 // Invalid Unicode code points.
167 {L"\xfffffffHello", "\xEF\xBF\xBDHello", false},
168 // The first character is a truncated UTF-16 character.
169 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false},
170 {L"\xdc01Hello", "\xef\xbf\xbdHello", false},
171 };
172
173 for (const auto& test : convert_cases) {
174 std::string converted;
175 EXPECT_EQ(test.success,
176 WideToUTF8(test.utf32, wcslen(test.utf32), &converted));
177 std::string expected(test.utf8);
178 EXPECT_EQ(expected, converted);
179 }
180 }
181 #endif // defined(WCHAR_T_IS_32_BIT)
182
TEST(UTFStringConversionsTest,ConvertMultiString)183 TEST(UTFStringConversionsTest, ConvertMultiString) {
184 // `operator""s` will avoid truncating the strings at the first embedded NUL.
185 using std::string_literals::operator""s;
186 std::u16string multistring16 = u"foo\0bar\0baz\0"s;
187 std::string expected = "foo\0bar\0baz\0"s;
188 ASSERT_EQ(12u, multistring16.size());
189 ASSERT_EQ(12u, expected.size());
190
191 EXPECT_EQ(expected, UTF16ToUTF8(multistring16));
192 }
193
194 } // namespace base
195