charon-nm: Set DPD/close action to restart and enable indefinite keying tries
[strongswan.git] / src / charon-tkm / tests / id_manager_tests.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * HSR Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include <tests/test_suite.h>
18
19 #include "tkm_id_manager.h"
20
21 static const tkm_limits_t limits = {125, 100, 55, 30, 200, 42};
22
23 START_TEST(test_id_mgr_creation)
24 {
25 tkm_id_manager_t *idmgr = NULL;
26
27 idmgr = tkm_id_manager_create(limits);
28 fail_if(idmgr == NULL, "Error creating tkm id manager");
29
30 idmgr->destroy(idmgr);
31 }
32 END_TEST
33
34 START_TEST(test_acquire_id)
35 {
36 int i, id = 0;
37 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
38
39 for (i = 0; i < TKM_CTX_MAX; i++)
40 {
41 id = idmgr->acquire_id(idmgr, i);
42 fail_unless(id > 0, "Error acquiring id of context kind %d", i);
43
44 /* Reset test variable */
45 id = 0;
46 }
47
48 idmgr->destroy(idmgr);
49 }
50 END_TEST
51
52 START_TEST(test_acquire_id_invalid_kind)
53 {
54 int id = 0;
55 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
56
57 id = idmgr->acquire_id(idmgr, TKM_CTX_MAX);
58 fail_unless(id == 0, "Acquired id for invalid context kind %d", TKM_CTX_MAX);
59
60 /* Reset test variable */
61 id = 0;
62
63 id = idmgr->acquire_id(idmgr, -1);
64 fail_unless(id == 0, "Acquired id for invalid context kind %d", -1);
65
66 idmgr->destroy(idmgr);
67 }
68 END_TEST
69
70 START_TEST(test_acquire_id_same)
71 {
72 int id1 = 0, id2 = 0;
73 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
74
75 id1 = idmgr->acquire_id(idmgr, TKM_CTX_NONCE);
76 fail_unless(id1 > 0, "Unable to acquire first id");
77
78 /* Acquire another id, must be different than first */
79 id2 = idmgr->acquire_id(idmgr, TKM_CTX_NONCE);
80 fail_unless(id2 > 0, "Unable to acquire second id");
81 fail_unless(id1 != id2, "Same id received twice");
82
83 idmgr->destroy(idmgr);
84 }
85 END_TEST
86
87 START_TEST(test_acquire_ref)
88 {
89 int i, id = 0;
90 bool acquired = false;
91 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
92
93 for (i = 0; i < TKM_CTX_MAX; i++)
94 {
95 id = idmgr->acquire_id(idmgr, i);
96 acquired = idmgr->acquire_ref(idmgr, i, id);
97 fail_unless(acquired, "Error acquiring reference context kind %d", i);
98
99 /* Reset test variable */
100 acquired = false;
101 }
102
103 idmgr->destroy(idmgr);
104 }
105 END_TEST
106
107 START_TEST(test_acquire_ref_invalid_kind)
108 {
109 bool acquired;
110 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
111
112 acquired = idmgr->acquire_ref(idmgr, TKM_CTX_MAX, 1);
113 fail_if(acquired, "Acquired reference for invalid context kind %d", TKM_CTX_MAX);
114
115 /* Reset test variable */
116 acquired = 0;
117
118 acquired = idmgr->acquire_ref(idmgr, -1, 1);
119 fail_if(acquired, "Acquired reference for invalid context kind %d", -1);
120
121 idmgr->destroy(idmgr);
122 }
123 END_TEST
124
125 START_TEST(test_acquire_ref_invalid_id)
126 {
127 int i;
128 bool acquired;
129 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
130
131 for (i = 0; i < TKM_CTX_MAX; i++)
132 {
133 acquired = idmgr->acquire_ref(idmgr, i, -1);
134 fail_if(acquired,
135 "Acquired reference for negative id of context kind %d", i);
136
137 /* Reset test variable */
138 acquired = false;
139
140 acquired = idmgr->acquire_ref(idmgr, i, limits[i] + 1);
141 fail_if(acquired,
142 "Acquired reference exceeding limit of context kind %d", i);
143
144 /* Reset test variable */
145 acquired = false;
146 }
147
148 idmgr->destroy(idmgr);
149 }
150 END_TEST
151
152 START_TEST(test_release_id)
153 {
154 int i, count, id = 0;
155 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
156
157 for (i = 0; i < TKM_CTX_MAX; i++)
158 {
159 id = idmgr->acquire_id(idmgr, i);
160 count = idmgr->release_id(idmgr, i, id);
161
162 fail_unless(count == 0, "Error releasing id of context kind %d", i);
163
164 /* Reset count variable */
165 count = 0;
166 }
167
168 idmgr->destroy(idmgr);
169 }
170 END_TEST
171
172 START_TEST(test_release_id_invalid_kind)
173 {
174 int count = 0;
175 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
176
177 count = idmgr->release_id(idmgr, TKM_CTX_MAX, 1);
178 fail_if(count >= 0, "Released id for invalid context kind %d", TKM_CTX_MAX);
179
180 /* Reset test variable */
181 count = 0;
182
183 count = idmgr->release_id(idmgr, -1, 1);
184 fail_if(count >= 0, "Released id for invalid context kind %d", -1);
185
186 idmgr->destroy(idmgr);
187 }
188 END_TEST
189
190 START_TEST(test_release_id_nonexistent)
191 {
192 int count = 0;
193 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
194
195 count = idmgr->release_id(idmgr, TKM_CTX_NONCE, 1);
196 fail_unless(count == 0, "Release of nonexistent id failed");
197
198 idmgr->destroy(idmgr);
199 }
200 END_TEST
201
202 Suite *make_id_manager_tests()
203 {
204 Suite *s;
205 TCase *tc;
206
207 s = suite_create("context id manager");
208
209 tc = tcase_create("creation");
210 tcase_add_test(tc, test_id_mgr_creation);
211 suite_add_tcase(s, tc);
212
213 tc = tcase_create("acquire");
214 tcase_add_test(tc, test_acquire_id);
215 tcase_add_test(tc, test_acquire_id_invalid_kind);
216 tcase_add_test(tc, test_acquire_id_same);
217 tcase_add_test(tc, test_acquire_ref);
218 tcase_add_test(tc, test_acquire_ref_invalid_kind);
219 tcase_add_test(tc, test_acquire_ref_invalid_id);
220 suite_add_tcase(s, tc);
221
222 tc = tcase_create("release");
223 tcase_add_test(tc, test_release_id);
224 tcase_add_test(tc, test_release_id_invalid_kind);
225 tcase_add_test(tc, test_release_id_nonexistent);
226 suite_add_tcase(s, tc);
227
228 return s;
229 }