blob: d54b015fb741a4ac0fc79e487d6706cb39da7996 [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 Albert7a7f9952014-06-02 11:33:04 -0700240 char16_t out;
241 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700242 mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700243}
244
245TEST(uchar, mbrtoc16_beyond_range) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700246 char16_t out;
247 ASSERT_EQ(static_cast<size_t>(-1),
Yi Kong32bc0fc2018-08-02 17:31:13 -0700248 mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700249}
250
Dan Albert7a7f9952014-06-02 11:33:04 -0700251void test_mbrtoc16_incomplete(mbstate_t* ps) {
252 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
253 uselocale(LC_GLOBAL_LOCALE);
254
255 char16_t out;
256 // 2-byte UTF-8.
257 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
258 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
259 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
260 ASSERT_TRUE(mbsinit(ps));
261 // 3-byte UTF-8.
262 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
263 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
264 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
265 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
266 ASSERT_TRUE(mbsinit(ps));
267 // 4-byte UTF-8.
268 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
269 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
Dan Albert1e8e0c12023-07-19 19:14:45 +0000270#ifdef __BIONIC__
271 // https://issuetracker.google.com/289419882
272 // See explanation in mbrtoc16 test for the same bug.
Dan Albert7a7f9952014-06-02 11:33:04 -0700273 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
274 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
275 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
276 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
Dan Albert1e8e0c12023-07-19 19:14:45 +0000277#else
278 ASSERT_EQ(1U, mbrtoc16(&out,
279 "\x8d"
280 "ef",
281 3, ps));
282 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
283 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out,
284 "\x80"
285 "ef",
286 3, ps));
287 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
288#endif
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));
295 ASSERT_EQ(EILSEQ, errno);
296}
Dan Albert7a7f9952014-06-02 11:33:04 -0700297
298TEST(uchar, mbrtoc16_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700299 mbstate_t ps;
300 memset(&ps, 0, sizeof(ps));
301
302 test_mbrtoc16_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700303 test_mbrtoc16_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700304}
305
306TEST(uchar, c32rtomb) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700307 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
308 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700309
310 char bytes[MB_LEN_MAX];
311
Elliott Hughes697f42a2017-07-14 17:00:05 -0700312 memset(bytes, 1, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700313 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
Elliott Hughes697f42a2017-07-14 17:00:05 -0700314 EXPECT_EQ('\0', bytes[0]);
315 EXPECT_EQ('\x01', bytes[1]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700316
317 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700318 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700319 EXPECT_EQ('h', bytes[0]);
320
321 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
322 uselocale(LC_GLOBAL_LOCALE);
323
324 // 1-byte UTF-8.
325 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700326 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700327 EXPECT_EQ('h', bytes[0]);
328 // 2-byte UTF-8.
329 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700330 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700331 EXPECT_EQ('\xc2', bytes[0]);
332 EXPECT_EQ('\xa2', bytes[1]);
333 // 3-byte UTF-8.
334 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700335 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700336 EXPECT_EQ('\xe2', bytes[0]);
337 EXPECT_EQ('\x82', bytes[1]);
338 EXPECT_EQ('\xac', bytes[2]);
339 // 4-byte UTF-8.
340 memset(bytes, 0, sizeof(bytes));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700341 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700342 EXPECT_EQ('\xf0', bytes[0]);
343 EXPECT_EQ('\xa4', bytes[1]);
344 EXPECT_EQ('\xad', bytes[2]);
345 EXPECT_EQ('\xa2', bytes[3]);
346 // Invalid code point.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700347 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700348 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700349 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700350}
351
Elliott Hughes402c7622018-07-06 17:18:05 -0700352TEST(uchar, mbrtoc32_valid_non_characters) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700353 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
354 uselocale(LC_GLOBAL_LOCALE);
355
356 char32_t out[8] = {};
357 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
358 ASSERT_EQ(0xfffeU, out[0]);
359 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
360 ASSERT_EQ(0xffffU, out[0]);
Elliott Hughes402c7622018-07-06 17:18:05 -0700361}
362
363TEST(uchar, mbrtoc32_out_of_range) {
Elliott Hughes402c7622018-07-06 17:18:05 -0700364 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
365 uselocale(LC_GLOBAL_LOCALE);
366
Dan Albert9fa76f12023-07-18 22:58:53 +0000367 char32_t out = U'\0';
Elliott Hughes402c7622018-07-06 17:18:05 -0700368 errno = 0;
Dan Albert9fa76f12023-07-18 22:58:53 +0000369 auto result = mbrtoc32(&out, "\xf5\x80\x80\x80", 4, nullptr);
Dan Albert78da2922023-07-19 18:23:46 +0000370 if (kLibcRejectsOverLongUtf8Sequences) {
Dan Albert9fa76f12023-07-18 22:58:53 +0000371 EXPECT_EQ(static_cast<size_t>(-1), result);
372 EXPECT_EQ(EILSEQ, errno);
373 EXPECT_EQ(U'\0', out);
Dan Albert78da2922023-07-19 18:23:46 +0000374 } else {
375 EXPECT_EQ(4U, result);
376 EXPECT_EQ(0, errno);
377 EXPECT_EQ(U'\x140000', out);
Dan Albert9fa76f12023-07-18 22:58:53 +0000378 }
Elliott Hughes402c7622018-07-06 17:18:05 -0700379}
380
Dan Albert7a7f9952014-06-02 11:33:04 -0700381TEST(uchar, mbrtoc32) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700382 char32_t out[8];
383
Alexander Ivchenko68b01662014-06-11 16:20:54 +0400384 out[0] = L'x';
Dan Albert9a9bbe52023-07-18 23:12:47 +0000385 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000386 EXPECT_EQ(static_cast<char32_t>(L'x'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700387
Dan Albert9a9bbe52023-07-18 23:12:47 +0000388 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "hello", 0, nullptr));
389 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(out, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000390 EXPECT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
391 EXPECT_EQ(static_cast<char32_t>(L'h'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700392
Dan Albert9a9bbe52023-07-18 23:12:47 +0000393 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "hello", 0, nullptr));
394 EXPECT_EQ(kExpectedResultForZeroLength, mbrtoc32(nullptr, "", 0, nullptr));
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000395 EXPECT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700396
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000397 EXPECT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
Dan Albert7a7f9952014-06-02 11:33:04 -0700398
399 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
400 uselocale(LC_GLOBAL_LOCALE);
401
402 // 1-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000403 EXPECT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
404 EXPECT_EQ(static_cast<char32_t>(L'a'), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700405 // 2-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000406 EXPECT_EQ(2U, mbrtoc32(out,
407 "\xc2\xa2"
408 "cdef",
409 6, nullptr));
410 EXPECT_EQ(static_cast<char32_t>(0x00a2), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700411 // 3-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000412 EXPECT_EQ(3U, mbrtoc32(out,
413 "\xe2\x82\xac"
414 "def",
415 6, nullptr));
416 EXPECT_EQ(static_cast<char32_t>(0x20ac), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700417 // 4-byte UTF-8.
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000418 EXPECT_EQ(4U, mbrtoc32(out,
419 "\xf0\xa4\xad\xa2"
420 "ef",
421 6, nullptr));
422 EXPECT_EQ(static_cast<char32_t>(0x24b62), out[0]);
Dan Albert7a7f9952014-06-02 11:33:04 -0700423#if defined(__BIONIC__) // glibc allows this.
424 // Illegal 5-byte UTF-8.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700425 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000426 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
427 "\xf8\xa1\xa2\xa3\xa4"
428 "f",
429 6, nullptr));
430 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700431#endif
432 // Illegal over-long sequence.
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700433 errno = 0;
Dan Albert0b8fbdf2023-07-18 22:21:36 +0000434 EXPECT_EQ(static_cast<size_t>(-1), mbrtoc32(out,
435 "\xf0\x82\x82\xac"
436 "ef",
437 6, nullptr));
438 EXPECT_EQ(EILSEQ, errno);
Dan Albert7a7f9952014-06-02 11:33:04 -0700439}
440
Dan Albert7a7f9952014-06-02 11:33:04 -0700441void test_mbrtoc32_incomplete(mbstate_t* ps) {
442 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
443 uselocale(LC_GLOBAL_LOCALE);
444
445 char32_t out;
446 // 2-byte UTF-8.
447 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
448 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
449 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
450 ASSERT_TRUE(mbsinit(ps));
451 // 3-byte UTF-8.
452 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
453 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
454 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
455 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
456 ASSERT_TRUE(mbsinit(ps));
457 // 4-byte UTF-8.
458 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
459 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
460 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
461 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
462 ASSERT_TRUE(mbsinit(ps));
463
464 // Invalid 2-byte
465 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
Elliott Hughes3d8156d2021-11-05 17:47:07 -0700466 errno = 0;
Dan Albert7a7f9952014-06-02 11:33:04 -0700467 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
468 ASSERT_EQ(EILSEQ, errno);
469}
Dan Albert7a7f9952014-06-02 11:33:04 -0700470
471TEST(uchar, mbrtoc32_incomplete) {
Dan Albert7a7f9952014-06-02 11:33:04 -0700472 mbstate_t ps;
473 memset(&ps, 0, sizeof(ps));
474
475 test_mbrtoc32_incomplete(&ps);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700476 test_mbrtoc32_incomplete(nullptr);
Dan Albert7a7f9952014-06-02 11:33:04 -0700477}