From 64e7708c5bc6554376e5769a20f8c2e13c460043 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sat, 6 Sep 2025 02:42:50 +0300 Subject: [PATCH] ci: run unit tests explicitly --- .github/workflows/CI-Linux.yml | 4 +-- .github/workflows/CI-Win.yml | 2 +- .github/workflows/CI-macOS.yml | 4 +-- test_all.cpp | 56 +++++++++++++++++++++++++++++----- test_totp.cpp | 43 ++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 13 deletions(-) diff --git a/.github/workflows/CI-Linux.yml b/.github/workflows/CI-Linux.yml index 728e5e7..a2d7c79 100644 --- a/.github/workflows/CI-Linux.yml +++ b/.github/workflows/CI-Linux.yml @@ -27,7 +27,7 @@ jobs: - name: Build run: cmake --build build - name: Run tests - run: cd build && ctest --output-on-failure + run: cd build && ctest --output-on-failure -R 'test_all|test_totp' vcpkg: runs-on: ubuntu-latest steps: @@ -42,4 +42,4 @@ jobs: - name: Build run: cmake --build build - name: Run tests - run: cd build && ctest --output-on-failure + run: cd build && ctest --output-on-failure -R 'test_all|test_totp' diff --git a/.github/workflows/CI-Win.yml b/.github/workflows/CI-Win.yml index 8e16889..528f119 100644 --- a/.github/workflows/CI-Win.yml +++ b/.github/workflows/CI-Win.yml @@ -36,4 +36,4 @@ jobs: run: cmake --build build - name: Run tests shell: msys2 {0} - run: ctest --test-dir build --output-on-failure + run: ctest --test-dir build --output-on-failure -R "test_all|test_totp" diff --git a/.github/workflows/CI-macOS.yml b/.github/workflows/CI-macOS.yml index ae1f198..6bcbed7 100644 --- a/.github/workflows/CI-macOS.yml +++ b/.github/workflows/CI-macOS.yml @@ -30,7 +30,7 @@ jobs: - name: Build run: cmake --build build - name: Run tests - run: cd build && ctest --output-on-failure + run: cd build && ctest --output-on-failure -R 'test_all|test_totp' vcpkg: runs-on: macos-latest steps: @@ -45,4 +45,4 @@ jobs: - name: Build run: cmake --build build - name: Run tests - run: cd build && ctest --output-on-failure + run: cd build && ctest --output-on-failure -R 'test_all|test_totp' diff --git a/test_all.cpp b/test_all.cpp index 65543d6..ec298bd 100644 --- a/test_all.cpp +++ b/test_all.cpp @@ -153,6 +153,42 @@ TEST(HMACTest, InvalidTypeThrowsString) { EXPECT_THROW(hmac::get_hmac(key, msg, invalid), std::invalid_argument); } +TEST(HMACTest, RFC2202SHA1Vectors) { + struct Vector { + std::string key; + std::string data; + std::string sha1; + }; + std::vector vectors; + vectors.push_back({std::string(20, '\x0b'), "Hi There", + "b617318655057264e28bc0b6fb378c8ef146be00"}); + vectors.push_back({"Jefe", "what do ya want for nothing?", + "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"}); + vectors.push_back({std::string(20, '\xaa'), std::string(50, '\xdd'), + "125d7342b9ac11cd91a39af48aa17b4f63f175d3"}); + { + std::string key; + for (int i = 1; i <= 25; ++i) key.push_back(static_cast(i)); + vectors.push_back({key, std::string(50, '\xcd'), + "4c9007f4026250c6bc8414f9bf50c86c2d7235da"}); + } + vectors.push_back({std::string(20, '\x0c'), "Test With Truncation", + "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04"}); + vectors.push_back({std::string(80, '\xaa'), + "Test Using Larger Than Block-Size Key - Hash Key First", + "aa4ae5e15272d00e95705637ce8a3b55ed402112"}); + vectors.push_back({std::string(80, '\xaa'), + "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", + "e8e99d0f45237d786d6bbaa7965c7808bbff1a91"}); + + for (size_t i = 0; i < vectors.size(); ++i) { + EXPECT_EQ(hmac::get_hmac(vectors[i].key, vectors[i].data, + hmac::TypeHash::SHA1, true), + vectors[i].sha1) + << "Case " << i + 1 << " SHA1 mismatch"; + } +} + TEST(HMACTest, RFC4231Vectors) { struct Vector { std::string key_hex; @@ -188,12 +224,12 @@ TEST(HMACTest, RFC4231Vectors) { "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd", 0 }, { repeat("0c", 20), "546573742057697468205472756e636174696f6e", - "a3b6167473100ee06e0c796c2955552b", - "415fad6271580a531d4179bc891d87a6", + "a3b6167473100ee06e0c796c2955552bfa6f7c0a6a8aef8b93f860aab0cd20c5", + "415fad6271580a531d4179bc891d87a650188707922a4fbb36663a1eb16da008711c5b50ddd0fc235084eb9d3364a1454fb2ef67cd1d29fe6773068ea266e96b", 16 }, { repeat("aa", 131), "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374", - "60e431591ee0b67f0d8a26aacbf5b77f", - "80b24263c7c1a3ebb71493c1dd7be8b4", + "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54", + "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598", 16 }, { repeat("aa", 131), "5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f2062652068617368656420746f67657468657220616e64207468652064617461206e6565647320746f2062652068617368656420746f6765746865722e", @@ -209,12 +245,16 @@ TEST(HMACTest, RFC4231Vectors) { auto h512 = hmac::get_hmac(key, data, hmac::TypeHash::SHA512); std::string h256_hex = hmac::to_hex(std::string(h256.begin(), h256.end())); std::string h512_hex = hmac::to_hex(std::string(h512.begin(), h512.end())); - if (vectors[i].truncate_to) { - h256_hex = h256_hex.substr(0, vectors[i].truncate_to * 2); - h512_hex = h512_hex.substr(0, vectors[i].truncate_to * 2); - } EXPECT_EQ(h256_hex, vectors[i].sha256) << "Case " << i + 1 << " SHA-256 mismatch"; EXPECT_EQ(h512_hex, vectors[i].sha512) << "Case " << i + 1 << " SHA-512 mismatch"; + if (vectors[i].truncate_to) { + EXPECT_EQ(h256_hex.substr(0, vectors[i].truncate_to * 2), + vectors[i].sha256.substr(0, vectors[i].truncate_to * 2)) + << "Case " << i + 1 << " SHA-256 trunc mismatch"; + EXPECT_EQ(h512_hex.substr(0, vectors[i].truncate_to * 2), + vectors[i].sha512.substr(0, vectors[i].truncate_to * 2)) + << "Case " << i + 1 << " SHA-512 trunc mismatch"; + } } } diff --git a/test_totp.cpp b/test_totp.cpp index 5cdf12d..82d14f8 100644 --- a/test_totp.cpp +++ b/test_totp.cpp @@ -37,3 +37,46 @@ TEST(TotpBoundaryTest, MaxTimestampValidatesMaxCounter) { int token = hmac::get_hotp_code(key.data(), key.size(), max_counter, digits, hmac::TypeHash::SHA1); EXPECT_TRUE(hmac::is_totp_token_valid(token, key.data(), key.size(), max_timestamp, 1, digits, hmac::TypeHash::SHA1)); } + +TEST(HotpTest, RFC4226Vectors) { + std::string key = "12345678901234567890"; + int expected[10] = {755224, 287082, 359152, 969429, 338314, + 254676, 287922, 162583, 399871, 520489}; + for (uint64_t counter = 0; counter < 10; ++counter) { + EXPECT_EQ(hmac::get_hotp_code(key.data(), key.size(), counter, + 6, hmac::TypeHash::SHA1), + expected[counter]) + << "Counter " << counter; + } +} + +TEST(TotpTest, RFC6238Vectors) { + struct Vector { + uint64_t time; + int sha1; + int sha256; + int sha512; + }; + std::vector vectors = { + {59, 94287082, 46119246, 90693936}, + {1111111109, 7081804, 68084774, 25091201}, + {1111111111, 14050471, 67062674, 99943326}, + {1234567890, 89005924, 91819424, 93441116}, + {2000000000, 69279037, 90698825, 38618901}, + {20000000000ULL, 65353130, 77737706, 47863826}, + }; + + std::string key_sha1 = "12345678901234567890"; + std::string key_sha256 = "12345678901234567890123456789012"; + std::string key_sha512 = + "1234567890123456789012345678901234567890123456789012345678901234"; + + for (const auto& v : vectors) { + EXPECT_EQ(hmac::get_totp_code_at(key_sha1, v.time, 30, 8, hmac::TypeHash::SHA1), + v.sha1); + EXPECT_EQ(hmac::get_totp_code_at(key_sha256, v.time, 30, 8, hmac::TypeHash::SHA256), + v.sha256); + EXPECT_EQ(hmac::get_totp_code_at(key_sha512, v.time, 30, 8, hmac::TypeHash::SHA512), + v.sha512); + } +}