xref: /freebsd/lib/libc/tests/locale/wcsnrtombs_test.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1 /*-
2  * Copyright (c) 2002-2004 Tim J. Robbins
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Test program for wcsnrtombs().
29  *
30  * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
31  * "ja_JP.eucJP". Other encodings are not tested.
32  */
33 
34 #include <errno.h>
35 #include <limits.h>
36 #include <locale.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <wchar.h>
41 
42 #include <atf-c.h>
43 
44 ATF_TC_WITHOUT_HEAD(wcsnrtombs_test);
45 ATF_TC_BODY(wcsnrtombs_test, tc)
46 {
47 	wchar_t srcbuf[128];
48 	char dstbuf[128];
49 	wchar_t *src;
50 	mbstate_t s;
51 
52 	/* C/POSIX locale. */
53 
54 	/* Simple null terminated string. */
55 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
56 	wcscpy(srcbuf, L"hello");
57 	memset(dstbuf, 0xcc, sizeof(dstbuf));
58 	src = srcbuf;
59 	memset(&s, 0, sizeof(s));
60 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, sizeof(dstbuf),
61 	    &s) == 5);
62 	ATF_REQUIRE(strcmp(dstbuf, "hello") == 0);
63 	ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc);
64 	ATF_REQUIRE(src == NULL);
65 
66 	/* Simple null terminated string, stopping early. */
67 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
68 	wcscpy(srcbuf, L"hello");
69 	memset(dstbuf, 0xcc, sizeof(dstbuf));
70 	src = srcbuf;
71 	memset(&s, 0, sizeof(s));
72 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 4, sizeof(dstbuf),
73 	    &s) == 4);
74 	ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0);
75 	ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc);
76 	ATF_REQUIRE(src == srcbuf + 4);
77 
78 	/* Not enough space in destination buffer. */
79 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
80 	wcscpy(srcbuf, L"hello");
81 	memset(dstbuf, 0xcc, sizeof(dstbuf));
82 	src = srcbuf;
83 	memset(&s, 0, sizeof(s));
84 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, 4,
85 	    &s) == 4);
86 	ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0);
87 	ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc);
88 	ATF_REQUIRE(src == srcbuf + 4);
89 
90 	/* Null terminated string, internal dest. buffer */
91 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
92 	wcscpy(srcbuf, L"hello");
93 	src = srcbuf;
94 	memset(&s, 0, sizeof(s));
95 	ATF_REQUIRE(wcsnrtombs(NULL, (const wchar_t **)&src, 6, sizeof(dstbuf),
96 	    &s) == 5);
97 
98 	/* Null terminated string, internal dest. buffer, stopping early. */
99 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
100 	wcscpy(srcbuf, L"hello");
101 	src = srcbuf;
102 	memset(&s, 0, sizeof(s));
103 	ATF_REQUIRE(wcsnrtombs(NULL, (const wchar_t **)&src, 4, sizeof(dstbuf),
104 	    &s) == 4);
105 
106 	/* Null terminated string, internal state. */
107 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
108 	wcscpy(srcbuf, L"hello");
109 	memset(dstbuf, 0xcc, sizeof(dstbuf));
110 	src = srcbuf;
111 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, sizeof(dstbuf),
112 	    NULL) == 5);
113 	ATF_REQUIRE(strcmp(dstbuf, "hello") == 0);
114 	ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc);
115 	ATF_REQUIRE(src == NULL);
116 
117 	/* Null terminated string, internal state, internal dest. buffer. */
118 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
119 	wcscpy(srcbuf, L"hello");
120 	src = srcbuf;
121 	ATF_REQUIRE(wcsnrtombs(NULL, (const wchar_t **)&src, 6, 0, NULL) == 5);
122 
123 	/* Empty source buffer. */
124 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
125 	srcbuf[0] = L'\0';
126 	memset(dstbuf, 0xcc, sizeof(dstbuf));
127 	src = srcbuf;
128 	memset(&s, 0, sizeof(s));
129 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 1, sizeof(dstbuf),
130 	    &s) == 0);
131 	ATF_REQUIRE(dstbuf[0] == L'\0');
132 
133 	/* Zero length destination buffer. */
134 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
135 	wcscpy(srcbuf, L"hello");
136 	memset(dstbuf, 0xcc, sizeof(dstbuf));
137 	src = srcbuf;
138 	memset(&s, 0, sizeof(s));
139 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, 0, &s) == 0);
140 	ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc);
141 
142 	/* Zero length source buffer. */
143 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
144 	memset(dstbuf, 0xcc, sizeof(dstbuf));
145 	src = srcbuf;
146 	memset(&s, 0, sizeof(s));
147 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 0, sizeof(dstbuf),
148 	    &s) == 0);
149 	ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc);
150 	ATF_REQUIRE(src == srcbuf);
151 
152 	/*
153 	 * Japanese (EUC) locale.
154 	 */
155 
156 	ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
157 	ATF_REQUIRE(MB_CUR_MAX > 1);
158 
159 	wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf));
160 	srcbuf[0] = 0xA3C1;
161 	srcbuf[1] = 0x0020;
162 	srcbuf[2] = 0x0042;
163 	srcbuf[3] = 0x0020;
164 	srcbuf[4] = 0xA3C3;
165 	srcbuf[5] = 0x0000;
166 	memset(dstbuf, 0xcc, sizeof(dstbuf));
167 	src = srcbuf;
168 	memset(&s, 0, sizeof(s));
169 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, sizeof(dstbuf),
170 	    &s) == 7);
171 	ATF_REQUIRE(strcmp(dstbuf, "\xA3\xC1 B \xA3\xC3") == 0);
172 	ATF_REQUIRE((unsigned char)dstbuf[8] == 0xcc);
173 	ATF_REQUIRE(src == NULL);
174 
175 	/* Stopping early. */
176 	memset(dstbuf, 0xcc, sizeof(dstbuf));
177 	src = srcbuf;
178 	memset(&s, 0, sizeof(s));
179 	ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, 6,
180 	    &s) == 5);
181 	ATF_REQUIRE(memcmp(dstbuf, "\xA3\xC1 B ", 5) == 0);
182 	ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc);
183 	ATF_REQUIRE(src == srcbuf + 4);
184 }
185 
186 ATF_TP_ADD_TCS(tp)
187 {
188 
189 	ATF_TP_ADD_TC(tp, wcsnrtombs_test);
190 
191 	return (atf_no_error());
192 }
193