ipsec_types: Add utility function to parse mark_t from strings
[strongswan.git] / src / libstrongswan / tests / test_utils.c
1 /*
2 * Copyright (C) 2013 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
22 #include <time.h>
23
24 /*******************************************************************************
25 * object storage on lib
26 */
27
28 START_TEST(test_objects)
29 {
30 char *k1 = "key1", *k2 = "key2";
31 char *v1 = "val1", *val;
32
33 ck_assert(lib->get(lib, k1) == NULL);
34
35 ck_assert(lib->set(lib, k1, v1));
36 ck_assert(!lib->set(lib, k1, v1));
37
38 val = lib->get(lib, k1);
39 ck_assert(val != NULL);
40 ck_assert(streq(val, v1));
41
42 ck_assert(lib->set(lib, k1, NULL));
43 ck_assert(!lib->set(lib, k2, NULL));
44
45 ck_assert(lib->get(lib, k1) == NULL);
46 }
47 END_TEST
48
49 /*******************************************************************************
50 * test return_... functions
51 */
52
53 START_TEST(test_return_functions)
54 {
55 ck_assert(return_null() == NULL);
56 ck_assert(return_null("asdf", 5, NULL, 1, "qwer") == NULL);
57
58 ck_assert(return_true() == TRUE);
59 ck_assert(return_true("asdf", 5, NULL, 1, "qwer") == TRUE);
60
61 ck_assert(return_false() == FALSE);
62 ck_assert(return_false("asdf", 5, NULL, 1, "qwer") == FALSE);
63
64 ck_assert(return_failed() == FAILED);
65 ck_assert(return_failed("asdf", 5, NULL, 1, "qwer") == FAILED);
66
67 ck_assert(return_success() == SUCCESS);
68 ck_assert(return_success("asdf", 5, NULL, 1, "qwer") == SUCCESS);
69
70 /* just make sure this works */
71 nop();
72 nop("asdf", 5, NULL, 1, "qwer");
73 }
74 END_TEST
75
76 /*******************************************************************************
77 * timeval_add_ms
78 */
79
80 START_TEST(test_timeval_add_ms)
81 {
82 timeval_t tv;
83
84 tv.tv_sec = 0;
85 tv.tv_usec = 0;
86 timeval_add_ms(&tv, 0);
87 ck_assert_int_eq(tv.tv_sec, 0);
88 ck_assert_int_eq(tv.tv_usec, 0);
89
90 timeval_add_ms(&tv, 1);
91 ck_assert_int_eq(tv.tv_sec, 0);
92 ck_assert_int_eq(tv.tv_usec, 1000);
93
94 timeval_add_ms(&tv, 0);
95 ck_assert_int_eq(tv.tv_sec, 0);
96 ck_assert_int_eq(tv.tv_usec, 1000);
97
98 timeval_add_ms(&tv, 999);
99 ck_assert_int_eq(tv.tv_sec, 1);
100 ck_assert_int_eq(tv.tv_usec, 0);
101
102 timeval_add_ms(&tv, 0);
103 ck_assert_int_eq(tv.tv_sec, 1);
104 ck_assert_int_eq(tv.tv_usec, 0);
105
106 timeval_add_ms(&tv, 1000);
107 ck_assert_int_eq(tv.tv_sec, 2);
108 ck_assert_int_eq(tv.tv_usec, 0);
109
110 timeval_add_ms(&tv, 1500);
111 ck_assert_int_eq(tv.tv_sec, 3);
112 ck_assert_int_eq(tv.tv_usec, 500000);
113 }
114 END_TEST
115
116 /*******************************************************************************
117 * htoun/untoh
118 */
119
120 START_TEST(test_htoun)
121 {
122 chunk_t net64, expected;
123 u_int16_t host16 = 513;
124 u_int32_t net16 = 0, host32 = 67305985;
125 u_int64_t net32 = 0, host64 = 578437695752307201ULL;
126
127 net64 = chunk_alloca(16);
128 memset(net64.ptr, 0, net64.len);
129
130 expected = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
131 htoun16((char*)&net16 + 1, host16);
132 ck_assert(chunk_equals(expected, chunk_from_thing(net16)));
133
134 expected = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
135 htoun32((u_int16_t*)&net32 + 1, host32);
136 ck_assert(chunk_equals(expected, chunk_from_thing(net32)));
137
138 expected = chunk_from_chars(0x00, 0x00, 0x00, 0x00,
139 0x08, 0x07, 0x06, 0x05,
140 0x04, 0x03, 0x02, 0x01,
141 0x00, 0x00, 0x00, 0x00);
142 htoun64((u_int32_t*)net64.ptr + 1, host64);
143 ck_assert(chunk_equals(expected, net64));
144 }
145 END_TEST
146
147 START_TEST(test_untoh)
148 {
149 chunk_t net;
150 u_int16_t host16;
151 u_int32_t host32;
152 u_int64_t host64;
153
154 net = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
155 host16 = untoh16(net.ptr + 1);
156 ck_assert(host16 == 513);
157
158 net = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
159 host32 = untoh32(net.ptr + 2);
160 ck_assert(host32 == 67305985);
161
162 net = chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05,
163 0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00);
164 host64 = untoh64(net.ptr + 4);
165 ck_assert(host64 == 578437695752307201ULL);
166 }
167 END_TEST
168
169 /*******************************************************************************
170 * round_up/down
171 */
172
173 START_TEST(test_round)
174 {
175 ck_assert_int_eq(round_up(0, 4), 0);
176 ck_assert_int_eq(round_up(1, 4), 4);
177 ck_assert_int_eq(round_up(2, 4), 4);
178 ck_assert_int_eq(round_up(3, 4), 4);
179 ck_assert_int_eq(round_up(4, 4), 4);
180 ck_assert_int_eq(round_up(5, 4), 8);
181
182 ck_assert_int_eq(round_down(0, 4), 0);
183 ck_assert_int_eq(round_down(1, 4), 0);
184 ck_assert_int_eq(round_down(2, 4), 0);
185 ck_assert_int_eq(round_down(3, 4), 0);
186 ck_assert_int_eq(round_down(4, 4), 4);
187 ck_assert_int_eq(round_down(5, 4), 4);
188 }
189 END_TEST
190
191 /*******************************************************************************
192 * strpfx
193 */
194
195 static struct {
196 char *str;
197 char *pfx;
198 bool prefix;
199 bool case_prefix;
200 } strpfx_data[] = {
201 {"", "", TRUE, TRUE},
202 {"abc", "", TRUE, TRUE},
203 {"abc", "a", TRUE, TRUE},
204 {"abc", "ab", TRUE, TRUE},
205 {"abc", "abc", TRUE, TRUE},
206 {"abc", "abcd", FALSE, FALSE},
207 {"abc", "AB", FALSE, TRUE},
208 {"ABC", "ab", FALSE, TRUE},
209 {" abc", "abc", FALSE, FALSE},
210 };
211
212 START_TEST(test_strpfx)
213 {
214 bool prefix;
215
216 prefix = strpfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
217 ck_assert(prefix == strpfx_data[_i].prefix);
218 prefix = strcasepfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
219 ck_assert(prefix == strpfx_data[_i].case_prefix);
220 }
221 END_TEST
222
223 /*******************************************************************************
224 * memxor
225 */
226
227 static void do_memxor(chunk_t a, chunk_t b, chunk_t exp)
228 {
229 chunk_t dst;
230
231 dst = chunk_clonea(a);
232 dst.len = b.len;
233 memxor(dst.ptr, b.ptr, b.len);
234 ck_assert(chunk_equals(dst, exp));
235 }
236
237 START_TEST(test_memxor)
238 {
239 chunk_t a, b, dst;
240 int i;
241
242 a = chunk_alloca(64);
243 memset(a.ptr, 0, a.len);
244 b = chunk_alloca(64);
245 for (i = 0; i < 64; i++)
246 {
247 b.ptr[i] = i;
248 b.len = i;
249 do_memxor(a, b, b);
250 }
251 b.len = 64;
252 do_memxor(a, b, b);
253
254 dst = chunk_clonea(a);
255 memxor(dst.ptr, b.ptr, b.len);
256 ck_assert(chunk_equals(dst, b));
257
258 memxor(dst.ptr, b.ptr, 0);
259 memxor(dst.ptr, b.ptr, 1);
260 memxor(dst.ptr + 1, b.ptr + 1, 1);
261 memxor(dst.ptr + 2, b.ptr + 2, b.len - 2);
262 ck_assert(chunk_equals(dst, a));
263 }
264 END_TEST
265
266 START_TEST(test_memxor_aligned)
267 {
268 u_int64_t a = 0, b = 0;
269 chunk_t ca, cb;
270 int i;
271
272 ca = chunk_from_thing(a);
273 cb = chunk_from_thing(b);
274
275 for (i = 0; i < 8; i++)
276 {
277 cb.ptr[i] = i + 1;
278 }
279
280 /* 64-bit aligned */
281 memxor(ca.ptr, cb.ptr, 8);
282 ck_assert(a == b);
283 /* 32-bit aligned source */
284 a = 0;
285 memxor(ca.ptr, cb.ptr + 4, 4);
286 ck_assert(chunk_equals(ca, chunk_from_chars(0x05, 0x06, 0x07, 0x08,
287 0x00, 0x00, 0x00, 0x00)));
288 /* 16-bit aligned source */
289 a = 0;
290 memxor(ca.ptr, cb.ptr + 2, 6);
291 ck_assert(chunk_equals(ca, chunk_from_chars(0x03, 0x04, 0x05, 0x06,
292 0x07, 0x08, 0x00, 0x00)));
293 /* 8-bit aligned source */
294 a = 0;
295 memxor(ca.ptr, cb.ptr + 1, 7);
296 ck_assert(chunk_equals(ca, chunk_from_chars(0x02, 0x03, 0x04, 0x05,
297 0x06, 0x07, 0x08, 0x00)));
298 }
299 END_TEST
300
301 /*******************************************************************************
302 * memstr
303 */
304
305 static struct {
306 char *haystack;
307 char *needle;
308 size_t n;
309 int offset;
310 } memstr_data[] = {
311 {NULL, NULL, 0, -1},
312 {NULL, NULL, 3, -1},
313 {NULL, "abc", 0, -1},
314 {NULL, "abc", 3, -1},
315 {"", "", 0, -1},
316 {"abc", NULL, 3, -1},
317 {"abc", "", 3, -1},
318 {"abc", "abc", 3, 0},
319 {" abc", "abc", 4, 1},
320 {" abc", "abc", 3, -1},
321 {"abcabc", "abc", 6, 0},
322 {" abc ", "abc", 5, 1},
323 };
324
325 START_TEST(test_memstr)
326 {
327 char *ret;
328
329 ret = memstr(memstr_data[_i].haystack, memstr_data[_i].needle, memstr_data[_i].n);
330 if (memstr_data[_i].offset >= 0)
331 {
332 ck_assert(ret == memstr_data[_i].haystack + memstr_data[_i].offset);
333 }
334 else
335 {
336 ck_assert(ret == NULL);
337 }
338 }
339 END_TEST
340
341 /*******************************************************************************
342 * translate
343 */
344
345 static struct {
346 char *in;
347 char *from;
348 char *to;
349 char *out;
350 } translate_data[] = {
351 {NULL, "", "", NULL},
352 {"abc", "", "", "abc"},
353 {"abc", "", "x", "abc"},
354 {"abc", "x", "", "abc"},
355 {"abc", "abc", "xyz", "xyz"},
356 {"aabbcc", "abc", "xyz", "xxyyzz"},
357 {"abbaccb", "abc", "xyz", "xyyxzzy"},
358 {"abxyzc", "abc", "xyz", "xyxyzz"},
359 {"abcdef", "abc", "xyz", "xyzdef"},
360 {"aaa", "abc", "xyz", "xxx"},
361 {"abc", "aaa", "xyz", "xbc"},
362 {"abc", "abc", "xxx", "xxx"},
363 };
364
365 START_TEST(test_translate)
366 {
367 char *str, *ret;
368
369 str = strdupnull(translate_data[_i].in);
370 ret = translate(str, translate_data[_i].from, translate_data[_i].to);
371 ck_assert(ret == str);
372 if (ret != translate_data[_i].out)
373 {
374 ck_assert_str_eq(str, translate_data[_i].out);
375 }
376 free(str);
377 }
378 END_TEST
379
380 /*******************************************************************************
381 * time_printf_hook
382 */
383
384 static struct {
385 time_t in;
386 bool utc;
387 char *out;
388 } time_data[] = {
389 {UNDEFINED_TIME, FALSE, "--- -- --:--:-- ----"},
390 {UNDEFINED_TIME, TRUE , "--- -- --:--:-- UTC ----"},
391 {1, FALSE, "Jan 01 01:00:01 1970"},
392 {1, TRUE , "Jan 01 00:00:01 UTC 1970"},
393 {1341150196, FALSE, "Jul 01 15:43:16 2012"},
394 {1341150196, TRUE , "Jul 01 13:43:16 UTC 2012"},
395 };
396
397 START_TEST(test_time_printf_hook)
398 {
399 char buf[32];
400 int len;
401
402 len = snprintf(buf, sizeof(buf), "%T", &time_data[_i].in, time_data[_i].utc);
403 ck_assert(len >= 0 && len < sizeof(buf));
404 ck_assert_str_eq(buf, time_data[_i].out);
405 }
406 END_TEST
407
408 /*******************************************************************************
409 * time_delta_printf_hook
410 */
411
412 static struct {
413 time_t a;
414 time_t b;
415 char *out;
416 } time_delta_data[] = {
417 {0, 0, "0 seconds"},
418 {0, 1, "1 second"},
419 {0, -1, "1 second"},
420 {1, 0, "1 second"},
421 {0, 2, "2 seconds"},
422 {2, 0, "2 seconds"},
423 {0, 60, "60 seconds"},
424 {0, 120, "120 seconds"},
425 {0, 121, "2 minutes"},
426 {0, 3600, "60 minutes"},
427 {0, 7200, "120 minutes"},
428 {0, 7201, "2 hours"},
429 {0, 86400, "24 hours"},
430 {0, 172800, "48 hours"},
431 {0, 172801, "2 days"},
432 {172801, 86400, "24 hours"},
433 };
434
435 START_TEST(test_time_delta_printf_hook)
436 {
437 char buf[16];
438 int len;
439
440 len = snprintf(buf, sizeof(buf), "%V", &time_delta_data[_i].a, &time_delta_data[_i].b);
441 ck_assert(len >= 0 && len < sizeof(buf));
442 ck_assert_str_eq(buf, time_delta_data[_i].out);
443 }
444 END_TEST
445
446 /*******************************************************************************
447 * mark_from_string
448 */
449
450 static struct {
451 char *s;
452 bool ok;
453 mark_t m;
454 } mark_data[] = {
455 {NULL, FALSE, { 0 }},
456 {"", TRUE, { 0, 0xffffffff }},
457 {"/", TRUE, { 0, 0 }},
458 {"42", TRUE, { 42, 0xffffffff }},
459 {"0x42", TRUE, { 0x42, 0xffffffff }},
460 {"x", FALSE, { 0 }},
461 {"42/", TRUE, { 0, 0 }},
462 {"42/0", TRUE, { 0, 0 }},
463 {"42/x", FALSE, { 0 }},
464 {"42/42", TRUE, { 42, 42 }},
465 {"42/0xff", TRUE, { 42, 0xff }},
466 {"0x42/0xff", TRUE, { 0x42, 0xff }},
467 {"/0xff", TRUE, { 0, 0xff }},
468 {"/x", FALSE, { 0 }},
469 {"x/x", FALSE, { 0 }},
470 {"0xffffffff/0x0000ffff", TRUE, { 0x0000ffff, 0x0000ffff }},
471 {"0xffffffff/0xffffffff", TRUE, { 0xffffffff, 0xffffffff }},
472 };
473
474 START_TEST(test_mark_from_string)
475 {
476 mark_t mark;
477
478 if (mark_from_string(mark_data[_i].s, &mark))
479 {
480 ck_assert_int_eq(mark.value, mark_data[_i].m.value);
481 ck_assert_int_eq(mark.mask, mark_data[_i].m.mask);
482 }
483 else
484 {
485 ck_assert(!mark_data[_i].ok);
486 }
487 }
488 END_TEST
489
490 Suite *utils_suite_create()
491 {
492 Suite *s;
493 TCase *tc;
494
495 /* force a timezone to match non-UTC conversions */
496 setenv("TZ", "Europe/Zurich", 1);
497 tzset();
498
499 s = suite_create("utils");
500
501 tc = tcase_create("objects");
502 tcase_add_test(tc, test_objects);
503 suite_add_tcase(s, tc);
504
505 tc = tcase_create("return functions");
506 tcase_add_test(tc, test_return_functions);
507 suite_add_tcase(s, tc);
508
509 tc = tcase_create("timeval_add_ms");
510 tcase_add_test(tc, test_timeval_add_ms);
511 suite_add_tcase(s, tc);
512
513 tc = tcase_create("htoun,untoh");
514 tcase_add_test(tc, test_htoun);
515 tcase_add_test(tc, test_untoh);
516 suite_add_tcase(s, tc);
517
518 tc = tcase_create("round");
519 tcase_add_test(tc, test_round);
520 suite_add_tcase(s, tc);
521
522 tc = tcase_create("string helper");
523 tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data));
524 suite_add_tcase(s, tc);
525
526 tc = tcase_create("memxor");
527 tcase_add_test(tc, test_memxor);
528 tcase_add_test(tc, test_memxor_aligned);
529 suite_add_tcase(s, tc);
530
531 tc = tcase_create("memstr");
532 tcase_add_loop_test(tc, test_memstr, 0, countof(memstr_data));
533 suite_add_tcase(s, tc);
534
535 tc = tcase_create("translate");
536 tcase_add_loop_test(tc, test_translate, 0, countof(translate_data));
537 suite_add_tcase(s, tc);
538
539 tc = tcase_create("printf_hooks");
540 tcase_add_loop_test(tc, test_time_printf_hook, 0, countof(time_data));
541 tcase_add_loop_test(tc, test_time_delta_printf_hook, 0, countof(time_delta_data));
542 suite_add_tcase(s, tc);
543
544 tc = tcase_create("mark_from_string");
545 tcase_add_loop_test(tc, test_mark_from_string, 0, countof(mark_data));
546 suite_add_tcase(s, tc);
547
548 return s;
549 }