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