aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2013-03-26 10:41:54 +0100
committerTobias Brunner <tobias@strongswan.org>2013-06-11 11:03:10 +0200
commitf15fcdc9d87a91a45ec1ca8ab9d4626b0b811514 (patch)
tree1d93a87d838f5edbe61c634a3341cb01bd2ee325 /src
parent156dcbc12e487c1a3649f8e1b9b474d569c67969 (diff)
downloadstrongswan-f15fcdc9d87a91a45ec1ca8ab9d4626b0b811514.tar.bz2
strongswan-f15fcdc9d87a91a45ec1ca8ab9d4626b0b811514.tar.xz
Add tests for enumerator_t
Diffstat (limited to 'src')
-rw-r--r--src/libstrongswan/tests/Makefile.am3
-rw-r--r--src/libstrongswan/tests/test_enumerator.c191
-rw-r--r--src/libstrongswan/tests/test_runner.c1
-rw-r--r--src/libstrongswan/tests/test_runner.h2
4 files changed, 196 insertions, 1 deletions
diff --git a/src/libstrongswan/tests/Makefile.am b/src/libstrongswan/tests/Makefile.am
index 215cdde8b..b16180b8d 100644
--- a/src/libstrongswan/tests/Makefile.am
+++ b/src/libstrongswan/tests/Makefile.am
@@ -3,7 +3,8 @@ TESTS = test_runner
check_PROGRAMS = $(TESTS)
test_runner_SOURCES = \
- test_runner.c test_runner.h
+ test_runner.c test_runner.h \
+ test_enumerator.c
test_runner_CFLAGS = \
diff --git a/src/libstrongswan/tests/test_enumerator.c b/src/libstrongswan/tests/test_enumerator.c
new file mode 100644
index 000000000..882e8cc61
--- /dev/null
+++ b/src/libstrongswan/tests/test_enumerator.c
@@ -0,0 +1,191 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Copyright (C) 2007 Martin Willi
+ * 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.
+ */
+
+#include <check.h>
+
+#include <collections/enumerator.h>
+#include <collections/linked_list.h>
+
+/*******************************************************************************
+ * token test
+ */
+
+static const char *token_results1[] = { "abc", "cde", "efg" };
+static const char *token_results2[] = { "a", "b", "c" };
+
+static struct {
+ char *string;
+ char *sep;
+ char *trim;
+ const char **results;
+} token_tests[] = {
+ {"abc, cde, efg", ",", " ", token_results1},
+ {" abc 1:2 cde;3 4efg5. ", ":;.,", " 12345", token_results1},
+ {"abc.cde,efg", ",.", "", token_results1},
+ {" abc cde efg ", " ", " ", token_results1},
+ {"a'abc' c 'cde' cefg", " ", " abcd", token_results1},
+ {"'abc' abc 'cde'd 'efg'", " ", " abcd", token_results1},
+
+ {"a, b, c", ",", " ", token_results2},
+ {"a,b,c", ",", " ", token_results2},
+ {" a 1:2 b;3 4c5. ", ":;.,", " 12345", token_results2},
+ {"a.b,c", ",.", "", token_results2},
+ {" a b c ", " ", " ", token_results2},
+};
+
+START_TEST(test_token)
+{
+ enumerator_t *enumerator;
+ const char **results;
+ char *token;
+ int tok = 0;
+
+ enumerator = enumerator_create_token(token_tests[_i].string,
+ token_tests[_i].sep, token_tests[_i].trim);
+ results = token_tests[_i].results;
+ while (enumerator->enumerate(enumerator, &token))
+ {
+ switch (tok)
+ {
+ case 0:
+ case 1:
+ case 2:
+ ck_assert_str_eq(token, results[tok]);
+ break;
+ default:
+ fail("unexpected token '%s'", token);
+ }
+ tok++;
+ }
+ fail_if(tok != 3, "not enough tokens (%d) extracted from '%s'",
+ tok, token_tests[_i].string);
+ enumerator->destroy(enumerator);
+}
+END_TEST
+
+/*******************************************************************************
+ * utility for filtered and nested tests
+ */
+
+static void destroy_data(void *data)
+{
+ fail_if(data != (void*)101, "data does not match '101' in destructor");
+}
+
+/*******************************************************************************
+ * filtered test
+ */
+
+static bool filter(void *data, int *v, int *vo, int *w, int *wo,
+ int *x, int *xo, int *y, int *yo, int *z, int *zo)
+{
+ int val = *v;
+
+ *vo = val++;
+ *wo = val++;
+ *xo = val++;
+ *yo = val++;
+ *zo = val++;
+ fail_if(data != (void*)101, "data does not match '101' in filter function");
+ return TRUE;
+}
+
+START_TEST(test_filtered)
+{
+ int round, v, w, x, y, z;
+ linked_list_t *list;
+ enumerator_t *enumerator;
+
+ list = linked_list_create_with_items((void*)1, (void*)2, (void*)3, (void*)4,
+ (void*)5, NULL);
+
+ round = 1;
+ enumerator = enumerator_create_filter(list->create_enumerator(list),
+ (void*)filter, (void*)101, destroy_data);
+ while (enumerator->enumerate(enumerator, &v, &w, &x, &y, &z))
+ {
+ ck_assert_int_eq(v, round);
+ ck_assert_int_eq(w, round + 1);
+ ck_assert_int_eq(x, round + 2);
+ ck_assert_int_eq(y, round + 3);
+ ck_assert_int_eq(z, round + 4);
+ round++;
+ }
+ enumerator->destroy(enumerator);
+
+ list->destroy(list);
+}
+END_TEST
+
+/*******************************************************************************
+ * nested test
+ */
+
+static enumerator_t* create_inner(linked_list_t *outer, void *data)
+{
+ fail_if(data != (void*)101, "data does not match '101' in nested constr.");
+ return outer->create_enumerator(outer);
+}
+
+START_TEST(test_nested)
+{
+ int round, x;
+ linked_list_t *list, *l1, *l2, *l3;
+ enumerator_t *enumerator;
+
+ l1 = linked_list_create_with_items((void*)1, (void*)2, NULL);
+ l2 = linked_list_create();
+ l3 = linked_list_create_with_items((void*)3, (void*)4, (void*)5, NULL);
+ list = linked_list_create_with_items(l1, l2, l3, NULL);
+
+ round = 1;
+ enumerator = enumerator_create_nested(list->create_enumerator(list),
+ (void*)create_inner, (void*)101, destroy_data);
+ while (enumerator->enumerate(enumerator, &x))
+ {
+ ck_assert_int_eq(round, x);
+ round++;
+ }
+ enumerator->destroy(enumerator);
+
+ list->destroy(list);
+ l1->destroy(l1);
+ l2->destroy(l2);
+ l3->destroy(l3);
+}
+END_TEST
+
+Suite *enumerator_suite_create()
+{
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("enumerator");
+
+ tc = tcase_create("tokens");
+ tcase_add_loop_test(tc, test_token, 0, countof(token_tests));
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("filtered");
+ tcase_add_test(tc, test_filtered);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("nested");
+ tcase_add_test(tc, test_nested);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
diff --git a/src/libstrongswan/tests/test_runner.c b/src/libstrongswan/tests/test_runner.c
index 264dd700d..2c327c2ee 100644
--- a/src/libstrongswan/tests/test_runner.c
+++ b/src/libstrongswan/tests/test_runner.c
@@ -28,6 +28,7 @@ int main()
library_init(NULL);
sr = srunner_create(NULL);
+ srunner_add_suite(sr, enumerator_suite_create());
srunner_run_all(sr, CK_NORMAL);
nf = srunner_ntests_failed(sr);
diff --git a/src/libstrongswan/tests/test_runner.h b/src/libstrongswan/tests/test_runner.h
index f7a7d7510..1bf9a5f29 100644
--- a/src/libstrongswan/tests/test_runner.h
+++ b/src/libstrongswan/tests/test_runner.h
@@ -18,4 +18,6 @@
#include <check.h>
+Suite *enumerator_suite_create();
+
#endif /** TEST_RUNNER_H_ */