2011-11-30 04:27:17 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2011-11-30 04:27:17 +00: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.
|
2011-11-30 04:27:17 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-23 22:52:30 -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:52:30 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-11-30 04:27:17 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2011-11-30 06:09:41 +00:00
|
|
|
#include <isc/print.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/symtab.h>
|
2018-10-23 22:52:30 -07:00
|
|
|
#include <isc/util.h>
|
2011-11-30 04:27:17 +00:00
|
|
|
|
|
|
|
#include "isctest.h"
|
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-23 22:52:30 -07: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) {
|
2018-10-23 22:52:30 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-11-30 04:27:17 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) {
|
2011-11-30 04:27:17 +00:00
|
|
|
UNUSED(arg);
|
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_int_equal(type, 1);
|
2019-11-09 14:01:08 +01:00
|
|
|
isc_mem_free(test_mctx, key);
|
|
|
|
isc_mem_free(test_mctx, value.as_pointer);
|
2011-11-30 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
/* test symbol table growth */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
symtab_grow(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_symtab_t *st = NULL;
|
|
|
|
isc_symvalue_t value;
|
2011-11-30 04:27:17 +00:00
|
|
|
isc_symexists_t policy = isc_symexists_reject;
|
2020-02-13 14:44:37 -08:00
|
|
|
int i;
|
2011-11-30 04:27:17 +00:00
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
UNUSED(state);
|
2011-11-30 04:27:17 +00:00
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
result = isc_symtab_create(test_mctx, 3, undefine, NULL, false, &st);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_non_null(st);
|
2011-11-30 04:27:17 +00:00
|
|
|
|
|
|
|
/* Nothing should be in the table yet */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put 1024 entries in the table (this should necessate
|
|
|
|
* regrowing the hash table several times
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 1024; i++) {
|
|
|
|
char str[16], *key;
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%04x", i);
|
2019-11-09 14:01:08 +01:00
|
|
|
key = isc_mem_strdup(test_mctx, str);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_non_null(key);
|
2019-11-09 14:01:08 +01:00
|
|
|
value.as_pointer = isc_mem_strdup(test_mctx, str);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_non_null(value.as_pointer);
|
2011-11-30 04:27:17 +00:00
|
|
|
result = isc_symtab_define(st, key, 1, value, policy);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2011-11-30 04:27:17 +00:00
|
|
|
undefine(key, 1, value, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-11-30 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to put them in again; this should fail
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 1024; i++) {
|
|
|
|
char str[16], *key;
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%04x", i);
|
2019-11-09 14:01:08 +01:00
|
|
|
key = isc_mem_strdup(test_mctx, str);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_non_null(key);
|
2019-11-09 14:01:08 +01:00
|
|
|
value.as_pointer = isc_mem_strdup(test_mctx, str);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_non_null(value.as_pointer);
|
2011-11-30 04:27:17 +00:00
|
|
|
result = isc_symtab_define(st, key, 1, value, policy);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_int_equal(result, ISC_R_EXISTS);
|
2011-11-30 04:27:17 +00:00
|
|
|
undefine(key, 1, value, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve them; this should succeed
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 1024; i++) {
|
|
|
|
char str[16];
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%04x", i);
|
|
|
|
result = isc_symtab_lookup(st, str, 0, &value);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(str, (char *)value.as_pointer);
|
2011-11-30 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undefine them
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 1024; i++) {
|
|
|
|
char str[16];
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%04x", i);
|
|
|
|
result = isc_symtab_undefine(st, str, 1);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2011-11-30 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve them again; this should fail
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 1024; i++) {
|
|
|
|
char str[16];
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%04x", i);
|
|
|
|
result = isc_symtab_lookup(st, str, 0, &value);
|
2018-10-23 22:52:30 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
2011-11-30 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_symtab_destroy(&st);
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-23 22:52:30 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(symtab_grow, _setup, _teardown),
|
2018-10-23 22:52:30 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2011-11-30 04:27:17 +00:00
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-23 22:52:30 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2011-11-30 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|