diff --git a/lib/dns/tests/rdata_test.c b/lib/dns/tests/rdata_test.c index 0f091214ba..e44b2f1cab 100644 --- a/lib/dns/tests/rdata_test.c +++ b/lib/dns/tests/rdata_test.c @@ -21,8 +21,9 @@ #include #define UNIT_TESTING -#include +#include +#include #include #include #include @@ -2656,14 +2657,44 @@ main(int argc, char **argv) { cmocka_unit_test_setup_teardown(atparent, NULL, NULL), cmocka_unit_test_setup_teardown(iszonecutauth, NULL, NULL), }; + struct CMUnitTest selected[sizeof(tests) / sizeof(tests[0])]; + size_t i; + int c; - UNUSED(argv); + memset(selected, 0, sizeof(selected)); - if (argc > 1) { - debug = true; + while ((c = isc_commandline_parse(argc, argv, "dlt:")) != -1) { + switch (c) { + case 'd': + debug = true; + break; + case 'l': + for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) + { + if (tests[i].name != NULL) { + fprintf(stdout, "%s\n", tests[i].name); + } + } + return (0); + case 't': + if (!cmocka_add_test_byname( + tests, isc_commandline_argument, selected)) + { + fprintf(stderr, "unknown test '%s'\n", + isc_commandline_argument); + exit(1); + } + break; + default: + break; + } } - return (cmocka_run_group_tests(tests, NULL, NULL)); + if (selected[0].name != NULL) { + return (cmocka_run_group_tests(selected, NULL, NULL)); + } else { + return (cmocka_run_group_tests(tests, NULL, NULL)); + } } #else /* HAVE_CMOCKA */ diff --git a/lib/isc/Makefile.am b/lib/isc/Makefile.am index 2c84285b16..c6b63a410b 100644 --- a/lib/isc/Makefile.am +++ b/lib/isc/Makefile.am @@ -16,6 +16,7 @@ libisc_la_HEADERS = \ include/isc/bind9.h \ include/isc/buffer.h \ include/isc/bufferlist.h \ + include/isc/cmocka.h \ include/isc/commandline.h \ include/isc/counter.h \ include/isc/crc64.h \ diff --git a/lib/isc/include/isc/cmocka.h b/lib/isc/include/isc/cmocka.h new file mode 100644 index 0000000000..aadfcadab1 --- /dev/null +++ b/lib/isc/include/isc/cmocka.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) Internet Systems Consortium, Inc. ("ISC") + * + * 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. + */ + +/*! \file isc/cmocka.h */ + +#pragma once + +#include + +#include + +ISC_LANG_BEGINDECLS + +/* + * Copy the test identified by 'name' from 'tests' to 'selected'. + */ +#define cmocka_add_test_byname(tests, name, selected) \ + _cmocka_add_test_byname(tests, sizeof(tests) / sizeof(tests[0]), name, \ + selected, \ + sizeof(selected) / sizeof(selected[0])) + +static inline bool +_cmocka_add_test_byname(const struct CMUnitTest *tests, size_t ntests, + const char *name, struct CMUnitTest *selected, + size_t nselected) { + size_t i, j; + + for (i = 0; i < ntests && tests[i].name != NULL; i++) { + if (strcmp(tests[i].name, name) != 0) { + continue; + } + for (j = 0; j < nselected && selected[j].name != NULL; j++) { + if (strcmp(tests[j].name, name) == 0) { + break; + } + } + if (j < nselected && selected[j].name == NULL) { + selected[j] = tests[i]; + } + return (true); + } + return (false); +} + +ISC_LANG_ENDDECLS diff --git a/lib/isc/tests/task_test.c b/lib/isc/tests/task_test.c index 43403d63d3..9cbe1c99c6 100644 --- a/lib/isc/tests/task_test.c +++ b/lib/isc/tests/task_test.c @@ -22,9 +22,9 @@ #include #define UNIT_TESTING -#include #include +#include #include #include #include @@ -1528,31 +1528,52 @@ purgeevent_notpurge(void **state) { int main(int argc, char **argv) { const struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(create_task, _setup, _teardown), - cmocka_unit_test_setup_teardown(shutdown, _setup4, _teardown), cmocka_unit_test(manytasks), cmocka_unit_test_setup_teardown(all_events, _setup, _teardown), cmocka_unit_test_setup_teardown(basic, _setup2, _teardown), - cmocka_unit_test_setup_teardown(privileged_events, _setup, - _teardown), - cmocka_unit_test_setup_teardown(privilege_drop, _setup, - _teardown), - cmocka_unit_test_setup_teardown(task_exclusive, _setup4, + cmocka_unit_test_setup_teardown(create_task, _setup, _teardown), + cmocka_unit_test_setup_teardown(pause_unpause, _setup, _teardown), cmocka_unit_test_setup_teardown(post_shutdown, _setup2, _teardown), + cmocka_unit_test_setup_teardown(privilege_drop, _setup, + _teardown), + cmocka_unit_test_setup_teardown(privileged_events, _setup, + _teardown), cmocka_unit_test_setup_teardown(purge, _setup2, _teardown), - cmocka_unit_test_setup_teardown(purgerange, _setup, _teardown), cmocka_unit_test_setup_teardown(purgeevent, _setup2, _teardown), cmocka_unit_test_setup_teardown(purgeevent_notpurge, _setup, _teardown), - cmocka_unit_test_setup_teardown(pause_unpause, _setup, + cmocka_unit_test_setup_teardown(purgerange, _setup, _teardown), + cmocka_unit_test_setup_teardown(shutdown, _setup4, _teardown), + cmocka_unit_test_setup_teardown(task_exclusive, _setup4, _teardown), }; + struct CMUnitTest selected[sizeof(tests) / sizeof(tests[0])]; + size_t i; int c; - while ((c = isc_commandline_parse(argc, argv, "v")) != -1) { + memset(selected, 0, sizeof(selected)); + + while ((c = isc_commandline_parse(argc, argv, "lt:v")) != -1) { switch (c) { + case 'l': + for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) + { + if (tests[i].name != NULL) { + fprintf(stdout, "%s\n", tests[i].name); + } + } + return (0); + case 't': + if (!cmocka_add_test_byname( + tests, isc_commandline_argument, selected)) + { + fprintf(stderr, "unknown test '%s'\n", + isc_commandline_argument); + exit(1); + } + break; case 'v': verbose = true; break; @@ -1561,7 +1582,11 @@ main(int argc, char **argv) { } } - return (cmocka_run_group_tests(tests, NULL, NULL)); + if (selected[0].name != NULL) { + return (cmocka_run_group_tests(selected, NULL, NULL)); + } else { + return (cmocka_run_group_tests(tests, NULL, NULL)); + } } #else /* HAVE_CMOCKA */ diff --git a/util/copyrights b/util/copyrights index e7594997b1..8f6d7c22a2 100644 --- a/util/copyrights +++ b/util/copyrights @@ -1791,6 +1791,7 @@ ./lib/isc/include/isc/bind9.h C 2009,2013,2016,2018,2019,2020 ./lib/isc/include/isc/buffer.h C 1998,1999,2000,2001,2002,2004,2005,2006,2007,2008,2010,2012,2014,2016,2017,2018,2019,2020 ./lib/isc/include/isc/bufferlist.h C 1999,2000,2001,2004,2005,2006,2007,2016,2018,2019,2020 +./lib/isc/include/isc/cmocka.h C 2020 ./lib/isc/include/isc/commandline.h C 1999,2000,2001,2004,2005,2006,2007,2015,2016,2018,2019,2020 ./lib/isc/include/isc/counter.h C 2014,2016,2018,2019,2020 ./lib/isc/include/isc/crc64.h C 2013,2016,2018,2019,2020