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