New upstream release:
[debian/iodine.git] / tests / base32.c
1 /*
2  * Copyright (c) 2006-2007 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <check.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22
23 #include "encoding.h"
24 #include "base32.h"
25 #include "test.h"
26
27 static struct tuple
28 {
29         char *a;
30         char *b;
31 } testpairs[] = {
32         { "iodinetestingtesting", "nfxwi0lomv0gk21unfxgo3dfon0gs1th" },
33         { "abc123", "mfrggmjsgm" },
34         { NULL, NULL }  
35 };
36
37 START_TEST(test_base32_encode)
38 {
39         size_t len;
40         char buf[4096];
41         int val;
42         int i;
43
44         for (i = 0; testpairs[i].a != NULL; i++) {
45                 len = sizeof(buf);
46                 val = base32_encode(buf, &len, testpairs[i].a, strlen(testpairs[i].a));
47
48                 fail_unless(val > 0, strerror(errno));
49                 fail_unless(strcmp(buf, testpairs[i].b) == 0, 
50                                 va_str("'%s' != '%s'", buf, testpairs[i].b));
51         }
52 }
53 END_TEST
54
55 START_TEST(test_base32_decode)
56 {
57         size_t len;
58         char buf[4096];
59         int val;
60         int i;
61
62         for (i = 0; testpairs[i].a != NULL; i++) {
63                 len = sizeof(buf);
64                 val = base32_decode(buf, &len, testpairs[i].b, strlen(testpairs[i].b));
65
66                 fail_unless(val > 0, strerror(errno));
67                 fail_unless(buf != NULL, "buf == NULL");
68                 fail_unless(strcmp(buf, testpairs[i].a) == 0, 
69                                 va_str("'%s' != '%s'", buf, testpairs[i].a));
70         }
71 }
72 END_TEST
73
74 TCase *
75 test_base32_create_tests()
76 {
77         TCase *tc;
78
79         tc = tcase_create("Base32");
80         tcase_add_test(tc, test_base32_encode);
81         tcase_add_test(tc, test_base32_decode);
82
83         return tc;
84 }