xref: /minix3/tests/lib/libc/gen/t_vis.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_vis.c,v 1.6 2013/02/13 04:51:56 christos Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
8*11be35a1SLionel Sambuc  *
9*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
11*11be35a1SLionel Sambuc  * are met:
12*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
15*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
16*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
17*11be35a1SLionel Sambuc  *
18*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
29*11be35a1SLionel Sambuc  */
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <atf-c.h>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include <string.h>
34*11be35a1SLionel Sambuc #include <stdlib.h>
35*11be35a1SLionel Sambuc #include <err.h>
36*11be35a1SLionel Sambuc #include <vis.h>
37*11be35a1SLionel Sambuc 
38*11be35a1SLionel Sambuc static int styles[] = {
39*11be35a1SLionel Sambuc 	VIS_OCTAL,
40*11be35a1SLionel Sambuc 	VIS_CSTYLE,
41*11be35a1SLionel Sambuc 	VIS_SP,
42*11be35a1SLionel Sambuc 	VIS_TAB,
43*11be35a1SLionel Sambuc 	VIS_NL,
44*11be35a1SLionel Sambuc 	VIS_WHITE,
45*11be35a1SLionel Sambuc 	VIS_SAFE,
46*11be35a1SLionel Sambuc #if 0	/* Not reversible */
47*11be35a1SLionel Sambuc 	VIS_NOSLASH,
48*11be35a1SLionel Sambuc #endif
49*11be35a1SLionel Sambuc 	VIS_HTTP1808,
50*11be35a1SLionel Sambuc 	VIS_MIMESTYLE,
51*11be35a1SLionel Sambuc #if 0	/* Not supported by vis(3) */
52*11be35a1SLionel Sambuc 	VIS_HTTP1866,
53*11be35a1SLionel Sambuc #endif
54*11be35a1SLionel Sambuc };
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc #define SIZE	256
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc ATF_TC(strvis_basic);
59*11be35a1SLionel Sambuc ATF_TC_HEAD(strvis_basic, tc)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc 
62*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test strvis(3)");
63*11be35a1SLionel Sambuc }
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc ATF_TC_BODY(strvis_basic, tc)
66*11be35a1SLionel Sambuc {
67*11be35a1SLionel Sambuc 	char *srcbuf, *dstbuf, *visbuf;
68*11be35a1SLionel Sambuc 	unsigned int i, j;
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc 	ATF_REQUIRE((dstbuf = malloc(SIZE)) != NULL);
71*11be35a1SLionel Sambuc 	ATF_REQUIRE((srcbuf = malloc(SIZE)) != NULL);
72*11be35a1SLionel Sambuc 	ATF_REQUIRE((visbuf = malloc(SIZE * 4 + 1)) != NULL);
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc 	for (i = 0; i < SIZE; i++)
75*11be35a1SLionel Sambuc 		srcbuf[i] = (char)i;
76*11be35a1SLionel Sambuc 
77*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(styles); i++) {
78*11be35a1SLionel Sambuc 		ATF_REQUIRE(strsvisx(visbuf, srcbuf, SIZE, styles[i], "") > 0);
79*11be35a1SLionel Sambuc 		memset(dstbuf, 0, SIZE);
80*11be35a1SLionel Sambuc 		ATF_REQUIRE(strunvisx(dstbuf, visbuf,
81*11be35a1SLionel Sambuc 		    styles[i] & (VIS_HTTP1808|VIS_MIMESTYLE)) > 0);
82*11be35a1SLionel Sambuc 		for (j = 0; j < SIZE; j++)
83*11be35a1SLionel Sambuc 			if (dstbuf[j] != (char)j)
84*11be35a1SLionel Sambuc 				atf_tc_fail_nonfatal("Failed for style %x, "
85*11be35a1SLionel Sambuc 				    "char %d [%d]", styles[i], j, dstbuf[j]);
86*11be35a1SLionel Sambuc 	}
87*11be35a1SLionel Sambuc 	free(dstbuf);
88*11be35a1SLionel Sambuc 	free(srcbuf);
89*11be35a1SLionel Sambuc 	free(visbuf);
90*11be35a1SLionel Sambuc }
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc ATF_TC(strunvis_hex);
93*11be35a1SLionel Sambuc ATF_TC_HEAD(strunvis_hex, tc)
94*11be35a1SLionel Sambuc {
95*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test strunvis(3) \\xXX");
96*11be35a1SLionel Sambuc }
97*11be35a1SLionel Sambuc 
98*11be35a1SLionel Sambuc ATF_TC_BODY(strunvis_hex, tc)
99*11be35a1SLionel Sambuc {
100*11be35a1SLionel Sambuc 	static const struct {
101*11be35a1SLionel Sambuc 		const char *e;
102*11be35a1SLionel Sambuc 		const char *d;
103*11be35a1SLionel Sambuc 		int error;
104*11be35a1SLionel Sambuc 	} ed[] = {
105*11be35a1SLionel Sambuc 		{ "\\xff", "\xff", 1 },
106*11be35a1SLionel Sambuc 		{ "\\x1", "\x1", 1 },
107*11be35a1SLionel Sambuc 		{ "\\x1\\x02", "\x1\x2", 2 },
108*11be35a1SLionel Sambuc 		{ "\\x1x", "\x1x", 2 },
109*11be35a1SLionel Sambuc 		{ "\\xx", "", -1 },
110*11be35a1SLionel Sambuc 	};
111*11be35a1SLionel Sambuc 	char uv[10];
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 	for (size_t i = 0; i < __arraycount(ed); i++) {
114*11be35a1SLionel Sambuc 		ATF_REQUIRE(strunvis(uv, ed[i].e) == ed[i].error);
115*11be35a1SLionel Sambuc 		if (ed[i].error > 0)
116*11be35a1SLionel Sambuc 			ATF_REQUIRE(memcmp(ed[i].d, uv, ed[i].error) == 0);
117*11be35a1SLionel Sambuc 	}
118*11be35a1SLionel Sambuc }
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
121*11be35a1SLionel Sambuc {
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, strvis_basic);
124*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, strunvis_hex);
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc 	return atf_no_error();
127*11be35a1SLionel Sambuc }
128