]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libstrongswan/tests/suites/test_utils.c
ipsec-types: Add helper to parse interface ID
[thirdparty/strongswan.git] / src / libstrongswan / tests / suites / test_utils.c
CommitLineData
9a8c873e 1/*
1f648d75 2 * Copyright (C) 2013-2015 Tobias Brunner
1b671669 3 * HSR Hochschule fuer Technik Rapperswil
9a8c873e
TB
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
95e99150 16#include "test_suite.h"
9a8c873e
TB
17
18#include <library.h>
af67613e 19#include <utils/utils.h>
434e530f 20#include <ipsec/ipsec_types.h>
1f648d75 21#include <credentials/keys/public_key.h>
9a8c873e 22
df76881f
MW
23#include <time.h>
24
9a8c873e
TB
25/*******************************************************************************
26 * object storage on lib
27 */
28
29START_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}
48END_TEST
49
af67613e
TB
50/*******************************************************************************
51 * test return_... functions
52 */
53
54START_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}
75END_TEST
76
77/*******************************************************************************
78 * timeval_add_ms
79 */
80
81START_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}
115END_TEST
116
b93ebb4e
TB
117/*******************************************************************************
118 * timespan_from_string
119 */
120
121static struct {
122 char *s;
123 char *u;
124 bool v;
125 time_t t;
126} ts_data[] = {
127 {NULL, NULL, FALSE, 0},
128 {"", NULL, FALSE, 0},
129 {"a", NULL, FALSE, 0},
130 {"0", NULL, TRUE, 0},
131 {"5", NULL, TRUE, 5},
132 {"5s", NULL, TRUE, 5},
133 {"5m", NULL, TRUE, 300},
134 {"5ms", NULL, TRUE, 300},
135 {"5h", NULL, TRUE, 18000},
136 {"5d", NULL, TRUE, 432000},
137 {"5x", NULL, FALSE, 0},
138 {"5", "", TRUE, 5},
139 {"5", "m", TRUE, 300},
140 {"5", "ms", TRUE, 300},
141 {"5", "x", FALSE, 0},
142 {"5x", "m", FALSE, 0},
143 {"18446744073709551616", NULL, FALSE, 0},
144};
145
146START_TEST(test_timespan_from_string)
147{
148 time_t val = 42;
149
150 ck_assert(timespan_from_string(ts_data[_i].s, ts_data[_i].u,
151 NULL) == ts_data[_i].v);
152 ck_assert(timespan_from_string(ts_data[_i].s, ts_data[_i].u,
153 &val) == ts_data[_i].v);
154 if (ts_data[_i].v)
155 {
156 ck_assert_int_eq(val, ts_data[_i].t);
157 }
158 else
159 {
160 ck_assert_int_eq(val, 42);
161 }
162}
163END_TEST
164
af67613e
TB
165/*******************************************************************************
166 * htoun/untoh
167 */
168
169START_TEST(test_htoun)
170{
171 chunk_t net64, expected;
b12c53ce
AS
172 uint16_t host16 = 513;
173 uint32_t net16 = 0, host32 = 67305985;
174 uint64_t net32 = 0, host64 = 578437695752307201ULL;
af67613e
TB
175
176 net64 = chunk_alloca(16);
177 memset(net64.ptr, 0, net64.len);
178
179 expected = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
180 htoun16((char*)&net16 + 1, host16);
181 ck_assert(chunk_equals(expected, chunk_from_thing(net16)));
182
183 expected = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
b12c53ce 184 htoun32((uint16_t*)&net32 + 1, host32);
af67613e
TB
185 ck_assert(chunk_equals(expected, chunk_from_thing(net32)));
186
187 expected = chunk_from_chars(0x00, 0x00, 0x00, 0x00,
188 0x08, 0x07, 0x06, 0x05,
189 0x04, 0x03, 0x02, 0x01,
190 0x00, 0x00, 0x00, 0x00);
b12c53ce 191 htoun64((uint32_t*)net64.ptr + 1, host64);
af67613e
TB
192 ck_assert(chunk_equals(expected, net64));
193}
194END_TEST
195
196START_TEST(test_untoh)
197{
198 chunk_t net;
b12c53ce
AS
199 uint16_t host16;
200 uint32_t host32;
201 uint64_t host64;
af67613e
TB
202
203 net = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
204 host16 = untoh16(net.ptr + 1);
205 ck_assert(host16 == 513);
206
207 net = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
208 host32 = untoh32(net.ptr + 2);
209 ck_assert(host32 == 67305985);
210
211 net = chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05,
212 0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00);
213 host64 = untoh64(net.ptr + 4);
52bff138 214 ck_assert(host64 == 578437695752307201ULL);
af67613e
TB
215}
216END_TEST
217
84044f9c 218/*******************************************************************************
812ae898 219 * pad_len/round_up/down
84044f9c
MW
220 */
221
222START_TEST(test_round)
223{
812ae898
TB
224 ck_assert_int_eq(pad_len(0, 4), 0);
225 ck_assert_int_eq(pad_len(1, 4), 3);
226 ck_assert_int_eq(pad_len(2, 4), 2);
227 ck_assert_int_eq(pad_len(3, 4), 1);
228 ck_assert_int_eq(pad_len(4, 4), 0);
229 ck_assert_int_eq(pad_len(5, 4), 3);
230
84044f9c
MW
231 ck_assert_int_eq(round_up(0, 4), 0);
232 ck_assert_int_eq(round_up(1, 4), 4);
233 ck_assert_int_eq(round_up(2, 4), 4);
234 ck_assert_int_eq(round_up(3, 4), 4);
235 ck_assert_int_eq(round_up(4, 4), 4);
236 ck_assert_int_eq(round_up(5, 4), 8);
237
238 ck_assert_int_eq(round_down(0, 4), 0);
239 ck_assert_int_eq(round_down(1, 4), 0);
240 ck_assert_int_eq(round_down(2, 4), 0);
241 ck_assert_int_eq(round_down(3, 4), 0);
242 ck_assert_int_eq(round_down(4, 4), 4);
243 ck_assert_int_eq(round_down(5, 4), 4);
244}
245END_TEST
246
85597f29
TB
247/*******************************************************************************
248 * streq
249 */
250
251static struct {
252 char *a;
253 char *b;
254 bool eq;
255 bool case_eq;
256} streq_data[] = {
257 {NULL, NULL, TRUE, TRUE},
258 {NULL, "", FALSE, FALSE},
259 {"", NULL, FALSE, FALSE},
260 {"abc", "", FALSE, FALSE},
261 {"abc", "abc", TRUE, TRUE},
262 {"abc", "ABC", FALSE, TRUE},
263};
264
265START_TEST(test_streq)
266{
267 bool eq;
268
269 ck_assert(streq(streq_data[_i].a, streq_data[_i].a));
270 ck_assert(streq(streq_data[_i].b, streq_data[_i].b));
271 eq = streq(streq_data[_i].a, streq_data[_i].b);
272 ck_assert(eq == streq_data[_i].eq);
273
274 ck_assert(strcaseeq(streq_data[_i].a, streq_data[_i].a));
275 ck_assert(strcaseeq(streq_data[_i].b, streq_data[_i].b));
276 eq = strcaseeq(streq_data[_i].a, streq_data[_i].b);
277 ck_assert(eq == streq_data[_i].case_eq);
278}
279END_TEST
280
281/*******************************************************************************
282 * strneq
283 */
284
285static struct {
286 char *a;
287 char *b;
288 size_t n;
289 bool eq;
290 bool case_eq;
291} strneq_data[] = {
292 {NULL, NULL, 0, TRUE, TRUE},
293 {NULL, NULL, 10, TRUE, TRUE},
294 {NULL, "", 0, FALSE, FALSE},
295 {"", NULL, 0, FALSE, FALSE},
296 {"abc", "", 0, TRUE, TRUE},
297 {"abc", "", 1, FALSE, FALSE},
298 {"abc", "ab", 1, TRUE, TRUE},
299 {"abc", "ab", 2, TRUE, TRUE},
300 {"abc", "ab", 3, FALSE, FALSE},
301 {"abc", "abc", 3, TRUE, TRUE},
302 {"abc", "abc", 4, TRUE, TRUE},
303 {"abc", "abC", 2, TRUE, TRUE},
304 {"abc", "abC", 3, FALSE, TRUE},
305};
306
307START_TEST(test_strneq)
308{
309 bool eq;
310
311 ck_assert(strneq(strneq_data[_i].a, strneq_data[_i].a, strneq_data[_i].n));
312 ck_assert(strneq(strneq_data[_i].b, strneq_data[_i].b, strneq_data[_i].n));
313 eq = strneq(strneq_data[_i].a, strneq_data[_i].b, strneq_data[_i].n);
314 ck_assert(eq == strneq_data[_i].eq);
315
316 ck_assert(strncaseeq(strneq_data[_i].a, strneq_data[_i].a, strneq_data[_i].n));
317 ck_assert(strncaseeq(strneq_data[_i].b, strneq_data[_i].b, strneq_data[_i].n));
318 eq = strncaseeq(strneq_data[_i].a, strneq_data[_i].b, strneq_data[_i].n);
319 ck_assert(eq == strneq_data[_i].case_eq);
320}
321END_TEST
322
32a145fd
TB
323/*******************************************************************************
324 * strpfx
325 */
326
327static struct {
328 char *str;
329 char *pfx;
330 bool prefix;
331 bool case_prefix;
332} strpfx_data[] = {
333 {"", "", TRUE, TRUE},
334 {"abc", "", TRUE, TRUE},
335 {"abc", "a", TRUE, TRUE},
336 {"abc", "ab", TRUE, TRUE},
337 {"abc", "abc", TRUE, TRUE},
338 {"abc", "abcd", FALSE, FALSE},
339 {"abc", "AB", FALSE, TRUE},
340 {"ABC", "ab", FALSE, TRUE},
341 {" abc", "abc", FALSE, FALSE},
342};
343
344START_TEST(test_strpfx)
345{
346 bool prefix;
347
348 prefix = strpfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
349 ck_assert(prefix == strpfx_data[_i].prefix);
350 prefix = strcasepfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
351 ck_assert(prefix == strpfx_data[_i].case_prefix);
352}
353END_TEST
354
f206bb74
MW
355/*******************************************************************************
356 * mallac_align/free_align
357 */
358
359START_TEST(test_malloc_align)
360{
361 void *ptr[128][256];
362 int size, align;
363
364 for (size = 0; size < countof(ptr); size++)
365 {
366 for (align = 0; align < countof(ptr[0]); align++)
367 {
368 ptr[size][align] = malloc_align(size, align);
369 if (align)
370 {
371 ck_assert((uintptr_t)ptr[size][align] % align == 0);
372 }
373 if (size)
374 {
375 ck_assert(ptr[size][align]);
376 memset(ptr[size][align], 0xEF, size);
377 }
378 }
379 }
380 for (size = 0; size < countof(ptr); size++)
381 {
382 for (align = 0; align < countof(ptr[0]); align++)
383 {
384 free_align(ptr[size][align]);
385 }
386 }
387}
388END_TEST
389
af67613e
TB
390/*******************************************************************************
391 * memxor
392 */
393
394static void do_memxor(chunk_t a, chunk_t b, chunk_t exp)
395{
396 chunk_t dst;
397
398 dst = chunk_clonea(a);
399 dst.len = b.len;
400 memxor(dst.ptr, b.ptr, b.len);
401 ck_assert(chunk_equals(dst, exp));
402}
403
404START_TEST(test_memxor)
405{
406 chunk_t a, b, dst;
407 int i;
408
409 a = chunk_alloca(64);
410 memset(a.ptr, 0, a.len);
411 b = chunk_alloca(64);
412 for (i = 0; i < 64; i++)
413 {
414 b.ptr[i] = i;
415 b.len = i;
416 do_memxor(a, b, b);
417 }
418 b.len = 64;
419 do_memxor(a, b, b);
420
421 dst = chunk_clonea(a);
422 memxor(dst.ptr, b.ptr, b.len);
423 ck_assert(chunk_equals(dst, b));
424
425 memxor(dst.ptr, b.ptr, 0);
426 memxor(dst.ptr, b.ptr, 1);
427 memxor(dst.ptr + 1, b.ptr + 1, 1);
428 memxor(dst.ptr + 2, b.ptr + 2, b.len - 2);
429 ck_assert(chunk_equals(dst, a));
430}
431END_TEST
432
433START_TEST(test_memxor_aligned)
434{
b12c53ce 435 uint64_t a = 0, b = 0;
af67613e
TB
436 chunk_t ca, cb;
437 int i;
438
439 ca = chunk_from_thing(a);
440 cb = chunk_from_thing(b);
441
442 for (i = 0; i < 8; i++)
443 {
444 cb.ptr[i] = i + 1;
445 }
446
447 /* 64-bit aligned */
448 memxor(ca.ptr, cb.ptr, 8);
449 ck_assert(a == b);
450 /* 32-bit aligned source */
451 a = 0;
452 memxor(ca.ptr, cb.ptr + 4, 4);
453 ck_assert(chunk_equals(ca, chunk_from_chars(0x05, 0x06, 0x07, 0x08,
454 0x00, 0x00, 0x00, 0x00)));
455 /* 16-bit aligned source */
456 a = 0;
457 memxor(ca.ptr, cb.ptr + 2, 6);
458 ck_assert(chunk_equals(ca, chunk_from_chars(0x03, 0x04, 0x05, 0x06,
459 0x07, 0x08, 0x00, 0x00)));
460 /* 8-bit aligned source */
461 a = 0;
462 memxor(ca.ptr, cb.ptr + 1, 7);
463 ck_assert(chunk_equals(ca, chunk_from_chars(0x02, 0x03, 0x04, 0x05,
464 0x06, 0x07, 0x08, 0x00)));
465}
466END_TEST
467
b8339632
MW
468/*******************************************************************************
469 * memeq/const
470 */
471
472static struct {
473 char *a;
474 char *b;
475 size_t n;
476 bool res;
477} memeq_data[] = {
478 {NULL, NULL, 0, TRUE},
479 {"a", "b", 0, TRUE},
480 {"", "", 1, TRUE},
481 {"abcdefgh", "abcdefgh", 8, TRUE},
482 {"a", "b", 1, FALSE},
483 {"A", "a", 1, FALSE},
484 {"\0a", "\0b", 2, FALSE},
485 {"abc", "abd", 3, FALSE},
486 {"abc", "dbd", 3, FALSE},
487 {"abcdefgh", "abcdffgh", 8, FALSE},
488 {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
489 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, TRUE},
490 {"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
491 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyy", 52, FALSE},
492 {"bbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
493 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52, FALSE},
494};
495
496START_TEST(test_memeq)
497{
498 ck_assert(memeq(memeq_data[_i].a, memeq_data[_i].b,
499 memeq_data[_i].n) == memeq_data[_i].res);
500}
501END_TEST
502
503START_TEST(test_memeq_const)
504{
505 ck_assert(memeq_const(memeq_data[_i].a, memeq_data[_i].b,
506 memeq_data[_i].n) == memeq_data[_i].res);
507}
508END_TEST
509
af67613e
TB
510/*******************************************************************************
511 * memstr
512 */
513
514static struct {
515 char *haystack;
516 char *needle;
517 size_t n;
518 int offset;
519} memstr_data[] = {
520 {NULL, NULL, 0, -1},
521 {NULL, NULL, 3, -1},
522 {NULL, "abc", 0, -1},
523 {NULL, "abc", 3, -1},
524 {"", "", 0, -1},
525 {"abc", NULL, 3, -1},
526 {"abc", "", 3, -1},
527 {"abc", "abc", 3, 0},
528 {" abc", "abc", 4, 1},
529 {" abc", "abc", 3, -1},
530 {"abcabc", "abc", 6, 0},
531 {" abc ", "abc", 5, 1},
532};
533
534START_TEST(test_memstr)
535{
536 char *ret;
537
538 ret = memstr(memstr_data[_i].haystack, memstr_data[_i].needle, memstr_data[_i].n);
539 if (memstr_data[_i].offset >= 0)
540 {
541 ck_assert(ret == memstr_data[_i].haystack + memstr_data[_i].offset);
542 }
543 else
544 {
545 ck_assert(ret == NULL);
546 }
547}
548END_TEST
549
2ed241ae
TB
550/*******************************************************************************
551 * utils_memrchr
552 */
553
554static struct {
555 char *s;
556 int c;
557 size_t n;
558 int offset;
559} memrchr_data[] = {
560 {NULL, 'f', 0, -1},
561 {NULL, 'f', 3, -1},
562 {"", 'f', 0, -1},
563 {"", '\0', 1, 0},
564 {"foo", '\0', 3, -1},
565 {"foo", '\0', 4, 3},
566 {"foo", 'f', 3, 0},
567 {"foo", 'o', 3, 2},
568 {"foo", 'o', 2, 1},
569 {"foo", 'o', 1, -1},
570 {"foo", 'o', 0, -1},
571 {"foo", 'x', 3, -1},
572};
573
574START_TEST(test_utils_memrchr)
575{
576 void *ret;
577
578 ret = utils_memrchr(memrchr_data[_i].s, memrchr_data[_i].c, memrchr_data[_i].n);
579 if (memrchr_data[_i].offset >= 0)
580 {
581 ck_assert(ret == memrchr_data[_i].s + memrchr_data[_i].offset);
582 }
583 else
584 {
585 ck_assert(ret == NULL);
586 }
587}
588END_TEST
589
af67613e
TB
590/*******************************************************************************
591 * translate
592 */
593
594static struct {
595 char *in;
596 char *from;
597 char *to;
598 char *out;
599} translate_data[] = {
600 {NULL, "", "", NULL},
601 {"abc", "", "", "abc"},
602 {"abc", "", "x", "abc"},
603 {"abc", "x", "", "abc"},
604 {"abc", "abc", "xyz", "xyz"},
605 {"aabbcc", "abc", "xyz", "xxyyzz"},
606 {"abbaccb", "abc", "xyz", "xyyxzzy"},
607 {"abxyzc", "abc", "xyz", "xyxyzz"},
608 {"abcdef", "abc", "xyz", "xyzdef"},
609 {"aaa", "abc", "xyz", "xxx"},
610 {"abc", "aaa", "xyz", "xbc"},
611 {"abc", "abc", "xxx", "xxx"},
612};
613
614START_TEST(test_translate)
615{
616 char *str, *ret;
617
618 str = strdupnull(translate_data[_i].in);
619 ret = translate(str, translate_data[_i].from, translate_data[_i].to);
620 ck_assert(ret == str);
621 if (ret != translate_data[_i].out)
622 {
623 ck_assert_str_eq(str, translate_data[_i].out);
624 }
625 free(str);
626}
627END_TEST
628
ccb6758e
TB
629/*******************************************************************************
630 * strreplace
631 */
632
633static struct {
634 char *in;
635 char *out;
636 char *search;
637 char *replace;
638 bool allocated;
639} strreplace_data[] = {
640 /* invalid arguments */
641 {NULL, NULL, NULL, NULL, FALSE},
642 {"", "", NULL, NULL, FALSE},
643 {"", "", "", NULL, FALSE},
644 {"", "", NULL, "", FALSE},
645 {"", "", "", "", FALSE},
646 {"", "", "", "asdf", FALSE},
647 {"", "", "asdf", "", FALSE},
648 {"asdf", "asdf", NULL, NULL, FALSE},
649 {"asdf", "asdf", "", NULL, FALSE},
650 {"asdf", "asdf", NULL, "", FALSE},
651 {"asdf", "asdf", "", "", FALSE},
652 {"asdf", "asdf", "", "asdf", FALSE},
653 {"asdf", "asdf", "asdf", NULL, FALSE},
654 {"qwer", "qwer", "", "asdf", FALSE},
655 /* replacement shorter */
656 {"asdf", "", "asdf", "", TRUE},
657 {"asdfasdf", "", "asdf", "", TRUE},
658 {"asasdfdf", "asdf", "asdf", "", TRUE},
659 {"asdf", "df", "as", "", TRUE},
660 {"asdf", "as", "df", "", TRUE},
661 {"qwer", "qwer", "asdf", "", FALSE},
662 /* replacement same length */
663 {"a", "b", "a", "b", TRUE},
664 {"aaa", "bbb", "a", "b", TRUE},
665 {"aaa", "bbb", "aaa", "bbb", TRUE},
666 {"asdf", "asdf", "asdf", "asdf", TRUE},
667 {"qwer", "qwer", "asdf", "asdf", FALSE},
668 /* replacement longer */
669 {"asdf", "asdf", "", "asdf", FALSE},
670 {"asdf", "asdfasdf", "asdf", "asdfasdf", TRUE},
671 {"asdf", "asdfsdf", "a", "asdf", TRUE},
672 {"asdf", "asdasdf", "f", "asdf", TRUE},
673 {"aaa", "asdfasdfasdf", "a", "asdf", TRUE},
674 {"qwer", "qwer", "asdf", "asdfasdf", FALSE},
675 /* real examples */
676 {"http://x.org/no/spaces", "http://x.org/no/spaces", " ", "%20", FALSE},
677 {"http://x.org/end ", "http://x.org/end%20", " ", "%20", TRUE},
678 {" http://x.org/start", "%20http://x.org/start", " ", "%20", TRUE},
679 {" http://x.org/both ", "%20http://x.org/both%20", " ", "%20", TRUE},
680 {"http://x.org/ /slash", "http://x.org/%20/slash", " ", "%20", TRUE},
681 {"http://x.org/ /three", "http://x.org/%20%20%20/three", " ", "%20", TRUE},
682 {"http://x.org/ ", "http://x.org/%20%20%20%20%20%20", " ", "%20", TRUE},
683 {"http://x.org/%20/encoded", "http://x.org/%20/encoded", " ", "%20", FALSE},
684};
685
686START_TEST(test_strreplace)
687{
688 char *ret;
689
690 ret = strreplace(strreplace_data[_i].in, strreplace_data[_i].search,
691 strreplace_data[_i].replace);
692 if (ret && strreplace_data[_i].out)
693 {
694 ck_assert_str_eq(ret, strreplace_data[_i].out);
695 }
696 else
697 {
698 ck_assert(ret == strreplace_data[_i].out);
699 }
700 if (strreplace_data[_i].allocated)
701 {
702 ck_assert(ret != strreplace_data[_i].in);
703 free(ret);
704 }
705 else
706 {
707 ck_assert(ret == strreplace_data[_i].in);
708 }
709}
710END_TEST
711
766141bc 712/*******************************************************************************
67b3bcd1 713 * path_dirname/basename/absolute
766141bc
TB
714 */
715
716static struct {
717 char *path;
718 char *dir;
719 char *base;
67b3bcd1 720 bool absolute;
766141bc 721} path_data[] = {
67b3bcd1
MW
722 {NULL, ".", ".", FALSE},
723 {"", ".", ".", FALSE},
724 {".", ".", ".", FALSE},
725 {"..", ".", "..", FALSE},
8182631b 726#ifdef WIN32
67b3bcd1
MW
727 {"C:\\", "C:", "C:", TRUE},
728 {"X:\\\\", "X:", "X:", TRUE},
729 {"foo", ".", "foo", FALSE},
730 {"f\\", ".", "f", FALSE},
731 {"foo\\", ".", "foo", FALSE},
732 {"foo\\\\", ".", "foo", FALSE},
733 {"d:\\f", "d:", "f", TRUE},
734 {"C:\\f\\", "C:", "f", TRUE},
735 {"C:\\foo", "C:", "foo", TRUE},
736 {"C:\\foo\\", "C:", "foo", TRUE},
737 {"foo\\bar", "foo", "bar", FALSE},
738 {"foo\\\\bar", "foo", "bar", FALSE},
739 {"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
740 {"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
741 {"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
742 {"\\foo\\bar", "\\foo", "bar", FALSE},
743 {"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
8182631b 744#else /* !WIN32 */
67b3bcd1
MW
745 {"/", "/", "/", TRUE},
746 {"//", "/", "/", TRUE},
747 {"foo", ".", "foo", FALSE},
748 {"f/", ".", "f", FALSE},
749 {"foo/", ".", "foo", FALSE},
750 {"foo//", ".", "foo", FALSE},
751 {"/f", "/", "f", TRUE},
752 {"/f/", "/", "f", TRUE},
753 {"/foo", "/", "foo", TRUE},
754 {"/foo/", "/", "foo", TRUE},
755 {"//foo/", "/", "foo", TRUE},
756 {"foo/bar", "foo", "bar", FALSE},
757 {"foo//bar", "foo", "bar", FALSE},
758 {"/foo/bar", "/foo", "bar", TRUE},
759 {"/foo/bar/", "/foo", "bar", TRUE},
760 {"/foo/bar/baz", "/foo/bar", "baz", TRUE},
8182631b 761#endif
766141bc
TB
762};
763
764START_TEST(test_path_dirname)
765{
766 char *dir;
767
768 dir = path_dirname(path_data[_i].path);
769 ck_assert_str_eq(path_data[_i].dir, dir);
770 free(dir);
771}
772END_TEST
773
774START_TEST(test_path_basename)
775{
776 char *base;
777
778 base = path_basename(path_data[_i].path);
779 ck_assert_str_eq(path_data[_i].base, base);
780 free(base);
781}
782END_TEST
783
67b3bcd1
MW
784START_TEST(test_path_absolute)
785{
786 ck_assert(path_data[_i].absolute == path_absolute(path_data[_i].path));
787}
788END_TEST
789
af67613e
TB
790/*******************************************************************************
791 * time_printf_hook
792 */
793
794static struct {
795 time_t in;
796 bool utc;
797 char *out;
798} time_data[] = {
799 {UNDEFINED_TIME, FALSE, "--- -- --:--:-- ----"},
800 {UNDEFINED_TIME, TRUE , "--- -- --:--:-- UTC ----"},
801 {1, FALSE, "Jan 01 01:00:01 1970"},
802 {1, TRUE , "Jan 01 00:00:01 UTC 1970"},
803 {1341150196, FALSE, "Jul 01 15:43:16 2012"},
804 {1341150196, TRUE , "Jul 01 13:43:16 UTC 2012"},
805};
806
807START_TEST(test_time_printf_hook)
808{
809 char buf[32];
810 int len;
811
812 len = snprintf(buf, sizeof(buf), "%T", &time_data[_i].in, time_data[_i].utc);
813 ck_assert(len >= 0 && len < sizeof(buf));
814 ck_assert_str_eq(buf, time_data[_i].out);
815}
816END_TEST
817
818/*******************************************************************************
819 * time_delta_printf_hook
820 */
821
822static struct {
823 time_t a;
824 time_t b;
825 char *out;
826} time_delta_data[] = {
827 {0, 0, "0 seconds"},
828 {0, 1, "1 second"},
829 {0, -1, "1 second"},
830 {1, 0, "1 second"},
831 {0, 2, "2 seconds"},
832 {2, 0, "2 seconds"},
833 {0, 60, "60 seconds"},
834 {0, 120, "120 seconds"},
835 {0, 121, "2 minutes"},
836 {0, 3600, "60 minutes"},
837 {0, 7200, "120 minutes"},
838 {0, 7201, "2 hours"},
839 {0, 86400, "24 hours"},
840 {0, 172800, "48 hours"},
841 {0, 172801, "2 days"},
842 {172801, 86400, "24 hours"},
843};
844
845START_TEST(test_time_delta_printf_hook)
846{
847 char buf[16];
848 int len;
849
850 len = snprintf(buf, sizeof(buf), "%V", &time_delta_data[_i].a, &time_delta_data[_i].b);
851 ck_assert(len >= 0 && len < sizeof(buf));
852 ck_assert_str_eq(buf, time_delta_data[_i].out);
853}
854END_TEST
855
434e530f
TB
856/*******************************************************************************
857 * mark_from_string
858 */
859
860static struct {
861 char *s;
862 bool ok;
ebd2d387 863 mark_op_t ops;
434e530f
TB
864 mark_t m;
865} mark_data[] = {
ebd2d387
MW
866 {NULL, FALSE, MARK_OP_NONE, { 0 }},
867 {"", TRUE, MARK_OP_NONE, { 0, 0xffffffff }},
868 {"/", TRUE, MARK_OP_NONE, { 0, 0 }},
869 {"42", TRUE, MARK_OP_NONE, { 42, 0xffffffff }},
870 {"0x42", TRUE, MARK_OP_NONE, { 0x42, 0xffffffff }},
871 {"x", FALSE, MARK_OP_NONE, { 0 }},
872 {"42/", TRUE, MARK_OP_NONE, { 0, 0 }},
873 {"42/0", TRUE, MARK_OP_NONE, { 0, 0 }},
874 {"42/x", FALSE, MARK_OP_NONE, { 0 }},
875 {"42/42", TRUE, MARK_OP_NONE, { 42, 42 }},
876 {"42/0xff", TRUE, MARK_OP_NONE, { 42, 0xff }},
877 {"0x42/0xff", TRUE, MARK_OP_NONE, { 0x42, 0xff }},
878 {"/0xff", TRUE, MARK_OP_NONE, { 0, 0xff }},
879 {"/x", FALSE, MARK_OP_NONE, { 0 }},
880 {"x/x", FALSE, MARK_OP_NONE, { 0 }},
881 {"0xfffffff0/0x0000ffff", TRUE, MARK_OP_UNIQUE,
882 { 0x0000fff0, 0x0000ffff }},
883 {"%unique", TRUE, MARK_OP_UNIQUE,
884 { MARK_UNIQUE, 0xffffffff }},
885 {"%unique/", TRUE, MARK_OP_UNIQUE,
886 { MARK_UNIQUE, 0 }},
887 {"%unique", FALSE, MARK_OP_NONE,
888 { 0, 0 }},
889 {"%unique/0x0000ffff", TRUE, MARK_OP_UNIQUE,
890 { MARK_UNIQUE, 0x0000ffff }},
891 {"%unique/0xffffffff", TRUE, MARK_OP_UNIQUE,
892 { MARK_UNIQUE, 0xffffffff }},
893 {"%unique0xffffffffff", FALSE, MARK_OP_UNIQUE,
894 { 0, 0 }},
895 {"0xffffffff/0x0000ffff", TRUE, MARK_OP_UNIQUE,
896 { MARK_UNIQUE, 0x0000ffff }},
897 {"0xffffffff/0xffffffff", TRUE, MARK_OP_UNIQUE,
898 { MARK_UNIQUE, 0xffffffff }},
899 {"%unique-dir", TRUE, MARK_OP_UNIQUE,
900 { MARK_UNIQUE_DIR, 0xffffffff }},
901 {"%unique-dir/", TRUE, MARK_OP_UNIQUE,
902 { MARK_UNIQUE_DIR, 0 }},
903 {"%unique-dir", FALSE, MARK_OP_NONE,
904 { 0, 0 }},
905 {"%unique-dir/0x0000ffff", TRUE, MARK_OP_UNIQUE,
906 { MARK_UNIQUE_DIR, 0x0000ffff }},
907 {"%unique-dir/0xffffffff", TRUE, MARK_OP_UNIQUE,
908 { MARK_UNIQUE_DIR, 0xffffffff }},
909 {"%unique-dir0xffffffff", FALSE, MARK_OP_UNIQUE,
910 { 0, 0 }},
911 {"0xfffffffe/0x0000ffff", TRUE, MARK_OP_UNIQUE,
912 { MARK_UNIQUE_DIR, 0x0000ffff }},
913 {"0xfffffffe/0xffffffff", TRUE, MARK_OP_UNIQUE,
914 { MARK_UNIQUE_DIR, 0xffffffff }},
915 {"%unique-/0xffffffff", FALSE, MARK_OP_UNIQUE,
916 { 0, 0 }},
917 {"%unique-foo/0xffffffff", FALSE, MARK_OP_UNIQUE,
918 { 0, 0 }},
902dc29f
MW
919 {"%same", TRUE, MARK_OP_SAME,
920 { MARK_SAME, 0xffffffff }},
921 {"%same/0x0000ffff", TRUE, MARK_OP_SAME,
922 { MARK_SAME, 0x0000ffff }},
923 {"%%same", FALSE, MARK_OP_NONE,
924 { 0, 0 }},
434e530f
TB
925};
926
927START_TEST(test_mark_from_string)
928{
929 mark_t mark;
930
ebd2d387 931 if (mark_from_string(mark_data[_i].s, mark_data[_i].ops, &mark))
434e530f
TB
932 {
933 ck_assert_int_eq(mark.value, mark_data[_i].m.value);
934 ck_assert_int_eq(mark.mask, mark_data[_i].m.mask);
935 }
936 else
937 {
938 ck_assert(!mark_data[_i].ok);
939 }
940}
941END_TEST
942
07166ce2
TB
943/*******************************************************************************
944 * if_id_from_string
945 */
946
947static struct {
948 char *s;
949 bool ok;
950 uint32_t i;
951} if_id_data[] = {
952 {NULL, FALSE, 0 },
953 {"", TRUE, 0 },
954 {"/", FALSE, 0 },
955 {"42", TRUE, 42 },
956 {"0x42", TRUE, 0x42 },
957 {"x", FALSE, 0 },
958 {"42/", FALSE, 0 },
959 {"42/0", FALSE, 0 },
960 {"%unique", TRUE, IF_ID_UNIQUE },
961 {"%unique/", FALSE, 0},
962 {"%unique0xffffffffff", FALSE, 0},
963 {"0xffffffff", TRUE, IF_ID_UNIQUE},
964 {"%unique-dir", TRUE, IF_ID_UNIQUE_DIR},
965 {"%unique-dir/",FALSE, 0},
966 {"0xfffffffe", TRUE, IF_ID_UNIQUE_DIR},
967 {"%unique-", FALSE, 0},
968 {"%unique-foo", FALSE, 0},
969};
970
971START_TEST(test_if_id_from_string)
972{
973 uint32_t if_id;
974
975 if (if_id_from_string(if_id_data[_i].s, &if_id))
976 {
977 ck_assert_int_eq(if_id, if_id_data[_i].i);
978 }
979 else
980 {
981 ck_assert(!if_id_data[_i].ok);
982 }
983}
984END_TEST
985
1f648d75
TB
986/*******************************************************************************
987 * signature_schemes_for_key
988 */
989
990static struct {
991 key_type_t type;
992 int size;
6f97c0d5 993 signature_scheme_t expected[7];
1f648d75 994} scheme_data[] = {
6f97c0d5
TB
995 {KEY_RSA, 1024, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
996 SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_256,
997 SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
40f2589a 998 SIGN_UNKNOWN }},
6f97c0d5
TB
999 {KEY_RSA, 2048, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
1000 SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_256,
1001 SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
1002 SIGN_UNKNOWN }},
1003 {KEY_RSA, 4096, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PSS,
1004 SIGN_RSA_EMSA_PKCS1_SHA2_384, SIGN_RSA_EMSA_PKCS1_SHA2_512,
1005 SIGN_UNKNOWN }},
1006 {KEY_RSA, 8192, { SIGN_RSA_EMSA_PSS, SIGN_RSA_EMSA_PKCS1_SHA2_512, SIGN_UNKNOWN }},
40f2589a
AS
1007 {KEY_ECDSA, 256, { SIGN_ECDSA_WITH_SHA256_DER, SIGN_ECDSA_WITH_SHA384_DER,
1008 SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
1009 {KEY_ECDSA, 384, { SIGN_ECDSA_WITH_SHA384_DER, SIGN_ECDSA_WITH_SHA512_DER,
1010 SIGN_UNKNOWN }},
1f648d75 1011 {KEY_ECDSA, 512, { SIGN_ECDSA_WITH_SHA512_DER, SIGN_UNKNOWN }},
40f2589a
AS
1012 {KEY_BLISS, 128, { SIGN_BLISS_WITH_SHA2_256, SIGN_BLISS_WITH_SHA2_384,
1013 SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
1014 {KEY_BLISS, 192, { SIGN_BLISS_WITH_SHA2_384, SIGN_BLISS_WITH_SHA2_512,
1015 SIGN_UNKNOWN }},
a88d9589 1016 {KEY_BLISS, 256, { SIGN_BLISS_WITH_SHA2_512, SIGN_UNKNOWN }},
1f648d75
TB
1017};
1018
1019START_TEST(test_signature_schemes_for_key)
1020{
1021 enumerator_t *enumerator;
6f97c0d5 1022 signature_params_t *params;
1f648d75
TB
1023 int i;
1024
1025 enumerator = signature_schemes_for_key(scheme_data[_i].type, scheme_data[_i].size);
1026 for (i = 0; scheme_data[_i].expected[i] != SIGN_UNKNOWN; i++)
1027 {
6f97c0d5
TB
1028 ck_assert(enumerator->enumerate(enumerator, &params));
1029 ck_assert_int_eq(scheme_data[_i].expected[i], params->scheme);
1f648d75 1030 }
6f97c0d5 1031 ck_assert(!enumerator->enumerate(enumerator, &params));
1f648d75
TB
1032 enumerator->destroy(enumerator);
1033}
1034END_TEST
1035
9a8c873e
TB
1036Suite *utils_suite_create()
1037{
1038 Suite *s;
1039 TCase *tc;
1040
df76881f 1041 /* force a timezone to match non-UTC conversions */
549502bc
MW
1042#ifdef WIN32
1043 _putenv("TZ=GST-1GDT");
1044#else
df76881f 1045 setenv("TZ", "Europe/Zurich", 1);
549502bc 1046#endif
df76881f
MW
1047 tzset();
1048
9a8c873e
TB
1049 s = suite_create("utils");
1050
1051 tc = tcase_create("objects");
1052 tcase_add_test(tc, test_objects);
1053 suite_add_tcase(s, tc);
1054
af67613e
TB
1055 tc = tcase_create("return functions");
1056 tcase_add_test(tc, test_return_functions);
1057 suite_add_tcase(s, tc);
1058
1059 tc = tcase_create("timeval_add_ms");
1060 tcase_add_test(tc, test_timeval_add_ms);
1061 suite_add_tcase(s, tc);
1062
b93ebb4e
TB
1063 tc = tcase_create("timespan_from_string");
1064 tcase_add_loop_test(tc, test_timespan_from_string, 0, countof(ts_data));
1065 suite_add_tcase(s, tc);
1066
af67613e
TB
1067 tc = tcase_create("htoun,untoh");
1068 tcase_add_test(tc, test_htoun);
1069 tcase_add_test(tc, test_untoh);
1070 suite_add_tcase(s, tc);
1071
84044f9c
MW
1072 tc = tcase_create("round");
1073 tcase_add_test(tc, test_round);
1074 suite_add_tcase(s, tc);
1075
32a145fd 1076 tc = tcase_create("string helper");
85597f29
TB
1077 tcase_add_loop_test(tc, test_streq, 0, countof(streq_data));
1078 tcase_add_loop_test(tc, test_strneq, 0, countof(strneq_data));
32a145fd
TB
1079 tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data));
1080 suite_add_tcase(s, tc);
1081
f206bb74
MW
1082 tc = tcase_create("malloc_align");
1083 tcase_add_test(tc, test_malloc_align);
1084 suite_add_tcase(s, tc);
1085
af67613e
TB
1086 tc = tcase_create("memxor");
1087 tcase_add_test(tc, test_memxor);
1088 tcase_add_test(tc, test_memxor_aligned);
1089 suite_add_tcase(s, tc);
1090
b8339632
MW
1091 tc = tcase_create("memeq");
1092 tcase_add_loop_test(tc, test_memeq, 0, countof(memeq_data));
1093 tcase_add_loop_test(tc, test_memeq_const, 0, countof(memeq_data));
1094 suite_add_tcase(s, tc);
1095
af67613e
TB
1096 tc = tcase_create("memstr");
1097 tcase_add_loop_test(tc, test_memstr, 0, countof(memstr_data));
1098 suite_add_tcase(s, tc);
1099
2ed241ae
TB
1100 tc = tcase_create("utils_memrchr");
1101 tcase_add_loop_test(tc, test_utils_memrchr, 0, countof(memrchr_data));
1102 suite_add_tcase(s, tc);
1103
af67613e
TB
1104 tc = tcase_create("translate");
1105 tcase_add_loop_test(tc, test_translate, 0, countof(translate_data));
1106 suite_add_tcase(s, tc);
1107
ccb6758e
TB
1108 tc = tcase_create("strreplace");
1109 tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
1110 suite_add_tcase(s, tc);
1111
67b3bcd1 1112 tc = tcase_create("path_dirname");
766141bc 1113 tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
67b3bcd1
MW
1114 suite_add_tcase(s, tc);
1115
1116 tc = tcase_create("path_basename");
766141bc
TB
1117 tcase_add_loop_test(tc, test_path_basename, 0, countof(path_data));
1118 suite_add_tcase(s, tc);
1119
67b3bcd1
MW
1120 tc = tcase_create("path_absolute");
1121 tcase_add_loop_test(tc, test_path_absolute, 0, countof(path_data));
1122 suite_add_tcase(s, tc);
1123
af67613e
TB
1124 tc = tcase_create("printf_hooks");
1125 tcase_add_loop_test(tc, test_time_printf_hook, 0, countof(time_data));
1126 tcase_add_loop_test(tc, test_time_delta_printf_hook, 0, countof(time_delta_data));
1127 suite_add_tcase(s, tc);
1128
434e530f
TB
1129 tc = tcase_create("mark_from_string");
1130 tcase_add_loop_test(tc, test_mark_from_string, 0, countof(mark_data));
1131 suite_add_tcase(s, tc);
1132
07166ce2
TB
1133 tc = tcase_create("if_id_from_string");
1134 tcase_add_loop_test(tc, test_if_id_from_string, 0, countof(if_id_data));
1135 suite_add_tcase(s, tc);
1136
1f648d75
TB
1137 tc = tcase_create("signature_schemes_for_key");
1138 tcase_add_loop_test(tc, test_signature_schemes_for_key, 0, countof(scheme_data));
1139 suite_add_tcase(s, tc);
1140
9a8c873e
TB
1141 return s;
1142}