xref: /minix3/tests/lib/libc/inet/t_inet_network.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_inet_network.c,v 1.4 2015/04/09 16:47:56 ginsbach Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc  * Copyright (c) 2008 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Brian Ginsbach.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc 
3211be35a1SLionel Sambuc #include <sys/cdefs.h>
3311be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
3411be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
35*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_inet_network.c,v 1.4 2015/04/09 16:47:56 ginsbach Exp $");
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include <arpa/inet.h>
3811be35a1SLionel Sambuc 
3911be35a1SLionel Sambuc #include <atf-c.h>
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc #define H_REQUIRE(input, expected)					\
4211be35a1SLionel Sambuc 	ATF_REQUIRE_EQ_MSG(inet_network(input), (in_addr_t) expected,	\
4311be35a1SLionel Sambuc 	    "inet_network(%s) returned: 0x%08X, expected: %s", #input,	\
4411be35a1SLionel Sambuc 	    inet_network(input), #expected)
4511be35a1SLionel Sambuc 
4611be35a1SLionel Sambuc ATF_TC(inet_network_basic);
ATF_TC_HEAD(inet_network_basic,tc)4711be35a1SLionel Sambuc ATF_TC_HEAD(inet_network_basic, tc)
4811be35a1SLionel Sambuc {
4911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks inet_network(3)");
5011be35a1SLionel Sambuc }
5111be35a1SLionel Sambuc 
ATF_TC_BODY(inet_network_basic,tc)5211be35a1SLionel Sambuc ATF_TC_BODY(inet_network_basic, tc)
5311be35a1SLionel Sambuc {
5411be35a1SLionel Sambuc 
5511be35a1SLionel Sambuc 	H_REQUIRE("0x12", 0x00000012);
5611be35a1SLionel Sambuc 	H_REQUIRE("127.1", 0x00007f01);
5711be35a1SLionel Sambuc 	H_REQUIRE("127.1.2.3", 0x7f010203);
5811be35a1SLionel Sambuc 	H_REQUIRE("0X12", 0x00000012);
5911be35a1SLionel Sambuc 	H_REQUIRE("0", 0x0);
6011be35a1SLionel Sambuc 	H_REQUIRE("01.02.07.077", 0x0102073f);
6111be35a1SLionel Sambuc 	H_REQUIRE("0x1.23.045.0", 0x01172500);
6211be35a1SLionel Sambuc 	H_REQUIRE("0x12.0x34", 0x00001234);
6311be35a1SLionel Sambuc 
6411be35a1SLionel Sambuc 	/* This is valid (because of the trailing space after the digit). */
6511be35a1SLionel Sambuc 	H_REQUIRE("1 bar", 0x00000001);
6611be35a1SLionel Sambuc }
6711be35a1SLionel Sambuc 
6811be35a1SLionel Sambuc ATF_TC(inet_network_err);
ATF_TC_HEAD(inet_network_err,tc)6911be35a1SLionel Sambuc ATF_TC_HEAD(inet_network_err, tc)
7011be35a1SLionel Sambuc {
7111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Invalid addresses w/ inet_network(3)");
7211be35a1SLionel Sambuc }
7311be35a1SLionel Sambuc 
ATF_TC_BODY(inet_network_err,tc)7411be35a1SLionel Sambuc ATF_TC_BODY(inet_network_err, tc)
7511be35a1SLionel Sambuc {
7611be35a1SLionel Sambuc 	/* Malformed requests. */
7711be35a1SLionel Sambuc 	H_REQUIRE("4.2.3.1.", 0xffffffff);
7811be35a1SLionel Sambuc 	H_REQUIRE("0x123456", 0xffffffff);
7911be35a1SLionel Sambuc 	H_REQUIRE("0x12.0x345", 0xffffffff);
8011be35a1SLionel Sambuc 	H_REQUIRE("1.2.3.4.5", 0xffffffff);
8111be35a1SLionel Sambuc 	H_REQUIRE("1..3.4", 0xffffffff);
8211be35a1SLionel Sambuc 	H_REQUIRE(".", 0xffffffff);
8311be35a1SLionel Sambuc 	H_REQUIRE("1.", 0xffffffff);
8411be35a1SLionel Sambuc 	H_REQUIRE(".1", 0xffffffff);
8511be35a1SLionel Sambuc 	H_REQUIRE("0x", 0xffffffff);
8611be35a1SLionel Sambuc 	H_REQUIRE("", 0xffffffff);
8711be35a1SLionel Sambuc 	H_REQUIRE(" ", 0xffffffff);
8811be35a1SLionel Sambuc 	H_REQUIRE("bar", 0xffffffff);
8911be35a1SLionel Sambuc 	H_REQUIRE("1.2bar", 0xffffffff);
9011be35a1SLionel Sambuc 	H_REQUIRE("1.", 0xffffffff);
9111be35a1SLionel Sambuc 	H_REQUIRE("\xc3\x8a\xc3\x83\xc3\x95\xc3\x8b\xc3\x85\xc3\x8e",
9211be35a1SLionel Sambuc 		0xffffffff);
9311be35a1SLionel Sambuc 	H_REQUIRE("255.255.255.255", 0xffffffff);
9411be35a1SLionel Sambuc 	H_REQUIRE("x", 0xffffffff);
9511be35a1SLionel Sambuc 	H_REQUIRE("078", 0xffffffff);
9611be35a1SLionel Sambuc 	H_REQUIRE("127.0xfff", 0xffffffff);
9711be35a1SLionel Sambuc }
9811be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)9911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
10011be35a1SLionel Sambuc {
10111be35a1SLionel Sambuc 
10211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, inet_network_basic);
10311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, inet_network_err);
10411be35a1SLionel Sambuc 
10511be35a1SLionel Sambuc 	return atf_no_error();
10611be35a1SLionel Sambuc }
107