diff options
author | Tobias Brunner <tobias@strongswan.org> | 2013-06-05 14:01:40 +0200 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2013-06-11 11:03:13 +0200 |
commit | 95e9915074c8ac1f6eefa4d8d986de7a4bd7d2ca (patch) | |
tree | 968da1c7a8fbd0a36173f72ee4d6b59e0048e91c /src/libstrongswan | |
parent | 2b4902973b95bbd8063fab612c325e302c648d29 (diff) | |
download | strongswan-95e9915074c8ac1f6eefa4d8d986de7a4bd7d2ca.tar.bz2 strongswan-95e9915074c8ac1f6eefa4d8d986de7a4bd7d2ca.tar.xz |
Enable leak detective for all test cases
Diffstat (limited to 'src/libstrongswan')
-rw-r--r-- | src/libstrongswan/tests/Makefile.am | 2 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_bio_reader.c | 2 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_bio_writer.c | 2 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_chunk.c | 2 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_enum.c | 2 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_enumerator.c | 8 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_hashtable.c | 8 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_identification.c | 2 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_linked_list.c | 8 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_linked_list_enumerator.c | 8 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_runner.c | 2 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_suite.h | 99 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_threading.c | 3 | ||||
-rw-r--r-- | src/libstrongswan/tests/test_utils.c | 3 |
14 files changed, 129 insertions, 22 deletions
diff --git a/src/libstrongswan/tests/Makefile.am b/src/libstrongswan/tests/Makefile.am index e0ac91e5d..6b4ba2c55 100644 --- a/src/libstrongswan/tests/Makefile.am +++ b/src/libstrongswan/tests/Makefile.am @@ -3,7 +3,7 @@ TESTS = test_runner check_PROGRAMS = $(TESTS) test_runner_SOURCES = \ - test_runner.c test_runner.h \ + test_runner.c test_runner.h test_suite.h \ test_linked_list.c test_enumerator.c test_linked_list_enumerator.c \ test_bio_reader.c test_bio_writer.c test_chunk.c test_enum.c test_hashtable.c \ test_identification.c test_threading.c test_utils.c diff --git a/src/libstrongswan/tests/test_bio_reader.c b/src/libstrongswan/tests/test_bio_reader.c index f2df1e260..4f6f8cf59 100644 --- a/src/libstrongswan/tests/test_bio_reader.c +++ b/src/libstrongswan/tests/test_bio_reader.c @@ -13,7 +13,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <bio/bio_reader.h> diff --git a/src/libstrongswan/tests/test_bio_writer.c b/src/libstrongswan/tests/test_bio_writer.c index 0bf4ab977..027606af2 100644 --- a/src/libstrongswan/tests/test_bio_writer.c +++ b/src/libstrongswan/tests/test_bio_writer.c @@ -13,7 +13,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <bio/bio_writer.h> diff --git a/src/libstrongswan/tests/test_chunk.c b/src/libstrongswan/tests/test_chunk.c index 9c5115b13..5fa1c0b1d 100644 --- a/src/libstrongswan/tests/test_chunk.c +++ b/src/libstrongswan/tests/test_chunk.c @@ -15,7 +15,7 @@ */ -#include <check.h> +#include "test_suite.h" #include <utils/chunk.h> diff --git a/src/libstrongswan/tests/test_enum.c b/src/libstrongswan/tests/test_enum.c index 66d5435ba..990d9cfad 100644 --- a/src/libstrongswan/tests/test_enum.c +++ b/src/libstrongswan/tests/test_enum.c @@ -13,7 +13,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <utils/enum.h> #include <utils/utils.h> diff --git a/src/libstrongswan/tests/test_enumerator.c b/src/libstrongswan/tests/test_enumerator.c index 687e476c1..b5dde4650 100644 --- a/src/libstrongswan/tests/test_enumerator.c +++ b/src/libstrongswan/tests/test_enumerator.c @@ -14,7 +14,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <collections/enumerator.h> #include <collections/linked_list.h> @@ -82,15 +82,17 @@ END_TEST static int destroy_data_called; -static void setup_destroy_data() +START_SETUP(setup_destroy_data) { destroy_data_called = 0; } +END_SETUP -static void teardown_destroy_data() +START_TEARDOWN(teardown_destroy_data) { ck_assert_int_eq(destroy_data_called, 1); } +END_TEARDOWN static void destroy_data(void *data) { diff --git a/src/libstrongswan/tests/test_hashtable.c b/src/libstrongswan/tests/test_hashtable.c index bee773535..8cc7bfe42 100644 --- a/src/libstrongswan/tests/test_hashtable.c +++ b/src/libstrongswan/tests/test_hashtable.c @@ -13,7 +13,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <collections/hashtable.h> #include <utils/chunk.h> @@ -38,17 +38,19 @@ static bool equals(char *key1, char *key2) static hashtable_t *ht; -static void setup_ht() +START_SETUP(setup_ht) { ht = hashtable_create((hashtable_hash_t)hash, (hashtable_equals_t)equals, 0); ck_assert_int_eq(ht->get_count(ht), 0); } +END_SETUP -static void teardown_ht() +START_TEARDOWN(teardown_ht) { ht->destroy(ht); } +END_TEARDOWN /******************************************************************************* * put/get diff --git a/src/libstrongswan/tests/test_identification.c b/src/libstrongswan/tests/test_identification.c index d21d4c7fa..b0b3ce826 100644 --- a/src/libstrongswan/tests/test_identification.c +++ b/src/libstrongswan/tests/test_identification.c @@ -14,7 +14,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <utils/identification.h> diff --git a/src/libstrongswan/tests/test_linked_list.c b/src/libstrongswan/tests/test_linked_list.c index 32b5cd7c9..fc055bb7f 100644 --- a/src/libstrongswan/tests/test_linked_list.c +++ b/src/libstrongswan/tests/test_linked_list.c @@ -13,7 +13,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <collections/linked_list.h> @@ -23,7 +23,7 @@ static linked_list_t *list; -static void setup_list() +START_SETUP(setup_list) { void *x = NULL; @@ -32,11 +32,13 @@ static void setup_list() ck_assert(list->get_first(list, &x) == NOT_FOUND); ck_assert(list->get_last(list, &x) == NOT_FOUND); } +END_SETUP -static void teardown_list() +START_TEARDOWN(teardown_list) { list->destroy(list); } +END_TEARDOWN /******************************************************************************* * insert first/last diff --git a/src/libstrongswan/tests/test_linked_list_enumerator.c b/src/libstrongswan/tests/test_linked_list_enumerator.c index d44f7ab9f..338a38224 100644 --- a/src/libstrongswan/tests/test_linked_list_enumerator.c +++ b/src/libstrongswan/tests/test_linked_list_enumerator.c @@ -13,7 +13,7 @@ * for more details. */ -#include <check.h> +#include "test_suite.h" #include <collections/linked_list.h> @@ -23,17 +23,19 @@ static linked_list_t *list; -static void setup_list() +START_SETUP(setup_list) { list = linked_list_create_with_items((void*)1, (void*)2, (void*)3, (void*)4, (void*)5, NULL); ck_assert_int_eq(list->get_count(list), 5); } +END_SETUP -static void teardown_list() +START_TEARDOWN(teardown_list) { list->destroy(list); } +END_TEARDOWN /******************************************************************************* * enumeration diff --git a/src/libstrongswan/tests/test_runner.c b/src/libstrongswan/tests/test_runner.c index 5f3b2d397..2cce42b27 100644 --- a/src/libstrongswan/tests/test_runner.c +++ b/src/libstrongswan/tests/test_runner.c @@ -25,7 +25,7 @@ int main() int nf; /* test cases are forked and there is no cleanup, so disable leak detective. - * can be enabled for individual tests with leak_detective_t.set_state */ + * if test_suite.h is included leak detective is enabled in test cases */ setenv("LEAK_DETECTIVE_DISABLE", "1", 1); /* redirect all output to stderr (to redirect make's stdout to /dev/null) */ dup2(2, 1); diff --git a/src/libstrongswan/tests/test_suite.h b/src/libstrongswan/tests/test_suite.h new file mode 100644 index 000000000..616a50e3a --- /dev/null +++ b/src/libstrongswan/tests/test_suite.h @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2013 Tobias Brunner + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef TEST_UTILS_H_ +#define TEST_UTILS_H_ + +#include <check.h> +#include <library.h> + +/** + * Used to mark test cases that use test fixtures. + */ +#define UNIT_TEST_FIXTURE_USED "UNIT_TEST_FIXTURE_USED" + +/** + * Check for memory leaks and fail if any are encountered. + */ +#define CHECK_FOR_LEAKS() do \ +{ \ + if (lib->leak_detective->leaks(lib->leak_detective)) { \ + lib->leak_detective->report(lib->leak_detective, TRUE); \ + } \ + ck_assert_int_eq(lib->leak_detective->leaks(lib->leak_detective), 0); \ +} \ +while(0) + +/** + * Extended versions of the START|END_TEST macros that use leak detective. + * + * Since each test case runs in its own fork of the test runner the stuff + * allocated before the test starts is not freed, so leak detective is disabled + * by default to prevent false positives. By enabling it right when the test + * starts we at least capture leaks created by the tested objects/functions and + * the test case itself. This allows writing test cases for cleanup functions. + * + * To define test fixture with possibly allocated/destroyed memory that is + * allocated/freed in a test case use the START|END_SETUP|TEARDOWN macros. + */ +#undef START_TEST +#define START_TEST(name) \ +static void name (int _i CK_ATTRIBUTE_UNUSED) \ +{ \ + tcase_fn_start(""#name, __FILE__, __LINE__); \ + lib->leak_detective->set_state(lib->leak_detective, TRUE); + +#undef END_TEST +#define END_TEST \ + if (!lib->get(lib, UNIT_TEST_FIXTURE_USED)) \ + { \ + CHECK_FOR_LEAKS(); \ + } \ +} + +/** + * Define a function to setup a test fixture that can be used with the above + * macros. + */ +#define START_SETUP(name) \ +static void name() \ +{ \ + lib->set(lib, UNIT_TEST_FIXTURE_USED, (void*)TRUE); \ + lib->leak_detective->set_state(lib->leak_detective, TRUE); + +/** + * End a setup function + */ +#define END_SETUP } + +/** + * Define a function to teardown a test fixture that can be used with the above + * macros. + */ +#define START_TEARDOWN(name) \ +static void name() \ +{ + +/** + * End a teardown function + */ +#define END_TEARDOWN \ + if (lib->get(lib, UNIT_TEST_FIXTURE_USED)) \ + { \ + CHECK_FOR_LEAKS(); \ + } \ +} + +#endif /** TEST_UTILS_H_ */ diff --git a/src/libstrongswan/tests/test_threading.c b/src/libstrongswan/tests/test_threading.c index 1ffbcb6b0..0c768b3e2 100644 --- a/src/libstrongswan/tests/test_threading.c +++ b/src/libstrongswan/tests/test_threading.c @@ -14,10 +14,11 @@ * for more details. */ -#include <check.h> #include <sched.h> #include <pthread.h> +#include "test_suite.h" + #include <threading/mutex.h> /******************************************************************************* diff --git a/src/libstrongswan/tests/test_utils.c b/src/libstrongswan/tests/test_utils.c index 9efc77621..f7cb605fa 100644 --- a/src/libstrongswan/tests/test_utils.c +++ b/src/libstrongswan/tests/test_utils.c @@ -13,8 +13,7 @@ * for more details. */ - -#include <check.h> +#include "test_suite.h" #include <library.h> #include <utils/utils.h> |