]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Add mock sender_t implementation for unit testing
authorTobias Brunner <tobias@strongswan.org>
Thu, 12 May 2016 13:33:44 +0000 (15:33 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 17 Jun 2016 16:48:01 +0000 (18:48 +0200)
This allows to retrieve packets sent by an IKE_SA and pass it to another
IKE_SA directly via process_message().

src/libcharon/tests/Makefile.am
src/libcharon/tests/libcharon_tests.h
src/libcharon/tests/utils/mock_sender.c [new file with mode: 0644]
src/libcharon/tests/utils/mock_sender.h [new file with mode: 0644]

index 0589269aa985db620c70c235b65a132cc2188319..7c98c2119fec2324f77aaa59c62c34dc42839128 100644 (file)
@@ -6,6 +6,7 @@ libcharon_tests_SOURCES = \
   suites/test_ike_cfg.c \
   suites/test_mem_pool.c \
   suites/test_message_chapoly.c \
+  utils/mock_sender.h utils/mock_sender.c \
   libcharon_tests.h libcharon_tests.c
 
 libcharon_tests_CFLAGS = \
index fb82baccb9b7abeaca71f86a7a3beb29b0360c2d..d17ea041d8eb7bc616d3f4bf7e0f6554a6353ea5 100644 (file)
@@ -1,4 +1,7 @@
 /*
+ * Copyright (C) 2014-2016 Tobias Brunner
+ * HSR Hochschule fuer Technik Rapperswil
+ *
  * Copyright (C) 2014 Martin Willi
  * Copyright (C) 2014 revosec AG
  *
  * for more details.
  */
 
+/**
+ * @defgroup libcharon-tests tests
+ * @ingroup libcharon
+ *
+ * @defgroup test_utils_c test_utils
+ * @ingroup libcharon-tests
+ */
+
 TEST_SUITE(ike_cfg_suite_create)
 TEST_SUITE(mem_pool_suite_create)
 TEST_SUITE_DEPEND(message_chapoly_suite_create, AEAD, ENCR_CHACHA20_POLY1305, 32)
diff --git a/src/libcharon/tests/utils/mock_sender.c b/src/libcharon/tests/utils/mock_sender.c
new file mode 100644 (file)
index 0000000..c090ff4
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * 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 "mock_sender.h"
+
+#include <collections/linked_list.h>
+
+typedef struct private_mock_sender_t private_mock_sender_t;
+
+/**
+ * Private data
+ */
+struct private_mock_sender_t {
+
+       /**
+        * Public interface
+        */
+       mock_sender_t public;
+
+       /**
+        * Packet queue, as message_t*
+        */
+       linked_list_t *queue;
+};
+
+
+METHOD(sender_t, send_, void,
+       private_mock_sender_t *this, packet_t *packet)
+{
+       message_t *message;
+
+       message = message_create_from_packet(packet);
+       message->parse_header(message);
+       this->queue->insert_last(this->queue, message);
+}
+
+METHOD(mock_sender_t, dequeue, message_t*,
+       private_mock_sender_t *this)
+{
+       message_t *message = NULL;
+
+       this->queue->remove_first(this->queue, (void**)&message);
+       return message;
+}
+
+METHOD(sender_t, destroy, void,
+       private_mock_sender_t *this)
+{
+       this->queue->destroy_offset(this->queue, offsetof(message_t, destroy));
+       free(this);
+}
+
+/*
+ * Described in header
+ */
+mock_sender_t *mock_sender_create()
+{
+       private_mock_sender_t *this;
+
+       INIT(this,
+               .public = {
+                       .interface = {
+                               .send = _send_,
+                               .send_no_marker = (void*)nop,
+                               .flush = (void*)nop,
+                               .destroy = _destroy,
+                       },
+                       .dequeue = _dequeue,
+               },
+               .queue = linked_list_create(),
+       );
+       return &this->public;
+}
diff --git a/src/libcharon/tests/utils/mock_sender.h b/src/libcharon/tests/utils/mock_sender.h
new file mode 100644 (file)
index 0000000..5eabdda
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+
+/**
+ * sender_t implementation that does not pass the sent packet to a socket but
+ * instead provides it for immediate delivery to an ike_sa_t object.
+ *
+ * @defgroup mock_sender mock_sender
+ * @{ @ingroup test_utils_c
+ */
+
+#ifndef MOCK_SENDER_H_
+#define MOCK_SENDER_H_
+
+#include <encoding/message.h>
+#include <network/sender.h>
+
+typedef struct mock_sender_t mock_sender_t;
+
+struct mock_sender_t {
+
+       /**
+        * Implemented interface
+        */
+       sender_t interface;
+
+       /**
+        * Remove the next packet in the send queue as message_t object.  The IKE
+        * header is already parsed (which is assumed does not fail) so it can
+        * directly be passed to ike_sa_t::process_message().
+        *
+        * @return              message or NULL if none is queued
+        */
+       message_t *(*dequeue)(mock_sender_t *this);
+};
+
+/**
+ * Creates a mock_sender_t instance.
+ *
+ * @return                     created object
+ */
+mock_sender_t *mock_sender_create();
+
+#endif /** MOCK_SENDER_H_ @} */