2013-11-11 10:49:28 +11:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2013-11-11 10:49:28 +11: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 https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2013-11-11 10:49:28 +11:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
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:33:45 -07:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2013-11-11 10:49:28 +11:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-23 22:33:45 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2013-11-11 10:49:28 +11:00
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/lex.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2022-05-03 11:37:31 +02:00
|
|
|
#include <tests/isc.h>
|
2019-11-09 14:01:08 +01:00
|
|
|
|
2020-08-04 00:16:50 +10:00
|
|
|
#define AS_STR(x) (x).value.as_textregion.base
|
|
|
|
|
2018-10-23 22:33:45 -07:00
|
|
|
/* check handling of 0xff */
|
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_RUN_TEST_IMPL(lex_0xff) {
|
2013-11-11 10:49:28 +11:00
|
|
|
isc_result_t result;
|
|
|
|
isc_lex_t *lex = NULL;
|
|
|
|
isc_buffer_t death_buf;
|
|
|
|
isc_token_t token;
|
|
|
|
|
|
|
|
unsigned char death[] = { EOF, 'A' };
|
|
|
|
|
2018-10-23 22:33:45 -07:00
|
|
|
UNUSED(state);
|
2013-11-11 10:49:28 +11:00
|
|
|
|
2022-10-26 12:35:41 +02:00
|
|
|
isc_lex_create(mctx, 1024, &lex);
|
2013-11-11 10:49:28 +11:00
|
|
|
|
|
|
|
isc_buffer_init(&death_buf, &death[0], sizeof(death));
|
|
|
|
isc_buffer_add(&death_buf, sizeof(death));
|
|
|
|
|
|
|
|
result = isc_lex_openbuffer(lex, &death_buf);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2013-11-11 10:49:28 +11:00
|
|
|
|
|
|
|
result = isc_lex_gettoken(lex, 0, &token);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-11-19 07:47:05 +01:00
|
|
|
|
|
|
|
isc_lex_destroy(&lex);
|
2013-11-11 10:49:28 +11:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:33:45 -07:00
|
|
|
/* check setting of source line */
|
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_RUN_TEST_IMPL(lex_setline) {
|
2016-08-25 18:08:26 -07:00
|
|
|
isc_result_t result;
|
|
|
|
isc_lex_t *lex = NULL;
|
|
|
|
unsigned char text[] = "text\nto\nbe\nprocessed\nby\nlexer";
|
|
|
|
isc_buffer_t buf;
|
|
|
|
isc_token_t token;
|
|
|
|
unsigned long line;
|
|
|
|
int i;
|
|
|
|
|
2018-10-23 22:33:45 -07:00
|
|
|
UNUSED(state);
|
2016-08-25 18:08:26 -07:00
|
|
|
|
2022-10-26 12:35:41 +02:00
|
|
|
isc_lex_create(mctx, 1024, &lex);
|
2016-08-25 18:08:26 -07:00
|
|
|
|
|
|
|
isc_buffer_init(&buf, &text[0], sizeof(text));
|
|
|
|
isc_buffer_add(&buf, sizeof(text));
|
|
|
|
|
|
|
|
result = isc_lex_openbuffer(lex, &buf);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-08-25 18:08:26 -07:00
|
|
|
|
|
|
|
result = isc_lex_setsourceline(lex, 100);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-08-25 18:08:26 -07:00
|
|
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
result = isc_lex_gettoken(lex, 0, &token);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-08-25 18:08:26 -07:00
|
|
|
|
|
|
|
line = isc_lex_getsourceline(lex);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(line, 100U + i);
|
2016-08-25 18:08:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result = isc_lex_gettoken(lex, 0, &token);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(result, ISC_R_EOF);
|
2016-08-25 18:08:26 -07:00
|
|
|
|
|
|
|
line = isc_lex_getsourceline(lex);
|
2018-10-23 22:33:45 -07:00
|
|
|
assert_int_equal(line, 105U);
|
2018-11-19 07:47:05 +01:00
|
|
|
|
|
|
|
isc_lex_destroy(&lex);
|
2016-08-25 18:08:26 -07:00
|
|
|
}
|
|
|
|
|
2020-08-04 00:16:50 +10:00
|
|
|
static struct {
|
|
|
|
const char *text;
|
|
|
|
const char *string_value;
|
|
|
|
isc_result_t string_result;
|
|
|
|
isc_tokentype_t string_type;
|
|
|
|
const char *qstring_value;
|
|
|
|
isc_result_t qstring_result;
|
|
|
|
isc_tokentype_t qstring_type;
|
|
|
|
const char *qvpair_value;
|
|
|
|
isc_result_t qvpair_result;
|
|
|
|
isc_tokentype_t qvpair_type;
|
|
|
|
} parse_tests[] = {
|
|
|
|
{ "", "", ISC_R_SUCCESS, isc_tokentype_eof, "", ISC_R_SUCCESS,
|
|
|
|
isc_tokentype_eof, "", ISC_R_SUCCESS, isc_tokentype_eof },
|
|
|
|
{ "1234", "1234", ISC_R_SUCCESS, isc_tokentype_string, "1234",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_string, "1234", ISC_R_SUCCESS,
|
|
|
|
isc_tokentype_string },
|
|
|
|
{ "1234=", "1234=", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"1234=", ISC_R_SUCCESS, isc_tokentype_string, "1234=", ISC_R_SUCCESS,
|
|
|
|
isc_tokentype_vpair },
|
|
|
|
{ "1234=foo", "1234=foo", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"1234=foo", ISC_R_SUCCESS, isc_tokentype_string, "1234=foo",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_vpair },
|
|
|
|
{ "1234=\"foo", "1234=\"foo", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"1234=\"foo", ISC_R_SUCCESS, isc_tokentype_string, NULL,
|
|
|
|
ISC_R_UNEXPECTEDEND, 0 },
|
|
|
|
{ "1234=\"foo\"", "1234=\"foo\"", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"1234=\"foo\"", ISC_R_SUCCESS, isc_tokentype_string, "1234=foo",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_qvpair },
|
|
|
|
{ "key", "key", ISC_R_SUCCESS, isc_tokentype_string, "key",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_string, "key", ISC_R_SUCCESS,
|
|
|
|
isc_tokentype_string },
|
|
|
|
{ "\"key=", "\"key=", ISC_R_SUCCESS, isc_tokentype_string, NULL,
|
|
|
|
ISC_R_UNEXPECTEDEND, 0, "\"key=", ISC_R_SUCCESS,
|
|
|
|
isc_tokentype_vpair },
|
|
|
|
{ "\"key=\"", "\"key=\"", ISC_R_SUCCESS, isc_tokentype_string, "key=",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_qstring, NULL, ISC_R_UNEXPECTEDEND, 0 },
|
|
|
|
{ "key=\"\"", "key=\"\"", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key=\"\"", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key=", ISC_R_SUCCESS, isc_tokentype_qvpair },
|
|
|
|
{ "key=\"a b\"", "key=\"a", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key=\"a", ISC_R_SUCCESS, isc_tokentype_string, "key=a b",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_qvpair },
|
|
|
|
{ "key=\"a\tb\"", "key=\"a", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key=\"a", ISC_R_SUCCESS, isc_tokentype_string, "key=a\tb",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_qvpair },
|
|
|
|
/* double quote not immediately after '=' is not special. */
|
|
|
|
{ "key=c\"a b\"", "key=c\"a", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key=c\"a", ISC_R_SUCCESS, isc_tokentype_string, "key=c\"a",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_vpair },
|
|
|
|
/* remove special meaning for '=' by escaping */
|
|
|
|
{ "key\\=", "key\\=", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\=", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\=", ISC_R_SUCCESS, isc_tokentype_string },
|
|
|
|
{ "key\\=\"a\"", "key\\=\"a\"", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\=\"a\"", ISC_R_SUCCESS, isc_tokentype_string, "key\\=\"a\"",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_string },
|
|
|
|
{ "key\\=\"a \"", "key\\=\"a", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\=\"a", ISC_R_SUCCESS, isc_tokentype_string, "key\\=\"a",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_string },
|
|
|
|
/* vpair with a key of 'key\=' (would need to be deescaped) */
|
|
|
|
{ "key\\==", "key\\==", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\==", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\==", ISC_R_SUCCESS, isc_tokentype_vpair },
|
|
|
|
{ "key\\==\"\"", "key\\==\"\"", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\==\"\"", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key\\==", ISC_R_SUCCESS, isc_tokentype_qvpair },
|
|
|
|
{ "key=\\\\\\\\", "key=\\\\\\\\", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key=\\\\\\\\", ISC_R_SUCCESS, isc_tokentype_string, "key=\\\\\\\\",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_vpair },
|
|
|
|
{ "key=\\\\\\\"", "key=\\\\\\\"", ISC_R_SUCCESS, isc_tokentype_string,
|
|
|
|
"key=\\\\\\\"", ISC_R_SUCCESS, isc_tokentype_string, "key=\\\\\\\"",
|
|
|
|
ISC_R_SUCCESS, isc_tokentype_vpair },
|
|
|
|
/* incomplete escape sequence */
|
|
|
|
{ "key=\\\"\\", NULL, ISC_R_UNEXPECTEDEND, isc_tokentype_string, NULL,
|
|
|
|
ISC_R_UNEXPECTEDEND, 0, NULL, ISC_R_UNEXPECTEDEND, 0 },
|
|
|
|
/* incomplete escape sequence */
|
|
|
|
{ "key=\\", NULL, ISC_R_UNEXPECTEDEND, isc_tokentype_string, NULL,
|
|
|
|
ISC_R_UNEXPECTEDEND, 0, NULL, ISC_R_UNEXPECTEDEND, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* string
|
|
|
|
*/
|
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_RUN_TEST_IMPL(lex_string) {
|
2020-08-04 00:16:50 +10:00
|
|
|
isc_buffer_t buf;
|
|
|
|
isc_lex_t *lex = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_token_t token;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(parse_tests); i++) {
|
2022-10-26 12:35:41 +02:00
|
|
|
isc_lex_create(mctx, 1024, &lex);
|
2020-08-04 00:16:50 +10:00
|
|
|
|
|
|
|
isc_buffer_constinit(&buf, parse_tests[i].text,
|
|
|
|
strlen(parse_tests[i].text));
|
|
|
|
isc_buffer_add(&buf, strlen(parse_tests[i].text));
|
|
|
|
|
|
|
|
result = isc_lex_openbuffer(lex, &buf);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
result = isc_lex_setsourceline(lex, 100);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
memset(&token, 0, sizeof(token));
|
|
|
|
result = isc_lex_getmastertoken(lex, &token,
|
|
|
|
isc_tokentype_string, true);
|
|
|
|
|
|
|
|
assert_int_equal(result, parse_tests[i].string_result);
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
switch (token.type) {
|
|
|
|
case isc_tokentype_string:
|
|
|
|
case isc_tokentype_qstring:
|
|
|
|
case isc_tokentype_vpair:
|
|
|
|
case isc_tokentype_qvpair:
|
|
|
|
assert_int_equal(token.type,
|
|
|
|
parse_tests[i].string_type);
|
|
|
|
assert_string_equal(
|
|
|
|
AS_STR(token),
|
|
|
|
parse_tests[i].string_value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert_int_equal(token.type,
|
|
|
|
parse_tests[i].string_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_lex_destroy(&lex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* qstring
|
|
|
|
*/
|
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_RUN_TEST_IMPL(lex_qstring) {
|
2020-08-04 00:16:50 +10:00
|
|
|
isc_buffer_t buf;
|
|
|
|
isc_lex_t *lex = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_token_t token;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(parse_tests); i++) {
|
2022-10-26 12:35:41 +02:00
|
|
|
isc_lex_create(mctx, 1024, &lex);
|
2020-08-04 00:16:50 +10:00
|
|
|
|
|
|
|
isc_buffer_constinit(&buf, parse_tests[i].text,
|
|
|
|
strlen(parse_tests[i].text));
|
|
|
|
isc_buffer_add(&buf, strlen(parse_tests[i].text));
|
|
|
|
|
|
|
|
result = isc_lex_openbuffer(lex, &buf);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
result = isc_lex_setsourceline(lex, 100);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
memset(&token, 0, sizeof(token));
|
|
|
|
result = isc_lex_getmastertoken(lex, &token,
|
|
|
|
isc_tokentype_qstring, true);
|
|
|
|
|
|
|
|
assert_int_equal(result, parse_tests[i].qstring_result);
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
switch (token.type) {
|
|
|
|
case isc_tokentype_string:
|
|
|
|
case isc_tokentype_qstring:
|
|
|
|
case isc_tokentype_vpair:
|
|
|
|
case isc_tokentype_qvpair:
|
|
|
|
assert_int_equal(token.type,
|
|
|
|
parse_tests[i].qstring_type);
|
|
|
|
assert_string_equal(
|
|
|
|
AS_STR(token),
|
|
|
|
parse_tests[i].qstring_value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert_int_equal(token.type,
|
|
|
|
parse_tests[i].qstring_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_lex_destroy(&lex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 01:25:37 +10:00
|
|
|
/*%
|
|
|
|
* keypair is <string>=<qstring>. This has implications double quotes
|
|
|
|
* in key names.
|
|
|
|
*/
|
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_RUN_TEST_IMPL(lex_keypair) {
|
2020-08-01 01:25:37 +10:00
|
|
|
isc_buffer_t buf;
|
|
|
|
isc_lex_t *lex = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_token_t token;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
2020-08-04 00:16:50 +10:00
|
|
|
for (i = 0; i < ARRAY_SIZE(parse_tests); i++) {
|
2022-10-26 12:35:41 +02:00
|
|
|
isc_lex_create(mctx, 1024, &lex);
|
2020-08-01 01:25:37 +10:00
|
|
|
|
2020-08-04 00:16:50 +10:00
|
|
|
isc_buffer_constinit(&buf, parse_tests[i].text,
|
|
|
|
strlen(parse_tests[i].text));
|
|
|
|
isc_buffer_add(&buf, strlen(parse_tests[i].text));
|
2020-08-01 01:25:37 +10:00
|
|
|
|
|
|
|
result = isc_lex_openbuffer(lex, &buf);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
result = isc_lex_setsourceline(lex, 100);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
memset(&token, 0, sizeof(token));
|
|
|
|
result = isc_lex_getmastertoken(lex, &token,
|
|
|
|
isc_tokentype_qvpair, true);
|
|
|
|
|
2020-08-04 00:16:50 +10:00
|
|
|
assert_int_equal(result, parse_tests[i].qvpair_result);
|
2020-08-01 01:25:37 +10:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
switch (token.type) {
|
|
|
|
case isc_tokentype_string:
|
|
|
|
case isc_tokentype_qstring:
|
|
|
|
case isc_tokentype_vpair:
|
|
|
|
case isc_tokentype_qvpair:
|
2020-08-04 00:16:50 +10:00
|
|
|
assert_int_equal(token.type,
|
|
|
|
parse_tests[i].qvpair_type);
|
|
|
|
assert_string_equal(
|
|
|
|
AS_STR(token),
|
|
|
|
parse_tests[i].qvpair_value);
|
2020-08-01 01:25:37 +10:00
|
|
|
break;
|
|
|
|
default:
|
2020-08-04 00:16:50 +10:00
|
|
|
assert_int_equal(token.type,
|
|
|
|
parse_tests[i].qvpair_type);
|
2020-08-01 01:25:37 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_lex_destroy(&lex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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_TEST_LIST_START
|
|
|
|
ISC_TEST_ENTRY(lex_0xff)
|
|
|
|
ISC_TEST_ENTRY(lex_keypair)
|
|
|
|
ISC_TEST_ENTRY(lex_setline)
|
|
|
|
ISC_TEST_ENTRY(lex_string)
|
|
|
|
ISC_TEST_ENTRY(lex_qstring)
|
|
|
|
ISC_TEST_LIST_END
|
2013-11-11 10:49:28 +11: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
|
|
|
ISC_TEST_MAIN
|