summaryrefslogtreecommitdiff
path: root/src/libcharon/tests/utils/exchange_test_helper.h
blob: e1fdb012abaa5074ae404394763921c9c97a6c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * 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.
 */

/**
 * This class and singleton object initializes charon and provides helper
 * methods to create unit tests for IKEv2 exchanges.
 *
 * It also registers special implementations for the kernel_ipsec_t interface,
 * the sender and provides dummy configs and credentials.
 *
 * @defgroup exchange_test_helper exchange_test_helper
 * @{ @ingroup test_utils_c
 */

#ifndef EXCHANGE_TEST_HELPER_H_
#define EXCHANGE_TEST_HELPER_H_

#include <daemon.h>

#include "mock_sender.h"

typedef struct exchange_test_helper_t exchange_test_helper_t;
typedef struct exchange_test_sa_conf_t exchange_test_sa_conf_t;

struct exchange_test_helper_t {

	/**
	 * Sender instance used during tests
	 */
	mock_sender_t *sender;

	/**
	 * Set the initial byte of all nonces generated by future nonce
	 * generators (already instatiated nonce generators are not affected).
	 */
	u_char nonce_first_byte;

	/**
	 * Creates an established IKE_SA/CHILD_SA
	 *
	 * @param[out] init		IKE_SA of the initiator
	 * @param[out] resp		IKE_SA of the responder
	 * @param conf			configuration for SAs
	 */
	void (*establish_sa)(exchange_test_helper_t *this, ike_sa_t **init,
						 ike_sa_t **resp, exchange_test_sa_conf_t *conf);

	/**
	 * Pass a message to the given IKE_SA for processing, setting the IKE_SA on
	 * the bus while processing the message.
	 *
	 * @param ike_sa		the IKE_SA receiving the message
	 * @param message		the message, or NULL to pass the next message in the
	 *						send queue (adopted)
	 * @return				return value from ike_sa_t::process_message()
	 */
	status_t (*process_message)(exchange_test_helper_t *this, ike_sa_t *sa,
								message_t *message);

	/**
	 * Register a listener with the bus.
	 *
	 * Don't use bus_t::add_listener() directly for listeners on the stack
	 * as that could lead to invalid listeners registered when hooks are
	 * triggered during cleanup if a test case fails.  All of the listeners
	 * added this way are unregistered with the bus before cleaning up.
	 *
	 * @param listener		listener to add to the bus
	 */
	void (*add_listener)(exchange_test_helper_t *this, listener_t *listener);
};

struct exchange_test_sa_conf_t {

	/**
	 * Configuration for initiator and responder
	 */
	struct {
		/** IKE proposal */
		char *ike;
		/** ESP proposal */
		char *esp;
	} initiator, responder;
};

/**
 * Since we don't use the IKE_SA manager to checkout SAs use this to call a
 * method on the given IKE_SA in its context.
 */
#define call_ikesa(sa, method, ...) ({ \
	charon->bus->set_sa(charon->bus, sa); \
	sa->method(sa, ##__VA_ARGS__); \
	charon->bus->set_sa(charon->bus, NULL); \
})

/**
 * The one and only instance of the helper object.
 *
 * Set between exchange_test_helper_setup() and exchange_test_helper_teardown()
 * calls.
 */
extern exchange_test_helper_t *exchange_test_helper;

/**
 * Initialize charon and the helper object.
 *
 * @param plugins			plugins to load
 */
void exchange_test_helper_init(char *plugins);

/**
 * Deinitialize the helper object.
 */
void exchange_test_helper_deinit();

#endif /** EXCHANGE_TEST_HELPER_H_ @} */