• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.06
/src/tests/test_otp.cpp
1
/*
2
* OTP tests
3
* (C) 2017 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_HOTP) && defined(BOTAN_HAS_TOTP)
11
   #include <botan/hash.h>
12
   #include <botan/otp.h>
13
   #include <botan/internal/calendar.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
#if defined(BOTAN_HAS_HOTP) && defined(BOTAN_HAS_TOTP)
21

22
class HOTP_KAT_Tests final : public Text_Based_Test {
×
23
   public:
24
      HOTP_KAT_Tests() : Text_Based_Test("otp/hotp.vec", "Key,Digits,Counter,OTP") {}
2✔
25

26
      bool clear_between_callbacks() const override { return false; }
32✔
27

28
      Test::Result run_one_test(const std::string& hash_algo, const VarMap& vars) override {
32✔
29
         Test::Result result("HOTP " + hash_algo);
32✔
30

31
         auto hash_test = Botan::HashFunction::create(hash_algo);
32✔
32
         if(!hash_test) {
32✔
33
            return {result};
×
34
         }
35

36
         const auto key = Botan::SymmetricKey(vars.get_req_bin("Key"));
32✔
37
         const uint32_t otp = static_cast<uint32_t>(vars.get_req_sz("OTP"));
32✔
38
         const uint64_t counter = vars.get_req_sz("Counter");
32✔
39
         const size_t digits = vars.get_req_sz("Digits");
32✔
40

41
         Botan::HOTP hotp(key, hash_algo, digits);
32✔
42

43
         result.test_u32_eq("OTP", hotp.generate_hotp(counter), otp);
32✔
44

45
         std::pair<bool, uint64_t> otp_res = hotp.verify_hotp(otp, counter, 0);
32✔
46
         result.test_is_true("OTP verify result", otp_res.first);
32✔
47
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter + 1);
32✔
48

49
         // Test invalid OTP
50
         otp_res = hotp.verify_hotp(otp + 1, counter, 0);
32✔
51
         result.test_is_false("OTP verify result", otp_res.first);
32✔
52
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter);
32✔
53

54
         // Test invalid OTP with long range
55
         otp_res = hotp.verify_hotp(otp + 1, counter, 100);
32✔
56
         result.test_is_false("OTP verify result", otp_res.first);
32✔
57
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter);
32✔
58

59
         // Test valid OTP with long range
60
         otp_res = hotp.verify_hotp(otp, counter - 90, 100);
32✔
61
         result.test_is_true("OTP verify result", otp_res.first);
32✔
62
         result.test_u64_eq("OTP verify next counter", otp_res.second, counter + 1);
32✔
63

64
         return result;
32✔
65
      }
96✔
66
};
67

68
BOTAN_REGISTER_TEST("otp", "otp_hotp", HOTP_KAT_Tests);
69

70
class TOTP_KAT_Tests final : public Text_Based_Test {
×
71
   public:
72
      TOTP_KAT_Tests() : Text_Based_Test("otp/totp.vec", "Key,Digits,Timestep,Timestamp,OTP") {}
2✔
73

74
      bool clear_between_callbacks() const override { return false; }
4✔
75

76
      Test::Result run_one_test(const std::string& hash_algo, const VarMap& vars) override {
4✔
77
         Test::Result result("TOTP " + hash_algo);
4✔
78

79
         auto hash_test = Botan::HashFunction::create(hash_algo);
4✔
80
         if(!hash_test) {
4✔
81
            return {result};
×
82
         }
83

84
         const auto key = Botan::SymmetricKey(vars.get_req_bin("Key"));
4✔
85
         const uint32_t otp = static_cast<uint32_t>(vars.get_req_sz("OTP"));
4✔
86
         const size_t digits = vars.get_req_sz("Digits");
4✔
87
         const size_t timestep = vars.get_req_sz("Timestep");
4✔
88
         const std::string timestamp = vars.get_req_str("Timestamp");
4✔
89

90
         Botan::TOTP totp(key, hash_algo, digits, timestep);
4✔
91

92
         const std::chrono::system_clock::time_point time = from_timestring(timestamp);
4✔
93
         const std::chrono::system_clock::time_point later_time = time + std::chrono::seconds(timestep);
4✔
94
         const std::chrono::system_clock::time_point too_late = time + std::chrono::seconds(2 * timestep);
4✔
95

96
         result.test_u32_eq("TOTP generate", totp.generate_totp(time), otp);
4✔
97

98
         result.test_is_true("TOTP verify valid", totp.verify_totp(otp, time, 0));
4✔
99
         result.test_is_false("TOTP verify invalid", totp.verify_totp(otp ^ 1, time, 0));
4✔
100
         result.test_is_false("TOTP verify time slip", totp.verify_totp(otp, later_time, 0));
4✔
101
         result.test_is_true("TOTP verify time slip allowed", totp.verify_totp(otp, later_time, 1));
4✔
102
         result.test_is_false("TOTP verify time slip out of range", totp.verify_totp(otp, too_late, 1));
4✔
103

104
         return result;
4✔
105
      }
12✔
106

107
   private:
108
      static std::chrono::system_clock::time_point from_timestring(const std::string& time_str) {
4✔
109
         if(time_str.size() != 19) {
4✔
110
            throw Test_Error("Invalid TOTP timestamp string " + time_str);
×
111
         }
112
         // YYYY-MM-DDTHH:MM:SS
113
         // 0123456789012345678
114
         const uint32_t year = static_cast<uint32_t>(std::stoi(time_str.substr(0, 4)));
8✔
115
         const uint32_t month = static_cast<uint32_t>(std::stoi(time_str.substr(5, 2)));
8✔
116
         const uint32_t day = static_cast<uint32_t>(std::stoi(time_str.substr(8, 2)));
8✔
117
         const uint32_t hour = static_cast<uint32_t>(std::stoi(time_str.substr(11, 2)));
8✔
118
         const uint32_t minute = static_cast<uint32_t>(std::stoi(time_str.substr(14, 2)));
8✔
119
         const uint32_t second = static_cast<uint32_t>(std::stoi(time_str.substr(17, 2)));
8✔
120
         return Botan::calendar_point(year, month, day, hour, minute, second).to_std_timepoint();
4✔
121
      }
122
};
123

124
BOTAN_REGISTER_TEST("otp", "otp_totp", TOTP_KAT_Tests);
125

126
#endif
127

128
}  // namespace
129

130
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc