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