2 * Copyright (C) 2013 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include "test_suite.h"
21 #include <utils/chunk.h>
23 /*******************************************************************************
27 static void assert_chunk_empty(chunk_t chunk
)
29 ck_assert(chunk
.len
== 0 && chunk
.ptr
== NULL
);
32 /*******************************************************************************
36 START_TEST(test_chunk_equals
)
38 chunk_t chunk
= chunk_from_str("chunk");
39 chunk_t chunk_a
, chunk_b
;
41 chunk_a
= chunk_empty
;
42 chunk_b
= chunk_empty
;
43 ck_assert(!chunk_equals(chunk_a
, chunk_b
));
46 ck_assert(!chunk_equals(chunk_a
, chunk_b
));
48 ck_assert(chunk_equals(chunk_a
, chunk_b
));
50 chunk_b
= chunk_from_str("asdf");
51 ck_assert(!chunk_equals(chunk_a
, chunk_b
));
53 chunk_b
= chunk_from_str("chunk");
54 ck_assert(chunk_equals(chunk_a
, chunk_b
));
58 /*******************************************************************************
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)},
80 START_TEST(test_compare
)
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));
92 /*******************************************************************************
96 START_TEST(test_chunk_clear
)
107 chunk
= chunk_alloc(64);
109 for (i
= 0; i
< 64; i
++)
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
++)
118 if (ptr
[i
] != 0 && ptr
[i
] == i
)
124 assert_chunk_empty(chunk
);
129 /*******************************************************************************
133 START_TEST(test_chunk_length
)
141 len
= chunk_length("ccc", a
, b
, c
);
142 ck_assert_int_eq(len
, 0);
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);
149 len
= chunk_length("zcc", a
, b
, c
);
150 ck_assert_int_eq(len
, 0);
152 len
= chunk_length("czc", a
, b
, c
);
153 ck_assert_int_eq(len
, 3);
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);
163 /*******************************************************************************
167 START_TEST(test_chunk_create_cat
)
173 foo
= chunk_from_str("foo");
174 bar
= chunk_from_str("bar");
176 /* to simplify things we use the chunk_cata macro */
180 c
= chunk_cata("cc", a
, b
);
181 ck_assert_int_eq(c
.len
, 0);
182 ck_assert(c
.ptr
!= NULL
);
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")));
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")));
196 a
= chunk_clone(foo
);
197 b
= chunk_clone(bar
);
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)));
209 /*******************************************************************************
213 static bool mem_in_chunk(u_char
*ptr
, chunk_t chunk
)
215 return ptr
>= chunk
.ptr
&& ptr
< (chunk
.ptr
+ chunk
.len
);
218 START_TEST(test_chunk_split
)
220 chunk_t foo
, bar
, foobar
;
224 foo
= chunk_from_str("foo");
225 bar
= chunk_from_str("bar");
226 foobar
= chunk_from_str("foobar");
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
));
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
));
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
));
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
);
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")));
263 chunk_split(foobar
, "mm", 6, &a
, 6, &b
);
264 ck_assert(chunk_equals(a
, foobar
));
265 assert_chunk_empty(b
);
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
);
274 /*******************************************************************************
278 START_TEST(test_chunk_skip
)
282 foobar
= chunk_from_str("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
);
294 a
= chunk_skip(a
, 6);
295 assert_chunk_empty(a
);
298 a
= chunk_skip(a
, 10);
299 assert_chunk_empty(a
);
303 START_TEST(test_chunk_skip_zero
)
308 a
= chunk_skip_zero(a
);
309 assert_chunk_empty(a
);
311 foobar
= chunk_from_str("foobar");
313 a
= chunk_skip_zero(a
);
314 ck_assert(chunk_equals(a
, foobar
));
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)));
324 /*******************************************************************************
325 * BASE16 encoding test
328 START_TEST(test_base16
)
330 /* test vectors from RFC 4648:
334 * BASE16("fo") = "666F"
335 * BASE16("foo") = "666F6F"
336 * BASE16("foob") = "666F6F62"
337 * BASE16("fooba") = "666F6F6261"
338 * BASE16("foobar") = "666F6F626172"
346 testdata_t test
[] = {
349 {TRUE
, "fo", "666F"},
350 {TRUE
, "foo", "666F6F"},
351 {TRUE
, "foob", "666F6F62"},
352 {TRUE
, "fooba", "666F6F6261"},
353 {TRUE
, "foobar", "666F6F626172"},
356 {FALSE
, "fo", "666f"},
357 {FALSE
, "foo", "666f6f"},
358 {FALSE
, "foob", "666f6f62"},
359 {FALSE
, "fooba", "666f6f6261"},
360 {FALSE
, "foobar", "666f6f626172"},
362 testdata_t test_colon
[] = {
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"},
374 for (i
= 0; i
< countof(test
); i
++)
378 out
= chunk_to_hex(chunk_create(test
[i
].in
, strlen(test
[i
].in
)), NULL
,
380 ck_assert_str_eq(out
.ptr
, test
[i
].out
);
384 for (i
= 0; i
< countof(test
); i
++)
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",
395 for (i
= 0; i
< countof(test_colon
); i
++)
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
);
408 /*******************************************************************************
409 * BASE64 encoding test
412 START_TEST(test_base64
)
414 /* test vectors from RFC 4648:
417 * BASE64("f") = "Zg=="
418 * BASE64("fo") = "Zm8="
419 * BASE64("foo") = "Zm9v"
420 * BASE64("foob") = "Zm9vYg=="
421 * BASE64("fooba") = "Zm9vYmE="
422 * BASE64("foobar") = "Zm9vYmFy"
429 testdata_t test
[] = {
434 {"foob", "Zm9vYg=="},
435 {"fooba", "Zm9vYmE="},
436 {"foobar", "Zm9vYmFy"},
440 for (i
= 0; i
< countof(test
); i
++)
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
);
449 for (i
= 0; i
< countof(test
); i
++)
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",
462 /*******************************************************************************
463 * BASE32 encoding test
466 START_TEST(test_base32
)
468 /* test vectors from RFC 4648:
471 * BASE32("f") = "MY======"
472 * BASE32("fo") = "MZXQ===="
473 * BASE32("foo") = "MZXW6==="
474 * BASE32("foob") = "MZXW6YQ="
475 * BASE32("fooba") = "MZXW6YTB"
476 * BASE32("foobar") = "MZXW6YTBOI======"
483 testdata_t test
[] = {
488 {"foob", "MZXW6YQ="},
489 {"fooba", "MZXW6YTB"},
490 {"foobar", "MZXW6YTBOI======"},
494 for (i
= 0; i
< countof(test
); i
++)
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
);
505 /*******************************************************************************
506 * chunk_increment test
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)},
524 START_TEST(test_increment
)
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
));
537 /*******************************************************************************
538 * chunk_printable tests
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"},
555 START_TEST(test_printable
)
559 printable
= chunk_printable(printable_data
[_i
].in
, NULL
, ' ');
560 ck_assert(printable
== printable_data
[_i
].printable
);
564 START_TEST(test_printable_sanitize
)
566 chunk_t sane
, expected
;
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
));
577 START_TEST(test_printable_empty
)
582 printable
= chunk_printable(chunk_empty
, NULL
, ' ');
583 ck_assert(printable
);
587 printable
= chunk_printable(chunk_empty
, &sane
, ' ');
588 ck_assert(printable
);
589 assert_chunk_empty(sane
);
593 /*******************************************************************************
594 * test for chunk_mac(), i.e. SipHash-2-4
598 * SipHash-2-4 output with
601 * in = (empty string)
603 * in = 00 01 (2 bytes)
604 * in = 00 01 02 (3 bytes)
606 * in = 00 01 02 ... 3e (63 bytes)
608 static const u_char sip_vectors
[64][8] =
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, }
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.
682 static inline bool sipeq(const void *a
, const void *b
, size_t n
)
684 u_char
*ap
= (u_char
*)a
, *bp
= (u_char
*)b
;
687 for (i
= 0; i
< n
; i
++)
689 #ifdef WORDS_BIGENDIAN
690 if (ap
[i
] != bp
[n
- i
- 1])
701 START_TEST(test_chunk_mac
)
708 count
= countof(sip_vectors
);
709 in
= chunk_alloca(count
);
711 for (i
= 0; i
< 16; ++i
)
716 for (i
= 0; i
< count
; ++i
)
720 out
= chunk_mac(in
, key
);
721 fail_unless(sipeq(&out
, sip_vectors
[i
], 8),
722 "test vector failed for %d bytes", i
);
727 /*******************************************************************************
728 * test for chunk_hash[_inc]()
731 START_TEST(test_chunk_hash
)
734 u_int32_t hash_a
, hash_b
, hash_c
;
736 chunk
= chunk_from_str("asdf");
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
);
749 /*******************************************************************************
750 * test for chunk_hash_static[_inc]()
753 START_TEST(test_chunk_hash_static
)
756 u_int32_t out
, hash_a
, hash_b
, hash_inc
= 0x7b891a95;
759 count
= countof(sip_vectors
);
760 in
= chunk_alloca(count
);
762 for (i
= 0; i
< count
; ++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
);
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
);
778 /*******************************************************************************
779 * test for chunk_map and friends
782 START_TEST(test_chunk_map
)
784 chunk_t
*map
, contents
= chunk_from_chars(0x01,0x02,0x03,0x04,0x05);
785 char *path
= "/tmp/strongswan-chunk-map-test";
787 ck_assert(chunk_write(contents
, path
, "chunk_map", 022, TRUE
));
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 */
795 ck_assert(chunk_unmap(map
));
798 map
= chunk_map(path
, TRUE
);
799 ck_assert(map
!= NULL
);
800 ck_assert_msg(chunk_equals(*map
, contents
), "%B", map
);
802 ck_assert(chunk_unmap(map
));
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
));
815 /*******************************************************************************
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"},
830 START_TEST(test_printf_hook_hash
)
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
);
841 START_TEST(test_printf_hook_plus
)
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
);
852 START_TEST(test_printf_hook
)
854 char buf
[128], mem
[128];
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
);
868 Suite
*chunk_suite_create()
873 s
= suite_create("chunk");
875 tc
= tcase_create("equals");
876 tcase_add_test(tc
, test_chunk_equals
);
877 suite_add_tcase(s
, tc
);
879 tc
= tcase_create("chunk_compare");
880 tcase_add_loop_test(tc
, test_compare
, 0, countof(compare_data
));
881 suite_add_tcase(s
, tc
);
883 tc
= tcase_create("clear");
884 tcase_add_test(tc
, test_chunk_clear
);
885 suite_add_tcase(s
, tc
);
887 tc
= tcase_create("chunk_length");
888 tcase_add_test(tc
, test_chunk_length
);
889 suite_add_tcase(s
, tc
);
891 tc
= tcase_create("chunk_create_cat");
892 tcase_add_test(tc
, test_chunk_create_cat
);
893 suite_add_tcase(s
, tc
);
895 tc
= tcase_create("chunk_split");
896 tcase_add_test(tc
, test_chunk_split
);
897 suite_add_tcase(s
, tc
);
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
);
904 tc
= tcase_create("chunk_increment");
905 tcase_add_loop_test(tc
, test_increment
, 0, countof(increment_data
));
906 suite_add_tcase(s
, tc
);
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
);
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
);
920 tc
= tcase_create("chunk_mac");
921 tcase_add_test(tc
, test_chunk_mac
);
922 suite_add_tcase(s
, tc
);
924 tc
= tcase_create("chunk_hash");
925 tcase_add_test(tc
, test_chunk_hash
);
926 suite_add_tcase(s
, tc
);
928 tc
= tcase_create("chunk_hash_static");
929 tcase_add_test(tc
, test_chunk_hash_static
);
930 suite_add_tcase(s
, tc
);
932 tc
= tcase_create("chunk_map");
933 tcase_add_test(tc
, test_chunk_map
);
934 suite_add_tcase(s
, tc
);
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
);