1 /* $NetBSD: t_vis.c,v 1.8 2015/05/23 14:02:11 christos Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Christos Zoulas.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
39 static int styles
[] = {
47 #if 0 /* Not reversible */
52 #if 0 /* Not supported by vis(3) */
60 ATF_TC_HEAD(strvis_basic
, tc
)
63 atf_tc_set_md_var(tc
, "descr", "Test strvis(3)");
66 ATF_TC_BODY(strvis_basic
, tc
)
68 char *srcbuf
, *dstbuf
, *visbuf
;
71 ATF_REQUIRE((dstbuf
= malloc(SIZE
)) != NULL
);
72 ATF_REQUIRE((srcbuf
= malloc(SIZE
)) != NULL
);
73 ATF_REQUIRE((visbuf
= malloc(SIZE
* 4 + 1)) != NULL
);
75 for (i
= 0; i
< SIZE
; i
++)
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
]);
94 ATF_TC_HEAD(strvis_null
, tc
)
96 atf_tc_set_md_var(tc
, "descr", "Test strvis(3) NULL");
99 ATF_TC_BODY(strvis_null
, tc
)
102 strvis(dst
, NULL
, VIS_SAFE
);
103 ATF_REQUIRE(dst
[0] == '\0' && dst
[1] == 'a');
106 ATF_TC(strvis_empty
);
107 ATF_TC_HEAD(strvis_empty
, tc
)
109 atf_tc_set_md_var(tc
, "descr", "Test strvis(3) empty");
112 ATF_TC_BODY(strvis_empty
, tc
)
115 strvis(dst
, "", VIS_SAFE
);
116 ATF_REQUIRE(dst
[0] == '\0' && dst
[1] == 'a');
119 ATF_TC(strunvis_hex
);
120 ATF_TC_HEAD(strunvis_hex
, tc
)
122 atf_tc_set_md_var(tc
, "descr", "Test strunvis(3) \\xXX");
125 ATF_TC_BODY(strunvis_hex
, tc
)
127 static const struct {
132 { "\\xff", "\xff", 1 },
133 { "\\x1", "\x1", 1 },
134 { "\\x1\\x02", "\x1\x2", 2 },
135 { "\\x1x", "\x1x", 2 },
140 for (size_t i
= 0; i
< __arraycount(ed
); i
++) {
141 ATF_REQUIRE(strunvis(uv
, ed
[i
].e
) == ed
[i
].error
);
143 ATF_REQUIRE(memcmp(ed
[i
].d
, uv
, ed
[i
].error
) == 0);
147 ATF_TC(strvis_locale
);
148 ATF_TC_HEAD(strvis_locale
, tc
)
150 atf_tc_set_md_var(tc
, "descr", "Test strvis(3) with locale");
153 ATF_TC_BODY(strvis_locale
, tc
)
155 char s
[256], cd
[sizeof(s
) * 4 + 1], jd
[sizeof(cd
)], *ol
;
158 for (size_t i
= 0; i
< sizeof(s
) - 1; i
++)
160 s
[sizeof(s
) - 1] = '\0';
162 ol
= setlocale(LC_CTYPE
, "ja_JP.UTF-8");
163 ATF_REQUIRE(ol
!= NULL
);
164 jr
= strvisx(jd
, s
, sizeof(s
), VIS_WHITE
| VIS_NOLOCALE
);
165 ATF_REQUIRE(jr
!= -1);
167 ATF_REQUIRE(ol
!= NULL
);
168 ATF_REQUIRE(setlocale(LC_CTYPE
, "C") != NULL
);
169 cr
= strvisx(cd
, s
, sizeof(s
), VIS_WHITE
);
170 ATF_REQUIRE(jr
== cr
);
171 ATF_REQUIRE(memcmp(jd
, cd
, jr
) == 0);
172 setlocale(LC_CTYPE
, ol
);
179 ATF_TP_ADD_TC(tp
, strvis_basic
);
180 ATF_TP_ADD_TC(tp
, strvis_null
);
181 ATF_TP_ADD_TC(tp
, strvis_empty
);
182 ATF_TP_ADD_TC(tp
, strunvis_hex
);
183 ATF_TP_ADD_TC(tp
, strvis_locale
);
185 return atf_no_error();