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