chunk: Add functions to map file contents to a chunk
[strongswan.git] / src / libstrongswan / tests / suites / test_chunk.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "test_suite.h"
18
19 #include <unistd.h>
20
21 #include <utils/chunk.h>
22
23 /*******************************************************************************
24 * utilities
25 */
26
27 static void assert_chunk_empty(chunk_t chunk)
28 {
29 ck_assert(chunk.len == 0 && chunk.ptr == NULL);
30 }
31
32 /*******************************************************************************
33 * equals
34 */
35
36 START_TEST(test_chunk_equals)
37 {
38 chunk_t chunk = chunk_from_str("chunk");
39 chunk_t chunk_a, chunk_b;
40
41 chunk_a = chunk_empty;
42 chunk_b = chunk_empty;
43 ck_assert(!chunk_equals(chunk_a, chunk_b));
44
45 chunk_a = chunk;
46 ck_assert(!chunk_equals(chunk_a, chunk_b));
47 chunk_b = chunk;
48 ck_assert(chunk_equals(chunk_a, chunk_b));
49
50 chunk_b = chunk_from_str("asdf");
51 ck_assert(!chunk_equals(chunk_a, chunk_b));
52
53 chunk_b = chunk_from_str("chunk");
54 ck_assert(chunk_equals(chunk_a, chunk_b));
55 }
56 END_TEST
57
58 /*******************************************************************************
59 * chunk_compare test
60 */
61
62 static struct {
63 int result;
64 chunk_t a;
65 chunk_t b;
66 } compare_data[] = {
67 { 0, { NULL, 0 }, { NULL, 0 }},
68 { 0, chunk_from_chars(0x00), chunk_from_chars(0x00)},
69 {-1, chunk_from_chars(0x00), chunk_from_chars(0x01)},
70 { 1, chunk_from_chars(0x01), chunk_from_chars(0x00)},
71 { 0, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x00, 0x00)},
72 {-1, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x00, 0x01)},
73 { 1, chunk_from_chars(0x00, 0x01), chunk_from_chars(0x00, 0x00)},
74 {-1, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x01, 0x00)},
75 { 1, chunk_from_chars(0x01, 0x00), chunk_from_chars(0x00, 0x00)},
76 {-1, chunk_from_chars(0xff), chunk_from_chars(0x00, 0x00)},
77 { 1, chunk_from_chars(0x00, 0x00), chunk_from_chars(0xff)},
78 };
79
80 START_TEST(test_compare)
81 {
82 int result, expected;
83
84 result = chunk_compare(compare_data[_i].a, compare_data[_i].b);
85 expected = compare_data[_i].result;
86 ck_assert((result == 0 && expected == 0) ||
87 (result < 0 && expected < 0) ||
88 (result > 0 && expected > 0));
89 }
90 END_TEST
91
92 /*******************************************************************************
93 * clear
94 */
95
96 START_TEST(test_chunk_clear)
97 {
98 chunk_t chunk;
99 u_char *ptr;
100 int i;
101 bool cleared = TRUE;
102
103 chunk = chunk_empty;
104 chunk_clear(&chunk);
105 chunk_free(&chunk);
106
107 chunk = chunk_alloc(64);
108 ptr = chunk.ptr;
109 for (i = 0; i < 64; i++)
110 {
111 chunk.ptr[i] = i;
112 }
113 chunk_clear(&chunk);
114 /* check memory area of freed chunk. We can't use ck_assert() for this
115 * test directly, as it might allocate data at the freed area. */
116 for (i = 0; i < 64; i++)
117 {
118 if (ptr[i] != 0 && ptr[i] == i)
119 {
120 cleared = FALSE;
121 break;
122 }
123 }
124 assert_chunk_empty(chunk);
125 ck_assert(cleared);
126 }
127 END_TEST
128
129 /*******************************************************************************
130 * chunk_length
131 */
132
133 START_TEST(test_chunk_length)
134 {
135 chunk_t a, b, c;
136 size_t len;
137
138 a = chunk_empty;
139 b = chunk_empty;
140 c = chunk_empty;
141 len = chunk_length("ccc", a, b, c);
142 ck_assert_int_eq(len, 0);
143
144 a = chunk_from_str("foo");
145 b = chunk_from_str("bar");
146 len = chunk_length("ccc", a, b, c);
147 ck_assert_int_eq(len, 6);
148
149 len = chunk_length("zcc", a, b, c);
150 ck_assert_int_eq(len, 0);
151
152 len = chunk_length("czc", a, b, c);
153 ck_assert_int_eq(len, 3);
154
155 a = chunk_from_str("foo");
156 b = chunk_from_str("bar");
157 c = chunk_from_str("baz");
158 len = chunk_length("ccc", a, b, c);
159 ck_assert_int_eq(len, 9);
160 }
161 END_TEST
162
163 /*******************************************************************************
164 * chunk_create_cat
165 */
166
167 START_TEST(test_chunk_create_cat)
168 {
169 chunk_t foo, bar;
170 chunk_t a, b, c;
171 u_char *ptra, *ptrb;
172
173 foo = chunk_from_str("foo");
174 bar = chunk_from_str("bar");
175
176 /* to simplify things we use the chunk_cata macro */
177
178 a = chunk_empty;
179 b = chunk_empty;
180 c = chunk_cata("cc", a, b);
181 ck_assert_int_eq(c.len, 0);
182 ck_assert(c.ptr != NULL);
183
184 a = foo;
185 b = bar;
186 c = chunk_cata("cc", a, b);
187 ck_assert_int_eq(c.len, 6);
188 ck_assert(chunk_equals(c, chunk_from_str("foobar")));
189
190 a = chunk_clone(foo);
191 b = chunk_clone(bar);
192 c = chunk_cata("mm", a, b);
193 ck_assert_int_eq(c.len, 6);
194 ck_assert(chunk_equals(c, chunk_from_str("foobar")));
195
196 a = chunk_clone(foo);
197 b = chunk_clone(bar);
198 ptra = a.ptr;
199 ptrb = b.ptr;
200 c = chunk_cata("ss", a, b);
201 ck_assert_int_eq(c.len, 6);
202 ck_assert(chunk_equals(c, chunk_from_str("foobar")));
203 /* check memory area of cleared chunk */
204 ck_assert(!chunk_equals(foo, chunk_create(ptra, 3)));
205 ck_assert(!chunk_equals(bar, chunk_create(ptrb, 3)));
206 }
207 END_TEST
208
209 /*******************************************************************************
210 * chunk_split
211 */
212
213 static bool mem_in_chunk(u_char *ptr, chunk_t chunk)
214 {
215 return ptr >= chunk.ptr && ptr < (chunk.ptr + chunk.len);
216 }
217
218 START_TEST(test_chunk_split)
219 {
220 chunk_t foo, bar, foobar;
221 chunk_t a, b, c;
222 u_char *ptra, *ptrb;
223
224 foo = chunk_from_str("foo");
225 bar = chunk_from_str("bar");
226 foobar = chunk_from_str("foobar");
227
228 chunk_split(foobar, "aa", 3, &a, 3, &b);
229 ck_assert(chunk_equals(a, foo));
230 ck_assert(chunk_equals(b, bar));
231 ck_assert(!mem_in_chunk(a.ptr, foobar));
232 ck_assert(!mem_in_chunk(b.ptr, foobar));
233 chunk_free(&a);
234 chunk_free(&b);
235
236 chunk_split(foobar, "mm", 3, &a, 3, &b);
237 ck_assert(chunk_equals(a, foo));
238 ck_assert(chunk_equals(b, bar));
239 ck_assert(mem_in_chunk(a.ptr, foobar));
240 ck_assert(mem_in_chunk(b.ptr, foobar));
241
242 chunk_split(foobar, "am", 3, &a, 3, &b);
243 ck_assert(chunk_equals(a, foo));
244 ck_assert(chunk_equals(b, bar));
245 ck_assert(!mem_in_chunk(a.ptr, foobar));
246 ck_assert(mem_in_chunk(b.ptr, foobar));
247 chunk_free(&a);
248
249 a = chunk_alloca(3);
250 ptra = a.ptr;
251 b = chunk_alloca(3);
252 ptrb = b.ptr;
253 chunk_split(foobar, "cc", 3, &a, 3, &b);
254 ck_assert(chunk_equals(a, foo));
255 ck_assert(chunk_equals(b, bar));
256 ck_assert(a.ptr == ptra);
257 ck_assert(b.ptr == ptrb);
258
259 chunk_split(foobar, "mm", 1, NULL, 2, &a, 2, NULL, 1, &b);
260 ck_assert(chunk_equals(a, chunk_from_str("oo")));
261 ck_assert(chunk_equals(b, chunk_from_str("r")));
262
263 chunk_split(foobar, "mm", 6, &a, 6, &b);
264 ck_assert(chunk_equals(a, foobar));
265 assert_chunk_empty(b);
266
267 chunk_split(foobar, "mac", 12, &a, 12, &b, 12, &c);
268 ck_assert(chunk_equals(a, foobar));
269 assert_chunk_empty(b);
270 assert_chunk_empty(c);
271 }
272 END_TEST
273
274 /*******************************************************************************
275 * chunk_skip[_zero]
276 */
277
278 START_TEST(test_chunk_skip)
279 {
280 chunk_t foobar, a;
281
282 foobar = chunk_from_str("foobar");
283 a = foobar;
284 a = chunk_skip(a, 0);
285 ck_assert(chunk_equals(a, foobar));
286 a = chunk_skip(a, 1);
287 ck_assert(chunk_equals(a, chunk_from_str("oobar")));
288 a = chunk_skip(a, 2);
289 ck_assert(chunk_equals(a, chunk_from_str("bar")));
290 a = chunk_skip(a, 3);
291 assert_chunk_empty(a);
292
293 a = foobar;
294 a = chunk_skip(a, 6);
295 assert_chunk_empty(a);
296
297 a = foobar;
298 a = chunk_skip(a, 10);
299 assert_chunk_empty(a);
300 }
301 END_TEST
302
303 START_TEST(test_chunk_skip_zero)
304 {
305 chunk_t foobar, a;
306
307 a = chunk_empty;
308 a = chunk_skip_zero(a);
309 assert_chunk_empty(a);
310
311 foobar = chunk_from_str("foobar");
312 a = foobar;
313 a = chunk_skip_zero(a);
314 ck_assert(chunk_equals(a, foobar));
315
316 a = chunk_from_chars(0x00, 0xaa, 0xbb, 0xcc);
317 a = chunk_skip_zero(a);
318 ck_assert(chunk_equals(a, chunk_from_chars(0xaa, 0xbb, 0xcc)));
319 a = chunk_skip_zero(a);
320 ck_assert(chunk_equals(a, chunk_from_chars(0xaa, 0xbb, 0xcc)));
321 }
322 END_TEST
323
324 /*******************************************************************************
325 * BASE16 encoding test
326 */
327
328 START_TEST(test_base16)
329 {
330 /* test vectors from RFC 4648:
331 *
332 * BASE16("") = ""
333 * BASE16("f") = "66"
334 * BASE16("fo") = "666F"
335 * BASE16("foo") = "666F6F"
336 * BASE16("foob") = "666F6F62"
337 * BASE16("fooba") = "666F6F6261"
338 * BASE16("foobar") = "666F6F626172"
339 */
340 typedef struct {
341 bool upper;
342 char *in;
343 char *out;
344 } testdata_t;
345
346 testdata_t test[] = {
347 {TRUE, "", ""},
348 {TRUE, "f", "66"},
349 {TRUE, "fo", "666F"},
350 {TRUE, "foo", "666F6F"},
351 {TRUE, "foob", "666F6F62"},
352 {TRUE, "fooba", "666F6F6261"},
353 {TRUE, "foobar", "666F6F626172"},
354 {FALSE, "", ""},
355 {FALSE, "f", "66"},
356 {FALSE, "fo", "666f"},
357 {FALSE, "foo", "666f6f"},
358 {FALSE, "foob", "666f6f62"},
359 {FALSE, "fooba", "666f6f6261"},
360 {FALSE, "foobar", "666f6f626172"},
361 };
362 testdata_t test_colon[] = {
363 {TRUE, "", ""},
364 {TRUE, "f", "66"},
365 {TRUE, "fo", "66:6F"},
366 {TRUE, "foo", "66:6F:6F"},
367 {FALSE, "foob", "66:6f:6f:62"},
368 {FALSE, "fooba", "66:6f:6f:62:61"},
369 {FALSE, "foobar", "66:6f:6f:62:61:72"},
370 {FALSE, "foobar", "66:6f6f:6261:72"},
371 };
372 int i;
373
374 for (i = 0; i < countof(test); i++)
375 {
376 chunk_t out;
377
378 out = chunk_to_hex(chunk_create(test[i].in, strlen(test[i].in)), NULL,
379 test[i].upper);
380 ck_assert_str_eq(out.ptr, test[i].out);
381 free(out.ptr);
382 }
383
384 for (i = 0; i < countof(test); i++)
385 {
386 chunk_t out;
387
388 out = chunk_from_hex(chunk_create(test[i].out, strlen(test[i].out)), NULL);
389 fail_unless(strneq(out.ptr, test[i].in, out.len),
390 "base16 conversion error - should '%s', is %#B",
391 test[i].in, &out);
392 free(out.ptr);
393 }
394
395 for (i = 0; i < countof(test_colon); i++)
396 {
397 chunk_t out;
398
399 out = chunk_from_hex(chunk_create(test_colon[i].out, strlen(test_colon[i].out)), NULL);
400 fail_unless(strneq(out.ptr, test_colon[i].in, out.len),
401 "base16 conversion error - should '%s', is %#B",
402 test_colon[i].in, &out);
403 free(out.ptr);
404 }
405 }
406 END_TEST
407
408 /*******************************************************************************
409 * BASE64 encoding test
410 */
411
412 START_TEST(test_base64)
413 {
414 /* test vectors from RFC 4648:
415 *
416 * BASE64("") = ""
417 * BASE64("f") = "Zg=="
418 * BASE64("fo") = "Zm8="
419 * BASE64("foo") = "Zm9v"
420 * BASE64("foob") = "Zm9vYg=="
421 * BASE64("fooba") = "Zm9vYmE="
422 * BASE64("foobar") = "Zm9vYmFy"
423 */
424 typedef struct {
425 char *in;
426 char *out;
427 } testdata_t;
428
429 testdata_t test[] = {
430 {"", ""},
431 {"f", "Zg=="},
432 {"fo", "Zm8="},
433 {"foo", "Zm9v"},
434 {"foob", "Zm9vYg=="},
435 {"fooba", "Zm9vYmE="},
436 {"foobar", "Zm9vYmFy"},
437 };
438 int i;
439
440 for (i = 0; i < countof(test); i++)
441 {
442 chunk_t out;
443
444 out = chunk_to_base64(chunk_create(test[i].in, strlen(test[i].in)), NULL);
445 ck_assert_str_eq(out.ptr, test[i].out);
446 free(out.ptr);
447 }
448
449 for (i = 0; i < countof(test); i++)
450 {
451 chunk_t out;
452
453 out = chunk_from_base64(chunk_create(test[i].out, strlen(test[i].out)), NULL);
454 fail_unless(strneq(out.ptr, test[i].in, out.len),
455 "base64 conversion error - should '%s', is %#B",
456 test[i].in, &out);
457 free(out.ptr);
458 }
459 }
460 END_TEST
461
462 /*******************************************************************************
463 * BASE32 encoding test
464 */
465
466 START_TEST(test_base32)
467 {
468 /* test vectors from RFC 4648:
469 *
470 * BASE32("") = ""
471 * BASE32("f") = "MY======"
472 * BASE32("fo") = "MZXQ===="
473 * BASE32("foo") = "MZXW6==="
474 * BASE32("foob") = "MZXW6YQ="
475 * BASE32("fooba") = "MZXW6YTB"
476 * BASE32("foobar") = "MZXW6YTBOI======"
477 */
478 typedef struct {
479 char *in;
480 char *out;
481 } testdata_t;
482
483 testdata_t test[] = {
484 {"", ""},
485 {"f", "MY======"},
486 {"fo", "MZXQ===="},
487 {"foo", "MZXW6==="},
488 {"foob", "MZXW6YQ="},
489 {"fooba", "MZXW6YTB"},
490 {"foobar", "MZXW6YTBOI======"},
491 };
492 int i;
493
494 for (i = 0; i < countof(test); i++)
495 {
496 chunk_t out;
497
498 out = chunk_to_base32(chunk_create(test[i].in, strlen(test[i].in)), NULL);
499 ck_assert_str_eq(out.ptr, test[i].out);
500 free(out.ptr);
501 }
502 }
503 END_TEST
504
505 /*******************************************************************************
506 * chunk_increment test
507 */
508
509 static struct {
510 bool overflow;
511 chunk_t in;
512 chunk_t out;
513 } increment_data[] = {
514 {TRUE, { NULL, 0 }, { NULL, 0 }},
515 {FALSE, chunk_from_chars(0x00), chunk_from_chars(0x01)},
516 {FALSE, chunk_from_chars(0xfe), chunk_from_chars(0xff)},
517 {TRUE, chunk_from_chars(0xff), chunk_from_chars(0x00)},
518 {FALSE, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x00, 0x01)},
519 {FALSE, chunk_from_chars(0x00, 0xff), chunk_from_chars(0x01, 0x00)},
520 {FALSE, chunk_from_chars(0xfe, 0xff), chunk_from_chars(0xff, 0x00)},
521 {TRUE, chunk_from_chars(0xff, 0xff), chunk_from_chars(0x00, 0x00)},
522 };
523
524 START_TEST(test_increment)
525 {
526 chunk_t chunk;
527 bool overflow;
528
529 chunk = chunk_clonea(increment_data[_i].in);
530 overflow = chunk_increment(chunk);
531 ck_assert(overflow == increment_data[_i].overflow);
532 ck_assert(!increment_data[_i].out.ptr ||
533 chunk_equals(chunk, increment_data[_i].out));
534 }
535 END_TEST
536
537 /*******************************************************************************
538 * chunk_printable tests
539 */
540
541 static struct {
542 bool printable;
543 chunk_t in;
544 char *out;
545 } printable_data[] = {
546 {TRUE, chunk_from_chars(0x31), "1"},
547 {FALSE, chunk_from_chars(0x00), "?"},
548 {FALSE, chunk_from_chars(0x31, 0x00), "1?"},
549 {FALSE, chunk_from_chars(0x00, 0x31), "?1"},
550 {TRUE, chunk_from_chars(0x3f, 0x31), "?1"},
551 {FALSE, chunk_from_chars(0x00, 0x31, 0x00), "?1?"},
552 {FALSE, chunk_from_chars(0x00, 0x31, 0x00, 0x32), "?1?2"},
553 };
554
555 START_TEST(test_printable)
556 {
557 bool printable;
558
559 printable = chunk_printable(printable_data[_i].in, NULL, ' ');
560 ck_assert(printable == printable_data[_i].printable);
561 }
562 END_TEST
563
564 START_TEST(test_printable_sanitize)
565 {
566 chunk_t sane, expected;
567 bool printable;
568
569 printable = chunk_printable(printable_data[_i].in, &sane, '?');
570 ck_assert(printable == printable_data[_i].printable);
571 expected = chunk_from_str(printable_data[_i].out);
572 ck_assert(chunk_equals(sane, expected));
573 chunk_free(&sane);
574 }
575 END_TEST
576
577 START_TEST(test_printable_empty)
578 {
579 chunk_t sane;
580 bool printable;
581
582 printable = chunk_printable(chunk_empty, NULL, ' ');
583 ck_assert(printable);
584
585 sane.ptr = (void*)1;
586 sane.len = 1;
587 printable = chunk_printable(chunk_empty, &sane, ' ');
588 ck_assert(printable);
589 assert_chunk_empty(sane);
590 }
591 END_TEST
592
593 /*******************************************************************************
594 * test for chunk_mac(), i.e. SipHash-2-4
595 */
596
597 /**
598 * SipHash-2-4 output with
599 * k = 00 01 02 ...
600 * and
601 * in = (empty string)
602 * in = 00 (1 byte)
603 * in = 00 01 (2 bytes)
604 * in = 00 01 02 (3 bytes)
605 * ...
606 * in = 00 01 02 ... 3e (63 bytes)
607 */
608 static const u_char sip_vectors[64][8] =
609 {
610 { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
611 { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
612 { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
613 { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
614 { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
615 { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
616 { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
617 { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
618 { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
619 { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
620 { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
621 { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
622 { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
623 { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
624 { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
625 { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
626 { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
627 { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
628 { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
629 { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
630 { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
631 { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
632 { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
633 { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
634 { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
635 { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
636 { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
637 { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
638 { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
639 { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
640 { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
641 { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
642 { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
643 { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
644 { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
645 { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
646 { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
647 { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
648 { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
649 { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
650 { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
651 { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
652 { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
653 { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
654 { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
655 { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
656 { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
657 { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
658 { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
659 { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
660 { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
661 { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
662 { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
663 { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
664 { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
665 { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
666 { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
667 { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
668 { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
669 { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
670 { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
671 { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
672 { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
673 { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, }
674 };
675
676 /**
677 * Our SipHash-2-4 implementation returns the result in host order, which
678 * doesn't matter for practical purposes and even avoids a byte swap. But
679 * because the test vectors are in little-endian we have to account for this
680 * with this custom comparison function.
681 */
682 static inline bool sipeq(const void *a, const void *b, size_t n)
683 {
684 u_char *ap = (u_char*)a, *bp = (u_char*)b;
685 int i;
686
687 for (i = 0; i < n; i++)
688 {
689 #ifdef WORDS_BIGENDIAN
690 if (ap[i] != bp[n - i - 1])
691 #else
692 if (ap[i] != bp[i])
693 #endif
694 {
695 return FALSE;
696 }
697 }
698 return TRUE;
699 }
700
701 START_TEST(test_chunk_mac)
702 {
703 chunk_t in;
704 u_char key[16];
705 u_int64_t out;
706 int i, count;
707
708 count = countof(sip_vectors);
709 in = chunk_alloca(count);
710
711 for (i = 0; i < 16; ++i)
712 {
713 key[i] = i;
714 }
715
716 for (i = 0; i < count; ++i)
717 {
718 in.ptr[i] = i;
719 in.len = i;
720 out = chunk_mac(in, key);
721 fail_unless(sipeq(&out, sip_vectors[i], 8),
722 "test vector failed for %d bytes", i);
723 }
724 }
725 END_TEST
726
727 /*******************************************************************************
728 * test for chunk_hash[_inc]()
729 */
730
731 START_TEST(test_chunk_hash)
732 {
733 chunk_t chunk;
734 u_int32_t hash_a, hash_b, hash_c;
735
736 chunk = chunk_from_str("asdf");
737
738 /* output is randomized, so there are no test-vectors we could use */
739 hash_a = chunk_hash(chunk);
740 hash_b = chunk_hash(chunk);
741 ck_assert(hash_a == hash_b);
742 hash_b = chunk_hash_inc(chunk, hash_a);
743 ck_assert(hash_a != hash_b);
744 hash_c = chunk_hash_inc(chunk, hash_a);
745 ck_assert(hash_b == hash_c);
746 }
747 END_TEST
748
749 /*******************************************************************************
750 * test for chunk_hash_static[_inc]()
751 */
752
753 START_TEST(test_chunk_hash_static)
754 {
755 chunk_t in;
756 u_int32_t out, hash_a, hash_b, hash_inc = 0x7b891a95;
757 int i, count;
758
759 count = countof(sip_vectors);
760 in = chunk_alloca(count);
761
762 for (i = 0; i < count; ++i)
763 {
764 in.ptr[i] = i;
765 in.len = i;
766 /* compared to chunk_mac() we only get half the value back */
767 out = chunk_hash_static(in);
768 fail_unless(sipeq(&out, sip_vectors[i], 4),
769 "test vector failed for %d bytes", i);
770 }
771 hash_a = chunk_hash_static_inc(in, out);
772 ck_assert_int_eq(hash_a, hash_inc);
773 hash_b = chunk_hash_static_inc(in, out);
774 ck_assert_int_eq(hash_a, hash_b);
775 }
776 END_TEST
777
778 /*******************************************************************************
779 * test for chunk_map and friends
780 */
781
782 START_TEST(test_chunk_map)
783 {
784 chunk_t *map, contents = chunk_from_chars(0x01,0x02,0x03,0x04,0x05);
785 char *path = "/tmp/strongswan-chunk-map-test";
786
787 ck_assert(chunk_write(contents, path, "chunk_map", 022, TRUE));
788
789 /* read */
790 map = chunk_map(path, FALSE);
791 ck_assert(map != NULL);
792 ck_assert_msg(chunk_equals(*map, contents), "%B", map);
793 /* altering mapped chunk should not hurt */
794 *map = chunk_empty;
795 ck_assert(chunk_unmap(map));
796
797 /* write */
798 map = chunk_map(path, TRUE);
799 ck_assert(map != NULL);
800 ck_assert_msg(chunk_equals(*map, contents), "%B", map);
801 map->ptr[0] = 0x06;
802 ck_assert(chunk_unmap(map));
803
804 /* verify write */
805 contents.ptr[0] = 0x06;
806 map = chunk_map(path, FALSE);
807 ck_assert(map != NULL);
808 ck_assert_msg(chunk_equals(*map, contents), "%B", map);
809 ck_assert(chunk_unmap(map));
810
811 unlink(path);
812 }
813 END_TEST
814
815 /*******************************************************************************
816 * printf_hook tests
817 */
818
819 static struct {
820 chunk_t in;
821 char *out;
822 char *out_plus;
823 } printf_hook_data[] = {
824 {chunk_from_chars(), "", ""},
825 {chunk_from_chars(0x00), "00", "00"},
826 {chunk_from_chars(0x00, 0x01), "00:01", "0001"},
827 {chunk_from_chars(0x00, 0x01, 0x02), "00:01:02", "000102"},
828 };
829
830 START_TEST(test_printf_hook_hash)
831 {
832 char buf[16];
833 int len;
834
835 len = snprintf(buf, sizeof(buf), "%#B", &printf_hook_data[_i].in);
836 ck_assert(len >= 0 && len < sizeof(buf));
837 ck_assert_str_eq(buf, printf_hook_data[_i].out);
838 }
839 END_TEST
840
841 START_TEST(test_printf_hook_plus)
842 {
843 char buf[16];
844 int len;
845
846 len = snprintf(buf, sizeof(buf), "%+B", &printf_hook_data[_i].in);
847 ck_assert(len >= 0 && len < sizeof(buf));
848 ck_assert_str_eq(buf, printf_hook_data[_i].out_plus);
849 }
850 END_TEST
851
852 START_TEST(test_printf_hook)
853 {
854 char buf[128], mem[128];
855 int len;
856
857 /* %B should be the same as %b, which is what we check, comparing the
858 * acutal result could be tricky as %b prints the chunk's memory address */
859 len = snprintf(buf, sizeof(buf), "%B", &printf_hook_data[_i].in);
860 ck_assert(len >= 0 && len < sizeof(buf));
861 len = snprintf(mem, sizeof(mem), "%b", printf_hook_data[_i].in.ptr,
862 (u_int)printf_hook_data[_i].in.len);
863 ck_assert(len >= 0 && len < sizeof(mem));
864 ck_assert_str_eq(buf, mem);
865 }
866 END_TEST
867
868 Suite *chunk_suite_create()
869 {
870 Suite *s;
871 TCase *tc;
872
873 s = suite_create("chunk");
874
875 tc = tcase_create("equals");
876 tcase_add_test(tc, test_chunk_equals);
877 suite_add_tcase(s, tc);
878
879 tc = tcase_create("chunk_compare");
880 tcase_add_loop_test(tc, test_compare, 0, countof(compare_data));
881 suite_add_tcase(s, tc);
882
883 tc = tcase_create("clear");
884 tcase_add_test(tc, test_chunk_clear);
885 suite_add_tcase(s, tc);
886
887 tc = tcase_create("chunk_length");
888 tcase_add_test(tc, test_chunk_length);
889 suite_add_tcase(s, tc);
890
891 tc = tcase_create("chunk_create_cat");
892 tcase_add_test(tc, test_chunk_create_cat);
893 suite_add_tcase(s, tc);
894
895 tc = tcase_create("chunk_split");
896 tcase_add_test(tc, test_chunk_split);
897 suite_add_tcase(s, tc);
898
899 tc = tcase_create("chunk_skip");
900 tcase_add_test(tc, test_chunk_skip);
901 tcase_add_test(tc, test_chunk_skip_zero);
902 suite_add_tcase(s, tc);
903
904 tc = tcase_create("chunk_increment");
905 tcase_add_loop_test(tc, test_increment, 0, countof(increment_data));
906 suite_add_tcase(s, tc);
907
908 tc = tcase_create("chunk_printable");
909 tcase_add_loop_test(tc, test_printable, 0, countof(printable_data));
910 tcase_add_loop_test(tc, test_printable_sanitize, 0, countof(printable_data));
911 tcase_add_test(tc, test_printable_empty);
912 suite_add_tcase(s, tc);
913
914 tc = tcase_create("baseXX");
915 tcase_add_test(tc, test_base64);
916 tcase_add_test(tc, test_base32);
917 tcase_add_test(tc, test_base16);
918 suite_add_tcase(s, tc);
919
920 tc = tcase_create("chunk_mac");
921 tcase_add_test(tc, test_chunk_mac);
922 suite_add_tcase(s, tc);
923
924 tc = tcase_create("chunk_hash");
925 tcase_add_test(tc, test_chunk_hash);
926 suite_add_tcase(s, tc);
927
928 tc = tcase_create("chunk_hash_static");
929 tcase_add_test(tc, test_chunk_hash_static);
930 suite_add_tcase(s, tc);
931
932 tc = tcase_create("chunk_map");
933 tcase_add_test(tc, test_chunk_map);
934 suite_add_tcase(s, tc);
935
936 tc = tcase_create("printf_hook");
937 tcase_add_loop_test(tc, test_printf_hook_hash, 0, countof(printf_hook_data));
938 tcase_add_loop_test(tc, test_printf_hook_plus, 0, countof(printf_hook_data));
939 tcase_add_loop_test(tc, test_printf_hook, 0, countof(printf_hook_data));
940 suite_add_tcase(s, tc);
941
942 return s;
943 }