1 // Copyright 2011 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 <stddef.h>
6
7 #include <string>
8
9 #include "base/strings/sys_string_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/test/scoped_locale.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 #ifdef WCHAR_T_IS_32_BIT
16 static const std::wstring kSysWideOldItalicLetterA = L"\x10300";
17 #else
18 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00";
19 #endif
20
21 namespace base {
22
TEST(SysStrings,SysWideToUTF8)23 TEST(SysStrings, SysWideToUTF8) {
24 EXPECT_EQ("Hello, world", SysWideToUTF8(L"Hello, world"));
25 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToUTF8(L"\x4f60\x597d"));
26
27 // A value outside of the BMP and therefore not representable with one UTF-16
28 // code unit.
29 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToUTF8(kSysWideOldItalicLetterA));
30
31 // Error case. When Windows finds a UTF-16 character going off the end of
32 // a string, it just converts that literal value to UTF-8, even though this
33 // is invalid.
34 //
35 // This is what XP does, but Vista has different behavior, so we don't bother
36 // verifying it:
37 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw",
38 // SysWideToUTF8(L"\x4f60\xd800zyxw"));
39
40 // Test embedded NULLs.
41 std::wstring wide_null(L"a");
42 wide_null.push_back(0);
43 wide_null.push_back('b');
44
45 std::string expected_null("a");
46 expected_null.push_back(0);
47 expected_null.push_back('b');
48
49 EXPECT_EQ(expected_null, SysWideToUTF8(wide_null));
50 }
51
TEST(SysStrings,SysUTF8ToWide)52 TEST(SysStrings, SysUTF8ToWide) {
53 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world"));
54 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd"));
55
56 // A value outside of the BMP and therefore not representable with one UTF-16
57 // code unit.
58 EXPECT_EQ(kSysWideOldItalicLetterA, SysUTF8ToWide("\xF0\x90\x8C\x80"));
59
60 // Error case. When Windows finds an invalid UTF-8 character, it just skips
61 // it. This seems weird because it's inconsistent with the reverse conversion.
62 //
63 // This is what XP does, but Vista has different behavior, so we don't bother
64 // verifying it:
65 // EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw"));
66
67 // Test embedded NULLs.
68 std::string utf8_null("a");
69 utf8_null.push_back(0);
70 utf8_null.push_back('b');
71
72 std::wstring expected_null(L"a");
73 expected_null.push_back(0);
74 expected_null.push_back('b');
75
76 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null));
77 }
78
79 // Tests depend on setting a specific Linux locale.
80 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
TEST(SysStrings,SysWideToNativeMB)81 TEST(SysStrings, SysWideToNativeMB) {
82 #if !defined(SYSTEM_NATIVE_UTF8)
83 ScopedLocale locale("en_US.UTF-8");
84 #endif
85 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world"));
86 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToNativeMB(L"\x4f60\x597d"));
87
88 // A value outside of the BMP and therefore not representable with one UTF-16
89 // code unit.
90 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA));
91
92 // Error case. When Windows finds a UTF-16 character going off the end of
93 // a string, it just converts that literal value to UTF-8, even though this
94 // is invalid.
95 //
96 // This is what XP does, but Vista has different behavior, so we don't bother
97 // verifying it:
98 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw",
99 // SysWideToNativeMB(L"\x4f60\xd800zyxw"));
100
101 // Test embedded NULLs.
102 std::wstring wide_null(L"a");
103 wide_null.push_back(0);
104 wide_null.push_back('b');
105
106 std::string expected_null("a");
107 expected_null.push_back(0);
108 expected_null.push_back('b');
109
110 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null));
111 }
112
113 // We assume the test is running in a UTF8 locale.
TEST(SysStrings,SysNativeMBToWide)114 TEST(SysStrings, SysNativeMBToWide) {
115 #if !defined(SYSTEM_NATIVE_UTF8)
116 ScopedLocale locale("en_US.UTF-8");
117 #endif
118 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world"));
119 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd"));
120
121 // A value outside of the BMP and therefore not representable with one UTF-16
122 // code unit.
123 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80"));
124
125 // Error case. When Windows finds an invalid UTF-8 character, it just skips
126 // it. This seems weird because it's inconsistent with the reverse conversion.
127 //
128 // This is what XP does, but Vista has different behavior, so we don't bother
129 // verifying it:
130 // EXPECT_EQ(L"\x4f60zyxw", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5zyxw"));
131
132 // Test embedded NULLs.
133 std::string utf8_null("a");
134 utf8_null.push_back(0);
135 utf8_null.push_back('b');
136
137 std::wstring expected_null(L"a");
138 expected_null.push_back(0);
139 expected_null.push_back('b');
140
141 EXPECT_EQ(expected_null, SysNativeMBToWide(utf8_null));
142 }
143
144 static const wchar_t* const kConvertRoundtripCases[] = {
145 L"Google Video",
146 // "网页 图片 资讯更多 »"
147 L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb",
148 // "Παγκόσμιος Ιστός"
149 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
150 L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2",
151 // "Поиск страниц на русском"
152 L"\x041f\x043e\x0438\x0441\x043a\x0020\x0441\x0442"
153 L"\x0440\x0430\x043d\x0438\x0446\x0020\x043d\x0430"
154 L"\x0020\x0440\x0443\x0441\x0441\x043a\x043e\x043c",
155 // "전체서비스"
156 L"\xc804\xccb4\xc11c\xbe44\xc2a4",
157
158 // Test characters that take more than 16 bits. This will depend on whether
159 // wchar_t is 16 or 32 bits.
160 #if defined(WCHAR_T_IS_16_BIT)
161 L"\xd800\xdf00",
162 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 :
163 // A,B,C,D,E)
164 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44",
165 #elif defined(WCHAR_T_IS_32_BIT)
166 L"\x10300",
167 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 :
168 // A,B,C,D,E)
169 L"\x11d40\x11d41\x11d42\x11d43\x11d44",
170 #endif
171 };
172
173
TEST(SysStrings,SysNativeMBAndWide)174 TEST(SysStrings, SysNativeMBAndWide) {
175 #if !defined(SYSTEM_NATIVE_UTF8)
176 ScopedLocale locale("en_US.UTF-8");
177 #endif
178 for (auto* i : kConvertRoundtripCases) {
179 std::wstring wide = i;
180 std::wstring trip = SysNativeMBToWide(SysWideToNativeMB(wide));
181 EXPECT_EQ(wide.size(), trip.size());
182 EXPECT_EQ(wide, trip);
183 }
184
185 // We assume our test is running in UTF-8, so double check through ICU.
186 for (auto* i : kConvertRoundtripCases) {
187 std::wstring wide = i;
188 std::wstring trip = SysNativeMBToWide(WideToUTF8(wide));
189 EXPECT_EQ(wide.size(), trip.size());
190 EXPECT_EQ(wide, trip);
191 }
192
193 for (auto* i : kConvertRoundtripCases) {
194 std::wstring wide = i;
195 std::wstring trip = UTF8ToWide(SysWideToNativeMB(wide));
196 EXPECT_EQ(wide.size(), trip.size());
197 EXPECT_EQ(wide, trip);
198 }
199 }
200 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
201
202 } // namespace base
203