summaryrefslogtreecommitdiff
path: root/src/tests/common/base64_tests.c
blob: 9453c98d825ebfd287582bc3f711b34bb91f6425 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 3 of the License, or
    (at your option) any later version.

    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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tests/common/base64_tests.h"

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "common/base64.h"

#define BUF_LEN 256

static int base64_tests_count(int argc, char *argv[]);
static int base64_tests_run(int argc, char *argv[]);

unit_api base64_tests_api = {
	"Base64 encoding",
	&base64_tests_count,
	&base64_tests_run
};

static int base64_tests_count(int argc, char *argv[])
{
	return 36;
}

static int base64_tests_run(int argc, char *argv[])
{
	int32_t  ret;
	uint8_t  in[BUF_LEN], ref[BUF_LEN], out[BUF_LEN], out2[BUF_LEN];
	uint32_t in_len, ref_len;

	// 1. test vector -> ENC -> DEC
	strcpy((char *)in, "");
	in_len = strlen((char *)in);
	strcpy((char *)ref, "");
	ref_len = strlen((char *)ref);
	ret = base64_encode(in, in_len, out, BUF_LEN);
	cmp_ok(ret, "==", ref_len, "1. test vector - ENC output length");
	ok(memcmp(out, ref, ret) == 0, "1. test vector - ENC output content");
	ret = base64_decode(out, ret, out2, BUF_LEN);
	cmp_ok(ret, "==", in_len, "1. test vector - DEC output length");
	ok(memcmp(out2, in, ret) == 0, "1. test vector - DEC output content");

	// 2. test vector -> ENC -> DEC
	strcpy((char *)in, "f");
	in_len = strlen((char *)in);
	strcpy((char *)ref, "Zg==");
	ref_len = strlen((char *)ref);
	ret = base64_encode(in, in_len, out, BUF_LEN);
	cmp_ok(ret, "==", ref_len, "2. test vector - ENC output length");
	ok(memcmp(out, ref, ret) == 0, "2. test vector - ENC output content");
	ret = base64_decode(out, ret, out2, BUF_LEN);
	cmp_ok(ret, "==", in_len, "2. test vector - DEC output length");
	ok(memcmp(out2, in, ret) == 0, "2. test vector - DEC output content");

	// 3. test vector -> ENC -> DEC
	strcpy((char *)in, "fo");
	in_len = strlen((char *)in);
	strcpy((char *)ref, "Zm8=");
	ref_len = strlen((char *)ref);
	ret = base64_encode(in, in_len, out, BUF_LEN);
	cmp_ok(ret, "==", ref_len, "3. test vector - ENC output length");
	ok(memcmp(out, ref, ret) == 0, "3. test vector - ENC output content");
	ret = base64_decode(out, ret, out2, BUF_LEN);
	cmp_ok(ret, "==", in_len, "3. test vector - DEC output length");
	ok(memcmp(out2, in, ret) == 0, "3. test vector - DEC output content");

	// 4. test vector -> ENC -> DEC
	strcpy((char *)in, "foo");
	in_len = strlen((char *)in);
	strcpy((char *)ref, "Zm9v");
	ref_len = strlen((char *)ref);
	ret = base64_encode(in, in_len, out, BUF_LEN);
	cmp_ok(ret, "==", ref_len, "4. test vector - ENC output length");
	ok(memcmp(out, ref, ret) == 0, "4. test vector - ENC output content");
	ret = base64_decode(out, ret, out2, BUF_LEN);
	cmp_ok(ret, "==", in_len, "4. test vector - DEC output length");
	ok(memcmp(out2, in, ret) == 0, "4. test vector - DEC output content");

	// 5. test vector -> ENC -> DEC
	strcpy((char *)in, "foob");
	in_len = strlen((char *)in);
	strcpy((char *)ref, "Zm9vYg==");
	ref_len = strlen((char *)ref);
	ret = base64_encode(in, in_len, out, BUF_LEN);
	cmp_ok(ret, "==", ref_len, "5. test vector - ENC output length");
	ok(memcmp(out, ref, ret) == 0, "5. test vector - ENC output content");
	ret = base64_decode(out, ret, out2, BUF_LEN);
	cmp_ok(ret, "==", in_len, "5. test vector - DEC output length");
	ok(memcmp(out2, in, ret) == 0, "5. test vector - DEC output content");

	// 6. test vector -> ENC -> DEC
	strcpy((char *)in, "fooba");
	in_len = strlen((char *)in);
	strcpy((char *)ref, "Zm9vYmE=");
	ref_len = strlen((char *)ref);
	ret = base64_encode(in, in_len, out, BUF_LEN);
	cmp_ok(ret, "==", ref_len, "6. test vector - ENC output length");
	ok(memcmp(out, ref, ret) == 0, "6. test vector - ENC output content");
	ret = base64_decode(out, ret, out2, BUF_LEN);
	cmp_ok(ret, "==", in_len, "6. test vector - DEC output length");
	ok(memcmp(out2, in, ret) == 0, "6. test vector - DEC output content");

	// 7. test vector -> ENC -> DEC
	strcpy((char *)in, "foobar");
	in_len = strlen((char *)in);
	strcpy((char *)ref, "Zm9vYmFy");
	ref_len = strlen((char *)ref);
	ret = base64_encode(in, in_len, out, BUF_LEN);
	cmp_ok(ret, "==", ref_len, "7. test vector - ENC output length");
	ok(memcmp(out, ref, ret) == 0, "7. test vector - ENC output content");
	ret = base64_decode(out, ret, out2, BUF_LEN);
	cmp_ok(ret, "==", in_len, "7. test vector - DEC output length");
	ok(memcmp(out2, in, ret) == 0, "7. test vector - DEC output content");

	// Bad paddings
        ret = base64_decode((uint8_t *)"A===", 4, out, BUF_LEN);
        cmp_ok(ret, "==", -2, "Bad padding length 3");
        ret = base64_decode((uint8_t *)"====", 4, out, BUF_LEN);
        cmp_ok(ret, "==", -2, "Bad padding length 4");

	// Bad data length
        ret = base64_decode((uint8_t *)"A", 1, out, BUF_LEN);
        cmp_ok(ret, "==", -1, "Bad data length 1");
        ret = base64_decode((uint8_t *)"AA", 2, out, BUF_LEN);
        cmp_ok(ret, "==", -1, "Bad data length 2");
        ret = base64_decode((uint8_t *)"AAA", 3, out, BUF_LEN);
        cmp_ok(ret, "==", -1, "Bad data length 3");
        ret = base64_decode((uint8_t *)"AAAAA", 5, out, BUF_LEN);
        cmp_ok(ret, "==", -1, "Bad data length 5");

	// Bad data character
        ret = base64_decode((uint8_t *)"AAA$", 4, out, BUF_LEN);
        cmp_ok(ret, "==", -2, "Bad data character dollar");
        ret = base64_decode((uint8_t *)"AAA ", 4, out, BUF_LEN);
        cmp_ok(ret, "==", -2, "Bad data character space");

	return 0;
}