correctly handle filter file loading error, small tweak
[mkp224o.git] / test_base32.c
blob2a815981bcb876547ec724f8d79501e265e97c87
1 #include <stddef.h>
2 #include <stdint.h>
3 #include "types.h"
4 #include "base32.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 const char *rev;
14 } tests0[] = {
15 {"", "", ""},
16 {"f", "my", "f"},
17 {"fo", "mzxq", "fo"},
18 {"foo", "mzxw6", "foo"},
19 {"foob", "mzxw6yq", "foob"},
20 {"fooba", "mzxw6ytb", "fooba"},
21 {"foobar", "mzxw6ytboi", "foobar"},
25 r:0, mask:FF
27 r:2, mask:C0
28 f -- f
29 r:3, mask:F0
30 fo -- fo
31 r:4, mask:80
32 foo -- foo
33 r:5, mask:E0
34 foob -- foob
35 r:5, mask:FF
36 fooba -- fooba
37 r:7, mask:C0
38 foobar -- foobar
42 struct masktestcase {
43 const char *src;
44 u8 mask;
45 } tests1[] = {
46 {"", 0x00},
47 {"a", 0x00},
48 {"ab", 0x00},
49 {"abc", 0x00},
50 {"abcd", 0x00},
51 {"abcde", 0x00},
52 {"abcdef", 0x00},
53 {"abcdefg", 0x00},
54 {"abcdefgh", 0x00},
55 {"abcdefghi", 0x00},
56 {"abcdefghij", 0x00},
60 int main(void)
62 char buf[1024], buf2[1024], mask;
63 size_t r;
64 for (size_t i = 0; i < sizeof(tests0)/sizeof(tests0[0]); ++i) {
65 base32_to(buf, (const u8 *)tests0[i].in, strlen(tests0[i].in));
66 assert(strcmp(buf, tests0[i].out) == 0);
67 r = base32_from((u8 *)buf2, (u8 *)&mask, buf);
68 buf2[r] = 0;
69 if (r > 0) {
70 assert((buf2[r-1] & ~mask) == 0);
72 //fprintf(stderr, "r:%d, mask:%02X\n", (int)r, ((unsigned int)mask) & 0xFF);
73 //assert(r == strlen(buf2));
74 //assert(r == strlen(tests0[i].rev));
75 //fprintf(stderr, "%s -- %s\n", buf2, tests0[i].rev);
76 assert(strcmp(buf2, tests0[i].rev) == 0);
79 //randombytes_buf(buf, 128);
80 //base32_to(buf2, (const u8 *)buf, 128);
81 //fprintf(stderr, ">%s\n", buf2);
83 return 0;