xref: /freebsd/contrib/netbsd-tests/lib/libutil/t_sockaddr_snprintf.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /* $NetBSD: t_sockaddr_snprintf.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*
4*57718be8SEnji Cooper  * Copyright (c) 2002, 2004, 2008, 2010 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
8*57718be8SEnji Cooper  *
9*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
10*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
11*57718be8SEnji Cooper  * are met:
12*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
13*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
14*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
15*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
16*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
17*57718be8SEnji Cooper  *
18*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
29*57718be8SEnji Cooper  */
30*57718be8SEnji Cooper 
31*57718be8SEnji Cooper #include <sys/cdefs.h>
32*57718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
33*57718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
34*57718be8SEnji Cooper __RCSID("$NetBSD: t_sockaddr_snprintf.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $");
35*57718be8SEnji Cooper 
36*57718be8SEnji Cooper #include <sys/socket.h>		/* AF_ */
37*57718be8SEnji Cooper #include <sys/un.h>			/* sun */
38*57718be8SEnji Cooper 
39*57718be8SEnji Cooper #include <net/if_dl.h>		/* sdl */
40*57718be8SEnji Cooper #include <netatalk/at.h>	/* sat */
41*57718be8SEnji Cooper #include <netinet/in.h>		/* sin/sin6 */
42*57718be8SEnji Cooper 
43*57718be8SEnji Cooper #include <string.h>
44*57718be8SEnji Cooper #include <util.h>
45*57718be8SEnji Cooper 
46*57718be8SEnji Cooper #include <atf-c.h>
47*57718be8SEnji Cooper 
48*57718be8SEnji Cooper ATF_TC(sockaddr_snprintf_in);
ATF_TC_HEAD(sockaddr_snprintf_in,tc)49*57718be8SEnji Cooper ATF_TC_HEAD(sockaddr_snprintf_in, tc)
50*57718be8SEnji Cooper {
51*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
52*57718be8SEnji Cooper 		"Checks sockaddr_snprintf(3) with sockaddr_in");
53*57718be8SEnji Cooper }
ATF_TC_BODY(sockaddr_snprintf_in,tc)54*57718be8SEnji Cooper ATF_TC_BODY(sockaddr_snprintf_in, tc)
55*57718be8SEnji Cooper {
56*57718be8SEnji Cooper 	char buf[1024];
57*57718be8SEnji Cooper 	struct sockaddr_in sin4;
58*57718be8SEnji Cooper 	int i;
59*57718be8SEnji Cooper 
60*57718be8SEnji Cooper 	memset(&sin4, 0, sizeof(sin4));
61*57718be8SEnji Cooper 	sin4.sin_len = sizeof(sin4);
62*57718be8SEnji Cooper 	sin4.sin_family = AF_INET;
63*57718be8SEnji Cooper 	sin4.sin_port = ntohs(80);
64*57718be8SEnji Cooper 	sin4.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
65*57718be8SEnji Cooper 	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %p %a",
66*57718be8SEnji Cooper 		(struct sockaddr *)&sin4);
67*57718be8SEnji Cooper 
68*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(i, 17, "bad length for sin4");
69*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(buf, "2 16 80 127.0.0.1");
70*57718be8SEnji Cooper }
71*57718be8SEnji Cooper 
72*57718be8SEnji Cooper ATF_TC(sockaddr_snprintf_in6);
ATF_TC_HEAD(sockaddr_snprintf_in6,tc)73*57718be8SEnji Cooper ATF_TC_HEAD(sockaddr_snprintf_in6, tc)
74*57718be8SEnji Cooper {
75*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
76*57718be8SEnji Cooper 		"Checks sockaddr_snprintf(3) with sockaddr_in6");
77*57718be8SEnji Cooper }
ATF_TC_BODY(sockaddr_snprintf_in6,tc)78*57718be8SEnji Cooper ATF_TC_BODY(sockaddr_snprintf_in6, tc)
79*57718be8SEnji Cooper {
80*57718be8SEnji Cooper #ifdef INET6
81*57718be8SEnji Cooper 	char buf[1024];
82*57718be8SEnji Cooper 	struct sockaddr_in6 sin6;
83*57718be8SEnji Cooper 	int i;
84*57718be8SEnji Cooper 
85*57718be8SEnji Cooper 	memset(&sin6, 0, sizeof(sin6));
86*57718be8SEnji Cooper 	sin6.sin6_len = sizeof(sin6);
87*57718be8SEnji Cooper 	sin6.sin6_family = AF_INET6;
88*57718be8SEnji Cooper 	sin6.sin6_port = ntohs(80);
89*57718be8SEnji Cooper 	sin6.sin6_addr = in6addr_nodelocal_allnodes;
90*57718be8SEnji Cooper 	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %p %a",
91*57718be8SEnji Cooper 		(struct sockaddr *)&sin6);
92*57718be8SEnji Cooper 
93*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(i, 16, "bad length for sin6");
94*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(buf, "24 28 80 ff01::1");
95*57718be8SEnji Cooper #else
96*57718be8SEnji Cooper 	atf_tc_skip("Tests built with USE_INET6=no");
97*57718be8SEnji Cooper #endif /* INET6 */
98*57718be8SEnji Cooper }
99*57718be8SEnji Cooper 
100*57718be8SEnji Cooper ATF_TC(sockaddr_snprintf_un);
ATF_TC_HEAD(sockaddr_snprintf_un,tc)101*57718be8SEnji Cooper ATF_TC_HEAD(sockaddr_snprintf_un, tc)
102*57718be8SEnji Cooper {
103*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
104*57718be8SEnji Cooper 		"Checks sockaddr_snprintf(3) with sockaddr_un");
105*57718be8SEnji Cooper }
ATF_TC_BODY(sockaddr_snprintf_un,tc)106*57718be8SEnji Cooper ATF_TC_BODY(sockaddr_snprintf_un, tc)
107*57718be8SEnji Cooper {
108*57718be8SEnji Cooper 	char buf[1024];
109*57718be8SEnji Cooper 	struct sockaddr_un sun;
110*57718be8SEnji Cooper 	int i;
111*57718be8SEnji Cooper 
112*57718be8SEnji Cooper 	memset(&sun, 0, sizeof(sun));
113*57718be8SEnji Cooper 	sun.sun_len = sizeof(sun);
114*57718be8SEnji Cooper 	sun.sun_family = AF_UNIX;
115*57718be8SEnji Cooper 	strncpy(sun.sun_path, "/tmp/sock", sizeof(sun.sun_path));
116*57718be8SEnji Cooper 	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
117*57718be8SEnji Cooper 		(struct sockaddr *)&sun);
118*57718be8SEnji Cooper 
119*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(i, 15, "bad length for sun");
120*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(buf, "1 106 /tmp/sock");
121*57718be8SEnji Cooper }
122*57718be8SEnji Cooper 
123*57718be8SEnji Cooper ATF_TC(sockaddr_snprintf_at);
ATF_TC_HEAD(sockaddr_snprintf_at,tc)124*57718be8SEnji Cooper ATF_TC_HEAD(sockaddr_snprintf_at, tc)
125*57718be8SEnji Cooper {
126*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
127*57718be8SEnji Cooper 		"Checks sockaddr_snprintf(3) with sockaddr_at");
128*57718be8SEnji Cooper }
ATF_TC_BODY(sockaddr_snprintf_at,tc)129*57718be8SEnji Cooper ATF_TC_BODY(sockaddr_snprintf_at, tc)
130*57718be8SEnji Cooper {
131*57718be8SEnji Cooper 	char buf[1024];
132*57718be8SEnji Cooper 	struct sockaddr_at sat;
133*57718be8SEnji Cooper 	int i;
134*57718be8SEnji Cooper 
135*57718be8SEnji Cooper 	memset(&sat, 0, sizeof(sat));
136*57718be8SEnji Cooper 	sat.sat_len = sizeof(sat);
137*57718be8SEnji Cooper 	sat.sat_family = AF_APPLETALK;
138*57718be8SEnji Cooper 	sat.sat_addr.s_net = ntohs(101);
139*57718be8SEnji Cooper 	sat.sat_addr.s_node = 3;
140*57718be8SEnji Cooper 	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
141*57718be8SEnji Cooper 		(struct sockaddr *)&sat);
142*57718be8SEnji Cooper 
143*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(i, 11, "bad length for sat");
144*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(buf, "16 16 101.3");
145*57718be8SEnji Cooper }
146*57718be8SEnji Cooper 
147*57718be8SEnji Cooper ATF_TC(sockaddr_snprintf_dl);
ATF_TC_HEAD(sockaddr_snprintf_dl,tc)148*57718be8SEnji Cooper ATF_TC_HEAD(sockaddr_snprintf_dl, tc)
149*57718be8SEnji Cooper {
150*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
151*57718be8SEnji Cooper 		"Checks sockaddr_snprintf(3) with sockaddr_dl");
152*57718be8SEnji Cooper }
ATF_TC_BODY(sockaddr_snprintf_dl,tc)153*57718be8SEnji Cooper ATF_TC_BODY(sockaddr_snprintf_dl, tc)
154*57718be8SEnji Cooper {
155*57718be8SEnji Cooper 	char buf[1024];
156*57718be8SEnji Cooper 	struct sockaddr_dl sdl;
157*57718be8SEnji Cooper 	int i;
158*57718be8SEnji Cooper 
159*57718be8SEnji Cooper 	memset(&sdl, 0, sizeof(sdl));
160*57718be8SEnji Cooper 	sdl.sdl_len = sizeof(sdl);
161*57718be8SEnji Cooper 	sdl.sdl_family = AF_LINK;
162*57718be8SEnji Cooper 	sdl.sdl_index = 0;
163*57718be8SEnji Cooper 	sdl.sdl_type = 0;
164*57718be8SEnji Cooper 	sdl.sdl_nlen = 0;
165*57718be8SEnji Cooper 	sdl.sdl_alen = 6;
166*57718be8SEnji Cooper 	sdl.sdl_slen = 0;
167*57718be8SEnji Cooper 	memcpy(sdl.sdl_data, "\01\02\03\04\05\06", 6);
168*57718be8SEnji Cooper 	i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
169*57718be8SEnji Cooper 		(struct sockaddr *)&sdl);
170*57718be8SEnji Cooper 
171*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(i, 17, "bad length for sdl");
172*57718be8SEnji Cooper 	ATF_REQUIRE_STREQ(buf, "18 20 1.2.3.4.5.6");
173*57718be8SEnji Cooper }
174*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)175*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
176*57718be8SEnji Cooper {
177*57718be8SEnji Cooper 
178*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sockaddr_snprintf_in);
179*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sockaddr_snprintf_in6);
180*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sockaddr_snprintf_un);
181*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sockaddr_snprintf_at);
182*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, sockaddr_snprintf_dl);
183*57718be8SEnji Cooper 
184*57718be8SEnji Cooper 	return atf_no_error();
185*57718be8SEnji Cooper }
186