From d9cb19a8fbb3b6f7b29617fa78fd8bf7492a6667 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Sat, 18 Nov 2017 18:47:55 +0100 Subject: tests: sanitize: fix mem leaks, clean after tests Fix various mem leaks in the testing code. Add test_common_cleanup() in test_common.c, to free talloc contexts; call in test-{helpers,hnbap,ranap}.c. Upon talloc ctx cleanup, ensure that they are actually empty, in order to catch newly introduced mem leaks. If non-empty, print talloc context reports. Change-Id: Ic66c005f2a264774e18bb54e58b87bef5944511c --- src/tests/test-helpers.c | 4 ++++ src/tests/test-hnbap.c | 4 ++++ src/tests/test-ranap.c | 2 ++ src/tests/test_common.c | 23 ++++++++++++++++++++++- src/tests/test_common.h | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/tests/test-helpers.c b/src/tests/test-helpers.c index 44fd735..f218a79 100644 --- a/src/tests/test-helpers.c +++ b/src/tests/test-helpers.c @@ -105,6 +105,8 @@ void test_asn1_helpers(void) ASSERT(enc.size == 24/8); ASSERT(enc.bits_unused == 0); + talloc_free(buffer); + rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer); printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc)); @@ -118,6 +120,7 @@ void test_asn1_helpers(void) printf("Decoding large string from asn1: %s\n", text); ASSERT(rc == 31); + talloc_free(buffer); } void test_ranap_common(void) @@ -211,5 +214,6 @@ int main(int argc, char **argv) test_asn1_helpers(); test_ranap_common(); + test_common_cleanup(); return 0; } diff --git a/src/tests/test-hnbap.c b/src/tests/test-hnbap.c index ef46070..dfd5ae9 100644 --- a/src/tests/test-hnbap.c +++ b/src/tests/test-hnbap.c @@ -142,6 +142,8 @@ void test_asn1_decoding(void) printf("HNBAP UE Register request from IMSI %s\n", imsi); hnbap_free_ueregisterrequesties(&ue_req_ies); + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu); + memset(pdu, 0, sizeof(*pdu)); dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu, hnbap_ue_reg_acc, sizeof(hnbap_ue_reg_acc), 0, 0); @@ -163,6 +165,7 @@ void test_asn1_decoding(void) printf("HNBAP UE Register accept to IMSI %s\n", imsi); hnbap_free_ueregisteraccepties(&ue_acc_ies); + ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu); } int main(int argc, char **argv) @@ -175,6 +178,7 @@ int main(int argc, char **argv) test_asn1_decoding(); + test_common_cleanup(); return 0; } diff --git a/src/tests/test-ranap.c b/src/tests/test-ranap.c index c1c7003..05be874 100644 --- a/src/tests/test-ranap.c +++ b/src/tests/test-ranap.c @@ -197,6 +197,8 @@ int main(int argc, char **argv) talloc_report(talloc_asn1_ctx, stdout); talloc_report(tall_msgb_ctx, stdout); //talloc_report(NULL, stdout); + + test_common_cleanup(); printf("exit\n"); exit(0); } diff --git a/src/tests/test_common.c b/src/tests/test_common.c index c8aafdd..0af8ceb 100644 --- a/src/tests/test_common.c +++ b/src/tests/test_common.c @@ -69,11 +69,13 @@ static const struct log_info test_log_info = { .num_cat = ARRAY_SIZE(log_cat), }; +static void *msgb_ctx; + int test_common_init(void) { int rc; - msgb_talloc_ctx_init(NULL, 0); + msgb_ctx = msgb_talloc_ctx_init(NULL, 0); talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); rc = osmo_init_logging(&test_log_info); @@ -85,3 +87,22 @@ int test_common_init(void) log_set_print_filename(osmo_stderr_target, 0); log_set_use_color(osmo_stderr_target, 0); } + +void test_common_cleanup(void) +{ + if (talloc_total_blocks(msgb_ctx) != 1 + || talloc_total_size(msgb_ctx) != 0) + talloc_report_full(msgb_ctx, stderr); + + OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1); + OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0); + talloc_free(msgb_ctx); + + if (talloc_total_blocks(talloc_asn1_ctx) != 1 + || talloc_total_size(talloc_asn1_ctx) != 0) + talloc_report_full(talloc_asn1_ctx, stderr); + + OSMO_ASSERT(talloc_total_blocks(talloc_asn1_ctx) == 1); + OSMO_ASSERT(talloc_total_size(talloc_asn1_ctx) == 0); + talloc_free(talloc_asn1_ctx); +} diff --git a/src/tests/test_common.h b/src/tests/test_common.h index 1af1abd..836d999 100644 --- a/src/tests/test_common.h +++ b/src/tests/test_common.h @@ -1,3 +1,4 @@ #pragma once int test_common_init(void); +void test_common_cleanup(void); -- cgit v1.2.3