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

randombit / botan / 12777311287

14 Jan 2025 10:12PM UTC coverage: 91.203% (-0.04%) from 91.247%
12777311287

push

github

web-flow
Merge pull request #4549 from randombit/jack/pcurves-test-cleanup

Remove the pcurves specific tests

93416 of 102426 relevant lines covered (91.2%)

11527204.15 hits per line

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

77.36
/src/tests/tests.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#include <botan/hex.h>
10
#include <botan/internal/cpuid.h>
11
#include <botan/internal/filesystem.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/parsing.h>
14
#include <botan/internal/stl_util.h>
15
#include <fstream>
16
#include <iomanip>
17
#include <sstream>
18

19
#if defined(BOTAN_HAS_BIGINT)
20
   #include <botan/bigint.h>
21
#endif
22

23
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
24
   #include <botan/ec_point.h>
25
#endif
26

27
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
28
   #include <stdlib.h>
29
   #include <unistd.h>
30
#endif
31

32
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
33
   #include <version>
34
   #if defined(__cpp_lib_filesystem)
35
      #include <filesystem>
36
   #endif
37
#endif
38

39
namespace Botan_Tests {
40

41
void Test::Result::merge(const Result& other, bool ignore_test_name) {
116,731✔
42
   if(who() != other.who()) {
116,731✔
43
      if(!ignore_test_name) {
73✔
44
         throw Test_Error("Merging tests from different sources");
×
45
      }
46

47
      // When deliberately merging results with different names, the code location is
48
      // likely inconsistent and must be discarded.
49
      m_where.reset();
73✔
50
   } else {
51
      m_where = other.m_where;
116,658✔
52
   }
53

54
   m_timestamp = std::min(m_timestamp, other.m_timestamp);
116,731✔
55
   m_ns_taken += other.m_ns_taken;
116,731✔
56
   m_tests_passed += other.m_tests_passed;
116,731✔
57
   m_fail_log.insert(m_fail_log.end(), other.m_fail_log.begin(), other.m_fail_log.end());
116,731✔
58
   m_log.insert(m_log.end(), other.m_log.begin(), other.m_log.end());
116,731✔
59
}
116,731✔
60

61
void Test::Result::start_timer() {
984✔
62
   if(m_started == 0) {
984✔
63
      m_started = Test::timestamp();
1,968✔
64
   }
65
}
984✔
66

67
void Test::Result::end_timer() {
1,096✔
68
   if(m_started > 0) {
1,096✔
69
      m_ns_taken += Test::timestamp() - m_started;
1,966✔
70
      m_started = 0;
983✔
71
   }
72
}
1,096✔
73

74
void Test::Result::test_note(const std::string& note, const char* extra) {
2,753✔
75
   if(!note.empty()) {
2,753✔
76
      std::ostringstream out;
2,753✔
77
      out << who() << " " << note;
2,753✔
78
      if(extra) {
2,753✔
79
         out << ": " << extra;
12✔
80
      }
81
      m_log.push_back(out.str());
2,753✔
82
   }
2,753✔
83
}
2,753✔
84

85
void Test::Result::note_missing(const std::string& whatever) {
275✔
86
   static std::set<std::string> s_already_seen;
275✔
87

88
   if(!s_already_seen.contains(whatever)) {
275✔
89
      test_note("Skipping tests due to missing " + whatever);
5✔
90
      s_already_seen.insert(whatever);
5✔
91
   }
92
}
275✔
93

94
bool Test::Result::ThrowExpectations::check(const std::string& test_name, Test::Result& result) {
47,845✔
95
   m_consumed = true;
47,845✔
96

97
   try {
47,845✔
98
      m_fn();
47,845✔
99
      if(!m_expect_success) {
1,645✔
100
         return result.test_failure(test_name + " failed to throw expected exception");
2✔
101
      }
102
   } catch(const std::exception& ex) {
46,200✔
103
      if(m_expect_success) {
46,198✔
104
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
2✔
105
      }
106
      if(m_expected_exception_type.has_value() && m_expected_exception_type.value() != typeid(ex)) {
46,197✔
107
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
4✔
108
      }
109
      if(m_expected_message.has_value() && m_expected_message.value() != ex.what()) {
46,195✔
110
         return result.test_failure(test_name + " threw exception with unexpected message (expected: '" +
3✔
111
                                    m_expected_message.value() + "', got: '" + ex.what() + "')");
6✔
112
      }
113
   } catch(...) {
46,200✔
114
      if(m_expect_success || m_expected_exception_type.has_value() || m_expected_message.has_value()) {
2✔
115
         return result.test_failure(test_name + " threw unexpected unknown exception");
1✔
116
      }
117
   }
2✔
118

119
   return result.test_success(test_name + " behaved as expected");
95,676✔
120
}
121

122
bool Test::Result::test_throws(const std::string& what, const std::function<void()>& fn) {
45,845✔
123
   return ThrowExpectations(fn).check(what, *this);
91,690✔
124
}
125

126
bool Test::Result::test_throws(const std::string& what, const std::string& expected, const std::function<void()>& fn) {
174✔
127
   return ThrowExpectations(fn).expect_message(expected).check(what, *this);
348✔
128
}
129

130
bool Test::Result::test_no_throw(const std::string& what, const std::function<void()>& fn) {
1,644✔
131
   return ThrowExpectations(fn).expect_success().check(what, *this);
3,288✔
132
}
133

134
bool Test::Result::test_success(const std::string& note) {
3,229,975✔
135
   if(Test::options().log_success()) {
3,229,844✔
136
      test_note(note);
×
137
   }
138
   ++m_tests_passed;
3,229,975✔
139
   return true;
462,120✔
140
}
141

142
bool Test::Result::test_failure(const std::string& what, const std::string& error) {
1✔
143
   return test_failure(who() + " " + what + " with error " + error);
4✔
144
}
145

146
void Test::Result::test_failure(const std::string& what, const uint8_t buf[], size_t buf_len) {
1✔
147
   test_failure(who() + ": " + what + " buf len " + std::to_string(buf_len) + " value " +
5✔
148
                Botan::hex_encode(buf, buf_len));
1✔
149
}
1✔
150

151
bool Test::Result::test_failure(const std::string& err) {
25✔
152
   m_fail_log.push_back(err);
25✔
153

154
   if(Test::options().abort_on_first_fail() && m_who != "Failing Test") {
25✔
155
      std::abort();
×
156
   }
157
   return false;
25✔
158
}
159

160
namespace {
161

162
bool same_contents(const uint8_t x[], const uint8_t y[], size_t len) {
257,536✔
163
   return (len == 0) ? true : std::memcmp(x, y, len) == 0;
256,187✔
164
}
165

166
}  // namespace
167

168
bool Test::Result::test_ne(const std::string& what,
3,347✔
169
                           const uint8_t produced[],
170
                           size_t produced_len,
171
                           const uint8_t expected[],
172
                           size_t expected_len) {
173
   if(produced_len == expected_len && same_contents(produced, expected, expected_len)) {
3,347✔
174
      return test_failure(who() + ": " + what + " produced matching");
6✔
175
   }
176
   return test_success();
6,690✔
177
}
178

179
bool Test::Result::test_eq(const char* producer,
256,093✔
180
                           const std::string& what,
181
                           const uint8_t produced[],
182
                           size_t produced_size,
183
                           const uint8_t expected[],
184
                           size_t expected_size) {
185
   if(produced_size == expected_size && same_contents(produced, expected, expected_size)) {
256,093✔
186
      return test_success();
512,184✔
187
   }
188

189
   std::ostringstream err;
1✔
190

191
   err << who();
1✔
192

193
   if(producer) {
1✔
194
      err << " producer '" << producer << "'";
×
195
   }
196

197
   err << " unexpected result for " << what;
1✔
198

199
   if(produced_size != expected_size) {
1✔
200
      err << " produced " << produced_size << " bytes expected " << expected_size;
1✔
201
   }
202

203
   std::vector<uint8_t> xor_diff(std::min(produced_size, expected_size));
2✔
204
   size_t bytes_different = 0;
1✔
205

206
   for(size_t i = 0; i != xor_diff.size(); ++i) {
4✔
207
      xor_diff[i] = produced[i] ^ expected[i];
3✔
208
      bytes_different += (xor_diff[i] > 0);
3✔
209
   }
210

211
   err << "\nProduced: " << Botan::hex_encode(produced, produced_size)
1✔
212
       << "\nExpected: " << Botan::hex_encode(expected, expected_size);
3✔
213

214
   if(bytes_different > 0) {
1✔
215
      err << "\nXOR Diff: " << Botan::hex_encode(xor_diff);
1✔
216
   }
217

218
   return test_failure(err.str());
1✔
219
}
1✔
220

221
bool Test::Result::test_is_nonempty(const std::string& what_is_it, const std::string& to_examine) {
32,437✔
222
   if(to_examine.empty()) {
32,437✔
223
      return test_failure(what_is_it + " was empty");
1✔
224
   }
225
   return test_success();
64,872✔
226
}
227

228
bool Test::Result::test_eq(const std::string& what, const std::string& produced, const std::string& expected) {
68,693✔
229
   return test_is_eq(what, produced, expected);
68,693✔
230
}
231

232
bool Test::Result::test_eq(const std::string& what, const char* produced, const char* expected) {
20✔
233
   return test_is_eq(what, std::string(produced), std::string(expected));
20✔
234
}
235

236
bool Test::Result::test_eq(const std::string& what, size_t produced, size_t expected) {
135,325✔
237
   return test_is_eq(what, produced, expected);
135,325✔
238
}
239

240
bool Test::Result::test_eq_sz(const std::string& what, size_t produced, size_t expected) {
45,785✔
241
   return test_is_eq(what, produced, expected);
45,785✔
242
}
243

244
bool Test::Result::test_eq(const std::string& what,
107✔
245
                           const Botan::OctetString& produced,
246
                           const Botan::OctetString& expected) {
247
   std::ostringstream out;
107✔
248
   out << m_who << " " << what;
107✔
249

250
   if(produced == expected) {
107✔
251
      out << " produced expected result " << produced.to_string();
214✔
252
      return test_success(out.str());
107✔
253
   } else {
254
      out << " produced unexpected result '" << produced.to_string() << "' expected '" << expected.to_string() << "'";
×
255
      return test_failure(out.str());
×
256
   }
257
}
107✔
258

259
bool Test::Result::test_lt(const std::string& what, size_t produced, size_t expected) {
5,613✔
260
   if(produced >= expected) {
5,613✔
261
      std::ostringstream err;
1✔
262
      err << m_who << " " << what;
1✔
263
      err << " unexpected result " << produced << " >= " << expected;
1✔
264
      return test_failure(err.str());
1✔
265
   }
1✔
266

267
   return test_success();
11,224✔
268
}
269

270
bool Test::Result::test_lte(const std::string& what, size_t produced, size_t expected) {
1,021,628✔
271
   if(produced > expected) {
1,021,628✔
272
      std::ostringstream err;
1✔
273
      err << m_who << " " << what << " unexpected result " << produced << " > " << expected;
1✔
274
      return test_failure(err.str());
1✔
275
   }
1✔
276

277
   return test_success();
2,043,254✔
278
}
279

280
bool Test::Result::test_gte(const std::string& what, size_t produced, size_t expected) {
1,138,701✔
281
   if(produced < expected) {
1,138,701✔
282
      std::ostringstream err;
1✔
283
      err << m_who;
1✔
284
      err << " " << what;
1✔
285
      err << " unexpected result " << produced << " < " << expected;
1✔
286
      return test_failure(err.str());
1✔
287
   }
1✔
288

289
   return test_success();
2,277,400✔
290
}
291

292
bool Test::Result::test_gt(const std::string& what, size_t produced, size_t expected) {
14,453✔
293
   if(produced <= expected) {
14,453✔
294
      std::ostringstream err;
×
295
      err << m_who;
×
296
      err << " " << what;
×
297
      err << " unexpected result " << produced << " <= " << expected;
×
298
      return test_failure(err.str());
×
299
   }
×
300

301
   return test_success();
28,906✔
302
}
303

304
bool Test::Result::test_ne(const std::string& what, const std::string& str1, const std::string& str2) {
25✔
305
   if(str1 != str2) {
25✔
306
      return test_success(str1 + " != " + str2);
48✔
307
   }
308

309
   return test_failure(who() + " " + what + " produced matching strings " + str1);
4✔
310
}
311

312
bool Test::Result::test_ne(const std::string& what, size_t produced, size_t expected) {
118✔
313
   if(produced != expected) {
118✔
314
      return test_success();
234✔
315
   }
316

317
   std::ostringstream err;
1✔
318
   err << who() << " " << what << " produced " << produced << " unexpected value";
1✔
319
   return test_failure(err.str());
1✔
320
}
1✔
321

322
#if defined(BOTAN_HAS_BIGINT)
323
bool Test::Result::test_eq(const std::string& what, const BigInt& produced, const BigInt& expected) {
11,083✔
324
   return test_is_eq(what, produced, expected);
11,083✔
325
}
326

327
bool Test::Result::test_ne(const std::string& what, const BigInt& produced, const BigInt& expected) {
97✔
328
   if(produced != expected) {
97✔
329
      return test_success();
192✔
330
   }
331

332
   std::ostringstream err;
1✔
333
   err << who() << " " << what << " produced " << produced << " prohibited value";
1✔
334
   return test_failure(err.str());
1✔
335
}
1✔
336
#endif
337

338
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
339
bool Test::Result::test_eq(const std::string& what, const Botan::EC_Point& a, const Botan::EC_Point& b) {
4,849✔
340
   //return test_is_eq(what, a, b);
341
   if(a == b) {
4,849✔
342
      return test_success();
9,698✔
343
   }
344

345
   std::ostringstream err;
×
346
   err << who() << " " << what << " a=(" << a.get_affine_x() << "," << a.get_affine_y() << ")"
×
347
       << " b=(" << b.get_affine_x() << "," << b.get_affine_y();
×
348
   return test_failure(err.str());
×
349
}
×
350
#endif
351

352
bool Test::Result::test_eq(const std::string& what, bool produced, bool expected) {
334,487✔
353
   return test_is_eq(what, produced, expected);
334,487✔
354
}
355

356
bool Test::Result::test_rc_init(const std::string& func, int rc) {
108✔
357
   if(rc == 0) {
108✔
358
      return test_success();
216✔
359
   } else {
360
      std::ostringstream msg;
×
361
      msg << m_who;
×
362
      msg << " " << func;
×
363

364
      // -40 is BOTAN_FFI_ERROR_NOT_IMPLEMENTED
365
      if(rc == -40) {
×
366
         msg << " returned not implemented";
×
367
      } else {
368
         msg << " unexpectedly failed with error code " << rc;
×
369
      }
370

371
      if(rc == -40) {
×
372
         this->test_note(msg.str());
×
373
      } else {
374
         this->test_failure(msg.str());
×
375
      }
376
      return false;
×
377
   }
×
378
}
379

380
bool Test::Result::test_rc(const std::string& func, int expected, int rc) {
335✔
381
   if(expected != rc) {
335✔
382
      std::ostringstream err;
1✔
383
      err << m_who;
1✔
384
      err << " call to " << func << " unexpectedly returned " << rc;
1✔
385
      err << " but expecting " << expected;
1✔
386
      return test_failure(err.str());
1✔
387
   }
1✔
388

389
   return test_success();
668✔
390
}
391

392
void Test::initialize(std::string test_name, CodeLocation location) {
394✔
393
   m_test_name = std::move(test_name);
394✔
394
   m_registration_location = std::move(location);
394✔
395
}
394✔
396

397
Botan::RandomNumberGenerator& Test::rng() const {
273,310✔
398
   if(!m_test_rng) {
273,310✔
399
      m_test_rng = Test::new_rng(m_test_name);
138✔
400
   }
401

402
   return *m_test_rng;
273,310✔
403
}
404

405
std::vector<std::string> Test::possible_providers(const std::string& /*unused*/) {
×
406
   return Test::provider_filter({"base"});
×
407
}
408

409
//static
410
std::string Test::format_time(uint64_t ns) {
1,339✔
411
   std::ostringstream o;
1,339✔
412

413
   if(ns > 1000000000) {
1,339✔
414
      o << std::setprecision(2) << std::fixed << ns / 1000000000.0 << " sec";
160✔
415
   } else {
416
      o << std::setprecision(2) << std::fixed << ns / 1000000.0 << " msec";
1,179✔
417
   }
418

419
   return o.str();
2,678✔
420
}
1,339✔
421

422
Test::Result::Result(std::string who, const std::vector<Result>& downstream_results) : Result(std::move(who)) {
14✔
423
   for(const auto& result : downstream_results) {
82✔
424
      merge(result, true /* ignore non-matching test names */);
68✔
425
   }
426
}
14✔
427

428
// TODO: this should move to `StdoutReporter`
429
std::string Test::Result::result_string() const {
2,374✔
430
   const bool verbose = Test::options().verbose();
2,374✔
431

432
   if(tests_run() == 0 && !verbose) {
2,374✔
433
      return "";
20✔
434
   }
435

436
   std::ostringstream report;
2,354✔
437

438
   report << who() << " ran ";
2,354✔
439

440
   if(tests_run() == 0) {
2,354✔
441
      report << "ZERO";
×
442
   } else {
443
      report << tests_run();
2,354✔
444
   }
445
   report << " tests";
2,354✔
446

447
   if(m_ns_taken > 0) {
2,354✔
448
      report << " in " << format_time(m_ns_taken);
2,676✔
449
   }
450

451
   if(tests_failed()) {
2,354✔
452
      report << " " << tests_failed() << " FAILED";
25✔
453
   } else {
454
      report << " all ok";
2,329✔
455
   }
456

457
   report << "\n";
2,354✔
458

459
   for(size_t i = 0; i != m_fail_log.size(); ++i) {
2,379✔
460
      report << "Failure " << (i + 1) << ": " << m_fail_log[i];
25✔
461
      if(m_where) {
25✔
462
         report << " (at " << m_where->path << ":" << m_where->line << ")";
×
463
      }
464
      report << "\n";
25✔
465
   }
466

467
   if(!m_fail_log.empty() || tests_run() == 0 || verbose) {
2,354✔
468
      for(size_t i = 0; i != m_log.size(); ++i) {
25✔
469
         report << "Note " << (i + 1) << ": " << m_log[i] << "\n";
×
470
      }
471
   }
472

473
   return report.str();
2,354✔
474
}
2,354✔
475

476
namespace {
477

478
class Test_Registry {
479
   public:
480
      static Test_Registry& instance() {
1,199✔
481
         static Test_Registry registry;
1,200✔
482
         return registry;
1,199✔
483
      }
484

485
      void register_test(const std::string& category,
394✔
486
                         const std::string& name,
487
                         bool smoke_test,
488
                         bool needs_serialization,
489
                         std::function<std::unique_ptr<Test>()> maker_fn) {
490
         if(m_tests.contains(name)) {
394✔
491
            throw Test_Error("Duplicate registration of test '" + name + "'");
×
492
         }
493

494
         if(m_tests.contains(category)) {
394✔
495
            throw Test_Error("'" + category + "' cannot be used as category, test exists");
×
496
         }
497

498
         if(m_categories.contains(name)) {
394✔
499
            throw Test_Error("'" + name + "' cannot be used as test name, category exists");
×
500
         }
501

502
         if(smoke_test) {
394✔
503
            m_smoke_tests.push_back(name);
10✔
504
         }
505

506
         if(needs_serialization) {
394✔
507
            m_mutexed_tests.push_back(name);
21✔
508
         }
509

510
         m_tests.emplace(name, std::move(maker_fn));
394✔
511
         m_categories.emplace(category, name);
394✔
512
      }
394✔
513

514
      std::unique_ptr<Test> get_test(const std::string& test_name) const {
394✔
515
         auto i = m_tests.find(test_name);
394✔
516
         if(i != m_tests.end()) {
394✔
517
            return i->second();
394✔
518
         }
519
         return nullptr;
×
520
      }
521

522
      std::set<std::string> registered_tests() const {
×
523
         std::set<std::string> s;
×
524
         for(auto&& i : m_tests) {
×
525
            s.insert(i.first);
×
526
         }
527
         return s;
×
528
      }
×
529

530
      std::set<std::string> registered_test_categories() const {
×
531
         std::set<std::string> s;
×
532
         for(auto&& i : m_categories) {
×
533
            s.insert(i.first);
×
534
         }
535
         return s;
×
536
      }
×
537

538
      std::vector<std::string> filter_registered_tests(const std::vector<std::string>& requested,
1✔
539
                                                       const std::set<std::string>& to_be_skipped) {
540
         std::vector<std::string> result;
1✔
541

542
         // TODO: this is O(n^2), but we have a relatively small number of tests.
543
         auto insert_if_not_exists_and_not_skipped = [&](const std::string& test_name) {
395✔
544
            if(!Botan::value_exists(result, test_name) && to_be_skipped.find(test_name) == to_be_skipped.end()) {
394✔
545
               result.push_back(test_name);
384✔
546
            }
547
         };
395✔
548

549
         if(requested.empty()) {
1✔
550
            /*
551
            If nothing was requested on the command line, run everything. First
552
            run the "essentials" to smoke test, then everything else in
553
            alphabetical order.
554
            */
555
            result = m_smoke_tests;
1✔
556
            for(const auto& [test_name, _] : m_tests) {
395✔
557
               insert_if_not_exists_and_not_skipped(test_name);
394✔
558
            }
559
         } else {
560
            for(const auto& r : requested) {
×
561
               if(m_tests.find(r) != m_tests.end()) {
×
562
                  insert_if_not_exists_and_not_skipped(r);
×
563
               } else if(auto elems = m_categories.equal_range(r); elems.first != m_categories.end()) {
×
564
                  for(; elems.first != elems.second; ++elems.first) {
×
565
                     insert_if_not_exists_and_not_skipped(elems.first->second);
×
566
                  }
567
               } else {
568
                  throw Test_Error("Unknown test suite or category: " + r);
×
569
               }
570
            }
571
         }
572

573
         return result;
1✔
574
      }
×
575

576
      bool needs_serialization(const std::string& test_name) const {
410✔
577
         return Botan::value_exists(m_mutexed_tests, test_name);
16✔
578
      }
579

580
   private:
581
      Test_Registry() = default;
1✔
582

583
   private:
584
      std::map<std::string, std::function<std::unique_ptr<Test>()>> m_tests;
585
      std::multimap<std::string, std::string> m_categories;
586
      std::vector<std::string> m_smoke_tests;
587
      std::vector<std::string> m_mutexed_tests;
588
};
589

590
}  // namespace
591

592
// static Test:: functions
593

594
//static
595
void Test::register_test(const std::string& category,
394✔
596
                         const std::string& name,
597
                         bool smoke_test,
598
                         bool needs_serialization,
599
                         std::function<std::unique_ptr<Test>()> maker_fn) {
600
   Test_Registry::instance().register_test(category, name, smoke_test, needs_serialization, std::move(maker_fn));
788✔
601
}
394✔
602

603
//static
604
uint64_t Test::timestamp() {
97,111✔
605
   auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
97,111✔
606
   return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
1,967✔
607
}
608

609
//static
610
std::vector<Test::Result> Test::flatten_result_lists(std::vector<std::vector<Test::Result>> result_lists) {
4✔
611
   std::vector<Test::Result> results;
4✔
612
   for(auto& result_list : result_lists) {
26✔
613
      for(auto& result : result_list) {
71✔
614
         results.emplace_back(std::move(result));
49✔
615
      }
616
   }
617
   return results;
4✔
618
}
×
619

620
//static
621
std::set<std::string> Test::registered_tests() {
×
622
   return Test_Registry::instance().registered_tests();
×
623
}
624

625
//static
626
std::set<std::string> Test::registered_test_categories() {
×
627
   return Test_Registry::instance().registered_test_categories();
×
628
}
629

630
//static
631
std::unique_ptr<Test> Test::get_test(const std::string& test_name) {
394✔
632
   return Test_Registry::instance().get_test(test_name);
394✔
633
}
634

635
//static
636
bool Test::test_needs_serialization(const std::string& test_name) {
394✔
637
   return Test_Registry::instance().needs_serialization(test_name);
394✔
638
}
639

640
//static
641
std::vector<std::string> Test::filter_registered_tests(const std::vector<std::string>& requested,
1✔
642
                                                       const std::set<std::string>& to_be_skipped) {
643
   return Test_Registry::instance().filter_registered_tests(requested, to_be_skipped);
1✔
644
}
645

646
//static
647
std::string Test::temp_file_name(const std::string& basename) {
21✔
648
   // TODO add a --tmp-dir option to the tests to specify where these files go
649

650
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
651

652
   // POSIX only calls for 6 'X' chars but OpenBSD allows arbitrary amount
653
   std::string mkstemp_basename = "/tmp/" + basename + ".XXXXXXXXXX";
42✔
654

655
   int fd = ::mkstemp(&mkstemp_basename[0]);
21✔
656

657
   // error
658
   if(fd < 0) {
21✔
659
      return "";
×
660
   }
661

662
   ::close(fd);
21✔
663

664
   return mkstemp_basename;
21✔
665
#else
666
   // For now just create the temp in the current working directory
667
   return basename;
668
#endif
669
}
21✔
670

671
bool Test::copy_file(const std::string& from, const std::string& to) {
1✔
672
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) && defined(__cpp_lib_filesystem)
673
   std::error_code ec;  // don't throw, just return false on error
1✔
674
   return std::filesystem::copy_file(from, to, std::filesystem::copy_options::overwrite_existing, ec);
1✔
675
#else
676
   // TODO: implement fallbacks to POSIX or WIN32
677
   // ... but then again: it's 2023 and we're using C++20 :o)
678
   BOTAN_UNUSED(from, to);
679
   throw Botan::No_Filesystem_Access();
680
#endif
681
}
682

