From b85a5b0dcdcaced0a33c3d79dfecb20a42d29836 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Wed, 24 Oct 2018 00:02:43 -0700 Subject: [PATCH] convert aes_test (cherry picked from commit dc88db015b4d8c491f3da05d028dbabeebc91d00) (cherry picked from commit c679b85424190417cd59835cb18dfdcffee98c37) --- lib/isc/tests/Kyuafile | 2 +- lib/isc/tests/Makefile.in | 5 +- lib/isc/tests/aes_test.c | 126 +++++++++++++++++++------------------- 3 files changed, 68 insertions(+), 65 deletions(-) diff --git a/lib/isc/tests/Kyuafile b/lib/isc/tests/Kyuafile index 7e96b85c03..ce64d434fa 100644 --- a/lib/isc/tests/Kyuafile +++ b/lib/isc/tests/Kyuafile @@ -1,7 +1,7 @@ syntax(2) test_suite('bind9') -atf_test_program{name='aes_test'} +tap_test_program{name='aes_test'} atf_test_program{name='atomic_test'} atf_test_program{name='buffer_test'} atf_test_program{name='counter_test'} diff --git a/lib/isc/tests/Makefile.in b/lib/isc/tests/Makefile.in index 65ed5ee5c2..446caf408e 100644 --- a/lib/isc/tests/Makefile.in +++ b/lib/isc/tests/Makefile.in @@ -55,8 +55,9 @@ atomic_test@EXEEXT@: atomic_test.@O@ isctest.@O@ ${ISCDEPLIBS} atomic_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS} aes_test@EXEEXT@: aes_test.@O@ ${ISCDEPLIBS} - ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ - aes_test.@O@ ${ISCLIBS} ${LIBS} + ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \ + ${LDFLAGS} -o $@ aes_test.@O@ \ + ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS} buffer_test@EXEEXT@: buffer_test.@O@ isctest.@O@ ${ISCDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ diff --git a/lib/isc/tests/aes_test.c b/lib/isc/tests/aes_test.c index 9c15b5295d..76530e7325 100644 --- a/lib/isc/tests/aes_test.c +++ b/lib/isc/tests/aes_test.c @@ -9,20 +9,25 @@ * information regarding copyright ownership. */ - -/* ! \file */ - #include -#include +#if HAVE_CMOCKA + +#include +#include +#include #include #include +#define UNIT_TESTING +#include + #include #include #include #include +#include #include #include #include @@ -57,15 +62,15 @@ tohexstr(unsigned char *d, char *out) { } size_t -fromhexstr(const char *in, unsigned char *d) -{ +fromhexstr(const char *in, unsigned char *d) { isc_buffer_t b; isc_result_t ret; isc_buffer_init(&b, d, ISC_AES256_KEYLENGTH + 1); ret = isc_hex_decodestring(in, &b); - if (ret != ISC_R_SUCCESS) - return 0; + if (ret != ISC_R_SUCCESS) { + return (0); + } return isc_buffer_usedlength(&b); } @@ -75,14 +80,9 @@ typedef struct aes_testcase { const char *result; } aes_testcase_t; - -ATF_TC(isc_aes128); -ATF_TC_HEAD(isc_aes128, tc) { - atf_tc_set_md_var(tc, "descr", "AES 128 test vectors"); -} -ATF_TC_BODY(isc_aes128, tc) { - UNUSED(tc); - +/* AES 128 test vectors */ +static void +isc_aes128_test(void **state) { aes_testcase_t testcases[] = { /* Test 1 (KAT ECBVarTxt128 #3) */ { @@ -125,26 +125,24 @@ ATF_TC_BODY(isc_aes128, tc) { aes_testcase_t *testcase = testcases; + UNUSED(state); + while (testcase->key != NULL) { len = fromhexstr(testcase->key, key); - ATF_CHECK_EQ(len, ISC_AES128_KEYLENGTH); + assert_int_equal(len, ISC_AES128_KEYLENGTH); len = fromhexstr(testcase->input, plaintext); - ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH); + assert_int_equal(len, ISC_AES_BLOCK_LENGTH); isc_aes128_crypt(key, plaintext, ciphertext); - ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS); - ATF_CHECK_STREQ(str, testcase->result); + assert_int_equal(tohexstr(ciphertext, str), ISC_R_SUCCESS); + assert_string_equal(str, testcase->result); testcase++; } } -ATF_TC(isc_aes192); -ATF_TC_HEAD(isc_aes192, tc) { - atf_tc_set_md_var(tc, "descr", "AES 192 test vectors"); -} -ATF_TC_BODY(isc_aes192, tc) { - UNUSED(tc); - +/* AES 192 test vectors */ +static void +isc_aes192_test(void **state) { aes_testcase_t testcases[] = { /* Test 1 (KAT ECBVarTxt192 #3) */ { @@ -187,26 +185,24 @@ ATF_TC_BODY(isc_aes192, tc) { aes_testcase_t *testcase = testcases; + UNUSED(state); + while (testcase->key != NULL) { len = fromhexstr(testcase->key, key); - ATF_CHECK_EQ(len, ISC_AES192_KEYLENGTH); + assert_int_equal(len, ISC_AES192_KEYLENGTH); len = fromhexstr(testcase->input, plaintext); - ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH); + assert_int_equal(len, ISC_AES_BLOCK_LENGTH); isc_aes192_crypt(key, plaintext, ciphertext); - ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS); - ATF_CHECK_STREQ(str, testcase->result); + assert_int_equal(tohexstr(ciphertext, str), ISC_R_SUCCESS); + assert_string_equal(str, testcase->result); testcase++; } } -ATF_TC(isc_aes256); -ATF_TC_HEAD(isc_aes256, tc) { - atf_tc_set_md_var(tc, "descr", "AES 256 test vectors"); -} -ATF_TC_BODY(isc_aes256, tc) { - UNUSED(tc); - +/* AES 256 test vectors */ +static void +isc_aes256_test(void **state) { aes_testcase_t testcases[] = { /* Test 1 (KAT ECBVarTxt256 #3) */ { @@ -255,40 +251,46 @@ ATF_TC_BODY(isc_aes256, tc) { aes_testcase_t *testcase = testcases; + UNUSED(state); + while (testcase->key != NULL) { len = fromhexstr(testcase->key, key); - ATF_CHECK_EQ(len, ISC_AES256_KEYLENGTH); + assert_int_equal(len, ISC_AES256_KEYLENGTH); len = fromhexstr(testcase->input, plaintext); - ATF_CHECK_EQ(len, ISC_AES_BLOCK_LENGTH); + assert_int_equal(len, ISC_AES_BLOCK_LENGTH); isc_aes256_crypt(key, plaintext, ciphertext); - ATF_CHECK(tohexstr(ciphertext, str) == ISC_R_SUCCESS); - ATF_CHECK_STREQ(str, testcase->result); + assert_int_equal(tohexstr(ciphertext, str), ISC_R_SUCCESS); + assert_string_equal(str, testcase->result); testcase++; } } -#else -ATF_TC(untested); -ATF_TC_HEAD(untested, tc) { - atf_tc_set_md_var(tc, "descr", "skipping aes test"); -} -ATF_TC_BODY(untested, tc) { - UNUSED(tc); - atf_tc_skip("AES not available"); -} -#endif +#endif /* ISC_PLATFORM_WANTAES */ -/* - * Main - */ -ATF_TP_ADD_TCS(tp) { +int +main(void) { #ifdef ISC_PLATFORM_WANTAES - ATF_TP_ADD_TC(tp, isc_aes128); - ATF_TP_ADD_TC(tp, isc_aes192); - ATF_TP_ADD_TC(tp, isc_aes256); + const struct CMUnitTest tests[] = { + cmocka_unit_test(isc_aes128_test), + cmocka_unit_test(isc_aes192_test), + cmocka_unit_test(isc_aes256_test), + }; + + return (cmocka_run_group_tests(tests, NULL, NULL)); #else - ATF_TP_ADD_TC(tp, untested); -#endif - return (atf_no_error()); + print_message("1..0 # Skipped: AES disabled"); +#endif /* ISC_PLATFORM_WANTAES */ + } +#else /* HAVE_CMOCKA */ + +#include + +int +main(void) { + printf("1..0 # Skipped: cmocka not available\n"); + return (0); +} + +#endif