Introduce TKM specific charon daemon (charon-tkm)
[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 <stdlib.h>
18 #include <check.h>
19
20 #include "tkm_id_manager.h"
21
22 START_TEST(test_id_mgr_creation)
23 {
24 tkm_id_manager_t *idmgr = NULL;
25
26 idmgr = tkm_id_manager_create();
27 fail_if(idmgr == NULL, "Error creating tkm id manager");
28
29 idmgr->destroy(idmgr);
30 }
31 END_TEST
32
33 START_TEST(test_acquire_id)
34 {
35 int i, id = 0;
36 tkm_id_manager_t *idmgr = tkm_id_manager_create();
37
38 for (i = 0; i < TKM_CTX_MAX; i++)
39 {
40 id = idmgr->acquire_id(idmgr, i);
41 fail_unless(id > 0, "Error acquiring id of context kind %d", i);
42
43 /* Reset test variable */
44 id = 0;
45 }
46
47 idmgr->destroy(idmgr);
48 }
49 END_TEST
50
51 START_TEST(test_acquire_id_invalid_kind)
52 {
53 int id = 0;
54 tkm_id_manager_t *idmgr = tkm_id_manager_create();
55
56 id = idmgr->acquire_id(idmgr, TKM_CTX_MAX);
57 fail_unless(id == 0, "Acquired id for invalid context kind %d", TKM_CTX_MAX);
58
59 /* Reset test variable */
60 id = 0;
61
62 id = idmgr->acquire_id(idmgr, -1);
63 fail_unless(id == 0, "Acquired id for invalid context kind %d", -1);
64
65 idmgr->destroy(idmgr);
66 }
67 END_TEST
68
69 START_TEST(test_release_id)
70 {
71 int i, id = 0;
72 bool released = false;
73 tkm_id_manager_t *idmgr = tkm_id_manager_create();
74
75 for (i = 0; i < TKM_CTX_MAX; i++)
76 {
77 id = idmgr->acquire_id(idmgr, i);
78 released = idmgr->release_id(idmgr, i, id);
79
80 fail_unless(released, "Error releasing id of context kind %d", i);
81
82 /* Reset released variable */
83 released = FALSE;
84 }
85
86 idmgr->destroy(idmgr);
87 }
88 END_TEST
89
90 START_TEST(test_release_id_invalid_kind)
91 {
92 bool released = TRUE;
93 tkm_id_manager_t *idmgr = tkm_id_manager_create();
94
95 released = idmgr->release_id(idmgr, TKM_CTX_MAX, 1);
96 fail_if(released, "Released id for invalid context kind %d", TKM_CTX_MAX);
97
98 /* Reset test variable */
99 released = TRUE;
100
101 released = idmgr->release_id(idmgr, -1, 1);
102 fail_if(released, "Released id for invalid context kind %d", -1);
103
104 idmgr->destroy(idmgr);
105 }
106 END_TEST
107
108 START_TEST(test_release_id_nonexistent)
109 {
110 bool released = FALSE;
111 tkm_id_manager_t *idmgr = tkm_id_manager_create();
112
113 released = idmgr->release_id(idmgr, TKM_CTX_NONCE, 1);
114 fail_unless(released, "Release of nonexistent id failed");
115
116 idmgr->destroy(idmgr);
117 }
118 END_TEST
119
120 TCase *make_id_manager_tests(void)
121 {
122 TCase *tc = tcase_create("Context id manager tests");
123 tcase_add_test(tc, test_id_mgr_creation);
124 tcase_add_test(tc, test_acquire_id);
125 tcase_add_test(tc, test_acquire_id_invalid_kind);
126 tcase_add_test(tc, test_release_id);
127 tcase_add_test(tc, test_release_id_invalid_kind);
128 tcase_add_test(tc, test_release_id_nonexistent);
129
130 return tc;
131 }