683
std::string Test::read_data_file(const std::string& path) {
37✔
684
   const std::string fsname = Test::data_file(path);
37✔
685
   std::ifstream file(fsname.c_str());
37✔
686
   if(!file.good()) {
37✔
687
      throw Test_Error("Error reading from " + fsname);
×
688
   }
689

690
   return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
74✔
691
}
37✔
692

693
std::vector<uint8_t> Test::read_binary_data_file(const std::string& path) {
34✔
694
   const std::string fsname = Test::data_file(path);
34✔
695
   std::ifstream file(fsname.c_str(), std::ios::binary);
34✔
696
   if(!file.good()) {
34✔
697
      throw Test_Error("Error reading from " + fsname);
×
698
   }
699

700
   std::vector<uint8_t> contents;
34✔
701

702
   while(file.good()) {
74✔
703
      std::vector<uint8_t> buf(4096);
40✔
704
      file.read(reinterpret_cast<char*>(buf.data()), buf.size());
40✔
705
      const size_t got = static_cast<size_t>(file.gcount());
40✔
706

707
      if(got == 0 && file.eof()) {
40✔
708
         break;
709
      }
710

711
      contents.insert(contents.end(), buf.data(), buf.data() + got);
40✔
712
   }
40✔
713

714
   return contents;
68✔
715
}
34✔
716

