Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/krb5/src/util/support/t_base64.c
34889 views
1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* util/support/t_base64.c - base64 encoding and decoding tests */
3
/*
4
* Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
5
* (Royal Institute of Technology, Stockholm, Sweden).
6
* All rights reserved.
7
*
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
10
* are met:
11
*
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
14
*
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
*
19
* 3. Neither the name of the Institute nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
22
*
23
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
* SUCH DAMAGE.
34
*/
35
36
#include <k5-platform.h>
37
#include <k5-base64.h>
38
39
static struct test {
40
void *data;
41
size_t len;
42
const char *result;
43
} tests[] = {
44
{ "", 0 , "" },
45
{ "1", 1, "MQ==" },
46
{ "22", 2, "MjI=" },
47
{ "333", 3, "MzMz" },
48
{ "4444", 4, "NDQ0NA==" },
49
{ "55555", 5, "NTU1NTU=" },
50
{ "abc:def", 7, "YWJjOmRlZg==" },
51
{ "f", 1, "Zg==" },
52
{ "fo", 2, "Zm8=" },
53
{ "foo", 3, "Zm9v" },
54
{ "foob", 4, "Zm9vYg==" },
55
{ "fooba", 5, "Zm9vYmE=" },
56
{ "foobar", 6, "Zm9vYmFy" },
57
{ NULL, 0, NULL }
58
};
59
60
static char *negative_tests[] = {
61
"M=M=",
62
"MM=M",
63
"MQ===",
64
"====",
65
"M===",
66
NULL
67
};
68
69
int
70
main(int argc, char **argv)
71
{
72
char *str, **ntest;
73
void *data;
74
int numerr = 0, numtest = 1;
75
const struct test *t;
76
size_t len;
77
78
for (t = tests; t->data != NULL; t++) {
79
str = k5_base64_encode(t->data, t->len);
80
if (strcmp(str, t->result) != 0) {
81
fprintf(stderr, "failed test %d: %s != %s\n", numtest,
82
str, t->result);
83
numerr++;
84
}
85
free(str);
86
data = k5_base64_decode(t->result, &len);
87
if (len != t->len) {
88
fprintf(stderr, "failed test %d: len %lu != %lu\n", numtest,
89
(unsigned long)len, (unsigned long)t->len);
90
numerr++;
91
} else if (memcmp(data, t->data, t->len) != 0) {
92
fprintf(stderr, "failed test %d: data\n", numtest);
93
numerr++;
94
}
95
free(data);
96
numtest++;
97
}
98
99
for (ntest = negative_tests; *ntest != NULL; ntest++) {
100
data = k5_base64_decode(*ntest, &len);
101
if (data != NULL || len != SIZE_MAX) {
102
fprintf(stderr, "failed test %d: successful decode: %s\n",
103
numtest, *ntest);
104
numerr++;
105
}
106
numtest++;
107
}
108
109
return numerr ? 1 : 0;
110
}
111
112