summaryrefslogtreecommitdiff
path: root/src/charon-tkm/tests/id_manager_tests.c
blob: fb5e56a054d663eba426c890d36b4bc148f9d029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (C) 2012 Reto Buerki
 * Copyright (C) 2012 Adrian-Ken Rueegsegger
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <tests/test_suite.h>

#include "tkm_id_manager.h"

static const tkm_limits_t limits = {125, 100, 55, 30, 200, 42};

START_TEST(test_id_mgr_creation)
{
	tkm_id_manager_t *idmgr = NULL;

	idmgr = tkm_id_manager_create(limits);
	fail_if(idmgr == NULL, "Error creating tkm id manager");

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_acquire_id)
{
	int i, id = 0;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	for (i = 0; i < TKM_CTX_MAX; i++)
	{
		id = idmgr->acquire_id(idmgr, i);
		fail_unless(id > 0, "Error acquiring id of context kind %d", i);

		/* Reset test variable */
		id = 0;
	}

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_acquire_id_invalid_kind)
{
	int id = 0;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	id = idmgr->acquire_id(idmgr, TKM_CTX_MAX);
	fail_unless(id == 0, "Acquired id for invalid context kind %d", TKM_CTX_MAX);

	/* Reset test variable */
	id = 0;

	id = idmgr->acquire_id(idmgr, -1);
	fail_unless(id == 0, "Acquired id for invalid context kind %d", -1);

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_acquire_id_same)
{
	int id1 = 0, id2 = 0;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	id1 = idmgr->acquire_id(idmgr, TKM_CTX_NONCE);
	fail_unless(id1 > 0, "Unable to acquire first id");

	/* Acquire another id, must be different than first */
	id2 = idmgr->acquire_id(idmgr, TKM_CTX_NONCE);
	fail_unless(id2 > 0, "Unable to acquire second id");
	fail_unless(id1 != id2, "Same id received twice");

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_acquire_ref)
{
	int i, id = 0;
	bool acquired = false;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	for (i = 0; i < TKM_CTX_MAX; i++)
	{
		id = idmgr->acquire_id(idmgr, i);
		acquired = idmgr->acquire_ref(idmgr, i, id);
		fail_unless(acquired, "Error acquiring reference context kind %d", i);

		/* Reset test variable */
		acquired = false;
	}

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_acquire_ref_invalid_kind)
{
	bool acquired;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	acquired = idmgr->acquire_ref(idmgr, TKM_CTX_MAX, 1);
	fail_if(acquired, "Acquired reference for invalid context kind %d", TKM_CTX_MAX);

	/* Reset test variable */
	acquired = 0;

	acquired = idmgr->acquire_ref(idmgr, -1, 1);
	fail_if(acquired, "Acquired reference for invalid context kind %d", -1);

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_acquire_ref_invalid_id)
{
	int i;
	bool acquired;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	for (i = 0; i < TKM_CTX_MAX; i++)
	{
		acquired = idmgr->acquire_ref(idmgr, i, -1);
		fail_if(acquired,
				"Acquired reference for negative id of context kind %d", i);

		/* Reset test variable */
		acquired = false;

		acquired = idmgr->acquire_ref(idmgr, i, limits[i] + 1);
		fail_if(acquired,
				"Acquired reference exceeding limit of context kind %d", i);

		/* Reset test variable */
		acquired = false;
	}

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_release_id)
{
	int i, count, id = 0;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	for (i = 0; i < TKM_CTX_MAX; i++)
	{
		id = idmgr->acquire_id(idmgr, i);
		count = idmgr->release_id(idmgr, i, id);

		fail_unless(count == 0, "Error releasing id of context kind %d", i);

		/* Reset count variable */
		count = 0;
	}

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_release_id_invalid_kind)
{
	int count = 0;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	count = idmgr->release_id(idmgr, TKM_CTX_MAX, 1);
	fail_if(count >= 0, "Released id for invalid context kind %d", TKM_CTX_MAX);

	/* Reset test variable */
	count = 0;

	count = idmgr->release_id(idmgr, -1, 1);
	fail_if(count >= 0, "Released id for invalid context kind %d", -1);

	idmgr->destroy(idmgr);
}
END_TEST

START_TEST(test_release_id_nonexistent)
{
	int count = 0;
	tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);

	count = idmgr->release_id(idmgr, TKM_CTX_NONCE, 1);
	fail_unless(count == 0, "Release of nonexistent id failed");

	idmgr->destroy(idmgr);
}
END_TEST

Suite *make_id_manager_tests()
{
	Suite *s;
	TCase *tc;

	s = suite_create("context id manager");

	tc = tcase_create("creation");
	tcase_add_test(tc, test_id_mgr_creation);
	suite_add_tcase(s, tc);

	tc = tcase_create("acquire");
	tcase_add_test(tc, test_acquire_id);
	tcase_add_test(tc, test_acquire_id_invalid_kind);
	tcase_add_test(tc, test_acquire_id_same);
	tcase_add_test(tc, test_acquire_ref);
	tcase_add_test(tc, test_acquire_ref_invalid_kind);
	tcase_add_test(tc, test_acquire_ref_invalid_id);
	suite_add_tcase(s, tc);

	tc = tcase_create("release");
	tcase_add_test(tc, test_release_id);
	tcase_add_test(tc, test_release_id_invalid_kind);
	tcase_add_test(tc, test_release_id_nonexistent);
	suite_add_tcase(s, tc);

	return s;
}