From: Tobias Brunner Date: Tue, 17 May 2016 11:49:58 +0000 (+0200) Subject: unit-tests: Make message asserts more flexible X-Git-Tag: 5.5.0dr1~4^2~68 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbb0b3cb0ace2bf96ea9d46deae123fe2ac27ed7;p=thirdparty%2Fstrongswan.git unit-tests: Make message asserts more flexible --- diff --git a/src/libcharon/tests/utils/exchange_test_asserts.c b/src/libcharon/tests/utils/exchange_test_asserts.c index 4fd5df52a5..d95af5993d 100644 --- a/src/libcharon/tests/utils/exchange_test_asserts.c +++ b/src/libcharon/tests/utils/exchange_test_asserts.c @@ -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; } diff --git a/src/libcharon/tests/utils/exchange_test_asserts.h b/src/libcharon/tests/utils/exchange_test_asserts.h index cdee76b0a6..5c91ae16be 100644 --- a/src/libcharon/tests/utils/exchange_test_asserts.h +++ b/src/libcharon/tests/utils/exchange_test_asserts.h @@ -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); \ })