File: test_base64.c

package info (click to toggle)
libgeier 0.13-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,832 kB
file content (123 lines) | stat: -rw-r--r-- 3,130 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2005  Juergen Stuber <juergen@jstuber.net>, Germany
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <string.h>

#include <base64.h>

/* Test cases from RFC 3548 */
unsigned char d1[6] = {0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e};
unsigned char e1[8] = "FPucA9l+";

unsigned char d2[5] = {0x14, 0xfb, 0x9c, 0x03, 0xd9};
unsigned char e2[8] = "FPucA9k=";

unsigned char d3[4] = {0x14, 0xfb, 0x9c, 0x03};
unsigned char e3[8] = "FPucAw==";

static int test_encode(unsigned char *d, size_t inlen,
		       unsigned char *e, size_t outlen);
static int test_decode(unsigned char *e, size_t inlen,
		       unsigned char *d, size_t outlen);


int main(int argc, char *argv[])
{
	int error_count = 0;

	error_count += test_encode(d1, sizeof(d1), e1, sizeof(e1));
	error_count += test_encode(d2, sizeof(d2), e2, sizeof(e2));
	error_count += test_encode(d3, sizeof(d3), e3, sizeof(e3));

	error_count += test_decode(e1, sizeof(e1), d1, sizeof(d1));
	error_count += test_decode(e2, sizeof(e2), d2, sizeof(d2));
	error_count += test_decode(e3, sizeof(e3), d3, sizeof(d3));

	if (error_count) {
		fprintf(stderr, "%s: %d errors\n", argv[0], error_count);
	}
	return error_count == 0 ? 0 : 1;
}

static int test_encode(unsigned char *d, size_t inlen,
		       unsigned char *e, size_t outlen)
{
	unsigned char *c;
	size_t cl;
	int result;

	result = geier_base64_encode(d, inlen, &c, &cl);
	if (outlen != cl || memcmp(e, c, cl) != 0) {
		int i;

		fprintf(stderr, "Expected: >");
		for (i=0; i<outlen; i++) {
			fprintf(stderr, "%c", e[i]);
		}
		fprintf(stderr, "<\n");

		fprintf(stderr, "result = %d\n", result);
		if (!result) {
			
			fprintf(stderr, "Computed: >");
			for (i=0; i<cl; i++) {
				fprintf(stderr, "%c", c[i]);
			}
			fprintf(stderr, "<\n");
		}
		return 1;
	}
	else {
		return 0;
	}
}


static int test_decode(unsigned char *e, size_t inlen,
		       unsigned char *d, size_t outlen)
{
	unsigned char *c;
	size_t cl;
	int result;

	result = geier_base64_decode(e, inlen, &c, &cl);
	if (outlen != cl || memcmp(d, c, cl) != 0) {
		int i;

		fprintf(stderr, "Expected: >");
		for (i=0; i<outlen; i++) {
			fprintf(stderr, " 0x%02x", d[i]);
		}
		fprintf(stderr, "<\n");

		fprintf(stderr, "result = %d\n", result);

		if (!result) {			
			fprintf(stderr, "Computed: >");
			for (i=0; i<cl; i++) {
				fprintf(stderr, " 0x%02x", c[i]);
			}
			fprintf(stderr, "<\n");
		}
		return 1;
	}
	else {
		return 0;
	}
}