717
// static member variables of Test
718

719
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
720
Test_Options Test::m_opts;
721
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
722
std::string Test::m_test_rng_seed;
723

724
//static
725
void Test::set_test_options(const Test_Options& opts) {
1✔
726
   m_opts = opts;
1✔
727
}
1✔
728

729
namespace {
730

731
/*
732
* This is a fast, simple, deterministic PRNG that's used for running
733
* the tests. It is not intended to be cryptographically secure.
734
*/
735
class Testsuite_RNG final : public Botan::RandomNumberGenerator {
8✔
736
   public:
737
      std::string name() const override { return "Testsuite_RNG"; }
×
738

739
      void clear() override { m_x = 0; }
×
740

741
      bool accepts_input() const override { return true; }
×
742

743
      bool is_seeded() const override { return true; }
362,415✔
744

745
      void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
10,026,145✔
746
         for(const auto byte : input) {
10,026,145✔
747
            mix(byte);
×
748
         }
749

750
         for(auto& byte : output) {
39,536,020✔
751
            byte = mix();
29,509,875✔
752
         }
753
      }
10,026,145✔
754

755
      Testsuite_RNG(std::string_view seed, std::string_view test_name) {
249✔
756
         m_x = 0;
249✔
757

758
         for(char c : seed) {
7,470✔
759
            this->mix(static_cast<uint8_t>(c));
7,221✔
760
         }
761
         for(char c : test_name) {
4,453✔
762
            this->mix(static_cast<uint8_t>(c));
4,204✔
763
         }
764
      }
249✔
765

766
   private:
767
      uint8_t mix(uint8_t input = 0) {
29,521,300✔
768
         m_x ^= input;
29,521,300✔
769
         m_x *= 0xF2E16957;
29,521,300✔
770
         m_x += 0xE50B590F;
29,521,300✔
771
         return static_cast<uint8_t>(m_x >> 27);
29,521,300✔
772
      }
773

774
      uint64_t m_x;
775
};
776

