b38f2cb52120787dcb8b35ae82e19b55124b04b2
[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 u_int16_t host16 = 513;
125 u_int32_t net16 = 0, host32 = 67305985;
126 u_int64_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((u_int16_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((u_int32_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 u_int16_t host16;
152 u_int32_t host32;
153 u_int64_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 * strpfx
201 */
202
203 static struct {
204 char *str;
205 char *pfx;
206 bool prefix;
207 bool case_prefix;
208 } strpfx_data[] = {
209 {"", "", TRUE, TRUE},
210 {"abc", "", TRUE, TRUE},
211 {"abc", "a", TRUE, TRUE},
212 {"abc", "ab", TRUE, TRUE},
213 {"abc", "abc", TRUE, TRUE},
214 {"abc", "abcd", FALSE, FALSE},
215 {"abc", "AB", FALSE, TRUE},
216 {"ABC", "ab", FALSE, TRUE},
217 {" abc", "abc", FALSE, FALSE},
218 };
219
220 START_TEST(test_strpfx)
221 {
222 bool prefix;
223
224 prefix = strpfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
225 ck_assert(prefix == strpfx_data[_i].prefix);
226 prefix = strcasepfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
227 ck_assert(prefix == strpfx_data[_i].case_prefix);
228 }
229 END_TEST
230
231 /*******************************************************************************
232 * mallac_align/free_align
233 */
234
235 START_TEST(test_malloc_align)
236 {
237 void *ptr[128][256];
238 int size, align;
239
240 for (size = 0; size < countof(ptr); size++)
241 {
242 for (align = 0; align < countof(ptr[0]); align++)
243 {
244 ptr[size][align] = malloc_align(size, align);
245 if (align)
246 {
247 ck_assert((uintptr_t)ptr[size][align] % align == 0);
248 }
249 if (size)
250 {
251 ck_assert(ptr[size][align]);
252 memset(ptr[size][align], 0xEF, size);
253 }
254 }
255 }
256 for (size = 0; size < countof(ptr); size++)
257 {
258 for (align = 0; align < countof(ptr[0]); align++)
259 {
260 free_align(ptr[size][align]);
261 }
262 }
263 }
264 END_TEST
265
266 /*******************************************************************************
267 * memxor
268 */
269
270 static void do_memxor(chunk_t a, chunk_t b, chunk_t exp)
271 {
272 chunk_t dst;
273
274 dst = chunk_clonea(a);
275 dst.len = b.len;
276 memxor(dst.ptr, b.ptr, b.len);
277 ck_assert(chunk_equals(dst, exp));
278 }
279
280 START_TEST(test_memxor)
281 {
282 chunk_t a, b, dst;
283 int i;
284
285 a = chunk_alloca(64);
286 memset(a.ptr, 0, a.len);
287 b = chunk_alloca(64);
288 for (i = 0; i < 64; i++)
289 {
290 b.ptr[i] = i;
291 b.len = i;
292 do_memxor(a, b, b);
293 }
294 b.len = 64;
295 do_memxor(a, b, b);
296
297 dst = chunk_clonea(a);
298 memxor(dst.ptr, b.ptr, b.len);
299 ck_assert(chunk_equals(dst, b));
300
301 memxor(dst.ptr, b.ptr, 0);
302 memxor(dst.ptr, b.ptr, 1);
303 memxor(dst.ptr + 1, b.ptr + 1, 1);
304 memxor(dst.ptr + 2, b.ptr + 2, b.len - 2);
305 ck_assert(chunk_equals(dst, a));
306 }
307 END_TEST
308
309 START_TEST(test_memxor_aligned)
310 {
311 u_int64_t a = 0, b = 0;
312 chunk_t ca, cb;
313 int i;
314
315 ca = chunk_from_thing(a);
316 cb = chunk_from_thing(b);
317
318 for (i = 0; i < 8; i++)
319 {
320 cb.ptr[i] = i + 1;
321 }
322
323 /* 64-bit aligned */
324 memxor(ca.ptr, cb.ptr, 8);
325 ck_assert(a == b);
326 /* 32-bit aligned source */
327 a = 0;
328 memxor(ca.ptr, cb.ptr + 4, 4);
329 ck_assert(chunk_equals(ca, chunk_from_chars(0x05, 0x06, 0x07, 0x08,
330 0x00, 0x00, 0x00, 0x00)));
331 /* 16-bit aligned source */
332 a = 0;
333 memxor(ca.ptr, cb.ptr + 2, 6);
334 ck_assert(chunk_equals(ca, chunk_from_chars(0x03, 0x04, 0x05, 0x06,
335 0x07, 0x08, 0x00, 0x00)));
336 /* 8-bit aligned source */
337 a = 0;
338 memxor(ca.ptr, cb.ptr + 1, 7);
339 ck_assert(chunk_equals(ca, chunk_from_chars(0x02, 0x03, 0x04, 0x05,
340 0x06, 0x07, 0x08, 0x00)));
341 }
342 END_TEST
343
344 /*******************************************************************************
345 * memeq/const
346 */
347
348 static struct {
349 char *a;
350 char *b;
351 size_t n;
352 bool res;
353 } memeq_data[] = {
354 {NULL, NULL, 0, TRUE},
355 {"a", "b", 0, TRUE},
356 {"", "", 1, TRUE},
357 {"abcdefgh", "abcdefgh", 8, TRUE},
358 {"a", "b", 1, FALSE},
359 {"A", "a", 1, FALSE},
360 {"\0a", "\0b", 2, FALSE},
361 {"abc", "abd", 3, FALSE},
362 {"abc", "dbd", 3, FALSE},
363 {"abcdefgh", "abcdffgh", 8, FALSE},
364 {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
365 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, TRUE},
366 {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
367 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyy", 52, FALSE},
368 {"bbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
369 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, FALSE},
370 };
371
372 START_TEST(test_memeq)
373 {
374 ck_assert(memeq(memeq_data[_i].a, memeq_data[_i].b,
375 memeq_data[_i].n) == memeq_data[_i].res);
376 }
377 END_TEST
378
379 START_TEST(test_memeq_const)
380 {
381 ck_assert(memeq_const(memeq_data[_i].a, memeq_data[_i].b,
382 memeq_data[_i].n) == memeq_data[_i].res);
383 }
384 END_TEST
385
386 /*******************************************************************************
387 * memstr
388 */
389
390 static struct {
391 char *haystack;
392 char *needle;
393 size_t n;
394 int offset;
395 } memstr_data[] = {
396 {NULL, NULL, 0, -1},
397 {NULL, NULL, 3, -1},
398 {NULL, "abc", 0, -1},
399 {NULL, "abc", 3, -1},
400 {"", "", 0, -1},
401 {"abc", NULL, 3, -1},
402 {"abc", "", 3, -1},
403 {"abc", "abc", 3, 0},
404 {" abc", "abc", 4, 1},
405 {" abc", "abc", 3, -1},
406 {"abcabc", "abc", 6, 0},
407 {" abc ", "abc", 5, 1},
408 };
409
410 START_TEST(test_memstr)
411 {
412 char *ret;
413
414 ret = memstr(memstr_data[_i].haystack, memstr_data[_i].needle, memstr_data[_i].n);
415 if (memstr_data[_i].offset >= 0)
416 {
417 ck_assert(ret == memstr_data[_i].haystack + memstr_data[_i].offset);
418 }
419 else
420 {
421 ck_assert(ret == NULL);
422 }
423 }
424 END_TEST
425
426 /*******************************************************************************
427 * utils_memrchr
428 */
429
430 static struct {
431 char *s;
432 int c;
433 size_t n;
434 int offset;
435 } memrchr_data[] = {
436 {NULL, 'f', 0, -1},
437 {NULL, 'f', 3, -1},
438 {"", 'f', 0, -1},
439 {"", '\0', 1, 0},
440 {"foo", '\0', 3, -1},
441 {"foo", '\0', 4, 3},
442 {"foo", 'f', 3, 0},
443 {"foo", 'o', 3, 2},
444 {"foo", 'o', 2, 1},
445 {"foo", 'o', 1, -1},
446 {"foo", 'o', 0, -1},
447 {"foo", 'x', 3, -1},
448 };
449
450 START_TEST(test_utils_memrchr)
451 {
452 void *ret;
453
454 ret = utils_memrchr(memrchr_data[_i].s, memrchr_data[_i].c, memrchr_data[_i].n);
455 if (memrchr_data[_i].offset >= 0)
456 {
457 ck_assert(ret == memrchr_data[_i].s + memrchr_data[_i].offset);
458 }
459 else
460 {
461 ck_assert(ret == NULL);
462 }
463 }
464 END_TEST
465
466 /*******************************************************************************
467 * translate
468 */
469
470 static struct {
471 char *in;
472 char *from;
473 char *to;
474 char *out;
475 } translate_data[] = {
476 {NULL, "", "", NULL},
477 {"abc", "", "", "abc"},
478 {"abc", "", "x", "abc"},
479 {"abc", "x", "", "abc"},
480 {"abc", "abc", "xyz", "xyz"},
481 {"aabbcc", "abc", "xyz", "xxyyzz"},
482 {"abbaccb", "abc", "xyz", "xyyxzzy"},
483 {"abxyzc", "abc", "xyz", "xyxyzz"},
484 {"abcdef", "abc", "xyz", "xyzdef"},
485 {"aaa", "abc", "xyz", "xxx"},
486 {"abc", "aaa", "xyz", "xbc"},
487 {"abc", "abc", "xxx", "xxx"},
488 };
489
490 START_TEST(test_translate)
491 {
492 char *str, *ret;
493
494 str = strdupnull(translate_data[_i].in);
495 ret = translate(str, translate_data[_i].from, translate_data[_i].to);
496 ck_assert(ret == str);
497 if (ret != translate_data[_i].out)
498 {
499 ck_assert_str_eq(str, translate_data[_i].out);
500 }
501 free(str);
502 }
503 END_TEST
504
505 /*******************************************************************************
506 * strreplace
507 */
508
509 static struct {
510 char *in;
511 char *out;
512 char *search;
513 char *replace;
514 bool allocated;
515 } strreplace_data[] = {
516 /* invalid arguments */
517 {NULL, NULL, NULL, NULL, FALSE},
518 {"", "", NULL, NULL, FALSE},
519 {"", "", "", NULL, FALSE},
520 {"", "", NULL, "", FALSE},
521 {"", "", "", "", FALSE},
522 {"", "", "", "asdf", FALSE},
523 {"", "", "asdf", "", FALSE},
524 {"asdf", "asdf", NULL, NULL, FALSE},
525 {"asdf", "asdf", "", NULL, FALSE},
526 {"asdf", "asdf", NULL, "", FALSE},
527 {"asdf", "asdf", "", "", FALSE},
528 {"asdf", "asdf", "", "asdf", FALSE},
529 {"asdf", "asdf", "asdf", NULL, FALSE},
530 {"qwer", "qwer", "", "asdf", FALSE},
531 /* replacement shorter */
532 {"asdf", "", "asdf", "", TRUE},
533 {"asdfasdf", "", "asdf", "", TRUE},
534 {"asasdfdf", "asdf", "asdf", "", TRUE},
535 {"asdf", "df", "as", "", TRUE},
536 {"asdf", "as", "df", "", TRUE},
537 {"qwer", "qwer", "asdf", "", FALSE},
538 /* replacement same length */
539 {"a", "b", "a", "b", TRUE},
540 {"aaa", "bbb", "a", "b", TRUE},
541 {"aaa", "bbb", "aaa", "bbb", TRUE},
542 {"asdf", "asdf", "asdf", "asdf", TRUE},
543 {"qwer", "qwer", "asdf", "asdf", FALSE},
544 /* replacement longer */
545 {"asdf", "asdf", "", "asdf", FALSE},
546 {"asdf", "asdfasdf", "asdf", "asdfasdf", TRUE},
547 {"asdf", "asdfsdf", "a", "asdf", TRUE},
548 {"asdf", "asdasdf", "f", "asdf", TRUE},
549 {"aaa", "asdfasdfasdf", "a", "asdf", TRUE},
550 {"qwer", "qwer", "asdf", "asdfasdf", FALSE},
551 /* real examples */
552 {"http://x.org/no/spaces", "http://x.org/no/spaces", " ", "%20", FALSE},
553 {"http://x.org/end ", "http://x.org/end%20", " ", "%20", TRUE},
554 {" http://x.org/start", "%20http://x.org/start", " ", "%20", TRUE},
555 {" http://x.org/both ", "%20http://x.org/both%20", " ", "%20", TRUE},
556 {"http://x.org/ /slash", "http://x.org/%20/slash", " ", "%20", TRUE},
557 {"http://x.org/ /three", "http://x.org/%20%20%20/three", " ", "%20", TRUE},
558 {"http://x.org/ ", "http://x.org/%20%20%20%20%20%20", " ", "%20", TRUE},
559 {"http://x.org/%20/encoded", "http://x.org/%20/encoded", " ", "%20", FALSE},
560 };
561
562 START_TEST(test_strreplace)
563 {
564 char *ret;
565
566 ret = strreplace(strreplace_data[_i].in, strreplace_data[_i].search,
567 strreplace_data[_i].replace);
568 if (ret && strreplace_data[_i].out)
569 {
570 ck_assert_str_eq(ret, strreplace_data[_i].out);
571 }
572 else
573 {
574 ck_assert(ret == strreplace_data[_i].out);
575 }
576 if (strreplace_data[_i].allocated)
577 {
578 ck_assert(ret != strreplace_data[_i].in);
579 free(ret);
580 }
581 else
582 {
583 ck_assert(ret == strreplace_data[_i].in);
584 }
585 }
586 END_TEST
587
588 /*******************************************************************************
589 * path_dirname/basename/absolute
590 */
591
592 static struct {
593 char *path;
594 char *dir;
595 char *base;
596 bool absolute;
597 } path_data[] = {
598 {NULL, ".", ".", FALSE},
599 {"", ".", ".", FALSE},
600 {".", ".", ".", FALSE},
601 {"..", ".", "..", FALSE},
602 #ifdef WIN32
603 {"C:\\", "C:", "C:", TRUE},
604 {"X:\\\\", "X:", "X:", TRUE},
605 {"foo", ".", "foo", FALSE},
606 {"f\\", ".", "f", FALSE},
607 {"foo\\", ".", "foo", FALSE},
608 {"foo\\\\", ".", "foo", FALSE},
609 {"d:\\f", "d:", "f", TRUE},
610 {"C:\\f\\", "C:", "f", TRUE},
611 {"C:\\foo", "C:", "foo", TRUE},
612 {"C:\\foo\\", "C:", "foo", TRUE},
613 {"foo\\bar", "foo", "bar", FALSE},
614 {"foo\\\\bar", "foo", "bar", FALSE},
615 {"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
616 {"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
617 {"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
618 {"\\foo\\bar", "\\foo", "bar", FALSE},
619 {"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
620 #else /* !WIN32 */
621 {"/", "/", "/", TRUE},
622 {"//", "/", "/", TRUE},
623 {"foo", ".", "foo", FALSE},
624 {"f/", ".", "f", FALSE},
625 {"foo/", ".", "foo", FALSE},
626 {"foo//", ".", "foo", FALSE},
627 {"/f", "/", "f", TRUE},
628 {"/f/", "/", "f", TRUE},
629 {"/foo", "/", "foo", TRUE},
630 {"/foo/", "/", "foo", TRUE},
631 {"//foo/", "/", "foo", TRUE},
632 {"foo/bar", "foo", "bar", FALSE},
633 {"foo//bar", "foo", "bar", FALSE},
634 {"/foo/bar", "/foo", "bar", TRUE},
635 {"/foo/bar/", "/foo", "bar", TRUE},
636 {"/foo/bar/baz", "/foo/bar", "baz", TRUE},
637 #endif
638 };
639
640 START_TEST(test_path_dirname)
641 {
642 char *dir;
643
644 dir = path_dirname(path_data[_i].path);
645 ck_assert_str_eq(path_data[_i].dir, dir);
646 free(dir);
647 }
648 END_TEST
649
650 START_TEST(test_path_basename)
651 {
652 char *base;
653
654 base = path_basename(path_data[_i].path);
655 ck_assert_str_eq(path_data[_i].base, base);
656 free(base);
657 }
658 END_TEST
659
660 START_TEST(test_path_absolute)
661 {
662 ck_assert(path_data[_i].absolute == path_absolute(path_data[_i].path));
663 }
664 END_TEST
665
666 /*******************************************************************************
667 * time_printf_hook
668 */
669
670 static struct {
671 time_t in;
672 bool utc;
673 char *out;
674 } time_data[] = {
675 {UNDEFINED_TIME, FALSE, "--- -- --:--:-- ----"},
676 {UNDEFINED_TIME, TRUE , "--- -- --:--:-- UTC ----"},
677 {1, FALSE, "Jan 01 01:00:01 1970"},
678 {1, TRUE , "Jan 01 00:00:01 UTC 1970"},
679 {1341150196, FALSE, "Jul 01 15:43:16 2012"},
680 {1341150196, TRUE , "Jul 01 13:43:16 UTC 2012"},
681 };
682
683 START_TEST(test_time_printf_hook)
684 {
685 char buf[32];
686 int len;
687
688 len = snprintf(buf, sizeof(buf), "%T", &time_data[_i].in, time_data[_i].utc);
689 ck_assert(len >= 0 && len < sizeof(buf));
690 ck_assert_str_eq(buf, time_data[_i].out);
691 }
692 END_TEST
693
694 /*******************************************************************************
695 * time_delta_printf_hook
696 */
697
698 static struct {
699 time_t a;
700 time_t b;
701 char *out;
702 } time_delta_data[] = {
703 {0, 0, "0 seconds"},
704 {0, 1, "1 second"},
705 {0, -1, "1 second"},
706 {1, 0, "1 second"},
707 {0, 2, "2 seconds"},
708 {2, 0, "2 seconds"},
709 {0, 60, "60 seconds"},
710 {0, 120, "120 seconds"},
711 {0, 121, "2 minutes"},
712 {0, 3600, "60 minutes"},
713 {0, 7200, "120 minutes"},
714 {0, 7201, "2 hours"},
715 {0, 86400, "24 hours"},
716 {0, 172800, "48 hours"},
717 {0, 172801, "2 days"},
718 {172801, 86400, "24 hours"},
719 };
720
721 START_TEST(test_time_delta_printf_hook)
722 {
723 char buf[16];
724 int len;
725
726 len = snprintf(buf, sizeof(buf), "%V", &time_delta_data[_i].a, &time_delta_data[_i].b);
727 ck_assert(len >= 0 && len < sizeof(buf));
728 ck_assert_str_eq(buf, time_delta_data[_i].out);
729 }
730 END_TEST
731
732 /*******************************************************************************
733 * mark_from_string
734 */
735
736 static struct {
737 char *s;
738 bool ok;
739 mark_t m;
740 } mark_data[] = {
741 {NULL, FALSE, { 0 }},
742 {"", TRUE, { 0, 0xffffffff }},
743 {"/", TRUE, { 0, 0 }},
744 {"42", TRUE, { 42, 0xffffffff }},
745 {"0x42", TRUE, { 0x42, 0xffffffff }},
746 {"x", FALSE, { 0 }},
747 {"42/", TRUE, { 0, 0 }},
748 {"42/0", TRUE, { 0, 0 }},
749 {"42/x", FALSE, { 0 }},
750 {"42/42", TRUE, { 42, 42 }},
751 {"42/0xff", TRUE, { 42, 0xff }},
752 {"0x42/0xff", TRUE, { 0x42, 0xff }},
753 {"/0xff", TRUE, { 0, 0xff }},
754 {"/x", FALSE, { 0 }},
755 {"x/x", FALSE, { 0 }},
756 {"0xffffffff/0x0000ffff", TRUE, { 0x0000ffff, 0x0000ffff }},
757 {"0xffffffff/0xffffffff", TRUE, { 0xffffffff, 0xffffffff }},
758 };
759
760 START_TEST(test_mark_from_string)
761 {
762 mark_t mark;
763
764 if (mark_from_string(mark_data[_i].s, &mark))
765 {
766 ck_assert_int_eq(mark.value, mark_data[_i].m.value);
767 ck_assert_int_eq(mark.mask, mark_data[_i].m.mask);
768 }
769 else
770 {
771 ck_assert(!mark_data[_i].ok);
772 }
773 }
774 END_TEST
775
776 /*******************************************************************************
777 * signature_schemes_for_key
778 */
779
780 static struct {
781 key_type_t type;
782 int size;
783 signature_scheme_t expected[4];
784 } scheme_data[] = {
785 {KEY_RSA, 1024, { SIGN_RSA_EMSA_PKCS1_SHA256, SIGN_RSA_EMSA_PKCS1_SHA384, SIGN_RSA_EMSA_PKCS1_SHA512, SIGN_UNKNOWN }},
786 {KEY_RSA, 2048, { SIGN_RSA_EMSA_PKCS1_SHA256, SIGN_RSA_EMSA_PKCS1_SHA384, SIGN_RSA_EMSA_PKCS1_SHA512, SIGN_UNKNOWN }},
787 {KEY_RSA, 4096, { SIGN_RSA_EMSA_PKCS1_SHA384, SIGN_RSA_EMSA_PKCS1_SHA512, SIGN_UNKNOWN }},
788 {KEY_RSA, 8192, { SIGN_RSA_EMSA_PKCS1_SHA512, SIGN_UNKNOWN }},
789 {KEY_ECDSA, 256, { SIGN_ECDSA_WITH_SHA256_DER, SIGN_ECDSA_WITH_SHA384_DER, SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
790 {KEY_ECDSA, 384, { SIGN_ECDSA_WITH_SHA384_DER, SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
791 {KEY_ECDSA, 512, { SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
792 {KEY_BLISS, 128, { SIGN_BLISS_WITH_SHA256, SIGN_BLISS_WITH_SHA384, SIGN_BLISS_WITH_SHA512, SIGN_UNKNOWN }},
793 {KEY_BLISS, 192, { SIGN_BLISS_WITH_SHA384, SIGN_BLISS_WITH_SHA512, SIGN_UNKNOWN }},
794 {KEY_BLISS, 256, { SIGN_BLISS_WITH_SHA512, SIGN_UNKNOWN }},
795 };
796
797 START_TEST(test_signature_schemes_for_key)
798 {
799 enumerator_t *enumerator;
800 signature_scheme_t scheme;
801 int i;
802
803 enumerator = signature_schemes_for_key(scheme_data[_i].type, scheme_data[_i].size);
804 for (i = 0; scheme_data[_i].expected[i] != SIGN_UNKNOWN; i++)
805 {
806 ck_assert(enumerator->enumerate(enumerator, &scheme));
807 ck_assert_int_eq(scheme_data[_i].expected[i], scheme);
808 }
809 ck_assert(!enumerator->enumerate(enumerator, &scheme));
810 enumerator->destroy(enumerator);
811 }
812 END_TEST
813
814 Suite *utils_suite_create()
815 {
816 Suite *s;
817 TCase *tc;
818
819 /* force a timezone to match non-UTC conversions */
820 #ifdef WIN32
821 _putenv("TZ=GST-1GDT");
822 #else
823 setenv("TZ", "Europe/Zurich", 1);
824 #endif
825 tzset();
826
827 s = suite_create("utils");
828
829 tc = tcase_create("objects");
830 tcase_add_test(tc, test_objects);
831 suite_add_tcase(s, tc);
832
833 tc = tcase_create("return functions");
834 tcase_add_test(tc, test_return_functions);
835 suite_add_tcase(s, tc);
836
837 tc = tcase_create("timeval_add_ms");
838 tcase_add_test(tc, test_timeval_add_ms);
839 suite_add_tcase(s, tc);
840
841 tc = tcase_create("htoun,untoh");
842 tcase_add_test(tc, test_htoun);
843 tcase_add_test(tc, test_untoh);
844 suite_add_tcase(s, tc);
845
846 tc = tcase_create("round");
847 tcase_add_test(tc, test_round);
848 suite_add_tcase(s, tc);
849
850 tc = tcase_create("string helper");
851 tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data));
852 suite_add_tcase(s, tc);
853
854 tc = tcase_create("malloc_align");
855 tcase_add_test(tc, test_malloc_align);
856 suite_add_tcase(s, tc);
857
858 tc = tcase_create("memxor");
859 tcase_add_test(tc, test_memxor);
860 tcase_add_test(tc, test_memxor_aligned);
861 suite_add_tcase(s, tc);
862
863 tc = tcase_create("memeq");
864 tcase_add_loop_test(tc, test_memeq, 0, countof(memeq_data));
865 tcase_add_loop_test(tc, test_memeq_const, 0, countof(memeq_data));
866 suite_add_tcase(s, tc);
867
868 tc = tcase_create("memstr");
869 tcase_add_loop_test(tc, test_memstr, 0, countof(memstr_data));
870 suite_add_tcase(s, tc);
871
872 tc = tcase_create("utils_memrchr");
873 tcase_add_loop_test(tc, test_utils_memrchr, 0, countof(memrchr_data));
874 suite_add_tcase(s, tc);
875
876 tc = tcase_create("translate");
877 tcase_add_loop_test(tc, test_translate, 0, countof(translate_data));
878 suite_add_tcase(s, tc);
879
880 tc = tcase_create("strreplace");
881 tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
882 suite_add_tcase(s, tc);
883
884 tc = tcase_create("path_dirname");
885 tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
886 suite_add_tcase(s, tc);
887
888 tc = tcase_create("path_basename");
889 tcase_add_loop_test(tc, test_path_basename, 0, countof(path_data));
890 suite_add_tcase(s, tc);
891
892 tc = tcase_create("path_absolute");
893 tcase_add_loop_test(tc, test_path_absolute, 0, countof(path_data));
894 suite_add_tcase(s, tc);
895
896 tc = tcase_create("printf_hooks");
897 tcase_add_loop_test(tc, test_time_printf_hook, 0, countof(time_data));
898 tcase_add_loop_test(tc, test_time_delta_printf_hook, 0, countof(time_delta_data));
899 suite_add_tcase(s, tc);
900
901 tc = tcase_create("mark_from_string");
902 tcase_add_loop_test(tc, test_mark_from_string, 0, countof(mark_data));
903 suite_add_tcase(s, tc);
904
905 tc = tcase_create("signature_schemes_for_key");
906 tcase_add_loop_test(tc, test_signature_schemes_for_key, 0, countof(scheme_data));
907 suite_add_tcase(s, tc);
908
909 return s;
910 }