xref: /minix3/tests/lib/libc/gen/t_vis.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_vis.c,v 1.8 2015/05/23 14:02:11 christos Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2002 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
811be35a1SLionel Sambuc  *
911be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1011be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1111be35a1SLionel Sambuc  * are met:
1211be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1311be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1411be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1511be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1611be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1711be35a1SLionel Sambuc  *
1811be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1911be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2011be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2111be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2211be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2311be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2411be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2511be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2611be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2711be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2811be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
2911be35a1SLionel Sambuc  */
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc #include <atf-c.h>
3211be35a1SLionel Sambuc 
3311be35a1SLionel Sambuc #include <string.h>
3411be35a1SLionel Sambuc #include <stdlib.h>
35*0a6a1f1dSLionel Sambuc #include <locale.h>
3611be35a1SLionel Sambuc #include <err.h>
3711be35a1SLionel Sambuc #include <vis.h>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc static int styles[] = {
4011be35a1SLionel Sambuc 	VIS_OCTAL,
4111be35a1SLionel Sambuc 	VIS_CSTYLE,
4211be35a1SLionel Sambuc 	VIS_SP,
4311be35a1SLionel Sambuc 	VIS_TAB,
4411be35a1SLionel Sambuc 	VIS_NL,
4511be35a1SLionel Sambuc 	VIS_WHITE,
4611be35a1SLionel Sambuc 	VIS_SAFE,
4711be35a1SLionel Sambuc #if 0	/* Not reversible */
4811be35a1SLionel Sambuc 	VIS_NOSLASH,
4911be35a1SLionel Sambuc #endif
5011be35a1SLionel Sambuc 	VIS_HTTP1808,
5111be35a1SLionel Sambuc 	VIS_MIMESTYLE,
5211be35a1SLionel Sambuc #if 0	/* Not supported by vis(3) */
5311be35a1SLionel Sambuc 	VIS_HTTP1866,
5411be35a1SLionel Sambuc #endif
5511be35a1SLionel Sambuc };
5611be35a1SLionel Sambuc 
5711be35a1SLionel Sambuc #define SIZE	256
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc ATF_TC(strvis_basic);
ATF_TC_HEAD(strvis_basic,tc)6011be35a1SLionel Sambuc ATF_TC_HEAD(strvis_basic, tc)
6111be35a1SLionel Sambuc {
6211be35a1SLionel Sambuc 
6311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test strvis(3)");
6411be35a1SLionel Sambuc }
6511be35a1SLionel Sambuc 
ATF_TC_BODY(strvis_basic,tc)6611be35a1SLionel Sambuc ATF_TC_BODY(strvis_basic, tc)
6711be35a1SLionel Sambuc {
6811be35a1SLionel Sambuc 	char *srcbuf, *dstbuf, *visbuf;
6911be35a1SLionel Sambuc 	unsigned int i, j;
7011be35a1SLionel Sambuc 
7111be35a1SLionel Sambuc 	ATF_REQUIRE((dstbuf = malloc(SIZE)) != NULL);
7211be35a1SLionel Sambuc 	ATF_REQUIRE((srcbuf = malloc(SIZE)) != NULL);
7311be35a1SLionel Sambuc 	ATF_REQUIRE((visbuf = malloc(SIZE * 4 + 1)) != NULL);
7411be35a1SLionel Sambuc 
7511be35a1SLionel Sambuc 	for (i = 0; i < SIZE; i++)
7611be35a1SLionel Sambuc 		srcbuf[i] = (char)i;
7711be35a1SLionel Sambuc 
7811be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(styles); i++) {
7911be35a1SLionel Sambuc 		ATF_REQUIRE(strsvisx(visbuf, srcbuf, SIZE, styles[i], "") > 0);
8011be35a1SLionel Sambuc 		memset(dstbuf, 0, SIZE);
8111be35a1SLionel Sambuc 		ATF_REQUIRE(strunvisx(dstbuf, visbuf,
8211be35a1SLionel Sambuc 		    styles[i] & (VIS_HTTP1808|VIS_MIMESTYLE)) > 0);
8311be35a1SLionel Sambuc 		for (j = 0; j < SIZE; j++)
8411be35a1SLionel Sambuc 			if (dstbuf[j] != (char)j)
8511be35a1SLionel Sambuc 				atf_tc_fail_nonfatal("Failed for style %x, "
8611be35a1SLionel Sambuc 				    "char %d [%d]", styles[i], j, dstbuf[j]);
8711be35a1SLionel Sambuc 	}
8811be35a1SLionel Sambuc 	free(dstbuf);
8911be35a1SLionel Sambuc 	free(srcbuf);
9011be35a1SLionel Sambuc 	free(visbuf);
9111be35a1SLionel Sambuc }
9211be35a1SLionel Sambuc 
93*0a6a1f1dSLionel Sambuc ATF_TC(strvis_null);
ATF_TC_HEAD(strvis_null,tc)94*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(strvis_null, tc)
95*0a6a1f1dSLionel Sambuc {
96*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) NULL");
97*0a6a1f1dSLionel Sambuc }
98*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(strvis_null,tc)99*0a6a1f1dSLionel Sambuc ATF_TC_BODY(strvis_null, tc)
100*0a6a1f1dSLionel Sambuc {
101*0a6a1f1dSLionel Sambuc 	char dst[] = "fail";
102*0a6a1f1dSLionel Sambuc 	strvis(dst, NULL, VIS_SAFE);
103*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(dst[0] == '\0' && dst[1] == 'a');
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc ATF_TC(strvis_empty);
ATF_TC_HEAD(strvis_empty,tc)107*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(strvis_empty, tc)
108*0a6a1f1dSLionel Sambuc {
109*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) empty");
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(strvis_empty,tc)112*0a6a1f1dSLionel Sambuc ATF_TC_BODY(strvis_empty, tc)
113*0a6a1f1dSLionel Sambuc {
114*0a6a1f1dSLionel Sambuc 	char dst[] = "fail";
115*0a6a1f1dSLionel Sambuc 	strvis(dst, "", VIS_SAFE);
116*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(dst[0] == '\0' && dst[1] == 'a');
117*0a6a1f1dSLionel Sambuc }
118*0a6a1f1dSLionel Sambuc 
11911be35a1SLionel Sambuc ATF_TC(strunvis_hex);
ATF_TC_HEAD(strunvis_hex,tc)12011be35a1SLionel Sambuc ATF_TC_HEAD(strunvis_hex, tc)
12111be35a1SLionel Sambuc {
12211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test strunvis(3) \\xXX");
12311be35a1SLionel Sambuc }
12411be35a1SLionel Sambuc 
ATF_TC_BODY(strunvis_hex,tc)12511be35a1SLionel Sambuc ATF_TC_BODY(strunvis_hex, tc)
12611be35a1SLionel Sambuc {
12711be35a1SLionel Sambuc 	static const struct {
12811be35a1SLionel Sambuc 		const char *e;
12911be35a1SLionel Sambuc 		const char *d;
13011be35a1SLionel Sambuc 		int error;
13111be35a1SLionel Sambuc 	} ed[] = {
13211be35a1SLionel Sambuc 		{ "\\xff", "\xff", 1 },
13311be35a1SLionel Sambuc 		{ "\\x1", "\x1", 1 },
13411be35a1SLionel Sambuc 		{ "\\x1\\x02", "\x1\x2", 2 },
13511be35a1SLionel Sambuc 		{ "\\x1x", "\x1x", 2 },
13611be35a1SLionel Sambuc 		{ "\\xx", "", -1 },
13711be35a1SLionel Sambuc 	};
13811be35a1SLionel Sambuc 	char uv[10];
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc 	for (size_t i = 0; i < __arraycount(ed); i++) {
14111be35a1SLionel Sambuc 		ATF_REQUIRE(strunvis(uv, ed[i].e) == ed[i].error);
14211be35a1SLionel Sambuc 		if (ed[i].error > 0)
14311be35a1SLionel Sambuc 			ATF_REQUIRE(memcmp(ed[i].d, uv, ed[i].error) == 0);
14411be35a1SLionel Sambuc 	}
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc 
147*0a6a1f1dSLionel Sambuc ATF_TC(strvis_locale);
ATF_TC_HEAD(strvis_locale,tc)148*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(strvis_locale, tc)
149*0a6a1f1dSLionel Sambuc {
150*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) with locale");
151*0a6a1f1dSLionel Sambuc }
152*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(strvis_locale,tc)153*0a6a1f1dSLionel Sambuc ATF_TC_BODY(strvis_locale, tc)
154*0a6a1f1dSLionel Sambuc {
155*0a6a1f1dSLionel Sambuc 	char s[256], cd[sizeof(s) * 4 + 1], jd[sizeof(cd)], *ol;
156*0a6a1f1dSLionel Sambuc 	int jr, cr;
157*0a6a1f1dSLionel Sambuc 
158*0a6a1f1dSLionel Sambuc 	for (size_t i = 0; i < sizeof(s) - 1; i++)
159*0a6a1f1dSLionel Sambuc 		s[i] = i + 1;
160*0a6a1f1dSLionel Sambuc 	s[sizeof(s) - 1] = '\0';
161*0a6a1f1dSLionel Sambuc 
162*0a6a1f1dSLionel Sambuc 	ol = setlocale(LC_CTYPE, "ja_JP.UTF-8");
163*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(ol != NULL);
164*0a6a1f1dSLionel Sambuc 	jr = strvisx(jd, s, sizeof(s), VIS_WHITE | VIS_NOLOCALE);
165*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(jr != -1);
166*0a6a1f1dSLionel Sambuc 	ol = strdup(ol);
167*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(ol != NULL);
168*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(setlocale(LC_CTYPE, "C") != NULL);
169*0a6a1f1dSLionel Sambuc 	cr = strvisx(cd, s, sizeof(s), VIS_WHITE);
170*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(jr == cr);
171*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(memcmp(jd, cd, jr) == 0);
172*0a6a1f1dSLionel Sambuc 	setlocale(LC_CTYPE, ol);
173*0a6a1f1dSLionel Sambuc 	free(ol);
174*0a6a1f1dSLionel Sambuc }
175*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)17611be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
17711be35a1SLionel Sambuc {
17811be35a1SLionel Sambuc 
17911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, strvis_basic);
180*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, strvis_null);
181*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, strvis_empty);
18211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, strunvis_hex);
183*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, strvis_locale);
18411be35a1SLionel Sambuc 
18511be35a1SLionel Sambuc 	return atf_no_error();
18611be35a1SLionel Sambuc }
187