0d1b9faed10035a541044ae5a9a544528410c839
[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 /**
28 * Check that there exists a specific number of CHILD_SAs.
29 */
30 #define assert_child_sa_count(ike_sa, count) \
31 ({ \
32 typeof(ike_sa) _sa = ike_sa; \
33 typeof(count) _count = count; \
34 test_assert_msg(_count == _sa->get_child_count(_sa), "unexpected number " \
35 "of CHILD_SAs in IKE_SA %s (%d != %d)", #ike_sa, _count, \
36 _sa->get_child_count(_sa)); \
37 })
38
39 /**
40 * Check if the CHILD_SA with the given SPI is in the expected state.
41 */
42 #define assert_child_sa_state(ike_sa, spi, state) \
43 ({ \
44 typeof(ike_sa) _sa = ike_sa; \
45 typeof(spi) _spi = spi; \
46 typeof(state) _state = state; \
47 child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
48 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
49 test_assert_msg(_child, "CHILD_SA with SPI %.8x does not exist", \
50 ntohl(_spi)); \
51 test_assert_msg(_state == _child->get_state(_child), "%N != %N", \
52 child_sa_state_names, _state, \
53 child_sa_state_names, _child->get_state(_child)); \
54 })
55
56 /**
57 * Assert that the CHILD_SA with the given inbound SPI does not exist.
58 */
59 #define assert_child_sa_not_exists(ike_sa, spi) \
60 ({ \
61 typeof(ike_sa) _sa = ike_sa; \
62 typeof(spi) _spi = spi; \
63 child_sa_t *_child = _sa->get_child_sa(_sa, PROTO_ESP, _spi, TRUE) ?: \
64 _sa->get_child_sa(_sa, PROTO_ESP, _spi, FALSE); \
65 test_assert_msg(!_child, "CHILD_SA with SPI %.8x exists", ntohl(_spi)); \
66 })
67
68 /**
69 * Assert that there is a specific number of tasks in a given queue
70 *
71 * @param ike_sa IKE_SA to check
72 * @param count number of expected tasks
73 * @param queue queue to check (task_queue_t)
74 */
75 #define assert_num_tasks(ike_sa, count, queue) \
76 ({ \
77 typeof(ike_sa) _sa = ike_sa; \
78 typeof(count) _count = count; \
79 int _c = 0; task_t *_task; \
80 enumerator_t *_enumerator = _sa->create_task_enumerator(_sa, queue); \
81 while (_enumerator->enumerate(_enumerator, &_task)) { _c++; } \
82 _enumerator->destroy(_enumerator); \
83 test_assert_msg(_count == _c, "unexpected number of tasks in " #queue " " \
84 "of IKE_SA %s (%d != %d)", #ike_sa, _count, _c); \
85 })
86
87 /**
88 * Assert that all task queues of the given IKE_SA are empty
89 *
90 * @param ike_sa IKE_SA to check
91 */
92 #define assert_sa_idle(ike_sa) \
93 ({ \
94 typeof(ike_sa) _ike_sa = ike_sa; \
95 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_QUEUED); \
96 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_ACTIVE); \
97 assert_num_tasks(_ike_sa, 0, TASK_QUEUE_PASSIVE); \
98 })
99
100 #endif /** SA_ASSERTS_H_ @}*/