forked from superwills/NibbleAndAHalf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestbase64.c
221 lines (192 loc) · 6.29 KB
/
testbase64.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
#include "testbase64.h"
#include "base64.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
int BASE64TESTSHOWDATA = 0;
int SHOWTIMING = 0;
const int BASE64TESTMAXDATALEN=1<<27; // Tests up to 128 MB
// Function for automated testing of base64.h. Also times.
int testBase64(const void* data, int dataLen) {
printf("Base64 test with ");
if(dataLen < 1<<10) printf("%d Bytes ", dataLen);
else if(dataLen < 1<<20) printf("%d KB ", dataLen >> 10);
else if(dataLen < 1<<30) printf("%d MB ", dataLen >> 20);
else printf("%d GB ", dataLen >> 30);
puts("of data");
printf("BASE64LEN(dataLen) = %d\n", BASE64LEN(dataLen));
unsigned char* binaryPtr = (unsigned char*)data;
char base64String[BASE64LEN(dataLen)];
CTimer t;
CTimerInit(&t);
int base64StringLen = base64(data, dataLen, base64String);
if(SHOWTIMING) printf("base64 %f seconds\n", CTimerGetTime(&t));
CTimerReset(&t);
if(!base64integrity(base64String, base64StringLen)) // Check the integrity of the base64'd string
{
puts("ERROR: Bad base64 characters detected");
return 0;
}
if(SHOWTIMING) printf("base64 integrity check %f seconds\n", CTimerGetTime(&t));
CTimerReset(&t);
unsigned char recoveredData[UNBASE64LEN(dataLen)];
int recoveredLen = unbase64(base64String, base64StringLen, recoveredData);
if(SHOWTIMING) printf("unbase64 %f seconds\n", CTimerGetTime(&t));
if(BASE64TESTSHOWDATA)
{
puts("Original data:");
for (int i = 0; i < dataLen; i++)
putc(binaryPtr[i], stdout);
puts("\n--------------------");
puts("Base64'd ascii text:");
puts(base64String);
puts("--------------------");
puts("Unbase64'd recovered data:");
for (int i = 0; i < recoveredLen; i++)
putc(recoveredData[i], stdout);
puts("\n--------------------");
}
printf("base64: %d bytes => %d bytes => %d bytes\n", dataLen, base64StringLen, recoveredLen);
puts("Checking..");
int allOk = 1;
if(dataLen != recoveredLen)
{
puts("ERROR: length(unbase64(base64(data))) != length(data)");
allOk = 0;
}
// Data is the exact same len. Good.
else for(int i = 0; i < dataLen; i++) // good ol' else for
{
if(BASE64TESTSHOWDATA)
printf("\n%4d | %3d | %3d |", i, binaryPtr[i], recoveredData[i]);
if(binaryPtr[i] != recoveredData[i])
{
printf(" X"); //\nERROR: byte @ %d != original data: (%d != %d)\n", i, data[i], recoveredData[i]);
allOk = 0;
}
}
if(allOk) puts("\n*** TEST SUCCESS DATA RECOVERED INTACT ***");
else puts("\n*** TEST FAILED ***");
return allOk;
}
int testBase64String(const char* str)
{
// Want to see the data output for these small data sizes
BASE64TESTSHOWDATA = 1;
puts("Test against a custom string.");
// Notice use of length of strlen(str)+1 to include NULL in the base64 encoding
return testBase64(str, (int)strlen(str)+1);
}
void testUnbase64WithBadAscii()
{
puts("Bad ascii test");
// BAD base64 data. These numbers represent non-base64 alphabet characters.
// if SAFEBASE64 is on, then unbase64() will catch it and send back a NULL
// pointer. Otherwise, you will just get invalid data back (but the program should not
// crash).
const char* badAscii = "-!#$asdf*()_";
int badAsciiLen = (int)strlen(badAscii);
puts(">> NOW TESTING UNBASE64 WITH INVALID BASE64 STRING:");
puts(badAscii);
for(int i = 0; i < badAsciiLen; i++)
printf("%3d, ", badAscii[i]);
puts("");
// check the integrity (it will show it's not valid base64)
if(!base64integrity(badAscii, badAsciiLen))
{
puts("<< EXPECTED >> There are some invalid ascii characters in your base64 string");
unsigned char baddat[UNBASE64LEN(badAsciiLen)];
int baddatLen = unbase64(badAscii, badAsciiLen, baddat);
printf("The unbase64'd data is %d bytes, anyway, it is:\n", baddatLen);
for(int i = 0; i < baddatLen; i++)
printf("%3d, ", baddat[i]);
puts("");
}
}
void testUnbase64WithBadLength()
{
puts("Bad length test (not multiple of 4)");
// As Christian Reitter pointed out, there are some input sequences
// that used to cause unbase64 to err-out.
const char* invalidBase64 = "==";
int badAsciiLen = (int)strlen(invalidBase64);
unsigned char binaryData[badAsciiLen];
int binaryLen = unbase64(invalidBase64, (int)strlen(invalidBase64), binaryData);
printf("Binary length=%d\n", binaryLen);
for(int i = 0; i < binaryLen; i++)
printf("%3d, ", binaryData[i]);
puts("");
}
void automatedTests()
{
// Don't show the output for large data
BASE64TESTSHOWDATA = 0;
puts("Automated tests");
int allOk=1;
//srand(220); // want same sequences
srand((unsigned int)time(0));
for(size_t testDatLen = 1; testDatLen <= BASE64TESTMAXDATALEN; testDatLen <<= 1) {
unsigned char dat[testDatLen];
for(size_t i = 0; i < testDatLen; i++) {
dat[i]=rand(); // make new random data
}
allOk &= testBase64(dat, testDatLen);
if(!allOk) { break; }
}
if(allOk) {
puts("-- ALL TESTS COMPLETED SUCCESSFULLY --");
} else {
puts("ERROR: At least one test failed. You should check it out.");
}
}
// -- other util --
// Prints the unb64 array in base64.h
void printUnbase64()
{
int i = 0;
puts("const static unsigned char unb64[] = {");
for(; i < '0'; i++)
{
if(i=='+') // + is 43
printf(" 62, ");
else if(i=='/') // / is 47
printf(" 63, ");
else
printf(" 0, ");
if(i && isMultipleOf(i+1, 10)) printf("//%d \n", (i+1));
}
for(; i <= '9'; i++)
{
printf("%3d, ", i-'0' + 52); // '0'=>52, like a deck of cards. Go Nana.
if(i && isMultipleOf(i+1, 10)) printf("//%d \n", (i+1));
}
for(; i < 'A'; i++)
{
printf(" 0, ");
if(i && isMultipleOf(i+1, 10)) printf("//%d \n", (i+1));
}
for(; i <= 'Z'; i++)
{
printf("%3d, ", i-'A');
if(i && isMultipleOf(i+1, 10)) printf("//%d \n", (i+1));
}
for(; i < 'a'; i++)
{
printf(" 0, ");
if(i && isMultipleOf(i+1,10)) printf("//%d \n", (i+1));
}
for(; i <= 'z'; i++)
{
printf("%3d, ", i-'a'+26); //'a' has the value of 26
if(i && isMultipleOf(i+1, 10)) printf("//%d \n", (i+1));
}
// Now put 0's until 255, in case string sent to be unbase64'd
// contains illegal characters
for(; i < 256; i++)
{
printf(" 0, ");
if(i && isMultipleOf(i+1, 10)) printf("//%d \n", (i+1));
}
printf("\n}; // This array has %d elements\n", i);
}