861ce0d47538617305f43a39781478b929a5cab6
[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
36 struct exchange_test_helper_t {
37
38 /**
39 * Sender instance used during tests
40 */
41 mock_sender_t *sender;
42
43 /**
44 * Set the initial byte of all nonces generated by future nonce
45 * generators (already instatiated nonce generators are not affected).
46 */
47 u_char nonce_first_byte;
48
49 /**
50 * Creates an established IKE_SA/CHILD_SA
51 *
52 * @param[out] init IKE_SA of the initiator
53 * @param[out] resp IKE_SA of the responder
54 */
55 void (*establish_sa)(exchange_test_helper_t *this, ike_sa_t **init,
56 ike_sa_t **resp);
57
58 /**
59 * Pass a message to the given IKE_SA for processing, setting the IKE_SA on
60 * the bus while processing the message.
61 *
62 * @param ike_sa the IKE_SA receiving the message
63 * @param message the message, or NULL to pass the next message in the
64 * send queue (adopted)
65 */
66 void (*process_message)(exchange_test_helper_t *this, ike_sa_t *sa,
67 message_t *message);
68 };
69
70 /**
71 * Since we don't use the IKE_SA manager to checkout SAs use this to call a
72 * method on the given IKE_SA in its context.
73 */
74 #define call_ikesa(sa, method, ...) ({ \
75 charon->bus->set_sa(charon->bus, sa); \
76 sa->method(sa, ##__VA_ARGS__); \
77 charon->bus->set_sa(charon->bus, NULL); \
78 })
79
80 /**
81 * The one and only instance of the helper object.
82 *
83 * Set between exchange_test_helper_setup() and exchange_test_helper_teardown()
84 * calls.
85 */
86 extern exchange_test_helper_t *exchange_test_helper;
87
88 /**
89 * Initialize charon and the helper object.
90 *
91 * @param plugins plugins to load
92 */
93 void exchange_test_helper_init(char *plugins);
94
95 /**
96 * Deinitialize the helper object.
97 */
98 void exchange_test_helper_deinit();
99
100 #endif /** EXCHANGE_TEST_HELPER_H_ @} */