2 * Copyright (C) 2016-2017 Tobias Brunner
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include <test_suite.h>
21 #include "exchange_test_asserts.h"
22 #include "mock_ipsec.h"
27 bool exchange_test_asserts_hook(listener_t
*listener
)
29 listener_hook_assert_t
*this = (listener_hook_assert_t
*)listener
;
38 bool exchange_test_asserts_ike_updown(listener_t
*listener
, ike_sa_t
*ike_sa
,
41 listener_hook_assert_t
*this = (listener_hook_assert_t
*)listener
;
44 assert_listener_msg(this->up
== up
, this, "IKE_SA not '%s'",
45 this->up
? "up" : "down");
52 bool exchange_test_asserts_child_updown(listener_t
*listener
, ike_sa_t
*ike_sa
,
53 child_sa_t
*child_sa
, bool up
)
55 listener_hook_assert_t
*this = (listener_hook_assert_t
*)listener
;
58 assert_listener_msg(this->up
== up
, this, "CHILD_SA not '%s'",
59 this->up
? "up" : "down");
66 bool exchange_test_asserts_ike_rekey(listener_t
*listener
, ike_sa_t
*old
,
69 listener_hook_assert_t
*this = (listener_hook_assert_t
*)listener
;
74 id
= old
->get_id(old
);
75 spi
= id
->get_initiator_spi(id
);
76 assert_listener_msg(this->spi_old
== spi
, this, "unexpected old IKE_SA "
77 "%.16"PRIx64
"_i instead of %.16"PRIx64
"_i",
78 be64toh(spi
), be64toh(this->spi_old
));
79 id
= new->get_id(new);
80 spi
= id
->get_initiator_spi(id
);
81 assert_listener_msg(this->spi_new
== spi
, this, "unexpected new IKE_SA "
82 "%.16"PRIx64
"_i instead of %.16"PRIx64
"_i",
83 be64toh(spi
), be64toh(this->spi_new
));
90 bool exchange_test_asserts_child_rekey(listener_t
*listener
, ike_sa_t
*ike_sa
,
91 child_sa_t
*old
, child_sa_t
*new)
93 listener_hook_assert_t
*this = (listener_hook_assert_t
*)listener
;
94 uint32_t spi
, expected
;
97 spi
= old
->get_spi(old
, TRUE
);
98 expected
= this->spi_old
;
99 assert_listener_msg(expected
== spi
, this, "unexpected old CHILD_SA %.8x "
100 "instead of %.8x", spi
, expected
);
101 spi
= new->get_spi(new, TRUE
);
102 expected
= this->spi_new
;
103 assert_listener_msg(expected
== spi
, this, "unexpected new CHILD_SA %.8x "
104 "instead of %.8x", spi
, expected
);
109 * Track SAs via updown event.
111 static void track_sa_updown(listener_track_sas_assert_t
*this, char *event
,
112 array_t
*sas
, uint32_t id
, bool up
)
120 for (i
= 0; i
< array_count(sas
); i
++)
122 array_get(sas
, i
, &existing
);
123 assert_listener_msg(id
!= existing
, this, "duplicate %s(up) event "
124 "for SA %u", event
, id
);
126 array_insert(sas
, ARRAY_TAIL
, &id
);
130 for (i
= 0; i
< array_count(sas
); i
++)
132 array_get(sas
, i
, &existing
);
135 array_remove(sas
, i
, NULL
);
140 assert_listener_msg(found
, this, "%s(down) event for unknown SA %u",
146 * Track SAs via a rekey event.
148 static void track_sa_rekey(listener_track_sas_assert_t
*this, char *event
,
149 array_t
*sas
, uint32_t old_id
, uint32_t new_id
)
155 for (i
= 0; i
< array_count(sas
); i
++)
157 array_get(sas
, i
, &existing
);
158 if (old_id
== existing
)
160 array_remove(sas
, i
, NULL
);
165 assert_listener_msg(found
, this, "%s() event for unknown old SA %u", event
,
168 for (i
= 0; i
< array_count(sas
); i
++)
170 array_get(sas
, i
, &existing
);
171 assert_listener_msg(new_id
!= existing
, this, "%s() event for "
172 "already up new SA %u", event
, new_id
);
174 array_insert(sas
, ARRAY_TAIL
, &new_id
);
178 * Described in header
180 bool exchange_test_asserts_track_ike_updown(listener_t
*listener
,
181 ike_sa_t
*ike_sa
, bool up
)
183 listener_track_sas_assert_t
*this = (listener_track_sas_assert_t
*)listener
;
185 track_sa_updown(this, "ike_updown", this->ike_sas
,
186 ike_sa
->get_unique_id(ike_sa
), up
);
191 * Described in header
193 bool exchange_test_asserts_track_child_updown(listener_t
*listener
,
195 child_sa_t
*child_sa
, bool up
)
197 listener_track_sas_assert_t
*this = (listener_track_sas_assert_t
*)listener
;
199 track_sa_updown(this, "child_updown", this->child_sas
,
200 child_sa
->get_unique_id(child_sa
), up
);
205 * Described in header
207 bool exchange_test_asserts_track_ike_rekey(listener_t
*listener
, ike_sa_t
*old
,
210 listener_track_sas_assert_t
*this = (listener_track_sas_assert_t
*)listener
;
212 track_sa_rekey(this, "ike_rekey", this->ike_sas
, old
->get_unique_id(old
),
213 new->get_unique_id(new));
218 * Described in header
220 bool exchange_test_asserts_track_child_rekey(listener_t
*listener
,
221 ike_sa_t
*ike_sa
, child_sa_t
*old
,
224 listener_track_sas_assert_t
*this = (listener_track_sas_assert_t
*)listener
;
226 track_sa_rekey(this, "child_rekey", this->child_sas
, old
->get_unique_id(old
),
227 new->get_unique_id(new));
232 * Assert a given message rule
234 static void assert_message_rule(listener_message_assert_t
*this, message_t
*msg
,
235 listener_message_rule_t
*rule
)
241 assert_listener_msg(msg
->get_payload(msg
, rule
->payload
),
242 this, "expected payload (%N) not found",
243 payload_type_names
, rule
->payload
);
248 assert_listener_msg(msg
->get_notify(msg
, rule
->notify
),
249 this, "expected notify payload (%N) not found",
250 notify_type_names
, rule
->notify
);
257 assert_listener_msg(!msg
->get_payload(msg
, rule
->payload
),
258 this, "unexpected payload (%N) found",
259 payload_type_names
, rule
->payload
);
264 assert_listener_msg(!msg
->get_notify(msg
, rule
->notify
),
265 this, "unexpected notify payload (%N) found",
266 notify_type_names
, rule
->notify
);
272 * Described in header
274 bool exchange_test_asserts_message(listener_t
*listener
, ike_sa_t
*ike_sa
,
275 message_t
*message
, bool incoming
, bool plain
)
277 listener_message_assert_t
*this = (listener_message_assert_t
*)listener
;
279 if (plain
&& this->incoming
== incoming
)
281 if (this->count
>= 0)
283 enumerator_t
*enumerator
;
285 enumerator
= message
->create_payload_enumerator(message
);
286 while (enumerator
->enumerate(enumerator
, NULL
))
290 enumerator
->destroy(enumerator
);
291 assert_listener_msg(this->count
== count
, this, "unexpected payload "
292 "count in message (%d != %d)", this->count
,
299 for (i
= 0; i
< this->num_rules
; i
++)
301 assert_message_rule(this, message
, &this->rules
[i
]);
314 static int spis_cmp(const void *a
, const void *b
)
316 return *(const uint32_t*)a
- *(const uint32_t*)b
;
320 * Compare two SPIs to sort them
322 static int spis_sort(const void *a
, const void *b
, void *data
)
324 return spis_cmp(a
, b
);
329 * Described in header
331 void exchange_test_asserts_ipsec_sas(ipsec_sas_assert_t
*sas
)
333 enumerator_t
*enumerator
;
339 spis
= array_create(sizeof(uint32_t), 0);
340 for (i
= 0; i
< sas
->count
; i
++)
342 array_insert(spis
, ARRAY_TAIL
, &sas
->spis
[i
]);
344 array_sort(spis
, spis_sort
, NULL
);
346 enumerator
= mock_ipsec_create_sa_enumerator();
347 while (enumerator
->enumerate(enumerator
, &ike_sa
, &spi
))
349 if (ike_sa
== sas
->ike_sa
)
351 i
= array_bsearch(spis
, &spi
, spis_cmp
, NULL
);
352 assert_listener_msg(i
!= -1, sas
, "unexpected IPsec SA %.8x", spi
);
353 array_remove(spis
, i
, NULL
);
356 enumerator
->destroy(enumerator
);
357 for (i
= 0; i
< array_count(spis
); i
++)
359 array_get(spis
, i
, &spi
);
360 assert_listener_msg(!spi
, sas
, "expected IPsec SA %.8x not found", spi
);