swanctl: Pass optional connection name to --initiate/install/uninstall
[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 * 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_release_id)
88 {
89 int i, id = 0;
90 bool released = 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 released = idmgr->release_id(idmgr, i, id);
97
98 fail_unless(released, "Error releasing id of context kind %d", i);
99
100 /* Reset released variable */
101 released = FALSE;
102 }
103
104 idmgr->destroy(idmgr);
105 }
106 END_TEST
107
108 START_TEST(test_release_id_invalid_kind)
109 {
110 bool released = TRUE;
111 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
112
113 released = idmgr->release_id(idmgr, TKM_CTX_MAX, 1);
114 fail_if(released, "Released id for invalid context kind %d", TKM_CTX_MAX);
115
116 /* Reset test variable */
117 released = TRUE;
118
119 released = idmgr->release_id(idmgr, -1, 1);
120 fail_if(released, "Released id for invalid context kind %d", -1);
121
122 idmgr->destroy(idmgr);
123 }
124 END_TEST
125
126 START_TEST(test_release_id_nonexistent)
127 {
128 bool released = FALSE;
129 tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
130
131 released = idmgr->release_id(idmgr, TKM_CTX_NONCE, 1);
132 fail_unless(released, "Release of nonexistent id failed");
133
134 idmgr->destroy(idmgr);
135 }
136 END_TEST
137
138 Suite *make_id_manager_tests()
139 {
140 Suite *s;
141 TCase *tc;
142
143 s = suite_create("context id manager");
144
145 tc = tcase_create("creation");
146 tcase_add_test(tc, test_id_mgr_creation);
147 suite_add_tcase(s, tc);
148
149 tc = tcase_create("acquire");
150 tcase_add_test(tc, test_acquire_id);
151 tcase_add_test(tc, test_acquire_id_invalid_kind);
152 tcase_add_test(tc, test_acquire_id_same);
153 suite_add_tcase(s, tc);
154
155 tc = tcase_create("release");
156 tcase_add_test(tc, test_release_id);
157 tcase_add_test(tc, test_release_id_invalid_kind);
158 tcase_add_test(tc, test_release_id_nonexistent);
159 suite_add_tcase(s, tc);
160
161 return s;
162 }