b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews *
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/.
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews */
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <config.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <isc/mem.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <isc/netaddr.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <isc/radix.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <isc/result.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <isc/util.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <atf-c.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <stdlib.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <stdint.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include "isctest.h"
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark AndrewsATF_TC(isc_radix_search);
b976c39c07f7672bd1293e878b3306c7decf8afeMark AndrewsATF_TC_HEAD(isc_radix_search, tc) {
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews atf_tc_set_md_var(tc, "descr", "test radix seaching");
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews}
b976c39c07f7672bd1293e878b3306c7decf8afeMark AndrewsATF_TC_BODY(isc_radix_search, tc) {
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_radix_tree_t *radix = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_radix_node_t *node;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_prefix_t prefix;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_result_t result;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews struct in_addr in_addr;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_netaddr_t netaddr;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews UNUSED(tc);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews result = isc_test_begin(NULL, ISC_TRUE);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews result = isc_radix_create(mctx, &radix, 32);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews in_addr.s_addr = inet_addr("3.3.3.0");
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_netaddr_fromin(&netaddr, &in_addr);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews NETADDR_TO_PREFIX_T(&netaddr, prefix, 24, ISC_FALSE);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews node = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews result = isc_radix_insert(radix, &node, NULL, &prefix);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews node->data[0] = (void *)1;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_refcount_destroy(&prefix.refcount);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews in_addr.s_addr = inet_addr("3.3.0.0");
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_netaddr_fromin(&netaddr, &in_addr);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews NETADDR_TO_PREFIX_T(&netaddr, prefix, 16, ISC_FALSE);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews node = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews result = isc_radix_insert(radix, &node, NULL, &prefix);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews node->data[0] = (void *)2;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_refcount_destroy(&prefix.refcount);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews in_addr.s_addr = inet_addr("3.3.3.3");
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_netaddr_fromin(&netaddr, &in_addr);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews NETADDR_TO_PREFIX_T(&netaddr, prefix, 22, ISC_FALSE);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews node = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews result = isc_radix_search(radix, &node, &prefix);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews ATF_REQUIRE_EQ(node->data[0], (void *)2);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_refcount_destroy(&prefix.refcount);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_radix_destroy(radix, NULL);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_test_end();
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews}
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews/*
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * Main
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews */
b976c39c07f7672bd1293e878b3306c7decf8afeMark AndrewsATF_TP_ADD_TCS(tp) {
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews ATF_TP_ADD_TC(tp, isc_radix_search);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews return (atf_no_error());
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews}