-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64_test.c
More file actions
61 lines (52 loc) · 2.85 KB
/
base64_test.c
File metadata and controls
61 lines (52 loc) · 2.85 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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "base64.h"
void testEncode(char *input, char *output);
void testDecode(char *input, uint8_t *output, unsigned long expectedSize);
int main() {
testEncode("M", "TQ==");
testEncode("Ma", "TWE=");
testEncode("Man", "TWFu");
testEncode("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.", "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=");
testDecode("TQ==", (uint8_t *)"M", 1);
testDecode("TQ", (uint8_t *)"M", 1); // missing padding
testDecode("TWE=", (uint8_t *)"Ma", 2);
testDecode("TWE", (uint8_t *)"Ma", 2); // missing padding
testDecode("TWFu", (uint8_t *)"Man", 3);
testDecode("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=", (uint8_t *)"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.", 269);
// invalid inputs
testDecode("T", NULL, 0);
testDecode("TWFu=", NULL, 0);
testDecode("=TWFu=", NULL, 0);
testDecode("T=WFu=", NULL, 0);
testDecode("TQ=", NULL, 0);
testDecode("TW|u=", NULL, 0);
testDecode("TQ==NN", NULL, 0);
testDecode("TQa*", NULL, 0);
return 0;
}
void testEncode(char *input, char *output) {
unsigned long eSize;
char *encodedValue = base64encode((uint8_t *)input, strlen(input), &eSize);
assert(strcmp(encodedValue, output) == 0);
}
void testDecode(char *input, uint8_t *output, unsigned long expectedSize) {
unsigned long dSize;
uint8_t *decodedValue = base64decode(input, strlen(input), &dSize);
if (output == NULL) {
assert(decodedValue == NULL);
return;
}
assert(dSize == expectedSize);
int equal = 1;
for (unsigned int i = 0; i < dSize; i++) {
if (decodedValue[i] != output[i]) {
equal = 0;
break;
}
}
assert(equal);
}