blob: fd3b3322e20ab565e8be4fe5177091afdb419f7d [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
Elliott Hughes95646e62023-09-21 14:11:19 -070027#include "utils.h"
28
Dan Albert1252ab02023-07-14 21:23:36 +000029// Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
30// newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
31// and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
32// https://datatracker.ietf.org/doc/html/rfc2279.
33//
34// Bionic's unicode implementation was written after the high values were
35// excluded, so it has never supported them. Other implementations (at least
36// as of glibc 2.36), do support those sequences.
37#if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
Dan Albert78da2922023-07-19 18:23:46 +000038constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
Dan Albert1252ab02023-07-14 21:23:36 +000039#elif defined(__GLIBC__)
Dan Albert78da2922023-07-19 18:23:46 +000040constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
Dan Albert1252ab02023-07-14 21:23:36 +000041#else
Dan Albert78da2922023-07-19 18:23:46 +000042#error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
Dan Albert1252ab02023-07-14 21:23:36 +000043#endif
44
Dan Albert7a7f9952014-06-02 11:33:04 -070045TEST(uchar, sizeof_uchar_t) {
Dan Albert7a7f9952014-06-02 11:33:04 -070046 EXPECT_EQ(2U, sizeof(char16_t));
47 EXPECT_EQ(4U, sizeof(char32_t));
Dan Albert7a7f9952014-06-02 11:33:04 -070048}
49
50TEST(uchar, start_state) {
Dan Albert09d3b502023-07-20 22:09:30 +000051 // C23 does not appear to specify the behavior of the conversion functions if
52 // a state is reused before the character is completed. In the wchar.h section
53 // (7.31.6.3) it says:
54 //
55 // If an mbstate_t object has been altered by any of the functions
56 // described in this subclause, and is then used with a different
57 // multibyte character sequence, or in the other conversion direction, or
58 // with a different LC_CTYPE category setting than on earlier function
59 // calls, the behavior is undefined.
60 //
61 // But "described in this subclause" refers to the wchar.h functions, not the
62 // uchar.h ones.
63 //
64 // Since C has no opinion, we need to make a choice. While no caller should
65 // ever do this (what does it mean to begin decoding a UTF-32 character while
66 // still in the middle of a UTF-8 sequence?), considering that a decoding
67 // error seems the least surprising. Bionic and glibc both have that behavior.
68 // musl ignores the state (it also doesn't make much sense to read the state
69 // when the entire conversion completes in a single call) and decodes the
70 // UTF-32 character.
71#if !defined(ANDROID_HOST_MUSL)
Dan Alberta0d0e352023-07-19 22:12:59 +000072 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
73 uselocale(LC_GLOBAL_LOCALE);
74
Dan Albert7a7f9952014-06-02 11:33:04 -070075 char out[MB_LEN_MAX];
76 mbstate_t ps;
77
Dan Albert7a7f9952014-06-02 11:33:04 -070078 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));
Elliott Hughes95646e62023-09-21 14:11:19 -070082 EXPECT_ERRNO(EILSEQ);
Dan Albert7a7f9952014-06-02 11:33:04 -070083
Dan Albert09d3b502023-07-20 22:09:30 +000084 // Similarly (but not in compliance with the standard afaict), musl seems to
85 // ignore the state entirely for the UTF-32 functions rather than reset it.
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 Albert09d3b502023-07-20 22:09:30 +000098#endif
Dan Albert7a7f9952014-06-02 11:33:04 -070099}
100
101TEST(uchar, c16rtomb_null_out) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000102 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
103 uselocale(LC_GLOBAL_LOCALE);
104
Yi Kong32bc0fc2018-08-02 17:31:13 -0700105 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
106 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700107}
108
109TEST(uchar, c16rtomb_null_char) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000110 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
111 uselocale(LC_GLOBAL_LOCALE);
112
Dan Albert7a7f9952014-06-02 11:33:04 -0700113 char bytes[MB_LEN_MAX];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700114 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700115}
116
117TEST(uchar, c16rtomb) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700118 char bytes[MB_LEN_MAX];
119
120 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700122 EXPECT_EQ('h', bytes[0]);
123
124 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
125 uselocale(LC_GLOBAL_LOCALE);
126
127 // 1-byte UTF-8.
128 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700129 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700130 EXPECT_EQ('h', bytes[0]);
131 // 2-byte UTF-8.
132 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700133 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700134 EXPECT_EQ('\xc2', bytes[0]);
135 EXPECT_EQ('\xa2', bytes[1]);
136 // 3-byte UTF-8.
137 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700138 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700139 EXPECT_EQ('\xe2', bytes[0]);
140 EXPECT_EQ('\x82', bytes[1]);
141 EXPECT_EQ('\xac', bytes[2]);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700142 // 4-byte UTF-8 from a surrogate pair...
Dan Albert7a7f9952014-06-02 11:33:04 -0700143 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700144 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
145 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700146 EXPECT_EQ('\xf4', bytes[0]);
147 EXPECT_EQ('\x8a', bytes[1]);
148 EXPECT_EQ('\xaf', bytes[2]);
149 EXPECT_EQ('\x8d', bytes[3]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700150}
151
152TEST(uchar, c16rtomb_invalid) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000153 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
154 uselocale(LC_GLOBAL_LOCALE);
155
Dan Albert7a7f9952014-06-02 11:33:04 -0700156 char bytes[MB_LEN_MAX];
157
158 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700159 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700160
Yi Kong32bc0fc2018-08-02 17:31:13 -0700161 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
162 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700163}
164
165TEST(uchar, mbrtoc16_null) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000166 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
167 uselocale(LC_GLOBAL_LOCALE);
168
Yi Kong32bc0fc2018-08-02 17:31:13 -0700169 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700170}
171
172TEST(uchar, mbrtoc16_zero_len) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000173 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
174 uselocale(LC_GLOBAL_LOCALE);
175
Dan Albert7a7f9952014-06-02 11:33:04 -0700176 char16_t out;
177
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400178 out = L'x';
Dan Albert16007d52023-07-20 23:38:57 +0000179 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "hello", 0, nullptr));
Dan Albert9a9bbe52023-07-18 23:12:47 +0000180 EXPECT_EQ(L'x', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700181
Dan Albert16007d52023-07-20 23:38:57 +0000182 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "hello", 0, nullptr));
183 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "", 0, nullptr));
Dan Albert9a9bbe52023-07-18 23:12:47 +0000184 EXPECT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
185 EXPECT_EQ(L'h', out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700186}
187
188TEST(uchar, mbrtoc16) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700189 char16_t out;
190
191 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
192 uselocale(LC_GLOBAL_LOCALE);
193
194 // 1-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700195 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700196 ASSERT_EQ(L'a', out);
197 // 2-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700198 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700199 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
200 // 3-byte UTF-8.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700201 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700202 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700203 // 4-byte UTF-8 will be returned as a surrogate pair...
Dan Albert1e8e0c12023-07-19 19:14:45 +0000204 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
205 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
206 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
207 "\xf4\x8a\xaf\x8d"
208 "ef",
209 6, nullptr));
210 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1252ab02023-07-14 21:23:36 +0000211}
212
213TEST(uchar, mbrtoc16_long_sequences) {
214 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
215 uselocale(LC_GLOBAL_LOCALE);
216
217 char16_t out = u'\0';
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700218 errno = 0;
Dan Albert1252ab02023-07-14 21:23:36 +0000219 auto result = mbrtoc16(&out, "\xf8\xa1\xa2\xa3\xa4", 5, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000220 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert1252ab02023-07-14 21:23:36 +0000221 EXPECT_EQ(static_cast<size_t>(-1), result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700222 EXPECT_ERRNO(EILSEQ);
Dan Albert1252ab02023-07-14 21:23:36 +0000223 EXPECT_EQ(u'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000224 } else {
225 EXPECT_EQ(5U, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700226 EXPECT_ERRNO(0);
Dan Albert78da2922023-07-19 18:23:46 +0000227 EXPECT_EQ(u'\uf94a', out);
Dan Albert1252ab02023-07-14 21:23:36 +0000228 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700229}
230
231TEST(uchar, mbrtoc16_reserved_range) {
Dan Albertef8e1582023-07-19 19:34:12 +0000232 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Dan Alberta0d0e352023-07-19 22:12:59 +0000233 uselocale(LC_GLOBAL_LOCALE);
Dan Albertef8e1582023-07-19 19:34:12 +0000234
235 errno = 0;
236 char16_t out = u'\0';
237 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
238 EXPECT_EQ(u'\0', out);
Elliott Hughes95646e62023-09-21 14:11:19 -0700239 EXPECT_ERRNO(EILSEQ);
Dan Albert7a7f9952014-06-02 11:33:04 -0700240}
241
242TEST(uchar, mbrtoc16_beyond_range) {
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000243 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
Dan Alberta0d0e352023-07-19 22:12:59 +0000244 uselocale(LC_GLOBAL_LOCALE);
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000245
246 errno = 0;
247 char16_t out = u'\0';
248 auto result = mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr);
249 if (kLibcRejectsOverLongUtf8Sequences) {
250 EXPECT_EQ(static_cast<size_t>(-1), result);
251 EXPECT_EQ(u'\0', out);
Elliott Hughes95646e62023-09-21 14:11:19 -0700252 EXPECT_ERRNO(EILSEQ);
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000253 } else {
254 EXPECT_EQ(4U, result);
255 EXPECT_EQ(u'\xdcc0', out);
Elliott Hughes95646e62023-09-21 14:11:19 -0700256 EXPECT_ERRNO(0);
Dan Albertf5b8c7d2023-07-19 19:38:11 +0000257 }
Dan Albert7a7f9952014-06-02 11:33:04 -0700258}
259
Dan Albert7a7f9952014-06-02 11:33:04 -0700260void test_mbrtoc16_incomplete(mbstate_t* ps) {
261 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
262 uselocale(LC_GLOBAL_LOCALE);
263
264 char16_t out;
265 // 2-byte UTF-8.
266 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
267 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
268 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
269 ASSERT_TRUE(mbsinit(ps));
270 // 3-byte UTF-8.
271 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
272 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
273 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
274 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
275 ASSERT_TRUE(mbsinit(ps));
276 // 4-byte UTF-8.
277 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
278 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
Dan Albert1e8e0c12023-07-19 19:14:45 +0000279 ASSERT_EQ(1U, mbrtoc16(&out,
280 "\x8d"
281 "ef",
282 3, ps));
283 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
284 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
285 "\x80"
286 "ef",
287 3, ps));
288 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert7a7f9952014-06-02 11:33:04 -0700289 ASSERT_TRUE(mbsinit(ps));
290
291 // Invalid 2-byte
292 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700293 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700294 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700295 ASSERT_ERRNO(EILSEQ);
Dan Albert7a7f9952014-06-02 11:33:04 -0700296}
Dan Albert7a7f9952014-06-02 11:33:04 -0700297
298TEST(uchar, mbrtoc16_incomplete) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000299 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
300 uselocale(LC_GLOBAL_LOCALE);
301
Dan Albert7a7f9952014-06-02 11:33:04 -0700302 mbstate_t ps;
303 memset(&ps, 0, sizeof(ps));
304
305 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700306 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700307}
308
309TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700310 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
311 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700312
313 char bytes[MB_LEN_MAX];
314
Elliott Hughes697f42a2017-07-14 17:00:05 -0700315 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700316 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700317 EXPECT_EQ('\0', bytes[0]);
318 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700319
320 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700321 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700322 EXPECT_EQ('h', bytes[0]);
323
324 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
325 uselocale(LC_GLOBAL_LOCALE);
326
327 // 1-byte UTF-8.
328 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700329 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700330 EXPECT_EQ('h', bytes[0]);
331 // 2-byte UTF-8.
332 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700333 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700334 EXPECT_EQ('\xc2', bytes[0]);
335 EXPECT_EQ('\xa2', bytes[1]);
336 // 3-byte UTF-8.
337 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700338 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700339 EXPECT_EQ('\xe2', bytes[0]);
340 EXPECT_EQ('\x82', bytes[1]);
341 EXPECT_EQ('\xac', bytes[2]);
342 // 4-byte UTF-8.
343 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700344 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700345 EXPECT_EQ('\xf0', bytes[0]);
346 EXPECT_EQ('\xa4', bytes[1]);
347 EXPECT_EQ('\xad', bytes[2]);
348 EXPECT_EQ('\xa2', bytes[3]);
349 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700350 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700351 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700352 EXPECT_ERRNO(EILSEQ);
Dan Albert7a7f9952014-06-02 11:33:04 -0700353}
354
Elliott Hughes402c7622018-07-06 17:18:05 -0700355TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700356 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
357 uselocale(LC_GLOBAL_LOCALE);
358
359 char32_t out[8] = {};
360 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
361 ASSERT_EQ(0xfffeU, out[0]);
362 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
363 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700364}
365
366TEST(uchar, mbrtoc32_out_of_range) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700367 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
368 uselocale(LC_GLOBAL_LOCALE);
369
Dan Albert9fa76f12023-07-18 22:58:53 +0000370 char32_t out = U'\0';
Elliott Hughes402c7622018-07-06 17:18:05 -0700371 errno = 0;
Dan Albert9fa76f12023-07-18 22:58:53 +0000372 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000373 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert9fa76f12023-07-18 22:58:53 +0000374 EXPECT_EQ(static_cast<size_t>(-1), result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700375 EXPECT_ERRNO(EILSEQ);
Dan Albert9fa76f12023-07-18 22:58:53 +0000376 EXPECT_EQ(U'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000377 } else {
378 EXPECT_EQ(4U, result);
Elliott Hughes95646e62023-09-21 14:11:19 -0700379 EXPECT_ERRNO(0);
Dan Albert78da2922023-07-19 18:23:46 +0000380 EXPECT_EQ(U'\x140000', out);
Dan Albert9fa76f12023-07-18 22:58:53 +0000381 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700382}
383
Dan Albert7a7f9952014-06-02 11:33:04 -0700384TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700385 char32_t out[8];
386
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400387 out[0] = L'x';
Dan Albert16007d52023-07-20 23:38:57 +0000388 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "hello", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000389 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700390
Dan Albert16007d52023-07-20 23:38:57 +0000391 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "hello", 0, nullptr));
392 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(out, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000393 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
394 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700395
Dan Albert16007d52023-07-20 23:38:57 +0000396 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "hello", 0, nullptr));
397 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000398 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700399
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000400 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700401
402 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
403 uselocale(LC_GLOBAL_LOCALE);
404
405 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000406 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
407 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700408 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000409 EXPECT_EQ(2U, mbrtoc32(out,
410 "\xc2\xa2"
411 "cdef",
412 6, nullptr));
413 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700414 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000415 EXPECT_EQ(3U, mbrtoc32(out,
416 "\xe2\x82\xac"
417 "def",
418 6, nullptr));
419 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700420 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000421 EXPECT_EQ(4U, mbrtoc32(out,
422 "\xf0\xa4\xad\xa2"
423 "ef",
424 6, nullptr));
425 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700426#if defined(__BIONIC__) // glibc allows this.
427 // Illegal 5-byte UTF-8.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700428 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000429 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
430 "\xf8\xa1\xa2\xa3\xa4"
431 "f",
432 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700433 EXPECT_ERRNO(EILSEQ);
Dan Albert7a7f9952014-06-02 11:33:04 -0700434#endif
435 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700436 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000437 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
438 "\xf0\x82\x82\xac"
439 "ef",
440 6, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700441 EXPECT_ERRNO(EILSEQ);
Dan Albert7a7f9952014-06-02 11:33:04 -0700442}
443
Dan Albert7a7f9952014-06-02 11:33:04 -0700444void test_mbrtoc32_incomplete(mbstate_t* ps) {
445 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
446 uselocale(LC_GLOBAL_LOCALE);
447
448 char32_t out;
449 // 2-byte UTF-8.
450 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
451 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
452 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
453 ASSERT_TRUE(mbsinit(ps));
454 // 3-byte UTF-8.
455 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
456 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
457 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
458 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
459 ASSERT_TRUE(mbsinit(ps));
460 // 4-byte UTF-8.
461 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
462 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
463 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
464 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
465 ASSERT_TRUE(mbsinit(ps));
466
467 // Invalid 2-byte
468 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700469 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700470 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
Elliott Hughes95646e62023-09-21 14:11:19 -0700471 ASSERT_ERRNO(EILSEQ);
Dan Albert7a7f9952014-06-02 11:33:04 -0700472}
Dan Albert7a7f9952014-06-02 11:33:04 -0700473
474TEST(uchar, mbrtoc32_incomplete) {
Dan Alberta0d0e352023-07-19 22:12:59 +0000475 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
476 uselocale(LC_GLOBAL_LOCALE);
477
Dan Albert7a7f9952014-06-02 11:33:04 -0700478 mbstate_t ps;
479 memset(&ps, 0, sizeof(ps));
480
481 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700482 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700483}