Debian Codegraph @

contrib/mod_auth_otp/t/api/base32.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_auth_otp testsuite
3 * Copyright (c) 2015-2016 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Base32 API tests
26 */
27
28 #include "tests.h"
29 #include "base32.h"
30
31 static pool *p = NULL;
32
33 /* These values are taken from RFC 4458, Section 10.
34 *
35 * Note that this base32 implementation does NOT emit the padding characters,
36 * as an "optimization".
37 *
38 * The base32 encoded values are used for interoperability with e.g. Google
39 * Authenticator, for entering into the app via human interaction. To
40 * reduce the friction, then, the padding characters are omitted.
41 */
42
43 struct kat {
44 const char *raw;
45 const char *encoded;
46 };
47
48 static struct kat expected_codes[] = {
49 { "", "" },
50 { "f", "MY", },
51 { "fo", "MZXQ" },
52 { "foo", "MZXW6" },
53 { "foob", "MZXW6YQ" },
54 { "foobar", "MZXW6YTBOI" }
55 };
56 static unsigned int expected_code_count = 6;
57
58 static void set_up(void) {
59 if (p == NULL) {
60 p = permanent_pool = make_sub_pool(NULL);
61 }
62 }
63
64 static void tear_down(void) {
65 if (p) {
66 destroy_pool(p);
67 p = NULL;
68 permanent_pool = NULL;
69 }
70 }
71
72 START_TEST (base32_encode_test) {
73 register unsigned int i;
74 int res;
75
76 res = auth_otp_base32_encode(p, NULL, 0, NULL, NULL);
77 ck_assert_msg(res < 0, "Failed to handle null arguments");
78 ck_assert_msg(errno == EINVAL, "Expected errno %s (%d), got %s (%d)",
79 strerror(EINVAL), EINVAL, strerror(errno), errno);
80
81 for (i = 0; i < expected_code_count; i++) {
82 const unsigned char *raw, *encoded = NULL;
83 size_t raw_len, encoded_len = 0;
84
85 raw = (const unsigned char *) expected_codes[i].raw;
86 raw_len = strlen((char *) raw);
87
88 res = auth_otp_base32_encode(p, raw, raw_len, &encoded, &encoded_len);
89 ck_assert_msg(res == 0, "Failed to base32 encode '%s': %s",
90 expected_codes[i].raw, strerror(errno));
91 ck_assert_msg(strcmp((char *) encoded, expected_codes[i].encoded) == 0,
92 "Expected '%s' for value '%s', got '%s'", expected_codes[i].encoded,
93 expected_codes[i].raw, encoded);
94 }
95 }
96 END_TEST
97
98 START_TEST (base32_decode_test) {
99 register unsigned int i;
100 int res;
101
102 res = auth_otp_base32_decode(p, NULL, 0, NULL, NULL);
103 ck_assert_msg(res < 0, "Failed to handle null arguments");
104 ck_assert_msg(errno == EINVAL, "Expected errno %s (%d), got %s (%d)",
105 strerror(EINVAL), EINVAL, strerror(errno), errno);
106
107 mark_point();
108
109 for (i = 0; i < expected_code_count; i++) {
110 const unsigned char *encoded, *raw = NULL;
111 size_t encoded_len, raw_len = 0;
112
113 encoded = (const unsigned char *) expected_codes[i].encoded;
114 encoded_len = strlen((char *) encoded);
115
116 res = auth_otp_base32_decode(p, encoded, encoded_len, &raw, &raw_len);
117 ck_assert_msg(res == 0, "Failed to base32 decode '%s': %s",
118 expected_codes[i].encoded, strerror(errno));
119 ck_assert_msg(strcmp((char *) raw, expected_codes[i].raw) == 0,
120 "Expected '%s' for value '%s', got '%s'", expected_codes[i].raw,
121 expected_codes[i].encoded, raw);
122 }
123 }
124 END_TEST
125
126 Suite *tests_get_base32_suite(void) {
127 Suite *suite;
128 TCase *testcase;
129
130 suite = suite_create("base32");
131
132 testcase = tcase_create("base32");
133 tcase_add_checked_fixture(testcase, set_up, tear_down);
134 tcase_add_test(testcase, base32_encode_test);
135 tcase_add_test(testcase, base32_decode_test);
136 suite_add_tcase(suite, testcase);
137
138 return suite;
139 }