t_c8rtomb.c revision 1.2 1 /* $NetBSD: t_c8rtomb.c,v 1.2 2024/08/17 21:31:22 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2002 Tim J. Robbins
5 * All rights reserved.
6 *
7 * Copyright (c) 2013 Ed Schouten <ed (at) FreeBSD.org>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31 /*
32 * Test program for c8rtomb() as specified by C23.
33 */
34
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: t_c8rtomb.c,v 1.2 2024/08/17 21:31:22 riastradh Exp $");
37
38 #include <errno.h>
39 #include <limits.h>
40 #include <locale.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <uchar.h>
44
45 #include <atf-c.h>
46
47 static void
48 require_lc_ctype(const char *locale_name)
49 {
50 char *lc_ctype_set;
51
52 lc_ctype_set = setlocale(LC_CTYPE, locale_name);
53 if (lc_ctype_set == NULL)
54 atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
55 locale_name, errno);
56
57 ATF_REQUIRE_EQ_MSG(strcmp(lc_ctype_set, locale_name), 0,
58 "lc_ctype_set=%s locale_name=%s", lc_ctype_set, locale_name);
59 }
60
61 static mbstate_t s;
62 static char buf[MB_LEN_MAX + 1];
63
64 ATF_TC_WITHOUT_HEAD(c8rtomb_c_locale_test);
65 ATF_TC_BODY(c8rtomb_c_locale_test, tc)
66 {
67 size_t n;
68
69 require_lc_ctype("C");
70
71 /*
72 * If the buffer argument is NULL, c8 is implicitly 0,
73 * c8rtomb() resets its internal state.
74 */
75 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
76 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0x80, NULL)), 1, "n=%zu", n);
77 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xc0, NULL)), 1, "n=%zu", n);
78 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xe0, NULL)), 1, "n=%zu", n);
79 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf0, NULL)), 1, "n=%zu", n);
80 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf8, NULL)), 1, "n=%zu", n);
81 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfc, NULL)), 1, "n=%zu", n);
82 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfe, NULL)), 1, "n=%zu", n);
83 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xff, NULL)), 1, "n=%zu", n);
84
85 /* Null wide character. */
86 memset(&s, 0, sizeof(s));
87 memset(buf, 0xcc, sizeof(buf));
88 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0, &s)), 1, "n=%zu", n);
89 ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
90 (unsigned char)buf[1] == 0xcc),
91 "buf=[%02x %02x]", buf[0], buf[1]);
92
93 /* Latin letter A, internal state. */
94 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
95 ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 'A', NULL)), 1, "n=%zu", n);
96
97 /* Latin letter A. */
98 memset(&s, 0, sizeof(s));
99 memset(buf, 0xcc, sizeof(buf));
100 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), 1, "n=%zu", n);
101 ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
102 (unsigned char)buf[1] == 0xcc),
103 "buf=[%02x %02x]", buf[0], buf[1]);
104
105 /* Unicode character 'Pile of poo'. */
106 memset(&s, 0, sizeof(s));
107 memset(buf, 0xcc, sizeof(buf));
108 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
109 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
110 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
111 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), (size_t)-1,
112 "n=%zu", n);
113 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
114 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
115
116 /* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
117 memset(&s, 0, sizeof(s));
118 memset(buf, 0xcc, sizeof(buf));
119 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
120 atf_tc_expect_fail("PR lib/58615:"
121 " incomplete c8rtomb, c16rtomb handles NUL termination wrong");
122 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
123 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
124 (unsigned char)buf[1] == 0xcc),
125 "buf=[%02x %02x]", buf[0], buf[1]);
126
127 memset(&s, 0, sizeof(s));
128 memset(buf, 0xcc, sizeof(buf));
129 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
130 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
131 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
132 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
133 (unsigned char)buf[1] == 0xcc),
134 "buf=[%02x %02x]", buf[0], buf[1]);
135
136 memset(&s, 0, sizeof(s));
137 memset(buf, 0xcc, sizeof(buf));
138 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
139 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
140 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
141 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
142 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
143 (unsigned char)buf[1] == 0xcc),
144 "buf=[%02x %02x]", buf[0], buf[1]);
145 }
146
147 ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_1_test);
148 ATF_TC_BODY(c8rtomb_iso_8859_1_test, tc)
149 {
150 size_t n;
151
152 require_lc_ctype("en_US.ISO8859-1");
153
154 /* Unicode character 'Euro sign'. */
155 memset(&s, 0, sizeof(s));
156 memset(buf, 0xcc, sizeof(buf));
157 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
158 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
159 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), (size_t)-1,
160 "n=%zu", n);
161 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
162 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
163 }
164
165 ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_15_test);
166 ATF_TC_BODY(c8rtomb_iso_8859_15_test, tc)
167 {
168 size_t n;
169
170 require_lc_ctype("en_US.ISO8859-15");
171
172 /* Unicode character 'Euro sign'. */
173 memset(&s, 0, sizeof(s));
174 memset(buf, 0xcc, sizeof(buf));
175 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
176 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
177 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), 1, "n=%zu", n);
178 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
179 (unsigned char)buf[1] == 0xcc),
180 "buf=[%02x %02x]", buf[0], buf[1]);
181 }
182
183 ATF_TC_WITHOUT_HEAD(c8rtomb_utf_8_test);
184 ATF_TC_BODY(c8rtomb_utf_8_test, tc)
185 {
186 size_t n;
187
188 require_lc_ctype("en_US.UTF-8");
189
190 /* Unicode character 'Pile of poo'. */
191 memset(&s, 0, sizeof(s));
192 memset(buf, 0xcc, sizeof(buf));
193 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
194 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
195 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
196 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), 4, "n=%zu", n);
197 ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
198 (unsigned char)buf[1] == 0x9f &&
199 (unsigned char)buf[2] == 0x92 &&
200 (unsigned char)buf[3] == 0xa9 &&
201 (unsigned char)buf[4] == 0xcc),
202 "buf=[%02x %02x %02x %02x %02x]",
203 buf[0], buf[1], buf[2], buf[3], buf[4]);
204
205 /* Invalid code; 'Pile of poo' without the last byte. */
206 memset(&s, 0, sizeof(s));
207 memset(buf, 0xcc, sizeof(buf));
208 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
209 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
210 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
211 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), (size_t)-1,
212 "n=%zu", n);
213 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
214 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
215
216 /* Invalid code; 'Pile of poo' without the first byte. */
217 memset(&s, 0, sizeof(s));
218 memset(buf, 0xcc, sizeof(buf));
219 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), (size_t)-1,
220 "n=%zu", n);
221 ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
222 ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
223
224 /* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
225 memset(&s, 0, sizeof(s));
226 memset(buf, 0xcc, sizeof(buf));
227 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
228 atf_tc_expect_fail("PR lib/58615:"
229 " incomplete c8rtomb, c16rtomb handles NUL termination wrong");
230 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
231 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
232 (unsigned char)buf[1] == 0xcc),
233 "buf=[%02x %02x]", buf[0], buf[1]);
234
235 memset(&s, 0, sizeof(s));
236 memset(buf, 0xcc, sizeof(buf));
237 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
238 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
239 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
240 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
241 (unsigned char)buf[1] == 0xcc),
242 "buf=[%02x %02x]", buf[0], buf[1]);
243
244 memset(&s, 0, sizeof(s));
245 memset(buf, 0xcc, sizeof(buf));
246 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
247 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
248 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
249 ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
250 ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
251 (unsigned char)buf[1] == 0xcc),
252 "buf=[%02x %02x]", buf[0], buf[1]);
253 }
254
255 ATF_TP_ADD_TCS(tp)
256 {
257
258 ATF_TP_ADD_TC(tp, c8rtomb_c_locale_test);
259 ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_1_test);
260 ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_15_test);
261 ATF_TP_ADD_TC(tp, c8rtomb_utf_8_test);
262
263 return (atf_no_error());
264 }
265