unit-tests: Make IKE and ESP proposals configurable
[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 */
68 void (*process_message)(exchange_test_helper_t *this, ike_sa_t *sa,
69 message_t *message);
70 };
71
72 struct exchange_test_sa_conf_t {
73
74 /**
75 * Configuration for initiator and responder
76 */
77 struct {
78 /** IKE proposal */
79 char *ike;
80 /** ESP proposal */
81 char *esp;
82 } initiator, responder;
83 };
84
85 /**
86 * Since we don't use the IKE_SA manager to checkout SAs use this to call a
87 * method on the given IKE_SA in its context.
88 */
89 #define call_ikesa(sa, method, ...) ({ \
90 charon->bus->set_sa(charon->bus, sa); \
91 sa->method(sa, ##__VA_ARGS__); \
92 charon->bus->set_sa(charon->bus, NULL); \
93 })
94
95 /**
96 * The one and only instance of the helper object.
97 *
98 * Set between exchange_test_helper_setup() and exchange_test_helper_teardown()
99 * calls.
100 */
101 extern exchange_test_helper_t *exchange_test_helper;
102
103 /**
104 * Initialize charon and the helper object.
105 *
106 * @param plugins plugins to load
107 */
108 void exchange_test_helper_init(char *plugins);
109
110 /**
111 * Deinitialize the helper object.
112 */
113 void exchange_test_helper_deinit();
114
115 #endif /** EXCHANGE_TEST_HELPER_H_ @} */