| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "base64.h" |
| 7 | |
| 8 | static void |
| 9 | test(unsigned char *encode, unsigned int encodelen, |
| 10 | char *decode, unsigned int decodelen) |
| 11 | { |
| 12 | char *encode_out;; |
| 13 | unsigned char *decode_out; |
| 14 | |
| 15 | encode_out = malloc(BASE64_ENCODE_OUT_SIZE(encodelen)); |
| 16 | decode_out = malloc(BASE64_DECODE_OUT_SIZE(decodelen)); |
| 17 | assert(encode_out); |
| 18 | assert(decode_out); |
| 19 | |
| 20 | assert(base64_encode(encode, encodelen, encode_out) == decodelen); |
| 21 | assert(memcmp(encode_out, decode, decodelen) == 0); |
| 22 | assert(base64_decode(decode, decodelen, decode_out) == encodelen); |
| 23 | assert(memcmp(decode_out, encode, encodelen) == 0); |
| 24 | |
| 25 | free(encode_out); |
| 26 | free(decode_out); |
| 27 | } |
| 28 | |
| 29 | int |
| 30 | main(void) |
| 31 | { |
| 32 | test((void *)"", 0, "", 0); |
| 33 | test((void *)"f", 1, "Zg==", 4); |
| 34 | test((void *)"fo", 2, "Zm8=", 4); |
| 35 | test((void *)"foo", 3, "Zm9v", 4); |
| 36 | test((void *)"foob", 4, "Zm9vYg==", 8); |
| 37 | test((void *)"fooba", 5, "Zm9vYmE=", 8); |
| 38 | test((void *)"foobar", 6, "Zm9vYmFy", 8); |
| 39 | |
| 40 | return 0; |
| 41 | } |