unit-tests: Add tests for IKE SA deletion
authorTobias Brunner <tobias@strongswan.org>
Mon, 30 May 2016 14:30:56 +0000 (16:30 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 17 Jun 2016 16:48:05 +0000 (18:48 +0200)
src/libcharon/tests/Makefile.am
src/libcharon/tests/exchange_tests.h
src/libcharon/tests/suites/test_ike_delete.c [new file with mode: 0644]

index 4e2bdfd..222d23f 100644 (file)
@@ -26,6 +26,7 @@ libcharon_tests_LDADD = \
 exchange_tests_SOURCES = \
   suites/test_child_delete.c \
   suites/test_child_rekey.c \
+  suites/test_ike_delete.c \
   suites/test_ike_rekey.c \
   utils/exchange_test_asserts.h utils/exchange_test_asserts.c \
   utils/exchange_test_helper.h utils/exchange_test_helper.c \
index 0d0c192..2e322b6 100644 (file)
@@ -13,6 +13,7 @@
  * for more details.
  */
 
+TEST_SUITE(ike_delete_suite_create)
 TEST_SUITE(ike_rekey_suite_create)
 TEST_SUITE(child_delete_suite_create)
 TEST_SUITE(child_rekey_suite_create)
diff --git a/src/libcharon/tests/suites/test_ike_delete.c b/src/libcharon/tests/suites/test_ike_delete.c
new file mode 100644 (file)
index 0000000..d79f9bc
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2016 Tobias Brunner
+ * HSR 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 "test_suite.h"
+
+#include <tests/utils/exchange_test_helper.h>
+#include <tests/utils/exchange_test_asserts.h>
+#include <tests/utils/sa_asserts.h>
+
+/**
+ * Regular IKE_SA delete either initiated by the original initiator or
+ * responder of the IKE_SA.
+ */
+START_TEST(test_regular)
+{
+       ike_sa_t *a, *b;
+       status_t s;
+
+       if (_i)
+       {       /* responder deletes the IKE_SA */
+               exchange_test_helper->establish_sa(exchange_test_helper,
+                                                                                  &b, &a, NULL);
+       }
+       else
+       {       /* initiator deletes the IKE_SA */
+               exchange_test_helper->establish_sa(exchange_test_helper,
+                                                                                  &a, &b, NULL);
+       }
+       assert_hook_not_called(ike_updown);
+       assert_hook_not_called(child_updown);
+       call_ikesa(a, delete);
+       assert_ike_sa_state(a, IKE_DELETING);
+       assert_hook();
+       assert_hook();
+
+       /* INFORMATIONAL { D } --> */
+       assert_hook_updown(ike_updown, FALSE);
+       assert_hook_updown(child_updown, FALSE);
+       assert_single_payload(IN, PLV2_DELETE);
+       s = exchange_test_helper->process_message(exchange_test_helper, b, NULL);
+       ck_assert_int_eq(DESTROY_ME, s);
+       call_ikesa(b, destroy);
+       assert_hook();
+       assert_hook();
+
+       /* <-- INFORMATIONAL { } */
+       assert_hook_updown(ike_updown, FALSE);
+       assert_hook_updown(child_updown, FALSE);
+       assert_message_empty(IN);
+       s = exchange_test_helper->process_message(exchange_test_helper, a, NULL);
+       ck_assert_int_eq(DESTROY_ME, s);
+       call_ikesa(a, destroy);
+       assert_hook();
+       assert_hook();
+}
+END_TEST
+
+/**
+ * Both peers initiate the IKE_SA deletion concurrently and should handle the
+ * collision properly.
+ */
+START_TEST(test_collision)
+{
+       ike_sa_t *a, *b;
+       status_t s;
+
+       exchange_test_helper->establish_sa(exchange_test_helper,
+                                                                          &a, &b, NULL);
+
+       assert_hook_not_called(ike_updown);
+       assert_hook_not_called(child_updown);
+       call_ikesa(a, delete);
+       assert_ike_sa_state(a, IKE_DELETING);
+       call_ikesa(b, delete);
+       assert_ike_sa_state(b, IKE_DELETING);
+       assert_hook();
+       assert_hook();
+
+       /* RFC 7296 says:  If a peer receives a request to close an IKE SA that it
+        * is currently trying to close, it SHOULD reply as usual, and forget about
+        * its own close request.
+        * So we expect the SA to just get closed with an empty response still sent.
+        */
+
+       /* INFORMATIONAL { D } --> */
+       assert_hook_updown(ike_updown, FALSE);
+       assert_hook_updown(child_updown, FALSE);
+       assert_single_payload(IN, PLV2_DELETE);
+       assert_message_empty(OUT);
+       s = exchange_test_helper->process_message(exchange_test_helper, b, NULL);
+       ck_assert_int_eq(DESTROY_ME, s);
+       call_ikesa(b, destroy);
+       assert_hook();
+       assert_hook();
+
+       /* <-- INFORMATIONAL { D } */
+       assert_hook_updown(ike_updown, FALSE);
+       assert_hook_updown(child_updown, FALSE);
+       assert_single_payload(IN, PLV2_DELETE);
+       assert_message_empty(OUT);
+       s = exchange_test_helper->process_message(exchange_test_helper, a, NULL);
+       ck_assert_int_eq(DESTROY_ME, s);
+       call_ikesa(a, destroy);
+       assert_hook();
+       assert_hook();
+}
+END_TEST
+
+Suite *ike_delete_suite_create()
+{
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("ike delete");
+
+       tc = tcase_create("regular");
+       tcase_add_loop_test(tc, test_regular, 0, 2);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("collisions");
+       tcase_add_test(tc, test_collision);
+       suite_add_tcase(s, tc);
+
+       return s;
+}