summaryrefslogtreecommitdiff
path: root/src/charon/plugins/unit_tester/tests/test_chunk.c
blob: 5356c1d968fac732be78864a90b27eeb8a104921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (C) 2008 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <library.h>
#include <daemon.h>

/*******************************************************************************
 * Base64 encoding/decoding test
 ******************************************************************************/
bool test_chunk_base64()
{
	/* test vectors from RFC4648:
	 *
	 * BASE64("") = ""
	 * BASE64("f") = "Zg=="
	 * BASE64("fo") = "Zm8="
	 * BASE64("foo") = "Zm9v"
	 * BASE64("foob") = "Zm9vYg=="
	 * BASE64("fooba") = "Zm9vYmE="
	 * BASE64("foobar") = "Zm9vYmFy"
	 */
	
	typedef struct {
		char *in;
		char *out;
	} testdata_t;
	
	testdata_t test[] = {
		{"", ""},
		{"f", "Zg=="},
		{"fo", "Zm8="},
		{"foo", "Zm9v"},
		{"foob", "Zm9vYg=="},
		{"fooba", "Zm9vYmE="},
		{"foobar", "Zm9vYmFy"},
	};
	int i;
	
	for (i = 0; i < countof(test); i++)
	{
		chunk_t out;
		
		out = chunk_to_base64(chunk_create(test[i].in, strlen(test[i].in)), NULL);
		
		if (!streq(out.ptr, test[i].out))
		{
			DBG1(DBG_CFG, "base64 conversion error - should %s, is %s", 
				test[i].out, out.ptr);
			return FALSE;
		}
		free(out.ptr);
	}
	
	for (i = 0; i < countof(test); i++)
	{
		chunk_t out;
		
		out = chunk_from_base64(chunk_create(test[i].out, strlen(test[i].out)), NULL);
		
		if (!strneq(out.ptr, test[i].in, out.len))
		{
			DBG1(DBG_CFG, "base64 conversion error - should %s, is %#B", 
				test[i].in, &out);
			return FALSE;
		}
		free(out.ptr);
	}
	return TRUE;
}