summaryrefslogtreecommitdiff
path: root/src/libcharon/tests/utils/sa_asserts.h
blob: d23f724f1c50b580f07fb4e0f9fa277d81b4664a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2016-2017 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.
 */

/**
 * Special assertions against IKE_SAs and CHILD_SAs (e.g. regarding their
 * state).
 *
 * @defgroup sa_asserts sa_asserts
 * @{ @ingroup test_utils_c
 */

#ifndef SA_ASSERTS_H_
#define SA_ASSERTS_H_

#include <inttypes.h>

/**
 * Check that there exists a specific number of IKE_SAs in the manager.
 */
#define assert_ike_sa_count(count) \
({ \
	typeof(count) _count = count; \
	u_int _actual = charon->ike_sa_manager->get_count(charon->ike_sa_manager); \
	test_assert_msg(_count == _actual, "unexpected number of IKE_SAs in " \
					"manager (%d != %d)", _count, _actual); \
})

/**
 * Check that the IKE_SA with the given SPIs and initiator flag is in the
 * manager and return it.  Does not actually keep the SA checked out as
 * that would block cleaning up if asserts against it fail (since we control
 * access to SAs it's also not really necessary).
 */
#define assert_ike_sa_checkout(spi_i, spi_r, initiator) \
({ \
	typeof(spi_i) _spi_i = spi_i; \
	typeof(spi_r) _spi_r = spi_r; \
	typeof(initiator) _init = initiator; \
	ike_sa_id_t *_id = ike_sa_id_create(IKEV2, _spi_i, _spi_r, _init); \
	ike_sa_t *_ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, _id); \
	test_assert_msg(_ike_sa, "IKE_SA with SPIs %.16"PRIx64"_i %.16"PRIx64"_r " \
					"(%d) does not exist", be64toh(_spi_i), be64toh(_spi_r), _init); \
	_id->destroy(_id); \
	charon->ike_sa_manager->checkin(charon->ike_sa_manager, _ike_sa); \
	_ike_sa; \
})

/**
 * Check if the given IKE_SA is in the expected state.
 */
#define assert_ike_sa_state(ike_sa, state) \
({ \
	typeof(ike_sa) _sa = ike_sa; \
	typeof(state) _state = state; \
	test_assert_msg(_state == _sa->get_state(_sa), "%N != %N", \
					ike_sa_state_names, _state, \
					ike_sa_state_names, _sa->get_state(_sa)); \
})

/**
 * Check that there exists a specific number of CHILD_SAs.
 */
#define assert_child_sa_count(ike_sa, count) \
({ \
	typeof(ike_sa) _sa = ike_sa; \
	typeof(count) _count = count; \
	test_assert_msg(_count == _sa->get_child_count(_sa), "unexpected number " \
					"of CHILD_SAs in IKE_SA %s (%d != %d)", #ike_sa, _count, \
					_sa->get_child_count(_sa)); \
})

/**
 * Check if the CHILD_SA with the given SPI is in the expected state, optionally
 * check the state of the outbound SA.
 */
#define assert_child_sa_state(...) VA_ARGS_DISPATCH(assert_child_sa_state, __VA_ARGS__)(__VA_ARGS__)

/**
 * Check if the CHILD_SA with the given SPI is in the expected state.
 */
#define assert_child_sa_state3(ike_sa, spi, state) \
({ \
	typeof(ike_sa) _sa = ike_sa; \
	typeof(spi) _spi = spi; \
	typeof(state) _state = state; \
	child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
						 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
	test_assert_msg(_child, "CHILD_SA with SPI %.8x does not exist", \
					ntohl(_spi)); \
	test_assert_msg(_state == _child->get_state(_child), "%N != %N", \
					child_sa_state_names, _state, \
					child_sa_state_names, _child->get_state(_child)); \
})

/**
 * Check if the outbound SA of a CHILD_SA with the given SPI is in the
 * expected state.
 */
#define assert_child_sa_state4(ike_sa, spi, state, outbound) \
({ \
	typeof(ike_sa) _sa = ike_sa; \
	typeof(spi) _spi = spi; \
	typeof(state) _state = state; \
	typeof(outbound) _outbound = outbound; \
	child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
						 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
	test_assert_msg(_child, "CHILD_SA with SPI %.8x does not exist", \
					ntohl(_spi)); \
	test_assert_msg(_state == _child->get_state(_child), "%N != %N", \
					child_sa_state_names, _state, \
					child_sa_state_names, _child->get_state(_child)); \
	test_assert_msg(_outbound == _child->get_outbound_state(_child), "%N != %N", \
					child_sa_outbound_state_names, _outbound, \
					child_sa_outbound_state_names, _child->get_outbound_state(_child)); \
})

/**
 * Assert that the CHILD_SA with the given inbound SPI does not exist.
 */
#define assert_child_sa_not_exists(ike_sa, spi) \
({ \
	typeof(ike_sa) _sa = ike_sa; \
	typeof(spi) _spi = spi; \
	child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
						 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
	test_assert_msg(!_child, "CHILD_SA with SPI %.8x exists", ntohl(_spi)); \
})

/**
 * Assert that there is a specific number of tasks in a given queue
 *
 * @param ike_sa		IKE_SA to check
 * @param count			number of expected tasks
 * @param queue			queue to check (task_queue_t)
 */
#define assert_num_tasks(ike_sa, count, queue) \
({ \
	typeof(ike_sa) _sa = ike_sa; \
	typeof(count) _count = count; \
	int _c = 0; task_t *_task; \
	enumerator_t *_enumerator = _sa->create_task_enumerator(_sa, queue); \
	while (_enumerator->enumerate(_enumerator, &_task)) { _c++; } \
	_enumerator->destroy(_enumerator); \
	test_assert_msg(_count == _c, "unexpected number of tasks in " #queue " " \
					"of IKE_SA %s (%d != %d)", #ike_sa, _count, _c); \
})

/**
 * Assert that all task queues of the given IKE_SA are empty
 *
 * @param ike_sa		IKE_SA to check
 */
#define assert_sa_idle(ike_sa) \
({ \
	typeof(ike_sa) _ike_sa = ike_sa; \
	assert_num_tasks(_ike_sa, 0, TASK_QUEUE_QUEUED); \
	assert_num_tasks(_ike_sa, 0, TASK_QUEUE_ACTIVE); \
	assert_num_tasks(_ike_sa, 0, TASK_QUEUE_PASSIVE); \
})

#endif /** SA_ASSERTS_H_ @}*/