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