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