777
}  // namespace
778

779
//static
780
void Test::set_test_rng_seed(std::span<const uint8_t> seed, size_t epoch) {
1✔
781
   m_test_rng_seed = Botan::fmt("seed={} epoch={}", Botan::hex_encode(seed), epoch);
1✔
782
}
1✔
783

784
//static
785
std::unique_ptr<Botan::RandomNumberGenerator> Test::new_rng(std::string_view test_name) {
241✔
786
   return std::make_unique<Testsuite_RNG>(m_test_rng_seed, test_name);
241✔
787
}
788

789
//static
790
std::shared_ptr<Botan::RandomNumberGenerator> Test::new_shared_rng(std::string_view test_name) {
8✔
791
   return std::make_shared<Testsuite_RNG>(m_test_rng_seed, test_name);
8✔
792
}
793

794
//static
795
std::string Test::data_file(const std::string& file) {
618✔
796
   return options().data_dir() + "/" + file;
1,236✔
797
}
798

799
//static
800
std::string Test::data_dir(const std::string& subdir) {
1✔
801
   return options().data_dir() + "/" + subdir;
2✔
802
}
803

804
//static
805
std::vector<std::string> Test::files_in_data_dir(const std::string& subdir) {
263✔
806
   auto fs = Botan::get_files_recursive(options().data_dir() + "/" + subdir);
789✔
807
   if(fs.empty()) {
263✔
808
      throw Test_Error("Test::files_in_data_dir encountered empty subdir " + subdir);
×
809
   }
810
   return fs;
263✔
811
}
×
812

