From af69bf54919ed38a98717375b70677d45eafa672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 4 Jun 2018 09:50:57 +0200 Subject: [PATCH] Add tkey_test.c with mocked isc_mem object --- lib/dns/tests/Kyuafile | 1 + lib/dns/tests/Makefile.in | 7 ++ lib/dns/tests/tkey_test.c | 147 ++++++++++++++++++++++++++++++++++++++ util/copyrights | 1 + 4 files changed, 156 insertions(+) create mode 100644 lib/dns/tests/tkey_test.c diff --git a/lib/dns/tests/Kyuafile b/lib/dns/tests/Kyuafile index fcdfeeb1e6..377933d980 100644 --- a/lib/dns/tests/Kyuafile +++ b/lib/dns/tests/Kyuafile @@ -26,6 +26,7 @@ atf_test_program{name='resolver_test'} atf_test_program{name='rsa_test'} atf_test_program{name='sigs_test'} atf_test_program{name='time_test'} +tap_test_program{name='tkey_test'} atf_test_program{name='tsig_test'} atf_test_program{name='update_test'} atf_test_program{name='zonemgr_test'} diff --git a/lib/dns/tests/Makefile.in b/lib/dns/tests/Makefile.in index 703b66ebf4..83ba5f07ea 100644 --- a/lib/dns/tests/Makefile.in +++ b/lib/dns/tests/Makefile.in @@ -62,6 +62,7 @@ SRCS = acl_test.c \ rsa_test.c \ sigs_test.c \ time_test.c \ + tkey_test.c \ tsig_test.c \ update_test.c \ zonemgr_test.c \ @@ -93,6 +94,7 @@ TARGETS = acl_test@EXEEXT@ \ rsa_test@EXEEXT@ \ sigs_test@EXEEXT@ \ time_test@EXEEXT@ \ + tkey_test@EXEEXT@ \ tsig_test@EXEEXT@ \ update_test@EXEEXT@ \ zonemgr_test@EXEEXT@ \ @@ -234,6 +236,11 @@ time_test@EXEEXT@: time_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} time_test.@O@ dnstest.@O@ ${DNSLIBS} \ ${ISCLIBS} ${LIBS} +tkey_test@EXEEXT@: tkey_test.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} + ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} ${LDFLAGS} -o $@ \ + tkey_test.@O@ ${DNSLIBS} \ + ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS} ${CMOCKA_MEM} + tsig_test@EXEEXT@: tsig_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ tsig_test.@O@ dnstest.@O@ ${DNSLIBS} \ diff --git a/lib/dns/tests/tkey_test.c b/lib/dns/tests/tkey_test.c new file mode 100644 index 0000000000..e2610f29a6 --- /dev/null +++ b/lib/dns/tests/tkey_test.c @@ -0,0 +1,147 @@ +/* + * 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 http://mozilla.org/MPL/2.0/. + * + * See the COPYRIGHT file distributed with this work for additional + * information regarding copyright ownership. + */ + +#include + +#if HAVE_CMOCKA + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#if LD_WRAP +static isc_mem_t mock_mctx = { + .impmagic = 0, + .magic = ISCAPI_MCTX_MAGIC, + .methods = NULL +}; + +static void * +__wrap_isc__mem_get(isc_mem_t *mctx __attribute__ ((unused)), + size_t size) +{ + bool has_enough_memory = mock_type(bool); + if (!has_enough_memory) { + return (NULL); + } + return (malloc(size)); +} + +static void +__wrap_isc__mem_put(isc_mem_t *ctx0 __attribute__ ((unused)), + void *ptr, + size_t size __attribute__ ((unused))) +{ + free(ptr); +} + +static void +__wrap_isc_mem_attach(isc_mem_t *source0, isc_mem_t **targetp) { + *targetp = source0; +} + +static void +__wrap_isc_mem_detach(isc_mem_t **ctxp) { + *ctxp = NULL; +} + +static int +_setup(void **state) { + dns_tkeyctx_t *tctx = NULL; + will_return(__wrap_isc__mem_get, true); + if (dns_tkeyctx_create(&mock_mctx, &tctx) != ISC_R_SUCCESS) { + return (-1); + } + *state = tctx; + return (0); +} + +static int +_teardown(void **state) { + dns_tkeyctx_t *tctx = *state; + if (tctx != NULL) { + dns_tkeyctx_destroy(&tctx); + } + return (0); +} + +static void +dns_tkeyctx_create_test(void **state) { + dns_tkeyctx_t *tctx; + + tctx = NULL; + will_return(__wrap_isc__mem_get, false); + assert_int_equal(dns_tkeyctx_create(&mock_mctx, &tctx), ISC_R_NOMEMORY); + + tctx = NULL; + will_return(__wrap_isc__mem_get, true); + assert_int_equal(dns_tkeyctx_create(&mock_mctx, &tctx), ISC_R_SUCCESS); + *state = tctx; +} + +static void +dns_tkeyctx_destroy_test(void **state) { + dns_tkeyctx_t *tctx = *state; + + assert_non_null(tctx); + dns_tkeyctx_destroy(&tctx); +} + +#else /* LD_WRAP */ + +#define _setup NULL +#define _teardown NULL + +static void +dns_tkeyctx_create_test(void **state __attribute__ ((unused))) { + skip(); +} + +static void +dns_tkeyctx_destroy_test(void **state __attribute__ ((unused))) { + skip(); +} + +#endif /* LD_WRAP */ + +int main(void) { + const struct CMUnitTest tkey_tests[] = { + cmocka_unit_test_teardown(dns_tkeyctx_create_test, _teardown), + /* cmocka_unit_test(dns_tkey_processquery_test), */ + /* cmocka_unit_test(dns_tkey_builddhquery_test), */ + /* cmocka_unit_test(dns_tkey_buildgssquery_test), */ + /* cmocka_unit_test(dns_tkey_builddeletequery_test), */ + /* cmocka_unit_test(dns_tkey_processdhresponse_test), */ + /* cmocka_unit_test(dns_tkey_processgssresponse_test), */ + /* cmocka_unit_test(dns_tkey_processdeleteresponse_test), */ + /* cmocka_unit_test(dns_tkey_gssnegotiate_test), */ + cmocka_unit_test_setup(dns_tkeyctx_destroy_test, _setup), + }; + return (cmocka_run_group_tests(tkey_tests, NULL, NULL)); +} + +#else + +#include + +int main(void) { + printf("1..0 # Skipped: cmocka not available\n"); + return (0); +} + +#endif diff --git a/util/copyrights b/util/copyrights index e1bf381c9f..25f5b52dc6 100644 --- a/util/copyrights +++ b/util/copyrights @@ -3257,6 +3257,7 @@ ./lib/dns/tests/testkeys/Kexample.+008+37464.key X 2018 ./lib/dns/tests/testkeys/Kexample.+008+37464.private X 2018 ./lib/dns/tests/time_test.c C 2011,2012,2016,2018 +./lib/dns/tests/tkey_test.c C 2018 ./lib/dns/tests/tsig_test.c C 2017,2018 ./lib/dns/tests/update_test.c C 2011,2012,2014,2016,2017,2018 ./lib/dns/tests/zonemgr_test.c C 2011,2012,2013,2015,2016,2018