Home | History | Annotate | Line # | Download | only in inet
t_inet_network.c revision 1.3
      1 /* $NetBSD: t_inet_network.c,v 1.3 2011/07/15 11:27:23 jruoho 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.3 2011/07/15 11:27:23 jruoho Exp $");
     36 
     37 #include <arpa/inet.h>
     38 
     39 #include <atf-c.h>
     40 #include <stdio.h>
     41 #include <string.h>
     42 
     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)
     47 
     48 ATF_TC(inet_addr_basic);
     49 ATF_TC_HEAD(inet_addr_basic, tc)
     50 {
     51 	atf_tc_set_md_var(tc, "descr", "Checks inet_addr(3)");
     52 }
     53 
     54 ATF_TC_BODY(inet_addr_basic, tc)
     55 {
     56 	static const char *addrs[] = {
     57 		"127.0.0.1", "99.99.99.99", "0.0.0.0", "255.255.255.255" };
     58 
     59 	struct in_addr ia;
     60 	const char *ian;
     61 	in_addr_t addr;
     62 	size_t i;
     63 
     64 	for (i = 0; i < __arraycount(addrs); i++) {
     65 
     66 		(void)fprintf(stderr, "checking %s\n", addrs[i]);;
     67 
     68 		addr = inet_addr(addrs[i]);
     69 		ia.s_addr = addr;
     70 		ian = inet_ntoa(ia);
     71 
     72 		ATF_REQUIRE(ian != NULL);
     73 		ATF_CHECK(strcmp(ian, addrs[i]) == 0);
     74 	}
     75 }
     76 
     77 ATF_TC(inet_addr_err);
     78 ATF_TC_HEAD(inet_addr_err, tc)
     79 {
     80 	atf_tc_set_md_var(tc, "descr", "Invalid addresses with inet_addr(3)");
     81 }
     82 
     83 ATF_TC_BODY(inet_addr_err, tc)
     84 {
     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", "", " "};
     89 
     90 	struct in_addr ia;
     91 	const char *ian;
     92 	in_addr_t addr;
     93 	size_t i;
     94 
     95 	for (i = 0; i < __arraycount(addrs); i++) {
     96 
     97 		(void)fprintf(stderr, "checking %s\n", addrs[i]);;
     98 
     99 		addr = inet_addr(addrs[i]);
    100 		ia.s_addr = addr;
    101 		ian = inet_ntoa(ia);
    102 
    103 		ATF_REQUIRE(ian != NULL);
    104 		ATF_CHECK(strcmp(ian, addrs[i]) != 0);
    105 	}
    106 }
    107 
    108 ATF_TC(inet_network_basic);
    109 ATF_TC_HEAD(inet_network_basic, tc)
    110 {
    111 	atf_tc_set_md_var(tc, "descr", "Checks inet_network(3)");
    112 }
    113 
    114 ATF_TC_BODY(inet_network_basic, tc)
    115 {
    116 
    117 	H_REQUIRE("0x12", 0x00000012);
    118 	H_REQUIRE("127.1", 0x00007f01);
    119 	H_REQUIRE("127.1.2.3", 0x7f010203);
    120 	H_REQUIRE("0X12", 0x00000012);
    121 	H_REQUIRE("0", 0x0);
    122 	H_REQUIRE("01.02.07.077", 0x0102073f);
    123 	H_REQUIRE("0x1.23.045.0", 0x01172500);
    124 	H_REQUIRE("0x12.0x34", 0x00001234);
    125 
    126 	/* This is valid (because of the trailing space after the digit). */
    127 	H_REQUIRE("1 bar", 0x00000001);
    128 }
    129 
    130 ATF_TC(inet_network_err);
    131 ATF_TC_HEAD(inet_network_err, tc)
    132 {
    133 	atf_tc_set_md_var(tc, "descr", "Invalid addresses w/ inet_network(3)");
    134 }
    135 
    136 ATF_TC_BODY(inet_network_err, tc)
    137 {
    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",
    154 		0xffffffff);
    155 	H_REQUIRE("255.255.255.255", 0xffffffff);
    156 	H_REQUIRE("x", 0xffffffff);
    157 	H_REQUIRE("078", 0xffffffff);
    158 	H_REQUIRE("127.0xfff", 0xffffffff);
    159 }
    160 
    161 ATF_TP_ADD_TCS(tp)
    162 {
    163 
    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);
    168 
    169 	return atf_no_error();
    170 }
    171