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