Home | History | Annotate | Line # | Download | only in gen
t_vis.c revision 1.12
      1 /*	$NetBSD: t_vis.c,v 1.12 2023/08/12 12:46:16 riastradh 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);
     60 ATF_TC_HEAD(strvis_basic, tc)
     61 {
     62 
     63 	atf_tc_set_md_var(tc, "descr", "Test strvis(3)");
     64 }
     65 
     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);
     94 ATF_TC_HEAD(strvis_null, tc)
     95 {
     96 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) NULL");
     97 }
     98 
     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);
    107 ATF_TC_HEAD(strvis_empty, tc)
    108 {
    109 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) empty");
    110 }
    111 
    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(strnvis_empty_empty);
    120 ATF_TC_HEAD(strnvis_empty_empty, tc)
    121 {
    122 	atf_tc_set_md_var(tc, "descr",
    123 	    "Test strnvis(3) with empty source and destination");
    124 }
    125 
    126 ATF_TC_BODY(strnvis_empty_empty, tc)
    127 {
    128 	char dst[] = "fail";
    129 	int n;
    130 
    131 	atf_tc_expect_fail("PR lib/57573: Overflow possibilities in vis(3)");
    132 
    133 	n = strnvis(dst, 0, "", VIS_SAFE);
    134 	ATF_CHECK(memcmp(dst, "fail", sizeof(dst)) == 0);
    135 	ATF_CHECK_EQ_MSG(n, -1, "n=%d", n);
    136 }
    137 
    138 ATF_TC(strunvis_hex);
    139 ATF_TC_HEAD(strunvis_hex, tc)
    140 {
    141 	atf_tc_set_md_var(tc, "descr", "Test strunvis(3) \\xXX");
    142 }
    143 
    144 ATF_TC_BODY(strunvis_hex, tc)
    145 {
    146 	static const struct {
    147 		const char *e;
    148 		const char *d;
    149 		int error;
    150 	} ed[] = {
    151 		{ "\\xff", "\xff", 1 },
    152 		{ "\\x1", "\x1", 1 },
    153 		{ "\\x1\\x02", "\x1\x2", 2 },
    154 		{ "\\x1x", "\x1x", 2 },
    155 		{ "\\xx", "", -1 },
    156 	};
    157 	char uv[10];
    158 
    159 	for (size_t i = 0; i < __arraycount(ed); i++) {
    160 		ATF_REQUIRE(strunvis(uv, ed[i].e) == ed[i].error);
    161 		if (ed[i].error > 0)
    162 			ATF_REQUIRE(memcmp(ed[i].d, uv, ed[i].error) == 0);
    163 	}
    164 }
    165 
    166 #ifdef VIS_NOLOCALE
    167 ATF_TC(strvis_locale);
    168 ATF_TC_HEAD(strvis_locale, tc)
    169 {
    170 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) with locale");
    171 }
    172 
    173 ATF_TC_BODY(strvis_locale, tc)
    174 {
    175 	char s[256], cd[sizeof(s) * 4 + 1], jd[sizeof(cd)], *ol;
    176 	int jr, cr;
    177 
    178 	for (size_t i = 0; i < sizeof(s) - 1; i++)
    179 		s[i] = i + 1;
    180 	s[sizeof(s) - 1] = '\0';
    181 
    182 	ol = setlocale(LC_CTYPE, "ja_JP.UTF-8");
    183 	ATF_REQUIRE(ol != NULL);
    184 	jr = strvisx(jd, s, sizeof(s), VIS_WHITE | VIS_NOLOCALE);
    185 	ATF_REQUIRE(jr != -1);
    186 	ol = strdup(ol);
    187 	ATF_REQUIRE(ol != NULL);
    188 	ATF_REQUIRE(setlocale(LC_CTYPE, "C") != NULL);
    189 	cr = strvisx(cd, s, sizeof(s), VIS_WHITE);
    190 	ATF_REQUIRE(jr == cr);
    191 	ATF_REQUIRE(memcmp(jd, cd, jr) == 0);
    192 	setlocale(LC_CTYPE, ol);
    193 	free(ol);
    194 }
    195 #endif /* VIS_NOLOCALE */
    196 
    197 #define	STRVIS_OVERFLOW_MARKER	((char)0xff)	/* Arbitrary */
    198 
    199 #ifdef VIS_NOLOCALE
    200 ATF_TC(strvis_overflow_mb);
    201 ATF_TC_HEAD(strvis_overflow_mb, tc)
    202 {
    203 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) multi-byte overflow");
    204 }
    205 
    206 ATF_TC_BODY(strvis_overflow_mb, tc)
    207 {
    208 	const char src[] = "\xf0\x9f\xa5\x91";
    209 	/* Extra byte to detect overflow */
    210 	char dst[sizeof(src) + 1];
    211 	unsigned i;
    212 	int n;
    213 
    214 	atf_tc_expect_fail("PR lib/57573: Overflow possibilities in vis(3)");
    215 
    216 	setlocale(LC_CTYPE, "en_US.UTF-8");
    217 
    218 	for (i = 0; i < sizeof(dst) - 1; i++) {
    219 		memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
    220 		n = strnvis(dst, i, src, VIS_SAFE);
    221 		ATF_CHECK_EQ_MSG(dst[i], STRVIS_OVERFLOW_MARKER,
    222 		    "[%u] dst=[%02hhx %02hhx %02hhx %02hhx %02hhx]"
    223 		    " STRVIS_OVERFLOW_MARKER=%02hhx",
    224 		    i, dst[0], dst[1], dst[2], dst[3], dst[4],
    225 		    STRVIS_OVERFLOW_MARKER);
    226 		ATF_CHECK_EQ_MSG(n, -1, "[%u] n=%d", i, n);
    227 	}
    228 
    229 	memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
    230 	n = strnvis(dst, sizeof(dst) - 1, src, VIS_SAFE);
    231 	ATF_CHECK_EQ_MSG(dst[sizeof(dst) - 1], STRVIS_OVERFLOW_MARKER,
    232 	    "[%u] dst=[%02hhx %02hhx %02hhx %02hhx %02hhx %02hhx]"
    233 	    " STRVIS_OVERFLOW_MARKER=%02hhx",
    234 	    i, dst[0], dst[1], dst[2], dst[3], dst[4], dst[5],
    235 	    STRVIS_OVERFLOW_MARKER);
    236 	ATF_CHECK_EQ_MSG(n, (int)sizeof(dst) - 2, "n=%d", n);
    237 }
    238 #endif
    239 
    240 ATF_TC(strvis_overflow_c);
    241 ATF_TC_HEAD(strvis_overflow_c, tc)
    242 {
    243 	atf_tc_set_md_var(tc, "descr", "Test strvis(3) C locale overflow");
    244 }
    245 
    246 ATF_TC_BODY(strvis_overflow_c, tc)
    247 {
    248 	const char src[] = "AAAA";
    249 	/* Extra byte to detect overflow */
    250 	char dst[sizeof(src) + 1];
    251 	unsigned i;
    252 	int n;
    253 
    254 	atf_tc_expect_fail("PR lib/57573: Overflow possibilities in vis(3)");
    255 
    256 	for (i = 0; i < sizeof(dst) - 1; i++) {
    257 		memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
    258 		n = strnvis(dst, i, src, VIS_SAFE | VIS_NOLOCALE);
    259 		ATF_CHECK_EQ_MSG(dst[i], STRVIS_OVERFLOW_MARKER,
    260 		    "[%u] dst=[%02hhx %02hhx %02hhx %02hhx %02hhx]"
    261 		    " STRVIS_OVERFLOW_MARKER=%02hhx",
    262 		    i, dst[0], dst[1], dst[2], dst[3], dst[4],
    263 		    STRVIS_OVERFLOW_MARKER);
    264 		ATF_CHECK_EQ_MSG(n, -1, "[%u] n=%d", i, n);
    265 	}
    266 
    267 	memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
    268 	n = strnvis(dst, sizeof(dst) - 1, src, VIS_SAFE | VIS_NOLOCALE);
    269 	ATF_CHECK_EQ_MSG(dst[sizeof(dst) - 1], STRVIS_OVERFLOW_MARKER,
    270 	    "[%u] dst=[%02hhx %02hhx %02hhx %02hhx %02hhx %02hhx]"
    271 	    " STRVIS_OVERFLOW_MARKER=%02hhx",
    272 	    i, dst[0], dst[1], dst[2], dst[3], dst[4], dst[5],
    273 	    STRVIS_OVERFLOW_MARKER);
    274 	ATF_CHECK_EQ_MSG(n, (int)sizeof(dst) - 2, "n=%d", n);
    275 }
    276 
    277 ATF_TP_ADD_TCS(tp)
    278 {
    279 
    280 	ATF_TP_ADD_TC(tp, strvis_basic);
    281 	ATF_TP_ADD_TC(tp, strvis_null);
    282 	ATF_TP_ADD_TC(tp, strvis_empty);
    283 	ATF_TP_ADD_TC(tp, strnvis_empty_empty);
    284 	ATF_TP_ADD_TC(tp, strunvis_hex);
    285 #ifdef VIS_NOLOCALE
    286 	ATF_TP_ADD_TC(tp, strvis_locale);
    287 	ATF_TP_ADD_TC(tp, strvis_overflow_mb);
    288 #endif /* VIS_NOLOCALE */
    289 	ATF_TP_ADD_TC(tp, strvis_overflow_c);
    290 
    291 	return atf_no_error();
    292 }
    293