1 /* $NetBSD: t_sockaddr_snprintf.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $ */
4 * Copyright (c) 2002, 2004, 2008, 2010 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Christos Zoulas.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
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 $");
36 #include <sys/socket.h> /* AF_ */
37 #include <sys/un.h> /* sun */
39 #include <net/if_dl.h> /* sdl */
40 #include <netatalk/at.h> /* sat */
41 #include <netinet/in.h> /* sin/sin6 */
48 ATF_TC(sockaddr_snprintf_in
);
49 ATF_TC_HEAD(sockaddr_snprintf_in
, tc
)
51 atf_tc_set_md_var(tc
, "descr",
52 "Checks sockaddr_snprintf(3) with sockaddr_in");
54 ATF_TC_BODY(sockaddr_snprintf_in
, tc
)
57 struct sockaddr_in sin4
;
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
);
68 ATF_REQUIRE_EQ_MSG(i
, 17, "bad length for sin4");
69 ATF_REQUIRE_STREQ(buf
, "2 16 80 127.0.0.1");
72 ATF_TC(sockaddr_snprintf_in6
);
73 ATF_TC_HEAD(sockaddr_snprintf_in6
, tc
)
75 atf_tc_set_md_var(tc
, "descr",
76 "Checks sockaddr_snprintf(3) with sockaddr_in6");
78 ATF_TC_BODY(sockaddr_snprintf_in6
, tc
)
82 struct sockaddr_in6 sin6
;
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
);
93 ATF_REQUIRE_EQ_MSG(i
, 16, "bad length for sin6");
94 ATF_REQUIRE_STREQ(buf
, "24 28 80 ff01::1");
96 atf_tc_skip("Tests built with USE_INET6=no");
100 ATF_TC(sockaddr_snprintf_un
);
101 ATF_TC_HEAD(sockaddr_snprintf_un
, tc
)
103 atf_tc_set_md_var(tc
, "descr",
104 "Checks sockaddr_snprintf(3) with sockaddr_un");
106 ATF_TC_BODY(sockaddr_snprintf_un
, tc
)
109 struct sockaddr_un sun
;
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
);
119 ATF_REQUIRE_EQ_MSG(i
, 15, "bad length for sun");
120 ATF_REQUIRE_STREQ(buf
, "1 106 /tmp/sock");
123 ATF_TC(sockaddr_snprintf_at
);
124 ATF_TC_HEAD(sockaddr_snprintf_at
, tc
)
126 atf_tc_set_md_var(tc
, "descr",
127 "Checks sockaddr_snprintf(3) with sockaddr_at");
129 ATF_TC_BODY(sockaddr_snprintf_at
, tc
)
132 struct sockaddr_at sat
;
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
);
143 ATF_REQUIRE_EQ_MSG(i
, 11, "bad length for sat");
144 ATF_REQUIRE_STREQ(buf
, "16 16 101.3");
147 ATF_TC(sockaddr_snprintf_dl
);
148 ATF_TC_HEAD(sockaddr_snprintf_dl
, tc
)
150 atf_tc_set_md_var(tc
, "descr",
151 "Checks sockaddr_snprintf(3) with sockaddr_dl");
153 ATF_TC_BODY(sockaddr_snprintf_dl
, tc
)
156 struct sockaddr_dl sdl
;
159 memset(&sdl
, 0, sizeof(sdl
));
160 sdl
.sdl_len
= sizeof(sdl
);
161 sdl
.sdl_family
= AF_LINK
;
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
);
171 ATF_REQUIRE_EQ_MSG(i
, 17, "bad length for sdl");
172 ATF_REQUIRE_STREQ(buf
, "18 20 1.2.3.4.5.6");
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
);
184 return atf_no_error();