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