From 005e19ad3fe099ea33b4597c54a855d9c181d610 Mon Sep 17 00:00:00 2001 From: Joey Date: Wed, 7 Nov 2018 12:23:15 +0700 Subject: [PATCH] convert db_test --- lib/dns/tests/Kyuafile | 2 +- lib/dns/tests/Makefile.in | 6 +- lib/dns/tests/db_test.c | 240 +++++++++++++++++++++----------------- 3 files changed, 135 insertions(+), 113 deletions(-) diff --git a/lib/dns/tests/Kyuafile b/lib/dns/tests/Kyuafile index 939f51fa6e..d5f65d4c01 100644 --- a/lib/dns/tests/Kyuafile +++ b/lib/dns/tests/Kyuafile @@ -2,7 +2,7 @@ syntax(2) test_suite('bind9') tap_test_program{name='acl_test'} -atf_test_program{name='db_test'} +tap_test_program{name='db_test'} tap_test_program{name='dbdiff_test'} tap_test_program{name='dbiterator_test'} atf_test_program{name='dbversion_test'} diff --git a/lib/dns/tests/Makefile.in b/lib/dns/tests/Makefile.in index 4021f3c0ef..946bb64691 100644 --- a/lib/dns/tests/Makefile.in +++ b/lib/dns/tests/Makefile.in @@ -103,9 +103,9 @@ acl_test@EXEEXT@: acl_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS} db_test@EXEEXT@: db_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} - ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ - db_test.@O@ dnstest.@O@ ${DNSLIBS} \ - ${ISCLIBS} ${LIBS} + ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \ + ${LDFLAGS} -o $@ db_test.@O@ dnstest.@O@ ${DNSLIBS} \ + ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS} dbdiff_test@EXEEXT@: dbdiff_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \ diff --git a/lib/dns/tests/db_test.c b/lib/dns/tests/db_test.c index e93210f464..0578387096 100644 --- a/lib/dns/tests/db_test.c +++ b/lib/dns/tests/db_test.c @@ -9,15 +9,20 @@ * information regarding copyright ownership. */ -/*! \file */ - #include -#include +#if HAVE_CMOCKA + +#include +#include +#include #include #include +#define UNIT_TESTING +#include + #include #include #include @@ -26,9 +31,26 @@ #include "dnstest.h" -/* - * Helper functions - */ +static int +_setup(void **state) { + isc_result_t result; + + UNUSED(state); + + result = dns_test_begin(NULL, false); + assert_int_equal(result, ISC_R_SUCCESS); + + return (0); +} + +static int +_teardown(void **state) { + UNUSED(state); + + dns_test_end(); + + return (0); +} #define BUFLEN 255 #define BIGBUFLEN (64 * 1024) @@ -38,78 +60,73 @@ * Individual unit tests */ -ATF_TC(getoriginnode); -ATF_TC_HEAD(getoriginnode, tc) { - atf_tc_set_md_var(tc, "descr", - "test multiple calls to dns_db_getoriginnode"); -} -ATF_TC_BODY(getoriginnode, tc) { +/* test multiple calls to dns_db_getoriginnode */ +static void +getoriginnode_test(void **state) { dns_db_t *db = NULL; dns_dbnode_t *node = NULL; isc_mem_t *mymctx = NULL; isc_result_t result; + UNUSED(state); + result = isc_mem_create(0, 0, &mymctx); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_create(mymctx, "rbt", dns_rootname, dns_dbtype_zone, dns_rdataclass_in, 0, NULL, &db); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_getoriginnode(db, &node); - ATF_CHECK_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); dns_db_detachnode(db, &node); result = dns_db_getoriginnode(db, &node); - ATF_CHECK_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); dns_db_detachnode(db, &node); dns_db_detach(&db); isc_mem_detach(&mymctx); } -ATF_TC(getsetservestalettl); -ATF_TC_HEAD(getsetservestalettl, tc) { - atf_tc_set_md_var(tc, "descr", - "test getservestalettl and setservestalettl"); -} -ATF_TC_BODY(getsetservestalettl, tc) { +/* test getservestalettl and setservestalettl */ +static void +getsetservestalettl_test(void **state) { dns_db_t *db = NULL; isc_mem_t *mymctx = NULL; isc_result_t result; dns_ttl_t ttl; + UNUSED(state); + result = isc_mem_create(0, 0, &mymctx); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_create(mymctx, "rbt", dns_rootname, dns_dbtype_cache, dns_rdataclass_in, 0, NULL, &db); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); ttl = 5000; result = dns_db_getservestalettl(db, &ttl); - ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_db_getservestalettl"); - ATF_CHECK_EQ_MSG(ttl, 0, "dns_db_getservestalettl initial value"); + assert_int_equal(result, ISC_R_SUCCESS); + assert_int_equal(ttl, 0); ttl = 6 * 3600; result = dns_db_setservestalettl(db, ttl); - ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_db_setservestalettl"); + assert_int_equal(result, ISC_R_SUCCESS); ttl = 5000; result = dns_db_getservestalettl(db, &ttl); - ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_db_getservestalettl"); - ATF_CHECK_EQ_MSG(ttl, 6 * 3600, "dns_db_getservestalettl update value"); + assert_int_equal(result, ISC_R_SUCCESS); + assert_int_equal(ttl, 6 * 3600); dns_db_detach(&db); isc_mem_detach(&mymctx); } -ATF_TC(dns_dbfind_staleok); -ATF_TC_HEAD(dns_dbfind_staleok, tc) { - atf_tc_set_md_var(tc, "descr", - "check DNS_DBFIND_STALEOK works"); -} -ATF_TC_BODY(dns_dbfind_staleok, tc) { +/* check DNS_DBFIND_STALEOK works */ +static void +dns_dbfind_staleok_test(void **state) { dns_db_t *db = NULL; dns_dbnode_t *node = NULL; dns_fixedname_t example_fixed; @@ -124,18 +141,20 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { isc_result_t result; unsigned char data[] = { 0x0a, 0x00, 0x00, 0x01 }; + UNUSED(state); + result = isc_mem_create(0, 0, &mymctx); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_create(mymctx, "rbt", dns_rootname, dns_dbtype_cache, dns_rdataclass_in, 0, NULL, &db); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); example = dns_fixedname_initname(&example_fixed); found = dns_fixedname_initname(&found_fixed); result = dns_name_fromstring(example, "example", 0, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); /* * Pass 0: default; no stale processing permitted. @@ -164,32 +183,32 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { case 1: /* turn on stale processing */ result = dns_db_setservestalettl(db, 1); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); break; case 2: /* turn off stale processing */ result = dns_db_setservestalettl(db, 0); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); break; } dns_rdataset_init(&rdataset); result = dns_rdatalist_tordataset(&rdatalist, &rdataset); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_findnode(db, example, true, &node); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_addrdataset(db, node, NULL, 0, &rdataset, 0, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); dns_db_detachnode(db, &node); dns_rdataset_disassociate(&rdataset); result = dns_db_find(db, example, NULL, dns_rdatatype_a, 0, 0, &node, found, &rdataset, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); /* * May loop for up to 2 seconds performing non stale lookups. @@ -197,10 +216,10 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { count = 0; do { count++; - ATF_REQUIRE(count < 21); /* loop sanity */ - ATF_CHECK_EQ(rdataset.attributes & + assert_in_range(count, 0, 20); /* loop sanity */ + assert_int_equal(rdataset.attributes & DNS_RDATASETATTR_STALE, 0); - ATF_CHECK(rdataset.ttl > 0); + assert_true(rdataset.ttl > 0); dns_db_detachnode(db, &node); dns_rdataset_disassociate(&rdataset); @@ -211,7 +230,7 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { &node, found, &rdataset, NULL); } while (result == ISC_R_SUCCESS); - ATF_CHECK_EQ(result, ISC_R_NOTFOUND); + assert_int_equal(result, ISC_R_NOTFOUND); /* * Check whether we can get stale data. @@ -221,7 +240,7 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { &node, found, &rdataset, NULL); switch (pass) { case 0: - ATF_CHECK_EQ(result, ISC_R_NOTFOUND); + assert_int_equal(result, ISC_R_NOTFOUND); break; case 1: /* @@ -231,10 +250,10 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { count = 0; do { count++; - ATF_REQUIRE(count < 50); /* loop sanity */ - ATF_CHECK_EQ(result, ISC_R_SUCCESS); - ATF_CHECK_EQ(rdataset.ttl, 0); - ATF_CHECK_EQ(rdataset.attributes & + assert_in_range(count, 0, 49); /* loop sanity */ + assert_int_equal(result, ISC_R_SUCCESS); + assert_int_equal(rdataset.ttl, 0); + assert_int_equal(rdataset.attributes & DNS_RDATASETATTR_STALE, DNS_RDATASETATTR_STALE); dns_db_detachnode(db, &node); @@ -248,12 +267,11 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { 0, &node, found, &rdataset, NULL); } while (result == ISC_R_SUCCESS); - ATF_CHECK(count > 1); - ATF_CHECK(count < 11); - ATF_CHECK_EQ(result, ISC_R_NOTFOUND); + assert_in_range(count, 1, 10); + assert_int_equal(result, ISC_R_NOTFOUND); break; case 2: - ATF_CHECK_EQ(result, ISC_R_NOTFOUND); + assert_int_equal(result, ISC_R_NOTFOUND); break; } } @@ -262,71 +280,62 @@ ATF_TC_BODY(dns_dbfind_staleok, tc) { isc_mem_detach(&mymctx); } -ATF_TC(class); -ATF_TC_HEAD(class, tc) { - atf_tc_set_md_var(tc, "descr", "database class"); -} -ATF_TC_BODY(class, tc) { +/* database class */ +static void +class_test(void **state) { isc_result_t result; dns_db_t *db = NULL; - result = dns_test_begin(NULL, false); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + UNUSED(state); result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone, dns_rdataclass_in, 0, NULL, &db); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_load(db, "testdata/db/data.db", dns_masterformat_text, 0); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); - ATF_CHECK_EQ(dns_db_class(db), dns_rdataclass_in); + assert_int_equal(dns_db_class(db), dns_rdataclass_in); dns_db_detach(&db); } -ATF_TC(dbtype); -ATF_TC_HEAD(dbtype, tc) { - atf_tc_set_md_var(tc, "descr", "database type"); -} -ATF_TC_BODY(dbtype, tc) { +/* database type */ +static void +dbtype_test(void **state) { isc_result_t result; dns_db_t *db = NULL; - result = dns_test_begin(NULL, false); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + UNUSED(state); /* DB has zone semantics */ result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone, dns_rdataclass_in, 0, NULL, &db); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_load(db, "testdata/db/data.db", dns_masterformat_text, 0); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); - ATF_CHECK(dns_db_iszone(db)); - ATF_CHECK(!dns_db_iscache(db)); + assert_int_equal(result, ISC_R_SUCCESS); + assert_true(dns_db_iszone(db)); + assert_false(dns_db_iscache(db)); dns_db_detach(&db); /* DB has cache semantics */ result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_cache, dns_rdataclass_in, 0, NULL, &db); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_load(db, "testdata/db/data.db", dns_masterformat_text, 0); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); - ATF_CHECK(dns_db_iscache(db)); - ATF_CHECK(!dns_db_iszone(db)); + assert_int_equal(result, ISC_R_SUCCESS); + assert_true(dns_db_iscache(db)); + assert_false(dns_db_iszone(db)); dns_db_detach(&db); - dns_test_end(); } -ATF_TC(version); -ATF_TC_HEAD(version, tc) { - atf_tc_set_md_var(tc, "descr", "database versions"); -} -ATF_TC_BODY(version, tc) { +/* database versions */ +static void +version_test(void **state) { isc_result_t result; dns_fixedname_t fname, ffound; dns_name_t *name, *foundname; @@ -335,12 +344,11 @@ ATF_TC_BODY(version, tc) { dns_dbnode_t *node = NULL; dns_rdataset_t rdataset; - result = dns_test_begin(NULL, false); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + UNUSED(state); result = dns_test_loaddb(&db, dns_dbtype_zone, "test.test", "testdata/db/data.db"); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); /* Open current version for reading */ dns_db_currentversion(db, &ver); @@ -350,7 +358,7 @@ ATF_TC_BODY(version, tc) { dns_rdataset_init(&rdataset); result = dns_db_find(db, name , ver, dns_rdatatype_a, 0, 0, &node, foundname, &rdataset, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); dns_rdataset_disassociate(&rdataset); dns_db_detachnode(db, &node); dns_db_closeversion(db, &ver, false); @@ -363,14 +371,14 @@ ATF_TC_BODY(version, tc) { dns_rdataset_init(&rdataset); result = dns_db_find(db, name , ver, dns_rdatatype_a, 0, 0, &node, foundname, &rdataset, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_db_newversion(db, &new); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); /* Delete the rdataset from the new verison */ result = dns_db_deleterdataset(db, node, new, dns_rdatatype_a, 0); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); dns_rdataset_disassociate(&rdataset); dns_db_detachnode(db, &node); @@ -378,32 +386,46 @@ ATF_TC_BODY(version, tc) { /* This should fail now */ result = dns_db_find(db, name, new, dns_rdatatype_a, 0, 0, &node, foundname, &rdataset, NULL); - ATF_REQUIRE_EQ(result, DNS_R_NXDOMAIN); + assert_int_equal(result, DNS_R_NXDOMAIN); dns_db_closeversion(db, &new, true); /* But this should still succeed */ result = dns_db_find(db, name, ver, dns_rdatatype_a, 0, 0, &node, foundname, &rdataset, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); dns_rdataset_disassociate(&rdataset); dns_db_detachnode(db, &node); dns_db_closeversion(db, &ver, false); dns_db_detach(&db); - dns_test_end(); } -/* - * Main - */ -ATF_TP_ADD_TCS(tp) { - ATF_TP_ADD_TC(tp, getoriginnode); - ATF_TP_ADD_TC(tp, getsetservestalettl); - ATF_TP_ADD_TC(tp, dns_dbfind_staleok); - ATF_TP_ADD_TC(tp, class); - ATF_TP_ADD_TC(tp, dbtype); - ATF_TP_ADD_TC(tp, version); +int +main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(getoriginnode_test), + cmocka_unit_test(getsetservestalettl_test), + cmocka_unit_test(dns_dbfind_staleok_test), + cmocka_unit_test_setup_teardown(class_test, + _setup, _teardown), + cmocka_unit_test_setup_teardown(dbtype_test, + _setup, _teardown), + cmocka_unit_test_setup_teardown(version_test, + _setup, _teardown), + }; - return (atf_no_error()); + return (cmocka_run_group_tests(tests, NULL, NULL)); } + +#else /* HAVE_CMOCKA */ + +#include + +int +main(void) { + printf("1..0 # Skipped: cmocka not available\n"); + return (0); +} + +#endif