813
//static
814
std::string Test::data_file_as_temporary_copy(const std::string& what) {
1✔
815
   auto tmp_basename = what;
1✔
816
   std::replace(tmp_basename.begin(), tmp_basename.end(), '/', '_');
1✔
817
   auto temp_file = temp_file_name("tmp-" + tmp_basename);
1✔
818
   if(temp_file.empty()) {
1✔
819
      return "";
×
820
   }
821
   if(!Test::copy_file(data_file(what), temp_file)) {
1✔
822
      return "";
×
823
   }
824
   return temp_file;
1✔
825
}
1✔
826

827
//static
828
std::vector<std::string> Test::provider_filter(const std::vector<std::string>& in) {
46,565✔
829
   if(m_opts.provider().empty()) {
46,565✔
830
      return in;
46,565✔
831
   }
832
   for(auto&& provider : in) {
×
833
      if(provider == m_opts.provider()) {
×
834
         return std::vector<std::string>{provider};
×
835
      }
836
   }
837
   return std::vector<std::string>{};
×
838
}
×
839

840
std::string Test::random_password(Botan::RandomNumberGenerator& rng) {
222✔
841
   const size_t len = 1 + rng.next_byte() % 32;
222✔
842
   return Botan::hex_encode(rng.random_vec(len));
444✔
843
}
844

