This repository was archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmodp_b64_test.c
266 lines (237 loc) · 7.86 KB
/
modp_b64_test.c
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* we compile as C90 but use snprintf */
#define _ISOC99_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "minunit.h"
#include "modp_b64.h"
/**
* checks to make sure results are the same reguardless of
* CPU endian type (i.e. Intel vs. Sparc, etc)
*
*/
static char* testEndian(void)
{
/* this test that "1" is "AAAB" */
char buf[100];
char result[10];
char endian[] = { (char)0, (char)0, (char)1 };
size_t d = modp_b64_encode(buf, endian, (size_t)3);
mu_assert_int_equals(4, d);
mu_assert_int_equals('A', buf[0]);
mu_assert_int_equals('A', buf[1]);
mu_assert_int_equals('A', buf[2]);
mu_assert_int_equals('B', buf[3]);
memset(result, 255, sizeof(result));
d = modp_b64_decode(result, "AAAB", (size_t)4);
mu_assert_int_equals(3, d);
mu_assert_int_equals(0, result[0]);
mu_assert_int_equals(0, result[1]);
mu_assert_int_equals(1, result[2]);
mu_assert_int_equals(-1, result[3]);
return 0;
}
/**
* sending 0 as length to encode and decode
* should basically do nothing
*/
static char* testEmpty(void)
{
char buf[10];
const char* input = 0; /* null */
size_t d;
memset(buf, 1, sizeof(buf));
d = modp_b64_encode(buf, input, (size_t)0);
mu_assert_int_equals(0, d);
mu_assert_int_equals(0, buf[0]);
mu_assert_int_equals(1, buf[1]);
memset(buf, 1, sizeof(buf));
d = modp_b64_decode(buf, input, (size_t)0);
mu_assert_int_equals(0, d);
mu_assert_int_equals(1, buf[0]);
mu_assert_int_equals(1, buf[1]);
return 0;
}
/**
* Test 1-6 bytes input and decode
*
*/
static char* testPadding(void)
{
char msg[100];
const char ibuf[6] = { 1, 1, 1, 1, 1, 1 };
char obuf[10];
char rbuf[10];
size_t d = 0;
/* 1 in, 4 out */
memset(obuf, 255, sizeof(obuf));
d = modp_b64_encode(obuf, ibuf, (size_t)1);
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
mu_assert_int_equals_msg(msg, 4, d);
mu_assert_int_equals_msg(msg, 0, obuf[4]);
memset(rbuf, 255, sizeof(rbuf));
d = modp_b64_decode(rbuf, obuf, d);
mu_assert_int_equals_msg(msg, 1, d);
mu_assert_int_equals(1, rbuf[0]);
mu_assert_int_equals(-1, rbuf[1]);
/* 2 in, 4 out */
memset(obuf, 255, sizeof(obuf));
d = modp_b64_encode(obuf, ibuf, (size_t)2);
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
mu_assert_int_equals_msg(msg, 4, d);
mu_assert_int_equals_msg(msg, 0, obuf[4]);
memset(rbuf, 255, sizeof(rbuf));
d = modp_b64_decode(rbuf, obuf, d);
mu_assert_int_equals_msg(msg, 2, d);
mu_assert_int_equals_msg(msg, 1, rbuf[0]);
mu_assert_int_equals_msg(msg, 1, rbuf[1]);
mu_assert_int_equals_msg(msg, -1, rbuf[2]);
/* 3 in, 4 out */
memset(obuf, 255, sizeof(obuf));
d = modp_b64_encode(obuf, ibuf, (size_t)3);
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
mu_assert_int_equals_msg(msg, 4, d);
mu_assert_int_equals_msg(msg, 0, obuf[4]);
memset(rbuf, 255, sizeof(rbuf));
d = modp_b64_decode(rbuf, obuf, d);
mu_assert_int_equals_msg(msg, 3, d);
mu_assert_int_equals_msg(msg, 1, rbuf[0]);
mu_assert_int_equals_msg(msg, 1, rbuf[1]);
mu_assert_int_equals_msg(msg, 1, rbuf[2]);
mu_assert_int_equals_msg(msg, -1, rbuf[3]);
/* 4 in, 8 out */
memset(obuf, 255, sizeof(obuf));
d = modp_b64_encode(obuf, ibuf, (size_t)4);
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
mu_assert_int_equals_msg(msg, 8, d);
mu_assert_int_equals_msg(msg, 0, obuf[8]);
memset(rbuf, 255, sizeof(rbuf));
d = modp_b64_decode(rbuf, obuf, d);
mu_assert_int_equals(4, d);
mu_assert_int_equals(1, rbuf[0]);
mu_assert_int_equals(1, rbuf[1]);
mu_assert_int_equals(1, rbuf[2]);
mu_assert_int_equals(1, rbuf[3]);
mu_assert_int_equals(-1, rbuf[4]);
/* 5 in, 8 out */
memset(obuf, 255, sizeof(obuf));
d = modp_b64_encode(obuf, ibuf, (size_t)5);
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
mu_assert_int_equals_msg(msg, 8, d);
mu_assert_int_equals_msg(msg, 0, obuf[8]);
memset(rbuf, 255, sizeof(rbuf));
d = modp_b64_decode(rbuf, obuf, d);
mu_assert_int_equals(5, d);
mu_assert_int_equals(1, rbuf[0]);
mu_assert_int_equals(1, rbuf[1]);
mu_assert_int_equals(1, rbuf[2]);
mu_assert_int_equals(1, rbuf[3]);
mu_assert_int_equals(1, rbuf[4]);
mu_assert_int_equals(-1, rbuf[5]);
/* 6 in, 8 out */
memset(obuf, 255, sizeof(obuf));
d = modp_b64_encode(obuf, ibuf, (size_t)6);
sprintf(msg, "b64='%s', d=%d", obuf, (int)d);
mu_assert_int_equals_msg(msg, 8, d);
mu_assert_int_equals_msg(msg, 0, obuf[8]);
memset(rbuf, 255, sizeof(rbuf));
d = modp_b64_decode(rbuf, obuf, d);
mu_assert_int_equals(6, d);
mu_assert_int_equals(1, rbuf[0]);
mu_assert_int_equals(1, rbuf[1]);
mu_assert_int_equals(1, rbuf[2]);
mu_assert_int_equals(1, rbuf[3]);
mu_assert_int_equals(1, rbuf[4]);
mu_assert_int_equals(1, rbuf[5]);
mu_assert_int_equals(-1, rbuf[6]);
return 0;
}
/**
* Test all 17M 3 bytes inputs to encoder, decode
* and make sure it's equal.
*/
static char* testEncodeDecode(void)
{
char ibuf[4];
char obuf[5];
char rbuf[4];
char msg[100];
msg[0] = 0; /* make msg an empty string */
unsigned int i, j, k;
size_t d = 0;
for (i = 0; i < 256; ++i) {
for (j = 0; j < 256; ++j) {
for (k = 0; k < 256; ++k) {
/* comment this out.. it really slows down the test */
/* sprintf(msg, "(i,j,k) = (%d,%d,%d):", i,j,k); */
ibuf[0] = (char)((unsigned char)i);
ibuf[1] = (char)((unsigned char)j);
ibuf[2] = (char)((unsigned char)k);
ibuf[3] = 0;
memset(obuf, 1, sizeof(obuf));
d = modp_b64_encode(obuf, ibuf, (size_t)3);
mu_assert_int_equals_msg(msg, 4, d);
mu_assert_int_equals_msg(msg, 0, obuf[4]);
memset(rbuf, 1, sizeof(rbuf));
d = modp_b64_decode(rbuf, obuf, (size_t)4);
mu_assert_int_equals_msg(msg, 3, d);
mu_assert_int_equals_msg(msg, ibuf[0], rbuf[0]);
mu_assert_int_equals_msg(msg, ibuf[1], rbuf[1]);
mu_assert_int_equals_msg(msg, ibuf[2], rbuf[2]);
mu_assert_int_equals_msg(msg, 1, rbuf[3]);
}
}
}
return 0;
}
static char* testDecodeErrors(void)
{
int i;
size_t y;
char out[1000];
char decode[5];
char msg[100];
/* negative length */
memset(decode, 1, sizeof(decode));
y = modp_b64_decode(out, decode, (size_t)-1);
mu_assert_int_equals(-1, y);
/* test bad input - all combinations */
char goodchar = 'A';
char badchar = '~';
for (i = 1; i < 16; ++i) {
decode[0] = (char)(((i & 0x01) == 0) ? goodchar : badchar);
decode[1] = (char)(((i & 0x02) == 0) ? goodchar : badchar);
decode[2] = (char)(((i & 0x04) == 0) ? goodchar : badchar);
decode[3] = (char)(((i & 0x08) == 0) ? goodchar : badchar);
decode[4] = '\0';
sprintf(msg, "i = %d, %s", i, decode);
y = modp_b64_decode(out, decode, (size_t)4);
mu_assert_int_equals_msg(msg, (size_t)-1, y);
}
/* test just 1-4 padchars */
for (i = 0; i < 4; ++i) {
decode[i] = '=';
decode[i + 1] = '\0';
y = modp_b64_decode(out, decode, (size_t)(i + 1));
sprintf(msg, "i=%d, b64=%s", i, decode);
mu_assert_int_equals_msg(msg, (size_t)-1, y);
}
/* Test good+3 pad chars (should be impossible) */
decode[0] = 'A';
decode[1] = '=';
decode[2] = '=';
decode[3] = '=';
y = modp_b64_decode(out, decode, (size_t)4);
mu_assert_int_equals(-1, y);
return 0;
}
static char* all_tests(void)
{
mu_run_test(testEndian);
mu_run_test(testEmpty);
mu_run_test(testPadding);
mu_run_test(testEncodeDecode);
mu_run_test(testDecodeErrors);
return 0;
}
UNITTESTS