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