]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Add asserts against ike|child_rekey hooks
authorTobias Brunner <tobias@strongswan.org>
Wed, 18 May 2016 15:16:29 +0000 (17:16 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 17 Jun 2016 16:48:02 +0000 (18:48 +0200)
src/libcharon/tests/utils/exchange_test_asserts.c
src/libcharon/tests/utils/exchange_test_asserts.h

index d95af5993d778c692453be781a1ce85e326d39b0..2602b97b74adc5fda4c04808ed5ed8f080526328 100644 (file)
@@ -13,6 +13,8 @@
  * for more details.
  */
 
+#include <inttypes.h>
+
 #include <test_suite.h>
 
 #include "exchange_test_asserts.h"
@@ -56,6 +58,51 @@ bool exchange_test_asserts_child_updown(listener_t *listener, ike_sa_t *ike_sa,
        return TRUE;
 }
 
+/*
+ * Described in header
+ */
+bool exchange_test_asserts_ike_rekey(listener_t *listener, ike_sa_t *old,
+                                                                        ike_sa_t *new)
+{
+       listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
+       ike_sa_id_t *id;
+       uint64_t spi;
+
+       this->count++;
+       id = old->get_id(old);
+       spi = id->get_initiator_spi(id);
+       assert_listener_msg(this->spi_old == spi, this, "unexpected old IKE_SA "
+                                               "%.16"PRIx64"_i instead of %.16"PRIx64"_i",
+                                               be64toh(spi), be64toh(this->spi_old));
+       id = new->get_id(new);
+       spi = id->get_initiator_spi(id);
+       assert_listener_msg(this->spi_new == spi, this, "unexpected new IKE_SA "
+                                               "%.16"PRIx64"_i instead of %.16"PRIx64"_i",
+                                               be64toh(spi), be64toh(this->spi_new));
+       return TRUE;
+}
+
+/*
+ * Described in header
+ */
+bool exchange_test_asserts_child_rekey(listener_t *listener, ike_sa_t *ike_sa,
+                                                                          child_sa_t *old, child_sa_t *new)
+{
+       listener_hook_assert_t *this = (listener_hook_assert_t*)listener;
+       uint32_t spi, expected;
+
+       this->count++;
+       spi = old->get_spi(old, TRUE);
+       expected = this->spi_old;
+       assert_listener_msg(expected == spi, this, "unexpected old CHILD_SA %.8x "
+                                               "instead of %.8x", spi, expected);
+       spi = new->get_spi(new, TRUE);
+       expected = this->spi_new;
+       assert_listener_msg(expected == spi, this, "unexpected new CHILD_SA %.8x "
+                                               "instead of %.8x", spi, expected);
+       return TRUE;
+}
+
 /**
  * Assert a given message rule
  */
index 5c91ae16be7bbdfc3812254e138e7a262a508869..906760c8b761021ac9e27c0726ebd3ecbb37f1a8 100644 (file)
@@ -65,6 +65,11 @@ struct listener_hook_assert_t {
         * Expected updown result
         */
        bool up;
+
+       /**
+        * Initiator/Inbound SPIs to expect in rekey event
+        */
+       uint64_t spi_old, spi_new;
 };
 
 /**
@@ -84,6 +89,18 @@ bool exchange_test_asserts_ike_updown(listener_t *this, ike_sa_t *ike_sa,
 bool exchange_test_asserts_child_updown(listener_t *this, ike_sa_t *ike_sa,
                                                                                child_sa_t *child_sa, bool up);
 
+/**
+ * Implementation of listener_t::ike_rekey.
+ */
+bool exchange_test_asserts_ike_rekey(listener_t *this, ike_sa_t *old,
+                                                                        ike_sa_t *new);
+
+/**
+ * Implementation of listener_t::child_rekey.
+ */
+bool exchange_test_asserts_child_rekey(listener_t *this, ike_sa_t *ike_sa,
+                                                                          child_sa_t *old, child_sa_t *new);
+
 /**
  * Check if a statement evaluates to TRUE, use original source file and line
  * in the error message if not.
@@ -135,6 +152,24 @@ bool exchange_test_asserts_child_updown(listener_t *this, ike_sa_t *ike_sa,
                .up = e, \
        )
 
+/**
+ * Initialize an assertion that enforces that the given rekey hook was called
+ * with the SAs with the matching initiator/inbound SPIs.
+ * Must be matched by a call to assert_hook().
+ *
+ * @param name         name of the hook
+ * @param old          SPI of the old SA
+ * @param new          SPI of the new SA
+ */
+#define assert_hook_rekey(name, old, new) \
+       _assert_hook_init(name, \
+               streq(#name, "ike_rekey") ? (void*)exchange_test_asserts_ike_rekey \
+                                                                  : (void*)exchange_test_asserts_child_rekey, \
+               .expected = 1, \
+               .spi_old = old, \
+               .spi_new = new, \
+       )
+
 /**
  * Initialize assertions against invocations of listener_t hooks.  Each call
  * must be matched by a call to assert_hook().