blob: 9c2d21902e61aaa57ee49555e56409277745eb86 [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 Alberta0d0e352023-07-19 22:12:59 +000074 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
75 uselocale(LC_GLOBAL_LOCALE);
76
Dan Albert7a7f9952014-06-02 11:33:04 -070077 char out[MB_LEN_MAX];
78 mbstate_t ps;
79
80 // Any non-initial state is invalid when calling c32rtomb.
81 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070082 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -070083 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -070084 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
85 EXPECT_EQ(EILSEQ, errno);
86
Yi Kong32bc0fc2018-08-02 17:31:13 -070087 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
Dan Albert7a7f9952014-06-02 11:33:04 -070088 // state should be reset.
89 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070090 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
91 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070092 EXPECT_TRUE(mbsinit(&ps));
93
94 memset(&ps, 0, sizeof(ps));
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070096 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
97 EXPECT_TRUE(mbsinit(&ps));
Dan Albert7a7f9952014-06-02 11:33:04 -070098}
99
100TEST(uchar, c16rtomb_null_out) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000101 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
102 uselocale(LC_GLOBAL_LOCALE);
103
Yi Kong32bc0fc2018-08-02 17:31:13 -0700104 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
105 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700106}
107
108TEST(uchar, c16rtomb_null_char) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000109 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
110 uselocale(LC_GLOBAL_LOCALE);
111
Dan Albert7a7f9952014-06-02 11:33:04 -0700112 char bytes[MB_LEN_MAX];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700113 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700114}
115
116TEST(uchar, c16rtomb) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700117 char bytes[MB_LEN_MAX];
118
119 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700120 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700121 EXPECT_EQ('h', bytes[0]);
122
123 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
124 uselocale(LC_GLOBAL_LOCALE);
125
126 // 1-byte UTF-8.
127 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700128 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700129 EXPECT_EQ('h', bytes[0]);
130 // 2-byte UTF-8.
131 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700132 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700133 EXPECT_EQ('\xc2', bytes[0]);
134 EXPECT_EQ('\xa2', bytes[1]);
135 // 3-byte UTF-8.
136 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700137 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700138 EXPECT_EQ('\xe2', bytes[0]);
139 EXPECT_EQ('\x82', bytes[1]);
140 EXPECT_EQ('\xac', bytes[2]);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700141 // 4-byte UTF-8 from a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700142 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700143 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
144 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700145 EXPECT_EQ('\xf4', bytes[0]);
146 EXPECT_EQ('\x8a', bytes[1]);
147 EXPECT_EQ('\xaf', bytes[2]);
148 EXPECT_EQ('\x8d', bytes[3]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700149}
150
151TEST(uchar, c16rtomb_invalid) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000152 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
153 uselocale(LC_GLOBAL_LOCALE);
154
Dan Albert7a7f9952014-06-02 11:33:04 -0700155 char bytes[MB_LEN_MAX];
156
157 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700158 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700159
Yi Kong32bc0fc2018-08-02 17:31:13 -0700160 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
161 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700162}
163
164TEST(uchar, mbrtoc16_null) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000165 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
166 uselocale(LC_GLOBAL_LOCALE);
167
Yi Kong32bc0fc2018-08-02 17:31:13 -0700168 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700169}
170
171TEST(uchar, mbrtoc16_zero_len) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000172 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
173 uselocale(LC_GLOBAL_LOCALE);
174
Dan Albert7a7f9952014-06-02 11:33:04 -0700175 char16_t out;
176
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400177 out = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000178 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "hello", 0, nullptr));
179 EXPECT_EQ(L'x', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700180
Dan Albert9a9bbe52023-07-18 23:12:47 +0000181 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "hello", 0, nullptr));
182 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc16(&out, "", 0, nullptr));
183 EXPECT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
184 EXPECT_EQ(L'h', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700185}
186
187TEST(uchar, mbrtoc16) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700188 char16_t out;
189
190 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
191 uselocale(LC_GLOBAL_LOCALE);
192
193 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700195 ASSERT_EQ(L'a', out);
196 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700197 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700198 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
199 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700200 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700201 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700202 // 4-byte UTF-8 will be returned as a surrogate pair...
Dan Albert1e8e0c12023-07-19 19:14:45 +0000203#ifdef __BIONIC__
204 // https://issuetracker.google.com/289419882
205 //
206 // We misread the spec when implementing this. The first call should return
207 // the length of the decoded character, and the second call should return -3
208 // to indicate that the output is a continuation of the character decoded by
209 // the first call.
210 //
211 // C23 7.30.1.3.4:
212 //
213 // between 1 and n inclusive if the next n or fewer bytes complete a valid
214 // multibyte character (which is the value stored); the value returned is
215 // the number of bytes that complete the multibyte character.
216 //
217 // (size_t)(-3) if the next character resulting from a previous call has
218 // been stored (no bytes from the input have been consumed by this call).
219 //
220 // Leaving the test for the wrong outputs here while we clean up and improve
221 // the rest of the tests to get a better handle on the behavior differences
222 // before fixing the bug.
Dan Albert7a7f9952014-06-02 11:33:04 -0700223 ASSERT_EQ(static_cast<size_t>(-3),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700224 mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700225 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700226 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d" "ef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700227 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1e8e0c12023-07-19 19:14:45 +0000228#else
229 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
230 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
231 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
232 "\xf4\x8a\xaf\x8d"
233 "ef",
234 6, nullptr));
235 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
236#endif
Dan Albert1252ab02023-07-14 21:23:36 +0000237}
238
239TEST(uchar, mbrtoc16_long_sequences) {
240 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
241 uselocale(LC_GLOBAL_LOCALE);
242
243 char16_t out = u'\0';
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700244 errno = 0;
Dan Albert1252ab02023-07-14 21:23:36 +0000245 auto result = mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000246 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert1252ab02023-07-14 21:23:36 +0000247 EXPECT_EQ(static_cast<size_t>(-1), result);
248 EXPECT_EQ(EILSEQ, errno);
249 EXPECT_EQ(u'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000250 } else {
251 EXPECT_EQ(5U, result);
252 EXPECT_EQ(0, errno);
253 EXPECT_EQ(u'\uf94a', out);
Dan Albert1252ab02023-07-14 21:23:36 +0000254 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700255}
256
257TEST(uchar, mbrtoc16_reserved_range) {
Dan Albertef8e1582023-07-19 19:34:12 +0000258 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Dan Alberta0d0e352023-07-19 22:12:59 +0000259 uselocale(LC_GLOBAL_LOCALE);
Dan Albertef8e1582023-07-19 19:34:12 +0000260
261 errno = 0;
262 char16_t out = u'\0';
263 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
264 EXPECT_EQ(u'\0', out);
265 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700266}
267
268TEST(uchar, mbrtoc16_beyond_range) {
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000269 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Dan Alberta0d0e352023-07-19 22:12:59 +0000270 uselocale(LC_GLOBAL_LOCALE);
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000271
272 errno = 0;
273 char16_t out = u'\0';
274 auto result = mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr);
275 if (kLibcRejectsOverLongUtf8Sequences) {
276 EXPECT_EQ(static_cast<size_t>(-1), result);
277 EXPECT_EQ(u'\0', out);
278 EXPECT_EQ(EILSEQ, errno);
279 } else {
280 EXPECT_EQ(4U, result);
281 EXPECT_EQ(u'\xdcc0', out);
282 EXPECT_EQ(0, errno);
283 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700284}
285
Dan Albert7a7f9952014-06-02 11:33:04 -0700286void test_mbrtoc16_incomplete(mbstate_t* ps) {
287 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
288 uselocale(LC_GLOBAL_LOCALE);
289
290 char16_t out;
291 // 2-byte UTF-8.
292 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
293 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
294 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
295 ASSERT_TRUE(mbsinit(ps));
296 // 3-byte UTF-8.
297 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
298 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
299 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
300 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
301 ASSERT_TRUE(mbsinit(ps));
302 // 4-byte UTF-8.
303 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
304 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
Dan Albert1e8e0c12023-07-19 19:14:45 +0000305#ifdef __BIONIC__
306 // https://issuetracker.google.com/289419882
307 // See explanation in mbrtoc16 test for the same bug.
Dan Albert7a7f9952014-06-02 11:33:04 -0700308 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
309 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
310 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
311 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1e8e0c12023-07-19 19:14:45 +0000312#else
313 ASSERT_EQ(1U, mbrtoc16(&out,
314 "\x8d"
315 "ef",
316 3, ps));
317 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
318 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
319 "\x80"
320 "ef",
321 3, ps));
322 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
323#endif
Dan Albert7a7f9952014-06-02 11:33:04 -0700324 ASSERT_TRUE(mbsinit(ps));
325
326 // Invalid 2-byte
327 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700328 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700329 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
330 ASSERT_EQ(EILSEQ, errno);
331}
Dan Albert7a7f9952014-06-02 11:33:04 -0700332
333TEST(uchar, mbrtoc16_incomplete) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000334 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
335 uselocale(LC_GLOBAL_LOCALE);
336
Dan Albert7a7f9952014-06-02 11:33:04 -0700337 mbstate_t ps;
338 memset(&ps, 0, sizeof(ps));
339
340 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700341 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700342}
343
344TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700345 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
346 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700347
348 char bytes[MB_LEN_MAX];
349
Elliott Hughes697f42a2017-07-14 17:00:05 -0700350 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700351 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700352 EXPECT_EQ('\0', bytes[0]);
353 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700354
355 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700356 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700357 EXPECT_EQ('h', bytes[0]);
358
359 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
360 uselocale(LC_GLOBAL_LOCALE);
361
362 // 1-byte UTF-8.
363 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700364 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700365 EXPECT_EQ('h', bytes[0]);
366 // 2-byte UTF-8.
367 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700368 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700369 EXPECT_EQ('\xc2', bytes[0]);
370 EXPECT_EQ('\xa2', bytes[1]);
371 // 3-byte UTF-8.
372 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700373 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700374 EXPECT_EQ('\xe2', bytes[0]);
375 EXPECT_EQ('\x82', bytes[1]);
376 EXPECT_EQ('\xac', bytes[2]);
377 // 4-byte UTF-8.
378 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700379 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700380 EXPECT_EQ('\xf0', bytes[0]);
381 EXPECT_EQ('\xa4', bytes[1]);
382 EXPECT_EQ('\xad', bytes[2]);
383 EXPECT_EQ('\xa2', bytes[3]);
384 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700385 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700386 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700387 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700388}
389
Elliott Hughes402c7622018-07-06 17:18:05 -0700390TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700391 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
392 uselocale(LC_GLOBAL_LOCALE);
393
394 char32_t out[8] = {};
395 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
396 ASSERT_EQ(0xfffeU, out[0]);
397 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
398 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700399}
400
401TEST(uchar, mbrtoc32_out_of_range) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700402 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
403 uselocale(LC_GLOBAL_LOCALE);
404
Dan Albert9fa76f12023-07-18 22:58:53 +0000405 char32_t out = U'\0';
Elliott Hughes402c7622018-07-06 17:18:05 -0700406 errno = 0;
Dan Albert9fa76f12023-07-18 22:58:53 +0000407 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000408 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert9fa76f12023-07-18 22:58:53 +0000409 EXPECT_EQ(static_cast<size_t>(-1), result);
410 EXPECT_EQ(EILSEQ, errno);
411 EXPECT_EQ(U'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000412 } else {
413 EXPECT_EQ(4U, result);
414 EXPECT_EQ(0, errno);
415 EXPECT_EQ(U'\x140000', out);
Dan Albert9fa76f12023-07-18 22:58:53 +0000416 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700417}
418
Dan Albert7a7f9952014-06-02 11:33:04 -0700419TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700420 char32_t out[8];
421
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400422 out[0] = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000423 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000424 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700425
Dan Albert9a9bbe52023-07-18 23:12:47 +0000426 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
427 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000428 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
429 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700430
Dan Albert9a9bbe52023-07-18 23:12:47 +0000431 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "hello", 0, nullptr));
432 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000433 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700434
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000435 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700436
437 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
438 uselocale(LC_GLOBAL_LOCALE);
439
440 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000441 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
442 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700443 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000444 EXPECT_EQ(2U, mbrtoc32(out,
445 "\xc2\xa2"
446 "cdef",
447 6, nullptr));
448 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700449 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000450 EXPECT_EQ(3U, mbrtoc32(out,
451 "\xe2\x82\xac"
452 "def",
453 6, nullptr));
454 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700455 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000456 EXPECT_EQ(4U, mbrtoc32(out,
457 "\xf0\xa4\xad\xa2"
458 "ef",
459 6, nullptr));
460 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700461#if defined(__BIONIC__) // glibc allows this.
462 // Illegal 5-byte UTF-8.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700463 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000464 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
465 "\xf8\xa1\xa2\xa3\xa4"
466 "f",
467 6, nullptr));
468 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700469#endif
470 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700471 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000472 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
473 "\xf0\x82\x82\xac"
474 "ef",
475 6, nullptr));
476 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700477}
478
Dan Albert7a7f9952014-06-02 11:33:04 -0700479void test_mbrtoc32_incomplete(mbstate_t* ps) {
480 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
481 uselocale(LC_GLOBAL_LOCALE);
482
483 char32_t out;
484 // 2-byte UTF-8.
485 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
486 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
487 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
488 ASSERT_TRUE(mbsinit(ps));
489 // 3-byte UTF-8.
490 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
491 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
492 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
493 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
494 ASSERT_TRUE(mbsinit(ps));
495 // 4-byte UTF-8.
496 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
497 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
498 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
499 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
500 ASSERT_TRUE(mbsinit(ps));
501
502 // Invalid 2-byte
503 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700504 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700505 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
506 ASSERT_EQ(EILSEQ, errno);
507}
Dan Albert7a7f9952014-06-02 11:33:04 -0700508
509TEST(uchar, mbrtoc32_incomplete) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000510 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
511 uselocale(LC_GLOBAL_LOCALE);
512
Dan Albert7a7f9952014-06-02 11:33:04 -0700513 mbstate_t ps;
514 memset(&ps, 0, sizeof(ps));
515
516 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700517 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700518}