Home | History | Annotate | Line # | Download | only in netinet6
t_print.c revision 1.2
      1  1.2  christos /*	$NetBSD: t_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.2  christos __RCSID("$NetBSD: t_print.c,v 1.2 2014/12/03 13:10:49 christos Exp $");
     33  1.1  christos 
     34  1.1  christos #include "netinet6/in6_print.c"
     35  1.1  christos #include "netinet/in_print.c"
     36  1.1  christos 
     37  1.1  christos #include <atf-c.h>
     38  1.1  christos 
     39  1.1  christos static const struct {
     40  1.1  christos 	struct in6_addr ia;
     41  1.1  christos 	const char *str;
     42  1.1  christos 	int len;
     43  1.1  christos } tst[] = {
     44  1.1  christos 	{
     45  1.1  christos 		IN6ADDR_ANY_INIT,
     46  1.1  christos 		"::",
     47  1.1  christos 		2,
     48  1.1  christos 	},
     49  1.1  christos 	{
     50  1.1  christos 		IN6ADDR_LOOPBACK_INIT,
     51  1.1  christos 		"::1",
     52  1.1  christos 		3,
     53  1.1  christos 	},
     54  1.1  christos 	{
     55  1.1  christos 		IN6ADDR_NODELOCAL_ALLNODES_INIT,
     56  1.1  christos 		"ff01::1",
     57  1.1  christos 		7,
     58  1.1  christos 	},
     59  1.1  christos 	{
     60  1.1  christos 		{{{ 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
     61  1.1  christos 		    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }}},
     62  1.1  christos 		"1020:3040:5060:7080:102:304:506:708",
     63  1.1  christos 		35,
     64  1.1  christos 	},
     65  1.1  christos 	{
     66  1.1  christos 		{{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     67  1.1  christos 		    0x00, 0x00, 0xff, 0xff, 0x88, 0x44, 0x22, 0x11 }}},
     68  1.1  christos 		"::ffff:136.68.34.17",
     69  1.1  christos 		19,
     70  1.1  christos 	},
     71  1.1  christos };
     72  1.1  christos 
     73  1.1  christos 
     74  1.1  christos ATF_TC(in6_print);
     75  1.1  christos ATF_TC_HEAD(in6_print, tc)
     76  1.1  christos {
     77  1.1  christos 
     78  1.1  christos 	atf_tc_set_md_var(tc, "descr", "printing of struct in6_addr");
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos ATF_TC_BODY(in6_print, tc)
     82  1.1  christos {
     83  1.1  christos 	char buf[INET6_ADDRSTRLEN];
     84  1.1  christos 	int r;
     85  1.1  christos 	size_t l = sizeof(buf);
     86  1.1  christos 
     87  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
     88  1.1  christos 		r = in6_print(buf, l, &tst[i].ia);
     89  1.1  christos 		ATF_REQUIRE_STREQ(buf, tst[i].str);
     90  1.1  christos 		ATF_REQUIRE_EQ(r, tst[i].len);
     91  1.1  christos 	}
     92  1.1  christos 
     93  1.1  christos 	l = 12;
     94  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
     95  1.1  christos 		r = in6_print(buf, l, &tst[i].ia);
     96  1.1  christos 		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
     97  1.1  christos 		if (r > (int)l)
     98  1.1  christos 			ATF_REQUIRE_EQ(buf[l - 1], '\0');
     99  1.1  christos 		ATF_REQUIRE_EQ(r, tst[i].len);
    100  1.1  christos 	}
    101  1.1  christos }
    102  1.1  christos 
    103  1.1  christos ATF_TC(sin6_print);
    104  1.1  christos ATF_TC_HEAD(sin6_print, tc)
    105  1.1  christos {
    106  1.1  christos 
    107  1.1  christos 	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_in6");
    108  1.1  christos }
    109  1.1  christos 
    110  1.1  christos ATF_TC_BODY(sin6_print, tc)
    111  1.1  christos {
    112  1.1  christos 	char buf[1024];
    113  1.1  christos 	char res[1024];
    114  1.1  christos 	int r, e;
    115  1.1  christos 	size_t l = sizeof(buf);
    116  1.1  christos 	struct sockaddr_in6 sin6;
    117  1.1  christos 	memset(&sin6, 0, sizeof(sin6));
    118  1.1  christos 
    119  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
    120  1.1  christos 		sin6.sin6_addr = tst[i].ia;
    121  1.2  christos 		sin6.sin6_port = (in_port_t)htons(i);
    122  1.1  christos 		r = sin6_print(buf, l, &sin6);
    123  1.1  christos 		if (i == 0)
    124  1.1  christos 			e = snprintf(res, sizeof(res), "%s", tst[i].str);
    125  1.1  christos 		else
    126  1.1  christos 			e = snprintf(res, sizeof(res), "[%s]:%zu",
    127  1.1  christos 			    tst[i].str, i);
    128  1.1  christos 
    129  1.1  christos 		ATF_REQUIRE_STREQ(buf, res);
    130  1.1  christos 		ATF_REQUIRE_EQ(r, e);
    131  1.1  christos 	}
    132  1.1  christos 
    133  1.1  christos 	l = 14;
    134  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
    135  1.1  christos 		sin6.sin6_addr = tst[i].ia;
    136  1.2  christos 		sin6.sin6_port = (in_port_t)htons(i);
    137  1.1  christos 		r = sin6_print(buf, l, &sin6);
    138  1.1  christos 		if (i == 0)
    139  1.1  christos 			e = snprintf(res, l, "%s", tst[i].str);
    140  1.1  christos 		else
    141  1.1  christos 			e = snprintf(res, l, "[%s]:%zu", tst[i].str, i);
    142  1.1  christos 
    143  1.1  christos 		ATF_REQUIRE_STREQ(buf, res);
    144  1.1  christos 		ATF_REQUIRE_EQ(r, e);
    145  1.1  christos 	}
    146  1.1  christos }
    147  1.1  christos 
    148  1.1  christos ATF_TP_ADD_TCS(tp)
    149  1.1  christos {
    150  1.1  christos 
    151  1.1  christos 	ATF_TP_ADD_TC(tp, in6_print);
    152  1.1  christos 	ATF_TP_ADD_TC(tp, sin6_print);
    153  1.1  christos 	return atf_no_error();
    154  1.1  christos }
    155