unit-tests: Overload helper macro to check for outbound SA state
[strongswan.git] / src / libcharon / tests / utils / sa_asserts.h
1 /*
2 * Copyright (C) 2016-2017 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 * Special assertions against IKE_SAs and CHILD_SAs (e.g. regarding their
18 * state).
19 *
20 * @defgroup sa_asserts sa_asserts
21 * @{ @ingroup test_utils_c
22 */
23
24 #ifndef SA_ASSERTS_H_
25 #define SA_ASSERTS_H_
26
27 #include <inttypes.h>
28
29 /**
30 * Check that there exists a specific number of IKE_SAs in the manager.
31 */
32 #define assert_ike_sa_count(count) \
33 ({ \
34 typeof(count) _count = count; \
35 u_int _actual = charon->ike_sa_manager->get_count(charon->ike_sa_manager); \
36 test_assert_msg(_count == _actual, "unexpected number of IKE_SAs in " \
37 "manager (%d != %d)", _count, _actual); \
38 })
39
40 /**
41 * Check that the IKE_SA with the given SPIs and initiator flag is in the
42 * manager and return it. Does not actually keep the SA checked out as
43 * that would block cleaning up if asserts against it fail (since we control
44 * access to SAs it's also not really necessary).
45 */
46 #define assert_ike_sa_checkout(spi_i, spi_r, initiator) \
47 ({ \
48 typeof(spi_i) _spi_i = spi_i; \
49 typeof(spi_r) _spi_r = spi_r; \
50 typeof(initiator) _init = initiator; \
51 ike_sa_id_t *_id = ike_sa_id_create(IKEV2, _spi_i, _spi_r, _init); \
52 ike_sa_t *_ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, _id); \
53 test_assert_msg(_ike_sa, "IKE_SA with SPIs %.16"PRIx64"_i %.16"PRIx64"_r " \
54 "(%d) does not exist", be64toh(_spi_i), be64toh(_spi_r), _init); \
55 _id->destroy(_id); \
56 charon->ike_sa_manager->checkin(charon->ike_sa_manager, _ike_sa); \
57 _ike_sa; \
58 })
59
60 /**
61 * Check if the given IKE_SA is in the expected state.
62 */
63 #define assert_ike_sa_state(ike_sa, state) \
64 ({ \
65 typeof(ike_sa) _sa = ike_sa; \
66 typeof(state) _state = state; \
67 test_assert_msg(_state == _sa->get_state(_sa), "%N != %N", \
68 ike_sa_state_names, _state, \
69 ike_sa_state_names, _sa->get_state(_sa)); \
70 })
71
72 /**
73 * Check that there exists a specific number of CHILD_SAs.
74 */
75 #define assert_child_sa_count(ike_sa, count) \
76 ({ \
77 typeof(ike_sa) _sa = ike_sa; \
78 typeof(count) _count = count; \
79 test_assert_msg(_count == _sa->get_child_count(_sa), "unexpected number " \
80 "of CHILD_SAs in IKE_SA %s (%d != %d)", #ike_sa, _count, \
81 _sa->get_child_count(_sa)); \
82 })
83
84 /**
85 * Check if the CHILD_SA with the given SPI is in the expected state, optionally
86 * check the state of the outbound SA.
87 */
88 #define assert_child_sa_state(...) VA_ARGS_DISPATCH(assert_child_sa_state, __VA_ARGS__)(__VA_ARGS__)
89
90 /**
91 * Check if the CHILD_SA with the given SPI is in the expected state.
92 */
93 #define assert_child_sa_state3(ike_sa, spi, state) \
94 ({ \
95 typeof(ike_sa) _sa = ike_sa; \
96 typeof(spi) _spi = spi; \
97 typeof(state) _state = state; \
98 child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
99 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
100 test_assert_msg(_child, "CHILD_SA with SPI %.8x does not exist", \
101 ntohl(_spi)); \
102 test_assert_msg(_state == _child->get_state(_child), "%N != %N", \
103 child_sa_state_names, _state, \
104 child_sa_state_names, _child->get_state(_child)); \
105 })
106
107 /**
108 * Check if the outbound SA of a CHILD_SA with the given SPI is in the
109 * expected state.
110 */
111 #define assert_child_sa_state4(ike_sa, spi, state, outbound) \
112 ({ \
113 typeof(ike_sa) _sa = ike_sa; \
114 typeof(spi) _spi = spi; \
115 typeof(state) _state = state; \
116 typeof(outbound) _outbound = outbound; \
117 child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
118 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
119 test_assert_msg(_child, "CHILD_SA with SPI %.8x does not exist", \
120 ntohl(_spi)); \
121 test_assert_msg(_state == _child->get_state(_child), "%N != %N", \
122 child_sa_state_names, _state, \
123 child_sa_state_names, _child->get_state(_child)); \
124 test_assert_msg(_outbound == _child->get_outbound_state(_child), "%N != %N", \
125 child_sa_outbound_state_names, _outbound, \
126 child_sa_outbound_state_names, _child->get_outbound_state(_child)); \
127 })
128
129 /**
130 * Assert that the CHILD_SA with the given inbound SPI does not exist.
131 */
132 #define assert_child_sa_not_exists(ike_sa, spi) \
133 ({ \
134 typeof(ike_sa) _sa = ike_sa; \
135 typeof(spi) _spi = spi; \
136 child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
137 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
138 test_assert_msg(!_child, "CHILD_SA with SPI %.8x exists", ntohl(_spi)); \
139 })
140
141 /**
142 * Assert that there is a specific number of tasks in a given queue
143 *
144 * @param ike_sa IKE_SA to check
145 * @param count number of expected tasks
146 * @param queue queue to check (task_queue_t)
147 */
148 #define assert_num_tasks(ike_sa, count, queue) \
149 ({ \
150 typeof(ike_sa) _sa = ike_sa; \
151 typeof(count) _count = count; \
152 int _c = 0; task_t *_task; \
153 enumerator_t *_enumerator = _sa->create_task_enumerator(_sa, queue); \
154 while (_enumerator->enumerate(_enumerator, &_task)) { _c++; } \
155 _enumerator->destroy(_enumerator); \
156 test_assert_msg(_count == _c, "unexpected number of tasks in " #queue " " \
157 "of IKE_SA %s (%d != %d)", #ike_sa, _count, _c); \
158 })
159
160 /**
161 * Assert that all task queues of the given IKE_SA are empty
162 *
163 * @param ike_sa IKE_SA to check
164 */
165 #define assert_sa_idle(ike_sa) \
166 ({ \
167 typeof(ike_sa) _ike_sa = ike_sa; \
168 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_QUEUED); \
169 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_ACTIVE); \
170 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_PASSIVE); \
171 })
172
173 #endif /** SA_ASSERTS_H_ @}*/