47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt/*
c88b4680d9c97d087edc8b226f489798db085341Tinderbox User * Copyright (C) 2012, 2015-2017 Internet Systems Consortium, Inc. ("ISC")
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt */
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt/* $Id$ */
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt/*! \file */
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt#include <config.h>
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt#include <atf-c.h>
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt#include <unistd.h>
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt#include <isc/sockaddr.h>
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt#include <isc/print.h>
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt#include "isctest.h"
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt/*
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt * Individual unit tests
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt */
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan HuntATF_TC(sockaddr_hash);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan HuntATF_TC_HEAD(sockaddr_hash, tc) {
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt atf_tc_set_md_var(tc, "descr", "sockaddr hash");
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt}
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan HuntATF_TC_BODY(sockaddr_hash, tc) {
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt isc_result_t result;
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt isc_sockaddr_t addr;
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt struct in_addr in;
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt struct in6_addr in6;
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt unsigned int h1, h2, h3, h4;
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt int ret;
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt UNUSED(tc);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt result = isc_test_begin(NULL, ISC_TRUE);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt in.s_addr = inet_addr("127.0.0.1");
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt isc_sockaddr_fromin(&addr, &in, 1);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt h1 = isc_sockaddr_hash(&addr, ISC_TRUE);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt h2 = isc_sockaddr_hash(&addr, ISC_FALSE);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt ATF_CHECK(h1 != h2);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt ret = inet_pton(AF_INET6, "::ffff:127.0.0.1", &in6);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt ATF_CHECK(ret == 1);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt isc_sockaddr_fromin6(&addr, &in6, 1);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt h3 = isc_sockaddr_hash(&addr, ISC_TRUE);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt h4 = isc_sockaddr_hash(&addr, ISC_FALSE);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt ATF_CHECK(h1 == h3);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt ATF_CHECK(h2 == h4);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt isc_test_end();
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt}
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
6588a2b404365cea59e71a3f639349272600f68dMark AndrewsATF_TC(sockaddr_isnetzero);
6588a2b404365cea59e71a3f639349272600f68dMark AndrewsATF_TC_HEAD(sockaddr_isnetzero, tc) {
6588a2b404365cea59e71a3f639349272600f68dMark Andrews atf_tc_set_md_var(tc, "descr", "sockaddr is net zero");
6588a2b404365cea59e71a3f639349272600f68dMark Andrews}
6588a2b404365cea59e71a3f639349272600f68dMark AndrewsATF_TC_BODY(sockaddr_isnetzero, tc) {
6588a2b404365cea59e71a3f639349272600f68dMark Andrews isc_sockaddr_t addr;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews struct in_addr in;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews struct in6_addr in6;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews isc_boolean_t r;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews int ret;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews size_t i;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews struct {
6588a2b404365cea59e71a3f639349272600f68dMark Andrews const char *string;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews isc_boolean_t expect;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews } data4[] = {
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "0.0.0.0", ISC_TRUE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "0.0.0.1", ISC_TRUE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "0.0.1.0", ISC_TRUE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "0.1.0.0", ISC_TRUE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "1.0.0.0", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "0.0.0.127", ISC_TRUE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "0.0.0.255", ISC_TRUE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "127.0.0.1", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "255.255.255.255", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews };
6588a2b404365cea59e71a3f639349272600f68dMark Andrews /*
6588a2b404365cea59e71a3f639349272600f68dMark Andrews * Mapped addresses are currently not netzero.
6588a2b404365cea59e71a3f639349272600f68dMark Andrews */
6588a2b404365cea59e71a3f639349272600f68dMark Andrews struct {
6588a2b404365cea59e71a3f639349272600f68dMark Andrews const char *string;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews isc_boolean_t expect;
6588a2b404365cea59e71a3f639349272600f68dMark Andrews } data6[] = {
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "::ffff:0.0.0.0", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "::ffff:0.0.0.1", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "::ffff:0.0.0.127", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "::ffff:0.0.0.255", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "::ffff:127.0.0.1", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews { "::ffff:255.255.255.255", ISC_FALSE },
6588a2b404365cea59e71a3f639349272600f68dMark Andrews };
6588a2b404365cea59e71a3f639349272600f68dMark Andrews
6588a2b404365cea59e71a3f639349272600f68dMark Andrews UNUSED(tc);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews
6588a2b404365cea59e71a3f639349272600f68dMark Andrews for (i = 0; i < sizeof(data4)/sizeof(data4[0]); i++) {
512765ba1e3f0b29e2c1ae4b0138838c6422f2bdEvan Hunt in.s_addr = inet_addr(data4[i].string);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews isc_sockaddr_fromin(&addr, &in, 1);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews r = isc_sockaddr_isnetzero(&addr);
512765ba1e3f0b29e2c1ae4b0138838c6422f2bdEvan Hunt ATF_CHECK_EQ_MSG(r, data4[i].expect, "%s", data4[i].string);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews }
6588a2b404365cea59e71a3f639349272600f68dMark Andrews
6588a2b404365cea59e71a3f639349272600f68dMark Andrews for (i = 0; i < sizeof(data6)/sizeof(data6[0]); i++) {
512765ba1e3f0b29e2c1ae4b0138838c6422f2bdEvan Hunt ret = inet_pton(AF_INET6, data6[i].string, &in6);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews ATF_CHECK_EQ(ret, 1);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews isc_sockaddr_fromin6(&addr, &in6, 1);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews r = isc_sockaddr_isnetzero(&addr);
512765ba1e3f0b29e2c1ae4b0138838c6422f2bdEvan Hunt ATF_CHECK_EQ_MSG(r, data6[i].expect, "%s", data6[i].string);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews }
6588a2b404365cea59e71a3f639349272600f68dMark Andrews}
6588a2b404365cea59e71a3f639349272600f68dMark Andrews
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt/*
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt * Main
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt */
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan HuntATF_TP_ADD_TCS(tp) {
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt ATF_TP_ADD_TC(tp, sockaddr_hash);
6588a2b404365cea59e71a3f639349272600f68dMark Andrews ATF_TP_ADD_TC(tp, sockaddr_isnetzero);
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt return (atf_no_error());
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt}
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt