1b44798c8ffec3a7fd32d6f6dbb859e3520bdc54
[strongswan.git] / src / libcharon / tests / suites / test_child_delete.c
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 #include "test_suite.h"
17
18 #include <daemon.h>
19 #include <tests/utils/exchange_test_helper.h>
20 #include <tests/utils/exchange_test_asserts.h>
21 #include <tests/utils/sa_asserts.h>
22
23 /**
24 * Regular CHILD_SA deletion either initiated by the original initiator or
25 * responder of the IKE_SA.
26 */
27 START_TEST(test_regular)
28 {
29 ike_sa_t *a, *b;
30
31 if (_i)
32 { /* responder deletes the CHILD_SA (SPI 2) */
33 exchange_test_helper->establish_sa(exchange_test_helper,
34 &b, &a);
35 }
36 else
37 { /* initiator deletes the CHILD_SA (SPI 1) */
38 exchange_test_helper->establish_sa(exchange_test_helper,
39 &a, &b);
40 }
41 assert_hook_not_called(child_updown);
42 call_ikesa(a, delete_child_sa, PROTO_ESP, _i+1, FALSE);
43 assert_child_sa_state(a, _i+1, CHILD_DELETING);
44 assert_hook();
45
46 /* INFORMATIONAL { D } --> */
47 assert_hook_updown(child_updown, FALSE);
48 assert_single_payload(IN, PLV2_DELETE);
49 exchange_test_helper->process_message(exchange_test_helper, b, NULL);
50 assert_child_sa_count(b, 0);
51 assert_hook();
52
53 /* <-- INFORMATIONAL { D } */
54 assert_hook_updown(child_updown, FALSE);
55 assert_single_payload(IN, PLV2_DELETE);
56 exchange_test_helper->process_message(exchange_test_helper, a, NULL);
57 assert_child_sa_count(a, 0);
58 assert_hook();
59
60 call_ikesa(a, destroy);
61 call_ikesa(b, destroy);
62 }
63 END_TEST
64
65 /**
66 * Both peers initiate the CHILD_SA deletion concurrently and should handle
67 * the collision properly.
68 */
69 START_TEST(test_collision)
70 {
71 ike_sa_t *a, *b;
72
73 exchange_test_helper->establish_sa(exchange_test_helper,
74 &a, &b);
75 /* both peers delete the CHILD_SA concurrently */
76 assert_hook_not_called(child_updown);
77 call_ikesa(a, delete_child_sa, PROTO_ESP, 1, FALSE);
78 assert_child_sa_state(a, 1, CHILD_DELETING);
79 call_ikesa(b, delete_child_sa, PROTO_ESP, 2, FALSE);
80 assert_child_sa_state(b, 2, CHILD_DELETING);
81 assert_hook();
82
83 /* RFC 7296 says:
84 *
85 * Normally, the response in the INFORMATIONAL exchange will contain
86 * Delete payloads for the paired SAs going in the other direction.
87 * There is one exception. If, by chance, both ends of a set of SAs
88 * independently decide to close them, each may send a Delete payload
89 * and the two requests may cross in the network. If a node receives a
90 * delete request for SAs for which it has already issued a delete
91 * request, it MUST delete the outgoing SAs while processing the request
92 * and the incoming SAs while processing the response. In that case,
93 * the responses MUST NOT include Delete payloads for the deleted SAs,
94 * since that would result in duplicate deletion and could in theory
95 * delete the wrong SA.
96 *
97 * We don't handle SAs separately so we expect both are still installed,
98 * but the INFORMATIONAL response should not contain a DELETE payload.
99 */
100
101 /* INFORMATIONAL { D } --> */
102 assert_hook_not_called(child_updown);
103 assert_single_payload(IN, PLV2_DELETE);
104 exchange_test_helper->process_message(exchange_test_helper, b, NULL);
105 assert_child_sa_state(b, 2, CHILD_DELETING);
106 /* <-- INFORMATIONAL { D } */
107 assert_single_payload(IN, PLV2_DELETE);
108 exchange_test_helper->process_message(exchange_test_helper, a, NULL);
109 assert_child_sa_state(a, 1, CHILD_DELETING);
110 assert_hook();
111
112 /* <-- INFORMATIONAL { } */
113 assert_hook_updown(child_updown, FALSE);
114 assert_message_empty(IN);
115 exchange_test_helper->process_message(exchange_test_helper, a, NULL);
116 assert_child_sa_count(a, 0);
117 assert_hook();
118 /* INFORMATIONAL { } --> */
119 assert_hook_updown(child_updown, FALSE);
120 assert_message_empty(IN);
121 exchange_test_helper->process_message(exchange_test_helper, b, NULL);
122 assert_child_sa_count(b, 0);
123 assert_hook();
124
125 call_ikesa(a, destroy);
126 call_ikesa(b, destroy);
127 }
128 END_TEST
129
130 /**
131 * This is like the collision above but one of the DELETEs is dropped or delayed
132 * so the other peer is not aware that there is a collision.
133 */
134 START_TEST(test_collision_drop)
135 {
136 ike_sa_t *a, *b;
137 message_t *msg;
138
139 exchange_test_helper->establish_sa(exchange_test_helper,
140 &a, &b);
141 /* both peers delete the CHILD_SA concurrently */
142 assert_hook_not_called(child_updown);
143 call_ikesa(a, delete_child_sa, PROTO_ESP, 1, FALSE);
144 assert_child_sa_state(a, 1, CHILD_DELETING);
145 call_ikesa(b, delete_child_sa, PROTO_ESP, 2, FALSE);
146 assert_child_sa_state(b, 2, CHILD_DELETING);
147 assert_hook();
148
149 /* INFORMATIONAL { D } --> */
150 assert_hook_not_called(child_updown);
151 assert_single_payload(IN, PLV2_DELETE);
152 exchange_test_helper->process_message(exchange_test_helper, b, NULL);
153 assert_child_sa_state(b, 2, CHILD_DELETING);
154 assert_hook();
155
156 /* drop/delay the responder's message */
157 msg = exchange_test_helper->sender->dequeue(exchange_test_helper->sender);
158
159 /* <-- INFORMATIONAL { } */
160 assert_hook_updown(child_updown, FALSE);
161 assert_message_empty(IN);
162 exchange_test_helper->process_message(exchange_test_helper, a, NULL);
163 assert_child_sa_count(a, 0);
164 assert_hook();
165
166 /* <-- INFORMATIONAL { D } (delayed/retransmitted) */
167 assert_hook_not_called(child_updown);
168 assert_single_payload(IN, PLV2_DELETE);
169 exchange_test_helper->process_message(exchange_test_helper, a, msg);
170 assert_hook();
171
172 /* INFORMATIONAL { } --> */
173 assert_hook_updown(child_updown, FALSE);
174 assert_message_empty(IN);
175 exchange_test_helper->process_message(exchange_test_helper, b, NULL);
176 assert_child_sa_count(b, 0);
177 assert_hook();
178
179 call_ikesa(a, destroy);
180 call_ikesa(b, destroy);
181 }
182 END_TEST
183
184 Suite *child_delete_suite_create()
185 {
186 Suite *s;
187 TCase *tc;
188
189 s = suite_create("child delete");
190
191 tc = tcase_create("regular");
192 tcase_add_loop_test(tc, test_regular, 0, 2);
193 suite_add_tcase(s, tc);
194
195 tc = tcase_create("collisions");
196 tcase_add_test(tc, test_collision);
197 tcase_add_test(tc, test_collision_drop);
198 suite_add_tcase(s, tc);
199
200 return s;
201 }