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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

92.54
/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") {}
2✔
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
         result.start_timer();
32✔
29

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

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

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

42
         result.test_int_eq("OTP", hotp.generate_hotp(counter), otp);
64✔
43

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

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

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

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

63
         result.end_timer();
32✔
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
         result.start_timer();
4✔
79

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

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

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

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

97
         result.test_int_eq("TOTP generate", totp.generate_totp(time), otp);
8✔
98

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

105
         result.end_timer();
4✔
106
         return result;
4✔
107
      }
12✔
108

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

126
BOTAN_REGISTER_TEST("otp", "otp_totp", TOTP_KAT_Tests);
127

128
#endif
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

© 2025 Coveralls, Inc