Home | History | Annotate | Line # | Download | only in netinet
      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 "netinet/in_print.c"
     35  1.1  christos 
     36  1.1  christos #include <atf-c.h>
     37  1.1  christos 
     38  1.1  christos static const struct {
     39  1.1  christos 	struct in_addr ia;
     40  1.1  christos 	const char *str;
     41  1.1  christos 	int len;
     42  1.1  christos } tst[] = {
     43  1.1  christos 	{
     44  1.1  christos 		{	.s_addr = ntohl(INADDR_LOOPBACK)	},
     45  1.1  christos 		"127.0.0.1",
     46  1.1  christos 		9,
     47  1.1  christos 	},
     48  1.1  christos 	{
     49  1.1  christos 		{	.s_addr = ntohl(INADDR_ANY)		},
     50  1.1  christos 		"0.0.0.0",
     51  1.1  christos 		7,
     52  1.1  christos 	},
     53  1.1  christos 	{
     54  1.1  christos 		{	.s_addr = ntohl(IN_CLASSC_NET)		},
     55  1.1  christos 		"255.255.255.0",
     56  1.1  christos 		13,
     57  1.1  christos 	},
     58  1.1  christos 	{
     59  1.1  christos 		{	.s_addr = ntohl(INADDR_ALLHOSTS_GROUP)	},
     60  1.1  christos 		"224.0.0.1",
     61  1.1  christos 		9,
     62  1.1  christos 	},
     63  1.1  christos };
     64  1.1  christos 
     65  1.1  christos 
     66  1.1  christos ATF_TC(in_print);
     67  1.1  christos ATF_TC_HEAD(in_print, tc)
     68  1.1  christos {
     69  1.1  christos 
     70  1.1  christos 	atf_tc_set_md_var(tc, "descr", "printing of struct in_addr");
     71  1.1  christos }
     72  1.1  christos 
     73  1.1  christos ATF_TC_BODY(in_print, tc)
     74  1.1  christos {
     75  1.1  christos 	char buf[INET_ADDRSTRLEN];
     76  1.1  christos 	int r;
     77  1.1  christos 	size_t l = sizeof(buf);
     78  1.1  christos 
     79  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
     80  1.1  christos 		r = in_print(buf, l, &tst[i].ia);
     81  1.1  christos 		ATF_REQUIRE_STREQ(buf, tst[i].str);
     82  1.1  christos 		ATF_REQUIRE_EQ(r, tst[i].len);
     83  1.1  christos 	}
     84  1.1  christos 
     85  1.1  christos 	l = 8;
     86  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
     87  1.1  christos 		r = in_print(buf, l, &tst[i].ia);
     88  1.1  christos 		ATF_CHECK(strncmp(buf, tst[i].str, l - 1) == 0);
     89  1.1  christos 		ATF_REQUIRE_EQ(buf[l - 1], '\0');
     90  1.1  christos 		ATF_REQUIRE_EQ(r, tst[i].len);
     91  1.1  christos 	}
     92  1.1  christos }
     93  1.1  christos 
     94  1.1  christos ATF_TC(sin_print);
     95  1.1  christos ATF_TC_HEAD(sin_print, tc)
     96  1.1  christos {
     97  1.1  christos 
     98  1.1  christos 	atf_tc_set_md_var(tc, "descr", "printing of sockaddr_in");
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos ATF_TC_BODY(sin_print, tc)
    102  1.1  christos {
    103  1.1  christos 	char buf[1024];
    104  1.1  christos 	char res[1024];
    105  1.1  christos 	int r, e;
    106  1.1  christos 	size_t l = sizeof(buf);
    107  1.1  christos 	struct sockaddr_in sin;
    108  1.1  christos 	memset(&sin, 0, sizeof(sin));
    109  1.1  christos 
    110  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
    111  1.1  christos 		sin.sin_addr = tst[i].ia;
    112  1.2  christos 		sin.sin_port = (in_port_t)htons(i);
    113  1.1  christos 		r = sin_print(buf, l, &sin);
    114  1.1  christos 		if (i == 0)
    115  1.1  christos 			e = snprintf(res, sizeof(res), "%s", tst[i].str);
    116  1.1  christos 		else
    117  1.1  christos 			e = snprintf(res, sizeof(res), "%s:%zu", tst[i].str, i);
    118  1.1  christos 
    119  1.1  christos 		ATF_REQUIRE_STREQ(buf, res);
    120  1.1  christos 		ATF_REQUIRE_EQ(r, e);
    121  1.1  christos 	}
    122  1.1  christos 
    123  1.1  christos 	l = 14;
    124  1.1  christos 	for (size_t i = 0; i < __arraycount(tst); i++) {
    125  1.1  christos 		sin.sin_addr = tst[i].ia;
    126  1.2  christos 		sin.sin_port = (in_port_t)htons(i);
    127  1.1  christos 		r = sin_print(buf, l, &sin);
    128  1.1  christos 		if (i == 0)
    129  1.1  christos 			e = snprintf(res, l, "%s", tst[i].str);
    130  1.1  christos 		else
    131  1.1  christos 			e = snprintf(res, l, "%s:%zu", tst[i].str, i);
    132  1.1  christos 
    133  1.1  christos 		ATF_REQUIRE_STREQ(buf, res);
    134  1.1  christos 		ATF_REQUIRE_EQ(r, e);
    135  1.1  christos 	}
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos ATF_TP_ADD_TCS(tp)
    139  1.1  christos {
    140  1.1  christos 
    141  1.1  christos 	ATF_TP_ADD_TC(tp, in_print);
    142  1.1  christos 	ATF_TP_ADD_TC(tp, sin_print);
    143  1.1  christos 	return atf_no_error();
    144  1.1  christos }
    145