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
|
|
|
|
* file, You can obtain one at http://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
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <setjmp.h>
|
2011-11-30 04:27:17 +00:00
|
|
|
|
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 */
|
2018-10-23 22:52:30 -07:00
|
|
|
#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 04:27:17 +00:00
|
|
|
#include <isc/symtab.h>
|
2011-11-30 06:09:41 +00:00
|
|
|
#include <isc/print.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
|
|
|
|
_setup(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_teardown(void **state) {
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-11-30 04:27:17 +00:00
|
|
|
static void
|
|
|
|
undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) {
|
|
|
|
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
|
|
|
|
symtab_grow(void **state) {
|
2011-11-30 04:27:17 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_symtab_t *st = NULL;
|
|
|
|
isc_symvalue_t value;
|
|
|
|
isc_symexists_t policy = isc_symexists_reject;
|
|
|
|
int i;
|
|
|
|
|
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);
|
2011-11-30 04:27:17 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
undefine(key, 1, value, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
main(void) {
|
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(symtab_grow,
|
|
|
|
_setup, _teardown),
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
main(void) {
|
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2011-11-30 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:52:30 -07:00
|
|
|
#endif
|