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