xref: /freebsd/contrib/netbsd-tests/lib/libc/inet/t_inet_network.c (revision c22165b4f1f5d38b681921797a44b3ba8c13b7e0)
1*640235e2SEnji Cooper /* $NetBSD: t_inet_network.c,v 1.4 2015/04/09 16:47:56 ginsbach Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*
457718be8SEnji Cooper  * Copyright (c) 2008 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Brian Ginsbach.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper 
3257718be8SEnji Cooper #include <sys/cdefs.h>
3357718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\
3457718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
35*640235e2SEnji Cooper __RCSID("$NetBSD: t_inet_network.c,v 1.4 2015/04/09 16:47:56 ginsbach Exp $");
3657718be8SEnji Cooper 
3757718be8SEnji Cooper #include <arpa/inet.h>
3857718be8SEnji Cooper 
3957718be8SEnji Cooper #include <atf-c.h>
4057718be8SEnji Cooper 
4157718be8SEnji Cooper #define H_REQUIRE(input, expected)					\
4257718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(inet_network(input), (in_addr_t) expected,	\
4357718be8SEnji Cooper 	    "inet_network(%s) returned: 0x%08X, expected: %s", #input,	\
4457718be8SEnji Cooper 	    inet_network(input), #expected)
4557718be8SEnji Cooper 
4657718be8SEnji Cooper ATF_TC(inet_network_basic);
ATF_TC_HEAD(inet_network_basic,tc)4757718be8SEnji Cooper ATF_TC_HEAD(inet_network_basic, tc)
4857718be8SEnji Cooper {
4957718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks inet_network(3)");
5057718be8SEnji Cooper }
5157718be8SEnji Cooper 
ATF_TC_BODY(inet_network_basic,tc)5257718be8SEnji Cooper ATF_TC_BODY(inet_network_basic, tc)
5357718be8SEnji Cooper {
5457718be8SEnji Cooper 
5557718be8SEnji Cooper 	H_REQUIRE("0x12", 0x00000012);
5657718be8SEnji Cooper 	H_REQUIRE("127.1", 0x00007f01);
5757718be8SEnji Cooper 	H_REQUIRE("127.1.2.3", 0x7f010203);
5857718be8SEnji Cooper 	H_REQUIRE("0X12", 0x00000012);
5957718be8SEnji Cooper 	H_REQUIRE("0", 0x0);
6057718be8SEnji Cooper 	H_REQUIRE("01.02.07.077", 0x0102073f);
6157718be8SEnji Cooper 	H_REQUIRE("0x1.23.045.0", 0x01172500);
6257718be8SEnji Cooper 	H_REQUIRE("0x12.0x34", 0x00001234);
6357718be8SEnji Cooper 
6457718be8SEnji Cooper 	/* This is valid (because of the trailing space after the digit). */
6557718be8SEnji Cooper 	H_REQUIRE("1 bar", 0x00000001);
6657718be8SEnji Cooper }
6757718be8SEnji Cooper 
6857718be8SEnji Cooper ATF_TC(inet_network_err);
ATF_TC_HEAD(inet_network_err,tc)6957718be8SEnji Cooper ATF_TC_HEAD(inet_network_err, tc)
7057718be8SEnji Cooper {
7157718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Invalid addresses w/ inet_network(3)");
7257718be8SEnji Cooper }
7357718be8SEnji Cooper 
ATF_TC_BODY(inet_network_err,tc)7457718be8SEnji Cooper ATF_TC_BODY(inet_network_err, tc)
7557718be8SEnji Cooper {
7657718be8SEnji Cooper 	/* Malformed requests. */
7757718be8SEnji Cooper 	H_REQUIRE("4.2.3.1.", 0xffffffff);
7857718be8SEnji Cooper 	H_REQUIRE("0x123456", 0xffffffff);
7957718be8SEnji Cooper 	H_REQUIRE("0x12.0x345", 0xffffffff);
8057718be8SEnji Cooper 	H_REQUIRE("1.2.3.4.5", 0xffffffff);
8157718be8SEnji Cooper 	H_REQUIRE("1..3.4", 0xffffffff);
8257718be8SEnji Cooper 	H_REQUIRE(".", 0xffffffff);
8357718be8SEnji Cooper 	H_REQUIRE("1.", 0xffffffff);
8457718be8SEnji Cooper 	H_REQUIRE(".1", 0xffffffff);
855896aab3SEnji Cooper #if defined(__FreeBSD__) || defined(__APPLE__)
865896aab3SEnji Cooper 	H_REQUIRE("0x", 0x0);
875896aab3SEnji Cooper #else
8857718be8SEnji Cooper 	H_REQUIRE("0x", 0xffffffff);
895896aab3SEnji Cooper #endif
9057718be8SEnji Cooper 	H_REQUIRE("", 0xffffffff);
9157718be8SEnji Cooper 	H_REQUIRE(" ", 0xffffffff);
9257718be8SEnji Cooper 	H_REQUIRE("bar", 0xffffffff);
9357718be8SEnji Cooper 	H_REQUIRE("1.2bar", 0xffffffff);
9457718be8SEnji Cooper 	H_REQUIRE("1.", 0xffffffff);
9557718be8SEnji Cooper 	H_REQUIRE("\xc3\x8a\xc3\x83\xc3\x95\xc3\x8b\xc3\x85\xc3\x8e",
9657718be8SEnji Cooper 		0xffffffff);
9757718be8SEnji Cooper 	H_REQUIRE("255.255.255.255", 0xffffffff);
9857718be8SEnji Cooper 	H_REQUIRE("x", 0xffffffff);
9957718be8SEnji Cooper 	H_REQUIRE("078", 0xffffffff);
10057718be8SEnji Cooper 	H_REQUIRE("127.0xfff", 0xffffffff);
10157718be8SEnji Cooper }
10257718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)10357718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
10457718be8SEnji Cooper {
10557718be8SEnji Cooper 
10657718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, inet_network_basic);
10757718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, inet_network_err);
10857718be8SEnji Cooper 
10957718be8SEnji Cooper 	return atf_no_error();
11057718be8SEnji Cooper }
111