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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 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
#if defined(BOTAN_HAS_HOTP) && defined(BOTAN_HAS_TOTP)
19

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

24
      bool clear_between_callbacks() const override { return false; }
32✔
25

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

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

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

38
         Botan::HOTP hotp(key, hash_algo, digits);
32✔
39

40
         result.test_int_eq("OTP", hotp.generate_hotp(counter), otp);
32✔
41

42
         std::pair<bool, uint64_t> otp_res = hotp.verify_hotp(otp, counter, 0);
32✔
43
         result.test_eq("OTP verify result", otp_res.first, true);
32✔
44
         result.confirm("OTP verify next counter", otp_res.second == counter + 1);
64✔
45

46
         // Test invalid OTP
47
         otp_res = hotp.verify_hotp(otp + 1, counter, 0);
32✔
48
         result.test_eq("OTP verify result", otp_res.first, false);
32✔
49
         result.confirm("OTP verify next counter", otp_res.second == counter);
64✔
50

51
         // Test invalid OTP with long range
52
         otp_res = hotp.verify_hotp(otp + 1, counter, 100);
32✔
53
         result.test_eq("OTP verify result", otp_res.first, false);
32✔
54
         result.confirm("OTP verify next counter", otp_res.second == counter);
64✔
55

56
         // Test valid OTP with long range
57
         otp_res = hotp.verify_hotp(otp, counter - 90, 100);
32✔
58
         result.test_eq("OTP verify result", otp_res.first, true);
32✔
59
         result.confirm("OTP verify next counter", otp_res.second == counter + 1);
64✔
60

61
         return result;
32✔
62
      }
96✔
63
};
64

65
BOTAN_REGISTER_TEST("otp", "otp_hotp", HOTP_KAT_Tests);
66

67
class TOTP_KAT_Tests final : public Text_Based_Test {
×
68
   public:
69
      TOTP_KAT_Tests() : Text_Based_Test("otp/totp.vec", "Key,Digits,Timestep,Timestamp,OTP") {}
3✔
70

71
      bool clear_between_callbacks() const override { return false; }
4✔
72

73
      Test::Result run_one_test(const std::string& hash_algo, const VarMap& vars) override {
4✔
74
         Test::Result result("TOTP " + hash_algo);
4✔
75

76
         auto hash_test = Botan::HashFunction::create(hash_algo);
4✔
77
         if(!hash_test)
4✔
78
            return {result};
×
79

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

86
         Botan::TOTP totp(key, hash_algo, digits, timestep);
4✔
87

88
         std::chrono::system_clock::time_point time = from_timestring(timestamp);
4✔
89
         std::chrono::system_clock::time_point later_time = time + std::chrono::seconds(timestep);
4✔
90
         std::chrono::system_clock::time_point too_late = time + std::chrono::seconds(2 * timestep);
4✔
91

92
         result.test_int_eq("TOTP generate", totp.generate_totp(time), otp);
4✔
93

94
         result.test_eq("TOTP verify valid", totp.verify_totp(otp, time, 0), true);
4✔
95
         result.test_eq("TOTP verify invalid", totp.verify_totp(otp ^ 1, time, 0), false);
4✔
96
         result.test_eq("TOTP verify time slip", totp.verify_totp(otp, later_time, 0), false);
4✔
97
         result.test_eq("TOTP verify time slip allowed", totp.verify_totp(otp, later_time, 1), true);
4✔
98
         result.test_eq("TOTP verify time slip out of range", totp.verify_totp(otp, too_late, 1), false);
4✔
99

100
         return result;
4✔
101
      }
16✔
102

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

119
BOTAN_REGISTER_TEST("otp", "otp_totp", TOTP_KAT_Tests);
120

121
#endif
122

123
}
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

© 2025 Coveralls, Inc