>From 8fef8d5da303a857d9eb689539d1c8ec898fb0c5 Mon Sep 17 00:00:00 2001 From: Lukas Slebodnik Date: Wed, 20 Aug 2014 19:09:59 +0200 Subject: [PATCH 3/7] test_dyndns: Use different talloc context in wrapped functions. Real functions use own allocation strategy. We use talloc in wrapped functions. But wrapped functions should not use global_talloc_context, leak_check_teardown will report false positive memory leaks. leak_check_teardown() ./src/tests/cmocka/test_dyndns.c:378: error: Failure! [ FAILED ] dyndns_test_ok_dyndns_test_teardown --- src/tests/cmocka/test_dyndns.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/tests/cmocka/test_dyndns.c b/src/tests/cmocka/test_dyndns.c index b7e45a19db5dd9e85260029fe81e46aa18c1c8ed..1349fff9dab2c97ebf4afcb77cc8c01a08cb0f15 100644 --- a/src/tests/cmocka/test_dyndns.c +++ b/src/tests/cmocka/test_dyndns.c @@ -47,6 +47,8 @@ enum mock_nsupdate_states { MOCK_NSUPDATE_TIMEOUT, }; +static TALLOC_CTX *global_mock_context = NULL; + struct dyndns_test_ctx { struct sss_test_ctx *tctx; @@ -104,7 +106,7 @@ int __wrap_getifaddrs(struct ifaddrs **_ifap) goto fail; } - ifap = talloc_zero(global_talloc_context, struct ifaddrs); + ifap = talloc_zero(global_mock_context, struct ifaddrs); if (ifap == NULL) { errno = ENOMEM; /* getifaddrs sets errno, too */ goto fail; @@ -198,6 +200,7 @@ void dyndns_test_get_ifaddr(void **state) assert_string_equal(straddr, "192.168.0.1"); talloc_free(addrlist); + assert_true(check_leaks_pop(dyndns_test_ctx) == true); } @@ -356,6 +359,9 @@ void dyndns_test_setup(void **state) }; assert_true(leak_check_setup()); + global_mock_context = talloc_new(global_talloc_context); + assert_non_null(global_mock_context); + dyndns_test_ctx = talloc_zero(global_talloc_context, struct dyndns_test_ctx); assert_non_null(dyndns_test_ctx); @@ -372,9 +378,20 @@ void dyndns_test_setup(void **state) dyndns_test_ctx->be_ctx->conf_path = dyndns_test_ctx->tctx->conf_dom_path; } +void dyndns_test_simple_setup(void **state) +{ + assert_true(leak_check_setup()); + global_mock_context = talloc_new(global_talloc_context); + assert_non_null(global_mock_context); + + dyndns_test_ctx = talloc_zero(global_talloc_context, struct dyndns_test_ctx); + assert_non_null(dyndns_test_ctx); +} + void dyndns_test_teardown(void **state) { talloc_free(dyndns_test_ctx); + talloc_free(global_mock_context); assert_true(leak_check_teardown()); } @@ -394,7 +411,9 @@ int main(int argc, const char *argv[]) const UnitTest tests[] = { /* Utility functions unit test */ - unit_test(dyndns_test_get_ifaddr), + unit_test_setup_teardown(dyndns_test_get_ifaddr, + dyndns_test_simple_setup, + dyndns_test_teardown), /* Dynamic DNS update unit tests*/ unit_test_setup_teardown(dyndns_test_ok, -- 2.1.0