2011-02-26 02:26:33 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2011-02-26 02:26:33 +00: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.
|
2011-02-26 02:26:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
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>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.h>
|
2015-10-06 14:10:49 +11:00
|
|
|
#include <stdlib.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 <string.h>
|
2011-09-03 19:22:43 +00:00
|
|
|
#include <time.h>
|
2011-09-02 21:15:39 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-11-14 20:30:41 +08:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2011-02-26 02:26:33 +00:00
|
|
|
#include <isc/buffer.h>
|
2015-10-02 12:32:42 -07:00
|
|
|
#include <isc/file.h>
|
2011-02-26 02:26:33 +00:00
|
|
|
#include <isc/hash.h>
|
2017-07-25 07:34:27 +02:00
|
|
|
#include <isc/hex.h>
|
2017-10-25 09:21:57 +02:00
|
|
|
#include <isc/lex.h>
|
2021-04-27 00:07:43 +02:00
|
|
|
#include <isc/managers.h>
|
2011-02-26 02:26:33 +00:00
|
|
|
#include <isc/mem.h>
|
2021-01-14 13:02:57 -08:00
|
|
|
#include <isc/netmgr.h>
|
2011-07-06 01:36:32 +00:00
|
|
|
#include <isc/os.h>
|
2015-10-02 14:38:59 -07:00
|
|
|
#include <isc/print.h>
|
2022-03-18 10:14:17 +01:00
|
|
|
#include <isc/random.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2015-10-02 12:32:42 -07:00
|
|
|
#include <isc/stdio.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/string.h>
|
2011-07-06 01:36:32 +00:00
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/timer.h>
|
2011-02-26 02:26:33 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2018-10-25 00:20:56 -07:00
|
|
|
#include <dns/callbacks.h>
|
2011-12-05 20:51:41 +00:00
|
|
|
#include <dns/db.h>
|
2011-09-02 21:15:39 +00:00
|
|
|
#include <dns/fixedname.h>
|
2011-02-26 02:26:33 +00:00
|
|
|
#include <dns/log.h>
|
|
|
|
#include <dns/name.h>
|
2011-09-02 21:15:39 +00:00
|
|
|
#include <dns/view.h>
|
|
|
|
#include <dns/zone.h>
|
2011-02-26 02:26:33 +00:00
|
|
|
|
2022-05-03 11:37:31 +02:00
|
|
|
#include <tests/dns.h>
|
2011-02-26 02:26:33 +00:00
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
dns_zonemgr_t *zonemgr = NULL;
|
2011-02-26 02:26:33 +00:00
|
|
|
|
|
|
|
/*
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
* Create a view.
|
2011-02-26 02:26:33 +00:00
|
|
|
*/
|
|
|
|
isc_result_t
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
dns_test_makeview(const char *name, bool with_cache, dns_view_t **viewp) {
|
2011-02-26 02:26:33 +00:00
|
|
|
isc_result_t result;
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
dns_view_t *view = NULL;
|
|
|
|
dns_cache_t *cache = NULL;
|
2011-02-26 02:26:33 +00:00
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_view_create(mctx, dns_rdataclass_in, name, &view);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
2018-11-15 03:40:56 +00:00
|
|
|
}
|
2011-09-02 21:15:39 +00:00
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
if (with_cache) {
|
|
|
|
result = dns_cache_create(mctx, mctx, taskmgr, timermgr,
|
|
|
|
dns_rdataclass_in, "", "rbt", 0, NULL,
|
|
|
|
&cache);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
dns_view_detach(&view);
|
|
|
|
return (result);
|
|
|
|
}
|
2016-07-11 13:36:16 +10:00
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
dns_view_setcache(view, cache, false);
|
|
|
|
/*
|
|
|
|
* Reference count for "cache" is now at 2, so decrement it in
|
|
|
|
* order for the cache to be automatically freed when "view"
|
|
|
|
* gets freed.
|
|
|
|
*/
|
|
|
|
dns_cache_detach(&cache);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2018-08-24 11:21:27 +02:00
|
|
|
|
2014-05-29 22:22:53 -07:00
|
|
|
*viewp = view;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2011-09-02 21:15:39 +00:00
|
|
|
isc_result_t
|
|
|
|
dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
|
2020-02-13 14:44:37 -08:00
|
|
|
bool createview) {
|
2018-03-13 14:06:04 +01:00
|
|
|
dns_fixedname_t fixed_origin;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_zone_t *zone = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
dns_name_t *origin;
|
2011-09-02 21:15:39 +00:00
|
|
|
|
2018-03-13 14:06:04 +01:00
|
|
|
REQUIRE(view == NULL || !createview);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the zone structure.
|
|
|
|
*/
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_zone_create(&zone, mctx, 0);
|
2018-03-13 14:06:04 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set zone type and origin.
|
|
|
|
*/
|
2021-08-25 22:29:25 -07:00
|
|
|
dns_zone_settype(zone, dns_zone_primary);
|
2018-03-13 14:06:04 +01:00
|
|
|
origin = dns_fixedname_initname(&fixed_origin);
|
|
|
|
result = dns_name_fromstring(origin, name, 0, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto detach_zone;
|
|
|
|
}
|
|
|
|
result = dns_zone_setorigin(zone, origin);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto detach_zone;
|
|
|
|
}
|
2011-09-02 21:15:39 +00:00
|
|
|
|
2018-03-13 14:06:04 +01:00
|
|
|
/*
|
|
|
|
* If requested, create a view.
|
|
|
|
*/
|
|
|
|
if (createview) {
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_test_makeview("view", false, &view);
|
2018-03-13 14:06:04 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto detach_zone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a view was passed as an argument or created above, attach the
|
|
|
|
* created zone to it. Otherwise, set the zone's class to IN.
|
|
|
|
*/
|
|
|
|
if (view != NULL) {
|
|
|
|
dns_zone_setview(zone, view);
|
|
|
|
dns_zone_setclass(zone, view->rdclass);
|
|
|
|
dns_view_addzone(view, zone);
|
|
|
|
} else {
|
|
|
|
dns_zone_setclass(zone, dns_rdataclass_in);
|
|
|
|
}
|
2011-09-02 21:15:39 +00:00
|
|
|
|
|
|
|
*zonep = zone;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
detach_zone:
|
2018-03-13 14:06:04 +01:00
|
|
|
dns_zone_detach(&zone);
|
|
|
|
|
2011-09-02 21:15:39 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_test_setupzonemgr(void) {
|
2011-09-02 21:15:39 +00:00
|
|
|
isc_result_t result;
|
|
|
|
REQUIRE(zonemgr == NULL);
|
2011-09-02 23:46:33 +00:00
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_zonemgr_create(mctx, taskmgr, timermgr, netmgr, &zonemgr);
|
2011-09-02 21:15:39 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_test_managezone(dns_zone_t *zone) {
|
2011-09-02 21:15:39 +00:00
|
|
|
isc_result_t result;
|
|
|
|
REQUIRE(zonemgr != NULL);
|
|
|
|
|
|
|
|
result = dns_zonemgr_managezone(zonemgr, zone);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_test_releasezone(dns_zone_t *zone) {
|
2011-09-02 21:15:39 +00:00
|
|
|
REQUIRE(zonemgr != NULL);
|
|
|
|
dns_zonemgr_releasezone(zonemgr, zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_test_closezonemgr(void) {
|
2011-09-02 21:15:39 +00:00
|
|
|
REQUIRE(zonemgr != NULL);
|
2011-09-02 23:46:33 +00:00
|
|
|
|
2011-09-02 21:15:39 +00:00
|
|
|
dns_zonemgr_shutdown(zonemgr);
|
|
|
|
dns_zonemgr_detach(&zonemgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sleep for 'usec' microseconds.
|
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_test_nap(uint32_t usec) {
|
2011-09-02 21:15:39 +00:00
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ts.tv_sec = usec / 1000000;
|
|
|
|
ts.tv_nsec = (usec % 1000000) * 1000;
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
}
|
2011-12-04 23:48:12 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_test_loaddb(dns_db_t **db, dns_dbtype_t dbtype, const char *origin,
|
2020-02-13 14:44:37 -08:00
|
|
|
const char *testfile) {
|
|
|
|
isc_result_t result;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_fixedname_t fixed;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *name;
|
2011-12-04 23:48:12 +00:00
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
name = dns_fixedname_initname(&fixed);
|
2011-12-04 23:48:12 +00:00
|
|
|
|
|
|
|
result = dns_name_fromstring(name, origin, 0, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-12-04 23:48:12 +00:00
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_db_create(mctx, "rbt", name, dbtype, dns_rdataclass_in, 0,
|
|
|
|
NULL, db);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2011-12-04 23:48:12 +00:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2011-12-04 23:48:12 +00:00
|
|
|
|
2018-04-03 13:22:09 +02:00
|
|
|
result = dns_db_load(*db, testfile, dns_masterformat_text, 0);
|
2011-12-04 23:48:12 +00:00
|
|
|
return (result);
|
|
|
|
}
|
2015-10-02 12:32:42 -07:00
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
fromhex(char c) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (c >= '0' && c <= '9') {
|
2015-10-02 12:32:42 -07:00
|
|
|
return (c - '0');
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
2015-10-02 12:32:42 -07:00
|
|
|
return (c - 'a' + 10);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
2015-10-02 12:32:42 -07:00
|
|
|
return (c - 'A' + 10);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-10-02 12:32:42 -07:00
|
|
|
|
|
|
|
printf("bad input format: %02x\n", c);
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
2017-07-25 07:34:27 +02:00
|
|
|
/*
|
|
|
|
* Format contents of given memory region as a hex string, using the buffer
|
|
|
|
* of length 'buflen' pointed to by 'buf'. 'buflen' must be at least three
|
|
|
|
* times 'len'. Always returns 'buf'.
|
|
|
|
*/
|
|
|
|
char *
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_test_tohex(const unsigned char *data, size_t len, char *buf,
|
|
|
|
size_t buflen) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_constregion_t source = { .base = data, .length = len };
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t target;
|
|
|
|
isc_result_t result;
|
2017-07-25 07:34:27 +02:00
|
|
|
|
|
|
|
memset(buf, 0, buflen);
|
|
|
|
isc_buffer_init(&target, buf, buflen);
|
|
|
|
result = isc_hex_totext((isc_region_t *)&source, 1, " ", &target);
|
2018-11-14 20:30:41 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-25 07:34:27 +02:00
|
|
|
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2015-10-02 12:32:42 -07:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_test_getdata(const char *file, unsigned char *buf, size_t bufsiz,
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t *sizep) {
|
|
|
|
isc_result_t result;
|
2015-10-02 12:32:42 -07:00
|
|
|
unsigned char *bp;
|
2020-02-13 14:44:37 -08:00
|
|
|
char *rp, *wp;
|
|
|
|
char s[BUFSIZ];
|
|
|
|
size_t len, i;
|
|
|
|
FILE *f = NULL;
|
|
|
|
int n;
|
2015-10-02 12:32:42 -07:00
|
|
|
|
|
|
|
result = isc_stdio_open(file, "r", &f);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2015-10-02 12:32:42 -07:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-10-02 12:32:42 -07:00
|
|
|
|
|
|
|
bp = buf;
|
|
|
|
while (fgets(s, sizeof(s), f) != NULL) {
|
|
|
|
rp = s;
|
|
|
|
wp = s;
|
|
|
|
len = 0;
|
|
|
|
while (*rp != '\0') {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*rp == '#') {
|
2015-10-02 12:32:42 -07:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (*rp != ' ' && *rp != '\t' && *rp != '\r' &&
|
|
|
|
*rp != '\n') {
|
2015-10-02 12:32:42 -07:00
|
|
|
*wp++ = *rp;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
rp++;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (len == 0U) {
|
2015-10-02 12:32:42 -07:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (len % 2 != 0U) {
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = ISC_R_UNEXPECTEDEND;
|
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (len > bufsiz * 2) {
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-10-02 12:32:42 -07:00
|
|
|
rp = s;
|
|
|
|
for (i = 0; i < len; i += 2) {
|
|
|
|
n = fromhex(*rp++);
|
|
|
|
n *= 16;
|
|
|
|
n += fromhex(*rp++);
|
|
|
|
*bp++ = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
*sizep = bp - buf;
|
|
|
|
}
|
2015-10-08 09:56:48 -07:00
|
|
|
|
2015-10-16 17:23:35 +11:00
|
|
|
isc_stdio_close(f);
|
2015-10-08 09:56:48 -07:00
|
|
|
return (result);
|
2015-10-02 12:32:42 -07:00
|
|
|
}
|
2017-10-25 09:21:57 +02:00
|
|
|
|
2018-10-25 00:20:56 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
nullmsg(dns_rdatacallbacks_t *cb, const char *fmt, ...) {
|
2018-10-25 00:20:56 -07:00
|
|
|
UNUSED(cb);
|
|
|
|
UNUSED(fmt);
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:21:57 +02:00
|
|
|
isc_result_t
|
2018-05-09 13:12:08 +02:00
|
|
|
dns_test_rdatafromstring(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
|
|
|
|
dns_rdatatype_t rdtype, unsigned char *dst,
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t dstlen, const char *src, bool warnings) {
|
2018-10-25 00:20:56 -07:00
|
|
|
dns_rdatacallbacks_t callbacks;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t source, target;
|
|
|
|
isc_lex_t *lex = NULL;
|
|
|
|
isc_lexspecials_t specials = { 0 };
|
|
|
|
isc_result_t result;
|
|
|
|
size_t length;
|
2017-10-25 09:21:57 +02:00
|
|
|
|
|
|
|
REQUIRE(rdata != NULL);
|
|
|
|
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
|
|
|
|
REQUIRE(dst != NULL);
|
|
|
|
REQUIRE(src != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up source to hold the input string.
|
|
|
|
*/
|
|
|
|
length = strlen(src);
|
|
|
|
isc_buffer_constinit(&source, src, length);
|
|
|
|
isc_buffer_add(&source, length);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a lexer as one is required by dns_rdata_fromtext().
|
|
|
|
*/
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = isc_lex_create(mctx, 64, &lex);
|
2017-10-25 09:21:57 +02:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:00:15 +11:00
|
|
|
/*
|
|
|
|
* Set characters which will be treated as valid multi-line RDATA
|
|
|
|
* delimiters while reading the source string. These should match
|
|
|
|
* specials from lib/dns/master.c.
|
|
|
|
*/
|
|
|
|
specials[0] = 1;
|
|
|
|
specials['('] = 1;
|
|
|
|
specials[')'] = 1;
|
|
|
|
specials['"'] = 1;
|
|
|
|
isc_lex_setspecials(lex, specials);
|
|
|
|
|
2019-04-11 18:54:24 +10:00
|
|
|
/*
|
|
|
|
* Expect DNS masterfile comments.
|
|
|
|
*/
|
|
|
|
isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
|
|
|
|
|
2017-10-25 09:21:57 +02:00
|
|
|
/*
|
|
|
|
* Point lexer at source.
|
|
|
|
*/
|
|
|
|
result = isc_lex_openbuffer(lex, &source);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto destroy_lexer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up target for storing uncompressed wire form of provided RDATA.
|
|
|
|
*/
|
|
|
|
isc_buffer_init(&target, dst, dstlen);
|
|
|
|
|
2018-10-25 00:20:56 -07:00
|
|
|
/*
|
|
|
|
* Set up callbacks so warnings and errors are not printed.
|
|
|
|
*/
|
|
|
|
if (!warnings) {
|
|
|
|
dns_rdatacallbacks_init(&callbacks);
|
|
|
|
callbacks.warn = callbacks.error = nullmsg;
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:21:57 +02:00
|
|
|
/*
|
|
|
|
* Parse input string, determining result.
|
|
|
|
*/
|
|
|
|
result = dns_rdata_fromtext(rdata, rdclass, rdtype, lex, dns_rootname,
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
0, mctx, &target, &callbacks);
|
2017-10-25 09:21:57 +02:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
destroy_lexer:
|
2017-10-25 09:21:57 +02:00
|
|
|
isc_lex_destroy(&lex);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
2018-02-28 14:12:02 -08:00
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_test_namefromstring(const char *namestr, dns_fixedname_t *fname) {
|
|
|
|
size_t length;
|
2018-02-28 14:12:02 -08:00
|
|
|
isc_buffer_t *b = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
dns_name_t *name;
|
2018-02-28 14:12:02 -08:00
|
|
|
|
|
|
|
length = strlen(namestr);
|
|
|
|
|
2018-11-14 20:30:41 +08:00
|
|
|
name = dns_fixedname_initname(fname);
|
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
isc_buffer_allocate(mctx, &b, length);
|
2018-02-28 14:12:02 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_buffer_putmem(b, (const unsigned char *)namestr, length);
|
2018-02-28 14:12:02 -08:00
|
|
|
result = dns_name_fromtext(name, b, dns_rootname, 0, NULL);
|
2018-11-14 20:30:41 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-28 14:12:02 -08:00
|
|
|
|
|
|
|
isc_buffer_free(&b);
|
|
|
|
}
|
2018-03-13 14:06:06 +01:00
|
|
|
|
|
|
|
isc_result_t
|
2018-10-25 00:20:56 -07:00
|
|
|
dns_test_difffromchanges(dns_diff_t *diff, const zonechange_t *changes,
|
2020-02-13 14:44:37 -08:00
|
|
|
bool warnings) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
unsigned char rdata_buf[1024];
|
|
|
|
dns_difftuple_t *tuple = NULL;
|
2018-03-13 14:06:06 +01:00
|
|
|
isc_consttextregion_t region;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdatatype_t rdatatype;
|
|
|
|
dns_fixedname_t fixedname;
|
|
|
|
dns_rdata_t rdata;
|
|
|
|
dns_name_t *name;
|
|
|
|
size_t i;
|
2018-03-13 14:06:06 +01:00
|
|
|
|
|
|
|
REQUIRE(diff != NULL);
|
|
|
|
REQUIRE(changes != NULL);
|
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
dns_diff_init(mctx, diff);
|
2018-03-13 14:06:06 +01:00
|
|
|
|
|
|
|
for (i = 0; changes[i].owner != NULL; i++) {
|
|
|
|
/*
|
|
|
|
* Parse owner name.
|
|
|
|
*/
|
|
|
|
name = dns_fixedname_initname(&fixedname);
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_name_fromstring(name, changes[i].owner, 0, mctx);
|
2018-03-13 14:06:06 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse RDATA type.
|
|
|
|
*/
|
|
|
|
region.base = changes[i].type;
|
|
|
|
region.length = strlen(changes[i].type);
|
|
|
|
result = dns_rdatatype_fromtext(&rdatatype,
|
|
|
|
(isc_textregion_t *)®ion);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse RDATA.
|
|
|
|
*/
|
|
|
|
dns_rdata_init(&rdata);
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_test_rdatafromstring(
|
|
|
|
&rdata, dns_rdataclass_in, rdatatype, rdata_buf,
|
|
|
|
sizeof(rdata_buf), changes[i].rdata, warnings);
|
2018-03-13 14:06:06 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a diff tuple for the parsed change and append it to
|
|
|
|
* the diff.
|
|
|
|
*/
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_difftuple_create(mctx, changes[i].op, name,
|
2018-03-13 14:06:06 +01:00
|
|
|
changes[i].ttl, &rdata, &tuple);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dns_diff_append(diff, &tuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
dns_diff_clear(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|