xref: /minix/tests/lib/libc/inet/t_inet_network.c (revision 0a6a1f1d)
1 /* $NetBSD: t_inet_network.c,v 1.4 2015/04/09 16:47:56 ginsbach Exp $ */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Brian Ginsbach.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 
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.4 2015/04/09 16:47:56 ginsbach Exp $");
36 
37 #include <arpa/inet.h>
38 
39 #include <atf-c.h>
40 
41 #define H_REQUIRE(input, expected)					\
42 	ATF_REQUIRE_EQ_MSG(inet_network(input), (in_addr_t) expected,	\
43 	    "inet_network(%s) returned: 0x%08X, expected: %s", #input,	\
44 	    inet_network(input), #expected)
45 
46 ATF_TC(inet_network_basic);
ATF_TC_HEAD(inet_network_basic,tc)47 ATF_TC_HEAD(inet_network_basic, tc)
48 {
49 	atf_tc_set_md_var(tc, "descr", "Checks inet_network(3)");
50 }
51 
ATF_TC_BODY(inet_network_basic,tc)52 ATF_TC_BODY(inet_network_basic, tc)
53 {
54 
55 	H_REQUIRE("0x12", 0x00000012);
56 	H_REQUIRE("127.1", 0x00007f01);
57 	H_REQUIRE("127.1.2.3", 0x7f010203);
58 	H_REQUIRE("0X12", 0x00000012);
59 	H_REQUIRE("0", 0x0);
60 	H_REQUIRE("01.02.07.077", 0x0102073f);
61 	H_REQUIRE("0x1.23.045.0", 0x01172500);
62 	H_REQUIRE("0x12.0x34", 0x00001234);
63 
64 	/* This is valid (because of the trailing space after the digit). */
65 	H_REQUIRE("1 bar", 0x00000001);
66 }
67 
68 ATF_TC(inet_network_err);
ATF_TC_HEAD(inet_network_err,tc)69 ATF_TC_HEAD(inet_network_err, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr", "Invalid addresses w/ inet_network(3)");
72 }
73 
ATF_TC_BODY(inet_network_err,tc)74 ATF_TC_BODY(inet_network_err, tc)
75 {
76 	/* Malformed requests. */
77 	H_REQUIRE("4.2.3.1.", 0xffffffff);
78 	H_REQUIRE("0x123456", 0xffffffff);
79 	H_REQUIRE("0x12.0x345", 0xffffffff);
80 	H_REQUIRE("1.2.3.4.5", 0xffffffff);
81 	H_REQUIRE("1..3.4", 0xffffffff);
82 	H_REQUIRE(".", 0xffffffff);
83 	H_REQUIRE("1.", 0xffffffff);
84 	H_REQUIRE(".1", 0xffffffff);
85 	H_REQUIRE("0x", 0xffffffff);
86 	H_REQUIRE("", 0xffffffff);
87 	H_REQUIRE(" ", 0xffffffff);
88 	H_REQUIRE("bar", 0xffffffff);
89 	H_REQUIRE("1.2bar", 0xffffffff);
90 	H_REQUIRE("1.", 0xffffffff);
91 	H_REQUIRE("\xc3\x8a\xc3\x83\xc3\x95\xc3\x8b\xc3\x85\xc3\x8e",
92 		0xffffffff);
93 	H_REQUIRE("255.255.255.255", 0xffffffff);
94 	H_REQUIRE("x", 0xffffffff);
95 	H_REQUIRE("078", 0xffffffff);
96 	H_REQUIRE("127.0xfff", 0xffffffff);
97 }
98 
ATF_TP_ADD_TCS(tp)99 ATF_TP_ADD_TCS(tp)
100 {
101 
102 	ATF_TP_ADD_TC(tp, inet_network_basic);
103 	ATF_TP_ADD_TC(tp, inet_network_err);
104 
105 	return atf_no_error();
106 }
107