7afa3b55b8e647c60c3c981338f6a2d5ba1a7d0a
[strongswan.git] / src / libcharon / tests / utils / sa_asserts.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 * 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.
86 */
87 #define assert_child_sa_state(ike_sa, spi, state) \
88 ({ \
89 typeof(ike_sa) _sa = ike_sa; \
90 typeof(spi) _spi = spi; \
91 typeof(state) _state = state; \
92 child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
93 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
94 test_assert_msg(_child, "CHILD_SA with SPI %.8x does not exist", \
95 ntohl(_spi)); \
96 test_assert_msg(_state == _child->get_state(_child), "%N != %N", \
97 child_sa_state_names, _state, \
98 child_sa_state_names, _child->get_state(_child)); \
99 })
100
101 /**
102 * Assert that the CHILD_SA with the given inbound SPI does not exist.
103 */
104 #define assert_child_sa_not_exists(ike_sa, spi) \
105 ({ \
106 typeof(ike_sa) _sa = ike_sa; \
107 typeof(spi) _spi = spi; \
108 child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
109 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
110 test_assert_msg(!_child, "CHILD_SA with SPI %.8x exists", ntohl(_spi)); \
111 })
112
113 /**
114 * Assert that there is a specific number of tasks in a given queue
115 *
116 * @param ike_sa IKE_SA to check
117 * @param count number of expected tasks
118 * @param queue queue to check (task_queue_t)
119 */
120 #define assert_num_tasks(ike_sa, count, queue) \
121 ({ \
122 typeof(ike_sa) _sa = ike_sa; \
123 typeof(count) _count = count; \
124 int _c = 0; task_t *_task; \
125 enumerator_t *_enumerator = _sa->create_task_enumerator(_sa, queue); \
126 while (_enumerator->enumerate(_enumerator, &_task)) { _c++; } \
127 _enumerator->destroy(_enumerator); \
128 test_assert_msg(_count == _c, "unexpected number of tasks in " #queue " " \
129 "of IKE_SA %s (%d != %d)", #ike_sa, _count, _c); \
130 })
131
132 /**
133 * Assert that all task queues of the given IKE_SA are empty
134 *
135 * @param ike_sa IKE_SA to check
136 */
137 #define assert_sa_idle(ike_sa) \
138 ({ \
139 typeof(ike_sa) _ike_sa = ike_sa; \
140 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_QUEUED); \
141 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_ACTIVE); \
142 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_PASSIVE); \
143 })
144
145 #endif /** SA_ASSERTS_H_ @}*/