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