1 /*
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
18 #include <uchar.h>
19
20 #include <gtest/gtest.h>
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <locale.h>
25 #include <stdint.h>
26
TEST(uchar,sizeof_uchar_t)27 TEST(uchar, sizeof_uchar_t) {
28 EXPECT_EQ(2U, sizeof(char16_t));
29 EXPECT_EQ(4U, sizeof(char32_t));
30 }
31
TEST(uchar,start_state)32 TEST(uchar, start_state) {
33 char out[MB_LEN_MAX];
34 mbstate_t ps;
35
36 // Any non-initial state is invalid when calling c32rtomb.
37 memset(&ps, 0, sizeof(ps));
38 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
39 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(out, 0x00a2, &ps));
40 EXPECT_EQ(EILSEQ, errno);
41
42 // If the first argument to c32rtomb is nullptr or the second is L'\0' the shift
43 // state should be reset.
44 memset(&ps, 0, sizeof(ps));
45 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xc2", 1, &ps));
46 EXPECT_EQ(1U, c32rtomb(nullptr, 0x00a2, &ps));
47 EXPECT_TRUE(mbsinit(&ps));
48
49 memset(&ps, 0, sizeof(ps));
50 EXPECT_EQ(static_cast<size_t>(-2), mbrtoc32(nullptr, "\xf0\xa4", 1, &ps));
51 EXPECT_EQ(1U, c32rtomb(out, L'\0', &ps));
52 EXPECT_TRUE(mbsinit(&ps));
53 }
54
TEST(uchar,c16rtomb_null_out)55 TEST(uchar, c16rtomb_null_out) {
56 EXPECT_EQ(1U, c16rtomb(nullptr, L'\0', nullptr));
57 EXPECT_EQ(1U, c16rtomb(nullptr, L'h', nullptr));
58 }
59
TEST(uchar,c16rtomb_null_char)60 TEST(uchar, c16rtomb_null_char) {
61 char bytes[MB_LEN_MAX];
62 EXPECT_EQ(1U, c16rtomb(bytes, L'\0', nullptr));
63 }
64
TEST(uchar,c16rtomb)65 TEST(uchar, c16rtomb) {
66 char bytes[MB_LEN_MAX];
67
68 memset(bytes, 0, sizeof(bytes));
69 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
70 EXPECT_EQ('h', bytes[0]);
71
72 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
73 uselocale(LC_GLOBAL_LOCALE);
74
75 // 1-byte UTF-8.
76 memset(bytes, 0, sizeof(bytes));
77 EXPECT_EQ(1U, c16rtomb(bytes, L'h', nullptr));
78 EXPECT_EQ('h', bytes[0]);
79 // 2-byte UTF-8.
80 memset(bytes, 0, sizeof(bytes));
81 EXPECT_EQ(2U, c16rtomb(bytes, 0x00a2, nullptr));
82 EXPECT_EQ('\xc2', bytes[0]);
83 EXPECT_EQ('\xa2', bytes[1]);
84 // 3-byte UTF-8.
85 memset(bytes, 0, sizeof(bytes));
86 EXPECT_EQ(3U, c16rtomb(bytes, 0x20ac, nullptr));
87 EXPECT_EQ('\xe2', bytes[0]);
88 EXPECT_EQ('\x82', bytes[1]);
89 EXPECT_EQ('\xac', bytes[2]);
90 }
91
TEST(uchar,c16rtomb_surrogate)92 TEST(uchar, c16rtomb_surrogate) {
93 char bytes[MB_LEN_MAX];
94
95 memset(bytes, 0, sizeof(bytes));
96 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
97 EXPECT_EQ(4U, c16rtomb(bytes, 0xdfcd, nullptr));
98 EXPECT_EQ('\xf4', bytes[0]);
99 EXPECT_EQ('\x8a', bytes[1]);
100 EXPECT_EQ('\xaf', bytes[2]);
101 EXPECT_EQ('\x8d', bytes[3]);
102 }
103
TEST(uchar,c16rtomb_invalid)104 TEST(uchar, c16rtomb_invalid) {
105 char bytes[MB_LEN_MAX];
106
107 memset(bytes, 0, sizeof(bytes));
108 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdfcd, nullptr));
109
110 EXPECT_EQ(0U, c16rtomb(bytes, 0xdbea, nullptr));
111 EXPECT_EQ(static_cast<size_t>(-1), c16rtomb(bytes, 0xdbea, nullptr));
112 }
113
TEST(uchar,mbrtoc16_null)114 TEST(uchar, mbrtoc16_null) {
115 ASSERT_EQ(0U, mbrtoc16(nullptr, nullptr, 0, nullptr));
116 }
117
TEST(uchar,mbrtoc16_zero_len)118 TEST(uchar, mbrtoc16_zero_len) {
119 char16_t out;
120
121 out = L'x';
122 ASSERT_EQ(0U, mbrtoc16(&out, "hello", 0, nullptr));
123 ASSERT_EQ(L'x', out);
124
125 ASSERT_EQ(0U, mbrtoc16(&out, "hello", 0, nullptr));
126 ASSERT_EQ(0U, mbrtoc16(&out, "", 0, nullptr));
127 ASSERT_EQ(1U, mbrtoc16(&out, "hello", 1, nullptr));
128 ASSERT_EQ(L'h', out);
129 }
130
TEST(uchar,mbrtoc16)131 TEST(uchar, mbrtoc16) {
132 char16_t out;
133
134 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
135 uselocale(LC_GLOBAL_LOCALE);
136
137 // 1-byte UTF-8.
138 ASSERT_EQ(1U, mbrtoc16(&out, "abcdef", 6, nullptr));
139 ASSERT_EQ(L'a', out);
140 // 2-byte UTF-8.
141 ASSERT_EQ(2U, mbrtoc16(&out, "\xc2\xa2" "cdef", 6, nullptr));
142 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
143 // 3-byte UTF-8.
144 ASSERT_EQ(3U, mbrtoc16(&out, "\xe2\x82\xac" "def", 6, nullptr));
145 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
146 }
147
TEST(uchar,mbrtoc16_surrogate)148 TEST(uchar, mbrtoc16_surrogate) {
149 char16_t out;
150
151 ASSERT_EQ(static_cast<size_t>(-3),
152 mbrtoc16(&out, "\xf4\x8a\xaf\x8d", 6, nullptr));
153 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
154 ASSERT_EQ(4U, mbrtoc16(&out, "\xf4\x8a\xaf\x8d" "ef", 6, nullptr));
155 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
156 }
157
TEST(uchar,mbrtoc16_reserved_range)158 TEST(uchar, mbrtoc16_reserved_range) {
159 char16_t out;
160 ASSERT_EQ(static_cast<size_t>(-1),
161 mbrtoc16(&out, "\xf0\x80\xbf\xbf", 6, nullptr));
162 }
163
TEST(uchar,mbrtoc16_beyond_range)164 TEST(uchar, mbrtoc16_beyond_range) {
165 char16_t out;
166 ASSERT_EQ(static_cast<size_t>(-1),
167 mbrtoc16(&out, "\xf5\x80\x80\x80", 6, nullptr));
168 }
169
test_mbrtoc16_incomplete(mbstate_t * ps)170 void test_mbrtoc16_incomplete(mbstate_t* ps) {
171 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
172 uselocale(LC_GLOBAL_LOCALE);
173
174 char16_t out;
175 // 2-byte UTF-8.
176 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
177 ASSERT_EQ(1U, mbrtoc16(&out, "\xa2" "cdef", 5, ps));
178 ASSERT_EQ(static_cast<char16_t>(0x00a2), out);
179 ASSERT_TRUE(mbsinit(ps));
180 // 3-byte UTF-8.
181 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xe2", 1, ps));
182 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x82", 1, ps));
183 ASSERT_EQ(1U, mbrtoc16(&out, "\xac" "def", 4, ps));
184 ASSERT_EQ(static_cast<char16_t>(0x20ac), out);
185 ASSERT_TRUE(mbsinit(ps));
186 // 4-byte UTF-8.
187 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xf4", 1, ps));
188 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\x8a\xaf", 2, ps));
189 ASSERT_EQ(static_cast<size_t>(-3), mbrtoc16(&out, "\x8d" "ef", 3, ps));
190 ASSERT_EQ(static_cast<char16_t>(0xdbea), out);
191 ASSERT_EQ(1U, mbrtoc16(&out, "\x80" "ef", 3, ps));
192 ASSERT_EQ(static_cast<char16_t>(0xdfcd), out);
193 ASSERT_TRUE(mbsinit(ps));
194
195 // Invalid 2-byte
196 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc16(&out, "\xc2", 1, ps));
197 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc16(&out, "\x20" "cdef", 5, ps));
198 ASSERT_EQ(EILSEQ, errno);
199 }
200
TEST(uchar,mbrtoc16_incomplete)201 TEST(uchar, mbrtoc16_incomplete) {
202 mbstate_t ps;
203 memset(&ps, 0, sizeof(ps));
204
205 test_mbrtoc16_incomplete(&ps);
206 test_mbrtoc16_incomplete(nullptr);
207 }
208
TEST(uchar,c32rtomb)209 TEST(uchar, c32rtomb) {
210 EXPECT_EQ(1U, c32rtomb(nullptr, L'\0', nullptr));
211 EXPECT_EQ(1U, c32rtomb(nullptr, L'h', nullptr));
212
213 char bytes[MB_LEN_MAX];
214
215 memset(bytes, 1, sizeof(bytes));
216 EXPECT_EQ(1U, c32rtomb(bytes, L'\0', nullptr));
217 EXPECT_EQ('\0', bytes[0]);
218 EXPECT_EQ('\x01', bytes[1]);
219
220 memset(bytes, 0, sizeof(bytes));
221 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
222 EXPECT_EQ('h', bytes[0]);
223
224 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
225 uselocale(LC_GLOBAL_LOCALE);
226
227 // 1-byte UTF-8.
228 memset(bytes, 0, sizeof(bytes));
229 EXPECT_EQ(1U, c32rtomb(bytes, L'h', nullptr));
230 EXPECT_EQ('h', bytes[0]);
231 // 2-byte UTF-8.
232 memset(bytes, 0, sizeof(bytes));
233 EXPECT_EQ(2U, c32rtomb(bytes, 0x00a2, nullptr));
234 EXPECT_EQ('\xc2', bytes[0]);
235 EXPECT_EQ('\xa2', bytes[1]);
236 // 3-byte UTF-8.
237 memset(bytes, 0, sizeof(bytes));
238 EXPECT_EQ(3U, c32rtomb(bytes, 0x20ac, nullptr));
239 EXPECT_EQ('\xe2', bytes[0]);
240 EXPECT_EQ('\x82', bytes[1]);
241 EXPECT_EQ('\xac', bytes[2]);
242 // 4-byte UTF-8.
243 memset(bytes, 0, sizeof(bytes));
244 EXPECT_EQ(4U, c32rtomb(bytes, 0x24b62, nullptr));
245 EXPECT_EQ('\xf0', bytes[0]);
246 EXPECT_EQ('\xa4', bytes[1]);
247 EXPECT_EQ('\xad', bytes[2]);
248 EXPECT_EQ('\xa2', bytes[3]);
249 // Invalid code point.
250 EXPECT_EQ(static_cast<size_t>(-1), c32rtomb(bytes, 0xffffffff, nullptr));
251 EXPECT_EQ(EILSEQ, errno);
252 }
253
TEST(uchar,mbrtoc32_valid_non_characters)254 TEST(uchar, mbrtoc32_valid_non_characters) {
255 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
256 uselocale(LC_GLOBAL_LOCALE);
257
258 char32_t out[8] = {};
259 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbe", 3, nullptr));
260 ASSERT_EQ(0xfffeU, out[0]);
261 ASSERT_EQ(3U, mbrtoc32(out, "\xef\xbf\xbf", 3, nullptr));
262 ASSERT_EQ(0xffffU, out[0]);
263 }
264
TEST(uchar,mbrtoc32_out_of_range)265 TEST(uchar, mbrtoc32_out_of_range) {
266 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
267 uselocale(LC_GLOBAL_LOCALE);
268
269 char32_t out[8] = {};
270 errno = 0;
271 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(out, "\xf5\x80\x80\x80", 4, nullptr));
272 ASSERT_EQ(EILSEQ, errno);
273 }
274
TEST(uchar,mbrtoc32)275 TEST(uchar, mbrtoc32) {
276 char32_t out[8];
277
278 out[0] = L'x';
279 ASSERT_EQ(0U, mbrtoc32(out, "hello", 0, nullptr));
280 ASSERT_EQ(static_cast<char32_t>(L'x'), out[0]);
281
282 ASSERT_EQ(0U, mbrtoc32(out, "hello", 0, nullptr));
283 ASSERT_EQ(0U, mbrtoc32(out, "", 0, nullptr));
284 ASSERT_EQ(1U, mbrtoc32(out, "hello", 1, nullptr));
285 ASSERT_EQ(static_cast<char32_t>(L'h'), out[0]);
286
287 ASSERT_EQ(0U, mbrtoc32(nullptr, "hello", 0, nullptr));
288 ASSERT_EQ(0U, mbrtoc32(nullptr, "", 0, nullptr));
289 ASSERT_EQ(1U, mbrtoc32(nullptr, "hello", 1, nullptr));
290
291 ASSERT_EQ(0U, mbrtoc32(nullptr, nullptr, 0, nullptr));
292
293 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
294 uselocale(LC_GLOBAL_LOCALE);
295
296 // 1-byte UTF-8.
297 ASSERT_EQ(1U, mbrtoc32(out, "abcdef", 6, nullptr));
298 ASSERT_EQ(static_cast<char32_t>(L'a'), out[0]);
299 // 2-byte UTF-8.
300 ASSERT_EQ(2U, mbrtoc32(out, "\xc2\xa2" "cdef", 6, nullptr));
301 ASSERT_EQ(static_cast<char32_t>(0x00a2), out[0]);
302 // 3-byte UTF-8.
303 ASSERT_EQ(3U, mbrtoc32(out, "\xe2\x82\xac" "def", 6, nullptr));
304 ASSERT_EQ(static_cast<char32_t>(0x20ac), out[0]);
305 // 4-byte UTF-8.
306 ASSERT_EQ(4U, mbrtoc32(out, "\xf0\xa4\xad\xa2" "ef", 6, nullptr));
307 ASSERT_EQ(static_cast<char32_t>(0x24b62), out[0]);
308 #if defined(__BIONIC__) // glibc allows this.
309 // Illegal 5-byte UTF-8.
310 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(out, "\xf8\xa1\xa2\xa3\xa4" "f", 6, nullptr));
311 ASSERT_EQ(EILSEQ, errno);
312 #endif
313 // Illegal over-long sequence.
314 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(out, "\xf0\x82\x82\xac" "ef", 6, nullptr));
315 ASSERT_EQ(EILSEQ, errno);
316 }
317
test_mbrtoc32_incomplete(mbstate_t * ps)318 void test_mbrtoc32_incomplete(mbstate_t* ps) {
319 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
320 uselocale(LC_GLOBAL_LOCALE);
321
322 char32_t out;
323 // 2-byte UTF-8.
324 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
325 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "cdef", 5, ps));
326 ASSERT_EQ(static_cast<char32_t>(0x00a2), out);
327 ASSERT_TRUE(mbsinit(ps));
328 // 3-byte UTF-8.
329 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xe2", 1, ps));
330 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\x82", 1, ps));
331 ASSERT_EQ(1U, mbrtoc32(&out, "\xac" "def", 4, ps));
332 ASSERT_EQ(static_cast<char32_t>(0x20ac), out);
333 ASSERT_TRUE(mbsinit(ps));
334 // 4-byte UTF-8.
335 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xf0", 1, ps));
336 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xa4\xad", 2, ps));
337 ASSERT_EQ(1U, mbrtoc32(&out, "\xa2" "ef", 3, ps));
338 ASSERT_EQ(static_cast<char32_t>(0x24b62), out);
339 ASSERT_TRUE(mbsinit(ps));
340
341 // Invalid 2-byte
342 ASSERT_EQ(static_cast<size_t>(-2), mbrtoc32(&out, "\xc2", 1, ps));
343 ASSERT_EQ(static_cast<size_t>(-1), mbrtoc32(&out, "\x20" "cdef", 5, ps));
344 ASSERT_EQ(EILSEQ, errno);
345 }
346
TEST(uchar,mbrtoc32_incomplete)347 TEST(uchar, mbrtoc32_incomplete) {
348 mbstate_t ps;
349 memset(&ps, 0, sizeof(ps));
350
351 test_mbrtoc32_incomplete(&ps);
352 test_mbrtoc32_incomplete(nullptr);
353 }
354