aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libcharon/plugins/unit_tester/Makefile.am3
-rw-r--r--src/libcharon/plugins/unit_tester/tests.h1
-rw-r--r--src/libcharon/plugins/unit_tester/tests/test_hashtable.c111
-rw-r--r--src/libstrongswan/tests/Makefile.am2
-rw-r--r--src/libstrongswan/tests/test_hashtable.c295
-rw-r--r--src/libstrongswan/tests/test_runner.c1
-rw-r--r--src/libstrongswan/tests/test_runner.h1
7 files changed, 299 insertions, 115 deletions
diff --git a/src/libcharon/plugins/unit_tester/Makefile.am b/src/libcharon/plugins/unit_tester/Makefile.am
index 84e717034..b597b928b 100644
--- a/src/libcharon/plugins/unit_tester/Makefile.am
+++ b/src/libcharon/plugins/unit_tester/Makefile.am
@@ -22,7 +22,6 @@ libstrongswan_unit_tester_la_SOURCES = \
tests/test_med_db.c \
tests/test_chunk.c \
tests/test_pool.c \
- tests/test_agent.c \
- tests/test_hashtable.c
+ tests/test_agent.c
libstrongswan_unit_tester_la_LDFLAGS = -module -avoid-version
diff --git a/src/libcharon/plugins/unit_tester/tests.h b/src/libcharon/plugins/unit_tester/tests.h
index fc7761bb5..527bb2572 100644
--- a/src/libcharon/plugins/unit_tester/tests.h
+++ b/src/libcharon/plugins/unit_tester/tests.h
@@ -18,7 +18,6 @@
* @{ @ingroup unit_tester
*/
-DEFINE_TEST("hashtable_t->remove_at()", test_hashtable_remove_at, FALSE)
DEFINE_TEST("auth cfg", test_auth_cfg, FALSE)
DEFINE_TEST("CURL get", test_curl_get, FALSE)
DEFINE_TEST("MySQL operations", test_mysql, FALSE)
diff --git a/src/libcharon/plugins/unit_tester/tests/test_hashtable.c b/src/libcharon/plugins/unit_tester/tests/test_hashtable.c
deleted file mode 100644
index 5513f6707..000000000
--- a/src/libcharon/plugins/unit_tester/tests/test_hashtable.c
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-
-#include <library.h>
-#include <collections/hashtable.h>
-
-static u_int hash(char *key)
-{
- return chunk_hash(chunk_create(key, strlen(key)));
-}
-
-static u_int equals(char *key1, char *key2)
-{
- return streq(key1, key2);
-}
-
-/**
- * Test the remove_at method
- */
-bool test_hashtable_remove_at()
-{
- char *k1 = "key1", *k2 = "key2", *k3 = "key3", *key;
- char *v1 = "val1", *v2 = "val2", *v3 = "val3", *value;
- enumerator_t *enumerator;
- hashtable_t *ht = hashtable_create((hashtable_hash_t)hash,
- (hashtable_equals_t)equals, 0);
-
- ht->put(ht, k1, v1);
- ht->put(ht, k2, v2);
- ht->put(ht, k3, v3);
-
- if (ht->get_count(ht) != 3)
- {
- return FALSE;
- }
-
- enumerator = ht->create_enumerator(ht);
- while (enumerator->enumerate(enumerator, &key, &value))
- {
- if (streq(key, k2))
- {
- ht->remove_at(ht, enumerator);
- }
- }
- enumerator->destroy(enumerator);
-
- if (ht->get_count(ht) != 2)
- {
- return FALSE;
- }
-
- if (ht->get(ht, k1) == NULL ||
- ht->get(ht, k3) == NULL)
- {
- return FALSE;
- }
-
- if (ht->get(ht, k2) != NULL)
- {
- return FALSE;
- }
-
- ht->put(ht, k2, v2);
-
- if (ht->get_count(ht) != 3)
- {
- return FALSE;
- }
-
- if (ht->get(ht, k1) == NULL ||
- ht->get(ht, k2) == NULL ||
- ht->get(ht, k3) == NULL)
- {
- return FALSE;
- }
-
- enumerator = ht->create_enumerator(ht);
- while (enumerator->enumerate(enumerator, &key, &value))
- {
- ht->remove_at(ht, enumerator);
- }
- enumerator->destroy(enumerator);
-
- if (ht->get_count(ht) != 0)
- {
- return FALSE;
- }
-
- if (ht->get(ht, k1) != NULL ||
- ht->get(ht, k2) != NULL ||
- ht->get(ht, k3) != NULL)
- {
- return FALSE;
- }
-
- ht->destroy(ht);
-
- return TRUE;
-}
diff --git a/src/libstrongswan/tests/Makefile.am b/src/libstrongswan/tests/Makefile.am
index e389bc3c4..45aab8736 100644
--- a/src/libstrongswan/tests/Makefile.am
+++ b/src/libstrongswan/tests/Makefile.am
@@ -5,7 +5,7 @@ check_PROGRAMS = $(TESTS)
test_runner_SOURCES = \
test_runner.c test_runner.h \
test_linked_list.c test_enumerator.c test_linked_list_enumerator.c \
- test_identification.c
+ test_hashtable.c test_identification.c
test_runner_CFLAGS = \
diff --git a/src/libstrongswan/tests/test_hashtable.c b/src/libstrongswan/tests/test_hashtable.c
new file mode 100644
index 000000000..802016f59
--- /dev/null
+++ b/src/libstrongswan/tests/test_hashtable.c
@@ -0,0 +1,295 @@
+/*
+ * Copyright (C) 2010-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.
+ */
+
+#include <check.h>
+
+#include <collections/hashtable.h>
+#include <utils/chunk.h>
+
+/*******************************************************************************
+ * string hash table functions
+ */
+
+static u_int hash(char *key)
+{
+ return chunk_hash(chunk_from_str(key));
+}
+
+static bool equals(char *key1, char *key2)
+{
+ return streq(key1, key2);
+}
+
+/*******************************************************************************
+ * test fixture
+ */
+
+static hashtable_t *ht;
+
+static void setup_ht()
+{
+ ht = hashtable_create((hashtable_hash_t)hash,
+ (hashtable_equals_t)equals, 0);
+ ck_assert_int_eq(ht->get_count(ht), 0);
+}
+
+static void teardown_ht()
+{
+ ht->destroy(ht);
+}
+
+/*******************************************************************************
+ * put/get
+ */
+
+START_TEST(test_put_get)
+{
+ char *k1 = "key1", *k2 = "key2", *k3 = "key3";
+ char *v1 = "val1", *v2 = "val2", *v3 = "val3", *value;
+
+ value = ht->put(ht, k1, v1);
+ ck_assert_int_eq(ht->get_count(ht), 1);
+ ck_assert(streq(ht->get(ht, k1), v1));
+ ck_assert(ht->get(ht, k2) == NULL);
+ ck_assert(ht->get(ht, k3) == NULL);
+ ck_assert(value == NULL);
+
+ ht->put(ht, k2, v2);
+ ht->put(ht, k3, v3);
+ ck_assert_int_eq(ht->get_count(ht), 3);
+ ck_assert(streq(ht->get(ht, k1), v1));
+ ck_assert(streq(ht->get(ht, k2), v2));
+ ck_assert(streq(ht->get(ht, k3), v3));
+
+ value = ht->put(ht, k2, v1);
+ ck_assert_int_eq(ht->get_count(ht), 3);
+ ck_assert(streq(value, v2));
+ ck_assert(streq(ht->get(ht, k2), v1));
+}
+END_TEST
+
+/*******************************************************************************
+ * get_match
+ */
+
+static u_int hash_match(char *key)
+{
+ return chunk_hash(chunk_create(key, 4));
+}
+
+static bool equal_match(char *key1, char *key2)
+{
+ if (!strneq(key1, key2, 4))
+ {
+ return FALSE;
+ }
+ /* look for an item with a key < than what we look for */
+ return strcmp(key1, key2) >= 0;
+}
+
+START_TEST(test_get_match)
+{
+ char *k1 = "key1_a", *k2 = "key2", *k3 = "key1_b", *k4 = "key1_c";
+ char *v1 = "val1", *v2 = "val2", *v3 = "val3", *value;
+
+ ht = hashtable_create((hashtable_hash_t)hash_match,
+ (hashtable_equals_t)equals, 0);
+
+ ht->put(ht, k1, v1);
+ ht->put(ht, k2, v2);
+ value = ht->put(ht, k3, v3);
+ ck_assert_int_eq(ht->get_count(ht), 3);
+ ck_assert(streq(ht->get(ht, k1), v1));
+ ck_assert(streq(ht->get(ht, k2), v2));
+ ck_assert(streq(ht->get(ht, k3), v3));
+ ck_assert(value == NULL);
+
+ value = ht->get_match(ht, k1, (hashtable_equals_t)equal_match);
+ ck_assert(value != NULL);
+ ck_assert(streq(value, v1));
+ value = ht->get_match(ht, k2, (hashtable_equals_t)equal_match);
+ ck_assert(value != NULL);
+ ck_assert(streq(value, v2));
+ value = ht->get_match(ht, k3, (hashtable_equals_t)equal_match);
+ ck_assert(value != NULL);
+ ck_assert(streq(value, v1));
+ value = ht->get_match(ht, k4, (hashtable_equals_t)equal_match);
+ ck_assert(value != NULL);
+ ck_assert(streq(value, v1));
+
+ ht->destroy(ht);
+}
+END_TEST
+
+/*******************************************************************************
+ * remove
+ */
+
+START_TEST(test_remove)
+{
+ char *k1 = "key1", *k2 = "key2", *k3 = "key3";
+ char *v1 = "val1", *v2 = "val2", *v3 = "val3", *value;
+
+ ht->put(ht, k1, v1);
+ ht->put(ht, k2, v2);
+ ht->put(ht, k3, v3);
+
+ value = ht->remove(ht, k2);
+ ck_assert_int_eq(ht->get_count(ht), 2);
+ ck_assert(streq(ht->get(ht, k1), v1));
+ ck_assert(streq(ht->get(ht, k3), v3));
+ ck_assert(streq(value, v2));
+ ck_assert(ht->get(ht, k2) == NULL);
+
+ value = ht->remove(ht, k2);
+ ck_assert_int_eq(ht->get_count(ht), 2);
+ ck_assert(value == NULL);
+
+ value = ht->remove(ht, k1);
+ value = ht->remove(ht, k3);
+ ck_assert_int_eq(ht->get_count(ht), 0);
+ ck_assert(ht->get(ht, k1) == NULL);
+ ck_assert(ht->get(ht, k2) == NULL);
+ ck_assert(ht->get(ht, k3) == NULL);
+}
+END_TEST
+
+/*******************************************************************************
+ * enumerator
+ */
+
+START_TEST(test_enumerator)
+{
+ char *k1 = "key1", *k2 = "key2", *k3 = "key3", *key;
+ char *v1 = "val1", *v2 = "val2", *v3 = "val3", *value;
+ enumerator_t *enumerator;
+ int count;
+
+ ht->put(ht, k1, v1);
+ ht->put(ht, k2, v2);
+ ht->put(ht, k3, v3);
+
+ count = 0;
+ enumerator = ht->create_enumerator(ht);
+ while (enumerator->enumerate(enumerator, &key, &value))
+ {
+ ck_assert(streq(key, k1) || streq(key, k2) || streq(key, k3));
+ ck_assert(streq(value, v1) || streq(value, v2) || streq(value, v3));
+ ck_assert(!streq(key, k1) || streq(value, v1));
+ ck_assert(!streq(key, k2) || streq(value, v2));
+ ck_assert(!streq(key, k3) || streq(value, v3));
+ count++;
+ }
+ enumerator->destroy(enumerator);
+ ck_assert_int_eq(count, 3);
+
+ value = ht->remove(ht, k1);
+ value = ht->remove(ht, k2);
+ value = ht->remove(ht, k3);
+
+ count = 0;
+ enumerator = ht->create_enumerator(ht);
+ while (enumerator->enumerate(enumerator, &key, &value))
+ {
+ count++;
+ }
+ enumerator->destroy(enumerator);
+ ck_assert_int_eq(count, 0);
+}
+END_TEST
+
+/*******************************************************************************
+ * remove_at
+ */
+
+START_TEST(test_remove_at)
+{
+ char *k1 = "key1", *k2 = "key2", *k3 = "key3", *key;
+ char *v1 = "val1", *v2 = "val2", *v3 = "val3", *value;
+ enumerator_t *enumerator;
+
+ ht->put(ht, k1, v1);
+ ht->put(ht, k2, v2);
+ ht->put(ht, k3, v3);
+
+ enumerator = ht->create_enumerator(ht);
+ while (enumerator->enumerate(enumerator, &key, &value))
+ {
+ if (streq(key, k2))
+ {
+ ht->remove_at(ht, enumerator);
+ }
+ }
+ enumerator->destroy(enumerator);
+
+ ck_assert_int_eq(ht->get_count(ht), 2);
+ ck_assert(ht->get(ht, k1) != NULL);
+ ck_assert(ht->get(ht, k3) != NULL);
+ ck_assert(ht->get(ht, k2) == NULL);
+
+ ht->put(ht, k2, v2);
+
+ ck_assert_int_eq(ht->get_count(ht), 3);
+ ck_assert(ht->get(ht, k1) != NULL);
+ ck_assert(ht->get(ht, k2) != NULL);
+ ck_assert(ht->get(ht, k3) != NULL);
+
+ enumerator = ht->create_enumerator(ht);
+ while (enumerator->enumerate(enumerator, &key, &value))
+ {
+ ht->remove_at(ht, enumerator);
+ }
+ enumerator->destroy(enumerator);
+
+ ck_assert_int_eq(ht->get_count(ht), 0);
+ ck_assert(ht->get(ht, k1) == NULL);
+ ck_assert(ht->get(ht, k2) == NULL);
+ ck_assert(ht->get(ht, k3) == NULL);
+}
+END_TEST
+
+Suite *hashtable_suite_create()
+{
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("hashtable");
+
+ tc = tcase_create("put/get");
+ tcase_add_checked_fixture(tc, setup_ht, teardown_ht);
+ tcase_add_test(tc, test_put_get);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("get_match");
+ tcase_add_test(tc, test_get_match);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("remove");
+ tcase_add_checked_fixture(tc, setup_ht, teardown_ht);
+ tcase_add_test(tc, test_remove);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("enumerator");
+ tcase_add_checked_fixture(tc, setup_ht, teardown_ht);
+ tcase_add_test(tc, test_enumerator);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("remove_at");
+ tcase_add_checked_fixture(tc, setup_ht, teardown_ht);
+ tcase_add_test(tc, test_remove_at);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
diff --git a/src/libstrongswan/tests/test_runner.c b/src/libstrongswan/tests/test_runner.c
index 7d5c50288..114ba39e1 100644
--- a/src/libstrongswan/tests/test_runner.c
+++ b/src/libstrongswan/tests/test_runner.c
@@ -35,6 +35,7 @@ int main()
srunner_add_suite(sr, enumerator_suite_create());
srunner_add_suite(sr, linked_list_suite_create());
srunner_add_suite(sr, linked_list_enumerator_suite_create());
+ srunner_add_suite(sr, hashtable_suite_create());
srunner_add_suite(sr, identification_suite_create());
srunner_run_all(sr, CK_NORMAL);
diff --git a/src/libstrongswan/tests/test_runner.h b/src/libstrongswan/tests/test_runner.h
index 71deba127..490bf54f8 100644
--- a/src/libstrongswan/tests/test_runner.h
+++ b/src/libstrongswan/tests/test_runner.h
@@ -21,6 +21,7 @@
Suite *enumerator_suite_create();
Suite *linked_list_suite_create();
Suite *linked_list_enumerator_suite_create();
+Suite *hashtable_suite_create();
Suite *identification_suite_create();
#endif /** TEST_RUNNER_H_ */