Skip to content

Commit 029a63a

Browse files
authored
test: add totp unit test
1 parent eae9f90 commit 029a63a

3 files changed

Lines changed: 33 additions & 25 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ if(BUILD_TESTS)
7070
target_link_libraries(test_all PRIVATE hmac_cpp gtest_main)
7171
target_include_directories(test_all PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
7272
add_test(NAME test_all COMMAND test_all)
73+
74+
add_executable(test_totp test_totp.cpp)
75+
target_link_libraries(test_totp PRIVATE hmac_cpp gtest_main)
76+
target_include_directories(test_totp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
77+
add_test(NAME test_totp COMMAND test_totp)
7378
endif()

scripts/run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
set -e
33
cmake -S . -B build -DBUILD_TESTS=ON -DCMAKE_CXX_STANDARD=${CXX_STANDARD:-11}
4-
cmake --build build
4+
cmake --build build --target test_all test_totp
55
cd build
66
ctest --output-on-failure

test_totp.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
1-
#include "hmac_cpp/hmac_utils.hpp"
2-
#include <cassert>
1+
#include <gtest/gtest.h>
32
#include <limits>
4-
#include <iostream>
3+
#include "hmac_cpp/hmac_utils.hpp"
54

6-
int main() {
5+
TEST(TotpBoundaryTest, EarlyTimestampValidatesCounterZero) {
76
std::string key = "12345678901234567890";
87
int digits = 6;
98
uint64_t period = 30;
109
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+
}
1113

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;
1819
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+
}
2223

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;
2427
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+
}
3331

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));
3639
}

0 commit comments

Comments
 (0)