Merge branch 'childless'
[strongswan.git] / src / libcharon / tests / suites / test_peer_cfg.c
1 /*
2 * Copyright (C) 2018 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 <config/peer_cfg.h>
19 #include <config/child_cfg.h>
20
21 /**
22 * Create a simple IKE config
23 */
24 static ike_cfg_t *create_ike_cfg()
25 {
26 ike_cfg_create_t ike = {
27 .version = IKEV2,
28 .local = "127.0.0.1",
29 .local_port = 500,
30 .remote = "127.0.0.1",
31 .remote_port = 500,
32 };
33 return ike_cfg_create(&ike);
34 }
35
36 /**
37 * Create a simple peer config
38 */
39 static peer_cfg_t *create_peer_cfg()
40 {
41 peer_cfg_create_t peer = {};
42
43 return peer_cfg_create("peer", create_ike_cfg(), &peer);
44 }
45
46 static peer_cfg_t *peer_a, *peer_b;
47
48 START_SETUP(setup_replace)
49 {
50 peer_a = create_peer_cfg();
51 peer_b = create_peer_cfg();
52 }
53 END_SETUP
54
55 START_TEARDOWN(teardown_replace)
56 {
57 peer_a->destroy(peer_a);
58 peer_b->destroy(peer_b);
59 }
60 END_TEARDOWN
61
62 /**
63 * Check if the changes are correctly reported
64 * All given objects are destroyed
65 */
66 static void test_replace(enumerator_t *changes, linked_list_t *rem,
67 linked_list_t *add)
68 {
69 child_cfg_t *child;
70 bool added;
71
72 while (changes->enumerate(changes, &child, &added))
73 {
74 if (added)
75 {
76 ck_assert_msg(add->remove(add, child, NULL) == 1, "child config "
77 "was unexpectedly added");
78 }
79 else
80 {
81 ck_assert_msg(rem->remove(rem, child, NULL) == 1, "child config "
82 "was unexpectedly removed");
83 }
84 }
85 changes->destroy(changes);
86 ck_assert_msg(!rem->get_count(rem), "expected child config was not removed");
87 ck_assert_msg(!add->get_count(add), "expected child config was not added");
88 rem->destroy(rem);
89 add->destroy(add);
90 }
91
92 /**
93 * Check if the given child configs are contained in the peer config
94 * The list is destroyed
95 */
96 static void test_child_cfgs(peer_cfg_t *peer, linked_list_t *children)
97 {
98 enumerator_t *enumerator;
99 child_cfg_t *child;
100
101 enumerator = peer->create_child_cfg_enumerator(peer);
102 while (enumerator->enumerate(enumerator, &child))
103 {
104 ck_assert_msg(children->remove(children, child, NULL) == 1, "child "
105 "config was unexpectedly contained in peer config");
106 }
107 enumerator->destroy(enumerator);
108 ck_assert_msg(!children->get_count(children), "expected child config was "
109 "not contained in peer config");
110 children->destroy(children);
111 }
112
113 START_TEST(replace_child_cfgs_empty)
114 {
115 child_cfg_create_t cfg = {};
116 child_cfg_t *child;
117
118 child = child_cfg_create("c", &cfg);
119 peer_b->add_child_cfg(peer_b, child->get_ref(child));
120
121 test_replace(peer_a->replace_child_cfgs(peer_a, peer_b),
122 linked_list_create(),
123 linked_list_create_with_items(child, NULL));
124 test_child_cfgs(peer_a,
125 linked_list_create_with_items(child, NULL));
126
127 child->destroy(child);
128 }
129 END_TEST
130
131 START_TEST(replace_child_cfgs_same)
132 {
133 child_cfg_create_t cfg = {};
134 child_cfg_t *child;
135
136 child = child_cfg_create("c", &cfg);
137 peer_a->add_child_cfg(peer_a, child->get_ref(child));
138 peer_b->add_child_cfg(peer_b, child->get_ref(child));
139
140 test_replace(peer_a->replace_child_cfgs(peer_a, peer_b),
141 linked_list_create(),
142 linked_list_create());
143 test_child_cfgs(peer_a,
144 linked_list_create_with_items(child, NULL));
145
146 child->destroy(child);
147 }
148 END_TEST
149
150 START_TEST(replace_child_cfgs_same_replace)
151 {
152 child_cfg_create_t cfg = {};
153 child_cfg_t *c1, *c2;
154
155 c1 = child_cfg_create("c1", &cfg);
156 peer_a->add_child_cfg(peer_a, c1->get_ref(c1));
157 c2 = child_cfg_create("c2", &cfg);
158 peer_b->add_child_cfg(peer_b, c2->get_ref(c2));
159
160 test_replace(peer_a->replace_child_cfgs(peer_a, peer_b),
161 linked_list_create(),
162 linked_list_create());
163 test_child_cfgs(peer_a,
164 linked_list_create_with_items(c2, NULL));
165
166 c1->destroy(c1);
167 c2->destroy(c2);
168 }
169 END_TEST
170
171 START_TEST(replace_child_cfgs_clear)
172 {
173 child_cfg_create_t cfg = {};
174 child_cfg_t *child;
175
176 child = child_cfg_create("c", &cfg);
177 peer_a->add_child_cfg(peer_a, child->get_ref(child));
178
179 test_replace(peer_a->replace_child_cfgs(peer_a, peer_b),
180 linked_list_create_with_items(child, NULL),
181 linked_list_create());
182 test_child_cfgs(peer_a,
183 linked_list_create());
184
185 child->destroy(child);
186 }
187 END_TEST
188
189 START_TEST(replace_child_cfgs_mixed)
190 {
191 child_cfg_create_t cfg1 = {}, cfg2 = { .mode = MODE_TUNNEL, };
192 child_cfg_create_t cfg3 = { .mode = MODE_TRANSPORT};
193 child_cfg_t *c1, *c2, *c3, *c4;
194
195 c1 = child_cfg_create("c1", &cfg1);
196 peer_a->add_child_cfg(peer_a, c1->get_ref(c1));
197 c2 = child_cfg_create("c2", &cfg2);
198 peer_a->add_child_cfg(peer_a, c2->get_ref(c2));
199
200 c3 = child_cfg_create("c3", &cfg3);
201 peer_b->add_child_cfg(peer_b, c3->get_ref(c3));
202 c4 = child_cfg_create("c4", &cfg2);
203 peer_b->add_child_cfg(peer_b, c4->get_ref(c4));
204
205 test_replace(peer_a->replace_child_cfgs(peer_a, peer_b),
206 linked_list_create_with_items(c1, NULL),
207 linked_list_create_with_items(c3, NULL));
208 test_child_cfgs(peer_a,
209 linked_list_create_with_items(c3, c4, NULL));
210
211 c1->destroy(c1);
212 c2->destroy(c2);
213 c3->destroy(c3);
214 c4->destroy(c4);
215 }
216 END_TEST
217
218 Suite *peer_cfg_suite_create()
219 {
220 Suite *s;
221 TCase *tc;
222
223 s = suite_create("peer_cfg");
224
225 tc = tcase_create("replace_child_cfgs");
226 tcase_add_checked_fixture(tc, setup_replace, teardown_replace);
227 tcase_add_test(tc, replace_child_cfgs_empty);
228 tcase_add_test(tc, replace_child_cfgs_same);
229 tcase_add_test(tc, replace_child_cfgs_same_replace);
230 tcase_add_test(tc, replace_child_cfgs_clear);
231 tcase_add_test(tc, replace_child_cfgs_mixed);
232 suite_add_tcase(s, tc);
233
234 return s;
235 }