• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
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 "base/logging.h"
8 #include "base/macros.h"
9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 
17 namespace {
18 
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 
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 (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 
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\x597d", false},
87     // This UTF-8 character decodes to a UTF-16 surrogate, which is illegal.
88     {"\xed\xb0\x80", L"\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_UTF16)
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_UTF32)
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 (size_t i = 0; i < arraysize(convert_cases); i++) {
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.
TEST(UTFStringConversionsTest,ConvertUTF16ToUTF8)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},
136     // Non-characters are passed through.
137     {L"\xffffHello", "\xEF\xBF\xBFHello", true},
138     {L"\xdbff\xdffeHello", "\xF4\x8F\xBF\xBEHello", true},
139     // The first character is a truncated UTF-16 character.
140     {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false},
141     // Truncated at the end.
142     {L"\x597d\xd800", "\xe5\xa5\xbd\xef\xbf\xbd", false},
143   };
144 
145   for (const auto& test : convert_cases) {
146     std::string converted;
147     EXPECT_EQ(test.success,
148               WideToUTF8(test.utf16, wcslen(test.utf16), &converted));
149     std::string expected(test.utf8);
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.
TEST(UTFStringConversionsTest,ConvertUTF32ToUTF8)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},
166     // Non-characters are passed through.
167     {L"\xffffHello", "\xEF\xBF\xBFHello", true},
168     {L"\x10fffeHello", "\xF4\x8F\xBF\xBEHello", true},
169     // Invalid Unicode code points.
170     {L"\xfffffffHello", "\xEF\xBF\xBDHello", false},
171     // The first character is a truncated UTF-16 character.
172     {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false},
173     {L"\xdc01Hello", "\xef\xbf\xbdHello", false},
174   };
175 
176   for (const auto& test : convert_cases) {
177     std::string converted;
178     EXPECT_EQ(test.success,
179               WideToUTF8(test.utf32, wcslen(test.utf32), &converted));
180     std::string expected(test.utf8);
181     EXPECT_EQ(expected, converted);
182   }
183 }
184 #endif  // defined(WCHAR_T_IS_UTF32)
185 
TEST(UTFStringConversionsTest,ConvertMultiString)186 TEST(UTFStringConversionsTest, ConvertMultiString) {
187   static char16 multi16[] = {
188     'f', 'o', 'o', '\0',
189     'b', 'a', 'r', '\0',
190     'b', 'a', 'z', '\0',
191     '\0'
192   };
193   static char multi[] = {
194     'f', 'o', 'o', '\0',
195     'b', 'a', 'r', '\0',
196     'b', 'a', 'z', '\0',
197     '\0'
198   };
199   string16 multistring16;
200   memcpy(WriteInto(&multistring16, arraysize(multi16)), multi16,
201                    sizeof(multi16));
202   EXPECT_EQ(arraysize(multi16) - 1, multistring16.length());
203   std::string expected;
204   memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi));
205   EXPECT_EQ(arraysize(multi) - 1, expected.length());
206   const std::string& converted = UTF16ToUTF8(multistring16);
207   EXPECT_EQ(arraysize(multi) - 1, converted.length());
208   EXPECT_EQ(expected, converted);
209 }
210 
211 }  // namespace base
212