1 /* $NetBSD: t_io.c,v 1.4 2014/01/21 00:32:16 yamt Exp $ */
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2011\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_io.c,v 1.4 2014/01/21 00:32:16 yamt Exp $");
36 
37 #include <sys/param.h>
38 #include <errno.h>
39 #include <locale.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <wchar.h>
44 
45 #include <atf-c.h>
46 
47 
48 ATF_TC(bad_big5_wprintf);
49 ATF_TC_HEAD(bad_big5_wprintf, tc)
50 {
51 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar wprintf");
52 }
53 
54 ATF_TC_BODY(bad_big5_wprintf, tc)
55 {
56 #ifdef __FreeBSD__
57 	atf_tc_skip("does not fail as expected (may be implementation "
58 	    "specific issue with the test)");
59 #endif
60 
61 	/* XXX implementation detail knowledge (wchar_t encoding) */
62 	wchar_t ibuf[] = { 0xcf10, 0 };
63 	setlocale(LC_CTYPE, "zh_TW.Big5");
64 
65 	ATF_REQUIRE_ERRNO(EILSEQ, wprintf(L"%ls\n", ibuf) < 0);
66 	ATF_REQUIRE(ferror(stdout));
67 }
68 
69 ATF_TC(bad_big5_swprintf);
70 ATF_TC_HEAD(bad_big5_swprintf, tc)
71 {
72 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
73 }
74 
75 ATF_TC_BODY(bad_big5_swprintf, tc)
76 {
77 #ifdef __FreeBSD__
78 	atf_tc_skip("does not fail as expected (may be implementation "
79 	    "specific issue with the test)");
80 #endif
81 
82 	/* XXX implementation detail knowledge (wchar_t encoding) */
83 	wchar_t ibuf[] = { 0xcf10, 0 };
84 	wchar_t obuf[20];
85 	setlocale(LC_CTYPE, "zh_TW.Big5");
86 
87 	ATF_REQUIRE_ERRNO(EILSEQ,
88 			  swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf) < 0);
89 }
90 
91 ATF_TC(good_big5_wprintf);
92 ATF_TC_HEAD(good_big5_wprintf, tc)
93 {
94 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
95 }
96 
97 ATF_TC_BODY(good_big5_wprintf, tc)
98 {
99 	/* XXX implementation detail knowledge (wchar_t encoding) */
100 	wchar_t ibuf[] = { 0xcf40, 0 };
101 	setlocale(LC_CTYPE, "zh_TW.Big5");
102 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
103 }
104 
105 ATF_TC(good_big5_swprintf);
106 ATF_TC_HEAD(good_big5_swprintf, tc)
107 {
108 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
109 }
110 
111 ATF_TC_BODY(good_big5_swprintf, tc)
112 {
113 	/* XXX implementation detail knowledge (wchar_t encoding) */
114 	wchar_t ibuf[] = { 0xcf40, 0 };
115 	wchar_t obuf[20];
116 	setlocale(LC_CTYPE, "zh_TW.Big5");
117 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
118 }
119 
120 struct ibuf {
121 	off_t off;
122 	size_t buflen;
123 	const char *buf;
124 };
125 
126 static int
127 readfn(void *vp, char *buf, int len)
128 {
129 	struct ibuf *ib = vp;
130 	size_t todo = MIN((size_t)len, ib->buflen - ib->off);
131 
132 	memcpy(buf, ib->buf + ib->off, todo);
133 	ib->off += todo;
134 	return todo;
135 }
136 
137 ATF_TC(good_big5_getwc);
138 ATF_TC_HEAD(good_big5_getwc, tc)
139 {
140 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
141 }
142 
143 ATF_TC_BODY(good_big5_getwc, tc)
144 {
145 	const char buf[] = { 0xcf, 0x40 };
146 	struct ibuf ib = {
147 		.buf = buf,
148 		.buflen = sizeof(buf),
149 	};
150 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
151 
152 	ATF_REQUIRE(fp != NULL);
153 	setlocale(LC_CTYPE, "zh_TW.Big5");
154 	/* XXX implementation detail knowledge (wchar_t encoding) */
155 	ATF_REQUIRE_EQ(getwc(fp), 0xcf40);
156 	fclose(fp);
157 }
158 
159 ATF_TC(bad_big5_getwc);
160 ATF_TC_HEAD(bad_big5_getwc, tc)
161 {
162 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
163 }
164 
165 ATF_TC_BODY(bad_big5_getwc, tc)
166 {
167 	const char buf[] = { 0xcf, 0x20 };
168 	struct ibuf ib = {
169 		.buf = buf,
170 		.buflen = sizeof(buf),
171 	};
172 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
173 
174 	ATF_REQUIRE(fp != NULL);
175 	setlocale(LC_CTYPE, "zh_TW.Big5");
176 #ifdef __FreeBSD__
177 	atf_tc_expect_fail("does not return WEOF as expected");
178 #endif
179 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
180 	fclose(fp);
181 }
182 
183 ATF_TP_ADD_TCS(tp)
184 {
185 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
186 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
187 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
188 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
189 	ATF_TP_ADD_TC(tp, good_big5_getwc);
190 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
191 
192 	return atf_no_error();
193 }
194