diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2020-03-05 22:21:48 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2020-03-05 22:21:48 +0100 |
commit | dcd10657b1385c3dfb7e7dc25bddf2437b87550e (patch) | |
tree | 4664275abf707af83729d4a08f68b43ca4ded122 /tests/unit/core | |
parent | dabf32f213b9fc2b3b1df8aa9a2120b2cb7acf6c (diff) | |
download | weechat-dcd10657b1385c3dfb7e7dc25bddf2437b87550e.zip |
tests: add tests on functions weecrypto_totp_generate and weecrypto_totp_validate
Diffstat (limited to 'tests/unit/core')
-rw-r--r-- | tests/unit/core/test-core-crypto.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/unit/core/test-core-crypto.cpp b/tests/unit/core/test-core-crypto.cpp index 10e365681..43a8bc160 100644 --- a/tests/unit/core/test-core-crypto.cpp +++ b/tests/unit/core/test-core-crypto.cpp @@ -24,6 +24,7 @@ extern "C" { #include <string.h> +#include <ctype.h> #include <gcrypt.h> #include "src/core/wee-crypto.h" #include "src/core/wee-string.h" @@ -275,6 +276,14 @@ TEST(CoreCrypto, TotpGenerate) WEE_CHECK_TOTP_GENERATE(NULL, TOTP_SECRET, 0, 3); WEE_CHECK_TOTP_GENERATE(NULL, TOTP_SECRET, 0, 11); + /* current time */ + totp = weecrypto_totp_generate (TOTP_SECRET, 0, 6); + CHECK(totp); + CHECK(isdigit (totp[0]) && isdigit (totp[1]) && isdigit (totp[2]) + && isdigit (totp[3]) && isdigit (totp[4]) && isdigit (totp[5])); + LONGS_EQUAL(6, strlen (totp)); + free (totp); + /* TOTP with 6 digits */ WEE_CHECK_TOTP_GENERATE("065486", TOTP_SECRET, 1540624066, 6); WEE_CHECK_TOTP_GENERATE("640073", TOTP_SECRET, 1540624085, 6); @@ -310,6 +319,18 @@ TEST(CoreCrypto, TotpValidate) WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 0, 0, NULL); WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 0, 0, ""); + /* not enough digits in OTP (min is 4) */ + WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 1234567890, 0, "1"); + WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 1234567890, 0, "12"); + WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 1234567890, 0, "123"); + + /* too many digits (max is 10) */ + WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 1234567890, 0, "12345678901"); + WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 1234567890, 0, "123456789012"); + + /* current time */ + weecrypto_totp_validate (TOTP_SECRET, 0, 0, "123456"); + /* validation error (wrong OTP) */ WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 1540624110, 0, "065486"); WEE_CHECK_TOTP_VALIDATE(0, TOTP_SECRET, 1540624110, 1, "065486"); |