proposal-substructure: Fix incorrect type for IKEv2 proposals
[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 * Pass a message to the given IKE_SA for processing, setting the IKE_SA on
62 * the bus while processing the message.
63 *
64 * @param ike_sa the IKE_SA receiving the message
65 * @param message the message, or NULL to pass the next message in the
66 * send queue (adopted)
67 * @return return value from ike_sa_t::process_message()
68 */
69 status_t (*process_message)(exchange_test_helper_t *this, ike_sa_t *sa,
70 message_t *message);
71
72 /**
73 * Register a listener with the bus.
74 *
75 * Don't use bus_t::add_listener() directly for listeners on the stack
76 * as that could lead to invalid listeners registered when hooks are
77 * triggered during cleanup if a test case fails. All of the listeners
78 * added this way are unregistered with the bus before cleaning up.
79 *
80 * @param listener listener to add to the bus
81 */
82 void (*add_listener)(exchange_test_helper_t *this, listener_t *listener);
83 };
84
85 struct exchange_test_sa_conf_t {
86
87 /**
88 * Configuration for initiator and responder
89 */
90 struct {
91 /** IKE proposal */
92 char *ike;
93 /** ESP proposal */
94 char *esp;
95 } initiator, responder;
96 };
97
98 /**
99 * Since we don't use the IKE_SA manager to checkout SAs use this to call a
100 * method on the given IKE_SA in its context.
101 */
102 #define call_ikesa(sa, method, ...) ({ \
103 charon->bus->set_sa(charon->bus, sa); \
104 sa->method(sa, ##__VA_ARGS__); \
105 charon->bus->set_sa(charon->bus, NULL); \
106 })
107
108 /**
109 * The one and only instance of the helper object.
110 *
111 * Set between exchange_test_helper_setup() and exchange_test_helper_teardown()
112 * calls.
113 */
114 extern exchange_test_helper_t *exchange_test_helper;
115
116 /**
117 * Initialize charon and the helper object.
118 *
119 * @param plugins plugins to load
120 */
121 void exchange_test_helper_init(char *plugins);
122
123 /**
124 * Deinitialize the helper object.
125 */
126 void exchange_test_helper_deinit();
127
128 #endif /** EXCHANGE_TEST_HELPER_H_ @} */