| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "tests.h" |
| 29 |
|
| 30 |
static pool *p = NULL; |
| 31 |
|
| 32 |
static void set_up(void) { |
| 33 |
if (p == NULL) { |
| 34 |
p = permanent_pool = make_sub_pool(NULL); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
static void tear_down(void) { |
| 39 |
if (p) { |
| 40 |
destroy_pool(p); |
| 41 |
p = NULL; |
| 42 |
permanent_pool = NULL; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
START_TEST (hotp_test) { |
| 47 |
register unsigned int i; |
| 48 |
int res; |
| 49 |
|
| 50 |
|
| 51 |
const char *key = "12345678901234567890"; |
| 52 |
size_t key_len = strlen((char *) key); |
| 53 |
struct kat { |
| 54 |
unsigned long count; |
| 55 |
unsigned int hotp; |
| 56 |
}; |
| 57 |
|
| 58 |
struct kat expected_codes[] = { |
| 59 |
{ 0, 755224 }, |
| 60 |
{ 1, 287082 }, |
| 61 |
{ 2, 359152 }, |
| 62 |
{ 3, 969429 }, |
| 63 |
{ 4, 338314 }, |
| 64 |
{ 5, 254676 }, |
| 65 |
{ 6, 287922 }, |
| 66 |
{ 7, 162583 }, |
| 67 |
{ 8, 399871 }, |
| 68 |
{ 9, 520489 } |
| 69 |
}; |
| 70 |
|
| 71 |
res = auth_otp_hotp(p, (const unsigned char *) key, key_len, 0, NULL); |
| 72 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 73 |
ck_assert_msg(errno == EINVAL, "Expected errno %s (%d), got %s (%d)", |
| 74 |
strerror(EINVAL), EINVAL, strerror(errno), errno); |
| 75 |
|
| 76 |
for (i = 0; i < 10; i++) { |
| 77 |
unsigned int code; |
| 78 |
|
| 79 |
res = auth_otp_hotp(p, (const unsigned char *) key, key_len, |
| 80 |
expected_codes[i].count, &code); |
| 81 |
ck_assert_msg(res == 0, "Failed to generate HOTP for value %lu: %s", |
| 82 |
expected_codes[i].count, strerror(errno)); |
| 83 |
ck_assert_msg(code == expected_codes[i].hotp, |
| 84 |
"Expected HOTP %u for value %lu, got %u", expected_codes[i].hotp, |
| 85 |
expected_codes[i].count, code); |
| 86 |
} |
| 87 |
} |
| 88 |
END_TEST |
| 89 |
|
| 90 |
START_TEST (totp_sha1_test) { |
| 91 |
register unsigned int i; |
| 92 |
int res; |
| 93 |
|
| 94 |
|
| 95 |
const char *key = "12345678901234567890"; |
| 96 |
size_t key_len = strlen(key); |
| 97 |
struct kat { |
| 98 |
unsigned long count; |
| 99 |
unsigned int totp; |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
struct kat expected_codes[] = { |
| 107 |
{ 59, 287082 }, |
| 108 |
{ 1111111109, 81804 }, |
| 109 |
{ 1111111111, 50471 }, |
| 110 |
{ 1234567890, 5924 }, |
| 111 |
{ 2000000000, 279037 }, |
| 112 |
{ 20000000000, 353130 } |
| 113 |
}; |
| 114 |
|
| 115 |
res = auth_otp_totp(p, (const unsigned char *) key, key_len, 0, 0, NULL); |
| 116 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 117 |
ck_assert_msg(errno == EINVAL, "Expected errno %s (%d), got %s (%d)", |
| 118 |
strerror(EINVAL), EINVAL, strerror(errno), errno); |
| 119 |
|
| 120 |
for (i = 0; i < 5; i++) { |
| 121 |
unsigned int code; |
| 122 |
|
| 123 |
res = auth_otp_totp(p, (const unsigned char *) key, key_len, |
| 124 |
expected_codes[i].count, AUTH_OTP_ALGO_TOTP_SHA1, &code); |
| 125 |
ck_assert_msg(res == 0, "Failed to generate TOTP-SHA1 for value %lu: %s", |
| 126 |
expected_codes[i].count, strerror(errno)); |
| 127 |
ck_assert_msg(code == expected_codes[i].totp, |
| 128 |
"Expected TOTP-SHA1 %u for value %lu, got %u", expected_codes[i].totp, |
| 129 |
expected_codes[i].count, code); |
| 130 |
} |
| 131 |
} |
| 132 |
END_TEST |
| 133 |
|
| 134 |
#ifdef HAVE_SHA256_OPENSSL |
| 135 |
START_TEST (totp_sha256_test) { |
| 136 |
register unsigned int i; |
| 137 |
int res; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
const char *key = "12345678901234567890123456789012"; |
| 143 |
size_t key_len = strlen(key); |
| 144 |
struct kat { |
| 145 |
unsigned long count; |
| 146 |
unsigned int totp; |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
struct kat expected_codes[] = { |
| 154 |
{ 59, 119246 }, |
| 155 |
{ 1111111109, 84774 }, |
| 156 |
{ 1111111111, 62674 }, |
| 157 |
{ 1234567890, 819424 }, |
| 158 |
{ 2000000000, 698825 }, |
| 159 |
{ 20000000000, 737706 } |
| 160 |
}; |
| 161 |
|
| 162 |
res = auth_otp_totp(p, (const unsigned char *) key, key_len, 0, 0, NULL); |
| 163 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 164 |
ck_assert_msg(errno == EINVAL, "Expected errno %s (%d), got %s (%d)", |
| 165 |
strerror(EINVAL), EINVAL, strerror(errno), errno); |
| 166 |
|
| 167 |
for (i = 0; i < 5; i++) { |
| 168 |
unsigned int code; |
| 169 |
|
| 170 |
res = auth_otp_totp(p, (const unsigned char *) key, key_len, |
| 171 |
expected_codes[i].count, AUTH_OTP_ALGO_TOTP_SHA256, &code); |
| 172 |
ck_assert_msg(res == 0, "Failed to generate TOTP-SHA256 for value %lu: %s", |
| 173 |
expected_codes[i].count, strerror(errno)); |
| 174 |
ck_assert_msg(code == expected_codes[i].totp, |
| 175 |
"Expected TOTP-SHA256 %u for value %lu, got %u", expected_codes[i].totp, |
| 176 |
expected_codes[i].count, code); |
| 177 |
} |
| 178 |
} |
| 179 |
END_TEST |
| 180 |
#endif |
| 181 |
|
| 182 |
#ifdef HAVE_SHA512_OPENSSL |
| 183 |
START_TEST (totp_sha512_test) { |
| 184 |
register unsigned int i; |
| 185 |
int res; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
const char *key = "1234567890123456789012345678901234567890123456789012345678901234"; |
| 191 |
size_t key_len = strlen(key); |
| 192 |
struct kat { |
| 193 |
unsigned long count; |
| 194 |
unsigned int totp; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
struct kat expected_codes[] = { |
| 202 |
{ 59, 693936 }, |
| 203 |
{ 1111111109, 91201 }, |
| 204 |
{ 1111111111, 943326 }, |
| 205 |
{ 1234567890, 441116 }, |
| 206 |
{ 2000000000, 618901 }, |
| 207 |
{ 20000000000, 863826 } |
| 208 |
}; |
| 209 |
|
| 210 |
res = auth_otp_totp(p, (const unsigned char *) key, key_len, 0, 0, NULL); |
| 211 |
ck_assert_msg(res < 0, "Failed to handle null arguments"); |
| 212 |
ck_assert_msg(errno == EINVAL, "Expected errno %s (%d), got %s (%d)", |
| 213 |
strerror(EINVAL), EINVAL, strerror(errno), errno); |
| 214 |
|
| 215 |
for (i = 0; i < 5; i++) { |
| 216 |
unsigned int code; |
| 217 |
|
| 218 |
res = auth_otp_totp(p, (const unsigned char *) key, key_len, |
| 219 |
expected_codes[i].count, AUTH_OTP_ALGO_TOTP_SHA512, &code); |
| 220 |
ck_assert_msg(res == 0, "Failed to generate TOTP-SHA512 for value %lu: %s", |
| 221 |
expected_codes[i].count, strerror(errno)); |
| 222 |
ck_assert_msg(code == expected_codes[i].totp, |
| 223 |
"Expected TOTP-SHA512 %u for value %lu, got %u", expected_codes[i].totp, |
| 224 |
expected_codes[i].count, code); |
| 225 |
} |
| 226 |
} |
| 227 |
END_TEST |
| 228 |
#endif |
| 229 |
|
| 230 |
Suite *tests_get_hotp_suite(void) { |
| 231 |
Suite *suite; |
| 232 |
TCase *testcase; |
| 233 |
|
| 234 |
suite = suite_create("hotp"); |
| 235 |
|
| 236 |
testcase = tcase_create("hotp"); |
| 237 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 238 |
tcase_add_test(testcase, hotp_test); |
| 239 |
suite_add_tcase(suite, testcase); |
| 240 |
|
| 241 |
return suite; |
| 242 |
} |
| 243 |
|
| 244 |
Suite *tests_get_totp_suite(void) { |
| 245 |
Suite *suite; |
| 246 |
TCase *testcase; |
| 247 |
|
| 248 |
suite = suite_create("totp"); |
| 249 |
|
| 250 |
testcase = tcase_create("totp"); |
| 251 |
tcase_add_checked_fixture(testcase, set_up, tear_down); |
| 252 |
tcase_add_test(testcase, totp_sha1_test); |
| 253 |
#ifdef HAVE_SHA256_OPENSSL |
| 254 |
tcase_add_test(testcase, totp_sha256_test); |
| 255 |
#endif |
| 256 |
#ifdef HAVE_SHA512_OPENSSL |
| 257 |
tcase_add_test(testcase, totp_sha512_test); |
| 258 |
#endif |
| 259 |
suite_add_tcase(suite, testcase); |
| 260 |
|
| 261 |
return suite; |
| 262 |
} |