blob: 381112d0484d030702bf88f2f440bae273bd61b9 [file] [log] [blame]
Dan Albert7a7f9952014-06-02 11:33:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
Elliott Hughes18181e62019-01-30 13:52:36 -080018#include <uchar.h>
Dan Albert7a7f9952014-06-02 11:33:04 -070019
20#include <gtest/gtest.h>
21
22#include <errno.h>
23#include <limits.h>
24#include <locale.h>
25#include <stdint.h>
26
Dan Albert1252ab02023-07-14 21:23:36 +000027// Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
28// newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
29// and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
30// https://datatracker.ietf.org/doc/html/rfc2279.
31//
32// Bionic's unicode implementation was written after the high values were
33// excluded, so it has never supported them. Other implementations (at least
34// as of glibc 2.36), do support those sequences.
35#if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
Dan Albert78da2922023-07-19 18:23:46 +000036constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
Dan Albert1252ab02023-07-14 21:23:36 +000037#elif defined(__GLIBC__)
Dan Albert78da2922023-07-19 18:23:46 +000038constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
Dan Albert1252ab02023-07-14 21:23:36 +000039#else
Dan Albert78da2922023-07-19 18:23:46 +000040#error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
Dan Albert1252ab02023-07-14 21:23:36 +000041#endif
42
Dan Albert9a9bbe52023-07-18 23:12:47 +000043// C23 7.30.1 (for each `mbrtoc*` function) says:
44//
45// Returns:
46//
47// 0 if the next n or fewer bytes complete the multibyte character that
48// corresponds to the null wide character (which is the value stored).
49//
50// (size_t)(-2) if the next n bytes contribute to an incomplete (but
51// potentially valid) multibyte character, and all n bytes have been
52// processed (no value is stored).
53//
54// Bionic historically interpreted the behavior when n is 0 to be the next 0
55// bytes decoding to the null. That's a pretty bad interpretation, and both
56// glibc and musl return -2 for that case.
57//
58// The tests currently checks the incorrect behavior for bionic because gtest
59// doesn't support explicit xfail annotations. The behavior difference here
60// should be fixed, but danalbert wants to add more tests before tackling the
61// bugs.
62#ifdef __ANDROID__
63constexpr size_t kExpectedResultForZeroLength = 0U;
64#else
65constexpr size_t kExpectedResultForZeroLength = static_cast<size_t>(-2);
66#endif
67
Dan Albert7a7f9952014-06-02 11:33:04 -070068TEST(uchar, sizeof_uchar_t) {
Dan Albert7a7f9952014-06-02 11:33:04 -070069 EXPECT_EQ(2U, sizeof(char16_t));
70 EXPECT_EQ(4U, sizeof(char32_t));
Dan Albert7a7f9952014-06-02 11:33:04 -070071}
72
73TEST(uchar, start_state) {
Dan Albert7a7f9952014-06-02 11:33:04 -070074 char out[MB_LEN_MAX];
75 mbstate_t ps;
76
77 // Any non-initial state is invalid when calling c32rtomb.
78 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070079 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -070080 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -070081 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
82 EXPECT_EQ(EILSEQ, errno);
83
Yi Kong32bc0fc2018-08-02 17:31:13 -070084 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
Dan Albert7a7f9952014-06-02 11:33:04 -070085 // state should be reset.
86 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070087 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
88 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070089 EXPECT_TRUE(mbsinit(&ps));
90
91 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070092 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070093 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
94 EXPECT_TRUE(mbsinit(&ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070095}
96
97TEST(uchar, c16rtomb_null_out) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
99 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700100}
101
102TEST(uchar, c16rtomb_null_char) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700103 char bytes[MB_LEN_MAX];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700104 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700105}
106
107TEST(uchar, c16rtomb) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700108 char bytes[MB_LEN_MAX];
109
110 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700111 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700112 EXPECT_EQ('h', bytes[0]);
113
114 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
115 uselocale(LC_GLOBAL_LOCALE);
116
117 // 1-byte UTF-8.
118 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700119 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700120 EXPECT_EQ('h', bytes[0]);
121 // 2-byte UTF-8.
122 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700123 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700124 EXPECT_EQ('\xc2', bytes[0]);
125 EXPECT_EQ('\xa2', bytes[1]);
126 // 3-byte UTF-8.
127 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700128 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700129 EXPECT_EQ('\xe2', bytes[0]);
130 EXPECT_EQ('\x82', bytes[1]);
131 EXPECT_EQ('\xac', bytes[2]);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700132 // 4-byte UTF-8 from a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700133 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700134 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
135 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700136 EXPECT_EQ('\xf4', bytes[0]);
137 EXPECT_EQ('\x8a', bytes[1]);
138 EXPECT_EQ('\xaf', bytes[2]);
139 EXPECT_EQ('\x8d', bytes[3]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700140}
141
142TEST(uchar, c16rtomb_invalid) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700143 char bytes[MB_LEN_MAX];
144
145 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700146 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700147
Yi Kong32bc0fc2018-08-02 17:31:13 -0700148 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
149 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700150}
151
152TEST(uchar, mbrtoc16_null) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700153 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700154}
155
156TEST(uchar, mbrtoc16_zero_len) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700157 char16_t out;
158
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400159 out = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000160 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "hello", 0, nullptr));
161 EXPECT_EQ(L'x', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700162
Dan Albert9a9bbe52023-07-18 23:12:47 +0000163 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "hello", 0, nullptr));
164 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "", 0, nullptr));
165 EXPECT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
166 EXPECT_EQ(L'h', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700167}
168
169TEST(uchar, mbrtoc16) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700170 char16_t out;
171
172 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
173 uselocale(LC_GLOBAL_LOCALE);
174
175 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700176 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700177 ASSERT_EQ(L'a', out);
178 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700179 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700180 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
181 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700182 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700183 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700184 // 4-byte UTF-8 will be returned as a surrogate pair...
Dan Albert1e8e0c12023-07-19 19:14:45 +0000185#ifdef __BIONIC__
186 // https://issuetracker.google.com/289419882
187 //
188 // We misread the spec when implementing this. The first call should return
189 // the length of the decoded character, and the second call should return -3
190 // to indicate that the output is a continuation of the character decoded by
191 // the first call.
192 //
193 // C23 7.30.1.3.4:
194 //
195 // between 1 and n inclusive if the next n or fewer bytes complete a valid
196 // multibyte character (which is the value stored); the value returned is
197 // the number of bytes that complete the multibyte character.
198 //
199 // (size_t)(-3) if the next character resulting from a previous call has
200 // been stored (no bytes from the input have been consumed by this call).
201 //
202 // Leaving the test for the wrong outputs here while we clean up and improve
203 // the rest of the tests to get a better handle on the behavior differences
204 // before fixing the bug.
Dan Albert7a7f9952014-06-02 11:33:04 -0700205 ASSERT_EQ(static_cast<size_t>(-3),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700206 mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700207 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700208 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d" "ef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700209 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1e8e0c12023-07-19 19:14:45 +0000210#else
211 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
212 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
213 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
214 "\xf4\x8a\xaf\x8d"
215 "ef",
216 6, nullptr));
217 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
218#endif
Dan Albert1252ab02023-07-14 21:23:36 +0000219}
220
221TEST(uchar, mbrtoc16_long_sequences) {
222 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
223 uselocale(LC_GLOBAL_LOCALE);
224
225 char16_t out = u'\0';
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700226 errno = 0;
Dan Albert1252ab02023-07-14 21:23:36 +0000227 auto result = mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000228 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert1252ab02023-07-14 21:23:36 +0000229 EXPECT_EQ(static_cast<size_t>(-1), result);
230 EXPECT_EQ(EILSEQ, errno);
231 EXPECT_EQ(u'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000232 } else {
233 EXPECT_EQ(5U, result);
234 EXPECT_EQ(0, errno);
235 EXPECT_EQ(u'\uf94a', out);
Dan Albert1252ab02023-07-14 21:23:36 +0000236 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700237}
238
239TEST(uchar, mbrtoc16_reserved_range) {
Dan Albertef8e1582023-07-19 19:34:12 +0000240 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
241
242 errno = 0;
243 char16_t out = u'\0';
244 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
245 EXPECT_EQ(u'\0', out);
246 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700247}
248
249TEST(uchar, mbrtoc16_beyond_range) {
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000250 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
251
252 errno = 0;
253 char16_t out = u'\0';
254 auto result = mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr);
255 if (kLibcRejectsOverLongUtf8Sequences) {
256 EXPECT_EQ(static_cast<size_t>(-1), result);
257 EXPECT_EQ(u'\0', out);
258 EXPECT_EQ(EILSEQ, errno);
259 } else {
260 EXPECT_EQ(4U, result);
261 EXPECT_EQ(u'\xdcc0', out);
262 EXPECT_EQ(0, errno);
263 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700264}
265
Dan Albert7a7f9952014-06-02 11:33:04 -0700266void test_mbrtoc16_incomplete(mbstate_t* ps) {
267 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
268 uselocale(LC_GLOBAL_LOCALE);
269
270 char16_t out;
271 // 2-byte UTF-8.
272 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
273 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
274 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
275 ASSERT_TRUE(mbsinit(ps));
276 // 3-byte UTF-8.
277 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
278 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
279 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
280 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
281 ASSERT_TRUE(mbsinit(ps));
282 // 4-byte UTF-8.
283 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
284 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
Dan Albert1e8e0c12023-07-19 19:14:45 +0000285#ifdef __BIONIC__
286 // https://issuetracker.google.com/289419882
287 // See explanation in mbrtoc16 test for the same bug.
Dan Albert7a7f9952014-06-02 11:33:04 -0700288 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
289 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
290 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
291 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1e8e0c12023-07-19 19:14:45 +0000292#else
293 ASSERT_EQ(1U, mbrtoc16(&out,
294 "\x8d"
295 "ef",
296 3, ps));
297 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
298 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
299 "\x80"
300 "ef",
301 3, ps));
302 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
303#endif
Dan Albert7a7f9952014-06-02 11:33:04 -0700304 ASSERT_TRUE(mbsinit(ps));
305
306 // Invalid 2-byte
307 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700308 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700309 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
310 ASSERT_EQ(EILSEQ, errno);
311}
Dan Albert7a7f9952014-06-02 11:33:04 -0700312
313TEST(uchar, mbrtoc16_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700314 mbstate_t ps;
315 memset(&ps, 0, sizeof(ps));
316
317 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700318 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700319}
320
321TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700322 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
323 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700324
325 char bytes[MB_LEN_MAX];
326
Elliott Hughes697f42a2017-07-14 17:00:05 -0700327 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700328 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700329 EXPECT_EQ('\0', bytes[0]);
330 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700331
332 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700333 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700334 EXPECT_EQ('h', bytes[0]);
335
336 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
337 uselocale(LC_GLOBAL_LOCALE);
338
339 // 1-byte UTF-8.
340 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700341 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700342 EXPECT_EQ('h', bytes[0]);
343 // 2-byte UTF-8.
344 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700345 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700346 EXPECT_EQ('\xc2', bytes[0]);
347 EXPECT_EQ('\xa2', bytes[1]);
348 // 3-byte UTF-8.
349 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700350 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700351 EXPECT_EQ('\xe2', bytes[0]);
352 EXPECT_EQ('\x82', bytes[1]);
353 EXPECT_EQ('\xac', bytes[2]);
354 // 4-byte UTF-8.
355 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700356 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700357 EXPECT_EQ('\xf0', bytes[0]);
358 EXPECT_EQ('\xa4', bytes[1]);
359 EXPECT_EQ('\xad', bytes[2]);
360 EXPECT_EQ('\xa2', bytes[3]);
361 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700362 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700363 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700364 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700365}
366
Elliott Hughes402c7622018-07-06 17:18:05 -0700367TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700368 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
369 uselocale(LC_GLOBAL_LOCALE);
370
371 char32_t out[8] = {};
372 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
373 ASSERT_EQ(0xfffeU, out[0]);
374 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
375 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700376}
377
378TEST(uchar, mbrtoc32_out_of_range) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700379 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
380 uselocale(LC_GLOBAL_LOCALE);
381
Dan Albert9fa76f12023-07-18 22:58:53 +0000382 char32_t out = U'\0';
Elliott Hughes402c7622018-07-06 17:18:05 -0700383 errno = 0;
Dan Albert9fa76f12023-07-18 22:58:53 +0000384 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000385 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert9fa76f12023-07-18 22:58:53 +0000386 EXPECT_EQ(static_cast<size_t>(-1), result);
387 EXPECT_EQ(EILSEQ, errno);
388 EXPECT_EQ(U'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000389 } else {
390 EXPECT_EQ(4U, result);
391 EXPECT_EQ(0, errno);
392 EXPECT_EQ(U'\x140000', out);
Dan Albert9fa76f12023-07-18 22:58:53 +0000393 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700394}
395
Dan Albert7a7f9952014-06-02 11:33:04 -0700396TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700397 char32_t out[8];
398
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400399 out[0] = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000400 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000401 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700402
Dan Albert9a9bbe52023-07-18 23:12:47 +0000403 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
404 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000405 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
406 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700407
Dan Albert9a9bbe52023-07-18 23:12:47 +0000408 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "hello", 0, nullptr));
409 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000410 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700411
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000412 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700413
414 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
415 uselocale(LC_GLOBAL_LOCALE);
416
417 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000418 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
419 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700420 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000421 EXPECT_EQ(2U, mbrtoc32(out,
422 "\xc2\xa2"
423 "cdef",
424 6, nullptr));
425 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700426 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000427 EXPECT_EQ(3U, mbrtoc32(out,
428 "\xe2\x82\xac"
429 "def",
430 6, nullptr));
431 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700432 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000433 EXPECT_EQ(4U, mbrtoc32(out,
434 "\xf0\xa4\xad\xa2"
435 "ef",
436 6, nullptr));
437 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700438#if defined(__BIONIC__) // glibc allows this.
439 // Illegal 5-byte UTF-8.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700440 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000441 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
442 "\xf8\xa1\xa2\xa3\xa4"
443 "f",
444 6, nullptr));
445 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700446#endif
447 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700448 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000449 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
450 "\xf0\x82\x82\xac"
451 "ef",
452 6, nullptr));
453 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700454}
455
Dan Albert7a7f9952014-06-02 11:33:04 -0700456void test_mbrtoc32_incomplete(mbstate_t* ps) {
457 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
458 uselocale(LC_GLOBAL_LOCALE);
459
460 char32_t out;
461 // 2-byte UTF-8.
462 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
463 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
464 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
465 ASSERT_TRUE(mbsinit(ps));
466 // 3-byte UTF-8.
467 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
468 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
469 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
470 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
471 ASSERT_TRUE(mbsinit(ps));
472 // 4-byte UTF-8.
473 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
474 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
475 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
476 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
477 ASSERT_TRUE(mbsinit(ps));
478
479 // Invalid 2-byte
480 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700481 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700482 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
483 ASSERT_EQ(EILSEQ, errno);
484}
Dan Albert7a7f9952014-06-02 11:33:04 -0700485
486TEST(uchar, mbrtoc32_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700487 mbstate_t ps;
488 memset(&ps, 0, sizeof(ps));
489
490 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700491 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700492}