From 7e6dfb76834f4ed5ebe574572559004e95d2bdea Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 30 Apr 2009 14:58:16 +0000 Subject: [PATCH] Fix leaks in tests * basic_tests.c: * login_tests.c: * login_tests_saml2.c: * random_tests.c: free replaced string, unref used nodes, initialize local variables when necessary, free newly allocated strings. --- tests/basic_tests.c | 1 + tests/login_tests.c | 1 + tests/login_tests_saml2.c | 73 ++++++++++++++++++++------------------- tests/random_tests.c | 20 ++++++++--- 4 files changed, 54 insertions(+), 41 deletions(-) diff --git a/tests/basic_tests.c b/tests/basic_tests.c index 56b91ad3..4503d258 100644 --- a/tests/basic_tests.c +++ b/tests/basic_tests.c @@ -166,6 +166,7 @@ START_TEST(test09_test_deserialization) node = lasso_node_new_from_dump(content); fail_unless(node != NULL, "node should be parsed"); g_object_unref(node); + g_free(content); } END_TEST diff --git a/tests/login_tests.c b/tests/login_tests.c index 21c1ad1d..eb7dc869 100644 --- a/tests/login_tests.c +++ b/tests/login_tests.c @@ -290,6 +290,7 @@ START_TEST(test02_serviceProviderLogin) rc = lasso_login_accept_sso(spLoginContext); fail_unless(rc == 0, "lasso_login_accept_sso must fail"); + g_free(spLoginDump); g_free(serviceProviderId); g_free(serviceProviderContextDump); g_free(identityProviderContextDump); diff --git a/tests/login_tests_saml2.c b/tests/login_tests_saml2.c index 2f367ca3..6726415e 100644 --- a/tests/login_tests_saml2.c +++ b/tests/login_tests_saml2.c @@ -83,12 +83,12 @@ generateServiceProviderContextDump() static char* generateIdentityProviderContextDumpMemory() { - LassoServer *serverContext; - char *metadata; - char *private_key; - char *certificate; + LassoServer *serverContext = NULL; + char *metadata = NULL; + char *private_key = NULL; + char *certificate = NULL; guint len; - char *ret; + char *ret = NULL; g_file_get_contents(TESTSDATADIR "/idp5-saml2/metadata.xml", &metadata, &len, NULL); g_file_get_contents(TESTSDATADIR "/idp5-saml2/private-key.pem", &private_key, &len, NULL); @@ -132,20 +132,20 @@ END_TEST START_TEST(test02_saml2_serviceProviderLogin) { - char *serviceProviderContextDump, *identityProviderContextDump; - LassoServer *spContext, *idpContext; - LassoLogin *spLoginContext, *idpLoginContext; - LassoSamlp2AuthnRequest *request; + char *serviceProviderContextDump = NULL, *identityProviderContextDump = NULL; + LassoServer *spContext = NULL, *idpContext = NULL; + LassoLogin *spLoginContext = NULL, *idpLoginContext = NULL; + LassoSamlp2AuthnRequest *request = NULL; int rc; - char *relayState; - char *authnRequestUrl, *authnRequestQuery; - char *responseUrl, *responseQuery; - char *idpIdentityContextDump, *idpSessionContextDump; - char *serviceProviderId, *soapRequestMsg, *soapResponseMsg; - char *spIdentityContextDump; - char *spSessionDump; - char *spLoginDump, *idpLoginDump; - char *found; + char *relayState = NULL; + char *authnRequestUrl = NULL, *authnRequestQuery = NULL; + char *responseUrl = NULL, *responseQuery = NULL; + char *idpIdentityContextDump = NULL, *idpSessionContextDump = NULL; + char *serviceProviderId = NULL, *soapRequestMsg = NULL, *soapResponseMsg = NULL; + char *spIdentityContextDump = NULL; + char *spSessionDump = NULL; + char *spLoginDump = NULL, *idpLoginDump = NULL; + char *found = NULL; serviceProviderContextDump = generateServiceProviderContextDump(); spContext = lasso_server_new_from_dump(serviceProviderContextDump); @@ -158,10 +158,10 @@ START_TEST(test02_saml2_serviceProviderLogin) request = LASSO_SAMLP2_AUTHN_REQUEST(LASSO_PROFILE(spLoginContext)->request); fail_unless(LASSO_IS_SAMLP2_AUTHN_REQUEST(request), "request should be authn_request"); request->IsPassive = 0; - request->NameIDPolicy->Format = g_strdup(LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT); + lasso_assign_string(request->NameIDPolicy->Format, LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT); request->NameIDPolicy->AllowCreate = 1; relayState = "fake[]"; - LASSO_PROFILE(spLoginContext)->msg_relayState = g_strdup(relayState); + lasso_assign_string(LASSO_PROFILE(spLoginContext)->msg_relayState, relayState); rc = lasso_login_build_authn_request_msg(spLoginContext); fail_unless(rc == 0, "lasso_login_build_authn_request_msg failed"); authnRequestUrl = LASSO_PROFILE(spLoginContext)->msg_url; @@ -223,7 +223,7 @@ START_TEST(test02_saml2_serviceProviderLogin) "responseQuery should contain a RelayState parameter"); fail_unless(strstr(responseQuery, "fake%5B%5D") != NULL, "responseQuery RelayState parameter should be encoded"); - serviceProviderId = g_strdup(LASSO_PROFILE(idpLoginContext)->remote_providerID); + lasso_assign_string(serviceProviderId, LASSO_PROFILE(idpLoginContext)->remote_providerID); fail_unless(serviceProviderId != NULL, "lasso_profile_get_remote_providerID shouldn't return NULL"); @@ -296,6 +296,7 @@ START_TEST(test02_saml2_serviceProviderLogin) g_free(idpIdentityContextDump); g_free(spIdentityContextDump); g_free(spSessionDump); + g_free(spLoginDump); g_object_unref(spContext); g_object_unref(idpContext); g_object_unref(spLoginContext); @@ -305,19 +306,19 @@ END_TEST START_TEST(test03_saml2_serviceProviderLogin) { - char *serviceProviderContextDump, *identityProviderContextDump; - LassoServer *spContext, *idpContext; - LassoLogin *spLoginContext, *idpLoginContext; - LassoSamlp2AuthnRequest *request; + char *serviceProviderContextDump = NULL, *identityProviderContextDump = NULL; + LassoServer *spContext = NULL, *idpContext = NULL; + LassoLogin *spLoginContext = NULL, *idpLoginContext = NULL; + LassoSamlp2AuthnRequest *request = NULL; + char *relayState = NULL; + char *authnRequestUrl = NULL, *authnRequestQuery = NULL; + char *responseUrl = NULL, *responseQuery = NULL; + char *idpIdentityContextDump = NULL, *idpSessionContextDump = NULL; + char *serviceProviderId = NULL, *soapRequestMsg = NULL, *soapResponseMsg = NULL; + char *spIdentityContextDump = NULL; + char *spSessionDump = NULL; + char *idpLoginDump = NULL; int rc; - char *relayState; - char *authnRequestUrl, *authnRequestQuery; - char *responseUrl, *responseQuery; - char *idpIdentityContextDump, *idpSessionContextDump; - char *serviceProviderId, *soapRequestMsg, *soapResponseMsg; - char *spIdentityContextDump; - char *spSessionDump; - char *idpLoginDump; serviceProviderContextDump = generateServiceProviderContextDump(); spContext = lasso_server_new_from_dump(serviceProviderContextDump); @@ -330,10 +331,10 @@ START_TEST(test03_saml2_serviceProviderLogin) request = LASSO_SAMLP2_AUTHN_REQUEST(LASSO_PROFILE(spLoginContext)->request); fail_unless(LASSO_IS_SAMLP2_AUTHN_REQUEST(request), "request should be authn_request"); request->IsPassive = 0; - request->NameIDPolicy->Format = g_strdup(LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT); + lasso_assign_string(request->NameIDPolicy->Format, LASSO_SAML2_NAME_IDENTIFIER_FORMAT_PERSISTENT); request->NameIDPolicy->AllowCreate = 1; relayState = "fake"; - LASSO_PROFILE(spLoginContext)->msg_relayState = g_strdup(relayState); + lasso_assign_string(LASSO_PROFILE(spLoginContext)->msg_relayState, relayState); rc = lasso_login_build_authn_request_msg(spLoginContext); fail_unless(rc == 0, "lasso_login_build_authn_request_msg failed"); authnRequestUrl = LASSO_PROFILE(spLoginContext)->msg_url; @@ -382,7 +383,7 @@ START_TEST(test03_saml2_serviceProviderLogin) responseQuery = strchr(responseUrl, '?')+1; fail_unless(strlen(responseQuery) > 0, "responseQuery shouldn't be an empty string"); - serviceProviderId = g_strdup(LASSO_PROFILE(idpLoginContext)->remote_providerID); + lasso_assign_string(serviceProviderId, LASSO_PROFILE(idpLoginContext)->remote_providerID); fail_unless(serviceProviderId != NULL, "lasso_profile_get_remote_providerID shouldn't return NULL"); diff --git a/tests/random_tests.c b/tests/random_tests.c index 47b5aaee..33841c17 100644 --- a/tests/random_tests.c +++ b/tests/random_tests.c @@ -32,6 +32,7 @@ #include #include #include +#include "../lasso/utils.h" Suite* random_suite(); @@ -49,6 +50,7 @@ START_TEST(test01_provider_new) dump = lasso_node_dump(LASSO_NODE(provider)); printf("dump:\n%s\n", dump); g_object_unref(provider); + lasso_release_string(dump); } END_TEST @@ -63,12 +65,13 @@ START_TEST(test02_provider_new_from_dump) TESTSDATADIR "/ca1-la/certificate.pem"); dump = lasso_node_dump(LASSO_NODE(provider1)); - provider2 = lasso_provider_new_from_dump(dump); + lasso_release_string(dump); dump = lasso_node_dump(LASSO_NODE(provider2)); printf("dump:\n%s\n", dump); g_object_unref(provider1); g_object_unref(provider2); + lasso_release_string(dump); } END_TEST @@ -86,6 +89,7 @@ START_TEST(test01_server_new) dump = lasso_node_dump(LASSO_NODE(server)); printf("dump:%s\n", dump); g_object_unref(server); + lasso_release_string(dump); } END_TEST @@ -108,6 +112,7 @@ START_TEST(test02_server_add_provider) dump = lasso_node_dump(LASSO_NODE(server)); g_object_unref(server); + lasso_release_string(dump); } END_TEST @@ -205,17 +210,22 @@ START_TEST(test06_lib_statuscode) * starts with lib:, that namespace must be defined. (was bug#416) */ LassoSamlpResponse *response = LASSO_SAMLP_RESPONSE(lasso_samlp_response_new()); + char *dump = NULL; - response->Status->StatusCode->Value = g_strdup(LASSO_SAML_STATUS_CODE_SUCCESS); - fail_unless(strstr(lasso_node_dump(LASSO_NODE(response)), "xmlns:lib=") == NULL, + lasso_assign_string(response->Status->StatusCode->Value, LASSO_SAML_STATUS_CODE_SUCCESS); + dump = lasso_node_dump(LASSO_NODE(response)); + fail_unless(strstr(dump, "xmlns:lib=") == NULL, "liberty namespace should not be defined"); + lasso_release_string(dump); - response->Status->StatusCode->Value = g_strdup(LASSO_SAML_STATUS_CODE_RESPONDER); + lasso_assign_string(response->Status->StatusCode->Value, LASSO_SAML_STATUS_CODE_RESPONDER); response->Status->StatusCode->StatusCode = lasso_samlp_status_code_new(); response->Status->StatusCode->StatusCode->Value = g_strdup( LASSO_LIB_STATUS_CODE_UNKNOWN_PRINCIPAL); - fail_unless(strstr(lasso_node_dump(LASSO_NODE(response)), "xmlns:lib=") != NULL, + dump = lasso_node_dump(LASSO_NODE(response)); + fail_unless(strstr(dump, "xmlns:lib=") != NULL, "liberty namespace should be defined"); + lasso_release_string(dump); g_object_unref(response); } END_TEST