845
std::vector<std::vector<uint8_t>> VarMap::get_req_bin_list(const std::string& key) const {
12✔
846
   auto i = m_vars.find(key);
12✔
847
   if(i == m_vars.end()) {
12✔
848
      throw Test_Error("Test missing variable " + key);
×
849
   }
850

851
   std::vector<std::vector<uint8_t>> bin_list;
12✔
852

853
   for(auto&& part : Botan::split_on(i->second, ',')) {
62✔
854
      try {
50✔
855
         bin_list.push_back(Botan::hex_decode(part));
100✔
856
      } catch(std::exception& e) {
×
857
         std::ostringstream oss;
×
858
         oss << "Bad input '" << part << "'"
×
859
             << " in binary list key " << key << " - " << e.what();
×
860
         throw Test_Error(oss.str());
×
861
      }
×
862
   }
12✔
863

864
   return bin_list;
12✔
865
}
×
866

867
std::vector<uint8_t> VarMap::get_req_bin(const std::string& key) const {
150,684✔
868
   auto i = m_vars.find(key);
150,684✔
869
   if(i == m_vars.end()) {
150,684✔
870
      throw Test_Error("Test missing variable " + key);
×
871
   }
872

873
   try {
150,684✔
874
      if(i->second.starts_with("0x")) {
150,684✔
875
         if(i->second.size() % 2 == 0) {
×
876
            return Botan::hex_decode(i->second.substr(2));
×
877
         } else {
878
            std::string z = i->second;
×
879
            std::swap(z[0], z[1]);  // swap 0x to x0 then remove x
×
880
            return Botan::hex_decode(z.substr(1));
×
881
         }
×
882
      } else {
883
         return Botan::hex_decode(i->second);
150,684✔
884
      }
885
   } catch(std::exception& e) {
×
886
      std::ostringstream oss;
×
887
      oss << "Bad input '" << i->second << "'"
×
888
          << " for key " << key << " - " << e.what();
×
889
      throw Test_Error(oss.str());
×
890
   }
×
891
}
×
892

893
std::string VarMap::get_opt_str(const std::string& key, const std::string& def_value) const {
14,364✔
894
   auto i = m_vars.find(key);
14,364✔
895
   if(i == m_vars.end()) {
14,364✔
896
      return def_value;
14,310✔
897
   }
898
   return i->second;
54✔
899
}
900

901
bool VarMap::get_req_bool(const std::string& key) const {
19✔
902
   auto i = m_vars.find(key);
19✔
903
   if(i == m_vars.end()) {
19✔
904
      throw Test_Error("Test missing variable " + key);
×
905
   }
906

907
   if(i->second == "true") {
19✔
908
      return true;
909
   } else if(i->second == "false") {
11✔
910
      return false;
911
   } else {
912
      throw Test_Error("Invalid boolean for key '" + key + "' value '" + i->second + "'");
×
913
   }
914
}
915

916
size_t VarMap::get_req_sz(const std::string& key) const {
4,513✔
917
   auto i = m_vars.find(key);
4,513✔
918
   if(i == m_vars.end()) {
4,513✔
919
      throw Test_Error("Test missing variable " + key);
×
920
   }
921
   return Botan::to_u32bit(i->second);
4,513✔
922
}
923

924
uint8_t VarMap::get_req_u8(const std::string& key) const {
17✔
925
   const size_t s = this->get_req_sz(key);
17✔
926
   if(s > 256) {
17✔
927
      throw Test_Error("Invalid " + key + " expected uint8_t got " + std::to_string(s));
×
928
   }
929
   return static_cast<uint8_t>(s);
17✔
930
}
931

932
uint32_t VarMap::get_req_u32(const std::string& key) const {
14✔
933
   return static_cast<uint32_t>(get_req_sz(key));
14✔
934
}
935

936
uint64_t VarMap::get_req_u64(const std::string& key) const {
17✔
937
   auto i = m_vars.find(key);
17✔
938
   if(i == m_vars.end()) {
17✔
939
      throw Test_Error("Test missing variable " + key);
×
940
   }
941
   try {
17✔
942
      return std::stoull(i->second);
17✔
943
   } catch(std::exception&) {
×
944
      throw Test_Error("Invalid u64 value '" + i->second + "'");
×
945
   }
×
946
}
947

948
size_t VarMap::get_opt_sz(const std::string& key, const size_t def_value) const {
30,331✔
949
   auto i = m_vars.find(key);
30,331✔
950
   if(i == m_vars.end()) {
30,331✔
951
      return def_value;
952
   }
953
   return Botan::to_u32bit(i->second);
12,244✔
954
}
955

956
uint64_t VarMap::get_opt_u64(const std::string& key, const uint64_t def_value) const {
3,538✔
957
   auto i = m_vars.find(key);
3,538✔
958
   if(i == m_vars.end()) {
3,538✔
959
      return def_value;
960
   }
961
   try {
641✔
962
      return std::stoull(i->second);
3,538✔
963
   } catch(std::exception&) {
×
964
      throw Test_Error("Invalid u64 value '" + i->second + "'");
×
965
   }
×
966
}
967

968
std::vector<uint8_t> VarMap::get_opt_bin(const std::string& key) const {
39,637✔
969
   auto i = m_vars.find(key);
39,637✔
970
   if(i == m_vars.end()) {
39,637✔
971
      return std::vector<uint8_t>();
28,323✔
972
   }
973

974
   try {
11,314✔
975
      return Botan::hex_decode(i->second);
11,314✔
976
   } catch(std::exception&) {
×
977
      throw Test_Error("Test invalid hex input '" + i->second + "'" + +" for key " + key);
×
978
   }
×
979
}
980

981
std::string VarMap::get_req_str(const std::string& key) const {
40,180✔
982
   auto i = m_vars.find(key);
40,180✔
983
   if(i == m_vars.end()) {
40,180✔
984
      throw Test_Error("Test missing variable " + key);
×
985
   }
986
   return i->second;
40,180✔
987
}
988

