1272343Sngie/* $NetBSD: t_inet_network.c,v 1.3 2011/07/15 11:27:23 jruoho Exp $ */ 2272343Sngie 3272343Sngie/* 4272343Sngie * Copyright (c) 2008 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Brian Ginsbach. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie 32272343Sngie#include <sys/cdefs.h> 33272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\ 34272343Sngie The NetBSD Foundation, inc. All rights reserved."); 35272343Sngie__RCSID("$NetBSD: t_inet_network.c,v 1.3 2011/07/15 11:27:23 jruoho Exp $"); 36272343Sngie 37272343Sngie#include <arpa/inet.h> 38272343Sngie 39272343Sngie#include <atf-c.h> 40272343Sngie#include <stdio.h> 41272343Sngie#include <string.h> 42272343Sngie 43272343Sngie#define H_REQUIRE(input, expected) \ 44272343Sngie ATF_REQUIRE_EQ_MSG(inet_network(input), (in_addr_t) expected, \ 45272343Sngie "inet_network(%s) returned: 0x%08X, expected: %s", #input, \ 46272343Sngie inet_network(input), #expected) 47272343Sngie 48272343SngieATF_TC(inet_addr_basic); 49272343SngieATF_TC_HEAD(inet_addr_basic, tc) 50272343Sngie{ 51272343Sngie atf_tc_set_md_var(tc, "descr", "Checks inet_addr(3)"); 52272343Sngie} 53272343Sngie 54272343SngieATF_TC_BODY(inet_addr_basic, tc) 55272343Sngie{ 56272343Sngie static const char *addrs[] = { 57272343Sngie "127.0.0.1", "99.99.99.99", "0.0.0.0", "255.255.255.255" }; 58272343Sngie 59272343Sngie struct in_addr ia; 60272343Sngie const char *ian; 61272343Sngie in_addr_t addr; 62272343Sngie size_t i; 63272343Sngie 64272343Sngie for (i = 0; i < __arraycount(addrs); i++) { 65272343Sngie 66272343Sngie (void)fprintf(stderr, "checking %s\n", addrs[i]);; 67272343Sngie 68272343Sngie addr = inet_addr(addrs[i]); 69272343Sngie ia.s_addr = addr; 70272343Sngie ian = inet_ntoa(ia); 71272343Sngie 72272343Sngie ATF_REQUIRE(ian != NULL); 73272343Sngie ATF_CHECK(strcmp(ian, addrs[i]) == 0); 74272343Sngie } 75272343Sngie} 76272343Sngie 77272343SngieATF_TC(inet_addr_err); 78272343SngieATF_TC_HEAD(inet_addr_err, tc) 79272343Sngie{ 80272343Sngie atf_tc_set_md_var(tc, "descr", "Invalid addresses with inet_addr(3)"); 81272343Sngie} 82272343Sngie 83272343SngieATF_TC_BODY(inet_addr_err, tc) 84272343Sngie{ 85272343Sngie static const char *addrs[] = { 86272343Sngie ". . . .", "1.2.3.", "0.0.0.256", "255.255.255.256", 87272343Sngie "................................................", 88272343Sngie "a.b.c.d", "0x0.0x1.0x2.0x3", "-1.-1.-1.-1", "", " "}; 89272343Sngie 90272343Sngie struct in_addr ia; 91272343Sngie const char *ian; 92272343Sngie in_addr_t addr; 93272343Sngie size_t i; 94272343Sngie 95272343Sngie for (i = 0; i < __arraycount(addrs); i++) { 96272343Sngie 97272343Sngie (void)fprintf(stderr, "checking %s\n", addrs[i]);; 98272343Sngie 99272343Sngie addr = inet_addr(addrs[i]); 100272343Sngie ia.s_addr = addr; 101272343Sngie ian = inet_ntoa(ia); 102272343Sngie 103272343Sngie ATF_REQUIRE(ian != NULL); 104272343Sngie ATF_CHECK(strcmp(ian, addrs[i]) != 0); 105272343Sngie } 106272343Sngie} 107272343Sngie 108272343SngieATF_TC(inet_network_basic); 109272343SngieATF_TC_HEAD(inet_network_basic, tc) 110272343Sngie{ 111272343Sngie atf_tc_set_md_var(tc, "descr", "Checks inet_network(3)"); 112272343Sngie} 113272343Sngie 114272343SngieATF_TC_BODY(inet_network_basic, tc) 115272343Sngie{ 116272343Sngie 117272343Sngie H_REQUIRE("0x12", 0x00000012); 118272343Sngie H_REQUIRE("127.1", 0x00007f01); 119272343Sngie H_REQUIRE("127.1.2.3", 0x7f010203); 120272343Sngie H_REQUIRE("0X12", 0x00000012); 121272343Sngie H_REQUIRE("0", 0x0); 122272343Sngie H_REQUIRE("01.02.07.077", 0x0102073f); 123272343Sngie H_REQUIRE("0x1.23.045.0", 0x01172500); 124272343Sngie H_REQUIRE("0x12.0x34", 0x00001234); 125272343Sngie 126272343Sngie /* This is valid (because of the trailing space after the digit). */ 127272343Sngie H_REQUIRE("1 bar", 0x00000001); 128272343Sngie} 129272343Sngie 130272343SngieATF_TC(inet_network_err); 131272343SngieATF_TC_HEAD(inet_network_err, tc) 132272343Sngie{ 133272343Sngie atf_tc_set_md_var(tc, "descr", "Invalid addresses w/ inet_network(3)"); 134272343Sngie} 135272343Sngie 136272343SngieATF_TC_BODY(inet_network_err, tc) 137272343Sngie{ 138272343Sngie /* Malformed requests. */ 139272343Sngie H_REQUIRE("4.2.3.1.", 0xffffffff); 140272343Sngie H_REQUIRE("0x123456", 0xffffffff); 141272343Sngie H_REQUIRE("0x12.0x345", 0xffffffff); 142272343Sngie H_REQUIRE("1.2.3.4.5", 0xffffffff); 143272343Sngie H_REQUIRE("1..3.4", 0xffffffff); 144272343Sngie H_REQUIRE(".", 0xffffffff); 145272343Sngie H_REQUIRE("1.", 0xffffffff); 146272343Sngie H_REQUIRE(".1", 0xffffffff); 147276478Sngie#if defined(__FreeBSD__) || defined(__APPLE__) 148276478Sngie H_REQUIRE("0x", 0x0); 149276478Sngie#else 150272343Sngie H_REQUIRE("0x", 0xffffffff); 151276478Sngie#endif 152272343Sngie H_REQUIRE("", 0xffffffff); 153272343Sngie H_REQUIRE(" ", 0xffffffff); 154272343Sngie H_REQUIRE("bar", 0xffffffff); 155272343Sngie H_REQUIRE("1.2bar", 0xffffffff); 156272343Sngie H_REQUIRE("1.", 0xffffffff); 157272343Sngie H_REQUIRE("\xc3\x8a\xc3\x83\xc3\x95\xc3\x8b\xc3\x85\xc3\x8e", 158272343Sngie 0xffffffff); 159272343Sngie H_REQUIRE("255.255.255.255", 0xffffffff); 160272343Sngie H_REQUIRE("x", 0xffffffff); 161272343Sngie H_REQUIRE("078", 0xffffffff); 162272343Sngie H_REQUIRE("127.0xfff", 0xffffffff); 163272343Sngie} 164272343Sngie 165272343SngieATF_TP_ADD_TCS(tp) 166272343Sngie{ 167272343Sngie 168272343Sngie ATF_TP_ADD_TC(tp, inet_addr_basic); 169272343Sngie ATF_TP_ADD_TC(tp, inet_addr_err); 170272343Sngie ATF_TP_ADD_TC(tp, inet_network_basic); 171272343Sngie ATF_TP_ADD_TC(tp, inet_network_err); 172272343Sngie 173272343Sngie return atf_no_error(); 174272343Sngie} 175