From f5352eae1e2741694eb0f40ff58cb04d1da24dbf Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Wed, 24 Oct 2018 09:01:48 -0700 Subject: [PATCH] convert dh_test (cherry picked from commit dcf65c82ada957748ea7ecca75df956a218ae18c) (cherry picked from commit e779fa30147f394c1366072f578f82217bf8e11b) --- lib/dns/tests/Kyuafile | 2 +- lib/dns/tests/Makefile.in | 6 +-- lib/dns/tests/dh_test.c | 109 ++++++++++++++++++++++++-------------- 3 files changed, 72 insertions(+), 45 deletions(-) diff --git a/lib/dns/tests/Kyuafile b/lib/dns/tests/Kyuafile index 831692bdd3..7ff3b8c786 100644 --- a/lib/dns/tests/Kyuafile +++ b/lib/dns/tests/Kyuafile @@ -6,7 +6,7 @@ atf_test_program{name='db_test'} atf_test_program{name='dbdiff_test'} atf_test_program{name='dbiterator_test'} atf_test_program{name='dbversion_test'} -atf_test_program{name='dh_test'} +tap_test_program{name='dh_test'} atf_test_program{name='dispatch_test'} atf_test_program{name='dnstap_test'} atf_test_program{name='dst_test'} diff --git a/lib/dns/tests/Makefile.in b/lib/dns/tests/Makefile.in index f52f3d3cd4..1dc99bab32 100644 --- a/lib/dns/tests/Makefile.in +++ b/lib/dns/tests/Makefile.in @@ -128,9 +128,9 @@ dbversion_test@EXEEXT@: dbversion_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIB ${ISCLIBS} ${LIBS} dh_test@EXEEXT@: dh_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} - ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ - dh_test.@O@ dnstest.@O@ ${DNSLIBS} \ - ${ISCLIBS} ${LIBS} + ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \ + ${LDFLAGS} -o $@ dh_test.@O@ dnstest.@O@ \ + ${DNSLIBS} ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS} dispatch_test@EXEEXT@: dispatch_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ diff --git a/lib/dns/tests/dh_test.c b/lib/dns/tests/dh_test.c index 6216b4e1be..4dbfd82fc4 100644 --- a/lib/dns/tests/dh_test.c +++ b/lib/dns/tests/dh_test.c @@ -9,17 +9,24 @@ * information regarding copyright ownership. */ - -/* ! \file */ - #include -#include +#if HAVE_CMOCKA +#include +#include +#include + +#include +#include #include -#include +#define UNIT_TESTING +#include + +#include #include +#include #include @@ -31,62 +38,82 @@ #include "dnstest.h" #if defined(OPENSSL) && !defined(PK11_DH_DISABLE) +static int +_setup(void **state) { + isc_result_t result; -ATF_TC(isc_dh_computesecret); -ATF_TC_HEAD(isc_dh_computesecret, tc) { - atf_tc_set_md_var(tc, "descr", "OpenSSL DH_compute_key() failure"); + UNUSED(state); + + result = dns_test_begin(NULL, false); + assert_int_equal(result, ISC_R_SUCCESS); + + return (0); } -ATF_TC_BODY(isc_dh_computesecret, tc) { + +static int +_teardown(void **state) { + UNUSED(state); + + dns_test_end(); + + return (0); +} + +/* OpenSSL DH_compute_key() failure */ +static void +dh_computesecret(void **state) { dst_key_t *key = NULL; isc_buffer_t buf; unsigned char array[1024]; - isc_result_t ret; + isc_result_t result; dns_fixedname_t fname; dns_name_t *name; - UNUSED(tc); - - ret = dns_test_begin(NULL, false); - ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS); + UNUSED(state); name = dns_fixedname_initname(&fname); isc_buffer_constinit(&buf, "dh.", 3); isc_buffer_add(&buf, 3); - ret = dns_name_fromtext(name, &buf, NULL, 0, NULL); - ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS); + result = dns_name_fromtext(name, &buf, NULL, 0, NULL); + assert_int_equal(result, ISC_R_SUCCESS); - ret = dst_key_fromfile(name, 18602, DST_ALG_DH, - DST_TYPE_PUBLIC | DST_TYPE_KEY, - "./", mctx, &key); - ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS); + result = dst_key_fromfile(name, 18602, DST_ALG_DH, + DST_TYPE_PUBLIC | DST_TYPE_KEY, + "./", mctx, &key); + assert_int_equal(result, ISC_R_SUCCESS); isc_buffer_init(&buf, array, sizeof(array)); - ret = dst_key_computesecret(key, key, &buf); - ATF_REQUIRE_EQ(ret, DST_R_NOTPRIVATEKEY); - ret = key->func->computesecret(key, key, &buf); - ATF_REQUIRE_EQ(ret, DST_R_COMPUTESECRETFAILURE); + result = dst_key_computesecret(key, key, &buf); + assert_int_equal(result, DST_R_NOTPRIVATEKEY); + result = key->func->computesecret(key, key, &buf); + assert_int_equal(result, DST_R_COMPUTESECRETFAILURE); dst_key_free(&key); - dns_test_end(); -} -#else -ATF_TC(untested); -ATF_TC_HEAD(untested, tc) { - atf_tc_set_md_var(tc, "descr", "skipping OpenSSL DH test"); -} -ATF_TC_BODY(untested, tc) { - UNUSED(tc); - atf_tc_skip("OpenSSL DH not compiled in"); } #endif -/* - * Main - */ -ATF_TP_ADD_TCS(tp) { + +int +main(void) { #if defined(OPENSSL) && !defined(PK11_DH_DISABLE) - ATF_TP_ADD_TC(tp, isc_dh_computesecret); + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(dh_computesecret, + _setup, _teardown), + }; + + return (cmocka_run_group_tests(tests, dns_test_init, dns_test_final)); #else - ATF_TP_ADD_TC(tp, untested); + print_message("1..0 # Skipped: dh test broken with PKCS11"); #endif - return (atf_no_error()); } + +#else /* HAVE_CMOCKA */ + +#include + +int +main(void) { + printf("1..0 # Skipped: cmocka not available\n"); + return (0); +} + +#endif