]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Add mock kernel_ipsec_t implementation for unit tests
authorTobias Brunner <tobias@strongswan.org>
Thu, 12 May 2016 14:14:03 +0000 (16:14 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 17 Jun 2016 16:48:01 +0000 (18:48 +0200)
Provides predictable sequential SPIs.

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

index 7c98c2119fec2324f77aaa59c62c34dc42839128..0d2dd4b00e64d9bdde8ee67ad47a839a4051f699 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_ipsec.h utils/mock_ipsec.c \
   utils/mock_sender.h utils/mock_sender.c \
   libcharon_tests.h libcharon_tests.c
 
diff --git a/src/libcharon/tests/utils/mock_ipsec.c b/src/libcharon/tests/utils/mock_ipsec.c
new file mode 100644 (file)
index 0000000..d57a26a
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2016 Tobias Brunner
+ * Copyright (C) 2008 Martin Willi
+ * 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_ipsec.h"
+
+typedef struct private_kernel_ipsec_t private_kernel_ipsec_t;
+
+/**
+ * Private data
+ */
+struct private_kernel_ipsec_t {
+
+       /**
+        * Public interface
+        */
+       kernel_ipsec_t public;
+
+       /**
+        * Allocated SPI
+        */
+       refcount_t spi;
+};
+
+METHOD(kernel_ipsec_t, get_spi, status_t,
+       private_kernel_ipsec_t *this, host_t *src, host_t *dst, uint8_t protocol,
+       uint32_t *spi)
+{
+       *spi = (uint32_t)ref_get(&this->spi);
+       return SUCCESS;
+}
+
+METHOD(kernel_ipsec_t, get_cpi, status_t,
+       private_kernel_ipsec_t *this, host_t *src, host_t *dst, uint16_t *cpi)
+{
+       return FAILED;
+}
+
+METHOD(kernel_ipsec_t, add_sa, status_t,
+       private_kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
+       kernel_ipsec_add_sa_t *data)
+{
+       return SUCCESS;
+}
+
+METHOD(kernel_ipsec_t, update_sa, status_t,
+       private_kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
+       kernel_ipsec_update_sa_t *data)
+{
+       return SUCCESS;
+}
+
+METHOD(kernel_ipsec_t, query_sa, status_t,
+       private_kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
+       kernel_ipsec_query_sa_t *data, uint64_t *bytes, uint64_t *packets,
+       time_t *time)
+{
+       return NOT_SUPPORTED;
+}
+
+METHOD(kernel_ipsec_t, del_sa, status_t,
+       private_kernel_ipsec_t *this, kernel_ipsec_sa_id_t *id,
+       kernel_ipsec_del_sa_t *data)
+{
+       return SUCCESS;
+}
+
+METHOD(kernel_ipsec_t, add_policy, status_t,
+       private_kernel_ipsec_t *this, kernel_ipsec_policy_id_t *id,
+       kernel_ipsec_manage_policy_t *data)
+{
+       return SUCCESS;
+}
+
+METHOD(kernel_ipsec_t, query_policy, status_t,
+       private_kernel_ipsec_t *this, kernel_ipsec_policy_id_t *id,
+       kernel_ipsec_query_policy_t *data, time_t *use_time)
+{
+       *use_time = 1;
+       return SUCCESS;
+}
+
+METHOD(kernel_ipsec_t, del_policy, status_t,
+       private_kernel_ipsec_t *this, kernel_ipsec_policy_id_t *id,
+       kernel_ipsec_manage_policy_t *data)
+{
+       return SUCCESS;
+}
+
+/*
+ * Described in header
+ */
+kernel_ipsec_t *mock_ipsec_create()
+{
+       private_kernel_ipsec_t *this;
+
+       INIT(this,
+               .public = {
+                       .get_spi = _get_spi,
+                       .get_cpi = _get_cpi,
+                       .add_sa = _add_sa,
+                       .update_sa = _update_sa,
+                       .query_sa = _query_sa,
+                       .del_sa = _del_sa,
+                       .flush_sas = (void*)return_failed,
+                       .add_policy = _add_policy,
+                       .query_policy = _query_policy,
+                       .del_policy = _del_policy,
+                       .flush_policies = (void*)return_failed,
+                       .bypass_socket = (void*)return_true,
+                       .enable_udp_decap = (void*)return_true,
+                       .destroy = (void*)free,
+               },
+       );
+       return &this->public;
+}
diff --git a/src/libcharon/tests/utils/mock_ipsec.h b/src/libcharon/tests/utils/mock_ipsec.h
new file mode 100644 (file)
index 0000000..cbf2152
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+
+/**
+ * kernel_ipsec_t implementation used for exchange unit tests.  Currently
+ * returns sequential SPIs, all other methods are noops.
+ *
+ * @defgroup mock_ipsec mock_ipsec
+ * @{ @ingroup test_utils_c
+ */
+
+#ifndef MOCK_IPSEC_H_
+#define MOCK_IPSEC_H_
+
+#include <kernel/kernel_ipsec.h>
+
+/**
+ * Create an instance of kernel_ipsec_t
+ *
+ * @return             created object
+ */
+kernel_ipsec_t *mock_ipsec_create();
+
+#endif /** MOCK_IPSEC_H_ @}*/