From dcd10657b1385c3dfb7e7dc25bddf2437b87550e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Thu, 5 Mar 2020 22:21:48 +0100 Subject: tests: add tests on functions weecrypto_totp_generate and weecrypto_totp_validate --- tests/unit/core/test-core-crypto.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests/unit/core') 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 +#include #include #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"); -- cgit v1.2.3