]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/tests/suites/test_crypto_factory.c
identification: Support prefixes in string constructors for an explicit type
[thirdparty/strongswan.git] / src / libstrongswan / tests / suites / test_crypto_factory.c
1 /*
2 * Copyright (C) 2014 Tobias Brunner
3 * 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 <crypto/crypto_factory.h>
19
20 static rng_t *rng_create(rng_quality_t quality)
21 {
22 rng_quality_t *q = malloc_thing(rng_quality_t);
23 *q = quality;
24 return (rng_t*)q;
25 }
26
27 static rng_t *rng_create_weak(rng_quality_t quality)
28 {
29 ck_assert(quality == RNG_WEAK);
30 return rng_create(RNG_WEAK);
31 }
32
33 static rng_t *rng_create_strong(rng_quality_t quality)
34 {
35 ck_assert(quality <= RNG_STRONG);
36 return rng_create(RNG_STRONG);
37 }
38
39 static rng_t *rng_create_true(rng_quality_t quality)
40 {
41 ck_assert(quality <= RNG_TRUE);
42 return rng_create(RNG_TRUE);
43 }
44
45 static rng_t *rng_create_true_second(rng_quality_t quality)
46 {
47 fail("should never be called");
48 return rng_create(RNG_TRUE);
49 }
50
51 static rng_quality_t rng_weak = RNG_WEAK;
52 static rng_quality_t rng_strong = RNG_STRONG;
53 static rng_quality_t rng_true = RNG_TRUE;
54
55 static struct {
56 rng_quality_t *exp_weak;
57 rng_quality_t *exp_strong;
58 rng_quality_t *exp_true;
59 struct {
60 rng_quality_t *q;
61 rng_constructor_t create;
62 } data[4];
63 } rng_data[] = {
64 { NULL, NULL, NULL, {
65 { NULL, NULL }
66 }},
67 { &rng_weak, NULL, NULL, {
68 { &rng_weak, rng_create_weak },
69 { NULL, NULL }
70 }},
71 { &rng_strong, &rng_strong, NULL, {
72 { &rng_strong, rng_create_strong },
73 { NULL, NULL }
74 }},
75 { &rng_true, &rng_true, &rng_true, {
76 { &rng_true, rng_create_true },
77 { NULL, NULL }
78 }},
79 { &rng_true, &rng_true, &rng_true, {
80 { &rng_true, rng_create_true },
81 { &rng_true, rng_create_true_second },
82 { NULL, NULL }
83 }},
84 { &rng_weak, &rng_true, &rng_true, {
85 { &rng_weak, rng_create_weak },
86 { &rng_true, rng_create_true },
87 { NULL, NULL }
88 }},
89 { &rng_weak, &rng_strong, &rng_true, {
90 { &rng_true, rng_create_true },
91 { &rng_strong, rng_create_strong },
92 { &rng_weak, rng_create_weak },
93 { NULL, NULL }
94 }},
95 { &rng_weak, &rng_strong, &rng_true, {
96 { &rng_weak, rng_create_weak },
97 { &rng_strong, rng_create_strong },
98 { &rng_true, rng_create_true },
99 { NULL, NULL }
100 }},
101 };
102
103 static void verify_rng(crypto_factory_t *factory, rng_quality_t request,
104 rng_quality_t *expected)
105 {
106 rng_quality_t *res;
107
108 res = (rng_quality_t*)factory->create_rng(factory, request);
109 if (!expected)
110 {
111 ck_assert(!res);
112 }
113 else
114 {
115 ck_assert(res);
116 ck_assert_int_eq(*expected, *res);
117 free(res);
118 }
119 }
120
121 START_TEST(test_create_rng)
122 {
123 crypto_factory_t *factory;
124 int i;
125
126 factory = crypto_factory_create();
127 for (i = 0; rng_data[_i].data[i].q; i++)
128 {
129 ck_assert(factory->add_rng(factory, *rng_data[_i].data[i].q, "test",
130 rng_data[_i].data[i].create));
131 }
132 verify_rng(factory, RNG_WEAK, rng_data[_i].exp_weak);
133 verify_rng(factory, RNG_STRONG, rng_data[_i].exp_strong);
134 verify_rng(factory, RNG_TRUE, rng_data[_i].exp_true);
135 for (i = 0; rng_data[_i].data[i].q; i++)
136 {
137 factory->remove_rng(factory, rng_data[_i].data[i].create);
138 }
139 factory->destroy(factory);
140 }
141 END_TEST
142
143 static diffie_hellman_t *dh_create(char *plugin)
144 {
145 return (diffie_hellman_t*)plugin;
146 }
147
148 static diffie_hellman_t *dh_create_modp1024(diffie_hellman_group_t group, ...)
149 {
150 ck_assert(group == MODP_1024_BIT);
151 return dh_create("plugin1");
152 }
153
154 static diffie_hellman_t *dh_create_modp1024_second(diffie_hellman_group_t group,
155 ...)
156 {
157 ck_assert(group == MODP_1024_BIT);
158 return dh_create("plugin2");
159 }
160
161 static diffie_hellman_t *dh_create_modp2048(diffie_hellman_group_t group, ...)
162 {
163 ck_assert(group == MODP_2048_BIT);
164 return dh_create("plugin1");
165 }
166
167 static diffie_hellman_t *dh_create_modp2048_second(diffie_hellman_group_t group,
168 ...)
169 {
170 ck_assert(group == MODP_2048_BIT);
171 return dh_create("plugin2");
172 }
173
174 static struct {
175 char *exp1024;
176 char *exp2048;
177 struct {
178 diffie_hellman_group_t g;
179 dh_constructor_t create;
180 char *plugin;
181 } data[4];
182 } dh_data[] = {
183 { NULL, NULL, {
184 { MODP_NONE, NULL, NULL }
185 }},
186 { "plugin1", NULL, {
187 { MODP_1024_BIT, dh_create_modp1024, "plugin1" },
188 { MODP_NONE, NULL, NULL }
189 }},
190 { "plugin1", NULL, {
191 { MODP_1024_BIT, dh_create_modp1024, "plugin1" },
192 { MODP_1024_BIT, dh_create_modp1024_second, "plugin2" },
193 { MODP_NONE, NULL, NULL }
194 }},
195 { "plugin2", NULL, {
196 { MODP_1024_BIT, dh_create_modp1024_second, "plugin2" },
197 { MODP_1024_BIT, dh_create_modp1024, "plugin1" },
198 { MODP_NONE, NULL, NULL }
199 }},
200 { "plugin1", "plugin1", {
201 { MODP_1024_BIT, dh_create_modp1024, "plugin1" },
202 { MODP_2048_BIT, dh_create_modp2048, "plugin1" },
203 { MODP_NONE, NULL }
204 }},
205 { "plugin1", "plugin1", {
206 { MODP_2048_BIT, dh_create_modp2048, "plugin1" },
207 { MODP_1024_BIT, dh_create_modp1024, "plugin1" },
208 { MODP_NONE, NULL }
209 }},
210 { "plugin1", "plugin1", {
211 { MODP_2048_BIT, dh_create_modp2048, "plugin1" },
212 { MODP_2048_BIT, dh_create_modp2048_second, "plugin2" },
213 { MODP_1024_BIT, dh_create_modp1024, "plugin1" },
214 { MODP_NONE, NULL }
215 }},
216 { "plugin1", "plugin2", {
217 { MODP_2048_BIT, dh_create_modp2048_second, "plugin2" },
218 { MODP_2048_BIT, dh_create_modp2048, "plugin1" },
219 { MODP_1024_BIT, dh_create_modp1024, "plugin1" },
220 { MODP_NONE, NULL }
221 }},
222 };
223
224 static void verify_dh(crypto_factory_t *factory, diffie_hellman_group_t request,
225 char *expected)
226 {
227 char *plugin;
228
229 plugin = (char*)factory->create_dh(factory, request);
230 if (!expected)
231 {
232 ck_assert(!plugin);
233 }
234 else
235 {
236 ck_assert(plugin);
237 ck_assert_str_eq(expected, plugin);
238 }
239 }
240
241 START_TEST(test_create_dh)
242 {
243 enumerator_t *enumerator;
244 crypto_factory_t *factory;
245 diffie_hellman_group_t group;
246 char *plugin;
247 int i, len = 0;
248
249
250 factory = crypto_factory_create();
251 for (i = 0; dh_data[_i].data[i].g != MODP_NONE; i++)
252 {
253 ck_assert(factory->add_dh(factory, dh_data[_i].data[i].g,
254 dh_data[_i].data[i].plugin,
255 dh_data[_i].data[i].create));
256 }
257 verify_dh(factory, MODP_1024_BIT, dh_data[_i].exp1024);
258 verify_dh(factory, MODP_2048_BIT, dh_data[_i].exp2048);
259
260 len = countof(dh_data[_i].data);
261 enumerator = factory->create_dh_enumerator(factory);
262 for (i = 0; enumerator->enumerate(enumerator, &group, &plugin) && i < len;)
263 {
264 ck_assert_int_eq(dh_data[_i].data[i].g, group);
265 while (dh_data[_i].data[i].g == group)
266 { /* skip other entries by the same group */
267 i++;
268 }
269 switch (group)
270 {
271 case MODP_1024_BIT:
272 ck_assert(dh_data[_i].exp1024);
273 ck_assert_str_eq(dh_data[_i].exp1024, plugin);
274 break;
275 case MODP_2048_BIT:
276 ck_assert(dh_data[_i].exp2048);
277 ck_assert_str_eq(dh_data[_i].exp2048, plugin);
278 break;
279 default:
280 fail("unexpected DH group");
281 break;
282 }
283 }
284 ck_assert(!enumerator->enumerate(enumerator));
285 ck_assert_int_eq(dh_data[_i].data[i].g, MODP_NONE);
286 enumerator->destroy(enumerator);
287
288 for (i = 0; dh_data[_i].data[i].g != MODP_NONE; i++)
289 {
290 factory->remove_dh(factory, dh_data[_i].data[i].create);
291 }
292 factory->destroy(factory);
293 }
294 END_TEST
295
296 Suite *crypto_factory_suite_create()
297 {
298 Suite *s;
299 TCase *tc;
300
301 s = suite_create("crypto-factory");
302
303 tc = tcase_create("create_rng");
304 tcase_add_loop_test(tc, test_create_rng, 0, countof(rng_data));
305 suite_add_tcase(s, tc);
306
307 tc = tcase_create("create_dh");
308 tcase_add_loop_test(tc, test_create_dh, 0, countof(dh_data));
309 suite_add_tcase(s, tc);
310
311 return s;
312 }