989
#if defined(BOTAN_HAS_BIGINT)
990
Botan::BigInt VarMap::get_req_bn(const std::string& key) const {
38,441✔
991
   auto i = m_vars.find(key);
38,441✔
992
   if(i == m_vars.end()) {
38,441✔
993
      throw Test_Error("Test missing variable " + key);
×
994
   }
995

996
   try {
38,441✔
997
      return Botan::BigInt(i->second);
38,441✔
998
   } catch(std::exception&) {
×
999
      throw Test_Error("Test invalid bigint input '" + i->second + "' for key " + key);
×
1000
   }
×
1001
}
1002

1003
Botan::BigInt VarMap::get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const
80✔
1004

1005
{
1006
   auto i = m_vars.find(key);
80✔
1007
   if(i == m_vars.end()) {
80✔
1008
      return def_value;
24✔
1009
   }
1010

1011
   try {
56✔
1012
      return Botan::BigInt(i->second);
56✔
1013
   } catch(std::exception&) {
×
1014
      throw Test_Error("Test invalid bigint input '" + i->second + "' for key " + key);
×
1015
   }
×
1016
}
1017
#endif
1018

1019
Text_Based_Test::Text_Based_Test(const std::string& data_src,
175✔
1020
                                 const std::string& required_keys_str,
1021
                                 const std::string& optional_keys_str) :
175✔
1022
      m_data_src(data_src) {
525✔
1023
   if(required_keys_str.empty()) {
175✔
1024
      throw Test_Error("Invalid test spec");
×
1025
   }
1026

1027
   std::vector<std::string> required_keys = Botan::split_on(required_keys_str, ',');
175✔
1028
   std::vector<std::string> optional_keys = Botan::split_on(optional_keys_str, ',');
175✔
1029

1030
   m_required_keys.insert(required_keys.begin(), required_keys.end());
175✔
1031
   m_optional_keys.insert(optional_keys.begin(), optional_keys.end());
175✔
1032
   m_output_key = required_keys.at(required_keys.size() - 1);
175✔
1033
}
175✔
1034

1035
std::string Text_Based_Test::get_next_line() {
154,280✔
1036
   while(true) {
154,531✔
1037
      if(m_cur == nullptr || m_cur->good() == false) {
154,531✔
1038
         if(m_srcs.empty()) {
435✔
1039
            if(m_first) {
350✔
1040
               if(m_data_src.ends_with(".vec")) {
175✔
1041
                  m_srcs.push_back(Test::data_file(m_data_src));
326✔
1042
               } else {
1043
                  const auto fs = Test::files_in_data_dir(m_data_src);
12✔
1044
                  m_srcs.assign(fs.begin(), fs.end());
12✔
1045
                  if(m_srcs.empty()) {
12✔
1046
                     throw Test_Error("Error reading test data dir " + m_data_src);
×
1047
                  }
1048
               }
12✔
1049

1050
               m_first = false;
175✔
1051
            } else {
1052
               return "";  // done
175✔
1053
            }
1054
         }
1055

1056
         m_cur = std::make_unique<std::ifstream>(m_srcs[0]);
345✔
1057
         m_cur_src_name = m_srcs[0];
260✔
1058

1059
         // Reinit cpuid on new file if needed
1060
         if(m_cpu_flags.empty() == false) {
260✔
1061
            m_cpu_flags.clear();
13✔
1062
            Botan::CPUID::initialize();
13✔
1063
         }
1064

1065
         if(!m_cur->good()) {
260✔
1066
            throw Test_Error("Could not open input file '" + m_cur_src_name);
×
1067
         }
1068

1069
         m_srcs.pop_front();
260✔
1070
      }
1071

1072
      while(m_cur->good()) {
223,927✔
1073
         std::string line;
223,676✔
1074
         std::getline(*m_cur, line);
223,676✔
1075

1076
         if(line.empty()) {
223,676✔
1077
            continue;
54,645✔
1078
         }
1079

1080
         if(line[0] == '#') {
169,031✔
1081
            if(line.compare(0, 6, "#test ") == 0) {
14,942✔
1082
               return line;
16✔
1083
            } else {
1084
               continue;
14,926✔
1085
            }
1086
         }
1087

1088
         return line;
154,089✔
1089
      }
223,676✔
1090
   }
1091
}
1092

1093
namespace {
1094

1095
// strips leading and trailing but not internal whitespace
1096
std::string strip_ws(const std::string& in) {
306,762✔
1097
   const char* whitespace = " ";
306,762✔
1098

1099
   const auto first_c = in.find_first_not_of(whitespace);
306,762✔
1100
   if(first_c == std::string::npos) {
306,762✔
1101
      return "";
1,001✔
1102
   }
1103

1104
   const auto last_c = in.find_last_not_of(whitespace);
305,761✔
1105

1106
   return in.substr(first_c, last_c - first_c + 1);
305,761✔
1107
}
1108

1109
std::vector<uint64_t> parse_cpuid_bits(const std::vector<std::string>& tok) {
16✔
1110
   std::vector<uint64_t> bits;
16✔
1111
   for(size_t i = 1; i < tok.size(); ++i) {
58✔
1112
      const std::vector<Botan::CPUID::CPUID_bits> more = Botan::CPUID::bit_from_string(tok[i]);
42✔
1113
      bits.insert(bits.end(), more.begin(), more.end());
42✔
1114
   }
42✔
1115

1116
   return bits;
16✔
1117
}
×
1118

1119
}  // namespace
1120

1121
bool Text_Based_Test::skip_this_test(const std::string& /*header*/, const VarMap& /*vars*/) {
46,720✔
1122
   return false;
46,720✔
1123
}
1124

