1*f5b5fa5bSryo /* $NetBSD: t_inet_network.c,v 1.5 2022/10/06 06:05:31 ryo Exp $ */
24eb3e852Spgoyette
34eb3e852Spgoyette /*
44eb3e852Spgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc.
54eb3e852Spgoyette * All rights reserved.
64eb3e852Spgoyette *
74eb3e852Spgoyette * This code is derived from software contributed to The NetBSD Foundation
84eb3e852Spgoyette * by Brian Ginsbach.
94eb3e852Spgoyette *
104eb3e852Spgoyette * Redistribution and use in source and binary forms, with or without
114eb3e852Spgoyette * modification, are permitted provided that the following conditions
124eb3e852Spgoyette * are met:
134eb3e852Spgoyette * 1. Redistributions of source code must retain the above copyright
144eb3e852Spgoyette * notice, this list of conditions and the following disclaimer.
154eb3e852Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
164eb3e852Spgoyette * notice, this list of conditions and the following disclaimer in the
174eb3e852Spgoyette * documentation and/or other materials provided with the distribution.
184eb3e852Spgoyette *
194eb3e852Spgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204eb3e852Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214eb3e852Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224eb3e852Spgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234eb3e852Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244eb3e852Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254eb3e852Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264eb3e852Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274eb3e852Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284eb3e852Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294eb3e852Spgoyette * POSSIBILITY OF SUCH DAMAGE.
304eb3e852Spgoyette */
314eb3e852Spgoyette
324eb3e852Spgoyette #include <sys/cdefs.h>
334eb3e852Spgoyette __COPYRIGHT("@(#) Copyright (c) 2008\
344eb3e852Spgoyette The NetBSD Foundation, inc. All rights reserved.");
35*f5b5fa5bSryo __RCSID("$NetBSD: t_inet_network.c,v 1.5 2022/10/06 06:05:31 ryo Exp $");
364eb3e852Spgoyette
374eb3e852Spgoyette #include <arpa/inet.h>
384eb3e852Spgoyette
394eb3e852Spgoyette #include <atf-c.h>
404eb3e852Spgoyette
414eb3e852Spgoyette #define H_REQUIRE(input, expected) \
424eb3e852Spgoyette ATF_REQUIRE_EQ_MSG(inet_network(input), (in_addr_t) expected, \
434eb3e852Spgoyette "inet_network(%s) returned: 0x%08X, expected: %s", #input, \
444eb3e852Spgoyette inet_network(input), #expected)
454eb3e852Spgoyette
4631e0c3fbSjruoho ATF_TC(inet_network_basic);
ATF_TC_HEAD(inet_network_basic,tc)4731e0c3fbSjruoho ATF_TC_HEAD(inet_network_basic, tc)
484eb3e852Spgoyette {
494eb3e852Spgoyette atf_tc_set_md_var(tc, "descr", "Checks inet_network(3)");
504eb3e852Spgoyette }
514eb3e852Spgoyette
ATF_TC_BODY(inet_network_basic,tc)5231e0c3fbSjruoho ATF_TC_BODY(inet_network_basic, tc)
534eb3e852Spgoyette {
544eb3e852Spgoyette
554eb3e852Spgoyette H_REQUIRE("0x12", 0x00000012);
564eb3e852Spgoyette H_REQUIRE("127.1", 0x00007f01);
574eb3e852Spgoyette H_REQUIRE("127.1.2.3", 0x7f010203);
584eb3e852Spgoyette H_REQUIRE("0X12", 0x00000012);
594eb3e852Spgoyette H_REQUIRE("0", 0x0);
604eb3e852Spgoyette H_REQUIRE("01.02.07.077", 0x0102073f);
614eb3e852Spgoyette H_REQUIRE("0x1.23.045.0", 0x01172500);
624eb3e852Spgoyette H_REQUIRE("0x12.0x34", 0x00001234);
634eb3e852Spgoyette
6431e0c3fbSjruoho /* This is valid (because of the trailing space after the digit). */
654eb3e852Spgoyette H_REQUIRE("1 bar", 0x00000001);
6631e0c3fbSjruoho }
674eb3e852Spgoyette
6831e0c3fbSjruoho ATF_TC(inet_network_err);
ATF_TC_HEAD(inet_network_err,tc)6931e0c3fbSjruoho ATF_TC_HEAD(inet_network_err, tc)
7031e0c3fbSjruoho {
7111ee5754Sjruoho atf_tc_set_md_var(tc, "descr", "Invalid addresses w/ inet_network(3)");
7231e0c3fbSjruoho }
7331e0c3fbSjruoho
ATF_TC_BODY(inet_network_err,tc)7431e0c3fbSjruoho ATF_TC_BODY(inet_network_err, tc)
7531e0c3fbSjruoho {
7631e0c3fbSjruoho /* Malformed requests. */
774eb3e852Spgoyette H_REQUIRE("4.2.3.1.", 0xffffffff);
784eb3e852Spgoyette H_REQUIRE("0x123456", 0xffffffff);
794eb3e852Spgoyette H_REQUIRE("0x12.0x345", 0xffffffff);
804eb3e852Spgoyette H_REQUIRE("1.2.3.4.5", 0xffffffff);
814eb3e852Spgoyette H_REQUIRE("1..3.4", 0xffffffff);
824eb3e852Spgoyette H_REQUIRE(".", 0xffffffff);
834eb3e852Spgoyette H_REQUIRE("1.", 0xffffffff);
844eb3e852Spgoyette H_REQUIRE(".1", 0xffffffff);
854eb3e852Spgoyette H_REQUIRE("0x", 0xffffffff);
864eb3e852Spgoyette H_REQUIRE("", 0xffffffff);
874eb3e852Spgoyette H_REQUIRE(" ", 0xffffffff);
884eb3e852Spgoyette H_REQUIRE("bar", 0xffffffff);
894eb3e852Spgoyette H_REQUIRE("1.2bar", 0xffffffff);
904eb3e852Spgoyette H_REQUIRE("1.", 0xffffffff);
914eb3e852Spgoyette H_REQUIRE("\xc3\x8a\xc3\x83\xc3\x95\xc3\x8b\xc3\x85\xc3\x8e",
924eb3e852Spgoyette 0xffffffff);
934eb3e852Spgoyette H_REQUIRE("255.255.255.255", 0xffffffff);
944eb3e852Spgoyette H_REQUIRE("x", 0xffffffff);
95*f5b5fa5bSryo H_REQUIRE("x1", 0xffffffff);
96*f5b5fa5bSryo H_REQUIRE("xab", 0xffffffff);
97*f5b5fa5bSryo H_REQUIRE("x100", 0xffffffff);
984eb3e852Spgoyette H_REQUIRE("078", 0xffffffff);
994eb3e852Spgoyette H_REQUIRE("127.0xfff", 0xffffffff);
1004eb3e852Spgoyette }
1014eb3e852Spgoyette
ATF_TP_ADD_TCS(tp)1024eb3e852Spgoyette ATF_TP_ADD_TCS(tp)
1034eb3e852Spgoyette {
1044eb3e852Spgoyette
10531e0c3fbSjruoho ATF_TP_ADD_TC(tp, inet_network_basic);
10631e0c3fbSjruoho ATF_TP_ADD_TC(tp, inet_network_err);
1074eb3e852Spgoyette
1084eb3e852Spgoyette return atf_no_error();
1094eb3e852Spgoyette }
110