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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/* Copyright (c) 2010-2015, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
#include "or.h"
#include "test.h"
#define UTIL_FORMAT_PRIVATE
#include "util_format.h"
#define NS_MODULE util_format
static void
test_util_format_base64_encode(void *ignored)
{
(void)ignored;
int res;
int i;
char *src;
char *dst;
src = tor_malloc_zero(256);
dst = tor_malloc_zero(1000);
for(i=0;i<256;i++) {
src[i] = (char)i;
}
res = base64_encode(NULL, 1, src, 1, 0);
tt_int_op(res, OP_EQ, -1);
res = base64_encode(dst, 1, NULL, 1, 0);
tt_int_op(res, OP_EQ, -1);
res = base64_encode(dst, 1, src, 10, 0);
tt_int_op(res, OP_EQ, -1);
res = base64_encode(dst, SSIZE_MAX-1, src, 1, 0);
tt_int_op(res, OP_EQ, -1);
res = base64_encode(dst, SSIZE_MAX-1, src, 10, 0);
tt_int_op(res, OP_EQ, -1);
res = base64_encode(dst, 1000, src, 256, 0);
tt_int_op(res, OP_EQ, 344);
tt_str_op(dst, OP_EQ, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==");
res = base64_encode(dst, 1000, src, 256, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 350);
tt_str_op(dst, OP_EQ, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4v\n"
"MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5f\n"
"YGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6P\n"
"kJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/\n"
"wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v\n"
"8PHy8/T19vf4+fr7/P3+/w==\n");
res = base64_encode(dst, 1000, src+1, 255, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 346);
for(i = 0;i<50;i++) {
src[i] = 0;
}
src[50] = 255;
src[51] = 255;
src[52] = 255;
src[53] = 255;
res = base64_encode(dst, 1000, src, 54, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 74);
res = base64_encode(dst, 1000, src+1, 53, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 74);
res = base64_encode(dst, 1000, src+2, 52, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 74);
res = base64_encode(dst, 1000, src+3, 51, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 70);
res = base64_encode(dst, 1000, src+4, 50, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 70);
res = base64_encode(dst, 1000, src+5, 49, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 70);
res = base64_encode(dst, 1000, src+6, 48, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 65);
res = base64_encode(dst, 1000, src+7, 47, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 65);
res = base64_encode(dst, 1000, src+8, 46, BASE64_ENCODE_MULTILINE);
tt_int_op(res, OP_EQ, 65);
done:
tor_free(src);
tor_free(dst);
}
static void
test_util_format_base64_decode_nopad(void *ignored)
{
(void)ignored;
int res;
int i;
char *src;
uint8_t *dst, *real_dst;
uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
char real_src[] = "ZXhhbXBsZQ";
src = tor_malloc_zero(256);
dst = tor_malloc_zero(1000);
real_dst = tor_malloc_zero(10);
for(i=0;i<256;i++) {
src[i] = (char)i;
}
res = base64_decode_nopad(dst, 1, src, SIZE_T_CEILING);
tt_int_op(res, OP_EQ, -1);
res = base64_decode_nopad(dst, 1, src, 5);
tt_int_op(res, OP_EQ, -1);
res = base64_decode_nopad(real_dst, 10, real_src, 10);
tt_int_op(res, OP_EQ, 7);
tt_mem_op(real_dst, OP_EQ, expected, 7);
done:
tor_free(src);
tor_free(dst);
tor_free(real_dst);
}
static void
test_util_format_base64_decode(void *ignored)
{
(void)ignored;
int res;
int i;
char *src;
char *dst, *real_dst;
uint8_t expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
char real_src[] = "ZXhhbXBsZQ==";
src = tor_malloc_zero(256);
dst = tor_malloc_zero(1000);
real_dst = tor_malloc_zero(10);
for(i=0;i<256;i++) {
src[i] = (char)i;
}
res = base64_decode(dst, 1, src, SIZE_T_CEILING);
tt_int_op(res, OP_EQ, -1);
res = base64_decode(dst, SIZE_T_CEILING+1, src, 10);
tt_int_op(res, OP_EQ, -1);
res = base64_decode(real_dst, 10, real_src, 10);
tt_int_op(res, OP_EQ, 7);
tt_mem_op(real_dst, OP_EQ, expected, 7);
done:
tor_free(src);
tor_free(dst);
tor_free(real_dst);
}
static void
test_util_format_base16_decode(void *ignored)
{
(void)ignored;
int res;
int i;
char *src;
char *dst, *real_dst;
char expected[] = {0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65};
char real_src[] = "6578616D706C65";
src = tor_malloc_zero(256);
dst = tor_malloc_zero(1000);
real_dst = tor_malloc_zero(10);
for(i=0;i<256;i++) {
src[i] = (char)i;
}
res = base16_decode(dst, 3, src, 3);
tt_int_op(res, OP_EQ, -1);
res = base16_decode(dst, 1, src, 10);
tt_int_op(res, OP_EQ, -1);
res = base16_decode(dst, SIZE_T_CEILING+2, src, 10);
tt_int_op(res, OP_EQ, -1);
res = base16_decode(real_dst, 10, real_src, 14);
tt_int_op(res, OP_EQ, 0);
tt_mem_op(real_dst, OP_EQ, expected, 7);
done:
tor_free(src);
tor_free(dst);
tor_free(real_dst);
}
struct testcase_t util_format_tests[] = {
{ "base64_encode", test_util_format_base64_encode, 0, NULL, NULL },
{ "base64_decode_nopad", test_util_format_base64_decode_nopad, 0, NULL, NULL },
{ "base64_decode", test_util_format_base64_decode, 0, NULL, NULL },
{ "base16_decode", test_util_format_base16_decode, 0, NULL, NULL },
END_OF_TESTCASES
};
|