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