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