Converted tests for chunk_t
[strongswan.git] / src / libstrongswan / tests / 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
18 #include <check.h>
19
20 #include <utils/chunk.h>
21
22 /*******************************************************************************
23 * BASE64 encoding test
24 */
25
26 START_TEST(test_base64)
27 {
28 /* test vectors from RFC4648:
29 *
30 * BASE64("") = ""
31 * BASE64("f") = "Zg=="
32 * BASE64("fo") = "Zm8="
33 * BASE64("foo") = "Zm9v"
34 * BASE64("foob") = "Zm9vYg=="
35 * BASE64("fooba") = "Zm9vYmE="
36 * BASE64("foobar") = "Zm9vYmFy"
37 */
38 typedef struct {
39 char *in;
40 char *out;
41 } testdata_t;
42
43 testdata_t test[] = {
44 {"", ""},
45 {"f", "Zg=="},
46 {"fo", "Zm8="},
47 {"foo", "Zm9v"},
48 {"foob", "Zm9vYg=="},
49 {"fooba", "Zm9vYmE="},
50 {"foobar", "Zm9vYmFy"},
51 };
52 int i;
53
54 for (i = 0; i < countof(test); i++)
55 {
56 chunk_t out;
57
58 out = chunk_to_base64(chunk_create(test[i].in, strlen(test[i].in)), NULL);
59 ck_assert_str_eq(out.ptr, test[i].out);
60 free(out.ptr);
61 }
62
63 for (i = 0; i < countof(test); i++)
64 {
65 chunk_t out;
66
67 out = chunk_from_base64(chunk_create(test[i].out, strlen(test[i].out)), NULL);
68 fail_unless(strneq(out.ptr, test[i].in, out.len),
69 "base64 conversion error - should '%s', is %#B",
70 test[i].in, &out);
71 free(out.ptr);
72 }
73 }
74 END_TEST
75
76 Suite *chunk_suite_create()
77 {
78 Suite *s;
79 TCase *tc;
80
81 s = suite_create("chunk");
82
83 tc = tcase_create("base64");
84 tcase_add_test(tc, test_base64);
85 suite_add_tcase(s, tc);
86
87 return s;
88 }