xref: /freebsd/contrib/netbsd-tests/lib/libc/inet/t_inet_addr.c (revision c22165b4f1f5d38b681921797a44b3ba8c13b7e0)
1*640235e2SEnji Cooper /* $NetBSD: t_inet_addr.c,v 1.1 2015/04/09 16:47:56 ginsbach Exp $ */
2*640235e2SEnji Cooper 
3*640235e2SEnji Cooper /*
4*640235e2SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*640235e2SEnji Cooper  * All rights reserved.
6*640235e2SEnji Cooper  *
7*640235e2SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
8*640235e2SEnji Cooper  *
9*640235e2SEnji Cooper  * Redistribution and use in source and binary forms, with or without
10*640235e2SEnji Cooper  * modification, are permitted provided that the following conditions
11*640235e2SEnji Cooper  * are met:
12*640235e2SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
13*640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
14*640235e2SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
15*640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
16*640235e2SEnji Cooper  *    documentation and/or other materials provided with the distribution.
17*640235e2SEnji Cooper  *
18*640235e2SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*640235e2SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*640235e2SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*640235e2SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*640235e2SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*640235e2SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*640235e2SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*640235e2SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*640235e2SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*640235e2SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*640235e2SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
29*640235e2SEnji Cooper  */
30*640235e2SEnji Cooper 
31*640235e2SEnji Cooper #include <sys/cdefs.h>
32*640235e2SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2011\
33*640235e2SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
34*640235e2SEnji Cooper __RCSID("$NetBSD: t_inet_addr.c,v 1.1 2015/04/09 16:47:56 ginsbach Exp $");
35*640235e2SEnji Cooper 
36*640235e2SEnji Cooper #include <arpa/inet.h>
37*640235e2SEnji Cooper 
38*640235e2SEnji Cooper #include <atf-c.h>
39*640235e2SEnji Cooper #include <stdio.h>
40*640235e2SEnji Cooper #include <string.h>
41*640235e2SEnji Cooper 
42*640235e2SEnji Cooper ATF_TC(inet_addr_basic);
ATF_TC_HEAD(inet_addr_basic,tc)43*640235e2SEnji Cooper ATF_TC_HEAD(inet_addr_basic, tc)
44*640235e2SEnji Cooper {
45*640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks inet_addr(3)");
46*640235e2SEnji Cooper }
47*640235e2SEnji Cooper 
ATF_TC_BODY(inet_addr_basic,tc)48*640235e2SEnji Cooper ATF_TC_BODY(inet_addr_basic, tc)
49*640235e2SEnji Cooper {
50*640235e2SEnji Cooper 	static const char *addrs[] = {
51*640235e2SEnji Cooper 		"127.0.0.1", "99.99.99.99", "0.0.0.0", "255.255.255.255" };
52*640235e2SEnji Cooper 
53*640235e2SEnji Cooper 	struct in_addr ia;
54*640235e2SEnji Cooper 	const char *ian;
55*640235e2SEnji Cooper 	in_addr_t addr;
56*640235e2SEnji Cooper 	size_t i;
57*640235e2SEnji Cooper 
58*640235e2SEnji Cooper 	for (i = 0; i < __arraycount(addrs); i++) {
59*640235e2SEnji Cooper 
60*640235e2SEnji Cooper 		(void)fprintf(stderr, "checking %s\n", addrs[i]);;
61*640235e2SEnji Cooper 
62*640235e2SEnji Cooper 		addr = inet_addr(addrs[i]);
63*640235e2SEnji Cooper 		ia.s_addr = addr;
64*640235e2SEnji Cooper 		ian = inet_ntoa(ia);
65*640235e2SEnji Cooper 
66*640235e2SEnji Cooper 		ATF_REQUIRE(ian != NULL);
67*640235e2SEnji Cooper 		ATF_CHECK(strcmp(ian, addrs[i]) == 0);
68*640235e2SEnji Cooper 	}
69*640235e2SEnji Cooper }
70*640235e2SEnji Cooper 
71*640235e2SEnji Cooper ATF_TC(inet_addr_err);
ATF_TC_HEAD(inet_addr_err,tc)72*640235e2SEnji Cooper ATF_TC_HEAD(inet_addr_err, tc)
73*640235e2SEnji Cooper {
74*640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Invalid addresses with inet_addr(3)");
75*640235e2SEnji Cooper }
76*640235e2SEnji Cooper 
ATF_TC_BODY(inet_addr_err,tc)77*640235e2SEnji Cooper ATF_TC_BODY(inet_addr_err, tc)
78*640235e2SEnji Cooper {
79*640235e2SEnji Cooper 	static const char *addrs[] = {
80*640235e2SEnji Cooper 		". . . .", "1.2.3.", "0.0.0.256", "255.255.255.256",
81*640235e2SEnji Cooper 		"................................................",
82*640235e2SEnji Cooper 		"a.b.c.d", "0x0.0x1.0x2.0x3", "-1.-1.-1.-1", "", " "};
83*640235e2SEnji Cooper 
84*640235e2SEnji Cooper 	struct in_addr ia;
85*640235e2SEnji Cooper 	const char *ian;
86*640235e2SEnji Cooper 	in_addr_t addr;
87*640235e2SEnji Cooper 	size_t i;
88*640235e2SEnji Cooper 
89*640235e2SEnji Cooper 	for (i = 0; i < __arraycount(addrs); i++) {
90*640235e2SEnji Cooper 
91*640235e2SEnji Cooper 		(void)fprintf(stderr, "checking %s\n", addrs[i]);;
92*640235e2SEnji Cooper 
93*640235e2SEnji Cooper 		addr = inet_addr(addrs[i]);
94*640235e2SEnji Cooper 		ia.s_addr = addr;
95*640235e2SEnji Cooper 		ian = inet_ntoa(ia);
96*640235e2SEnji Cooper 
97*640235e2SEnji Cooper 		ATF_REQUIRE(ian != NULL);
98*640235e2SEnji Cooper 		ATF_CHECK(strcmp(ian, addrs[i]) != 0);
99*640235e2SEnji Cooper 	}
100*640235e2SEnji Cooper }
101*640235e2SEnji Cooper 
ATF_TP_ADD_TCS(tp)102*640235e2SEnji Cooper ATF_TP_ADD_TCS(tp)
103*640235e2SEnji Cooper {
104*640235e2SEnji Cooper 
105*640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, inet_addr_basic);
106*640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, inet_addr_err);
107*640235e2SEnji Cooper 
108*640235e2SEnji Cooper 	return atf_no_error();
109*640235e2SEnji Cooper }
110