From c1be24c9c4050f3c9efa8478867f3e6957919a9d Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Thu, 12 Feb 2015 21:53:15 +0100 Subject: [PATCH 101/114] Add leak check and command line option to test_authtok MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Lukáš Slebodník (cherry picked from commit 80b5dbe123ec94c5a8fcb99f9a4953c1513deb58) --- Makefile.am | 3 ++ src/tests/cmocka/test_authtok.c | 67 +++++++++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Makefile.am b/Makefile.am index 99729ff6041a29dc79de7f90511d60420af8fd19..fc369f6bdae5d414fad1cc6abd3a514ce931ecec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1856,11 +1856,14 @@ test_authtok_SOURCES = \ test_authtok_CFLAGS = \ $(AM_CFLAGS) \ $(TALLOC_CFLAGS) \ + $(POPT_CFLAGS) \ $(DHASH_CFLAGS) test_authtok_LDADD = \ $(TALLOC_LIBS) \ $(CMOCKA_LIBS) \ $(DHASH_LIBS) \ + $(POPT_LIBS) \ + libsss_test_common.la \ libsss_debug.la sss_nss_idmap_tests_SOURCES = \ diff --git a/src/tests/cmocka/test_authtok.c b/src/tests/cmocka/test_authtok.c index e37e92f68373d564f53b1267f078ea89c31ae051..0c7b7197fb2c03d69dc4df2310229ea100ad28d4 100644 --- a/src/tests/cmocka/test_authtok.c +++ b/src/tests/cmocka/test_authtok.c @@ -22,11 +22,10 @@ along with this program. If not, see . */ -#include -#include -#include -#include #include +#include + +#include "tests/cmocka/common_mock.h" #include "util/authtok.h" @@ -39,12 +38,15 @@ static int setup(void **state) { struct test_state *ts = NULL; - ts = talloc(NULL, struct test_state); + assert_true(leak_check_setup()); + + ts = talloc(global_talloc_context, struct test_state); assert_non_null(ts); ts->authtoken = sss_authtok_new(ts); assert_non_null(ts->authtoken); + check_leaks_push(ts); *state = (void *)ts; return 0; } @@ -52,7 +54,12 @@ static int setup(void **state) static int teardown(void **state) { struct test_state *ts = talloc_get_type_abort(*state, struct test_state); + + assert_non_null(ts); + + assert_true(check_leaks_pop(ts) == true); talloc_free(ts); + assert_true(leak_check_teardown()); return 0; } @@ -85,7 +92,7 @@ static void test_sss_authtok_password(void **state) { size_t len; errno_t ret; - const char *data; + char *data; size_t ret_len; const char *pwd; struct test_state *ts; @@ -117,6 +124,9 @@ static void test_sss_authtok_password(void **state) assert_int_equal(ret, EOK); assert_string_equal(data, pwd); assert_int_equal(len - 1, ret_len); + + talloc_free(data); + sss_authtok_set_empty(ts->authtoken); } /* Test when type has value SSS_AUTHTOK_TYPE_CCFILE */ @@ -124,7 +134,7 @@ static void test_sss_authtok_ccfile(void **state) { size_t len; errno_t ret; - const char *data; + char *data; size_t ret_len; const char *pwd; struct test_state *ts; @@ -172,6 +182,9 @@ static void test_sss_authtok_ccfile(void **state) assert_int_equal(ret, EOK); assert_string_equal(data, pwd); assert_int_equal(len - 1, ret_len); + + talloc_free(data); + sss_authtok_set_empty(ts->authtoken); } /* Test when type has value SSS_AUTHTOK_TYPE_EMPTY */ @@ -226,7 +239,7 @@ static void test_sss_authtok_wipe_password(void **state) { size_t len; errno_t ret; - const char *data; + char *data; size_t ret_len; const char *pwd; struct test_state *ts; @@ -249,13 +262,16 @@ static void test_sss_authtok_wipe_password(void **state) assert_int_equal(ret, EOK); assert_string_equal(pwd, ""); assert_int_equal(len - 1, ret_len); + + sss_authtok_set_empty(ts->authtoken); + talloc_free(data); } static void test_sss_authtok_copy(void **state) { size_t len; errno_t ret; - const char *data; + char *data; struct test_state *ts; enum sss_authtok_type type; struct sss_auth_token *dest_authtoken; @@ -276,6 +292,7 @@ static void test_sss_authtok_copy(void **state) assert_int_equal(EOK, sss_authtok_copy(ts->authtoken, dest_authtoken)); assert_int_equal(type, sss_authtok_get_type(dest_authtoken)); + sss_authtok_set_empty(dest_authtoken); type = SSS_AUTHTOK_TYPE_PASSWORD; ret = sss_authtok_set(ts->authtoken, type, (const uint8_t *)data, len); @@ -287,10 +304,23 @@ static void test_sss_authtok_copy(void **state) assert_int_equal(type, sss_authtok_get_type(dest_authtoken)); assert_string_equal(data, sss_authtok_get_data(dest_authtoken)); assert_int_equal(len, sss_authtok_get_size(dest_authtoken)); + + sss_authtok_set_empty(dest_authtoken); + talloc_free(dest_authtoken); + sss_authtok_set_empty(ts->authtoken); + talloc_free(data); } -int main(void) +int main(int argc, const char *argv[]) { + poptContext pc; + int opt; + struct poptOption long_options[] = { + POPT_AUTOHELP + SSSD_DEBUG_OPTS + POPT_TABLEEND + }; + const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(test_sss_authtok_new, setup, teardown), @@ -306,5 +336,22 @@ int main(void) setup, teardown) }; + /* Set debug level to invalid value so we can deside if -d 0 was used. */ + debug_level = SSSDBG_INVALID; + + pc = poptGetContext(argv[0], argc, argv, long_options, 0); + while((opt = poptGetNextOpt(pc)) != -1) { + switch(opt) { + default: + fprintf(stderr, "\nInvalid option %s: %s\n\n", + poptBadOption(pc, 0), poptStrerror(opt)); + poptPrintUsage(pc, stderr, 0); + return 1; + } + } + poptFreeContext(pc); + + DEBUG_CLI_INIT(debug_level); + return cmocka_run_group_tests(tests, NULL, NULL); } -- 2.4.0