| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#define UNIT_TESTING |
| 23 |
#include <cmocka.h> |
| 24 |
|
| 25 |
#include <isc/mem.h> |
| 26 |
#include <isc/netaddr.h> |
| 27 |
#include <isc/radix.h> |
| 28 |
#include <isc/result.h> |
| 29 |
#include <isc/util.h> |
| 30 |
|
| 31 |
#include <tests/isc.h> |
| 32 |
|
| 33 |
/* test radix node removal */ |
| 34 |
ISC_RUN_TEST_IMPL(isc_radix_remove) { |
| 35 |
isc_radix_tree_t *radix = NULL; |
| 36 |
isc_radix_node_t *node; |
| 37 |
isc_prefix_t prefix; |
| 38 |
isc_result_t result; |
| 39 |
struct in_addr in_addr; |
| 40 |
isc_netaddr_t netaddr; |
| 41 |
|
| 42 |
UNUSED(state); |
| 43 |
|
| 44 |
isc_radix_create(mctx, &radix, 32); |
| 45 |
|
| 46 |
in_addr.s_addr = inet_addr("1.1.1.1"); |
| 47 |
isc_netaddr_fromin(&netaddr, &in_addr); |
| 48 |
NETADDR_TO_PREFIX_T(&netaddr, prefix, 32); |
| 49 |
|
| 50 |
node = NULL; |
| 51 |
result = isc_radix_insert(radix, &node, NULL, &prefix); |
| 52 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 53 |
node->data[0] = (void *)32; |
| 54 |
isc_refcount_destroy(&prefix.refcount); |
| 55 |
|
| 56 |
in_addr.s_addr = inet_addr("1.0.0.0"); |
| 57 |
isc_netaddr_fromin(&netaddr, &in_addr); |
| 58 |
NETADDR_TO_PREFIX_T(&netaddr, prefix, 8); |
| 59 |
|
| 60 |
node = NULL; |
| 61 |
result = isc_radix_insert(radix, &node, NULL, &prefix); |
| 62 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 63 |
node->data[0] = (void *)8; |
| 64 |
isc_refcount_destroy(&prefix.refcount); |
| 65 |
|
| 66 |
in_addr.s_addr = inet_addr("1.1.1.0"); |
| 67 |
isc_netaddr_fromin(&netaddr, &in_addr); |
| 68 |
NETADDR_TO_PREFIX_T(&netaddr, prefix, 24); |
| 69 |
|
| 70 |
node = NULL; |
| 71 |
result = isc_radix_insert(radix, &node, NULL, &prefix); |
| 72 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 73 |
node->data[0] = (void *)24; |
| 74 |
isc_refcount_destroy(&prefix.refcount); |
| 75 |
|
| 76 |
isc_radix_remove(radix, node); |
| 77 |
|
| 78 |
isc_radix_destroy(radix, NULL); |
| 79 |
} |
| 80 |
|
| 81 |
/* test radix searching */ |
| 82 |
ISC_RUN_TEST_IMPL(isc_radix_search) { |
| 83 |
isc_radix_tree_t *radix = NULL; |
| 84 |
isc_radix_node_t *node; |
| 85 |
isc_prefix_t prefix; |
| 86 |
isc_result_t result; |
| 87 |
struct in_addr in_addr; |
| 88 |
isc_netaddr_t netaddr; |
| 89 |
|
| 90 |
UNUSED(state); |
| 91 |
|
| 92 |
isc_radix_create(mctx, &radix, 32); |
| 93 |
|
| 94 |
in_addr.s_addr = inet_addr("3.3.3.0"); |
| 95 |
isc_netaddr_fromin(&netaddr, &in_addr); |
| 96 |
NETADDR_TO_PREFIX_T(&netaddr, prefix, 24); |
| 97 |
|
| 98 |
node = NULL; |
| 99 |
result = isc_radix_insert(radix, &node, NULL, &prefix); |
| 100 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 101 |
node->data[0] = (void *)1; |
| 102 |
isc_refcount_destroy(&prefix.refcount); |
| 103 |
|
| 104 |
in_addr.s_addr = inet_addr("3.3.0.0"); |
| 105 |
isc_netaddr_fromin(&netaddr, &in_addr); |
| 106 |
NETADDR_TO_PREFIX_T(&netaddr, prefix, 16); |
| 107 |
|
| 108 |
node = NULL; |
| 109 |
result = isc_radix_insert(radix, &node, NULL, &prefix); |
| 110 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 111 |
node->data[0] = (void *)2; |
| 112 |
isc_refcount_destroy(&prefix.refcount); |
| 113 |
|
| 114 |
in_addr.s_addr = inet_addr("3.3.3.3"); |
| 115 |
isc_netaddr_fromin(&netaddr, &in_addr); |
| 116 |
NETADDR_TO_PREFIX_T(&netaddr, prefix, 22); |
| 117 |
|
| 118 |
node = NULL; |
| 119 |
result = isc_radix_search(radix, &node, &prefix); |
| 120 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 121 |
assert_ptr_equal(node->data[0], (void *)2); |
| 122 |
|
| 123 |
isc_refcount_destroy(&prefix.refcount); |
| 124 |
|
| 125 |
isc_radix_destroy(radix, NULL); |
| 126 |
} |
| 127 |
|
| 128 |
ISC_TEST_LIST_START |
| 129 |
|
| 130 |
ISC_TEST_ENTRY(isc_radix_remove) |
| 131 |
ISC_TEST_ENTRY(isc_radix_search) |
| 132 |
|
| 133 |
ISC_TEST_LIST_END |
| 134 |
ISC_TEST_MAIN |