Repository navigation
Expand file tree
/
Copy pathtest.c
More file actions
337 lines (260 loc) · 8.22 KB
/
Copy pathtest.c
File metadata and controls
337 lines (260 loc) · 8.22 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"
//Test values
///////////////////////////////////////////////////////////////
#define STRING_A "9TrJ"
#define STRING_B "9TrJUQ=="
#define STRING_C "9TrJUfA="
#define HEXNUM_A {0xF5,0x3A,0xC9}
#define HEXNUM_B {0xF5,0x3A,0xC9,0x51}
#define HEXNUM_C {0xF5,0x3A,0xC9,0x51,0xF0}
#define HEXSTR_A "0xF5 0x3A 0xC9"
#define HEXSTR_B "0xF5 0x3A 0xC9 0x51"
#define HEXSTR_C "0xF5 0x3A 0xC9 0x51 0xF0"
// #define TEXT_STR "test1234567"
// #define TEXT_B64 "dGVzdDEyMzQ1Njc="
#define TEXT_STR "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua..."
#define TEXT_B64 "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuLi4="
///////////////////////////////////////////////////////////////
#define NELEMS(x) (sizeof(x) / sizeof(x[0]))
//#define STATUS(x) ((x>0)?"[PASS]":"[FAIL]")
#define STATUS(x) score(x)
#define PERCENT(a,b) ((float)((float)a/(float)b)*100)
int test_b64_encode();
int test_b64_decode();
int test_b64_encodef();
int test_b64_decodef();
int test_b64_text_encode();
int test_b64_text_decode();
int test_b64e_size();
int test_b64d_size();
int hexputs(const char* data, int len);
int hexprint(const char* data, int len);
int compare(char *a, char *b, int l);
void deepCompare(int pass, char *a, char *b, int len);
char *score(int x);
int testScore = 0;
int testTotal = 0;
int main() {
puts("\nbase64.c [Test Data]");
puts("------------------------------------");
printf("%s : %s\n",HEXSTR_A,STRING_A);
printf("%s : %s\n",HEXSTR_B,STRING_B);
printf("%s : %s\n",HEXSTR_C,STRING_C);
printf("%s : %s\n",TEXT_STR,TEXT_B64);
puts("\nTesting b64_encode() ...\n");
test_b64_encode();
puts("\nTesting b64_decode() ...\n");
test_b64_decode();
puts("\nTesting test_b64_encodef() ...\n");
printf("%s\n",STATUS(test_b64_encodef()));
puts("\nTesting test_b64_decodef() ...\n");
printf("%s\n",STATUS(test_b64_decodef()));
puts("\nTesting test_b64_text_encode() ...\n");
test_b64_text_encode();
puts("\nTesting test_b64_text_decode() ...\n");
test_b64_text_decode();
puts("\nTesting b64e_size() ...\n");
test_b64e_size();
puts("\nTesting b64d_size() ...\n");
test_b64d_size();
puts("------------------------------------");
printf("\n[END] Test score: %g%% (%d/%d)\n",PERCENT(testScore,testTotal),testScore,testTotal);
return 0;
}
int test_b64_encode() {
char test_a[] = HEXNUM_A;
char test_b[] = HEXNUM_B;
char test_c[] = HEXNUM_C;
int size_a = NELEMS(test_a);
int size_b = NELEMS(test_b);
int size_c = NELEMS(test_c);
int out_size_a = b64e_size(size_a) + 1;
int out_size_b = b64e_size(size_b) + 1;
int out_size_c = b64e_size(size_c) + 1;
unsigned char *out_a = malloc( (sizeof(char) * out_size_a) );
unsigned char *out_b = malloc( (sizeof(char) * out_size_b) );
unsigned char *out_c = malloc( (sizeof(char) * out_size_c) );
out_size_a = b64_encode(test_a,size_a,out_a);
out_size_b = b64_encode(test_b,size_b,out_b);
out_size_c = b64_encode(test_c,size_c,out_c);
printf("\t%s\t%s\n",STATUS(strcmp(out_a,STRING_A)==0),out_a);
printf("\t%s\t%s\n",STATUS(strcmp(out_b,STRING_B)==0),out_b);
printf("\t%s\t%s\n",STATUS(strcmp(out_c,STRING_C)==0),out_c);
free(out_a);
free(out_b);
free(out_c);
return 0;
}
int test_b64_decode() {
char test_a[] = STRING_A;
char test_b[] = STRING_B;
char test_c[] = STRING_C;
int len_a = strlen(test_a);
int len_b = strlen(test_b);
int len_c = strlen(test_c);
int out_size_a = b64d_size(len_a);
int out_size_b = b64d_size(len_b);
int out_size_c = b64d_size(len_c);
unsigned char *out_a = malloc( (sizeof(char) * out_size_a) +1);
unsigned char *out_b = malloc( (sizeof(char) * out_size_b) +1);
unsigned char *out_c = malloc( (sizeof(char) * out_size_c) +1);
out_size_a = b64_decode(test_a,len_a,out_a);
out_size_b = b64_decode(test_b,len_b,out_b);
out_size_c = b64_decode(test_c,len_c,out_c);
char r_a[] = HEXNUM_A;
char r_b[] = HEXNUM_B;
char r_c[] = HEXNUM_C;
printf("\t%s\t",STATUS(compare(r_a,out_a,NELEMS(r_a)))); hexputs(out_a,out_size_a);
printf("\t%s\t",STATUS(compare(r_b,out_b,NELEMS(r_b)))); hexputs(out_b,out_size_b);
printf("\t%s\t",STATUS(compare(r_c,out_c,NELEMS(r_c)))); hexputs(out_c,out_size_c);
free(out_a);
free(out_b);
free(out_c);
return 0;
}
int test_b64_encodef() {
FILE *pFile;
pFile = fopen("B64_TEST01A.tmp","wb");
if (pFile==NULL)
return 0;
int i, j=0;
unsigned int test_a[] = HEXNUM_B;
unsigned int size_a = NELEMS(test_a);
for (i=0;i<size_a;i++) {
fputc(test_a[i],pFile);
}
fclose(pFile);
j = b64_encodef("B64_TEST01A.tmp","B64_TEST01B.tmp");
remove("B64_TEST01A.tmp");
if (!j)
return 0;
pFile = fopen("B64_TEST01B.tmp","rb");
if (pFile==NULL)
return 0;
char *out = malloc(j+1);
fgets(out,j+1,pFile);
fclose(pFile);
remove("B64_TEST01B.tmp");
printf("\tComparing \"%s\" to \"%s\" : ",STRING_B,out);
int retVal = 0;
if (strcmp(STRING_B,out)==0)
retVal = 1;
free(out);
return retVal;
}
int test_b64_decodef() {
FILE *pFile;
pFile = fopen("B64_TEST02A.tmp","wb");
if (pFile==NULL)
return 0;
int j=0;
fputs(STRING_B,pFile);
fclose(pFile);
j = b64_decodef("B64_TEST02A.tmp","B64_TEST02B.tmp");
remove("B64_TEST02A.tmp");
if (!j)
return 0;
pFile = fopen("B64_TEST02B.tmp","rb");
if (pFile==NULL)
return 0;
char c, l=0, out[j+1];
while(c!=EOF) {
c=fgetc(pFile);
if (c==EOF)
break;
out[l++] = c;
}
fclose(pFile);
remove("B64_TEST02B.tmp");
printf("\tComparing \"%s\" to \"",HEXSTR_B); hexprint(out,j); printf("\" : ");
char r_b[] = HEXNUM_B;
if (compare(r_b,out,j))
return 1;
return 0;
}
int test_b64_text_encode() {
char *test_str = TEXT_STR;
int length = strlen(test_str);
unsigned char *out_a = (char *)malloc(1 + (sizeof(char) * b64e_size(length)));
int out_size_a = b64_encode(test_str,length,out_a);
int test_passed = (strcmp(out_a,TEXT_B64)==0);
printf("\t%s\t%s\n",STATUS(test_passed),out_a);
deepCompare(test_passed,out_a,TEXT_B64,out_size_a);
free(out_a);
return 0;
}
int test_b64_text_decode() {
char *test_str = TEXT_B64;
int length = strlen(test_str) + 1;
unsigned char *out_a = malloc( b64d_size(length)+1 );
int out_size_a = b64_decode(test_str,length,out_a);
out_a[out_size_a] = '\0';
int test_passed = (strcmp(out_a,TEXT_STR)==0);
printf("\t%s\t%s\n",STATUS(test_passed),out_a);
deepCompare(test_passed,out_a,TEXT_STR,out_size_a);
free(out_a);
return 0;
}
int test_b64e_size() {
// get ideal size with null byte
unsigned int idealsize = b64e_size(sizeof(TEXT_STR)) + 1;
// allocate memory with space for null byte
unsigned char *result = malloc( (sizeof(char) * idealsize) );
// encode it and get the actual size
unsigned int actualsize = b64_encode((unsigned char *)TEXT_STR, sizeof(TEXT_STR), &result[0]);
printf("\t%s\tideal size = %d\tactual size = %d\n", STATUS(idealsize == actualsize), idealsize, actualsize);
return 0;
}
int test_b64d_size() {
// get ideal size for decoded
unsigned int idealsize = b64d_size(sizeof(TEXT_B64));
// allocate memory of ideal size
unsigned char *result = malloc( (sizeof(char) * idealsize) );
// decode it and get the actual size
unsigned int actualsize = b64_decode((unsigned char *)TEXT_B64, sizeof(TEXT_B64), &result[0]);
// Should be equal or larger, with the difference no more than a few bytes?
int tolerance = 2;
int diff = idealsize - actualsize;
printf("\t%s\tideal size = %d\tactual size = %d\n", STATUS(diff <= tolerance && diff >= 0), idealsize, actualsize);
return 0;
}
int hexputs(const char* data, int len) {
hexprint(data,len);
printf("\n");
return 0;
}
int hexprint(const char* data, int len) {
int i;
for (i=0;i<len;i++) {
printf("0x%X",data[i]&255);
if (i<len-1)
printf(" ");
}
return 0;
}
int compare(char *a, char *b, int l) {
int i;
for (i=0;i<l;i++) {
if (a[i]!=b[i])
return 0;
}
return 1;
}
void deepCompare(int pass, char *a, char *b, int len){
if (!pass) {
for(int j=0;j<len;j++){
printf("\t\'%c\' == \'%c\'",a[j],b[j]);
if (a[j] != b[j])
printf(" <- Error.");
printf("\n");
}
}
}
char *score(int x) {
testScore+=(!!x)*1;
testTotal+=1;
return ((x>0)?"[PASS]":"[FAIL]");
}