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