summaryrefslogtreecommitdiffstats
path: root/comm/third_party/libotr/tests/unit/test_b64.c
blob: 6d5599288e508b0b1e3157706af79578e2d7560c (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
/*
 * Copyright (C) 2014 - Julien Voisin <julien.voisin@dustri.org>
 *                      David Goulet <dgoulet@ev0ke.net>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License, version 2 only, as
 * published by the Free Software Foundation.
 *
 * 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., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

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

#include <b64.h>
#include <tap/tap.h>
#include <proto.h>

GCRY_THREAD_OPTION_PTHREAD_IMPL;

#define NUM_TESTS 10

const char *alphanum_encoded =
	"?OTR:" "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwCg==" ".";
const char *alphanum_decoded =
	"abcdefghijklmnopqrstuvwxyz1234567890\n";

static void test_otrl_base64_otr_decode(void)
{
	int ret;
	unsigned char *bufp = NULL;
	size_t len = 0;

	/*
	 * Invalid decoding.
	 */

	ok(otrl_base64_otr_decode("hello", NULL, NULL) == -2,
			"Call with no prefix returned an error");
	ok(otrl_base64_otr_decode("?OTR:" "MTIzNAo=", NULL, NULL) == -2,
			"Call with no suffix returned an error");
	/* Message of size 0. */
	ret = otrl_base64_otr_decode("", &bufp, &len);
	ok(ret == -2 && bufp == NULL && len == 0,
			"Decode b64 with message of len 0");
	/*
	 * Valid decoding.
	 */

	/* Invalid chars are ignored */
	ok(otrl_base64_otr_decode("?OTR:invalid_base64_thing.", &bufp, &len) == 0
			&& len == 12, "Invalid b64 data");
	free(bufp);
	bufp = NULL;
	len = 0;

	ok(otrl_base64_otr_decode(alphanum_encoded, &bufp, &len) == 0,
			"Call with valid data successfull");
	ok(strcmp((const char*)bufp, alphanum_decoded) == 0
			&& len == 37, "Decoded valid b64 test vector with success");
	free(bufp);
	bufp = NULL;
	len = 0;

	/* Invalid base64 char. */
	ret = otrl_base64_otr_decode("?OTR:_*&?!!*\"().", &bufp, &len);
	ok(ret == 0 && bufp != NULL && len == 0,
			"Decode b64 with invalid b64 characters");
	free(bufp);
	bufp = NULL;
	len = 0;

}

static void test_otrl_base64_otr_encode(void)
{
	unsigned char *bufp = NULL;
	size_t len = 0;
	char *encoded;

	encoded = otrl_base64_otr_encode((const unsigned char *) alphanum_decoded,
			strlen(alphanum_decoded));

	ok(strcmp(encoded, alphanum_encoded) == 0,
			"Encoded b64 test vector with success");
	ok(otrl_base64_otr_decode(encoded, &bufp, &len) == 0,
			"Decoded previously encoded test vector");
	ok(memcmp(bufp, alphanum_decoded, len) == 0
		&& len == strlen(alphanum_decoded),
		"Decoded value is exact");
	free(bufp);
	free(encoded);
}

int main(int argc, char** argv)
{
	plan_tests(NUM_TESTS);

	gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
	OTRL_INIT;

	test_otrl_base64_otr_decode();
	test_otrl_base64_otr_encode();

	return 0;
}