2014-11-04 12:34:12 +11:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2014-11-04 12:34:12 +11:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2014-11-04 12:34:12 +11:00
|
|
|
*/
|
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
|
|
#include <setjmp.h>
|
2018-10-23 22:22:10 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2014-11-04 12:34:12 +11:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/netaddr.h>
|
|
|
|
#include <isc/radix.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
#include "isctest.h"
|
2014-11-04 12:34:12 +11:00
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-23 22:22:10 -07:00
|
|
|
isc_result_t result;
|
2014-11-04 12:34:12 +11:00
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2014-11-04 12:34:12 +11:00
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-10-23 22:22:10 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
2014-11-04 12:34:12 +11:00
|
|
|
}
|
2018-10-23 22:22:10 -07:00
|
|
|
|
|
|
|
/* test radix searching */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_radix_search_test(void **state) {
|
2014-11-04 12:34:12 +11:00
|
|
|
isc_radix_tree_t *radix = NULL;
|
|
|
|
isc_radix_node_t *node;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_prefix_t prefix;
|
|
|
|
isc_result_t result;
|
|
|
|
struct in_addr in_addr;
|
|
|
|
isc_netaddr_t netaddr;
|
2014-11-04 12:34:12 +11:00
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
UNUSED(state);
|
2014-11-04 12:34:12 +11:00
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
result = isc_radix_create(test_mctx, &radix, 32);
|
2018-10-23 22:22:10 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2014-11-04 12:34:12 +11:00
|
|
|
|
|
|
|
in_addr.s_addr = inet_addr("3.3.3.0");
|
|
|
|
isc_netaddr_fromin(&netaddr, &in_addr);
|
2018-04-26 20:57:41 -07:00
|
|
|
NETADDR_TO_PREFIX_T(&netaddr, prefix, 24);
|
2014-11-04 12:34:12 +11:00
|
|
|
|
|
|
|
node = NULL;
|
|
|
|
result = isc_radix_insert(radix, &node, NULL, &prefix);
|
2018-10-23 22:22:10 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2014-11-04 12:34:12 +11:00
|
|
|
node->data[0] = (void *)1;
|
|
|
|
isc_refcount_destroy(&prefix.refcount);
|
|
|
|
|
|
|
|
in_addr.s_addr = inet_addr("3.3.0.0");
|
|
|
|
isc_netaddr_fromin(&netaddr, &in_addr);
|
2018-04-26 20:57:41 -07:00
|
|
|
NETADDR_TO_PREFIX_T(&netaddr, prefix, 16);
|
2014-11-04 12:34:12 +11:00
|
|
|
|
|
|
|
node = NULL;
|
|
|
|
result = isc_radix_insert(radix, &node, NULL, &prefix);
|
2018-10-23 22:22:10 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2014-11-04 12:34:12 +11:00
|
|
|
node->data[0] = (void *)2;
|
|
|
|
isc_refcount_destroy(&prefix.refcount);
|
|
|
|
|
|
|
|
in_addr.s_addr = inet_addr("3.3.3.3");
|
|
|
|
isc_netaddr_fromin(&netaddr, &in_addr);
|
2018-04-26 20:57:41 -07:00
|
|
|
NETADDR_TO_PREFIX_T(&netaddr, prefix, 22);
|
2014-11-04 12:34:12 +11:00
|
|
|
|
|
|
|
node = NULL;
|
|
|
|
result = isc_radix_search(radix, &node, &prefix);
|
2018-10-23 22:22:10 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(node->data[0], (void *)2);
|
2014-11-04 12:34:12 +11:00
|
|
|
|
|
|
|
isc_refcount_destroy(&prefix.refcount);
|
|
|
|
|
|
|
|
isc_radix_destroy(radix, NULL);
|
2018-10-23 22:22:10 -07:00
|
|
|
}
|
2014-11-04 12:34:12 +11:00
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-23 22:22:10 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(isc_radix_search_test, _setup,
|
|
|
|
_teardown),
|
2018-10-23 22:22:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2014-11-04 12:34:12 +11:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2014-11-04 12:34:12 +11:00
|
|
|
|
2018-10-23 22:22:10 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-23 22:22:10 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2014-11-04 12:34:12 +11:00
|
|
|
}
|
2018-10-23 22:22:10 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|