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

randombit / botan / 16293079084

15 Jul 2025 12:20PM UTC coverage: 90.627% (+0.003%) from 90.624%
16293079084

push

github

web-flow
Merge pull request #4990 from randombit/jack/string-and-span

Improve string<->span conversions

99640 of 109945 relevant lines covered (90.63%)

12253617.72 hits per line

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

88.69
/src/tests/test_roughtime.cpp
1
/*
2
* (C) 2019 Nuno Goncalves <nunojpg@gmail.com>
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "test_rng.h"
8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_BIGINT)
11
   #include <botan/bigint.h>
12
#endif
13

14
#if defined(BOTAN_HAS_ROUGHTIME)
15
   #include <botan/base64.h>
16
   #include <botan/ed25519.h>
17
   #include <botan/hex.h>
18
   #include <botan/mem_ops.h>
19
   #include <botan/roughtime.h>
20
#endif
21

22
namespace Botan_Tests {
23

24
#if defined(BOTAN_HAS_ROUGHTIME)
25

26
class Roughtime_Request_Tests final : public Text_Based_Test {
×
27
   public:
28
      Roughtime_Request_Tests() : Text_Based_Test("roughtime/roughtime_request.vec", "Nonce,Request") {}
2✔
29

30
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
2✔
31
         Test::Result result("Roughtime request");
2✔
32

33
         const auto nonce = Botan::Roughtime::Nonce(vars.get_req_bin("Nonce"));
4✔
34
         const auto request_v = vars.get_req_bin("Request");
2✔
35

36
         const auto request = Botan::Roughtime::encode_request(nonce);
2✔
37
         result.test_eq(
2✔
38
            "encode", type == "Valid", request == Botan::typecast_copy<std::array<uint8_t, 1024>>(request_v.data()));
2✔
39

40
         return result;
2✔
41
      }
2✔
42
};
43

44
BOTAN_REGISTER_TEST("roughtime", "roughtime_request", Roughtime_Request_Tests);
45

46
class Roughtime_Response_Tests final : public Text_Based_Test {
×
47
   public:
48
      Roughtime_Response_Tests() :
1✔
49
            Text_Based_Test(
50
               "roughtime/roughtime_response.vec", "Response", "Nonce,Pubkey,MidpointMicroSeconds,RadiusMicroSeconds") {
2✔
51
      }
1✔
52

53
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
17✔
54
         Test::Result result("Roughtime response");
17✔
55

56
         const auto response_v = vars.get_req_bin("Response");
17✔
57
         const auto nonce_bits = vars.has_key("Nonce") ? vars.get_opt_bin("Nonce") : std::vector<uint8_t>(64);
40✔
58

59
         const Botan::Roughtime::Nonce nonce(nonce_bits);
17✔
60
         try {
17✔
61
            const auto response = Botan::Roughtime::Response::from_bits(response_v, nonce);
17✔
62

63
            const auto pubkey = vars.get_req_bin("Pubkey");
4✔
64
            if(pubkey.size() != 32) {
4✔
65
               throw Test_Error("Unexpected Roughtime Ed25519 pubkey size");
×
66
            }
67

68
            if(!response.validate(Botan::Ed25519_PublicKey(pubkey))) {
4✔
69
               result.confirm("fail_validation", type == "Invalid");
2✔
70
            } else {
71
               const auto midpoint = Botan::Roughtime::Response::sys_microseconds64(
3✔
72
                  std::chrono::microseconds(vars.get_req_u64("MidpointMicroSeconds")));
3✔
73
               const auto radius = std::chrono::microseconds(vars.get_req_u32("RadiusMicroSeconds"));
3✔
74

75
               result.confirm("midpoint", response.utc_midpoint() == midpoint);
6✔
76
               result.confirm("radius", response.utc_radius() == radius);
6✔
77
               result.confirm("OK", type == "Valid");
6✔
78
            }
79
         } catch(const Botan::Roughtime::Roughtime_Error& e) {
17✔
80
            result.confirm(e.what(), type == "Invalid");
26✔
81
         }
13✔
82

83
         return result;
17✔
84
      }
34✔
85
};
86

87
BOTAN_REGISTER_TEST("roughtime", "roughtime_response", Roughtime_Response_Tests);
88

89
class Roughtime_nonce_from_blind_Tests final : public Text_Based_Test {
×
90
   public:
91
      Roughtime_nonce_from_blind_Tests() :
1✔
92
            Text_Based_Test("roughtime/roughtime_nonce_from_blind.vec", "Response,Blind,Nonce") {}
2✔
93

94
      Test::Result run_one_test(const std::string& type, const VarMap& vars) override {
2✔
95
         Test::Result result("roughtime nonce_from_blind");
2✔
96

97
         const auto response = vars.get_req_bin("Response");
2✔
98
         const auto blind = Botan::Roughtime::Nonce(vars.get_req_bin("Blind"));
4✔
99
         const auto nonce = Botan::Roughtime::Nonce(vars.get_req_bin("Nonce"));
4✔
100

101
         result.test_eq(
2✔
102
            "fail_validation", Botan::Roughtime::nonce_from_blind(response, blind) == nonce, type == "Valid");
2✔
103

104
         return result;
2✔
105
      }
2✔
106
};
107

108
BOTAN_REGISTER_TEST("roughtime", "roughtime_nonce_from_blind", Roughtime_nonce_from_blind_Tests);
109

110
class Roughtime final : public Test {
×
111
   private:
112
      static Test::Result test_nonce(Botan::RandomNumberGenerator& rng) {
1✔
113
         Test::Result result("roughtime nonce");
1✔
114

115
         auto rand64 = Botan::unlock(rng.random_vec(64));
2✔
116
         Botan::Roughtime::Nonce nonce_v(rand64);
1✔
117
         result.confirm("nonce from vector",
2✔
118
                        nonce_v.get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
1✔
119
         Botan::Roughtime::Nonce nonce_a(Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
1✔
120
         result.confirm("nonce from array",
2✔
121
                        nonce_v.get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
1✔
122
         rand64.push_back(10);
1✔
123
         result.test_throws("vector oversize", [&rand64]() { Botan::Roughtime::Nonce nonce_v2(rand64); });  //size 65
3✔
124
         rand64.pop_back();
1✔
125
         rand64.pop_back();
1✔
126
         result.test_throws("vector undersize", [&rand64]() { Botan::Roughtime::Nonce nonce_v2(rand64); });  //size 63
3✔
127

128
         return result;
1✔
129
      }
1✔
130

131
      static Test::Result test_chain(Botan::RandomNumberGenerator& rng) {
1✔
132
         Test::Result result("roughtime chain");
1✔
133

134
         Botan::Roughtime::Chain c1;
1✔
135
         result.confirm("default constructed is empty", c1.links().empty() && c1.responses().empty());
2✔
136

137
         auto rand64 = Botan::unlock(rng.random_vec(64));
2✔
138
         Botan::Roughtime::Nonce nonce_v(rand64);
1✔
139
         result.confirm(
3✔
140
            "empty chain nonce is blind",
141
            c1.next_nonce(nonce_v).get_nonce() == Botan::typecast_copy<std::array<uint8_t, 64>>(rand64.data()));
1✔
142

143
         const std::string chain_str =
1✔
144
            "ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= eu9yhsJfVfguVSqGZdE8WKIxaBBM0ZG3Vmuc+IyZmG2YVmrIktUByDdwIFw6F4rZqmSFsBO85ljoVPz5bVPCOw== BQAAAEAAAABAAAAApAAAADwBAABTSUcAUEFUSFNSRVBDRVJUSU5EWBnGOEajOwPA6G7oL47seBP4C7eEpr57H43C2/fK/kMA0UGZVUdf4KNX8oxOK6JIcsbVk8qhghTwA70qtwpYmQkDAAAABAAAAAwAAABSQURJTUlEUFJPT1RAQg8AJrA8tEqPBQAqisiuAxgy2Pj7UJAiWbCdzGz1xcCnja3T+AqhC8fwpeIwW4GPy/vEb/awXW2DgSLKJfzWIAz+2lsR7t4UjNPvAgAAAEAAAABTSUcAREVMRes9Ch4X0HIw5KdOTB8xK4VDFSJBD/G9t7Et/CU7UW61OiTBXYYQTG2JekWZmGa0OHX1JPGG+APkpbsNw0BKUgYDAAAAIAAAACgAAABQVUJLTUlOVE1BWFR/9BWjpsWTQ1f6iUJea3EfZ1MkX3ftJiV3ABqNLpncFwAAAAAAAAAA//////////8AAAAA\n"
145
            "ed25519 gD63hSj3ScS+wuOeGrubXlq35N1c5Lby/S+T7MNTjxo= uLeTON9D+2HqJMzK6sYWLNDEdtBl9t/9yw1cVAOm0/sONH5Oqdq9dVPkC9syjuWbglCiCPVF+FbOtcxCkrgMmA== BQAAAEAAAABAAAAApAAAADwBAABTSUcAUEFUSFNSRVBDRVJUSU5EWOw1jl0uSiBEH9HE8/6r7zxoSc01f48vw+UzH8+VJoPelnvVJBj4lnH8uRLh5Aw0i4Du7XM1dp2u0r/I5PzhMQoDAAAABAAAAAwAAABSQURJTUlEUFJPT1RAQg8AUBo+tEqPBQC47l77to7ESFTVhlw1SC74P5ssx6gpuJ6eP+1916GuUiySGE/x3Fp0c3otUGAdsRQou5p9PDTeane/YEeVq4/8AgAAAEAAAABTSUcAREVMRe5T1ml8wHyWAcEtHP/U5Rg/jFXTEXOSglngSa4aI/CECVdy4ZNWeP6vv+2//ZW7lQsrWo7ZkXpvm9BdBONRSQIDAAAAIAAAACgAAABQVUJLTUlOVE1BWFQpXlenV0OfVisvp9jDHXLw8vymZVK9Pgw9k6Edf8ZEhUgSGEc5jwUASHLvZE2PBQAAAAAA\n";
1✔
146

147
         Botan::Roughtime::Chain c2(chain_str);
1✔
148
         result.confirm("have two elements", c2.links().size() == 2 && c2.responses().size() == 2);
3✔
149
         result.confirm("serialize loopback", c2.to_string() == chain_str);
2✔
150

151
         c1.append(c2.links()[0], 1);
1✔
152
         result.confirm("append ok", c1.links().size() == 1 && c1.responses().size() == 1);
3✔
153
         c1.append(c2.links()[1], 1);
1✔
154
         result.confirm("max size", c1.links().size() == 1 && c1.responses().size() == 1);
3✔
155

156
         result.test_throws("non-positive max chain size", [&]() { c1.append(c2.links()[1], 0); });
3✔
157
         result.test_throws("1 field", [&]() { Botan::Roughtime::Chain a("ed25519"); });
3✔
158
         result.test_throws(
2✔
159
            "2 fields", [&]() { Botan::Roughtime::Chain a("ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE="); });
1✔
160
         result.test_throws("3 fields", [&]() {
2✔
161
            Botan::Roughtime::Chain a(
1✔
162
               "ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= eu9yhsJfVfguVSqGZdE8WKIxaBBM0ZG3Vmuc+IyZmG2YVmrIktUByDdwIFw6F4rZqmSFsBO85ljoVPz5bVPCOw==");
1✔
163
         });
×
164
         result.test_throws("5 fields", [&]() {
2✔
165
            Botan::Roughtime::Chain a(
1✔
166
               "ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= eu9yhsJfVfguVSqGZdE8WKIxaBBM0ZG3Vmuc+IyZmG2YVmrIktUByDdwIFw6F4rZqmSFsBO85ljoVPz5bVPCOw== BQAAAEAAAABAAAAApAAAADwBAABTSUcAUEFUSFNSRVBDRVJUSU5EWBnGOEajOwPA6G7oL47seBP4C7eEpr57H43C2/fK/kMA0UGZVUdf4KNX8oxOK6JIcsbVk8qhghTwA70qtwpYmQkDAAAABAAAAAwAAABSQURJTUlEUFJPT1RAQg8AJrA8tEqPBQAqisiuAxgy2Pj7UJAiWbCdzGz1xcCnja3T+AqhC8fwpeIwW4GPy/vEb/awXW2DgSLKJfzWIAz+2lsR7t4UjNPvAgAAAEAAAABTSUcAREVMRes9Ch4X0HIw5KdOTB8xK4VDFSJBD/G9t7Et/CU7UW61OiTBXYYQTG2JekWZmGa0OHX1JPGG+APkpbsNw0BKUgYDAAAAIAAAACgAAABQVUJLTUlOVE1BWFR/9BWjpsWTQ1f6iUJea3EfZ1MkX3ftJiV3ABqNLpncFwAAAAAAAAAA//////////8AAAAA abc");
1✔
167
         });
×
168
         result.test_throws("invalid key type", [&]() {
2✔
169
            Botan::Roughtime::Chain a(
1✔
170
               "rsa bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= eu9yhsJfVfguVSqGZdE8WKIxaBBM0ZG3Vmuc+IyZmG2YVmrIktUByDdwIFw6F4rZqmSFsBO85ljoVPz5bVPCOw== BQAAAEAAAABAAAAApAAAADwBAABTSUcAUEFUSFNSRVBDRVJUSU5EWBnGOEajOwPA6G7oL47seBP4C7eEpr57H43C2/fK/kMA0UGZVUdf4KNX8oxOK6JIcsbVk8qhghTwA70qtwpYmQkDAAAABAAAAAwAAABSQURJTUlEUFJPT1RAQg8AJrA8tEqPBQAqisiuAxgy2Pj7UJAiWbCdzGz1xcCnja3T+AqhC8fwpeIwW4GPy/vEb/awXW2DgSLKJfzWIAz+2lsR7t4UjNPvAgAAAEAAAABTSUcAREVMRes9Ch4X0HIw5KdOTB8xK4VDFSJBD/G9t7Et/CU7UW61OiTBXYYQTG2JekWZmGa0OHX1JPGG+APkpbsNw0BKUgYDAAAAIAAAACgAAABQVUJLTUlOVE1BWFR/9BWjpsWTQ1f6iUJea3EfZ1MkX3ftJiV3ABqNLpncFwAAAAAAAAAA//////////8AAAAA");
1✔
171
         });
×
172
         result.test_throws("invalid key", [&]() {
2✔
173
            Botan::Roughtime::Chain a(
1✔
174
               "ed25519 bbT+RPS7zKX6wssPibzmwWqU9ffRV5oj2OresSmhE= eu9yhsJfVfguVSqGZdE8WKIxaBBM0ZG3Vmuc+IyZmG2YVmrIktUByDdwIFw6F4rZqmSFsBO85ljoVPz5bVPCOw== BQAAAEAAAABAAAAApAAAADwBAABTSUcAUEFUSFNSRVBDRVJUSU5EWBnGOEajOwPA6G7oL47seBP4C7eEpr57H43C2/fK/kMA0UGZVUdf4KNX8oxOK6JIcsbVk8qhghTwA70qtwpYmQkDAAAABAAAAAwAAABSQURJTUlEUFJPT1RAQg8AJrA8tEqPBQAqisiuAxgy2Pj7UJAiWbCdzGz1xcCnja3T+AqhC8fwpeIwW4GPy/vEb/awXW2DgSLKJfzWIAz+2lsR7t4UjNPvAgAAAEAAAABTSUcAREVMRes9Ch4X0HIw5KdOTB8xK4VDFSJBD/G9t7Et/CU7UW61OiTBXYYQTG2JekWZmGa0OHX1JPGG+APkpbsNw0BKUgYDAAAAIAAAACgAAABQVUJLTUlOVE1BWFR/9BWjpsWTQ1f6iUJea3EfZ1MkX3ftJiV3ABqNLpncFwAAAAAAAAAA//////////8AAAAA");
1✔
175
         });
×
176
         result.test_throws("invalid nonce", [&]() {
2✔
177
            Botan::Roughtime::Chain a(
1✔
178
               "ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= eu9yhsJfVfguVSqGZdE8WKIxaBBM0ZG3Vmuc+IyZmG2UByDdwIFw6F4rZqmSFsBO85ljoVPz5bVPCOw== BQAAAEAAAABAAAAApAAAADwBAABTSUcAUEFUSFNSRVBDRVJUSU5EWBnGOEajOwPA6G7oL47seBP4C7eEpr57H43C2/fK/kMA0UGZVUdf4KNX8oxOK6JIcsbVk8qhghTwA70qtwpYmQkDAAAABAAAAAwAAABSQURJTUlEUFJPT1RAQg8AJrA8tEqPBQAqisiuAxgy2Pj7UJAiWbCdzGz1xcCnja3T+AqhC8fwpeIwW4GPy/vEb/awXW2DgSLKJfzWIAz+2lsR7t4UjNPvAgAAAEAAAABTSUcAREVMRes9Ch4X0HIw5KdOTB8xK4VDFSJBD/G9t7Et/CU7UW61OiTBXYYQTG2JekWZmGa0OHX1JPGG+APkpbsNw0BKUgYDAAAAIAAAACgAAABQVUJLTUlOVE1BWFR/9BWjpsWTQ1f6iUJea3EfZ1MkX3ftJiV3ABqNLpncFwAAAAAAAAAA//////////8AAAAA");
1✔
179
         });
×
180

181
         return result;
1✔
182
      }
2✔
183

184
      static Test::Result test_server_information() {
1✔
185
         Test::Result result("roughtime server_information");
1✔
186

187
         const auto servers = Botan::Roughtime::servers_from_str(
1✔
188
            "Chainpoint-Roughtime ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= udp roughtime.chainpoint.org:2002\n"
189
            "Cloudflare-Roughtime ed25519 0GD7c3yP8xEc4Zl2zeuN2SlLvDVVocjsPSL8/Rl/7zg= udp roughtime.cloudflare.com:2003\n"
190
            "Google-Sandbox-Roughtime ed25519 etPaaIxcBMY1oUeGpwvPMCJMwlRVNxv51KK/tktoJTQ= udp roughtime.sandbox.google.com:2002\n"
191
            "int08h-Roughtime ed25519 AW5uAoTSTDfG5NfY1bTh08GUnOqlRb+HVhbJ3ODJvsE= udp roughtime.int08h.com:2002\n"
192
            "ticktock ed25519 cj8GsiNlRkqiDElAeNMSBBMwrAl15hYPgX50+GWX/lA= udp ticktock.mixmin.net:5333\n");
1✔
193

194
         result.confirm("size", servers.size() == 5);
2✔
195
         result.test_eq("name", servers[0].name(), "Chainpoint-Roughtime");
2✔
196
         result.test_eq("name", servers[4].name(), "ticktock");
2✔
197
         result.confirm(
3✔
198
            "public key",
199
            servers[0].public_key().get_public_key() ==
1✔
200
               Botan::Ed25519_PublicKey(Botan::base64_decode("bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE="))
2✔
201
                  .get_public_key());
202
         result.confirm("single address", servers[0].addresses().size() == 1);
2✔
203
         result.test_eq("address", servers[0].addresses()[0], "roughtime.chainpoint.org:2002");
2✔
204

205
         result.test_throws("1 field", [&]() { Botan::Roughtime::servers_from_str("A"); });
3✔
206
         result.test_throws("2 fields", [&]() { Botan::Roughtime::servers_from_str("A ed25519"); });
3✔
207
         result.test_throws("3 fields", [&]() {
2✔
208
            Botan::Roughtime::servers_from_str("A ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE=");
1✔
209
         });
×
210
         result.test_throws("4 fields", [&]() {
2✔
211
            Botan::Roughtime::servers_from_str("A ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= udp");
1✔
212
         });
×
213
         result.test_throws("invalid address", [&]() {
2✔
214
            Botan::Roughtime::servers_from_str("A ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= udp ");
1✔
215
         });
×
216
         result.test_throws("invalid key type", [&]() {
2✔
217
            Botan::Roughtime::servers_from_str(
1✔
218
               "A rsa bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= udp roughtime.chainpoint.org:2002");
219
         });
×
220
         result.test_throws("invalid key", [&]() {
2✔
221
            Botan::Roughtime::servers_from_str(
1✔
222
               "A ed25519 bbT+RP7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= udp roughtime.chainpoint.org:2002");
223
         });
×
224
         result.test_throws("invalid protocol", [&]() {
2✔
225
            Botan::Roughtime::servers_from_str(
1✔
226
               "A ed25519 bbT+RPS7zKX6w71ssPibzmwWqU9ffRV5oj2OresSmhE= tcp roughtime.chainpoint.org:2002");
227
         });
×
228

229
         return result;
1✔
230
      }
1✔
231

232
      static Test::Result test_request_online(Botan::RandomNumberGenerator& rng) {
1✔
233
         Test::Result result("roughtime request online");
1✔
234

235
         Botan::Roughtime::Nonce nonce(rng);
1✔
236
         try {
1✔
237
            const auto response_raw =
1✔
238
               Botan::Roughtime::online_request("roughtime.cloudflare.com:2003", nonce, std::chrono::seconds(5));
1✔
239
            const auto now = std::chrono::system_clock::now();
1✔
240
            const auto response = Botan::Roughtime::Response::from_bits(response_raw, nonce);
1✔
241
            std::chrono::milliseconds local_clock_max_error(1000);
1✔
242
            const auto diff_abs =
1✔
243
               now >= response.utc_midpoint() ? now - response.utc_midpoint() : response.utc_midpoint() - now;
1✔
244
            result.confirm("online", diff_abs <= (response.utc_radius() + local_clock_max_error));
2✔
245
         } catch(const std::exception& e) {
1✔
246
            result.test_failure(e.what());
×
247
         }
×
248
         return result;
1✔
249
      }
×
250

251
   public:
252
      std::vector<Test::Result> run() override {
1✔
253
         auto& rng = this->rng();
1✔
254

255
         std::vector<Test::Result> results{test_nonce(rng), test_chain(rng), test_server_information()};
4✔
256

257
         if(Test::options().run_online_tests()) {
1✔
258
            results.push_back(test_request_online(rng));
2✔
259
         }
260

261
         return results;
1✔
262
      }
1✔
263
};
264

265
BOTAN_REGISTER_TEST("roughtime", "roughtime_tests", Roughtime);
266

267
#endif
268

269
}  // 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