blob: 0114adfd57f5512dedda62fb30f71b66fb7bf35c [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 Albert7a7f9952014-06-02 11:33:04 -0700250 char16_t out;
251 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700252 mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700253}
254
Dan Albert7a7f9952014-06-02 11:33:04 -0700255void test_mbrtoc16_incomplete(mbstate_t* ps) {
256 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
257 uselocale(LC_GLOBAL_LOCALE);
258
259 char16_t out;
260 // 2-byte UTF-8.
261 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
262 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
263 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
264 ASSERT_TRUE(mbsinit(ps));
265 // 3-byte UTF-8.
266 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
267 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
268 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
269 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
270 ASSERT_TRUE(mbsinit(ps));
271 // 4-byte UTF-8.
272 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
273 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
Dan Albert1e8e0c12023-07-19 19:14:45 +0000274#ifdef __BIONIC__
275 // https://issuetracker.google.com/289419882
276 // See explanation in mbrtoc16 test for the same bug.
Dan Albert7a7f9952014-06-02 11:33:04 -0700277 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
278 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
279 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
280 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1e8e0c12023-07-19 19:14:45 +0000281#else
282 ASSERT_EQ(1U, mbrtoc16(&out,
283 "\x8d"
284 "ef",
285 3, ps));
286 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
287 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
288 "\x80"
289 "ef",
290 3, ps));
291 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
292#endif
Dan Albert7a7f9952014-06-02 11:33:04 -0700293 ASSERT_TRUE(mbsinit(ps));
294
295 // Invalid 2-byte
296 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700297 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700298 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
299 ASSERT_EQ(EILSEQ, errno);
300}
Dan Albert7a7f9952014-06-02 11:33:04 -0700301
302TEST(uchar, mbrtoc16_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700303 mbstate_t ps;
304 memset(&ps, 0, sizeof(ps));
305
306 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700307 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700308}
309
310TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700311 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
312 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700313
314 char bytes[MB_LEN_MAX];
315
Elliott Hughes697f42a2017-07-14 17:00:05 -0700316 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700317 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700318 EXPECT_EQ('\0', bytes[0]);
319 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700320
321 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700322 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700323 EXPECT_EQ('h', bytes[0]);
324
325 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
326 uselocale(LC_GLOBAL_LOCALE);
327
328 // 1-byte UTF-8.
329 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700330 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700331 EXPECT_EQ('h', bytes[0]);
332 // 2-byte UTF-8.
333 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700334 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700335 EXPECT_EQ('\xc2', bytes[0]);
336 EXPECT_EQ('\xa2', bytes[1]);
337 // 3-byte UTF-8.
338 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700339 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700340 EXPECT_EQ('\xe2', bytes[0]);
341 EXPECT_EQ('\x82', bytes[1]);
342 EXPECT_EQ('\xac', bytes[2]);
343 // 4-byte UTF-8.
344 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700345 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700346 EXPECT_EQ('\xf0', bytes[0]);
347 EXPECT_EQ('\xa4', bytes[1]);
348 EXPECT_EQ('\xad', bytes[2]);
349 EXPECT_EQ('\xa2', bytes[3]);
350 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700351 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700352 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700353 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700354}
355
Elliott Hughes402c7622018-07-06 17:18:05 -0700356TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700357 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
358 uselocale(LC_GLOBAL_LOCALE);
359
360 char32_t out[8] = {};
361 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
362 ASSERT_EQ(0xfffeU, out[0]);
363 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
364 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700365}
366
367TEST(uchar, mbrtoc32_out_of_range) {
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
Dan Albert9fa76f12023-07-18 22:58:53 +0000371 char32_t out = U'\0';
Elliott Hughes402c7622018-07-06 17:18:05 -0700372 errno = 0;
Dan Albert9fa76f12023-07-18 22:58:53 +0000373 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000374 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert9fa76f12023-07-18 22:58:53 +0000375 EXPECT_EQ(static_cast<size_t>(-1), result);
376 EXPECT_EQ(EILSEQ, errno);
377 EXPECT_EQ(U'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000378 } else {
379 EXPECT_EQ(4U, result);
380 EXPECT_EQ(0, errno);
381 EXPECT_EQ(U'\x140000', out);
Dan Albert9fa76f12023-07-18 22:58:53 +0000382 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700383}
384
Dan Albert7a7f9952014-06-02 11:33:04 -0700385TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700386 char32_t out[8];
387
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400388 out[0] = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000389 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000390 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700391
Dan Albert9a9bbe52023-07-18 23:12:47 +0000392 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
393 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000394 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
395 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700396
Dan Albert9a9bbe52023-07-18 23:12:47 +0000397 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "hello", 0, nullptr));
398 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000399 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700400
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000401 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700402
403 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
404 uselocale(LC_GLOBAL_LOCALE);
405
406 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000407 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
408 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700409 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000410 EXPECT_EQ(2U, mbrtoc32(out,
411 "\xc2\xa2"
412 "cdef",
413 6, nullptr));
414 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700415 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000416 EXPECT_EQ(3U, mbrtoc32(out,
417 "\xe2\x82\xac"
418 "def",
419 6, nullptr));
420 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700421 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000422 EXPECT_EQ(4U, mbrtoc32(out,
423 "\xf0\xa4\xad\xa2"
424 "ef",
425 6, nullptr));
426 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700427#if defined(__BIONIC__) // glibc allows this.
428 // Illegal 5-byte UTF-8.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700429 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000430 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
431 "\xf8\xa1\xa2\xa3\xa4"
432 "f",
433 6, nullptr));
434 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700435#endif
436 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700437 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000438 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
439 "\xf0\x82\x82\xac"
440 "ef",
441 6, nullptr));
442 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700443}
444
Dan Albert7a7f9952014-06-02 11:33:04 -0700445void test_mbrtoc32_incomplete(mbstate_t* ps) {
446 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
447 uselocale(LC_GLOBAL_LOCALE);
448
449 char32_t out;
450 // 2-byte UTF-8.
451 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
452 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
453 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
454 ASSERT_TRUE(mbsinit(ps));
455 // 3-byte UTF-8.
456 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
457 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
458 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
459 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
460 ASSERT_TRUE(mbsinit(ps));
461 // 4-byte UTF-8.
462 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
463 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
464 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
465 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
466 ASSERT_TRUE(mbsinit(ps));
467
468 // Invalid 2-byte
469 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700470 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700471 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
472 ASSERT_EQ(EILSEQ, errno);
473}
Dan Albert7a7f9952014-06-02 11:33:04 -0700474
475TEST(uchar, mbrtoc32_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700476 mbstate_t ps;
477 memset(&ps, 0, sizeof(ps));
478
479 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700480 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700481}