]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Make message asserts more flexible
authorTobias Brunner <tobias@strongswan.org>
Tue, 17 May 2016 11:49:58 +0000 (13:49 +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 4fd5df52a5c94a1196812cd7d7dad2979fccf58f..d95af5993d778c692453be781a1ce85e326d39b0 100644 (file)
@@ -56,6 +56,46 @@ bool exchange_test_asserts_child_updown(listener_t *listener, ike_sa_t *ike_sa,
        return TRUE;
 }
 
+/**
+ * Assert a given message rule
+ */
+static void assert_message_rule(listener_message_assert_t *this, message_t *msg,
+                                                               listener_message_rule_t *rule)
+{
+       if (rule->expected)
+       {
+               if (rule->payload)
+               {
+                       assert_listener_msg(msg->get_payload(msg, rule->payload),
+                                                               this, "expected payload (%N) not found",
+                                                               payload_type_names, rule->payload);
+
+               }
+               if (rule->notify)
+               {
+                       assert_listener_msg(msg->get_notify(msg, rule->notify),
+                                                               this, "expected notify payload (%N) not found",
+                                                               notify_type_names, rule->notify);
+               }
+       }
+       else
+       {
+               if (rule->payload)
+               {
+                       assert_listener_msg(!msg->get_payload(msg, rule->payload),
+                                                               this, "unexpected payload (%N) found",
+                                                               payload_type_names, rule->payload);
+
+               }
+               if (rule->notify)
+               {
+                       assert_listener_msg(!msg->get_notify(msg, rule->notify),
+                                                               this, "unexpected notify payload (%N) found",
+                                                               notify_type_names, rule->notify);
+               }
+       }
+}
+
 /*
  * Described in header
  */
@@ -80,17 +120,14 @@ bool exchange_test_asserts_message(listener_t *listener, ike_sa_t *ike_sa,
                                                                "count in message (%d != %d)", this->count,
                                                                count);
                }
-               if (this->payload)
+               if (this->num_rules)
                {
-                       assert_listener_msg(message->get_payload(message, this->payload),
-                                                               this, "expected payload (%N) not found",
-                                                               payload_type_names, this->payload);
-               }
-               if (this->notify)
-               {
-                       assert_listener_msg(message->get_notify(message, this->notify),
-                                                               this, "expected notify payload (%N) not found",
-                                                               notify_type_names, this->notify);
+                       int i;
+
+                       for (i = 0; i < this->num_rules; i++)
+                       {
+                               assert_message_rule(this, message, &this->rules[i]);
+                       }
                }
                return FALSE;
        }
index cdee76b0a6c36006fdcf7d728b423b5cb5d3b52a..5c91ae16be7bbdfc3812254e138e7a262a508869 100644 (file)
@@ -27,6 +27,7 @@
 
 typedef struct listener_hook_assert_t listener_hook_assert_t;
 typedef struct listener_message_assert_t listener_message_assert_t;
+typedef struct listener_message_rule_t listener_message_rule_t;
 
 struct listener_hook_assert_t {
 
@@ -171,6 +172,29 @@ do { \
        } \
 } while(FALSE)
 
+/**
+ * Rules regarding payloads/notifies to expect/not expect in a message
+ */
+struct listener_message_rule_t {
+
+       /**
+        * Whether the payload/notify is expected in the message, FALSE to fail if
+        * it is found
+        */
+       bool expected;
+
+       /**
+        * Payload type to expect/not expect
+        */
+       payload_type_t payload;
+
+       /**
+        * Notify type to expect/not expect (paylod type does not have to be
+        * specified)
+        */
+       notify_type_t notify;
+};
+
 /**
  * Data used to check plaintext messages via listener_t
  */
@@ -197,19 +221,19 @@ struct listener_message_assert_t {
        bool incoming;
 
        /**
-        * Payload count to expect
+        * Payload count to expect (-1 to ignore the count)
         */
        int count;
 
        /**
-        * Payload type to look for
+        * Payloads to expect or not expect in a message
         */
-       payload_type_t payload;
+       listener_message_rule_t *rules;
 
        /**
-        * Notify type to look for
+        * Number of rules
         */
-       notify_type_t notify;
+       int num_rules;
 };
 
 /**
@@ -224,9 +248,8 @@ bool exchange_test_asserts_message(listener_t *this, ike_sa_t *ike_sa,
  *
  * @param dir                  IN or OUT to check the next in- or outbound message
  */
-#define assert_message_empty(dir) ({ \
-       _assert_payload(dir, 0, 0, 0); \
-})
+#define assert_message_empty(dir) \
+                               _assert_payload(dir, 0)
 
 /**
  * Assert that the next in- or outbound plaintext message contains exactly
@@ -235,9 +258,8 @@ bool exchange_test_asserts_message(listener_t *this, ike_sa_t *ike_sa,
  * @param dir                  IN or OUT to check the next in- or outbound message
  * @param expected             expected payload type
  */
-#define assert_single_payload(dir, expected) ({ \
-       _assert_payload(dir, 1, expected, 0); \
-})
+#define assert_single_payload(dir, expected) \
+                               _assert_payload(dir, 1, { TRUE, expected, 0 })
 
 /**
  * Assert that the next in- or outbound plaintext message contains exactly
@@ -246,18 +268,39 @@ bool exchange_test_asserts_message(listener_t *this, ike_sa_t *ike_sa,
  * @param dir                  IN or OUT to check the next in- or outbound message
  * @param expected             expected notify type
  */
-#define assert_single_notify(dir, expected) ({ \
-       _assert_payload(dir, 1, 0, expected); \
-})
+#define assert_single_notify(dir, expected) \
+                               _assert_payload(dir, 1, { TRUE, 0, expected })
+
+/**
+ * Assert that the next in- or outbound plaintext message contains a notify
+ * of the given type.
+ *
+ * @param dir                  IN or OUT to check the next in- or outbound message
+ * @param expected             expected notify type
+ */
+#define assert_notify(dir, expected) \
+                               _assert_payload(dir, -1, { TRUE, 0, expected })
+
+/**
+ * Assert that the next in- or outbound plaintext message does not contain a
+ * notify of the given type.
+ *
+ * @param dir                  IN or OUT to check the next in- or outbound message
+ * @param unexpected   not expected notify type
+ */
+#define assert_no_notify(dir, unexpected) \
+                               _assert_payload(dir, -1, { FALSE, 0, unexpected })
 
-#define _assert_payload(dir, c, p, n) ({ \
+#define _assert_payload(dir, c, ...) ({ \
+       listener_message_rule_t _rules[] = { __VA_ARGS__ }; \
        listener_message_assert_t _listener = { \
                .listener = { .message = exchange_test_asserts_message, }, \
                .file = __FILE__, \
                .line = __LINE__, \
                .incoming = streq(#dir, "IN") ? TRUE : FALSE, \
                .count = c, \
-               .payload = p, \
+               .rules = _rules, \
+               .num_rules = countof(_rules), \
        }; \
        charon->bus->add_listener(charon->bus, &_listener.listener); \
 })