]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/tests/suites/test_chunk.c
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
18 #include "test_suite.h"
20 #include <utils/chunk.h>
22 /*******************************************************************************
26 static void assert_chunk_empty(chunk_t chunk
)
28 ck_assert(chunk
.len
== 0 && chunk
.ptr
== NULL
);
31 /*******************************************************************************
35 START_TEST(test_chunk_equals
)
37 chunk_t chunk
= chunk_from_str("chunk");
38 chunk_t chunk_a
, chunk_b
;
40 chunk_a
= chunk_empty
;
41 chunk_b
= chunk_empty
;
42 ck_assert(!chunk_equals(chunk_a
, chunk_b
));
45 ck_assert(!chunk_equals(chunk_a
, chunk_b
));
47 ck_assert(chunk_equals(chunk_a
, chunk_b
));
49 chunk_b
= chunk_from_str("asdf");
50 ck_assert(!chunk_equals(chunk_a
, chunk_b
));
52 chunk_b
= chunk_from_str("chunk");
53 ck_assert(chunk_equals(chunk_a
, chunk_b
));
57 /*******************************************************************************
66 { 0, { NULL
, 0 }, { NULL
, 0 }},
67 { 0, chunk_from_chars(0x00), chunk_from_chars(0x00)},
68 {-1, chunk_from_chars(0x00), chunk_from_chars(0x01)},
69 { 1, chunk_from_chars(0x01), chunk_from_chars(0x00)},
70 { 0, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x00, 0x00)},
71 {-1, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x00, 0x01)},
72 { 1, chunk_from_chars(0x00, 0x01), chunk_from_chars(0x00, 0x00)},
73 {-1, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x01, 0x00)},
74 { 1, chunk_from_chars(0x01, 0x00), chunk_from_chars(0x00, 0x00)},
75 {-1, chunk_from_chars(0xff), chunk_from_chars(0x00, 0x00)},
76 { 1, chunk_from_chars(0x00, 0x00), chunk_from_chars(0xff)},
79 START_TEST(test_compare
)
83 result
= chunk_compare(compare_data
[_i
].a
, compare_data
[_i
].b
);
84 expected
= compare_data
[_i
].result
;
85 ck_assert((result
== 0 && expected
== 0) ||
86 (result
< 0 && expected
< 0) ||
87 (result
> 0 && expected
> 0));
91 /*******************************************************************************
95 START_TEST(test_chunk_clear
)
106 chunk
= chunk_alloc(64);
108 for (i
= 0; i
< 64; i
++)
113 /* check memory area of freed chunk. We can't use ck_assert() for this
114 * test directly, as it might allocate data at the freed area. */
115 for (i
= 0; i
< 64; i
++)
117 if (ptr
[i
] != 0 && ptr
[i
] == i
)
123 assert_chunk_empty(chunk
);
128 /*******************************************************************************
132 START_TEST(test_chunk_length
)
140 len
= chunk_length("ccc", a
, b
, c
);
141 ck_assert_int_eq(len
, 0);
143 a
= chunk_from_str("foo");
144 b
= chunk_from_str("bar");
145 len
= chunk_length("ccc", a
, b
, c
);
146 ck_assert_int_eq(len
, 6);
148 len
= chunk_length("zcc", a
, b
, c
);
149 ck_assert_int_eq(len
, 0);
151 len
= chunk_length("czc", a
, b
, c
);
152 ck_assert_int_eq(len
, 3);
154 a
= chunk_from_str("foo");
155 b
= chunk_from_str("bar");
156 c
= chunk_from_str("baz");
157 len
= chunk_length("ccc", a
, b
, c
);
158 ck_assert_int_eq(len
, 9);
162 /*******************************************************************************
166 START_TEST(test_chunk_create_cat
)
172 foo
= chunk_from_str("foo");
173 bar
= chunk_from_str("bar");
175 /* to simplify things we use the chunk_cata macro */
179 c
= chunk_cata("cc", a
, b
);
180 ck_assert_int_eq(c
.len
, 0);
181 ck_assert(c
.ptr
!= NULL
);
185 c
= chunk_cata("cc", a
, b
);
186 ck_assert_int_eq(c
.len
, 6);
187 ck_assert(chunk_equals(c
, chunk_from_str("foobar")));
189 a
= chunk_clone(foo
);
190 b
= chunk_clone(bar
);
191 c
= chunk_cata("mm", a
, b
);
192 ck_assert_int_eq(c
.len
, 6);
193 ck_assert(chunk_equals(c
, chunk_from_str("foobar")));
195 a
= chunk_clone(foo
);
196 b
= chunk_clone(bar
);
199 c
= chunk_cata("ss", a
, b
);
200 ck_assert_int_eq(c
.len
, 6);
201 ck_assert(chunk_equals(c
, chunk_from_str("foobar")));
202 /* check memory area of cleared chunk */
203 ck_assert(!chunk_equals(foo
, chunk_create(ptra
, 3)));
204 ck_assert(!chunk_equals(bar
, chunk_create(ptrb
, 3)));
208 /*******************************************************************************
212 static bool mem_in_chunk(u_char
*ptr
, chunk_t chunk
)
214 return ptr
>= chunk
.ptr
&& ptr
< (chunk
.ptr
+ chunk
.len
);
217 START_TEST(test_chunk_split
)
219 chunk_t foo
, bar
, foobar
;
223 foo
= chunk_from_str("foo");
224 bar
= chunk_from_str("bar");
225 foobar
= chunk_from_str("foobar");
227 chunk_split(foobar
, "aa", 3, &a
, 3, &b
);
228 ck_assert(chunk_equals(a
, foo
));
229 ck_assert(chunk_equals(b
, bar
));
230 ck_assert(!mem_in_chunk(a
.ptr
, foobar
));
231 ck_assert(!mem_in_chunk(b
.ptr
, foobar
));
235 chunk_split(foobar
, "mm", 3, &a
, 3, &b
);
236 ck_assert(chunk_equals(a
, foo
));
237 ck_assert(chunk_equals(b
, bar
));
238 ck_assert(mem_in_chunk(a
.ptr
, foobar
));
239 ck_assert(mem_in_chunk(b
.ptr
, foobar
));
241 chunk_split(foobar
, "am", 3, &a
, 3, &b
);
242 ck_assert(chunk_equals(a
, foo
));
243 ck_assert(chunk_equals(b
, bar
));
244 ck_assert(!mem_in_chunk(a
.ptr
, foobar
));
245 ck_assert(mem_in_chunk(b
.ptr
, foobar
));
252 chunk_split(foobar
, "cc", 3, &a
, 3, &b
);
253 ck_assert(chunk_equals(a
, foo
));
254 ck_assert(chunk_equals(b
, bar
));
255 ck_assert(a
.ptr
== ptra
);
256 ck_assert(b
.ptr
== ptrb
);
258 chunk_split(foobar
, "mm", 1, NULL
, 2, &a
, 2, NULL
, 1, &b
);
259 ck_assert(chunk_equals(a
, chunk_from_str("oo")));
260 ck_assert(chunk_equals(b
, chunk_from_str("r")));
262 chunk_split(foobar
, "mm", 6, &a
, 6, &b
);
263 ck_assert(chunk_equals(a
, foobar
));
264 assert_chunk_empty(b
);
266 chunk_split(foobar
, "mac", 12, &a
, 12, &b
, 12, &c
);
267 ck_assert(chunk_equals(a
, foobar
));
268 assert_chunk_empty(b
);
269 assert_chunk_empty(c
);
273 /*******************************************************************************
277 START_TEST(test_chunk_skip
)
281 foobar
= chunk_from_str("foobar");
283 a
= chunk_skip(a
, 0);
284 ck_assert(chunk_equals(a
, foobar
));
285 a
= chunk_skip(a
, 1);
286 ck_assert(chunk_equals(a
, chunk_from_str("oobar")));
287 a
= chunk_skip(a
, 2);
288 ck_assert(chunk_equals(a
, chunk_from_str("bar")));
289 a
= chunk_skip(a
, 3);
290 assert_chunk_empty(a
);
293 a
= chunk_skip(a
, 6);
294 assert_chunk_empty(a
);
297 a
= chunk_skip(a
, 10);
298 assert_chunk_empty(a
);
302 START_TEST(test_chunk_skip_zero
)
307 a
= chunk_skip_zero(a
);
308 assert_chunk_empty(a
);
310 foobar
= chunk_from_str("foobar");
312 a
= chunk_skip_zero(a
);
313 ck_assert(chunk_equals(a
, foobar
));
315 a
= chunk_from_chars(0x00, 0xaa, 0xbb, 0xcc);
316 a
= chunk_skip_zero(a
);
317 ck_assert(chunk_equals(a
, chunk_from_chars(0xaa, 0xbb, 0xcc)));
318 a
= chunk_skip_zero(a
);
319 ck_assert(chunk_equals(a
, chunk_from_chars(0xaa, 0xbb, 0xcc)));
323 /*******************************************************************************
324 * BASE16 encoding test
327 START_TEST(test_base16
)
329 /* test vectors from RFC 4648:
333 * BASE16("fo") = "666F"
334 * BASE16("foo") = "666F6F"
335 * BASE16("foob") = "666F6F62"
336 * BASE16("fooba") = "666F6F6261"
337 * BASE16("foobar") = "666F6F626172"
345 testdata_t test
[] = {
348 {TRUE
, "fo", "666F"},
349 {TRUE
, "foo", "666F6F"},
350 {TRUE
, "foob", "666F6F62"},
351 {TRUE
, "fooba", "666F6F6261"},
352 {TRUE
, "foobar", "666F6F626172"},
355 {FALSE
, "fo", "666f"},
356 {FALSE
, "foo", "666f6f"},
357 {FALSE
, "foob", "666f6f62"},
358 {FALSE
, "fooba", "666f6f6261"},
359 {FALSE
, "foobar", "666f6f626172"},
361 testdata_t test_colon
[] = {
364 {TRUE
, "fo", "66:6F"},
365 {TRUE
, "foo", "66:6F:6F"},
366 {FALSE
, "foob", "66:6f:6f:62"},
367 {FALSE
, "fooba", "66:6f:6f:62:61"},
368 {FALSE
, "foobar", "66:6f:6f:62:61:72"},
369 {FALSE
, "foobar", "66:6f6f:6261:72"},
373 for (i
= 0; i
< countof(test
); i
++)
377 out
= chunk_to_hex(chunk_create(test
[i
].in
, strlen(test
[i
].in
)), NULL
,
379 ck_assert_str_eq(out
.ptr
, test
[i
].out
);
383 for (i
= 0; i
< countof(test
); i
++)
387 out
= chunk_from_hex(chunk_create(test
[i
].out
, strlen(test
[i
].out
)), NULL
);
388 fail_unless(strneq(out
.ptr
, test
[i
].in
, out
.len
),
389 "base16 conversion error - should '%s', is %#B",
394 for (i
= 0; i
< countof(test_colon
); i
++)
398 out
= chunk_from_hex(chunk_create(test_colon
[i
].out
, strlen(test_colon
[i
].out
)), NULL
);
399 fail_unless(strneq(out
.ptr
, test_colon
[i
].in
, out
.len
),
400 "base16 conversion error - should '%s', is %#B",
401 test_colon
[i
].in
, &out
);
407 /*******************************************************************************
408 * BASE64 encoding test
411 START_TEST(test_base64
)
413 /* test vectors from RFC 4648:
416 * BASE64("f") = "Zg=="
417 * BASE64("fo") = "Zm8="
418 * BASE64("foo") = "Zm9v"
419 * BASE64("foob") = "Zm9vYg=="
420 * BASE64("fooba") = "Zm9vYmE="
421 * BASE64("foobar") = "Zm9vYmFy"
428 testdata_t test
[] = {
433 {"foob", "Zm9vYg=="},
434 {"fooba", "Zm9vYmE="},
435 {"foobar", "Zm9vYmFy"},
439 for (i
= 0; i
< countof(test
); i
++)
443 out
= chunk_to_base64(chunk_create(test
[i
].in
, strlen(test
[i
].in
)), NULL
);
444 ck_assert_str_eq(out
.ptr
, test
[i
].out
);
448 for (i
= 0; i
< countof(test
); i
++)
452 out
= chunk_from_base64(chunk_create(test
[i
].out
, strlen(test
[i
].out
)), NULL
);
453 fail_unless(strneq(out
.ptr
, test
[i
].in
, out
.len
),
454 "base64 conversion error - should '%s', is %#B",
461 /*******************************************************************************
462 * BASE32 encoding test
465 START_TEST(test_base32
)
467 /* test vectors from RFC 4648:
470 * BASE32("f") = "MY======"
471 * BASE32("fo") = "MZXQ===="
472 * BASE32("foo") = "MZXW6==="
473 * BASE32("foob") = "MZXW6YQ="
474 * BASE32("fooba") = "MZXW6YTB"
475 * BASE32("foobar") = "MZXW6YTBOI======"
482 testdata_t test
[] = {
487 {"foob", "MZXW6YQ="},
488 {"fooba", "MZXW6YTB"},
489 {"foobar", "MZXW6YTBOI======"},
493 for (i
= 0; i
< countof(test
); i
++)
497 out
= chunk_to_base32(chunk_create(test
[i
].in
, strlen(test
[i
].in
)), NULL
);
498 ck_assert_str_eq(out
.ptr
, test
[i
].out
);
504 /*******************************************************************************
505 * chunk_increment test
512 } increment_data
[] = {
513 {TRUE
, { NULL
, 0 }, { NULL
, 0 }},
514 {FALSE
, chunk_from_chars(0x00), chunk_from_chars(0x01)},
515 {FALSE
, chunk_from_chars(0xfe), chunk_from_chars(0xff)},
516 {TRUE
, chunk_from_chars(0xff), chunk_from_chars(0x00)},
517 {FALSE
, chunk_from_chars(0x00, 0x00), chunk_from_chars(0x00, 0x01)},
518 {FALSE
, chunk_from_chars(0x00, 0xff), chunk_from_chars(0x01, 0x00)},
519 {FALSE
, chunk_from_chars(0xfe, 0xff), chunk_from_chars(0xff, 0x00)},
520 {TRUE
, chunk_from_chars(0xff, 0xff), chunk_from_chars(0x00, 0x00)},
523 START_TEST(test_increment
)
528 chunk
= chunk_clonea(increment_data
[_i
].in
);
529 overflow
= chunk_increment(chunk
);
530 ck_assert(overflow
== increment_data
[_i
].overflow
);
531 ck_assert(!increment_data
[_i
].out
.ptr
||
532 chunk_equals(chunk
, increment_data
[_i
].out
));
536 /*******************************************************************************
537 * chunk_printable tests
544 } printable_data
[] = {
545 {TRUE
, chunk_from_chars(0x31), "1"},
546 {FALSE
, chunk_from_chars(0x00), "?"},
547 {FALSE
, chunk_from_chars(0x31, 0x00), "1?"},
548 {FALSE
, chunk_from_chars(0x00, 0x31), "?1"},
549 {TRUE
, chunk_from_chars(0x3f, 0x31), "?1"},
550 {FALSE
, chunk_from_chars(0x00, 0x31, 0x00), "?1?"},
551 {FALSE
, chunk_from_chars(0x00, 0x31, 0x00, 0x32), "?1?2"},
554 START_TEST(test_printable
)
558 printable
= chunk_printable(printable_data
[_i
].in
, NULL
, ' ');
559 ck_assert(printable
== printable_data
[_i
].printable
);
563 START_TEST(test_printable_sanitize
)
565 chunk_t sane
, expected
;
568 printable
= chunk_printable(printable_data
[_i
].in
, &sane
, '?');
569 ck_assert(printable
== printable_data
[_i
].printable
);
570 expected
= chunk_from_str(printable_data
[_i
].out
);
571 ck_assert(chunk_equals(sane
, expected
));
576 START_TEST(test_printable_empty
)
581 printable
= chunk_printable(chunk_empty
, NULL
, ' ');
582 ck_assert(printable
);
586 printable
= chunk_printable(chunk_empty
, &sane
, ' ');
587 ck_assert(printable
);
588 assert_chunk_empty(sane
);
592 /*******************************************************************************
593 * test for chunk_mac(), i.e. SipHash-2-4
597 * SipHash-2-4 output with
600 * in = (empty string)
602 * in = 00 01 (2 bytes)
603 * in = 00 01 02 (3 bytes)
605 * in = 00 01 02 ... 3e (63 bytes)
607 static const u_char sip_vectors
[64][8] =
609 { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
610 { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
611 { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
612 { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
613 { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
614 { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
615 { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
616 { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
617 { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
618 { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
619 { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
620 { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
621 { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
622 { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
623 { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
624 { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
625 { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
626 { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
627 { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
628 { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
629 { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
630 { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
631 { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
632 { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
633 { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
634 { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
635 { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
636 { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
637 { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
638 { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
639 { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
640 { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
641 { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
642 { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
643 { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
644 { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
645 { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
646 { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
647 { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
648 { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
649 { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
650 { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
651 { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
652 { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
653 { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
654 { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
655 { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
656 { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
657 { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
658 { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
659 { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
660 { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
661 { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
662 { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
663 { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
664 { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
665 { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
666 { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
667 { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
668 { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
669 { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
670 { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
671 { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
672 { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, }
675 START_TEST(test_chunk_mac
)
682 count
= countof(sip_vectors
);
683 in
= chunk_alloca(count
);
685 for (i
= 0; i
< 16; ++i
)
690 for (i
= 0; i
< count
; ++i
)
694 out
= chunk_mac(in
, key
);
695 fail_unless(memeq(&out
, sip_vectors
[i
], 8),
696 "test vector failed for %d bytes", i
);
701 /*******************************************************************************
702 * test for chunk_hash[_inc]()
705 START_TEST(test_chunk_hash
)
708 u_int32_t hash_a
, hash_b
, hash_c
;
710 chunk
= chunk_from_str("asdf");
712 /* output is randomized, so there are no test-vectors we could use */
713 hash_a
= chunk_hash(chunk
);
714 hash_b
= chunk_hash(chunk
);
715 ck_assert(hash_a
== hash_b
);
716 hash_b
= chunk_hash_inc(chunk
, hash_a
);
717 ck_assert(hash_a
!= hash_b
);
718 hash_c
= chunk_hash_inc(chunk
, hash_a
);
719 ck_assert(hash_b
== hash_c
);
723 /*******************************************************************************
724 * test for chunk_hash_static[_inc]()
727 START_TEST(test_chunk_hash_static
)
730 u_int32_t out
, hash_a
, hash_b
, hash_inc
= 0x7b891a95;
733 count
= countof(sip_vectors
);
734 in
= chunk_alloca(count
);
736 for (i
= 0; i
< count
; ++i
)
740 /* compared to chunk_mac() we only get half the value back */
741 out
= chunk_hash_static(in
);
742 fail_unless(memeq(&out
, sip_vectors
[i
], 4),
743 "test vector failed for %d bytes", i
);
745 hash_a
= chunk_hash_static_inc(in
, out
);
746 ck_assert_int_eq(hash_a
, hash_inc
);
747 hash_b
= chunk_hash_static_inc(in
, out
);
748 ck_assert_int_eq(hash_a
, hash_b
);
752 /*******************************************************************************
760 } printf_hook_data
[] = {
761 {chunk_from_chars(), "", ""},
762 {chunk_from_chars(0x00), "00", "00"},
763 {chunk_from_chars(0x00, 0x01), "00:01", "0001"},
764 {chunk_from_chars(0x00, 0x01, 0x02), "00:01:02", "000102"},
767 START_TEST(test_printf_hook_hash
)
772 len
= snprintf(buf
, sizeof(buf
), "%#B", &printf_hook_data
[_i
].in
);
773 ck_assert(len
>= 0 && len
< sizeof(buf
));
774 ck_assert_str_eq(buf
, printf_hook_data
[_i
].out
);
778 START_TEST(test_printf_hook_plus
)
783 len
= snprintf(buf
, sizeof(buf
), "%+B", &printf_hook_data
[_i
].in
);
784 ck_assert(len
>= 0 && len
< sizeof(buf
));
785 ck_assert_str_eq(buf
, printf_hook_data
[_i
].out_plus
);
789 START_TEST(test_printf_hook
)
791 char buf
[128], mem
[128];
794 /* %B should be the same as %b, which is what we check, comparing the
795 * acutal result could be tricky as %b prints the chunk's memory address */
796 len
= snprintf(buf
, sizeof(buf
), "%B", &printf_hook_data
[_i
].in
);
797 ck_assert(len
>= 0 && len
< sizeof(buf
));
798 len
= snprintf(mem
, sizeof(mem
), "%b", printf_hook_data
[_i
].in
.ptr
,
799 (u_int
)printf_hook_data
[_i
].in
.len
);
800 ck_assert(len
>= 0 && len
< sizeof(mem
));
801 ck_assert_str_eq(buf
, mem
);
805 Suite
*chunk_suite_create()
810 s
= suite_create("chunk");
812 tc
= tcase_create("equals");
813 tcase_add_test(tc
, test_chunk_equals
);
814 suite_add_tcase(s
, tc
);
816 tc
= tcase_create("chunk_compare");
817 tcase_add_loop_test(tc
, test_compare
, 0, countof(compare_data
));
818 suite_add_tcase(s
, tc
);
820 tc
= tcase_create("clear");
821 tcase_add_test(tc
, test_chunk_clear
);
822 suite_add_tcase(s
, tc
);
824 tc
= tcase_create("chunk_length");
825 tcase_add_test(tc
, test_chunk_length
);
826 suite_add_tcase(s
, tc
);
828 tc
= tcase_create("chunk_create_cat");
829 tcase_add_test(tc
, test_chunk_create_cat
);
830 suite_add_tcase(s
, tc
);
832 tc
= tcase_create("chunk_split");
833 tcase_add_test(tc
, test_chunk_split
);
834 suite_add_tcase(s
, tc
);
836 tc
= tcase_create("chunk_skip");
837 tcase_add_test(tc
, test_chunk_skip
);
838 tcase_add_test(tc
, test_chunk_skip_zero
);
839 suite_add_tcase(s
, tc
);
841 tc
= tcase_create("chunk_increment");
842 tcase_add_loop_test(tc
, test_increment
, 0, countof(increment_data
));
843 suite_add_tcase(s
, tc
);
845 tc
= tcase_create("chunk_printable");
846 tcase_add_loop_test(tc
, test_printable
, 0, countof(printable_data
));
847 tcase_add_loop_test(tc
, test_printable_sanitize
, 0, countof(printable_data
));
848 tcase_add_test(tc
, test_printable_empty
);
849 suite_add_tcase(s
, tc
);
851 tc
= tcase_create("baseXX");
852 tcase_add_test(tc
, test_base64
);
853 tcase_add_test(tc
, test_base32
);
854 tcase_add_test(tc
, test_base16
);
855 suite_add_tcase(s
, tc
);
857 tc
= tcase_create("chunk_mac");
858 tcase_add_test(tc
, test_chunk_mac
);
859 suite_add_tcase(s
, tc
);
861 tc
= tcase_create("chunk_hash");
862 tcase_add_test(tc
, test_chunk_hash
);
863 suite_add_tcase(s
, tc
);
865 tc
= tcase_create("chunk_hash_static");
866 tcase_add_test(tc
, test_chunk_hash_static
);
867 suite_add_tcase(s
, tc
);
869 tc
= tcase_create("printf_hook");
870 tcase_add_loop_test(tc
, test_printf_hook_hash
, 0, countof(printf_hook_data
));
871 tcase_add_loop_test(tc
, test_printf_hook_plus
, 0, countof(printf_hook_data
));
872 tcase_add_loop_test(tc
, test_printf_hook
, 0, countof(printf_hook_data
));
873 suite_add_tcase(s
, tc
);