1125
std::vector<Test::Result> Text_Based_Test::run() {
175✔
1126
   std::vector<Test::Result> results;
175✔
1127

1128
   std::string header, header_or_name = m_data_src;
175✔
1129
   VarMap vars;
175✔
1130
   size_t test_cnt = 0;
175✔
1131

1132
   while(true) {
154,280✔
1133
      const std::string line = get_next_line();
154,280✔
1134
      if(line.empty())  // EOF
154,280✔
1135
      {
1136
         break;
1137
      }
1138

1139
      if(line.compare(0, 6, "#test ") == 0) {
154,105✔
1140
         std::vector<std::string> pragma_tokens = Botan::split_on(line.substr(6), ' ');
16✔
1141

1142
         if(pragma_tokens.empty()) {
16✔
1143
            throw Test_Error("Empty pragma found in " + m_cur_src_name);
×
1144
         }
1145

1146
         if(pragma_tokens[0] != "cpuid") {
16✔
1147
            throw Test_Error("Unknown test pragma '" + line + "' in " + m_cur_src_name);
×
1148
         }
1149

1150
         if(!Test_Registry::instance().needs_serialization(this->test_name())) {
16✔
1151
            throw Test_Error(Botan::fmt("'{}' used cpuid control but is not serialized", this->test_name()));
×
1152
         }
1153

1154
         m_cpu_flags = parse_cpuid_bits(pragma_tokens);
16✔
1155

1156
         continue;
16✔
1157
      } else if(line[0] == '#') {
154,105✔
1158
         throw Test_Error("Unknown test pragma '" + line + "' in " + m_cur_src_name);
×
1159
      }
1160

1161
      if(line[0] == '[' && line[line.size() - 1] == ']') {
154,089✔
1162
         header = line.substr(1, line.size() - 2);
708✔
1163
         header_or_name = header;
708✔
1164
         test_cnt = 0;
708✔
1165
         vars.clear();
708✔
1166
         continue;
708✔
1167
      }
1168

1169
      const std::string test_id = "test " + std::to_string(test_cnt);
306,762✔
1170

1171
      auto equal_i = line.find_first_of('=');
153,381✔
1172

1173
      if(equal_i == std::string::npos) {
153,381✔
1174
         results.push_back(Test::Result::Failure(header_or_name, "invalid input '" + line + "'"));
×
1175
         continue;
×
1176
      }
1177

1178
      std::string key = strip_ws(std::string(line.begin(), line.begin() + equal_i - 1));
306,762✔
1179
      std::string val = strip_ws(std::string(line.begin() + equal_i + 1, line.end()));
306,762✔
1180

1181
      if(!m_required_keys.contains(key) && !m_optional_keys.contains(key)) {
153,381✔
1182
         auto r = Test::Result::Failure(header_or_name, Botan::fmt("{} failed unknown key {}", test_id, key));
×
1183
         results.push_back(r);
×
1184
      }
×
1185

1186
      vars.add(key, val);
153,381✔
1187

1188
      if(key == m_output_key) {
153,381✔
1189
         try {
47,707✔
1190
            for(auto& req_key : m_required_keys) {
242,982✔
1191
               if(!vars.has_key(req_key)) {
390,550✔
1192
                  auto r =
×
1193
                     Test::Result::Failure(header_or_name, Botan::fmt("{} missing required key {}", test_id, req_key));
×
1194
                  results.push_back(r);
×
1195
               }
×
1196
            }
1197

1198
            if(skip_this_test(header, vars)) {
47,707✔
1199
               continue;
138✔
1200
            }
1201

1202
            ++test_cnt;
47,569✔
1203

1204
            uint64_t start = Test::timestamp();
47,569✔
1205

1206
            Test::Result result = run_one_test(header, vars);
47,569✔
1207
            if(!m_cpu_flags.empty()) {
47,569✔
1208
               for(const auto& cpuid_u64 : m_cpu_flags) {
19,298✔
1209
                  Botan::CPUID::CPUID_bits cpuid_bit = static_cast<Botan::CPUID::CPUID_bits>(cpuid_u64);
13,687✔
1210
                  if(Botan::CPUID::has_cpuid_bit(cpuid_bit)) {
13,687✔
1211
                     Botan::CPUID::clear_cpuid_bit(cpuid_bit);
12,512✔
1212
                     // now re-run the test
1213
                     result.merge(run_one_test(header, vars));
12,512✔
1214
                  }
1215
               }
1216
               Botan::CPUID::initialize();
5,611✔
1217
            }
1218
            result.set_ns_consumed(Test::timestamp() - start);
47,569✔
1219

1220
            if(result.tests_failed()) {
47,569✔
1221
               std::ostringstream oss;
×
1222
               oss << "Test # " << test_cnt << " ";
×
1223
               if(!header.empty()) {
×
1224
                  oss << header << " ";
×
1225
               }
1226
               oss << "failed ";
×
1227

1228
               for(const auto& k : m_required_keys) {
×
1229
                  oss << k << "=" << vars.get_req_str(k) << " ";
×
1230
               }
1231

1232
               result.test_note(oss.str());
×
1233
            }
×
1234
            results.push_back(result);
47,569✔
1235
         } catch(std::exception& e) {
47,569✔
1236
            std::ostringstream oss;
×
1237
            oss << "Test # " << test_cnt << " ";
×
1238
            if(!header.empty()) {
×
1239
               oss << header << " ";
×
1240
            }
1241

1242
            for(const auto& k : m_required_keys) {
×
1243
               oss << k << "=" << vars.get_req_str(k) << " ";
×
1244
            }
1245

1246
            oss << "failed with exception '" << e.what() << "'";
×
1247

1248
            results.push_back(Test::Result::Failure(header_or_name, oss.str()));
×
1249
         }
×
1250

1251
         if(clear_between_callbacks()) {
47,569✔
1252
            vars.clear();
32,836✔
1253
         }
1254
      }
1255
   }
154,381✔
1256

1257
   if(results.empty()) {
175✔
1258
      return results;
1259
   }
1260

1261
   try {
175✔
1262
      std::vector<Test::Result> final_tests = run_final_tests();
175✔
1263
      results.insert(results.end(), final_tests.begin(), final_tests.end());
175✔
1264
   } catch(std::exception& e) {
175✔
1265
      results.push_back(Test::Result::Failure(header_or_name, "run_final_tests exception " + std::string(e.what())));
×
1266
   }
×
1267

1268
   m_first = true;
175✔
1269

1270
   return results;
175✔
1271
}
175✔
1272

1273
std::map<std::string, std::string> Test_Options::report_properties() const {
1✔
1274
   std::map<std::string, std::string> result;
1✔
1275

1276
   for(const auto& prop : m_report_properties) {
3✔
1277
      const auto colon = prop.find(':');
2✔
1278
      // props without a colon separator or without a name are not allowed
1279
      if(colon == std::string::npos || colon == 0) {
2✔
1280
         throw Test_Error("--report-properties should be of the form <key>:<value>,<key>:<value>,...");
×
1281
      }
1282

1283
      result.insert_or_assign(prop.substr(0, colon), prop.substr(colon + 1, prop.size() - colon - 1));
4✔
1284
   }
1285

1286
   return result;
1✔
1287
}
×
1288

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