diff --git a/lib/isc/tests/Makefile.in b/lib/isc/tests/Makefile.in index 046b8dcff3..d743821a9e 100644 --- a/lib/isc/tests/Makefile.in +++ b/lib/isc/tests/Makefile.in @@ -89,9 +89,9 @@ hash_test@EXEEXT@: hash_test.@O@ ${ISCDEPLIBS} ${LDFLAGS} -o $@ hash_test.@O@ \ ${ISCLIBS} ${LIBS} -heap_test@EXEEXT@: heap_test.@O@ ${ISCDEPLIBS} +heap_test@EXEEXT@: heap_test.@O@ isctest.@O@ ${ISCDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} \ - ${LDFLAGS} -o $@ heap_test.@O@ \ + ${LDFLAGS} -o $@ heap_test.@O@ isctest.@O@ \ ${ISCLIBS} ${LIBS} hmac_test@EXEEXT@: hmac_test.@O@ ${ISCDEPLIBS} @@ -99,14 +99,14 @@ hmac_test@EXEEXT@: hmac_test.@O@ ${ISCDEPLIBS} ${LDFLAGS} -o $@ hmac_test.@O@ \ ${ISCLIBS} ${LIBS} -ht_test@EXEEXT@: ht_test.@O@ ${ISCDEPLIBS} +ht_test@EXEEXT@: ht_test.@O@ isctest.@O@ ${ISCDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} \ - ${LDFLAGS} -o $@ ht_test.@O@ \ + ${LDFLAGS} -o $@ ht_test.@O@ isctest.@O@ \ ${ISCLIBS} ${LIBS} -lex_test@EXEEXT@: lex_test.@O@ ${ISCDEPLIBS} +lex_test@EXEEXT@: lex_test.@O@ isctest.@O@ ${ISCDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} \ - ${LDFLAGS} -o $@ lex_test.@O@ \ + ${LDFLAGS} -o $@ lex_test.@O@ isctest.@O@ \ ${ISCLIBS} ${LIBS} md_test@EXEEXT@: md_test.@O@ ${ISCDEPLIBS} diff --git a/lib/isc/tests/buffer_test.c b/lib/isc/tests/buffer_test.c index 80d01e04e4..71b9aa2110 100644 --- a/lib/isc/tests/buffer_test.c +++ b/lib/isc/tests/buffer_test.c @@ -65,7 +65,7 @@ isc_buffer_reserve_test(void **state) { UNUSED(state); b = NULL; - result = isc_buffer_allocate(mctx, &b, 1024); + result = isc_buffer_allocate(test_mctx, &b, 1024); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(b->length, 1024); @@ -135,7 +135,7 @@ isc_buffer_dynamic_test(void **state) { UNUSED(state); b = NULL; - result = isc_buffer_allocate(mctx, &b, last_length); + result = isc_buffer_allocate(test_mctx, &b, last_length); assert_int_equal(result, ISC_R_SUCCESS); assert_non_null(b); assert_int_equal(b->length, last_length); @@ -194,7 +194,7 @@ isc_buffer_copyregion_test(void **state) { UNUSED(state); - result = isc_buffer_allocate(mctx, &b, sizeof(data)); + result = isc_buffer_allocate(test_mctx, &b, sizeof(data)); assert_int_equal(result, ISC_R_SUCCESS); /* @@ -234,7 +234,7 @@ isc_buffer_printf_test(void **state) { * Prepare a buffer with auto-reallocation enabled. */ b = NULL; - result = isc_buffer_allocate(mctx, &b, 0); + result = isc_buffer_allocate(test_mctx, &b, 0); assert_int_equal(result, ISC_R_SUCCESS); isc_buffer_setautorealloc(b, true); diff --git a/lib/isc/tests/counter_test.c b/lib/isc/tests/counter_test.c index 6770dd2453..55ef991685 100644 --- a/lib/isc/tests/counter_test.c +++ b/lib/isc/tests/counter_test.c @@ -58,7 +58,7 @@ isc_counter_test(void **state) { UNUSED(state); - result = isc_counter_create(mctx, 0, &counter); + result = isc_counter_create(test_mctx, 0, &counter); assert_int_equal(result, ISC_R_SUCCESS); for (i = 0; i < 10; i++) { diff --git a/lib/isc/tests/heap_test.c b/lib/isc/tests/heap_test.c index b0b8e64481..4a6d55c665 100644 --- a/lib/isc/tests/heap_test.c +++ b/lib/isc/tests/heap_test.c @@ -28,6 +28,29 @@ #include #include +#include "isctest.h" + +static int +_setup(void **state) { + isc_result_t result; + + UNUSED(state); + + result = isc_test_begin(NULL, true, 0); + assert_int_equal(result, ISC_R_SUCCESS); + + return (0); +} + +static int +_teardown(void **state) { + UNUSED(state); + + isc_test_end(); + + return (0); +} + struct e { unsigned int value; unsigned int index; @@ -51,16 +74,13 @@ idx(void *p, unsigned int i) { /* test isc_heap_delete() */ static void isc_heap_delete_test(void **state) { - isc_mem_t *mctx = NULL; isc_heap_t *heap = NULL; isc_result_t result; struct e e1 = { 100, 0 }; UNUSED(state); - isc_mem_create(&mctx); - - result = isc_heap_create(mctx, compare, idx, 0, &heap); + result = isc_heap_create(test_mctx, compare, idx, 0, &heap); assert_int_equal(result, ISC_R_SUCCESS); assert_non_null(heap); @@ -73,9 +93,6 @@ isc_heap_delete_test(void **state) { isc_heap_destroy(&heap); assert_int_equal(heap, NULL); - - isc_mem_detach(&mctx); - assert_int_equal(mctx, NULL); } int @@ -84,7 +101,7 @@ main(void) { cmocka_unit_test(isc_heap_delete_test), }; - return (cmocka_run_group_tests(tests, NULL, NULL)); + return (cmocka_run_group_tests(tests, _setup, _teardown)); } #else /* HAVE_CMOCKA */ diff --git a/lib/isc/tests/ht_test.c b/lib/isc/tests/ht_test.c index 40bfccbc75..c186614549 100644 --- a/lib/isc/tests/ht_test.c +++ b/lib/isc/tests/ht_test.c @@ -31,16 +31,37 @@ #include #include +#include "isctest.h" + +static int +_setup(void **state) { + isc_result_t result; + + UNUSED(state); + + result = isc_test_begin(NULL, true, 0); + assert_int_equal(result, ISC_R_SUCCESS); + + return (0); +} + +static int +_teardown(void **state) { + UNUSED(state); + + isc_test_end(); + + return (0); +} + + static void test_ht_full(int bits, uintptr_t count) { isc_ht_t *ht = NULL; isc_result_t result; - isc_mem_t *mctx = NULL; uintptr_t i; - isc_mem_create(&mctx); - - result = isc_ht_init(&ht, mctx, bits); + result = isc_ht_init(&ht, test_mctx, bits); assert_int_equal(result, ISC_R_SUCCESS); assert_non_null(ht); @@ -174,15 +195,12 @@ test_ht_full(int bits, uintptr_t count) { isc_ht_destroy(&ht); assert_null(ht); - - isc_mem_detach(&mctx); } static void test_ht_iterator() { isc_ht_t *ht = NULL; isc_result_t result; - isc_mem_t *mctx = NULL; isc_ht_iter_t * iter = NULL; uintptr_t i; uintptr_t count = 10000; @@ -190,9 +208,7 @@ test_ht_iterator() { unsigned char key[16]; size_t tksize; - isc_mem_create(&mctx); - - result = isc_ht_init(&ht, mctx, 16); + result = isc_ht_init(&ht, test_mctx, 16); assert_int_equal(result, ISC_R_SUCCESS); assert_non_null(ht); for (i = 1; i <= count; i++) { @@ -293,8 +309,6 @@ test_ht_iterator() { isc_ht_destroy(&ht); assert_null(ht); - - isc_mem_detach(&mctx); } /* 20 bit, 200K elements test */ @@ -335,7 +349,7 @@ main(void) { cmocka_unit_test(isc_ht_iterator_test), }; - return (cmocka_run_group_tests(tests, NULL, NULL)); + return (cmocka_run_group_tests(tests, _setup, _teardown)); } #else /* HAVE_CMOCKA */ diff --git a/lib/isc/tests/isctest.c b/lib/isc/tests/isctest.c index 08ced36b30..e3d59611e7 100644 --- a/lib/isc/tests/isctest.c +++ b/lib/isc/tests/isctest.c @@ -28,8 +28,8 @@ #include "isctest.h" -isc_mem_t *mctx = NULL; -isc_log_t *lctx = NULL; +isc_mem_t *test_mctx = NULL; +isc_log_t *test_lctx = NULL; isc_taskmgr_t *taskmgr = NULL; isc_timermgr_t *timermgr = NULL; isc_socketmgr_t *socketmgr = NULL; @@ -56,9 +56,6 @@ static isc_logcategory_t categories[] = { static void cleanup_managers(void) { - if (netmgr != NULL) { - isc_nm_detach(&netmgr); - } if (maintask != NULL) { isc_task_shutdown(maintask); isc_task_destroy(&maintask); @@ -72,6 +69,9 @@ cleanup_managers(void) { if (timermgr != NULL) { isc_timermgr_destroy(&timermgr); } + if (netmgr != NULL) { + isc_nm_detach(&netmgr); + } } static isc_result_t @@ -88,13 +88,13 @@ create_managers(unsigned int workers) { workers = atoi(p); } - CHECK(isc_taskmgr_create(mctx, workers, 0, NULL, &taskmgr)); + netmgr = isc_nm_start(test_mctx, workers); + CHECK(isc_taskmgr_create(test_mctx, workers, 0, netmgr, &taskmgr)); CHECK(isc_task_create(taskmgr, 0, &maintask)); isc_taskmgr_setexcltask(taskmgr, maintask); - CHECK(isc_timermgr_create(mctx, &timermgr)); - CHECK(isc_socketmgr_create(mctx, &socketmgr)); - netmgr = isc_nm_start(mctx, 3); + CHECK(isc_timermgr_create(test_mctx, &timermgr)); + CHECK(isc_socketmgr_create(test_mctx, &socketmgr)); return (ISC_R_SUCCESS); cleanup: @@ -113,18 +113,18 @@ isc_test_begin(FILE *logfile, bool start_managers, isc_mem_debugging |= ISC_MEM_DEBUGRECORD; - INSIST(mctx == NULL); - isc_mem_create(&mctx); + INSIST(test_mctx == NULL); + isc_mem_create(&test_mctx); if (logfile != NULL) { isc_logdestination_t destination; isc_logconfig_t *logconfig = NULL; - INSIST(lctx == NULL); - CHECK(isc_log_create(mctx, &lctx, &logconfig)); + INSIST(test_lctx == NULL); + CHECK(isc_log_create(test_mctx, &test_lctx, &logconfig)); - isc_log_registercategories(lctx, categories); - isc_log_setcontext(lctx); + isc_log_registercategories(test_lctx, categories); + isc_log_setcontext(test_lctx); destination.file.stream = logfile; destination.file.name = NULL; @@ -161,11 +161,11 @@ isc_test_end(void) { cleanup_managers(); - if (lctx != NULL) { - isc_log_destroy(&lctx); + if (test_lctx != NULL) { + isc_log_destroy(&test_lctx); } - if (mctx != NULL) { - isc_mem_destroy(&mctx); + if (test_mctx != NULL) { + isc_mem_destroy(&test_mctx); } test_running = false; diff --git a/lib/isc/tests/isctest.h b/lib/isc/tests/isctest.h index c89996721a..17b0342d23 100644 --- a/lib/isc/tests/isctest.h +++ b/lib/isc/tests/isctest.h @@ -33,8 +33,8 @@ goto cleanup; \ } while (0) -extern isc_mem_t *mctx; -extern isc_log_t *lctx; +extern isc_mem_t *test_mctx; +extern isc_log_t *test_lctx; extern isc_taskmgr_t *taskmgr; extern isc_timermgr_t *timermgr; extern isc_socketmgr_t *socketmgr; diff --git a/lib/isc/tests/lex_test.c b/lib/isc/tests/lex_test.c index 462375ae93..709574e0e3 100644 --- a/lib/isc/tests/lex_test.c +++ b/lib/isc/tests/lex_test.c @@ -28,10 +28,32 @@ #include #include +#include "isctest.h" + +static int +_setup(void **state) { + isc_result_t result; + + UNUSED(state); + + result = isc_test_begin(NULL, true, 0); + assert_int_equal(result, ISC_R_SUCCESS); + + return (0); +} + +static int +_teardown(void **state) { + UNUSED(state); + + isc_test_end(); + + return (0); +} + /* check handling of 0xff */ static void lex_0xff(void **state) { - isc_mem_t *mctx = NULL; isc_result_t result; isc_lex_t *lex = NULL; isc_buffer_t death_buf; @@ -41,9 +63,7 @@ lex_0xff(void **state) { UNUSED(state); - isc_mem_create(&mctx); - - result = isc_lex_create(mctx, 1024, &lex); + result = isc_lex_create(test_mctx, 1024, &lex); assert_int_equal(result, ISC_R_SUCCESS); isc_buffer_init(&death_buf, &death[0], sizeof(death)); @@ -56,14 +76,11 @@ lex_0xff(void **state) { assert_int_equal(result, ISC_R_SUCCESS); isc_lex_destroy(&lex); - - isc_mem_destroy(&mctx); } /* check setting of source line */ static void lex_setline(void **state) { - isc_mem_t *mctx = NULL; isc_result_t result; isc_lex_t *lex = NULL; unsigned char text[] = "text\nto\nbe\nprocessed\nby\nlexer"; @@ -74,9 +91,7 @@ lex_setline(void **state) { UNUSED(state); - isc_mem_create(&mctx); - - result = isc_lex_create(mctx, 1024, &lex); + result = isc_lex_create(test_mctx, 1024, &lex); assert_int_equal(result, ISC_R_SUCCESS); isc_buffer_init(&buf, &text[0], sizeof(text)); @@ -103,8 +118,6 @@ lex_setline(void **state) { assert_int_equal(line, 105U); isc_lex_destroy(&lex); - - isc_mem_destroy(&mctx); } int @@ -114,7 +127,7 @@ main(void) { cmocka_unit_test(lex_setline), }; - return (cmocka_run_group_tests(tests, NULL, NULL)); + return (cmocka_run_group_tests(tests, _setup, _teardown)); } #else /* HAVE_CMOCKA */ diff --git a/lib/isc/tests/mem_test.c b/lib/isc/tests/mem_test.c index 0f783422e6..c1c63be610 100644 --- a/lib/isc/tests/mem_test.c +++ b/lib/isc/tests/mem_test.c @@ -73,19 +73,16 @@ isc_mem_test(void **state) { void *items1[50]; void *items2[50]; void *tmp; - isc_mem_t *localmctx = NULL; isc_mempool_t *mp1 = NULL, *mp2 = NULL; unsigned int i, j; int rval; UNUSED(state); - isc_mem_create(&localmctx); - - result = isc_mempool_create(localmctx, 24, &mp1); + result = isc_mempool_create(test_mctx, 24, &mp1); assert_int_equal(result, ISC_R_SUCCESS); - result = isc_mempool_create(localmctx, 31, &mp2); + result = isc_mempool_create(test_mctx, 31, &mp2); assert_int_equal(result, ISC_R_SUCCESS); isc_mempool_setfreemax(mp1, MP1_FREEMAX); @@ -151,11 +148,7 @@ isc_mem_test(void **state) { isc_mempool_destroy(&mp1); isc_mempool_destroy(&mp2); - isc_mem_destroy(&localmctx); - - isc_mem_create(&localmctx); - - result = isc_mempool_create(localmctx, 2, &mp1); + result = isc_mempool_create(test_mctx, 2, &mp1); assert_int_equal(result, ISC_R_SUCCESS); tmp = isc_mempool_get(mp1); @@ -164,9 +157,6 @@ isc_mem_test(void **state) { isc_mempool_put(mp1, tmp); isc_mempool_destroy(&mp1); - - isc_mem_destroy(&localmctx); - } /* test TotalUse calculation */ @@ -200,16 +190,16 @@ isc_mem_total_test(void **state) { /* ISC_MEMFLAG_INTERNAL */ - before = isc_mem_total(mctx); + before = isc_mem_total(test_mctx); for (i = 0; i < 100000; i++) { void *ptr; - ptr = isc_mem_allocate(mctx, 2048); - isc_mem_free(mctx, ptr); + ptr = isc_mem_allocate(test_mctx, 2048); + isc_mem_free(test_mctx, ptr); } - after = isc_mem_total(mctx); + after = isc_mem_total(test_mctx); diff = after - before; /* 2048 +8 bytes extra for size_info */ @@ -397,10 +387,10 @@ mem_thread(void *arg) { for (int i = 0; i < ITERS; i++) { for (int j = 0; j < NUM_ITEMS; j++) { - items[j] = isc_mem_get(mctx, size); + items[j] = isc_mem_get(test_mctx, size); } for (int j = 0; j < NUM_ITEMS; j++) { - isc_mem_put(mctx, items[j], size); + isc_mem_put(test_mctx, items[j], size); } } @@ -470,7 +460,7 @@ isc_mempool_benchmark(void **state) { isc_mutex_init(&mplock); - result = isc_mempool_create(mctx, ITEM_SIZE, &mp); + result = isc_mempool_create(test_mctx, ITEM_SIZE, &mp); assert_int_equal(result, ISC_R_SUCCESS); isc_mempool_associatelock(mp, &mplock); diff --git a/lib/isc/tests/pool_test.c b/lib/isc/tests/pool_test.c index b902869b86..5f803632f8 100644 --- a/lib/isc/tests/pool_test.c +++ b/lib/isc/tests/pool_test.c @@ -79,7 +79,7 @@ create_pool(void **state) { UNUSED(state); - result = isc_pool_create(mctx, 8, poolfree, poolinit, taskmgr, &pool); + result = isc_pool_create(test_mctx, 8, poolfree, poolinit, taskmgr, &pool); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(isc_pool_count(pool), 8); @@ -95,7 +95,7 @@ expand_pool(void **state) { UNUSED(state); - result = isc_pool_create(mctx, 10, poolfree, poolinit, taskmgr, &pool1); + result = isc_pool_create(test_mctx, 10, poolfree, poolinit, taskmgr, &pool1); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(isc_pool_count(pool1), 10); @@ -141,7 +141,7 @@ get_objects(void **state) { UNUSED(state); - result = isc_pool_create(mctx, 2, poolfree, poolinit, taskmgr, &pool); + result = isc_pool_create(test_mctx, 2, poolfree, poolinit, taskmgr, &pool); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(isc_pool_count(pool), 2); diff --git a/lib/isc/tests/radix_test.c b/lib/isc/tests/radix_test.c index 5f49ac08fd..26fd006fff 100644 --- a/lib/isc/tests/radix_test.c +++ b/lib/isc/tests/radix_test.c @@ -63,7 +63,7 @@ isc_radix_search_test(void **state) { UNUSED(state); - result = isc_radix_create(mctx, &radix, 32); + result = isc_radix_create(test_mctx, &radix, 32); assert_int_equal(result, ISC_R_SUCCESS); in_addr.s_addr = inet_addr("3.3.3.0"); diff --git a/lib/isc/tests/random_test.c b/lib/isc/tests/random_test.c index 556acffa4b..1dbdf0068a 100644 --- a/lib/isc/tests/random_test.c +++ b/lib/isc/tests/random_test.c @@ -39,6 +39,8 @@ #include #include +#include "isctest.h" + #define REPS 25000 typedef double (pvalue_func_t)(isc_mem_t *mctx, @@ -74,6 +76,27 @@ typedef enum { ISC_NONCE_BYTES } isc_random_func; +static int +_setup(void **state) { + isc_result_t result; + + UNUSED(state); + + result = isc_test_begin(NULL, true, 0); + assert_int_equal(result, ISC_R_SUCCESS); + + return (0); +} + +static int +_teardown(void **state) { + UNUSED(state); + + isc_test_end(); + + return (0); +} + static double igamc(double a, double x) { double ans, ax, c, yc, r, t, y, z; @@ -272,7 +295,6 @@ matrix_binaryrank(uint32_t *bits, size_t rows, size_t cols) { static void random_test(pvalue_func_t *func, isc_random_func test_func) { - isc_mem_t *mctx = NULL; uint32_t m; uint32_t j; uint32_t histogram[11] = { 0 }; @@ -286,8 +308,6 @@ random_test(pvalue_func_t *func, isc_random_func test_func) { tables_init(); - isc_mem_create(&mctx); - m = 1000; passed = 0; @@ -334,7 +354,7 @@ random_test(pvalue_func_t *func, isc_random_func test_func) { break; } - p_value = (*func)(mctx, (uint16_t *)values, REPS * 2); + p_value = (*func)(test_mctx, (uint16_t *)values, REPS * 2); if (p_value >= 0.01) { passed++; } @@ -841,7 +861,7 @@ main(int argc, char **argv) { } } - return (cmocka_run_group_tests(tests, NULL, NULL)); + return (cmocka_run_group_tests(tests, _setup, _teardown)); } #else /* HAVE_CMOCKA */ diff --git a/lib/isc/tests/socket_test.c b/lib/isc/tests/socket_test.c index 20cf26b22d..77c2475f87 100644 --- a/lib/isc/tests/socket_test.c +++ b/lib/isc/tests/socket_test.c @@ -358,7 +358,7 @@ udp_dscp_v4_test(void **state) { completion_init(&completion); - socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE, + socketevent = isc_socket_socketevent(test_mctx, s1, ISC_SOCKEVENT_SENDDONE, event_done, &completion); assert_non_null(socketevent); @@ -452,7 +452,7 @@ udp_dscp_v6_test(void **state) { completion_init(&completion); - socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE, + socketevent = isc_socket_socketevent(test_mctx, s1, ISC_SOCKEVENT_SENDDONE, event_done, &completion); assert_non_null(socketevent); @@ -769,7 +769,7 @@ udp_trunc_test(void **state) { completion_init(&completion); - socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE, + socketevent = isc_socket_socketevent(test_mctx, s1, ISC_SOCKEVENT_SENDDONE, event_done, &completion); assert_non_null(socketevent); @@ -801,7 +801,7 @@ udp_trunc_test(void **state) { completion_init(&completion); - socketevent = isc_socket_socketevent(mctx, s1, ISC_SOCKEVENT_SENDDONE, + socketevent = isc_socket_socketevent(test_mctx, s1, ISC_SOCKEVENT_SENDDONE, event_done, &completion); assert_non_null(socketevent); diff --git a/lib/isc/tests/symtab_test.c b/lib/isc/tests/symtab_test.c index fea0cda699..28bb053831 100644 --- a/lib/isc/tests/symtab_test.c +++ b/lib/isc/tests/symtab_test.c @@ -55,8 +55,8 @@ undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) { UNUSED(arg); assert_int_equal(type, 1); - isc_mem_free(mctx, key); - isc_mem_free(mctx, value.as_pointer); + isc_mem_free(test_mctx, key); + isc_mem_free(test_mctx, value.as_pointer); } /* test symbol table growth */ @@ -70,7 +70,7 @@ symtab_grow(void **state) { UNUSED(state); - result = isc_symtab_create(mctx, 3, undefine, NULL, false, &st); + result = isc_symtab_create(test_mctx, 3, undefine, NULL, false, &st); assert_int_equal(result, ISC_R_SUCCESS); assert_non_null(st); @@ -84,9 +84,9 @@ symtab_grow(void **state) { char str[16], *key; snprintf(str, sizeof(str), "%04x", i); - key = isc_mem_strdup(mctx, str); + key = isc_mem_strdup(test_mctx, str); assert_non_null(key); - value.as_pointer = isc_mem_strdup(mctx, str); + value.as_pointer = isc_mem_strdup(test_mctx, str); assert_non_null(value.as_pointer); result = isc_symtab_define(st, key, 1, value, policy); assert_int_equal(result, ISC_R_SUCCESS); @@ -101,9 +101,9 @@ symtab_grow(void **state) { char str[16], *key; snprintf(str, sizeof(str), "%04x", i); - key = isc_mem_strdup(mctx, str); + key = isc_mem_strdup(test_mctx, str); assert_non_null(key); - value.as_pointer = isc_mem_strdup(mctx, str); + value.as_pointer = isc_mem_strdup(test_mctx, str); assert_non_null(value.as_pointer); result = isc_symtab_define(st, key, 1, value, policy); assert_int_equal(result, ISC_R_EXISTS); diff --git a/lib/isc/tests/task_test.c b/lib/isc/tests/task_test.c index 9b15897a94..d44aee5898 100644 --- a/lib/isc/tests/task_test.c +++ b/lib/isc/tests/task_test.c @@ -167,14 +167,14 @@ all_events(void **state) { assert_int_equal(result, ISC_R_SUCCESS); /* First event */ - event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task, ISC_TASKEVENT_TEST, set, &a, sizeof (isc_event_t)); assert_non_null(event); assert_int_equal(atomic_load(&a), 0); isc_task_send(task, &event); - event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task, ISC_TASKEVENT_TEST, set, &b, sizeof (isc_event_t)); assert_non_null(event); @@ -229,7 +229,7 @@ privileged_events(void **state) { assert_false(isc_task_privilege(task2)); /* First event: privileged */ - event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task1, ISC_TASKEVENT_TEST, set, &a, sizeof (isc_event_t)); assert_non_null(event); @@ -237,7 +237,7 @@ privileged_events(void **state) { isc_task_send(task1, &event); /* Second event: not privileged */ - event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task2, ISC_TASKEVENT_TEST, set, &b, sizeof (isc_event_t)); assert_non_null(event); @@ -245,7 +245,7 @@ privileged_events(void **state) { isc_task_send(task2, &event); /* Third event: privileged */ - event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task1, ISC_TASKEVENT_TEST, set, &c, sizeof (isc_event_t)); assert_non_null(event); @@ -253,7 +253,7 @@ privileged_events(void **state) { isc_task_send(task1, &event); /* Fourth event: privileged */ - event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task1, ISC_TASKEVENT_TEST, set, &d, sizeof (isc_event_t)); assert_non_null(event); @@ -261,7 +261,7 @@ privileged_events(void **state) { isc_task_send(task1, &event); /* Fifth event: not privileged */ - event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task2, ISC_TASKEVENT_TEST, set, &e, sizeof (isc_event_t)); assert_non_null(event); @@ -350,7 +350,7 @@ privilege_drop(void **state) { assert_false(isc_task_privilege(task2)); /* First event: privileged */ - event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task1, ISC_TASKEVENT_TEST, set_and_drop, &a, sizeof (isc_event_t)); assert_non_null(event); @@ -358,7 +358,7 @@ privilege_drop(void **state) { isc_task_send(task1, &event); /* Second event: not privileged */ - event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task2, ISC_TASKEVENT_TEST, set_and_drop, &b, sizeof (isc_event_t)); assert_non_null(event); @@ -366,7 +366,7 @@ privilege_drop(void **state) { isc_task_send(task2, &event); /* Third event: privileged */ - event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task1, ISC_TASKEVENT_TEST, set_and_drop, &c, sizeof (isc_event_t)); assert_non_null(event); @@ -374,7 +374,7 @@ privilege_drop(void **state) { isc_task_send(task1, &event); /* Fourth event: privileged */ - event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task1, ISC_TASKEVENT_TEST, set_and_drop, &d, sizeof (isc_event_t)); assert_non_null(event); @@ -382,7 +382,7 @@ privilege_drop(void **state) { isc_task_send(task1, &event); /* Fifth event: not privileged */ - event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST, + event = isc_event_allocate(test_mctx, task2, ISC_TASKEVENT_TEST, set_and_drop, &e, sizeof (isc_event_t)); assert_non_null(event); @@ -551,7 +551,7 @@ basic(void **state) { * structure (socket, timer, task, etc) but this is just a * test program. */ - event = isc_event_allocate(mctx, (void *)1, 1, basic_cb, + event = isc_event_allocate(test_mctx, (void *)1, 1, basic_cb, testarray[i], sizeof(*event)); assert_non_null(event); isc_task_send(task1, &event); @@ -653,12 +653,12 @@ task_exclusive(void **state) { isc_taskmgr_setexcltask(taskmgr, tasks[6]); } - v = isc_mem_get(mctx, sizeof *v); + v = isc_mem_get(test_mctx, sizeof *v); assert_non_null(v); *v = i; - event = isc_event_allocate(mctx, NULL, 1, exclusive_cb, + event = isc_event_allocate(test_mctx, NULL, 1, exclusive_cb, v, sizeof(*event)); assert_non_null(event); @@ -718,6 +718,7 @@ maxtask_cb(isc_task_t *task, isc_event_t *event) { static void manytasks(void **state) { + isc_mem_t *mctx = NULL; isc_result_t result; isc_event_t *event = NULL; uintptr_t ntasks = 10000; @@ -851,7 +852,7 @@ shutdown(void **state) { /* * This event causes the task to wait on cv. */ - event = isc_event_allocate(mctx, &senders[1], event_type, sd_event1, + event = isc_event_allocate(test_mctx, &senders[1], event_type, sd_event1, NULL, sizeof(*event)); assert_non_null(event); isc_task_send(task, &event); @@ -860,7 +861,7 @@ shutdown(void **state) { * Now we fill up the task's event queue with some events. */ for (i = 0; i < 256; ++i) { - event = isc_event_allocate(mctx, &senders[1], event_type, + event = isc_event_allocate(test_mctx, &senders[1], event_type, sd_event2, NULL, sizeof(*event)); assert_non_null(event); isc_task_send(task, &event); @@ -942,7 +943,7 @@ post_shutdown(void **state) { /* * This event causes the task to wait on cv. */ - event = isc_event_allocate(mctx, &senders[1], event_type, psd_event1, + event = isc_event_allocate(test_mctx, &senders[1], event_type, psd_event1, NULL, sizeof(*event)); assert_non_null(event); isc_task_send(task, &event); @@ -1083,7 +1084,7 @@ test_purge(int sender, int type, int tag, int exp_purged) { /* * Block the task on cv. */ - event = isc_event_allocate(mctx, (void *)1, 9999, + event = isc_event_allocate(test_mctx, (void *)1, 9999, pg_event1, NULL, sizeof(*event)); assert_non_null(event); @@ -1098,7 +1099,7 @@ test_purge(int sender, int type, int tag, int exp_purged) { for (type_cnt = 0; type_cnt < TYPECNT; ++type_cnt) { for (tag_cnt = 0; tag_cnt < TAGCNT; ++tag_cnt) { eventtab[event_cnt] = - isc_event_allocate(mctx, + isc_event_allocate(test_mctx, &senders[sender + sender_cnt], (isc_eventtype_t)(type + type_cnt), pg_event2, NULL, sizeof(*event)); @@ -1382,12 +1383,12 @@ try_purgeevent(bool purgeable) { /* * Block the task on cv. */ - event1 = isc_event_allocate(mctx, (void *)1, (isc_eventtype_t)1, + event1 = isc_event_allocate(test_mctx, (void *)1, (isc_eventtype_t)1, pge_event1, NULL, sizeof(*event1)); assert_non_null(event1); isc_task_send(task, &event1); - event2 = isc_event_allocate(mctx, (void *)1, (isc_eventtype_t)1, + event2 = isc_event_allocate(test_mctx, (void *)1, (isc_eventtype_t)1, pge_event2, NULL, sizeof(*event2)); assert_non_null(event2); diff --git a/lib/isc/tests/taskpool_test.c b/lib/isc/tests/taskpool_test.c index 0dfdc0b93e..9621690bb5 100644 --- a/lib/isc/tests/taskpool_test.c +++ b/lib/isc/tests/taskpool_test.c @@ -58,7 +58,7 @@ create_pool(void **state) { UNUSED(state); - result = isc_taskpool_create(taskmgr, mctx, 8, 2, &pool); + result = isc_taskpool_create(taskmgr, test_mctx, 8, 2, &pool); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(isc_taskpool_size(pool), 8); @@ -74,7 +74,7 @@ expand_pool(void **state) { UNUSED(state); - result = isc_taskpool_create(taskmgr, mctx, 10, 2, &pool1); + result = isc_taskpool_create(taskmgr, test_mctx, 10, 2, &pool1); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(isc_taskpool_size(pool1), 10); @@ -119,7 +119,7 @@ get_tasks(void **state) { UNUSED(state); - result = isc_taskpool_create(taskmgr, mctx, 2, 2, &pool); + result = isc_taskpool_create(taskmgr, test_mctx, 2, 2, &pool); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(isc_taskpool_size(pool), 2); @@ -150,7 +150,7 @@ set_privilege(void **state) { UNUSED(state); - result = isc_taskpool_create(taskmgr, mctx, 2, 2, &pool); + result = isc_taskpool_create(taskmgr, test_mctx, 2, 2, &pool); assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(isc_taskpool_size(pool), 2); diff --git a/lib/isc/tests/timer_test.c b/lib/isc/tests/timer_test.c index 829019986b..22325d4a38 100644 --- a/lib/isc/tests/timer_test.c +++ b/lib/isc/tests/timer_test.c @@ -36,6 +36,8 @@ #include "isctest.h" +#include "../timer.c" + /* Set to true (or use -v option) for verbose output */ static bool verbose = false; @@ -46,10 +48,12 @@ static isc_timer_t *timer = NULL; static isc_condition_t cv; static isc_mutex_t mx; static isc_time_t endtime; +static isc_mutex_t lasttime_mx = PTHREAD_MUTEX_INITIALIZER; static isc_time_t lasttime; static int seconds; static int nanoseconds; static atomic_int_fast32_t eventcnt; +static atomic_uint_fast32_t errcnt; static int nevents; static int @@ -62,6 +66,8 @@ _setup(void **state) { result = isc_test_begin(NULL, true, 2); assert_int_equal(result, ISC_R_SUCCESS); + atomic_init(&errcnt, ISC_R_SUCCESS); + return (0); } @@ -117,7 +123,9 @@ setup_test(isc_timertype_t timertype, isc_time_t *expires, result = isc_task_onshutdown(task, shutdown, NULL); assert_int_equal(result, ISC_R_SUCCESS); + isc_mutex_lock(&lasttime_mx); result = isc_time_now(&lasttime); + isc_mutex_unlock(&lasttime_mx); assert_int_equal(result, ISC_R_SUCCESS); result = isc_timer_create(timermgr, timertype, expires, interval, @@ -135,11 +143,41 @@ setup_test(isc_timertype_t timertype, isc_time_t *expires, UNLOCK(&mx); + assert_int_equal(atomic_load(&errcnt), ISC_R_SUCCESS); + isc_task_detach(&task); isc_mutex_destroy(&mx); (void) isc_condition_destroy(&cv); } +static void +set_global_error(isc_result_t result) { + (void)atomic_compare_exchange_strong(&errcnt, + &(uint_fast32_t){ ISC_R_SUCCESS }, + result); +} + +static void +subthread_assert_true(bool expected) { + if (!expected) { + set_global_error(ISC_R_UNEXPECTED); + } +} + +static void +subthread_assert_int_equal(int observed, int expected) { + if (observed != expected) { + set_global_error(ISC_R_UNEXPECTED); + } +} + +static void +subthread_assert_result_equal(isc_result_t result, isc_result_t expected) { + if (result != expected) { + set_global_error(result); + } +} + static void ticktock(isc_task_t *task, isc_event_t *event) { isc_result_t result; @@ -167,26 +205,33 @@ ticktock(isc_task_t *task, isc_event_t *event) { } result = isc_time_now(&now); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_interval_set(&interval, seconds, nanoseconds); + isc_mutex_lock(&lasttime_mx); result = isc_time_add(&lasttime, &interval, &base); - assert_int_equal(result, ISC_R_SUCCESS); + isc_mutex_unlock(&lasttime_mx); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS); result = isc_time_add(&base, &interval, &ulim); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); result = isc_time_subtract(&base, &interval, &llim); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); - assert_true(isc_time_compare(&llim, &now) <= 0); - assert_true(isc_time_compare(&ulim, &now) >= 0); - lasttime = now; + subthread_assert_true(isc_time_compare(&llim, &now) <= 0); + subthread_assert_true(isc_time_compare(&ulim, &now) >= 0); + + isc_interval_set(&interval, 0, 0); + isc_mutex_lock(&lasttime_mx); + result = isc_time_add(&now, &interval, &lasttime); + isc_mutex_unlock(&lasttime_mx); + subthread_assert_result_equal(result, ISC_R_SUCCESS); if (atomic_load(&eventcnt) == nevents) { result = isc_time_now(&endtime); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_timer_detach(&timer); isc_task_shutdown(task); } @@ -254,24 +299,30 @@ test_idle(isc_task_t *task, isc_event_t *event) { } result = isc_time_now(&now); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_interval_set(&interval, seconds, nanoseconds); + isc_mutex_lock(&lasttime_mx); result = isc_time_add(&lasttime, &interval, &base); - assert_int_equal(result, ISC_R_SUCCESS); + isc_mutex_unlock(&lasttime_mx); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS); result = isc_time_add(&base, &interval, &ulim); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); result = isc_time_subtract(&base, &interval, &llim); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); - assert_true(isc_time_compare(&llim, &now) <= 0); - assert_true(isc_time_compare(&ulim, &now) >= 0); - lasttime = now; + subthread_assert_true(isc_time_compare(&llim, &now) <= 0); + subthread_assert_true(isc_time_compare(&ulim, &now) >= 0); - assert_int_equal(event->ev_type, ISC_TIMEREVENT_IDLE); + isc_interval_set(&interval, 0, 0); + isc_mutex_lock(&lasttime_mx); + isc_time_add(&now, &interval, &lasttime); + isc_mutex_unlock(&lasttime_mx); + + subthread_assert_int_equal(event->ev_type, ISC_TIMEREVENT_IDLE); isc_timer_detach(&timer); isc_task_shutdown(task); @@ -322,41 +373,46 @@ test_reset(isc_task_t *task, isc_event_t *event) { */ result = isc_time_now(&now); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_interval_set(&interval, seconds, nanoseconds); + isc_mutex_lock(&lasttime_mx); result = isc_time_add(&lasttime, &interval, &base); - assert_int_equal(result, ISC_R_SUCCESS); + isc_mutex_unlock(&lasttime_mx); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_interval_set(&interval, FUDGE_SECONDS, FUDGE_NANOSECONDS); result = isc_time_add(&base, &interval, &ulim); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); result = isc_time_subtract(&base, &interval, &llim); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); - assert_true(isc_time_compare(&llim, &now) <= 0); - assert_true(isc_time_compare(&ulim, &now) >= 0); - lasttime = now; + subthread_assert_true(isc_time_compare(&llim, &now) <= 0); + subthread_assert_true(isc_time_compare(&ulim, &now) >= 0); + + isc_interval_set(&interval, 0, 0); isc_mutex_lock(&lasttime_mx); + isc_time_add(&now, &interval, &lasttime); + isc_mutex_unlock(&lasttime_mx); int _eventcnt = atomic_load(&eventcnt); if (_eventcnt < 3) { - assert_int_equal(event->ev_type, ISC_TIMEREVENT_TICK); + subthread_assert_int_equal(event->ev_type, ISC_TIMEREVENT_TICK); if (_eventcnt == 2) { isc_interval_set(&interval, seconds, nanoseconds); result = isc_time_nowplusinterval(&expires, &interval); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_interval_set(&interval, 0, 0); result = isc_timer_reset(timer, isc_timertype_once, &expires, &interval, false); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); } } else { - assert_int_equal(event->ev_type, ISC_TIMEREVENT_LIFE); + subthread_assert_int_equal(event->ev_type, ISC_TIMEREVENT_LIFE); isc_timer_detach(&timer); isc_task_shutdown(task); @@ -433,7 +489,7 @@ tick_event(isc_task_t *task, isc_event_t *event) { isc_interval_set(&interval, seconds, 0); result = isc_timer_reset(tickertimer, isc_timertype_ticker, &expires, &interval, true); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); isc_task_shutdown(task); } @@ -456,7 +512,7 @@ once_event(isc_task_t *task, isc_event_t *event) { startflag = 1; result = isc_condition_broadcast(&cv); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); UNLOCK(&mx); isc_event_free(&event); @@ -481,7 +537,7 @@ shutdown_purge(isc_task_t *task, isc_event_t *event) { shutdownflag = 1; result = isc_condition_signal(&cv); - assert_int_equal(result, ISC_R_SUCCESS); + subthread_assert_result_equal(result, ISC_R_SUCCESS); UNLOCK(&mx); isc_event_free(&event); @@ -518,7 +574,7 @@ purge(void **state) { LOCK(&mx); - event = isc_event_allocate(mctx, (void *)1 , (isc_eventtype_t)1, + event = isc_event_allocate(test_mctx, (void *)1 , (isc_eventtype_t)1, start_event, NULL, sizeof(*event)); assert_non_null(event); isc_task_send(task1, &event); @@ -554,6 +610,8 @@ purge(void **state) { UNLOCK(&mx); + assert_int_equal(atomic_load(&errcnt), ISC_R_SUCCESS); + assert_int_equal(atomic_load(&eventcnt), 1); isc_timer_detach(&tickertimer); @@ -566,11 +624,11 @@ purge(void **state) { int main(int argc, char **argv) { const struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(ticker, _setup, _teardown), - cmocka_unit_test_setup_teardown(once_life, _setup, _teardown), - cmocka_unit_test_setup_teardown(once_idle, _setup, _teardown), - cmocka_unit_test_setup_teardown(reset, _setup, _teardown), - cmocka_unit_test_setup_teardown(purge, _setup, _teardown), + cmocka_unit_test(ticker), + cmocka_unit_test(once_life), + cmocka_unit_test(once_idle), + cmocka_unit_test(reset), + cmocka_unit_test(purge), }; int c; @@ -584,7 +642,7 @@ main(int argc, char **argv) { } } - return (cmocka_run_group_tests(tests, NULL, NULL)); + return (cmocka_run_group_tests(tests, _setup, _teardown)); } #else /* HAVE_CMOCKA */