1 #include <libcper/base64.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 #include <string.h>
7
8 const char *good_encode_outputs[] = {
9 "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy",
10 };
11 const char *good_encode_inputs[] = {
12 "f", "fo", "foo", "foob", "fooba", "foobar",
13 };
14
test_base64_encode_good()15 void test_base64_encode_good()
16 {
17 int32_t encoded_len = 0;
18 for (long unsigned int i = 0;
19 i < sizeof(good_encode_inputs) / sizeof(good_encode_inputs[0]);
20 i++) {
21 const char *data = good_encode_inputs[i];
22 char *encoded = base64_encode((unsigned char *)data,
23 strlen(data), &encoded_len);
24 assert((size_t)encoded_len == strlen(good_encode_outputs[i]));
25 assert(memcmp(encoded, good_encode_outputs[i], encoded_len) ==
26 0);
27 free(encoded);
28 }
29 }
30
31 const char *good_decode_inputs[] = {
32 "Zg==", "Zg", "Zm8=", "Zm8", "Zm9v",
33 };
34 const char *good_decode_outputs[] = {
35 "f", "f", "fo", "fo", "foo",
36 };
37
test_base64_decode_good()38 void test_base64_decode_good()
39 {
40 for (long unsigned int i = 0;
41 i < sizeof(good_decode_inputs) / sizeof(good_decode_inputs[0]);
42 i++) {
43 int32_t decoded_len = 0;
44 const char *data = good_decode_inputs[i];
45 UINT8 *decoded =
46 base64_decode(data, strlen(data), &decoded_len);
47 assert(decoded != NULL);
48 assert((size_t)decoded_len == strlen(good_decode_outputs[i]));
49 assert(memcmp(decoded, good_decode_outputs[i], decoded_len) ==
50 0);
51 free(decoded);
52 }
53 }
54