xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_vis.c (revision 79e6ffb6951bd75a92326fabadd48e87ee0aed96)
1 /*	$NetBSD: t_vis.c,v 1.9 2017/01/10 15:16:57 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <atf-c.h>
32 
33 #include <string.h>
34 #include <stdlib.h>
35 #include <locale.h>
36 #include <err.h>
37 #include <vis.h>
38 
39 static int styles[] = {
40 	VIS_OCTAL,
41 	VIS_CSTYLE,
42 	VIS_SP,
43 	VIS_TAB,
44 	VIS_NL,
45 	VIS_WHITE,
46 	VIS_SAFE,
47 #if 0	/* Not reversible */
48 	VIS_NOSLASH,
49 #endif
50 	VIS_HTTP1808,
51 	VIS_MIMESTYLE,
52 #if 0	/* Not supported by vis(3) */
53 	VIS_HTTP1866,
54 #endif
55 };
56 
57 #define SIZE	256
58 
59 ATF_TC(strvis_basic);
ATF_TC_HEAD(strvis_basic,tc)60 ATF_TC_HEAD(strvis_basic, tc)
61 {
62 
63 	atf_tc_set_md_var(tc, "descr", "Test strvis(3)");
64 }
65 
ATF_TC_BODY(strvis_basic,tc)66 ATF_TC_BODY(strvis_basic, tc)
67 {
68 	char *srcbuf, *dstbuf, *visbuf;
69 	unsigned int i, j;
70 
71 	ATF_REQUIRE((dstbuf = malloc(SIZE)) != NULL);
72 	ATF_REQUIRE((srcbuf = malloc(SIZE)) != NULL);
73 	ATF_REQUIRE((visbuf = malloc(SIZE * 4 + 1)) != NULL);
74 
75 	for (i = 0; i < SIZE; i++)
76 		srcbuf[i] = (char)i;
77 
78 	for (i = 0; i < __arraycount(styles); i++) {
79 		ATF_REQUIRE(strsvisx(visbuf, srcbuf, SIZE, styles[i], "") > 0);
80 		memset(dstbuf, 0, SIZE);
81 		ATF_REQUIRE(strunvisx(dstbuf, visbuf,
82 		    styles[i] & (VIS_HTTP1808|VIS_MIMESTYLE)) > 0);
83 		for (j = 0; j < SIZE; j++)
84 			if (dstbuf[j] != (char)j)
85 				atf_tc_fail_nonfatal("Failed for style %x, "
86 				    "char %d [%d]", styles[i], j, dstbuf[j]);
87 	}
88 	free(dstbuf);
89 	free(srcbuf);
90 	free(visbuf);
91 }
92 
93 ATF_TC(strvis_null);
ATF_TC_HEAD(strvis_null,tc)94 ATF_TC_HEAD(strvis_null, tc)
95 {
96 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) NULL");
97 }
98 
ATF_TC_BODY(strvis_null,tc)99 ATF_TC_BODY(strvis_null, tc)
100 {
101 	char dst[] = "fail";
102 	strvis(dst, NULL, VIS_SAFE);
103 	ATF_REQUIRE(dst[0] == '\0' && dst[1] == 'a');
104 }
105 
106 ATF_TC(strvis_empty);
ATF_TC_HEAD(strvis_empty,tc)107 ATF_TC_HEAD(strvis_empty, tc)
108 {
109 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) empty");
110 }
111 
ATF_TC_BODY(strvis_empty,tc)112 ATF_TC_BODY(strvis_empty, tc)
113 {
114 	char dst[] = "fail";
115 	strvis(dst, "", VIS_SAFE);
116 	ATF_REQUIRE(dst[0] == '\0' && dst[1] == 'a');
117 }
118 
119 ATF_TC(strunvis_hex);
ATF_TC_HEAD(strunvis_hex,tc)120 ATF_TC_HEAD(strunvis_hex, tc)
121 {
122 	atf_tc_set_md_var(tc, "descr", "Test strunvis(3) \\xXX");
123 }
124 
ATF_TC_BODY(strunvis_hex,tc)125 ATF_TC_BODY(strunvis_hex, tc)
126 {
127 	static const struct {
128 		const char *e;
129 		const char *d;
130 		int error;
131 	} ed[] = {
132 		{ "\\xff", "\xff", 1 },
133 		{ "\\x1", "\x1", 1 },
134 		{ "\\x1\\x02", "\x1\x2", 2 },
135 		{ "\\x1x", "\x1x", 2 },
136 		{ "\\xx", "", -1 },
137 	};
138 	char uv[10];
139 
140 	for (size_t i = 0; i < __arraycount(ed); i++) {
141 		ATF_REQUIRE(strunvis(uv, ed[i].e) == ed[i].error);
142 		if (ed[i].error > 0)
143 			ATF_REQUIRE(memcmp(ed[i].d, uv, ed[i].error) == 0);
144 	}
145 }
146 
147 #ifdef VIS_NOLOCALE
148 ATF_TC(strvis_locale);
ATF_TC_HEAD(strvis_locale,tc)149 ATF_TC_HEAD(strvis_locale, tc)
150 {
151 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) with locale");
152 }
153 
ATF_TC_BODY(strvis_locale,tc)154 ATF_TC_BODY(strvis_locale, tc)
155 {
156 	char s[256], cd[sizeof(s) * 4 + 1], jd[sizeof(cd)], *ol;
157 	int jr, cr;
158 
159 	for (size_t i = 0; i < sizeof(s) - 1; i++)
160 		s[i] = i + 1;
161 	s[sizeof(s) - 1] = '\0';
162 
163 	ol = setlocale(LC_CTYPE, "ja_JP.UTF-8");
164 	ATF_REQUIRE(ol != NULL);
165 	jr = strvisx(jd, s, sizeof(s), VIS_WHITE | VIS_NOLOCALE);
166 	ATF_REQUIRE(jr != -1);
167 	ol = strdup(ol);
168 	ATF_REQUIRE(ol != NULL);
169 	ATF_REQUIRE(setlocale(LC_CTYPE, "C") != NULL);
170 	cr = strvisx(cd, s, sizeof(s), VIS_WHITE);
171 	ATF_REQUIRE(jr == cr);
172 	ATF_REQUIRE(memcmp(jd, cd, jr) == 0);
173 	setlocale(LC_CTYPE, ol);
174 	free(ol);
175 }
176 #endif /* VIS_NOLOCALE */
177 
178 #ifdef __FreeBSD__
179 #define	STRVIS_OVERFLOW_MARKER	((char)0xff)	/* Arbitrary */
180 
181 ATF_TC(strvis_overflow_mb);
ATF_TC_HEAD(strvis_overflow_mb,tc)182 ATF_TC_HEAD(strvis_overflow_mb, tc)
183 {
184 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) multi-byte overflow");
185 }
186 
ATF_TC_BODY(strvis_overflow_mb,tc)187 ATF_TC_BODY(strvis_overflow_mb, tc)
188 {
189 	const char src[] = "\xf0\x9f\xa5\x91";
190 	/* Extra byte to detect overflow */
191 	char dst[sizeof(src) + 1];
192 	int n;
193 
194 	setlocale(LC_CTYPE, "en_US.UTF-8");
195 
196 	/* Arbitrary */
197 	memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
198 
199 	/*
200 	 * If we only provide four bytes of buffer, we shouldn't be able encode
201 	 * a full 4-byte sequence.
202 	 */
203 	n = strnvis(dst, 4, src, VIS_SAFE);
204 	ATF_REQUIRE(dst[4] == STRVIS_OVERFLOW_MARKER);
205 	ATF_REQUIRE(n == -1);
206 
207 	n = strnvis(dst, sizeof(src), src, VIS_SAFE);
208 	ATF_REQUIRE(n == sizeof(src) - 1);
209 }
210 
211 ATF_TC(strvis_overflow_c);
ATF_TC_HEAD(strvis_overflow_c,tc)212 ATF_TC_HEAD(strvis_overflow_c, tc)
213 {
214 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) C locale overflow");
215 }
216 
ATF_TC_BODY(strvis_overflow_c,tc)217 ATF_TC_BODY(strvis_overflow_c, tc)
218 {
219 	const char src[] = "AAAA";
220 	/* Extra byte to detect overflow */
221 	char dst[sizeof(src) + 1];
222 	int n;
223 
224 	/* Arbitrary */
225 	memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
226 
227 	/*
228 	 * If we only provide four bytes of buffer, we shouldn't be able encode
229 	 * 4 bytes of input.
230 	 */
231 	n = strnvis(dst, 4, src, VIS_SAFE | VIS_NOLOCALE);
232 	ATF_REQUIRE(dst[4] == STRVIS_OVERFLOW_MARKER);
233 	ATF_REQUIRE(n == -1);
234 
235 	n = strnvis(dst, sizeof(src), src, VIS_SAFE | VIS_NOLOCALE);
236 	ATF_REQUIRE(n == sizeof(src) - 1);
237 }
238 #endif /* __FreeBSD__ */
239 
ATF_TP_ADD_TCS(tp)240 ATF_TP_ADD_TCS(tp)
241 {
242 
243 	ATF_TP_ADD_TC(tp, strvis_basic);
244 	ATF_TP_ADD_TC(tp, strvis_null);
245 	ATF_TP_ADD_TC(tp, strvis_empty);
246 	ATF_TP_ADD_TC(tp, strunvis_hex);
247 #ifdef VIS_NOLOCALE
248 	ATF_TP_ADD_TC(tp, strvis_locale);
249 #endif /* VIS_NOLOCALE */
250 #ifdef __FreeBSD__
251 	ATF_TP_ADD_TC(tp, strvis_overflow_mb);
252 	ATF_TP_ADD_TC(tp, strvis_overflow_c);
253 #endif
254 
255 	return atf_no_error();
256 }
257