|
1 | | -#include "hmac_cpp/hmac_utils.hpp" |
2 | | -#include <cassert> |
| 1 | +#include <gtest/gtest.h> |
3 | 2 | #include <limits> |
4 | | -#include <iostream> |
| 3 | +#include "hmac_cpp/hmac_utils.hpp" |
5 | 4 |
|
6 | | -int main() { |
| 5 | +TEST(TotpBoundaryTest, EarlyTimestampValidatesCounterZero) { |
7 | 6 | std::string key = "12345678901234567890"; |
8 | 7 | int digits = 6; |
9 | 8 | uint64_t period = 30; |
10 | 9 | uint64_t early_timestamp = 5; // less than one period |
| 10 | + int token = hmac::get_hotp_code(key.data(), key.size(), 0, digits, hmac::TypeHash::SHA1); |
| 11 | + EXPECT_TRUE(hmac::is_totp_token_valid(token, key.data(), key.size(), early_timestamp, period, digits, hmac::TypeHash::SHA1)); |
| 12 | +} |
11 | 13 |
|
12 | | - // Token generated for counter = 0 should be valid at early timestamp |
13 | | - int token_counter0 = hmac::get_hotp_code(key.data(), key.size(), 0, digits, hmac::TypeHash::SHA1); |
14 | | - bool valid = hmac::is_totp_token_valid(token_counter0, key.data(), key.size(), early_timestamp, period, digits, hmac::TypeHash::SHA1); |
15 | | - assert(valid); |
16 | | - |
17 | | - // Token from max counter should NOT be considered valid when timestamp is in the first period |
| 14 | +TEST(TotpBoundaryTest, MaxCounterInvalidAtEarlyTimestamp) { |
| 15 | + std::string key = "12345678901234567890"; |
| 16 | + int digits = 6; |
| 17 | + uint64_t period = 30; |
| 18 | + uint64_t early_timestamp = 5; |
18 | 19 | uint64_t max_counter = std::numeric_limits<uint64_t>::max(); |
19 | | - int token_max = hmac::get_hotp_code(key.data(), key.size(), max_counter, digits, hmac::TypeHash::SHA1); |
20 | | - bool valid_max = hmac::is_totp_token_valid(token_max, key.data(), key.size(), early_timestamp, period, digits, hmac::TypeHash::SHA1); |
21 | | - assert(!valid_max); |
| 20 | + int token = hmac::get_hotp_code(key.data(), key.size(), max_counter, digits, hmac::TypeHash::SHA1); |
| 21 | + EXPECT_FALSE(hmac::is_totp_token_valid(token, key.data(), key.size(), early_timestamp, period, digits, hmac::TypeHash::SHA1)); |
| 22 | +} |
22 | 23 |
|
23 | | - // At maximum timestamp, ensure overflow does not validate counter 0 token |
| 24 | +TEST(TotpBoundaryTest, MaxTimestampDoesNotValidateCounterZero) { |
| 25 | + std::string key = "12345678901234567890"; |
| 26 | + int digits = 6; |
24 | 27 | uint64_t max_timestamp = std::numeric_limits<uint64_t>::max(); |
25 | | - int token_zero = hmac::get_hotp_code(key.data(), key.size(), 0, digits, hmac::TypeHash::SHA1); |
26 | | - bool valid_zero_at_max = hmac::is_totp_token_valid(token_zero, key.data(), key.size(), max_timestamp, 1, digits, hmac::TypeHash::SHA1); |
27 | | - assert(!valid_zero_at_max); |
28 | | - |
29 | | - // Token for max counter should still be valid at that timestamp |
30 | | - int token_max_ts = hmac::get_hotp_code(key.data(), key.size(), max_counter, digits, hmac::TypeHash::SHA1); |
31 | | - bool valid_max_ts = hmac::is_totp_token_valid(token_max_ts, key.data(), key.size(), max_timestamp, 1, digits, hmac::TypeHash::SHA1); |
32 | | - assert(valid_max_ts); |
| 28 | + int token = hmac::get_hotp_code(key.data(), key.size(), 0, digits, hmac::TypeHash::SHA1); |
| 29 | + EXPECT_FALSE(hmac::is_totp_token_valid(token, key.data(), key.size(), max_timestamp, 1, digits, hmac::TypeHash::SHA1)); |
| 30 | +} |
33 | 31 |
|
34 | | - std::cout << "TOTP tests passed" << std::endl; |
35 | | - return 0; |
| 32 | +TEST(TotpBoundaryTest, MaxTimestampValidatesMaxCounter) { |
| 33 | + std::string key = "12345678901234567890"; |
| 34 | + int digits = 6; |
| 35 | + uint64_t max_timestamp = std::numeric_limits<uint64_t>::max(); |
| 36 | + uint64_t max_counter = std::numeric_limits<uint64_t>::max(); |
| 37 | + int token = hmac::get_hotp_code(key.data(), key.size(), max_counter, digits, hmac::TypeHash::SHA1); |
| 38 | + EXPECT_TRUE(hmac::is_totp_token_valid(token, key.data(), key.size(), max_timestamp, 1, digits, hmac::TypeHash::SHA1)); |
36 | 39 | } |
0 commit comments