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
This commit is contained in:
Neels Hofmeyr 2017-11-18 18:47:55 +01:00
parent da86fe5ae3
commit d9cb19a8fb
5 changed files with 33 additions and 1 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -1,3 +1,4 @@
#pragma once
int test_common_init(void);
void test_common_cleanup(void);