implemented append mode for xcbc, testcase
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_chunk.c
1 /*
2 * Copyright (C) 2008 Martin Willi
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 <library.h>
17 #include <daemon.h>
18
19 #define countof(array) (sizeof(array)/sizeof(typeof(array[0])))
20
21 /*******************************************************************************
22 * Base64 encoding/decoding test
23 ******************************************************************************/
24 bool test_chunk_base64()
25 {
26 /* test vectors from RFC4648:
27 *
28 * BASE64("") = ""
29 * BASE64("f") = "Zg=="
30 * BASE64("fo") = "Zm8="
31 * BASE64("foo") = "Zm9v"
32 * BASE64("foob") = "Zm9vYg=="
33 * BASE64("fooba") = "Zm9vYmE="
34 * BASE64("foobar") = "Zm9vYmFy"
35 */
36
37 typedef struct {
38 char *in;
39 char *out;
40 } testdata_t;
41
42 testdata_t test[] = {
43 {"", ""},
44 {"f", "Zg=="},
45 {"fo", "Zm8="},
46 {"foo", "Zm9v"},
47 {"foob", "Zm9vYg=="},
48 {"fooba", "Zm9vYmE="},
49 {"foobar", "Zm9vYmFy"},
50 };
51 int i;
52
53 for (i = 0; i < countof(test); i++)
54 {
55 chunk_t out;
56
57 out = chunk_to_base64(chunk_create(test[i].in, strlen(test[i].in)), NULL);
58
59 if (!streq(out.ptr, test[i].out))
60 {
61 DBG1(DBG_CFG, "base64 conversion error - should %s, is %s",
62 test[i].out, out.ptr);
63 return FALSE;
64 }
65 free(out.ptr);
66 }
67
68 for (i = 0; i < countof(test); i++)
69 {
70 chunk_t out;
71
72 out = chunk_from_base64(chunk_create(test[i].out, strlen(test[i].out)), NULL);
73
74 if (!strneq(out.ptr, test[i].in, out.len))
75 {
76 DBG1(DBG_CFG, "base64 conversion error - should %s, is %#B",
77 test[i].in, &out);
78 return FALSE;
79 }
80 free(out.ptr);
81 }
82 return TRUE;
83 }
84