unit-tests: Add helper to create but not yet establish two IKE_SAs
[strongswan.git] / src / libcharon / tests / utils / exchange_test_helper.h
1 /*
2 * Copyright (C) 2016 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * This class and singleton object initializes charon and provides helper
18 * methods to create unit tests for IKEv2 exchanges.
19 *
20 * It also registers special implementations for the kernel_ipsec_t interface,
21 * the sender and provides dummy configs and credentials.
22 *
23 * @defgroup exchange_test_helper exchange_test_helper
24 * @{ @ingroup test_utils_c
25 */
26
27 #ifndef EXCHANGE_TEST_HELPER_H_
28 #define EXCHANGE_TEST_HELPER_H_
29
30 #include <daemon.h>
31
32 #include "mock_sender.h"
33
34 typedef struct exchange_test_helper_t exchange_test_helper_t;
35 typedef struct exchange_test_sa_conf_t exchange_test_sa_conf_t;
36
37 struct exchange_test_helper_t {
38
39 /**
40 * Sender instance used during tests
41 */
42 mock_sender_t *sender;
43
44 /**
45 * Set the initial byte of all nonces generated by future nonce
46 * generators (already instatiated nonce generators are not affected).
47 */
48 u_char nonce_first_byte;
49
50 /**
51 * Creates an established IKE_SA/CHILD_SA
52 *
53 * @param[out] init IKE_SA of the initiator
54 * @param[out] resp IKE_SA of the responder
55 * @param conf configuration for SAs
56 */
57 void (*establish_sa)(exchange_test_helper_t *this, ike_sa_t **init,
58 ike_sa_t **resp, exchange_test_sa_conf_t *conf);
59
60 /**
61 * Similar to establish_sa() but does only create the SA and config
62 * objects, no exchanges are initiated/handled. The returned child_cfg
63 * object is that created for the initiator to be used for a call to
64 * initiate(). The config objects for the responder are managed and
65 * provided by an internal config backend.
66 *
67 * Note that the responder SPIs are not yet set.
68 *
69 * @param[out] init IKE_SA of the initiator
70 * @param[out] resp IKE_SA of the responder
71 * @param conf configuration for SAs
72 * @return child_cfg for the initiator
73 */
74 child_cfg_t *(*create_sa)(exchange_test_helper_t *this, ike_sa_t **init,
75 ike_sa_t **resp, exchange_test_sa_conf_t *conf);
76
77 /**
78 * Pass a message to the given IKE_SA for processing, setting the IKE_SA on
79 * the bus while processing the message.
80 *
81 * @param ike_sa the IKE_SA receiving the message
82 * @param message the message, or NULL to pass the next message in the
83 * send queue (adopted)
84 * @return return value from ike_sa_t::process_message()
85 */
86 status_t (*process_message)(exchange_test_helper_t *this, ike_sa_t *sa,
87 message_t *message);
88
89 /**
90 * Register a listener with the bus.
91 *
92 * Don't use bus_t::add_listener() directly for listeners on the stack
93 * as that could lead to invalid listeners registered when hooks are
94 * triggered during cleanup if a test case fails. All of the listeners
95 * added this way are unregistered with the bus before cleaning up.
96 *
97 * @param listener listener to add to the bus
98 */
99 void (*add_listener)(exchange_test_helper_t *this, listener_t *listener);
100 };
101
102 struct exchange_test_sa_conf_t {
103
104 /**
105 * Configuration for initiator and responder
106 */
107 struct {
108 /** IKE proposal */
109 char *ike;
110 /** ESP proposal */
111 char *esp;
112 } initiator, responder;
113 };
114
115 /**
116 * Since we don't use the IKE_SA manager to checkout SAs use this to call a
117 * method on the given IKE_SA in its context.
118 */
119 #define call_ikesa(sa, method, ...) ({ \
120 charon->bus->set_sa(charon->bus, sa); \
121 sa->method(sa, ##__VA_ARGS__); \
122 charon->bus->set_sa(charon->bus, NULL); \
123 })
124
125 /**
126 * The one and only instance of the helper object.
127 *
128 * Set between exchange_test_helper_setup() and exchange_test_helper_teardown()
129 * calls.
130 */
131 extern exchange_test_helper_t *exchange_test_helper;
132
133 /**
134 * Initialize charon and the helper object.
135 *
136 * @param plugins plugins to load
137 */
138 void exchange_test_helper_init(char *plugins);
139
140 /**
141 * Deinitialize the helper object.
142 */
143 void exchange_test_helper_deinit();
144
145 #endif /** EXCHANGE_TEST_HELPER_H_ @} */