From eb78ad20803b28f1a5ede52115ad6bd73cfeb843 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Mon, 9 Dec 2024 14:45:38 +1100 Subject: [PATCH 1/2] Fix parsing of unknown directives in resolv.conf Only call eatline() to skip to the next line if we're not already at the end of a line when parsing an unknown directive. We were accidentally skipping the next line when there was only a single unknown directive on the current line. --- lib/dns/resconf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/dns/resconf.c b/lib/dns/resconf.c index 47e8e4e663..d1d660e60a 100644 --- a/lib/dns/resconf.c +++ b/lib/dns/resconf.c @@ -585,9 +585,11 @@ irs_resconf_load(isc_mem_t *mctx, const char *filename, irs_resconf_t **confp) { } else { /* unrecognised word. Ignore entire line */ rval = ISC_R_SUCCESS; - stopchar = eatline(fp); - if (stopchar == EOF) { - break; + if (stopchar != '\n') { + stopchar = eatline(fp); + if (stopchar == EOF) { + break; + } } } if (ret == ISC_R_SUCCESS && rval != ISC_R_SUCCESS) { From c44c4fcbfbc65ffc8226c461b172c2bef83872f8 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Mon, 9 Dec 2024 13:04:05 +1100 Subject: [PATCH 2/2] Extend resconf_test Update to the new unit test framework. Add a test for an unknown directive without any arguments. Add test for an unknown directive without arguments, followed by a search directive. --- tests/irs/resconf_test.c | 77 +++++++++---------- tests/irs/testdata/unknown+search.conf | 13 ++++ .../{unknown.conf => unknown-with-value.conf} | 0 tests/irs/testdata/unknown-without-value.conf | 12 +++ 4 files changed, 61 insertions(+), 41 deletions(-) create mode 100644 tests/irs/testdata/unknown+search.conf rename tests/irs/testdata/{unknown.conf => unknown-with-value.conf} (100%) create mode 100644 tests/irs/testdata/unknown-without-value.conf diff --git a/tests/irs/resconf_test.c b/tests/irs/resconf_test.c index ac21bbf1dd..35d966d241 100644 --- a/tests/irs/resconf_test.c +++ b/tests/irs/resconf_test.c @@ -11,8 +11,6 @@ * information regarding copyright ownership. */ -#if HAVE_CMOCKA - #include #include /* IWYU pragma: keep */ #include @@ -31,19 +29,7 @@ #include -static isc_mem_t *mctx = NULL; - -static void -setup_test(void) { - isc_mem_create(&mctx); - - /* - * the caller might run from another directory, but tests - * that access test data files must first chdir to the proper - * location. - */ - assert_return_code(chdir(TESTS_DIR), 0); -} +#include static isc_result_t check_number(unsigned int n, unsigned int expected) { @@ -65,6 +51,24 @@ check_ndots(irs_resconf_t *resconf) { return check_number(irs_resconf_getndots(resconf), 2); } +static isc_result_t +search_example(irs_resconf_t *resconf) { + irs_resconf_search_t *entry; + irs_resconf_searchlist_t *list; + list = irs_resconf_getsearchlist(resconf); + if (list == NULL) { + return ISC_R_NOTFOUND; + } + entry = ISC_LIST_HEAD(*list); + assert_true(entry != NULL && entry->domain != NULL); + assert_string_equal(entry->domain, "example.com"); + + entry = ISC_LIST_TAIL(*list); + assert_true(entry != NULL && entry->domain != NULL); + assert_string_equal(entry->domain, "example.net"); + return ISC_R_SUCCESS; +} + static isc_result_t check_options(irs_resconf_t *resconf) { if (irs_resconf_getattempts(resconf) != 3) { @@ -83,8 +87,7 @@ check_options(irs_resconf_t *resconf) { } /* test irs_resconf_load() */ -static void -irs_resconf_load_test(void **state) { +ISC_RUN_TEST_IMPL(irs_resconf_load) { isc_result_t result; irs_resconf_t *resconf = NULL; unsigned int i; @@ -119,18 +122,27 @@ irs_resconf_load_test(void **state) { ISC_R_SUCCESS }, { "testdata/port.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }, { "testdata/resolv.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }, - { "testdata/search.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }, + { "testdata/search.conf", ISC_R_SUCCESS, search_example, + ISC_R_SUCCESS }, { "testdata/sortlist-v4.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }, { "testdata/timeout.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }, - { "testdata/unknown.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS } + { "testdata/unknown-with-value.conf", ISC_R_SUCCESS, NULL, + ISC_R_SUCCESS }, + { "testdata/unknown-without-value.conf", ISC_R_SUCCESS, NULL, + ISC_R_SUCCESS }, + { "testdata/unknown+search.conf", ISC_R_SUCCESS, search_example, + ISC_R_SUCCESS } }; UNUSED(state); - setup_test(); + assert_return_code(chdir(TESTS_DIR), 0); for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) { + if (debug) { + fprintf(stderr, "# testing '%s'\n", tests[i].file); + } result = irs_resconf_load(mctx, tests[i].file, &resconf); if (result != tests[i].loadres) { fail_msg("# unexpected result %s loading %s", @@ -156,27 +168,10 @@ irs_resconf_load_test(void **state) { irs_resconf_destroy(&resconf); } } - - isc_mem_detach(&mctx); } -int -main(void) { - const struct CMUnitTest tests[] = { - cmocka_unit_test(irs_resconf_load_test), - }; +ISC_TEST_LIST_START +ISC_TEST_ENTRY(irs_resconf_load) +ISC_TEST_LIST_END - return cmocka_run_group_tests(tests, NULL, NULL); -} - -#else /* HAVE_CMOCKA */ - -#include - -int -main(void) { - printf("1..0 # Skipped: cmocka not available\n"); - return SKIPPED_TEST_EXIT_CODE; -} - -#endif /* if HAVE_CMOCKA */ +ISC_TEST_MAIN diff --git a/tests/irs/testdata/unknown+search.conf b/tests/irs/testdata/unknown+search.conf new file mode 100644 index 0000000000..7e9cec3f14 --- /dev/null +++ b/tests/irs/testdata/unknown+search.conf @@ -0,0 +1,13 @@ +# Copyright (C) Internet Systems Consortium, Inc. ("ISC") +# +# SPDX-License-Identifier: MPL-2.0 +# +# 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/. +# +# See the COPYRIGHT file distributed with this work for additional +# information regarding copyright ownership. + +unknown +search example.com example.net diff --git a/tests/irs/testdata/unknown.conf b/tests/irs/testdata/unknown-with-value.conf similarity index 100% rename from tests/irs/testdata/unknown.conf rename to tests/irs/testdata/unknown-with-value.conf diff --git a/tests/irs/testdata/unknown-without-value.conf b/tests/irs/testdata/unknown-without-value.conf new file mode 100644 index 0000000000..94d12c1099 --- /dev/null +++ b/tests/irs/testdata/unknown-without-value.conf @@ -0,0 +1,12 @@ +# Copyright (C) Internet Systems Consortium, Inc. ("ISC") +# +# SPDX-License-Identifier: MPL-2.0 +# +# 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/. +# +# See the COPYRIGHT file distributed with this work for additional +# information regarding copyright ownership. + +unknown