1 /* $NetBSD: t_vis.c,v 1.7 2014/09/08 19:01:03 christos 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 <err.h> 36 #include <vis.h> 37 38 static int styles[] = { 39 VIS_OCTAL, 40 VIS_CSTYLE, 41 VIS_SP, 42 VIS_TAB, 43 VIS_NL, 44 VIS_WHITE, 45 VIS_SAFE, 46 #if 0 /* Not reversible */ 47 VIS_NOSLASH, 48 #endif 49 VIS_HTTP1808, 50 VIS_MIMESTYLE, 51 #if 0 /* Not supported by vis(3) */ 52 VIS_HTTP1866, 53 #endif 54 }; 55 56 #define SIZE 256 57 58 ATF_TC(strvis_basic); 59 ATF_TC_HEAD(strvis_basic, tc) 60 { 61 62 atf_tc_set_md_var(tc, "descr", "Test strvis(3)"); 63 } 64 65 ATF_TC_BODY(strvis_basic, tc) 66 { 67 char *srcbuf, *dstbuf, *visbuf; 68 unsigned int i, j; 69 70 ATF_REQUIRE((dstbuf = malloc(SIZE)) != NULL); 71 ATF_REQUIRE((srcbuf = malloc(SIZE)) != NULL); 72 ATF_REQUIRE((visbuf = malloc(SIZE * 4 + 1)) != NULL); 73 74 for (i = 0; i < SIZE; i++) 75 srcbuf[i] = (char)i; 76 77 for (i = 0; i < __arraycount(styles); i++) { 78 ATF_REQUIRE(strsvisx(visbuf, srcbuf, SIZE, styles[i], "") > 0); 79 memset(dstbuf, 0, SIZE); 80 ATF_REQUIRE(strunvisx(dstbuf, visbuf, 81 styles[i] & (VIS_HTTP1808|VIS_MIMESTYLE)) > 0); 82 for (j = 0; j < SIZE; j++) 83 if (dstbuf[j] != (char)j) 84 atf_tc_fail_nonfatal("Failed for style %x, " 85 "char %d [%d]", styles[i], j, dstbuf[j]); 86 } 87 free(dstbuf); 88 free(srcbuf); 89 free(visbuf); 90 } 91 92 ATF_TC(strvis_null); 93 ATF_TC_HEAD(strvis_null, tc) 94 { 95 atf_tc_set_md_var(tc, "descr", "Test strvis(3) NULL"); 96 } 97 98 ATF_TC_BODY(strvis_null, tc) 99 { 100 char dst[] = "fail"; 101 strvis(dst, NULL, VIS_SAFE); 102 ATF_REQUIRE(dst[0] == '\0' && dst[1] == 'a'); 103 } 104 105 ATF_TC(strvis_empty); 106 ATF_TC_HEAD(strvis_empty, tc) 107 { 108 atf_tc_set_md_var(tc, "descr", "Test strvis(3) empty"); 109 } 110 111 ATF_TC_BODY(strvis_empty, tc) 112 { 113 char dst[] = "fail"; 114 strvis(dst, "", VIS_SAFE); 115 ATF_REQUIRE(dst[0] == '\0' && dst[1] == 'a'); 116 } 117 118 ATF_TC(strunvis_hex); 119 ATF_TC_HEAD(strunvis_hex, tc) 120 { 121 atf_tc_set_md_var(tc, "descr", "Test strunvis(3) \\xXX"); 122 } 123 124 ATF_TC_BODY(strunvis_hex, tc) 125 { 126 static const struct { 127 const char *e; 128 const char *d; 129 int error; 130 } ed[] = { 131 { "\\xff", "\xff", 1 }, 132 { "\\x1", "\x1", 1 }, 133 { "\\x1\\x02", "\x1\x2", 2 }, 134 { "\\x1x", "\x1x", 2 }, 135 { "\\xx", "", -1 }, 136 }; 137 char uv[10]; 138 139 for (size_t i = 0; i < __arraycount(ed); i++) { 140 ATF_REQUIRE(strunvis(uv, ed[i].e) == ed[i].error); 141 if (ed[i].error > 0) 142 ATF_REQUIRE(memcmp(ed[i].d, uv, ed[i].error) == 0); 143 } 144 } 145 146 ATF_TP_ADD_TCS(tp) 147 { 148 149 ATF_TP_ADD_TC(tp, strvis_basic); 150 ATF_TP_ADD_TC(tp, strvis_null); 151 ATF_TP_ADD_TC(tp, strvis_empty); 152 ATF_TP_ADD_TC(tp, strunvis_hex); 153 154 return atf_no_error(); 155 } 156