1 /* $NetBSD: t_inet_network.c,v 1.3 2011/07/15 11:27:23 jruoho Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34 The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_inet_network.c,v 1.3 2011/07/15 11:27:23 jruoho Exp $");
37 #include <arpa/inet.h>
43 #define H_REQUIRE(input, expected) \
44 ATF_REQUIRE_EQ_MSG(inet_network(input), (in_addr_t) expected, \
45 "inet_network(%s) returned: 0x%08X, expected: %s", #input, \
46 inet_network(input), #expected)
48 ATF_TC(inet_addr_basic
);
49 ATF_TC_HEAD(inet_addr_basic
, tc
)
51 atf_tc_set_md_var(tc
, "descr", "Checks inet_addr(3)");
54 ATF_TC_BODY(inet_addr_basic
, tc
)
56 static const char *addrs
[] = {
57 "127.0.0.1", "99.99.99.99", "0.0.0.0", "255.255.255.255" };
64 for (i
= 0; i
< __arraycount(addrs
); i
++) {
66 (void)fprintf(stderr
, "checking %s\n", addrs
[i
]);;
68 addr
= inet_addr(addrs
[i
]);
72 ATF_REQUIRE(ian
!= NULL
);
73 ATF_CHECK(strcmp(ian
, addrs
[i
]) == 0);
77 ATF_TC(inet_addr_err
);
78 ATF_TC_HEAD(inet_addr_err
, tc
)
80 atf_tc_set_md_var(tc
, "descr", "Invalid addresses with inet_addr(3)");
83 ATF_TC_BODY(inet_addr_err
, tc
)
85 static const char *addrs
[] = {
86 ". . . .", "1.2.3.", "0.0.0.256", "255.255.255.256",
87 "................................................",
88 "a.b.c.d", "0x0.0x1.0x2.0x3", "-1.-1.-1.-1", "", " "};
95 for (i
= 0; i
< __arraycount(addrs
); i
++) {
97 (void)fprintf(stderr
, "checking %s\n", addrs
[i
]);;
99 addr
= inet_addr(addrs
[i
]);
103 ATF_REQUIRE(ian
!= NULL
);
104 ATF_CHECK(strcmp(ian
, addrs
[i
]) != 0);
108 ATF_TC(inet_network_basic
);
109 ATF_TC_HEAD(inet_network_basic
, tc
)
111 atf_tc_set_md_var(tc
, "descr", "Checks inet_network(3)");
114 ATF_TC_BODY(inet_network_basic
, tc
)
117 H_REQUIRE("0x12", 0x00000012);
118 H_REQUIRE("127.1", 0x00007f01);
119 H_REQUIRE("127.1.2.3", 0x7f010203);
120 H_REQUIRE("0X12", 0x00000012);
122 H_REQUIRE("01.02.07.077", 0x0102073f);
123 H_REQUIRE("0x1.23.045.0", 0x01172500);
124 H_REQUIRE("0x12.0x34", 0x00001234);
126 /* This is valid (because of the trailing space after the digit). */
127 H_REQUIRE("1 bar", 0x00000001);
130 ATF_TC(inet_network_err
);
131 ATF_TC_HEAD(inet_network_err
, tc
)
133 atf_tc_set_md_var(tc
, "descr", "Invalid addresses w/ inet_network(3)");
136 ATF_TC_BODY(inet_network_err
, tc
)
138 /* Malformed requests. */
139 H_REQUIRE("4.2.3.1.", 0xffffffff);
140 H_REQUIRE("0x123456", 0xffffffff);
141 H_REQUIRE("0x12.0x345", 0xffffffff);
142 H_REQUIRE("1.2.3.4.5", 0xffffffff);
143 H_REQUIRE("1..3.4", 0xffffffff);
144 H_REQUIRE(".", 0xffffffff);
145 H_REQUIRE("1.", 0xffffffff);
146 H_REQUIRE(".1", 0xffffffff);
147 H_REQUIRE("0x", 0xffffffff);
148 H_REQUIRE("", 0xffffffff);
149 H_REQUIRE(" ", 0xffffffff);
150 H_REQUIRE("bar", 0xffffffff);
151 H_REQUIRE("1.2bar", 0xffffffff);
152 H_REQUIRE("1.", 0xffffffff);
153 H_REQUIRE("\xc3\x8a\xc3\x83\xc3\x95\xc3\x8b\xc3\x85\xc3\x8e",
155 H_REQUIRE("255.255.255.255", 0xffffffff);
156 H_REQUIRE("x", 0xffffffff);
157 H_REQUIRE("078", 0xffffffff);
158 H_REQUIRE("127.0xfff", 0xffffffff);
164 ATF_TP_ADD_TC(tp
, inet_addr_basic
);
165 ATF_TP_ADD_TC(tp
, inet_addr_err
);
166 ATF_TP_ADD_TC(tp
, inet_network_basic
);
167 ATF_TP_ADD_TC(tp
, inet_network_err
);
169 return atf_no_error();