2016-05-26 21:23:19 +02:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2016-05-26 21:23:19 +02:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
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.
|
2016-05-26 21:23:19 +02:00
|
|
|
*/
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2016-05-26 21:23:19 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-10-24 01:13:13 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
#include <isc/hash.h>
|
|
|
|
#include <isc/ht.h>
|
|
|
|
#include <isc/mem.h>
|
2016-05-26 12:36:17 -07:00
|
|
|
#include <isc/print.h>
|
2017-09-27 16:02:02 +10:00
|
|
|
#include <isc/string.h>
|
2016-05-26 21:23:19 +02:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
#include "isctest.h"
|
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
/* INCLUDE LAST */
|
|
|
|
|
|
|
|
#include "../ht.c"
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2019-11-09 14:01:08 +01:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2019-11-09 14:01:08 +01:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
static void
|
2022-03-16 11:11:38 +01:00
|
|
|
test_ht_full(uint8_t init_bits, uint8_t finish_bits, uintptr_t count) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ht_t *ht = NULL;
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
uintptr_t i;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
isc_ht_init(&ht, test_mctx, init_bits);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_non_null(ht);
|
2017-10-19 13:08:31 +11:00
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
/*
|
2017-09-27 16:02:02 +10:00
|
|
|
* Note: snprintf() is followed with strlcat()
|
|
|
|
* to ensure we are always filling the 16 byte key.
|
2016-05-26 21:23:19 +02:00
|
|
|
*/
|
|
|
|
unsigned char key[16];
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *)i);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2020-02-13 14:44:37 -08:00
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2020-02-12 13:59:18 +01:00
|
|
|
assert_ptr_equal((void *)i, (uintptr_t)f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *)i);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_EXISTS);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
char key[64];
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: the key size is now strlen(key) which is bigger
|
|
|
|
* then the keys added above.
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_add(ht, (const unsigned char *)key, strlen(key),
|
|
|
|
(void *)i);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2020-02-13 14:44:37 -08:00
|
|
|
void *f = NULL;
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: case of KEY is now in capitals,
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
|
|
|
assert_null(f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
2020-02-13 14:44:37 -08:00
|
|
|
char key[64];
|
2016-05-26 21:23:19 +02:00
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_find(ht, (const unsigned char *)key,
|
2016-05-26 21:23:19 +02:00
|
|
|
strlen(key), &f);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2020-02-12 13:59:18 +01:00
|
|
|
assert_ptr_equal(f, (void *)i);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2020-02-13 14:44:37 -08:00
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_delete(ht, key, 16);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
|
|
|
assert_null(f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: upper case KEY.
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *)i);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
2020-02-13 14:44:37 -08:00
|
|
|
char key[64];
|
2016-05-26 21:23:19 +02:00
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_delete(ht, (const unsigned char *)key,
|
2016-05-26 21:23:19 +02:00
|
|
|
strlen(key));
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_find(ht, (const unsigned char *)key,
|
2016-05-26 21:23:19 +02:00
|
|
|
strlen(key), &f);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
|
|
|
assert_null(f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2020-02-13 14:44:37 -08:00
|
|
|
void *f = NULL;
|
2017-09-27 16:02:02 +10:00
|
|
|
/*
|
|
|
|
* Note: case of KEY is now in capitals,
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2020-02-12 13:59:18 +01:00
|
|
|
assert_ptr_equal((void *)i, (uintptr_t)f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
unsigned char key[16];
|
2020-02-13 14:44:37 -08:00
|
|
|
void *f = NULL;
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
|
2016-05-26 21:23:19 +02:00
|
|
|
result = isc_ht_find(ht, key, 16, &f);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
|
|
|
assert_null(f);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
assert_int_equal(ht->hashbits[ht->hindex], finish_bits);
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
isc_ht_destroy(&ht);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_null(ht);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
static void
|
2018-08-07 16:46:53 +02:00
|
|
|
test_ht_iterator(void) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ht_t *ht = NULL;
|
|
|
|
isc_result_t result;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_ht_iter_t *iter = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
uintptr_t i;
|
2022-03-16 11:11:38 +01:00
|
|
|
uintptr_t count = 6300;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t walked;
|
|
|
|
unsigned char key[16];
|
|
|
|
size_t tksize;
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
isc_ht_init(&ht, test_mctx, HT_MIN_BITS);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_non_null(ht);
|
2016-05-26 21:23:19 +02:00
|
|
|
for (i = 1; i <= count; i++) {
|
|
|
|
/*
|
|
|
|
* Note that the string we're snprintfing is always > 16 bytes
|
|
|
|
* so we are always filling the key.
|
|
|
|
*/
|
2017-09-27 17:57:07 +10:00
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2020-02-12 13:59:18 +01:00
|
|
|
result = isc_ht_add(ht, key, 16, (void *)i);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
/* We want to iterate while rehashing is in progress */
|
|
|
|
assert_true(rehashing_in_progress(ht));
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
walked = 0;
|
2022-03-08 11:22:55 +01:00
|
|
|
isc_ht_iter_create(ht, &iter);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (result = isc_ht_iter_first(iter); result == ISC_R_SUCCESS;
|
2020-02-13 14:44:37 -08:00
|
|
|
result = isc_ht_iter_next(iter))
|
|
|
|
{
|
2018-11-28 18:57:38 +11:00
|
|
|
unsigned char *tkey = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
void *v = NULL;
|
2018-11-28 18:57:38 +11:00
|
|
|
|
2017-10-20 16:04:59 +11:00
|
|
|
isc_ht_iter_current(iter, &v);
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(tksize, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
i = (uintptr_t)v;
|
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_memory_equal(key, tkey, 16);
|
2016-05-31 23:01:53 +02:00
|
|
|
walked++;
|
|
|
|
}
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(walked, count);
|
|
|
|
assert_int_equal(result, ISC_R_NOMORE);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
/* erase odd */
|
2016-05-26 21:23:19 +02:00
|
|
|
walked = 0;
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_first(iter);
|
|
|
|
while (result == ISC_R_SUCCESS) {
|
2018-11-28 18:57:38 +11:00
|
|
|
unsigned char *tkey = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
void *v = NULL;
|
2018-11-28 18:57:38 +11:00
|
|
|
|
2017-10-20 16:04:59 +11:00
|
|
|
isc_ht_iter_current(iter, &v);
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(tksize, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
i = (uintptr_t)v;
|
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_memory_equal(key, tkey, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
if ((uintptr_t)v % 2 == 0) {
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_delcurrent_next(iter);
|
|
|
|
} else {
|
|
|
|
result = isc_ht_iter_next(iter);
|
|
|
|
}
|
|
|
|
walked++;
|
|
|
|
}
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOMORE);
|
|
|
|
assert_int_equal(walked, count);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2016-05-31 23:01:53 +02:00
|
|
|
/* erase even */
|
2016-05-26 21:23:19 +02:00
|
|
|
walked = 0;
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_first(iter);
|
|
|
|
while (result == ISC_R_SUCCESS) {
|
2018-11-28 18:57:38 +11:00
|
|
|
unsigned char *tkey = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
void *v = NULL;
|
2018-11-28 18:57:38 +11:00
|
|
|
|
2017-10-20 16:04:59 +11:00
|
|
|
isc_ht_iter_current(iter, &v);
|
2016-05-31 23:01:53 +02:00
|
|
|
isc_ht_iter_currentkey(iter, &tkey, &tksize);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(tksize, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
i = (uintptr_t)v;
|
|
|
|
snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
|
2017-09-27 16:02:02 +10:00
|
|
|
strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_memory_equal(key, tkey, 16);
|
2017-10-20 16:04:59 +11:00
|
|
|
if ((uintptr_t)v % 2 == 1) {
|
2016-05-31 23:01:53 +02:00
|
|
|
result = isc_ht_iter_delcurrent_next(iter);
|
|
|
|
} else {
|
|
|
|
result = isc_ht_iter_next(iter);
|
|
|
|
}
|
|
|
|
walked++;
|
|
|
|
}
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOMORE);
|
2020-02-12 13:59:18 +01:00
|
|
|
assert_int_equal(walked, count / 2);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
walked = 0;
|
2020-02-12 13:59:18 +01:00
|
|
|
for (result = isc_ht_iter_first(iter); result == ISC_R_SUCCESS;
|
2020-02-13 14:44:37 -08:00
|
|
|
result = isc_ht_iter_next(iter))
|
|
|
|
{
|
2016-05-31 23:01:53 +02:00
|
|
|
walked++;
|
|
|
|
}
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOMORE);
|
|
|
|
assert_int_equal(walked, 0);
|
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
/* Iterator doesn't progress rehashing */
|
|
|
|
assert_true(rehashing_in_progress(ht));
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
isc_ht_iter_destroy(&iter);
|
|
|
|
assert_null(iter);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
isc_ht_destroy(&ht);
|
2018-10-24 01:13:13 -07:00
|
|
|
assert_null(ht);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
/* 24 bit, 200K elements test, no rehashing */
|
2018-10-24 01:13:13 -07:00
|
|
|
static void
|
2022-03-16 11:11:38 +01:00
|
|
|
isc_ht_24(void **state) {
|
2018-10-24 01:13:13 -07:00
|
|
|
UNUSED(state);
|
2022-03-16 11:11:38 +01:00
|
|
|
test_ht_full(24, 24, 200000);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
/* 15 bit, 45K elements test, full rehashing */
|
2018-10-24 01:13:13 -07:00
|
|
|
static void
|
2022-03-16 11:11:38 +01:00
|
|
|
isc_ht_15(void **state) {
|
2018-10-24 01:13:13 -07:00
|
|
|
UNUSED(state);
|
2022-03-16 11:11:38 +01:00
|
|
|
test_ht_full(1, 15, 48000);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 11:11:38 +01:00
|
|
|
/* 8 bit, 20k elements test, partial rehashing */
|
2018-10-24 01:13:13 -07:00
|
|
|
static void
|
2022-03-16 11:11:38 +01:00
|
|
|
isc_ht_8(void **state) {
|
2018-10-24 01:13:13 -07:00
|
|
|
UNUSED(state);
|
2022-03-16 11:11:38 +01:00
|
|
|
test_ht_full(8, 14, 20000);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
/* test hashtable iterator */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ht_iterator_test(void **state) {
|
2018-10-24 01:13:13 -07:00
|
|
|
UNUSED(state);
|
|
|
|
test_ht_iterator();
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 01:13:13 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2022-03-16 11:11:38 +01:00
|
|
|
cmocka_unit_test(isc_ht_24),
|
|
|
|
cmocka_unit_test(isc_ht_15),
|
2018-10-24 01:13:13 -07:00
|
|
|
cmocka_unit_test(isc_ht_8),
|
|
|
|
cmocka_unit_test(isc_ht_iterator_test),
|
|
|
|
};
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
return (cmocka_run_group_tests(tests, _setup, _teardown));
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
#else /* HAVE_CMOCKA */
|
2016-05-26 21:23:19 +02:00
|
|
|
|
2018-10-24 01:13:13 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 01:13:13 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2016-05-26 21:23:19 +02:00
|
|
|
}
|
2018-10-24 01:13:13 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|