correctly handle filter file loading error, small tweak
[mkp224o.git] / test_base64.c
blob95e43206a503b716b5f04aa6bc81a117b6071d8b
1 #include <stddef.h>
2 #include <stdint.h>
3 #include "types.h"
4 #include "base64.h"
5 #include <string.h>
6 #include <assert.h>
7 #include <stdio.h>
8 #include <sodium/randombytes.h>
10 struct texttestcase {
11 const char *in;
12 const char *out;
13 } tests0[] = {
14 { "" ,"" },
15 { "f" ,"Zg==" },
16 { "fo" ,"Zm8=" },
17 { "foo" ,"Zm9v" },
18 { "foob" ,"Zm9vYg==" },
19 { "fooba" ,"Zm9vYmE=" },
20 { "foobar","Zm9vYmFy" },
22 { "foobarf" ,"Zm9vYmFyZg==" },
23 { "foobarfo" ,"Zm9vYmFyZm8=" },
24 { "foobarfoo" ,"Zm9vYmFyZm9v" },
25 { "foobarfoob" ,"Zm9vYmFyZm9vYg==" },
26 { "foobarfooba" ,"Zm9vYmFyZm9vYmE=" },
27 { "foobarfoobar","Zm9vYmFyZm9vYmFy" },
30 int main(void)
32 char buf[1024], buf2[1024];
33 size_t r;
34 for (size_t i = 0; i < sizeof(tests0)/sizeof(tests0[0]); ++i) {
35 base64_to(buf, (const u8 *)tests0[i].in, strlen(tests0[i].in));
36 if (strcmp(buf, tests0[i].out) != 0) {
37 printf("invalid encoding result: \"%s\" -> encoded as \"%s\", but expected \"%s\".\n",
38 tests0[i].in, buf, tests0[i].out);
39 return 1;
41 if (strlen(buf) != BASE64_TO_LEN(strlen(tests0[i].in))) {
42 printf("encoded length mismatch: got %d expected %d\n",
43 (int) strlen(buf), (int) BASE64_TO_LEN(strlen(tests0[i].in)));
44 return 1;
46 if (!base64_valid(buf,0)) {
47 printf("encoded data is considered invalid\n");
48 return 1;
50 r = base64_from((u8 *)buf2, buf, strlen(buf));
51 buf2[r] = '\000';
52 if (strcmp(buf2, tests0[i].in) != 0) {
53 printf("invalid decoding result: encoded \"%s\", decoded as \"%s\", but expected \"%s\".\n",
54 tests0[i].out, buf2, tests0[i].in);
55 return 1;
58 return 0;