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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/*
* 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.
*/
/**
* Special assertions using listener_t.
*
* @defgroup exchange_test_asserts exchange_test_asserts
* @{ @ingroup test_utils_c
*/
#ifndef EXCHANGE_TEST_ASSERTS_H_
#define EXCHANGE_TEST_ASSERTS_H_
#include <bus/listeners/listener.h>
typedef struct listener_hook_assert_t listener_hook_assert_t;
typedef struct listener_message_assert_t listener_message_assert_t;
typedef struct listener_message_rule_t listener_message_rule_t;
struct listener_hook_assert_t {
/**
* Implemented interface
*/
listener_t listener;
/**
* Original source file
*/
const char *file;
/**
* Source line
*/
int line;
/**
* Name of the hook
*/
const char *name;
/**
* Expected number of calls (-1 to ignore)
*/
int expected;
/**
* Number of times the hook was called
*/
int count;
/**
* Expected updown result
*/
bool up;
/**
* Initiator/Inbound SPIs to expect in rekey event
*/
uint64_t spi_old, spi_new;
};
/**
* Basic callback for methods on listener_t, counting the number of calls.
*/
bool exchange_test_asserts_hook(listener_t *this);
/**
* Implementation of listener_t::ike_updown.
*/
bool exchange_test_asserts_ike_updown(listener_t *this, ike_sa_t *ike_sa,
bool up);
/**
* Implementation of listener_t::child_updown.
*/
bool exchange_test_asserts_child_updown(listener_t *this, ike_sa_t *ike_sa,
child_sa_t *child_sa, bool up);
/**
* Implementation of listener_t::ike_rekey.
*/
bool exchange_test_asserts_ike_rekey(listener_t *this, ike_sa_t *old,
ike_sa_t *new);
/**
* Implementation of listener_t::child_rekey.
*/
bool exchange_test_asserts_child_rekey(listener_t *this, ike_sa_t *ike_sa,
child_sa_t *old, child_sa_t *new);
/**
* Check if a statement evaluates to TRUE, use original source file and line
* in the error message if not.
*
* @param x statement to evaluate
* @param l listener providing original source file and line
* @param fmt printf format string
* @param ... arguments for fmt
*/
#define assert_listener_msg(x, l, fmt, ...) ({ \
test_fail_if_worker_failed(); \
if (!(x)) \
{ \
test_fail_msg((l)->file, (l)->line, "%s: " fmt, #x, ##__VA_ARGS__); \
} \
})
/**
* Initialize an assertion that enforces that the given hook was called.
* Must be matched by a call to assert_hook().
*
* @param name name of the hook
*/
#define assert_hook_called(name) \
_assert_hook_init(name, exchange_test_asserts_hook, .expected = 1)
/**
* Initialize an assertion that enforces that the given hook was not called.
* Must be matched by a call to assert_hook().
*
* @param name name of the hook
*/
#define assert_hook_not_called(name) \
_assert_hook_init(name, exchange_test_asserts_hook, .expected = 0)
/**
* Initialize an assertion that enforces that the given updown hook was called
* with the expected result.
* Must be matched by a call to assert_hook().
*
* @param name name of the hook
* @param e whether to expect up in the hook to be TRUE or not
*/
#define assert_hook_updown(name, e) \
_assert_hook_init(name, \
streq(#name, "ike_updown") ? (void*)exchange_test_asserts_ike_updown \
: (void*)exchange_test_asserts_child_updown, \
.expected = 1, \
.up = e, \
)
/**
* Initialize an assertion that enforces that the given rekey hook was called
* with the SAs with the matching initiator/inbound SPIs.
* Must be matched by a call to assert_hook().
*
* @param name name of the hook
* @param old SPI of the old SA
* @param new SPI of the new SA
*/
#define assert_hook_rekey(name, old, new) \
_assert_hook_init(name, \
streq(#name, "ike_rekey") ? (void*)exchange_test_asserts_ike_rekey \
: (void*)exchange_test_asserts_child_rekey, \
.expected = 1, \
.spi_old = old, \
.spi_new = new, \
)
/**
* Initialize assertions against invocations of listener_t hooks. Each call
* must be matched by a call to assert_hook().
*/
#define _assert_hook_init(n, callback, ...) \
do { \
listener_hook_assert_t _hook_listener = { \
.listener = { .n = (void*)callback, }, \
.file = __FILE__, \
.line = __LINE__, \
.name = #n, \
##__VA_ARGS__ \
}; \
exchange_test_helper->add_listener(exchange_test_helper, &_hook_listener.listener)
/**
* Enforce the most recently initialized hook assertion.
*/
#define assert_hook() \
charon->bus->remove_listener(charon->bus, &_hook_listener.listener); \
if (_hook_listener.expected > 0) { \
if (_hook_listener.count > 0) { \
assert_listener_msg(_hook_listener.expected == _hook_listener.count, \
&_hook_listener, "hook '%s' was called %d times " \
"instead of %d", _hook_listener.name, \
_hook_listener.count, _hook_listener.expected); \
} else { \
assert_listener_msg(_hook_listener.count, &_hook_listener, \
"hook '%s' was not called (expected %d)", _hook_listener.name, \
_hook_listener.expected); \
} \
} else if (_hook_listener.expected == 0) { \
assert_listener_msg(_hook_listener.count == 0, &_hook_listener, \
"hook '%s' was called unexpectedly", _hook_listener.name); \
} \
} while(FALSE)
/**
* Rules regarding payloads/notifies to expect/not expect in a message
*/
struct listener_message_rule_t {
/**
* Whether the payload/notify is expected in the message, FALSE to fail if
* it is found
*/
bool expected;
/**
* Payload type to expect/not expect
*/
payload_type_t payload;
/**
* Notify type to expect/not expect (paylod type does not have to be
* specified)
*/
notify_type_t notify;
};
/**
* Data used to check plaintext messages via listener_t
*/
struct listener_message_assert_t {
/**
* Implemented interface
*/
listener_t listener;
/**
* Original source file
*/
const char *file;
/**
* Source line
*/
int line;
/**
* Whether to check the next inbound or outbound message
*/
bool incoming;
/**
* Payload count to expect (-1 to ignore the count)
*/
int count;
/**
* Payloads to expect or not expect in a message
*/
listener_message_rule_t *rules;
/**
* Number of rules
*/
int num_rules;
};
/**
* Implementation of listener_t::message collecting data and asserting
* certain things.
*/
bool exchange_test_asserts_message(listener_t *this, ike_sa_t *ike_sa,
message_t *message, bool incoming, bool plain);
/**
* Assert that the next in- or outbound plaintext message is empty.
*
* @param dir IN or OUT to check the next in- or outbound message
*/
#define assert_message_empty(dir) \
_assert_payload(dir, 0)
/**
* Assert that the next in- or outbound plaintext message contains exactly
* one payload of the given type.
*
* @param dir IN or OUT to check the next in- or outbound message
* @param expected expected payload type
*/
#define assert_single_payload(dir, expected) \
_assert_payload(dir, 1, { TRUE, expected, 0 })
/**
* Assert that the next in- or outbound plaintext message contains exactly
* one notify of the given type.
*
* @param dir IN or OUT to check the next in- or outbound message
* @param expected expected notify type
*/
#define assert_single_notify(dir, expected) \
_assert_payload(dir, 1, { TRUE, 0, expected })
/**
* Assert that the next in- or outbound plaintext message contains a notify
* of the given type.
*
* @param dir IN or OUT to check the next in- or outbound message
* @param expected expected notify type
*/
#define assert_notify(dir, expected) \
_assert_payload(dir, -1, { TRUE, 0, expected })
/**
* Assert that the next in- or outbound plaintext message does not contain a
* notify of the given type.
*
* @param dir IN or OUT to check the next in- or outbound message
* @param unexpected not expected notify type
*/
#define assert_no_notify(dir, unexpected) \
_assert_payload(dir, -1, { FALSE, 0, unexpected })
#define _assert_payload(dir, c, ...) ({ \
listener_message_rule_t _rules[] = { __VA_ARGS__ }; \
listener_message_assert_t _listener = { \
.listener = { .message = exchange_test_asserts_message, }, \
.file = __FILE__, \
.line = __LINE__, \
.incoming = streq(#dir, "IN") ? TRUE : FALSE, \
.count = c, \
.rules = _rules, \
.num_rules = countof(_rules), \
}; \
exchange_test_helper->add_listener(exchange_test_helper, &_listener.listener); \
})
#endif /** EXCHANGE_TEST_ASSERTS_H_ @}*/
|