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