gmp: Support of SHA-3 RSA signatures
[strongswan.git] / src / libstrongswan / tests / suites / test_utils.c
1 /*
2 * Copyright (C) 2013-2015 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 <library.h>
19 #include <utils/utils.h>
20 #include <ipsec/ipsec_types.h>
21 #include <credentials/keys/public_key.h>
22
23 #include <time.h>
24
25 /*******************************************************************************
26 * object storage on lib
27 */
28
29 START_TEST(test_objects)
30 {
31 char *k1 = "key1", *k2 = "key2";
32 char *v1 = "val1", *val;
33
34 ck_assert(lib->get(lib, k1) == NULL);
35
36 ck_assert(lib->set(lib, k1, v1));
37 ck_assert(!lib->set(lib, k1, v1));
38
39 val = lib->get(lib, k1);
40 ck_assert(val != NULL);
41 ck_assert(streq(val, v1));
42
43 ck_assert(lib->set(lib, k1, NULL));
44 ck_assert(!lib->set(lib, k2, NULL));
45
46 ck_assert(lib->get(lib, k1) == NULL);
47 }
48 END_TEST
49
50 /*******************************************************************************
51 * test return_... functions
52 */
53
54 START_TEST(test_return_functions)
55 {
56 ck_assert(return_null() == NULL);
57 ck_assert(return_null("asdf", 5, NULL, 1, "qwer") == NULL);
58
59 ck_assert(return_true() == TRUE);
60 ck_assert(return_true("asdf", 5, NULL, 1, "qwer") == TRUE);
61
62 ck_assert(return_false() == FALSE);
63 ck_assert(return_false("asdf", 5, NULL, 1, "qwer") == FALSE);
64
65 ck_assert(return_failed() == FAILED);
66 ck_assert(return_failed("asdf", 5, NULL, 1, "qwer") == FAILED);
67
68 ck_assert(return_success() == SUCCESS);
69 ck_assert(return_success("asdf", 5, NULL, 1, "qwer") == SUCCESS);
70
71 /* just make sure this works */
72 nop();
73 nop("asdf", 5, NULL, 1, "qwer");
74 }
75 END_TEST
76
77 /*******************************************************************************
78 * timeval_add_ms
79 */
80
81 START_TEST(test_timeval_add_ms)
82 {
83 timeval_t tv;
84
85 tv.tv_sec = 0;
86 tv.tv_usec = 0;
87 timeval_add_ms(&tv, 0);
88 ck_assert_int_eq(tv.tv_sec, 0);
89 ck_assert_int_eq(tv.tv_usec, 0);
90
91 timeval_add_ms(&tv, 1);
92 ck_assert_int_eq(tv.tv_sec, 0);
93 ck_assert_int_eq(tv.tv_usec, 1000);
94
95 timeval_add_ms(&tv, 0);
96 ck_assert_int_eq(tv.tv_sec, 0);
97 ck_assert_int_eq(tv.tv_usec, 1000);
98
99 timeval_add_ms(&tv, 999);
100 ck_assert_int_eq(tv.tv_sec, 1);
101 ck_assert_int_eq(tv.tv_usec, 0);
102
103 timeval_add_ms(&tv, 0);
104 ck_assert_int_eq(tv.tv_sec, 1);
105 ck_assert_int_eq(tv.tv_usec, 0);
106
107 timeval_add_ms(&tv, 1000);
108 ck_assert_int_eq(tv.tv_sec, 2);
109 ck_assert_int_eq(tv.tv_usec, 0);
110
111 timeval_add_ms(&tv, 1500);
112 ck_assert_int_eq(tv.tv_sec, 3);
113 ck_assert_int_eq(tv.tv_usec, 500000);
114 }
115 END_TEST
116
117 /*******************************************************************************
118 * htoun/untoh
119 */
120
121 START_TEST(test_htoun)
122 {
123 chunk_t net64, expected;
124 uint16_t host16 = 513;
125 uint32_t net16 = 0, host32 = 67305985;
126 uint64_t net32 = 0, host64 = 578437695752307201ULL;
127
128 net64 = chunk_alloca(16);
129 memset(net64.ptr, 0, net64.len);
130
131 expected = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
132 htoun16((char*)&net16 + 1, host16);
133 ck_assert(chunk_equals(expected, chunk_from_thing(net16)));
134
135 expected = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
136 htoun32((uint16_t*)&net32 + 1, host32);
137 ck_assert(chunk_equals(expected, chunk_from_thing(net32)));
138
139 expected = chunk_from_chars(0x00, 0x00, 0x00, 0x00,
140 0x08, 0x07, 0x06, 0x05,
141 0x04, 0x03, 0x02, 0x01,
142 0x00, 0x00, 0x00, 0x00);
143 htoun64((uint32_t*)net64.ptr + 1, host64);
144 ck_assert(chunk_equals(expected, net64));
145 }
146 END_TEST
147
148 START_TEST(test_untoh)
149 {
150 chunk_t net;
151 uint16_t host16;
152 uint32_t host32;
153 uint64_t host64;
154
155 net = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
156 host16 = untoh16(net.ptr + 1);
157 ck_assert(host16 == 513);
158
159 net = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
160 host32 = untoh32(net.ptr + 2);
161 ck_assert(host32 == 67305985);
162
163 net = chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05,
164 0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00);
165 host64 = untoh64(net.ptr + 4);
166 ck_assert(host64 == 578437695752307201ULL);
167 }
168 END_TEST
169
170 /*******************************************************************************
171 * pad_len/round_up/down
172 */
173
174 START_TEST(test_round)
175 {
176 ck_assert_int_eq(pad_len(0, 4), 0);
177 ck_assert_int_eq(pad_len(1, 4), 3);
178 ck_assert_int_eq(pad_len(2, 4), 2);
179 ck_assert_int_eq(pad_len(3, 4), 1);
180 ck_assert_int_eq(pad_len(4, 4), 0);
181 ck_assert_int_eq(pad_len(5, 4), 3);
182
183 ck_assert_int_eq(round_up(0, 4), 0);
184 ck_assert_int_eq(round_up(1, 4), 4);
185 ck_assert_int_eq(round_up(2, 4), 4);
186 ck_assert_int_eq(round_up(3, 4), 4);
187 ck_assert_int_eq(round_up(4, 4), 4);
188 ck_assert_int_eq(round_up(5, 4), 8);
189
190 ck_assert_int_eq(round_down(0, 4), 0);
191 ck_assert_int_eq(round_down(1, 4), 0);
192 ck_assert_int_eq(round_down(2, 4), 0);
193 ck_assert_int_eq(round_down(3, 4), 0);
194 ck_assert_int_eq(round_down(4, 4), 4);
195 ck_assert_int_eq(round_down(5, 4), 4);
196 }
197 END_TEST
198
199 /*******************************************************************************
200 * streq
201 */
202
203 static struct {
204 char *a;
205 char *b;
206 bool eq;
207 bool case_eq;
208 } streq_data[] = {
209 {NULL, NULL, TRUE, TRUE},
210 {NULL, "", FALSE, FALSE},
211 {"", NULL, FALSE, FALSE},
212 {"abc", "", FALSE, FALSE},
213 {"abc", "abc", TRUE, TRUE},
214 {"abc", "ABC", FALSE, TRUE},
215 };
216
217 START_TEST(test_streq)
218 {
219 bool eq;
220
221 ck_assert(streq(streq_data[_i].a, streq_data[_i].a));
222 ck_assert(streq(streq_data[_i].b, streq_data[_i].b));
223 eq = streq(streq_data[_i].a, streq_data[_i].b);
224 ck_assert(eq == streq_data[_i].eq);
225
226 ck_assert(strcaseeq(streq_data[_i].a, streq_data[_i].a));
227 ck_assert(strcaseeq(streq_data[_i].b, streq_data[_i].b));
228 eq = strcaseeq(streq_data[_i].a, streq_data[_i].b);
229 ck_assert(eq == streq_data[_i].case_eq);
230 }
231 END_TEST
232
233 /*******************************************************************************
234 * strneq
235 */
236
237 static struct {
238 char *a;
239 char *b;
240 size_t n;
241 bool eq;
242 bool case_eq;
243 } strneq_data[] = {
244 {NULL, NULL, 0, TRUE, TRUE},
245 {NULL, NULL, 10, TRUE, TRUE},
246 {NULL, "", 0, FALSE, FALSE},
247 {"", NULL, 0, FALSE, FALSE},
248 {"abc", "", 0, TRUE, TRUE},
249 {"abc", "", 1, FALSE, FALSE},
250 {"abc", "ab", 1, TRUE, TRUE},
251 {"abc", "ab", 2, TRUE, TRUE},
252 {"abc", "ab", 3, FALSE, FALSE},
253 {"abc", "abc", 3, TRUE, TRUE},
254 {"abc", "abc", 4, TRUE, TRUE},
255 {"abc", "abC", 2, TRUE, TRUE},
256 {"abc", "abC", 3, FALSE, TRUE},
257 };
258
259 START_TEST(test_strneq)
260 {
261 bool eq;
262
263 ck_assert(strneq(strneq_data[_i].a, strneq_data[_i].a, strneq_data[_i].n));
264 ck_assert(strneq(strneq_data[_i].b, strneq_data[_i].b, strneq_data[_i].n));
265 eq = strneq(strneq_data[_i].a, strneq_data[_i].b, strneq_data[_i].n);
266 ck_assert(eq == strneq_data[_i].eq);
267
268 ck_assert(strncaseeq(strneq_data[_i].a, strneq_data[_i].a, strneq_data[_i].n));
269 ck_assert(strncaseeq(strneq_data[_i].b, strneq_data[_i].b, strneq_data[_i].n));
270 eq = strncaseeq(strneq_data[_i].a, strneq_data[_i].b, strneq_data[_i].n);
271 ck_assert(eq == strneq_data[_i].case_eq);
272 }
273 END_TEST
274
275 /*******************************************************************************
276 * strpfx
277 */
278
279 static struct {
280 char *str;
281 char *pfx;
282 bool prefix;
283 bool case_prefix;
284 } strpfx_data[] = {
285 {"", "", TRUE, TRUE},
286 {"abc", "", TRUE, TRUE},
287 {"abc", "a", TRUE, TRUE},
288 {"abc", "ab", TRUE, TRUE},
289 {"abc", "abc", TRUE, TRUE},
290 {"abc", "abcd", FALSE, FALSE},
291 {"abc", "AB", FALSE, TRUE},
292 {"ABC", "ab", FALSE, TRUE},
293 {" abc", "abc", FALSE, FALSE},
294 };
295
296 START_TEST(test_strpfx)
297 {
298 bool prefix;
299
300 prefix = strpfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
301 ck_assert(prefix == strpfx_data[_i].prefix);
302 prefix = strcasepfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
303 ck_assert(prefix == strpfx_data[_i].case_prefix);
304 }
305 END_TEST
306
307 /*******************************************************************************
308 * mallac_align/free_align
309 */
310
311 START_TEST(test_malloc_align)
312 {
313 void *ptr[128][256];
314 int size, align;
315
316 for (size = 0; size < countof(ptr); size++)
317 {
318 for (align = 0; align < countof(ptr[0]); align++)
319 {
320 ptr[size][align] = malloc_align(size, align);
321 if (align)
322 {
323 ck_assert((uintptr_t)ptr[size][align] % align == 0);
324 }
325 if (size)
326 {
327 ck_assert(ptr[size][align]);
328 memset(ptr[size][align], 0xEF, size);
329 }
330 }
331 }
332 for (size = 0; size < countof(ptr); size++)
333 {
334 for (align = 0; align < countof(ptr[0]); align++)
335 {
336 free_align(ptr[size][align]);
337 }
338 }
339 }
340 END_TEST
341
342 /*******************************************************************************
343 * memxor
344 */
345
346 static void do_memxor(chunk_t a, chunk_t b, chunk_t exp)
347 {
348 chunk_t dst;
349
350 dst = chunk_clonea(a);
351 dst.len = b.len;
352 memxor(dst.ptr, b.ptr, b.len);
353 ck_assert(chunk_equals(dst, exp));
354 }
355
356 START_TEST(test_memxor)
357 {
358 chunk_t a, b, dst;
359 int i;
360
361 a = chunk_alloca(64);
362 memset(a.ptr, 0, a.len);
363 b = chunk_alloca(64);
364 for (i = 0; i < 64; i++)
365 {
366 b.ptr[i] = i;
367 b.len = i;
368 do_memxor(a, b, b);
369 }
370 b.len = 64;
371 do_memxor(a, b, b);
372
373 dst = chunk_clonea(a);
374 memxor(dst.ptr, b.ptr, b.len);
375 ck_assert(chunk_equals(dst, b));
376
377 memxor(dst.ptr, b.ptr, 0);
378 memxor(dst.ptr, b.ptr, 1);
379 memxor(dst.ptr + 1, b.ptr + 1, 1);
380 memxor(dst.ptr + 2, b.ptr + 2, b.len - 2);
381 ck_assert(chunk_equals(dst, a));
382 }
383 END_TEST
384
385 START_TEST(test_memxor_aligned)
386 {
387 uint64_t a = 0, b = 0;
388 chunk_t ca, cb;
389 int i;
390
391 ca = chunk_from_thing(a);
392 cb = chunk_from_thing(b);
393
394 for (i = 0; i < 8; i++)
395 {
396 cb.ptr[i] = i + 1;
397 }
398
399 /* 64-bit aligned */
400 memxor(ca.ptr, cb.ptr, 8);
401 ck_assert(a == b);
402 /* 32-bit aligned source */
403 a = 0;
404 memxor(ca.ptr, cb.ptr + 4, 4);
405 ck_assert(chunk_equals(ca, chunk_from_chars(0x05, 0x06, 0x07, 0x08,
406 0x00, 0x00, 0x00, 0x00)));
407 /* 16-bit aligned source */
408 a = 0;
409 memxor(ca.ptr, cb.ptr + 2, 6);
410 ck_assert(chunk_equals(ca, chunk_from_chars(0x03, 0x04, 0x05, 0x06,
411 0x07, 0x08, 0x00, 0x00)));
412 /* 8-bit aligned source */
413 a = 0;
414 memxor(ca.ptr, cb.ptr + 1, 7);
415 ck_assert(chunk_equals(ca, chunk_from_chars(0x02, 0x03, 0x04, 0x05,
416 0x06, 0x07, 0x08, 0x00)));
417 }
418 END_TEST
419
420 /*******************************************************************************
421 * memeq/const
422 */
423
424 static struct {
425 char *a;
426 char *b;
427 size_t n;
428 bool res;
429 } memeq_data[] = {
430 {NULL, NULL, 0, TRUE},
431 {"a", "b", 0, TRUE},
432 {"", "", 1, TRUE},
433 {"abcdefgh", "abcdefgh", 8, TRUE},
434 {"a", "b", 1, FALSE},
435 {"A", "a", 1, FALSE},
436 {"\0a", "\0b", 2, FALSE},
437 {"abc", "abd", 3, FALSE},
438 {"abc", "dbd", 3, FALSE},
439 {"abcdefgh", "abcdffgh", 8, FALSE},
440 {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
441 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, TRUE},
442 {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
443 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyy", 52, FALSE},
444 {"bbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
445 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, FALSE},
446 };
447
448 START_TEST(test_memeq)
449 {
450 ck_assert(memeq(memeq_data[_i].a, memeq_data[_i].b,
451 memeq_data[_i].n) == memeq_data[_i].res);
452 }
453 END_TEST
454
455 START_TEST(test_memeq_const)
456 {
457 ck_assert(memeq_const(memeq_data[_i].a, memeq_data[_i].b,
458 memeq_data[_i].n) == memeq_data[_i].res);
459 }
460 END_TEST
461
462 /*******************************************************************************
463 * memstr
464 */
465
466 static struct {
467 char *haystack;
468 char *needle;
469 size_t n;
470 int offset;
471 } memstr_data[] = {
472 {NULL, NULL, 0, -1},
473 {NULL, NULL, 3, -1},
474 {NULL, "abc", 0, -1},
475 {NULL, "abc", 3, -1},
476 {"", "", 0, -1},
477 {"abc", NULL, 3, -1},
478 {"abc", "", 3, -1},
479 {"abc", "abc", 3, 0},
480 {" abc", "abc", 4, 1},
481 {" abc", "abc", 3, -1},
482 {"abcabc", "abc", 6, 0},
483 {" abc ", "abc", 5, 1},
484 };
485
486 START_TEST(test_memstr)
487 {
488 char *ret;
489
490 ret = memstr(memstr_data[_i].haystack, memstr_data[_i].needle, memstr_data[_i].n);
491 if (memstr_data[_i].offset >= 0)
492 {
493 ck_assert(ret == memstr_data[_i].haystack + memstr_data[_i].offset);
494 }
495 else
496 {
497 ck_assert(ret == NULL);
498 }
499 }
500 END_TEST
501
502 /*******************************************************************************
503 * utils_memrchr
504 */
505
506 static struct {
507 char *s;
508 int c;
509 size_t n;
510 int offset;
511 } memrchr_data[] = {
512 {NULL, 'f', 0, -1},
513 {NULL, 'f', 3, -1},
514 {"", 'f', 0, -1},
515 {"", '\0', 1, 0},
516 {"foo", '\0', 3, -1},
517 {"foo", '\0', 4, 3},
518 {"foo", 'f', 3, 0},
519 {"foo", 'o', 3, 2},
520 {"foo", 'o', 2, 1},
521 {"foo", 'o', 1, -1},
522 {"foo", 'o', 0, -1},
523 {"foo", 'x', 3, -1},
524 };
525
526 START_TEST(test_utils_memrchr)
527 {
528 void *ret;
529
530 ret = utils_memrchr(memrchr_data[_i].s, memrchr_data[_i].c, memrchr_data[_i].n);
531 if (memrchr_data[_i].offset >= 0)
532 {
533 ck_assert(ret == memrchr_data[_i].s + memrchr_data[_i].offset);
534 }
535 else
536 {
537 ck_assert(ret == NULL);
538 }
539 }
540 END_TEST
541
542 /*******************************************************************************
543 * translate
544 */
545
546 static struct {
547 char *in;
548 char *from;
549 char *to;
550 char *out;
551 } translate_data[] = {
552 {NULL, "", "", NULL},
553 {"abc", "", "", "abc"},
554 {"abc", "", "x", "abc"},
555 {"abc", "x", "", "abc"},
556 {"abc", "abc", "xyz", "xyz"},
557 {"aabbcc", "abc", "xyz", "xxyyzz"},
558 {"abbaccb", "abc", "xyz", "xyyxzzy"},
559 {"abxyzc", "abc", "xyz", "xyxyzz"},
560 {"abcdef", "abc", "xyz", "xyzdef"},
561 {"aaa", "abc", "xyz", "xxx"},
562 {"abc", "aaa", "xyz", "xbc"},
563 {"abc", "abc", "xxx", "xxx"},
564 };
565
566 START_TEST(test_translate)
567 {
568 char *str, *ret;
569
570 str = strdupnull(translate_data[_i].in);
571 ret = translate(str, translate_data[_i].from, translate_data[_i].to);
572 ck_assert(ret == str);
573 if (ret != translate_data[_i].out)
574 {
575 ck_assert_str_eq(str, translate_data[_i].out);
576 }
577 free(str);
578 }
579 END_TEST
580
581 /*******************************************************************************
582 * strreplace
583 */
584
585 static struct {
586 char *in;
587 char *out;
588 char *search;
589 char *replace;
590 bool allocated;
591 } strreplace_data[] = {
592 /* invalid arguments */
593 {NULL, NULL, NULL, NULL, FALSE},
594 {"", "", NULL, NULL, FALSE},
595 {"", "", "", NULL, FALSE},
596 {"", "", NULL, "", FALSE},
597 {"", "", "", "", FALSE},
598 {"", "", "", "asdf", FALSE},
599 {"", "", "asdf", "", FALSE},
600 {"asdf", "asdf", NULL, NULL, FALSE},
601 {"asdf", "asdf", "", NULL, FALSE},
602 {"asdf", "asdf", NULL, "", FALSE},
603 {"asdf", "asdf", "", "", FALSE},
604 {"asdf", "asdf", "", "asdf", FALSE},
605 {"asdf", "asdf", "asdf", NULL, FALSE},
606 {"qwer", "qwer", "", "asdf", FALSE},
607 /* replacement shorter */
608 {"asdf", "", "asdf", "", TRUE},
609 {"asdfasdf", "", "asdf", "", TRUE},
610 {"asasdfdf", "asdf", "asdf", "", TRUE},
611 {"asdf", "df", "as", "", TRUE},
612 {"asdf", "as", "df", "", TRUE},
613 {"qwer", "qwer", "asdf", "", FALSE},
614 /* replacement same length */
615 {"a", "b", "a", "b", TRUE},
616 {"aaa", "bbb", "a", "b", TRUE},
617 {"aaa", "bbb", "aaa", "bbb", TRUE},
618 {"asdf", "asdf", "asdf", "asdf", TRUE},
619 {"qwer", "qwer", "asdf", "asdf", FALSE},
620 /* replacement longer */
621 {"asdf", "asdf", "", "asdf", FALSE},
622 {"asdf", "asdfasdf", "asdf", "asdfasdf", TRUE},
623 {"asdf", "asdfsdf", "a", "asdf", TRUE},
624 {"asdf", "asdasdf", "f", "asdf", TRUE},
625 {"aaa", "asdfasdfasdf", "a", "asdf", TRUE},
626 {"qwer", "qwer", "asdf", "asdfasdf", FALSE},
627 /* real examples */
628 {"http://x.org/no/spaces", "http://x.org/no/spaces", " ", "%20", FALSE},
629 {"http://x.org/end ", "http://x.org/end%20", " ", "%20", TRUE},
630 {" http://x.org/start", "%20http://x.org/start", " ", "%20", TRUE},
631 {" http://x.org/both ", "%20http://x.org/both%20", " ", "%20", TRUE},
632 {"http://x.org/ /slash", "http://x.org/%20/slash", " ", "%20", TRUE},
633 {"http://x.org/ /three", "http://x.org/%20%20%20/three", " ", "%20", TRUE},
634 {"http://x.org/ ", "http://x.org/%20%20%20%20%20%20", " ", "%20", TRUE},
635 {"http://x.org/%20/encoded", "http://x.org/%20/encoded", " ", "%20", FALSE},
636 };
637
638 START_TEST(test_strreplace)
639 {
640 char *ret;
641
642 ret = strreplace(strreplace_data[_i].in, strreplace_data[_i].search,
643 strreplace_data[_i].replace);
644 if (ret && strreplace_data[_i].out)
645 {
646 ck_assert_str_eq(ret, strreplace_data[_i].out);
647 }
648 else
649 {
650 ck_assert(ret == strreplace_data[_i].out);
651 }
652 if (strreplace_data[_i].allocated)
653 {
654 ck_assert(ret != strreplace_data[_i].in);
655 free(ret);
656 }
657 else
658 {
659 ck_assert(ret == strreplace_data[_i].in);
660 }
661 }
662 END_TEST
663
664 /*******************************************************************************
665 * path_dirname/basename/absolute
666 */
667
668 static struct {
669 char *path;
670 char *dir;
671 char *base;
672 bool absolute;
673 } path_data[] = {
674 {NULL, ".", ".", FALSE},
675 {"", ".", ".", FALSE},
676 {".", ".", ".", FALSE},
677 {"..", ".", "..", FALSE},
678 #ifdef WIN32
679 {"C:\\", "C:", "C:", TRUE},
680 {"X:\\\\", "X:", "X:", TRUE},
681 {"foo", ".", "foo", FALSE},
682 {"f\\", ".", "f", FALSE},
683 {"foo\\", ".", "foo", FALSE},
684 {"foo\\\\", ".", "foo", FALSE},
685 {"d:\\f", "d:", "f", TRUE},
686 {"C:\\f\\", "C:", "f", TRUE},
687 {"C:\\foo", "C:", "foo", TRUE},
688 {"C:\\foo\\", "C:", "foo", TRUE},
689 {"foo\\bar", "foo", "bar", FALSE},
690 {"foo\\\\bar", "foo", "bar", FALSE},
691 {"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
692 {"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
693 {"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
694 {"\\foo\\bar", "\\foo", "bar", FALSE},
695 {"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
696 #else /* !WIN32 */
697 {"/", "/", "/", TRUE},
698 {"//", "/", "/", TRUE},
699 {"foo", ".", "foo", FALSE},
700 {"f/", ".", "f", FALSE},
701 {"foo/", ".", "foo", FALSE},
702 {"foo//", ".", "foo", FALSE},
703 {"/f", "/", "f", TRUE},
704 {"/f/", "/", "f", TRUE},
705 {"/foo", "/", "foo", TRUE},
706 {"/foo/", "/", "foo", TRUE},
707 {"//foo/", "/", "foo", TRUE},
708 {"foo/bar", "foo", "bar", FALSE},
709 {"foo//bar", "foo", "bar", FALSE},
710 {"/foo/bar", "/foo", "bar", TRUE},
711 {"/foo/bar/", "/foo", "bar", TRUE},
712 {"/foo/bar/baz", "/foo/bar", "baz", TRUE},
713 #endif
714 };
715
716 START_TEST(test_path_dirname)
717 {
718 char *dir;
719
720 dir = path_dirname(path_data[_i].path);
721 ck_assert_str_eq(path_data[_i].dir, dir);
722 free(dir);
723 }
724 END_TEST
725
726 START_TEST(test_path_basename)
727 {
728 char *base;
729
730 base = path_basename(path_data[_i].path);
731 ck_assert_str_eq(path_data[_i].base, base);
732 free(base);
733 }
734 END_TEST
735
736 START_TEST(test_path_absolute)
737 {
738 ck_assert(path_data[_i].absolute == path_absolute(path_data[_i].path));
739 }
740 END_TEST
741
742 /*******************************************************************************
743 * time_printf_hook
744 */
745
746 static struct {
747 time_t in;
748 bool utc;
749 char *out;
750 } time_data[] = {
751 {UNDEFINED_TIME, FALSE, "--- -- --:--:-- ----"},
752 {UNDEFINED_TIME, TRUE , "--- -- --:--:-- UTC ----"},
753 {1, FALSE, "Jan 01 01:00:01 1970"},
754 {1, TRUE , "Jan 01 00:00:01 UTC 1970"},
755 {1341150196, FALSE, "Jul 01 15:43:16 2012"},
756 {1341150196, TRUE , "Jul 01 13:43:16 UTC 2012"},
757 };
758
759 START_TEST(test_time_printf_hook)
760 {
761 char buf[32];
762 int len;
763
764 len = snprintf(buf, sizeof(buf), "%T", &time_data[_i].in, time_data[_i].utc);
765 ck_assert(len >= 0 && len < sizeof(buf));
766 ck_assert_str_eq(buf, time_data[_i].out);
767 }
768 END_TEST
769
770 /*******************************************************************************
771 * time_delta_printf_hook
772 */
773
774 static struct {
775 time_t a;
776 time_t b;
777 char *out;
778 } time_delta_data[] = {
779 {0, 0, "0 seconds"},
780 {0, 1, "1 second"},
781 {0, -1, "1 second"},
782 {1, 0, "1 second"},
783 {0, 2, "2 seconds"},
784 {2, 0, "2 seconds"},
785 {0, 60, "60 seconds"},
786 {0, 120, "120 seconds"},
787 {0, 121, "2 minutes"},
788 {0, 3600, "60 minutes"},
789 {0, 7200, "120 minutes"},
790 {0, 7201, "2 hours"},
791 {0, 86400, "24 hours"},
792 {0, 172800, "48 hours"},
793 {0, 172801, "2 days"},
794 {172801, 86400, "24 hours"},
795 };
796
797 START_TEST(test_time_delta_printf_hook)
798 {
799 char buf[16];
800 int len;
801
802 len = snprintf(buf, sizeof(buf), "%V", &time_delta_data[_i].a, &time_delta_data[_i].b);
803 ck_assert(len >= 0 && len < sizeof(buf));
804 ck_assert_str_eq(buf, time_delta_data[_i].out);
805 }
806 END_TEST
807
808 /*******************************************************************************
809 * mark_from_string
810 */
811
812 static struct {
813 char *s;
814 bool ok;
815 mark_t m;
816 } mark_data[] = {
817 {NULL, FALSE, { 0 }},
818 {"", TRUE, { 0, 0xffffffff }},
819 {"/", TRUE, { 0, 0 }},
820 {"42", TRUE, { 42, 0xffffffff }},
821 {"0x42", TRUE, { 0x42, 0xffffffff }},
822 {"x", FALSE, { 0 }},
823 {"42/", TRUE, { 0, 0 }},
824 {"42/0", TRUE, { 0, 0 }},
825 {"42/x", FALSE, { 0 }},
826 {"42/42", TRUE, { 42, 42 }},
827 {"42/0xff", TRUE, { 42, 0xff }},
828 {"0x42/0xff", TRUE, { 0x42, 0xff }},
829 {"/0xff", TRUE, { 0, 0xff }},
830 {"/x", FALSE, { 0 }},
831 {"x/x", FALSE, { 0 }},
832 {"0xffffffff/0x0000ffff", TRUE, { 0x0000ffff, 0x0000ffff }},
833 {"0xffffffff/0xffffffff", TRUE, { 0xffffffff, 0xffffffff }},
834 };
835
836 START_TEST(test_mark_from_string)
837 {
838 mark_t mark;
839
840 if (mark_from_string(mark_data[_i].s, &mark))
841 {
842 ck_assert_int_eq(mark.value, mark_data[_i].m.value);
843 ck_assert_int_eq(mark.mask, mark_data[_i].m.mask);
844 }
845 else
846 {
847 ck_assert(!mark_data[_i].ok);
848 }
849 }
850 END_TEST
851
852 /*******************************************************************************
853 * signature_schemes_for_key
854 */
855
856 static struct {
857 key_type_t type;
858 int size;
859 signature_scheme_t expected[4];
860 } scheme_data[] = {
861 {KEY_RSA, 1024, { SIGN_RSA_EMSA_PKCS1_SHA2_256, SIGN_RSA_EMSA_PKCS1_SHA2_384,
862 SIGN_RSA_EMSA_PKCS1_SHA2_512, SIGN_UNKNOWN }},
863 {KEY_RSA, 2048, { SIGN_RSA_EMSA_PKCS1_SHA2_256, SIGN_RSA_EMSA_PKCS1_SHA2_384,
864 SIGN_RSA_EMSA_PKCS1_SHA2_512, SIGN_UNKNOWN }},
865 {KEY_RSA, 4096, { SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
866 SIGN_UNKNOWN }},
867 {KEY_RSA, 8192, { SIGN_RSA_EMSA_PKCS1_SHA2_512, SIGN_UNKNOWN }},
868 {KEY_ECDSA, 256, { SIGN_ECDSA_WITH_SHA256_DER, SIGN_ECDSA_WITH_SHA384_DER,
869 SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
870 {KEY_ECDSA, 384, { SIGN_ECDSA_WITH_SHA384_DER, SIGN_ECDSA_WITH_SHA512_DER,
871 SIGN_UNKNOWN }},
872 {KEY_ECDSA, 512, { SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
873 {KEY_BLISS, 128, { SIGN_BLISS_WITH_SHA2_256, SIGN_BLISS_WITH_SHA2_384,
874 SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
875 {KEY_BLISS, 192, { SIGN_BLISS_WITH_SHA2_384, SIGN_BLISS_WITH_SHA2_512,
876 SIGN_UNKNOWN }},
877 {KEY_BLISS, 256, { SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
878 };
879
880 START_TEST(test_signature_schemes_for_key)
881 {
882 enumerator_t *enumerator;
883 signature_scheme_t scheme;
884 int i;
885
886 enumerator = signature_schemes_for_key(scheme_data[_i].type, scheme_data[_i].size);
887 for (i = 0; scheme_data[_i].expected[i] != SIGN_UNKNOWN; i++)
888 {
889 ck_assert(enumerator->enumerate(enumerator, &scheme));
890 ck_assert_int_eq(scheme_data[_i].expected[i], scheme);
891 }
892 ck_assert(!enumerator->enumerate(enumerator, &scheme));
893 enumerator->destroy(enumerator);
894 }
895 END_TEST
896
897 Suite *utils_suite_create()
898 {
899 Suite *s;
900 TCase *tc;
901
902 /* force a timezone to match non-UTC conversions */
903 #ifdef WIN32
904 _putenv("TZ=GST-1GDT");
905 #else
906 setenv("TZ", "Europe/Zurich", 1);
907 #endif
908 tzset();
909
910 s = suite_create("utils");
911
912 tc = tcase_create("objects");
913 tcase_add_test(tc, test_objects);
914 suite_add_tcase(s, tc);
915
916 tc = tcase_create("return functions");
917 tcase_add_test(tc, test_return_functions);
918 suite_add_tcase(s, tc);
919
920 tc = tcase_create("timeval_add_ms");
921 tcase_add_test(tc, test_timeval_add_ms);
922 suite_add_tcase(s, tc);
923
924 tc = tcase_create("htoun,untoh");
925 tcase_add_test(tc, test_htoun);
926 tcase_add_test(tc, test_untoh);
927 suite_add_tcase(s, tc);
928
929 tc = tcase_create("round");
930 tcase_add_test(tc, test_round);
931 suite_add_tcase(s, tc);
932
933 tc = tcase_create("string helper");
934 tcase_add_loop_test(tc, test_streq, 0, countof(streq_data));
935 tcase_add_loop_test(tc, test_strneq, 0, countof(strneq_data));
936 tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data));
937 suite_add_tcase(s, tc);
938
939 tc = tcase_create("malloc_align");
940 tcase_add_test(tc, test_malloc_align);
941 suite_add_tcase(s, tc);
942
943 tc = tcase_create("memxor");
944 tcase_add_test(tc, test_memxor);
945 tcase_add_test(tc, test_memxor_aligned);
946 suite_add_tcase(s, tc);
947
948 tc = tcase_create("memeq");
949 tcase_add_loop_test(tc, test_memeq, 0, countof(memeq_data));
950 tcase_add_loop_test(tc, test_memeq_const, 0, countof(memeq_data));
951 suite_add_tcase(s, tc);
952
953 tc = tcase_create("memstr");
954 tcase_add_loop_test(tc, test_memstr, 0, countof(memstr_data));
955 suite_add_tcase(s, tc);
956
957 tc = tcase_create("utils_memrchr");
958 tcase_add_loop_test(tc, test_utils_memrchr, 0, countof(memrchr_data));
959 suite_add_tcase(s, tc);
960
961 tc = tcase_create("translate");
962 tcase_add_loop_test(tc, test_translate, 0, countof(translate_data));
963 suite_add_tcase(s, tc);
964
965 tc = tcase_create("strreplace");
966 tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
967 suite_add_tcase(s, tc);
968
969 tc = tcase_create("path_dirname");
970 tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
971 suite_add_tcase(s, tc);
972
973 tc = tcase_create("path_basename");
974 tcase_add_loop_test(tc, test_path_basename, 0, countof(path_data));
975 suite_add_tcase(s, tc);
976
977 tc = tcase_create("path_absolute");
978 tcase_add_loop_test(tc, test_path_absolute, 0, countof(path_data));
979 suite_add_tcase(s, tc);
980
981 tc = tcase_create("printf_hooks");
982 tcase_add_loop_test(tc, test_time_printf_hook, 0, countof(time_data));
983 tcase_add_loop_test(tc, test_time_delta_printf_hook, 0, countof(time_delta_data));
984 suite_add_tcase(s, tc);
985
986 tc = tcase_create("mark_from_string");
987 tcase_add_loop_test(tc, test_mark_from_string, 0, countof(mark_data));
988 suite_add_tcase(s, tc);
989
990 tc = tcase_create("signature_schemes_for_key");
991 tcase_add_loop_test(tc, test_signature_schemes_for_key, 0, countof(scheme_data));
992 suite_add_tcase(s, tc);
993
994 return s;
995 }