xref: /freebsd/contrib/netbsd-tests/lib/libc/inet/t_inet_addr.c (revision c22165b4f1f5d38b681921797a44b3ba8c13b7e0)
1 /* $NetBSD: t_inet_addr.c,v 1.1 2015/04/09 16:47:56 ginsbach Exp $ */
2 
3 /*
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
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) 2011\
33  The NetBSD Foundation, inc. All rights reserved.");
34 __RCSID("$NetBSD: t_inet_addr.c,v 1.1 2015/04/09 16:47:56 ginsbach Exp $");
35 
36 #include <arpa/inet.h>
37 
38 #include <atf-c.h>
39 #include <stdio.h>
40 #include <string.h>
41 
42 ATF_TC(inet_addr_basic);
ATF_TC_HEAD(inet_addr_basic,tc)43 ATF_TC_HEAD(inet_addr_basic, tc)
44 {
45 	atf_tc_set_md_var(tc, "descr", "Checks inet_addr(3)");
46 }
47 
ATF_TC_BODY(inet_addr_basic,tc)48 ATF_TC_BODY(inet_addr_basic, tc)
49 {
50 	static const char *addrs[] = {
51 		"127.0.0.1", "99.99.99.99", "0.0.0.0", "255.255.255.255" };
52 
53 	struct in_addr ia;
54 	const char *ian;
55 	in_addr_t addr;
56 	size_t i;
57 
58 	for (i = 0; i < __arraycount(addrs); i++) {
59 
60 		(void)fprintf(stderr, "checking %s\n", addrs[i]);;
61 
62 		addr = inet_addr(addrs[i]);
63 		ia.s_addr = addr;
64 		ian = inet_ntoa(ia);
65 
66 		ATF_REQUIRE(ian != NULL);
67 		ATF_CHECK(strcmp(ian, addrs[i]) == 0);
68 	}
69 }
70 
71 ATF_TC(inet_addr_err);
ATF_TC_HEAD(inet_addr_err,tc)72 ATF_TC_HEAD(inet_addr_err, tc)
73 {
74 	atf_tc_set_md_var(tc, "descr", "Invalid addresses with inet_addr(3)");
75 }
76 
ATF_TC_BODY(inet_addr_err,tc)77 ATF_TC_BODY(inet_addr_err, tc)
78 {
79 	static const char *addrs[] = {
80 		". . . .", "1.2.3.", "0.0.0.256", "255.255.255.256",
81 		"................................................",
82 		"a.b.c.d", "0x0.0x1.0x2.0x3", "-1.-1.-1.-1", "", " "};
83 
84 	struct in_addr ia;
85 	const char *ian;
86 	in_addr_t addr;
87 	size_t i;
88 
89 	for (i = 0; i < __arraycount(addrs); i++) {
90 
91 		(void)fprintf(stderr, "checking %s\n", addrs[i]);;
92 
93 		addr = inet_addr(addrs[i]);
94 		ia.s_addr = addr;
95 		ian = inet_ntoa(ia);
96 
97 		ATF_REQUIRE(ian != NULL);
98 		ATF_CHECK(strcmp(ian, addrs[i]) != 0);
99 	}
100 }
101 
ATF_TP_ADD_TCS(tp)102 ATF_TP_ADD_TCS(tp)
103 {
104 
105 	ATF_TP_ADD_TC(tp, inet_addr_basic);
106 	ATF_TP_ADD_TC(tp, inet_addr_err);
107 
108 	return atf_no_error();
109 }
110