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

randombit / botan / 16055899095

03 Jul 2025 04:34PM UTC coverage: 90.571% (-0.003%) from 90.574%
16055899095

push

github

web-flow
Merge pull request #4931 from randombit/jack/moar-tidy

Address various warnings from clang-tidy

99050 of 109362 relevant lines covered (90.57%)

12478386.65 hits per line

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

77.39
/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/filesystem.h>
11
#include <botan/internal/fmt.h>
12
#include <botan/internal/loadstor.h>
13
#include <botan/internal/parsing.h>
14
#include <botan/internal/stl_util.h>
15
#include <botan/internal/target_info.h>
16
#include <fstream>
17
#include <iomanip>
18
#include <sstream>
19

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

24
#if defined(BOTAN_HAS_CPUID)
25
   #include <botan/internal/cpuid.h>
26
#endif
27

28
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
29
   #include <botan/ec_point.h>
30
#endif
31

32
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
33
   #include <stdlib.h>
34
   #include <unistd.h>
35
#endif
36

37
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
38
   #include <version>
39
   #if defined(__cpp_lib_filesystem)
40
      #include <filesystem>
41
   #endif
42
#endif
43

44
namespace Botan_Tests {
45

46
void Test::Result::merge(const Result& other, bool ignore_test_name) {
118,420✔
47
   if(who() != other.who()) {
118,420✔
48
      if(!ignore_test_name) {
73✔
49
         throw Test_Error("Merging tests from different sources");
×
50
      }
51

52
      // When deliberately merging results with different names, the code location is
53
      // likely inconsistent and must be discarded.
54
      m_where.reset();
73✔
55
   } else {
56
      m_where = other.m_where;
118,347✔
57
   }
58

59
   m_timestamp = std::min(m_timestamp, other.m_timestamp);
118,420✔
60
   m_ns_taken += other.m_ns_taken;
118,420✔
61
   m_tests_passed += other.m_tests_passed;
118,420✔
62
   m_fail_log.insert(m_fail_log.end(), other.m_fail_log.begin(), other.m_fail_log.end());
118,420✔
63
   m_log.insert(m_log.end(), other.m_log.begin(), other.m_log.end());
118,420✔
64
}
118,420✔
65

66
void Test::Result::start_timer() {
1,004✔
67
   if(m_started == 0) {
1,004✔
68
      m_started = Test::timestamp();
2,008✔
69
   }
70
}
1,004✔
71

72
void Test::Result::end_timer() {
1,116✔
73
   if(m_started > 0) {
1,116✔
74
      m_ns_taken += Test::timestamp() - m_started;
2,006✔
75
      m_started = 0;
1,003✔
76
   }
77
}
1,116✔
78

79
void Test::Result::test_note(const std::string& note, const char* extra) {
2,760✔
80
   if(!note.empty()) {
2,760✔
81
      std::ostringstream out;
2,760✔
82
      out << who() << " " << note;
2,760✔
83
      if(extra) {
2,760✔
84
         out << ": " << extra;
12✔
85
      }
86
      m_log.push_back(out.str());
2,760✔
87
   }
2,760✔
88
}
2,760✔
89

90
void Test::Result::note_missing(const std::string& whatever) {
275✔
91
   static std::set<std::string> s_already_seen;
275✔
92

93
   if(!s_already_seen.contains(whatever)) {
275✔
94
      test_note("Skipping tests due to missing " + whatever);
5✔
95
      s_already_seen.insert(whatever);
5✔
96
   }
97
}
275✔
98

99
bool Test::Result::ThrowExpectations::check(const std::string& test_name, Test::Result& result) {
47,999✔
100
   m_consumed = true;
47,999✔
101

102
   try {
47,999✔
103
      m_fn();
47,999✔
104
      if(!m_expect_success) {
1,645✔
105
         return result.test_failure(test_name + " failed to throw expected exception");
2✔
106
      }
107
   } catch(const std::exception& ex) {
46,354✔
108
      if(m_expect_success) {
46,352✔
109
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
2✔
110
      }
111
      if(m_expected_exception_type.has_value() && m_expected_exception_type.value() != typeid(ex)) {
46,351✔
112
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
4✔
113
      }
114
      if(m_expected_message.has_value() && m_expected_message.value() != ex.what()) {
46,349✔
115
         return result.test_failure(test_name + " threw exception with unexpected message (expected: '" +
3✔
116
                                    m_expected_message.value() + "', got: '" + ex.what() + "')");
6✔
117
      }
118
   } catch(...) {
46,354✔
119
      if(m_expect_success || m_expected_exception_type.has_value() || m_expected_message.has_value()) {
2✔
120
         return result.test_failure(test_name + " threw unexpected unknown exception");
1✔
121
      }
122
   }
2✔
123

124
   return result.test_success(test_name + " behaved as expected");
95,984✔
125
}
126

127
bool Test::Result::test_throws(const std::string& what, const std::function<void()>& fn) {
45,911✔
128
   return ThrowExpectations(fn).check(what, *this);
91,822✔
129
}
130

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

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

139
bool Test::Result::test_success(const std::string& note) {
3,441,545✔
140
   if(Test::options().log_success()) {
3,441,414✔
141
      test_note(note);
×
142
   }
143
   ++m_tests_passed;
3,441,545✔
144
   return true;
650,429✔
145
}
146

147
bool Test::Result::test_failure(const std::string& what, const std::string& error) {
1✔
148
   return test_failure(who() + " " + what + " with error " + error);
4✔
149
}
150

151
void Test::Result::test_failure(const std::string& what, const uint8_t buf[], size_t buf_len) {
1✔
152
   test_failure(who() + ": " + what + " buf len " + std::to_string(buf_len) + " value " +
5✔
153
                Botan::hex_encode(buf, buf_len));
1✔
154
}
1✔
155

156
bool Test::Result::test_failure(const std::string& err) {
25✔
157
   m_fail_log.push_back(err);
25✔
158

159
   if(Test::options().abort_on_first_fail() && m_who != "Failing Test") {
25✔
160
      std::abort();
×
161
   }
162
   return false;
25✔
163
}
164

165
namespace {
166

167
bool same_contents(const uint8_t x[], const uint8_t y[], size_t len) {
271,605✔
168
   return (len == 0) ? true : std::memcmp(x, y, len) == 0;
270,256✔
169
}
170

171
}  // namespace
172

173
bool Test::Result::test_ne(const std::string& what,
3,394✔
174
                           const uint8_t produced[],
175
                           size_t produced_len,
176
                           const uint8_t expected[],
177
                           size_t expected_len) {
178
   if(produced_len == expected_len && same_contents(produced, expected, expected_len)) {
3,394✔
179
      return test_failure(who() + ": " + what + " produced matching");
6✔
180
   }
181
   return test_success();
6,784✔
182
}
183

184
bool Test::Result::test_eq(const char* producer,
270,190✔
185
                           const std::string& what,
186
                           const uint8_t produced[],
187
                           size_t produced_size,
188
                           const uint8_t expected[],
189
                           size_t expected_size) {
190
   if(produced_size == expected_size && same_contents(produced, expected, expected_size)) {
270,190✔
191
      return test_success();
540,378✔
192
   }
193

194
   std::ostringstream err;
1✔
195

196
   err << who();
1✔
197

198
   if(producer) {
1✔
199
      err << " producer '" << producer << "'";
×
200
   }
201

202
   err << " unexpected result for " << what;
1✔
203

204
   if(produced_size != expected_size) {
1✔
205
      err << " produced " << produced_size << " bytes expected " << expected_size;
1✔
206
   }
207

208
   std::vector<uint8_t> xor_diff(std::min(produced_size, expected_size));
2✔
209
   size_t bytes_different = 0;
1✔
210

211
   for(size_t i = 0; i != xor_diff.size(); ++i) {
4✔
212
      xor_diff[i] = produced[i] ^ expected[i];
3✔
213
      bytes_different += (xor_diff[i] > 0);
3✔
214
   }
215

216
   err << "\nProduced: " << Botan::hex_encode(produced, produced_size)
1✔
217
       << "\nExpected: " << Botan::hex_encode(expected, expected_size);
3✔
218

219
   if(bytes_different > 0) {
1✔
220
      err << "\nXOR Diff: " << Botan::hex_encode(xor_diff);
1✔
221
   }
222

223
   return test_failure(err.str());
1✔
224
}
1✔
225

226
bool Test::Result::test_is_nonempty(const std::string& what_is_it, const std::string& to_examine) {
33,715✔
227
   if(to_examine.empty()) {
33,715✔
228
      return test_failure(what_is_it + " was empty");
1✔
229
   }
230
   return test_success();
67,428✔
231
}
232

233
bool Test::Result::test_eq(const std::string& what, const std::string& produced, const std::string& expected) {
71,615✔
234
   return test_is_eq(what, produced, expected);
71,615✔
235
}
236

237
bool Test::Result::test_eq(const std::string& what, const char* produced, const char* expected) {
22✔
238
   return test_is_eq(what, std::string(produced), std::string(expected));
22✔
239
}
240

241
bool Test::Result::test_eq(const std::string& what, size_t produced, size_t expected) {
135,688✔
242
   return test_is_eq(what, produced, expected);
135,688✔
243
}
244

245
bool Test::Result::test_eq_sz(const std::string& what, size_t produced, size_t expected) {
45,786✔
246
   return test_is_eq(what, produced, expected);
45,786✔
247
}
248

249
bool Test::Result::test_eq(const std::string& what,
107✔
250
                           const Botan::OctetString& produced,
251
                           const Botan::OctetString& expected) {
252
   std::ostringstream out;
107✔
253
   out << m_who << " " << what;
107✔
254

255
   if(produced == expected) {
107✔
256
      out << " produced expected result " << produced.to_string();
214✔
257
      return test_success(out.str());
107✔
258
   } else {
259
      out << " produced unexpected result '" << produced.to_string() << "' expected '" << expected.to_string() << "'";
×
260
      return test_failure(out.str());
×
261
   }
262
}
107✔
263

264
bool Test::Result::test_lt(const std::string& what, size_t produced, size_t expected) {
5,640✔
265
   if(produced >= expected) {
5,640✔
266
      std::ostringstream err;
1✔
267
      err << m_who << " " << what;
1✔
268
      err << " unexpected result " << produced << " >= " << expected;
1✔
269
      return test_failure(err.str());
1✔
270
   }
1✔
271

272
   return test_success();
11,278✔
273
}
274

275
bool Test::Result::test_lte(const std::string& what, size_t produced, size_t expected) {
1,021,636✔
276
   if(produced > expected) {
1,021,636✔
277
      std::ostringstream err;
1✔
278
      err << m_who << " " << what << " unexpected result " << produced << " > " << expected;
1✔
279
      return test_failure(err.str());
1✔
280
   }
1✔
281

282
   return test_success();
2,043,270✔
283
}
284

285
bool Test::Result::test_gte(const std::string& what, size_t produced, size_t expected) {
1,142,072✔
286
   if(produced < expected) {
1,142,072✔
287
      std::ostringstream err;
1✔
288
      err << m_who;
1✔
289
      err << " " << what;
1✔
290
      err << " unexpected result " << produced << " < " << expected;
1✔
291
      return test_failure(err.str());
1✔
292
   }
1✔
293

294
   return test_success();
2,284,142✔
295
}
296

297
bool Test::Result::test_gt(const std::string& what, size_t produced, size_t expected) {
14,473✔
298
   if(produced <= expected) {
14,473✔
299
      std::ostringstream err;
×
300
      err << m_who;
×
301
      err << " " << what;
×
302
      err << " unexpected result " << produced << " <= " << expected;
×
303
      return test_failure(err.str());
×
304
   }
×
305

306
   return test_success();
28,946✔
307
}
308

309
bool Test::Result::test_ne(const std::string& what, const std::string& str1, const std::string& str2) {
25✔
310
   if(str1 != str2) {
25✔
311
      return test_success(str1 + " != " + str2);
48✔
312
   }
313

314
   return test_failure(who() + " " + what + " produced matching strings " + str1);
4✔
315
}
316

317
bool Test::Result::test_ne(const std::string& what, size_t produced, size_t expected) {
118✔
318
   if(produced != expected) {
118✔
319
      return test_success();
234✔
320
   }
321

322
   std::ostringstream err;
1✔
323
   err << who() << " " << what << " produced " << produced << " unexpected value";
1✔
324
   return test_failure(err.str());
1✔
325
}
1✔
326

327
#if defined(BOTAN_HAS_BIGINT)
328
bool Test::Result::test_eq(const std::string& what, const BigInt& produced, const BigInt& expected) {
184,681✔
329
   return test_is_eq(what, produced, expected);
184,681✔
330
}
331

332
bool Test::Result::test_ne(const std::string& what, const BigInt& produced, const BigInt& expected) {
97✔
333
   if(produced != expected) {
97✔
334
      return test_success();
192✔
335
   }
336

337
   std::ostringstream err;
1✔
338
   err << who() << " " << what << " produced " << produced << " prohibited value";
1✔
339
   return test_failure(err.str());
1✔
340
}
1✔
341
#endif
342

343
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
344
bool Test::Result::test_eq(const std::string& what, const Botan::EC_Point& a, const Botan::EC_Point& b) {
3,248✔
345
   //return test_is_eq(what, a, b);
346
   if(a == b) {
3,248✔
347
      return test_success();
6,496✔
348
   }
349

350
   std::ostringstream err;
×
351
   err << who() << " " << what << " a=(" << a.get_affine_x() << "," << a.get_affine_y() << ")"
×
352
       << " b=(" << b.get_affine_x() << "," << b.get_affine_y();
×
353
   return test_failure(err.str());
×
354
}
×
355
#endif
356

357
bool Test::Result::test_eq(const std::string& what, bool produced, bool expected) {
346,120✔
358
   return test_is_eq(what, produced, expected);
346,120✔
359
}
360

361
bool Test::Result::test_rc_init(const std::string& func, int rc) {
110✔
362
   if(rc == 0) {
110✔
363
      return test_success();
220✔
364
   } else {
365
      std::ostringstream msg;
×
366
      msg << m_who;
×
367
      msg << " " << func;
×
368

369
      // -40 is BOTAN_FFI_ERROR_NOT_IMPLEMENTED
370
      if(rc == -40) {
×
371
         msg << " returned not implemented";
×
372
      } else {
373
         msg << " unexpectedly failed with error code " << rc;
×
374
      }
375

376
      if(rc == -40) {
×
377
         this->test_note(msg.str());
×
378
      } else {
379
         this->test_failure(msg.str());
×
380
      }
381
      return false;
×
382
   }
×
383
}
384

385
bool Test::Result::test_rc(const std::string& func, int expected, int rc) {
371✔
386
   if(expected != rc) {
371✔
387
      std::ostringstream err;
1✔
388
      err << m_who;
1✔
389
      err << " call to " << func << " unexpectedly returned " << rc;
1✔
390
      err << " but expecting " << expected;
1✔
391
      return test_failure(err.str());
1✔
392
   }
1✔
393

394
   return test_success();
740✔
395
}
396

397
void Test::initialize(std::string test_name, CodeLocation location) {
409✔
398
   m_test_name = std::move(test_name);
409✔
399
   m_registration_location = location;
409✔
400
}
409✔
401

402
Botan::RandomNumberGenerator& Test::rng() const {
453,237✔
403
   if(!m_test_rng) {
453,237✔
404
      m_test_rng = Test::new_rng(m_test_name);
144✔
405
   }
406

407
   return *m_test_rng;
453,237✔
408
}
409

410
std::vector<std::string> Test::possible_providers(const std::string& /*unused*/) {
×
411
   return Test::provider_filter({"base"});
×
412
}
413

414
//static
415
std::string Test::format_time(uint64_t ns) {
1,448✔
416
   std::ostringstream o;
1,448✔
417

418
   if(ns > 1000000000) {
1,448✔
419
      o << std::setprecision(2) << std::fixed << ns / 1000000000.0 << " sec";
154✔
420
   } else {
421
      o << std::setprecision(2) << std::fixed << ns / 1000000.0 << " msec";
1,294✔
422
   }
423

424
   return o.str();
2,896✔
425
}
1,448✔
426

427
Test::Result::Result(std::string who, const std::vector<Result>& downstream_results) : Result(std::move(who)) {
14✔
428
   for(const auto& result : downstream_results) {
82✔
429
      merge(result, true /* ignore non-matching test names */);
68✔
430
   }
431
}
14✔
432

433
// TODO: this should move to `StdoutReporter`
434
std::string Test::Result::result_string() const {
2,497✔
435
   const bool verbose = Test::options().verbose();
2,497✔
436

437
   if(tests_run() == 0 && !verbose) {
2,497✔
438
      return "";
20✔
439
   }
440

441
   std::ostringstream report;
2,477✔
442

443
   report << who() << " ran ";
2,477✔
444

445
   if(tests_run() == 0) {
2,477✔
446
      report << "ZERO";
×
447
   } else {
448
      report << tests_run();
2,477✔
449
   }
450
   report << " tests";
2,477✔
451

452
   if(m_ns_taken > 0) {
2,477✔
453
      report << " in " << format_time(m_ns_taken);
2,894✔
454
   }
455

456
   if(tests_failed()) {
2,477✔
457
      report << " " << tests_failed() << " FAILED";
25✔
458
   } else {
459
      report << " all ok";
2,452✔
460
   }
461

462
   report << "\n";
2,477✔
463

464
   for(size_t i = 0; i != m_fail_log.size(); ++i) {
2,502✔
465
      report << "Failure " << (i + 1) << ": " << m_fail_log[i];
25✔
466
      if(m_where) {
25✔
467
         report << " (at " << m_where->path << ":" << m_where->line << ")";
×
468
      }
469
      report << "\n";
25✔
470
   }
471

472
   if(!m_fail_log.empty() || tests_run() == 0 || verbose) {
2,477✔
473
      for(size_t i = 0; i != m_log.size(); ++i) {
25✔
474
         report << "Note " << (i + 1) << ": " << m_log[i] << "\n";
×
475
      }
476
   }
477

478
   return report.str();
2,477✔
479
}
2,477✔
480

481
namespace {
482

483
class Test_Registry {
484
   public:
485
      static Test_Registry& instance() {
1,245✔
486
         static Test_Registry registry;
1,246✔
487
         return registry;
1,245✔
488
      }
489

490
      void register_test(const std::string& category,
409✔
491
                         const std::string& name,
492
                         bool smoke_test,
493
                         bool needs_serialization,
494
                         std::function<std::unique_ptr<Test>()> maker_fn) {
495
         if(m_tests.contains(name)) {
409✔
496
            throw Test_Error("Duplicate registration of test '" + name + "'");
×
497
         }
498

499
         if(m_tests.contains(category)) {
409✔
500
            throw Test_Error("'" + category + "' cannot be used as category, test exists");
×
501
         }
502

503
         if(m_categories.contains(name)) {
409✔
504
            throw Test_Error("'" + name + "' cannot be used as test name, category exists");
×
505
         }
506

507
         if(smoke_test) {
409✔
508
            m_smoke_tests.push_back(name);
10✔
509
         }
510

511
         if(needs_serialization) {
409✔
512
            m_mutexed_tests.push_back(name);
21✔
513
         }
514

515
         m_tests.emplace(name, std::move(maker_fn));
409✔
516
         m_categories.emplace(category, name);
409✔
517
      }
409✔
518

519
      std::unique_ptr<Test> get_test(const std::string& test_name) const {
409✔
520
         auto i = m_tests.find(test_name);
409✔
521
         if(i != m_tests.end()) {
409✔
522
            return i->second();
409✔
523
         }
524
         return nullptr;
×
525
      }
526

527
      std::set<std::string> registered_tests() const {
×
528
         std::set<std::string> s;
×
529
         for(auto&& i : m_tests) {
×
530
            s.insert(i.first);
×
531
         }
532
         return s;
×
533
      }
×
534

535
      std::set<std::string> registered_test_categories() const {
×
536
         std::set<std::string> s;
×
537
         for(auto&& i : m_categories) {
×
538
            s.insert(i.first);
×
539
         }
540
         return s;
×
541
      }
×
542

543
      std::vector<std::string> filter_registered_tests(const std::vector<std::string>& requested,
1✔
544
                                                       const std::set<std::string>& to_be_skipped) {
545
         std::vector<std::string> result;
1✔
546

547
         // TODO: this is O(n^2), but we have a relatively small number of tests.
548
         auto insert_if_not_exists_and_not_skipped = [&](const std::string& test_name) {
410✔
549
            if(!Botan::value_exists(result, test_name) && !to_be_skipped.contains(test_name)) {
409✔
550
               result.push_back(test_name);
399✔
551
            }
552
         };
410✔
553

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

578
         return result;
1✔
579
      }
×
580

581
      bool needs_serialization(const std::string& test_name) const {
426✔
582
         return Botan::value_exists(m_mutexed_tests, test_name);
17✔
583
      }
584

585
   private:
586
      Test_Registry() = default;
1✔
587

588
   private:
589
      std::map<std::string, std::function<std::unique_ptr<Test>()>> m_tests;
590
      std::multimap<std::string, std::string> m_categories;
591
      std::vector<std::string> m_smoke_tests;
592
      std::vector<std::string> m_mutexed_tests;
593
};
594

595
}  // namespace
596

597
// static Test:: functions
598

599
//static
600
void Test::register_test(const std::string& category,
409✔
601
                         const std::string& name,
602
                         bool smoke_test,
603
                         bool needs_serialization,
604
                         std::function<std::unique_ptr<Test>()> maker_fn) {
605
   Test_Registry::instance().register_test(category, name, smoke_test, needs_serialization, std::move(maker_fn));
818✔
606
}
409✔
607

608
//static
609
uint64_t Test::timestamp() {
97,261✔
610
   auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
97,261✔
611
   return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
2,007✔
612
}
613

614
//static
615
std::vector<Test::Result> Test::flatten_result_lists(std::vector<std::vector<Test::Result>> result_lists) {
4✔
616
   std::vector<Test::Result> results;
4✔
617
   for(auto& result_list : result_lists) {
26✔
618
      for(auto& result : result_list) {
71✔
619
         results.emplace_back(std::move(result));
49✔
620
      }
621
   }
622
   return results;
4✔
623
}
×
624

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

630
//static
631
std::set<std::string> Test::registered_test_categories() {
×
632
   return Test_Registry::instance().registered_test_categories();
×
633
}
634

635
//static
636
std::unique_ptr<Test> Test::get_test(const std::string& test_name) {
409✔
637
   return Test_Registry::instance().get_test(test_name);
409✔
638
}
639

640
//static
641
bool Test::test_needs_serialization(const std::string& test_name) {
409✔
642
   return Test_Registry::instance().needs_serialization(test_name);
409✔
643
}
644

645
//static
646
std::vector<std::string> Test::filter_registered_tests(const std::vector<std::string>& requested,
1✔
647
                                                       const std::set<std::string>& to_be_skipped) {
648
   return Test_Registry::instance().filter_registered_tests(requested, to_be_skipped);
1✔
649
}
650

651
//static
652
std::string Test::temp_file_name(const std::string& basename) {
21✔
653
   // TODO add a --tmp-dir option to the tests to specify where these files go
654

655
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
656

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

660
   int fd = ::mkstemp(&mkstemp_basename[0]);
21✔
661

662
   // error
663
   if(fd < 0) {
21✔
664
      return "";
×
665
   }
666

667
   ::close(fd);
21✔
668

669
   return mkstemp_basename;
21✔
670
#else
671
   // For now just create the temp in the current working directory
672
   return basename;
673
#endif
674
}
21✔
675

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

688
std::string Test::read_data_file(const std::string& path) {
38✔
689
   const std::string fsname = Test::data_file(path);
38✔
690
   std::ifstream file(fsname.c_str());
38✔
691
   if(!file.good()) {
38✔
692
      throw Test_Error("Error reading from " + fsname);
×
693
   }
694

695
   return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
76✔
696
}
38✔
697

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

705
   std::vector<uint8_t> contents;
34✔
706

707
   while(file.good()) {
74✔
708
      std::vector<uint8_t> buf(4096);
40✔
709
      file.read(reinterpret_cast<char*>(buf.data()), buf.size());
40✔
710
      const size_t got = static_cast<size_t>(file.gcount());
40✔
711

712
      if(got == 0 && file.eof()) {
40✔
713
         break;
714
      }
715

716
      contents.insert(contents.end(), buf.data(), buf.data() + got);
40✔
717
   }
40✔
718

719
   return contents;
68✔
720
}
34✔
721

722
// static member variables of Test
723

724
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
725
Test_Options Test::m_opts;
726
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
727
std::string Test::m_test_rng_seed;
728

729
//static
730
void Test::set_test_options(const Test_Options& opts) {
1✔
731
   m_opts = opts;
1✔
732
}
1✔
733

734
namespace {
735

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

744
      void clear() override { m_x = 0; }
×
745

746
      bool accepts_input() const override { return true; }
×
747

748
      bool is_seeded() const override { return true; }
275,451✔
749

750
      void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
10,212,669✔
751
         for(const auto byte : input) {
10,212,669✔
752
            mix(byte);
×
753
         }
754

755
         for(auto& byte : output) {
73,272,331✔
756
            byte = mix();
63,059,662✔
757
         }
758
      }
10,212,669✔
759

760
      Testsuite_RNG(std::string_view seed, std::string_view test_name) {
265✔
761
         m_x = 0;
265✔
762

763
         for(char c : seed) {
7,950✔
764
            this->mix(static_cast<uint8_t>(c));
7,685✔
765
         }
766
         for(char c : test_name) {
5,007✔
767
            this->mix(static_cast<uint8_t>(c));
4,742✔
768
         }
769
      }
265✔
770

771
   private:
772
      uint8_t mix(uint8_t input = 0) {
63,072,089✔
773
         m_x ^= input;
63,072,089✔
774
         m_x *= 0xF2E16957;
63,072,089✔
775
         m_x += 0xE50B590F;
63,072,089✔
776
         return static_cast<uint8_t>(m_x >> 27);
63,072,089✔
777
      }
778

779
      uint64_t m_x;
780
};
781

782
}  // namespace
783

784
//static
785
void Test::set_test_rng_seed(std::span<const uint8_t> seed, size_t epoch) {
1✔
786
   m_test_rng_seed = Botan::fmt("seed={} epoch={}", Botan::hex_encode(seed), epoch);
1✔
787
}
1✔
788

789
//static
790
std::unique_ptr<Botan::RandomNumberGenerator> Test::new_rng(std::string_view test_name) {
257✔
791
   return std::make_unique<Testsuite_RNG>(m_test_rng_seed, test_name);
257✔
792
}
793

794
//static
795
std::shared_ptr<Botan::RandomNumberGenerator> Test::new_shared_rng(std::string_view test_name) {
8✔
796
   return std::make_shared<Testsuite_RNG>(m_test_rng_seed, test_name);
8✔
797
}
798

799
//static
800
std::string Test::data_file(const std::string& file) {
631✔
801
   return options().data_dir() + "/" + file;
1,262✔
802
}
803

804
//static
805
std::string Test::data_dir(const std::string& subdir) {
1✔
806
   return options().data_dir() + "/" + subdir;
2✔
807
}
808

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

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

832
//static
833
std::vector<std::string> Test::provider_filter(const std::vector<std::string>& in) {
47,853✔
834
   if(m_opts.provider().empty()) {
47,853✔
835
      return in;
47,853✔
836
   }
837
   for(auto&& provider : in) {
×
838
      if(provider == m_opts.provider()) {
×
839
         return std::vector<std::string>{provider};
×
840
      }
841
   }
842
   return std::vector<std::string>{};
×
843
}
×
844

845
std::string Test::random_password(Botan::RandomNumberGenerator& rng) {
222✔
846
   const size_t len = 1 + rng.next_byte() % 32;
222✔
847
   return Botan::hex_encode(rng.random_vec(len));
444✔
848
}
849

850
size_t Test::random_index(Botan::RandomNumberGenerator& rng, size_t max) {
8,062✔
851
   return Botan::load_be(rng.random_array<8>()) % max;
8,062✔
852
}
853

854
std::vector<std::vector<uint8_t>> VarMap::get_req_bin_list(const std::string& key) const {
12✔
855
   auto i = m_vars.find(key);
12✔
856
   if(i == m_vars.end()) {
12✔
857
      throw Test_Error("Test missing variable " + key);
×
858
   }
859

860
   std::vector<std::vector<uint8_t>> bin_list;
12✔
861

862
   for(auto&& part : Botan::split_on(i->second, ',')) {
62✔
863
      try {
50✔
864
         bin_list.push_back(Botan::hex_decode(part));
100✔
865
      } catch(std::exception& e) {
×
866
         std::ostringstream oss;
×
867
         oss << "Bad input '" << part << "'"
×
868
             << " in binary list key " << key << " - " << e.what();
×
869
         throw Test_Error(oss.str());
×
870
      }
×
871
   }
12✔
872

873
   return bin_list;
12✔
874
}
×
875

876
std::vector<uint8_t> VarMap::get_req_bin(const std::string& key) const {
154,832✔
877
   auto i = m_vars.find(key);
154,832✔
878
   if(i == m_vars.end()) {
154,832✔
879
      throw Test_Error("Test missing variable " + key);
×
880
   }
881

882
   try {
154,832✔
883
      if(i->second.starts_with("0x")) {
154,832✔
884
         if(i->second.size() % 2 == 0) {
×
885
            return Botan::hex_decode(i->second.substr(2));
×
886
         } else {
887
            std::string z = i->second;
×
888
            std::swap(z[0], z[1]);  // swap 0x to x0 then remove x
×
889
            return Botan::hex_decode(z.substr(1));
×
890
         }
×
891
      } else {
892
         return Botan::hex_decode(i->second);
154,832✔
893
      }
894
   } catch(std::exception& e) {
×
895
      std::ostringstream oss;
×
896
      oss << "Bad input '" << i->second << "'"
×
897
          << " for key " << key << " - " << e.what();
×
898
      throw Test_Error(oss.str());
×
899
   }
×
900
}
×
901

902
std::string VarMap::get_opt_str(const std::string& key, const std::string& def_value) const {
14,376✔
903
   auto i = m_vars.find(key);
14,376✔
904
   if(i == m_vars.end()) {
14,376✔
905
      return def_value;
14,321✔
906
   }
907
   return i->second;
55✔
908
}
909

910
bool VarMap::get_req_bool(const std::string& key) const {
39✔
911
   auto i = m_vars.find(key);
39✔
912
   if(i == m_vars.end()) {
39✔
913
      throw Test_Error("Test missing variable " + key);
×
914
   }
915

916
   if(i->second == "true") {
39✔
917
      return true;
918
   } else if(i->second == "false") {
23✔
919
      return false;
920
   } else {
921
      throw Test_Error("Invalid boolean for key '" + key + "' value '" + i->second + "'");
×
922
   }
923
}
924

925
size_t VarMap::get_req_sz(const std::string& key) const {
4,547✔
926
   auto i = m_vars.find(key);
4,547✔
927
   if(i == m_vars.end()) {
4,547✔
928
      throw Test_Error("Test missing variable " + key);
×
929
   }
930
   return Botan::to_u32bit(i->second);
4,547✔
931
}
932

933
uint8_t VarMap::get_req_u8(const std::string& key) const {
17✔
934
   const size_t s = this->get_req_sz(key);
17✔
935
   if(s > 256) {
17✔
936
      throw Test_Error("Invalid " + key + " expected uint8_t got " + std::to_string(s));
×
937
   }
938
   return static_cast<uint8_t>(s);
17✔
939
}
940

941
uint32_t VarMap::get_req_u32(const std::string& key) const {
14✔
942
   return static_cast<uint32_t>(get_req_sz(key));
14✔
943
}
944

945
uint64_t VarMap::get_req_u64(const std::string& key) const {
17✔
946
   auto i = m_vars.find(key);
17✔
947
   if(i == m_vars.end()) {
17✔
948
      throw Test_Error("Test missing variable " + key);
×
949
   }
950
   try {
17✔
951
      return std::stoull(i->second);
17✔
952
   } catch(std::exception&) {
×
953
      throw Test_Error("Invalid u64 value '" + i->second + "'");
×
954
   }
×
955
}
956

957
size_t VarMap::get_opt_sz(const std::string& key, const size_t def_value) const {
31,377✔
958
   auto i = m_vars.find(key);
31,377✔
959
   if(i == m_vars.end()) {
31,377✔
960
      return def_value;
961
   }
962
   return Botan::to_u32bit(i->second);
12,244✔
963
}
964

965
uint64_t VarMap::get_opt_u64(const std::string& key, const uint64_t def_value) const {
3,538✔
966
   auto i = m_vars.find(key);
3,538✔
967
   if(i == m_vars.end()) {
3,538✔
968
      return def_value;
969
   }
970
   try {
641✔
971
      return std::stoull(i->second);
3,538✔
972
   } catch(std::exception&) {
×
973
      throw Test_Error("Invalid u64 value '" + i->second + "'");
×
974
   }
×
975
}
976

977
std::vector<uint8_t> VarMap::get_opt_bin(const std::string& key) const {
41,050✔
978
   auto i = m_vars.find(key);
41,050✔
979
   if(i == m_vars.end()) {
41,050✔
980
      return std::vector<uint8_t>();
29,373✔
981
   }
982

983
   try {
11,677✔
984
      return Botan::hex_decode(i->second);
11,677✔
985
   } catch(std::exception&) {
×
986
      throw Test_Error("Test invalid hex input '" + i->second + "'" + +" for key " + key);
×
987
   }
×
988
}
989

990
std::string VarMap::get_req_str(const std::string& key) const {
52,783✔
991
   auto i = m_vars.find(key);
52,783✔
992
   if(i == m_vars.end()) {
52,783✔
993
      throw Test_Error("Test missing variable " + key);
×
994
   }
995
   return i->second;
52,783✔
996
}
997

998
#if defined(BOTAN_HAS_BIGINT)
999
Botan::BigInt VarMap::get_req_bn(const std::string& key) const {
38,480✔
1000
   auto i = m_vars.find(key);
38,480✔
1001
   if(i == m_vars.end()) {
38,480✔
1002
      throw Test_Error("Test missing variable " + key);
×
1003
   }
1004

1005
   try {
38,480✔
1006
      return Botan::BigInt(i->second);
38,480✔
1007
   } catch(std::exception&) {
×
1008
      throw Test_Error("Test invalid bigint input '" + i->second + "' for key " + key);
×
1009
   }
×
1010
}
1011

1012
Botan::BigInt VarMap::get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const {
80✔
1013
   auto i = m_vars.find(key);
80✔
1014
   if(i == m_vars.end()) {
80✔
1015
      return def_value;
24✔
1016
   }
1017

1018
   try {
56✔
1019
      return Botan::BigInt(i->second);
56✔
1020
   } catch(std::exception&) {
×
1021
      throw Test_Error("Test invalid bigint input '" + i->second + "' for key " + key);
×
1022
   }
×
1023
}
1024
#endif
1025

1026
Text_Based_Test::Text_Based_Test(const std::string& data_src,
180✔
1027
                                 const std::string& required_keys_str,
1028
                                 const std::string& optional_keys_str) :
180✔
1029
      m_data_src(data_src) {
540✔
1030
   if(required_keys_str.empty()) {
180✔
1031
      throw Test_Error("Invalid test spec");
×
1032
   }
1033

1034
   std::vector<std::string> required_keys = Botan::split_on(required_keys_str, ',');
180✔
1035
   std::vector<std::string> optional_keys = Botan::split_on(optional_keys_str, ',');
180✔
1036

1037
   m_required_keys.insert(required_keys.begin(), required_keys.end());
180✔
1038
   m_optional_keys.insert(optional_keys.begin(), optional_keys.end());
180✔
1039
   m_output_key = required_keys.at(required_keys.size() - 1);
180✔
1040
}
180✔
1041

1042
std::string Text_Based_Test::get_next_line() {
154,938✔
1043
   while(true) {
155,192✔
1044
      if(m_cur == nullptr || m_cur->good() == false) {
155,192✔
1045
         if(m_srcs.empty()) {
445✔
1046
            if(m_first) {
360✔
1047
               if(m_data_src.ends_with(".vec")) {
180✔
1048
                  m_srcs.push_back(Test::data_file(m_data_src));
336✔
1049
               } else {
1050
                  const auto fs = Test::files_in_data_dir(m_data_src);
12✔
1051
                  m_srcs.assign(fs.begin(), fs.end());
12✔
1052
                  if(m_srcs.empty()) {
12✔
1053
                     throw Test_Error("Error reading test data dir " + m_data_src);
×
1054
                  }
1055
               }
12✔
1056

1057
               m_first = false;
180✔
1058
            } else {
1059
               return "";  // done
180✔
1060
            }
1061
         }
1062

1063
         m_cur = std::make_unique<std::ifstream>(m_srcs[0]);
350✔
1064
         m_cur_src_name = m_srcs[0];
265✔
1065

1066
#if defined(BOTAN_HAS_CPUID)
1067
         // Reinit cpuid on new file if needed
1068
         if(m_cpu_flags.empty() == false) {
265✔
1069
            m_cpu_flags.clear();
15✔
1070
            Botan::CPUID::initialize();
15✔
1071
         }
1072
#endif
1073

1074
         if(!m_cur->good()) {
265✔
1075
            throw Test_Error("Could not open input file '" + m_cur_src_name);
×
1076
         }
1077

1078
         m_srcs.pop_front();
265✔
1079
      }
1080

1081
      while(m_cur->good()) {
224,783✔
1082
         std::string line;
224,529✔
1083
         std::getline(*m_cur, line);
224,529✔
1084

1085
         if(line.empty()) {
224,529✔
1086
            continue;
54,666✔
1087
         }
1088

1089
         if(line[0] == '#') {
169,863✔
1090
            if(line.starts_with("#test ")) {
15,122✔
1091
               return line;
17✔
1092
            } else {
1093
               continue;
15,105✔
1094
            }
1095
         }
1096

1097
         return line;
154,741✔
1098
      }
224,529✔
1099
   }
1100
}
1101

1102
namespace {
1103

1104
// strips leading and trailing but not internal whitespace
1105
std::string strip_ws(const std::string& in) {
307,880✔
1106
   const char* whitespace = " ";
307,880✔
1107

1108
   const auto first_c = in.find_first_not_of(whitespace);
307,880✔
1109
   if(first_c == std::string::npos) {
307,880✔
1110
      return "";
1,007✔
1111
   }
1112

1113
   const auto last_c = in.find_last_not_of(whitespace);
306,873✔
1114

1115
   return in.substr(first_c, last_c - first_c + 1);
306,873✔
1116
}
1117

1118
std::vector<std::string> parse_cpuid_bits(const std::vector<std::string>& tok) {
17✔
1119
   std::vector<std::string> bits;
17✔
1120

1121
#if defined(BOTAN_HAS_CPUID)
1122
   for(size_t i = 1; i < tok.size(); ++i) {
82✔
1123
      if(auto bit = Botan::CPUID::bit_from_string(tok[i])) {
65✔
1124
         bits.push_back(bit->to_string());
78✔
1125
      }
1126
   }
1127
#else
1128
   BOTAN_UNUSED(tok);
1129
#endif
1130

1131
   return bits;
17✔
1132
}
×
1133

1134
}  // namespace
1135

1136
bool Text_Based_Test::skip_this_test(const std::string& /*header*/, const VarMap& /*vars*/) {
31,984✔
1137
   return false;
31,984✔
1138
}
1139

1140
std::vector<Test::Result> Text_Based_Test::run() {
180✔
1141
   std::vector<Test::Result> results;
180✔
1142

1143
   std::string header, header_or_name = m_data_src;
180✔
1144
   VarMap vars;
180✔
1145
   size_t test_cnt = 0;
180✔
1146

1147
   while(true) {
154,938✔
1148
      const std::string line = get_next_line();
154,938✔
1149
      if(line.empty())  // EOF
154,938✔
1150
      {
1151
         break;
1152
      }
1153

1154
      if(line.starts_with("#test ")) {
154,758✔
1155
         std::vector<std::string> pragma_tokens = Botan::split_on(line.substr(6), ' ');
17✔
1156

1157
         if(pragma_tokens.empty()) {
17✔
1158
            throw Test_Error("Empty pragma found in " + m_cur_src_name);
×
1159
         }
1160

1161
         if(pragma_tokens[0] != "cpuid") {
17✔
1162
            throw Test_Error("Unknown test pragma '" + line + "' in " + m_cur_src_name);
×
1163
         }
1164

1165
         if(!Test_Registry::instance().needs_serialization(this->test_name())) {
17✔
1166
            throw Test_Error(Botan::fmt("'{}' used cpuid control but is not serialized", this->test_name()));
×
1167
         }
1168

1169
         m_cpu_flags = parse_cpuid_bits(pragma_tokens);
17✔
1170

1171
         continue;
17✔
1172
      } else if(line[0] == '#') {
154,758✔
1173
         throw Test_Error("Unknown test pragma '" + line + "' in " + m_cur_src_name);
×
1174
      }
1175

1176
      if(line[0] == '[' && line[line.size() - 1] == ']') {
154,741✔
1177
         header = line.substr(1, line.size() - 2);
801✔
1178
         header_or_name = header;
801✔
1179
         test_cnt = 0;
801✔
1180
         vars.clear();
801✔
1181
         continue;
801✔
1182
      }
1183

1184
      const std::string test_id = "test " + std::to_string(test_cnt);
307,880✔
1185

1186
      auto equal_i = line.find_first_of('=');
153,940✔
1187

1188
      if(equal_i == std::string::npos) {
153,940✔
1189
         results.push_back(Test::Result::Failure(header_or_name, "invalid input '" + line + "'"));
×
1190
         continue;
×
1191
      }
1192

1193
      std::string key = strip_ws(std::string(line.begin(), line.begin() + equal_i - 1));
307,880✔
1194
      std::string val = strip_ws(std::string(line.begin() + equal_i + 1, line.end()));
307,880✔
1195

1196
      if(!m_required_keys.contains(key) && !m_optional_keys.contains(key)) {
153,940✔
1197
         auto r = Test::Result::Failure(header_or_name, Botan::fmt("{} failed unknown key {}", test_id, key));
×
1198
         results.push_back(r);
×
1199
      }
×
1200

1201
      vars.add(key, val);
153,940✔
1202

1203
      if(key == m_output_key) {
153,940✔
1204
         try {
47,763✔
1205
            for(auto& req_key : m_required_keys) {
243,280✔
1206
               if(!vars.has_key(req_key)) {
391,034✔
1207
                  auto r =
×
1208
                     Test::Result::Failure(header_or_name, Botan::fmt("{} missing required key {}", test_id, req_key));
×
1209
                  results.push_back(r);
×
1210
               }
×
1211
            }
1212

1213
            if(skip_this_test(header, vars)) {
47,763✔
1214
               continue;
139✔
1215
            }
1216

1217
            ++test_cnt;
47,624✔
1218

1219
            uint64_t start = Test::timestamp();
47,624✔
1220

1221
            Test::Result result = run_one_test(header, vars);
47,624✔
1222
#if defined(BOTAN_HAS_CPUID)
1223
            if(!m_cpu_flags.empty()) {
47,624✔
1224
               for(const auto& cpuid_str : m_cpu_flags) {
23,221✔
1225
                  if(const auto bit = Botan::CPUID::Feature::from_string(cpuid_str)) {
16,559✔
1226
                     if(Botan::CPUID::has(*bit)) {
16,559✔
1227
                        Botan::CPUID::clear_cpuid_bit(*bit);
14,035✔
1228
                        // now re-run the test
1229
                        result.merge(run_one_test(header, vars));
14,035✔
1230
                     }
1231
                  }
1232
               }
1233
               Botan::CPUID::initialize();
6,662✔
1234
            }
1235
#endif
1236
            result.set_ns_consumed(Test::timestamp() - start);
47,624✔
1237

1238
            if(result.tests_failed()) {
47,624✔
1239
               std::ostringstream oss;
×
1240
               oss << "Test # " << test_cnt << " ";
×
1241
               if(!header.empty()) {
×
1242
                  oss << header << " ";
×
1243
               }
1244
               oss << "failed ";
×
1245

1246
               for(const auto& k : m_required_keys) {
×
1247
                  oss << k << "=" << vars.get_req_str(k) << " ";
×
1248
               }
1249

1250
               result.test_note(oss.str());
×
1251
            }
×
1252
            results.push_back(result);
47,624✔
1253
         } catch(std::exception& e) {
47,624✔
1254
            std::ostringstream oss;
×
1255
            oss << "Test # " << test_cnt << " ";
×
1256
            if(!header.empty()) {
×
1257
               oss << header << " ";
×
1258
            }
1259

1260
            for(const auto& k : m_required_keys) {
×
1261
               oss << k << "=" << vars.get_req_str(k) << " ";
×
1262
            }
1263

1264
            oss << "failed with exception '" << e.what() << "'";
×
1265

1266
            results.push_back(Test::Result::Failure(header_or_name, oss.str()));
×
1267
         }
×
1268

1269
         if(clear_between_callbacks()) {
47,624✔
1270
            vars.clear();
32,852✔
1271
         }
1272
      }
1273
   }
155,036✔
1274

1275
   if(results.empty()) {
180✔
1276
      return results;
1277
   }
1278

1279
   try {
180✔
1280
      std::vector<Test::Result> final_tests = run_final_tests();
180✔
1281
      results.insert(results.end(), final_tests.begin(), final_tests.end());
180✔
1282
   } catch(std::exception& e) {
180✔
1283
      results.push_back(Test::Result::Failure(header_or_name, "run_final_tests exception " + std::string(e.what())));
×
1284
   }
×
1285

1286
   m_first = true;
180✔
1287

1288
   return results;
180✔
1289
}
180✔
1290

1291
std::map<std::string, std::string> Test_Options::report_properties() const {
1✔
1292
   std::map<std::string, std::string> result;
1✔
1293

1294
   for(const auto& prop : m_report_properties) {
3✔
1295
      const auto colon = prop.find(':');
2✔
1296
      // props without a colon separator or without a name are not allowed
1297
      if(colon == std::string::npos || colon == 0) {
2✔
1298
         throw Test_Error("--report-properties should be of the form <key>:<value>,<key>:<value>,...");
×
1299
      }
1300

1301
      result.insert_or_assign(prop.substr(0, colon), prop.substr(colon + 1, prop.size() - colon - 1));
4✔
1302
   }
1303

1304
   return result;
1✔
1305
}
×
1306

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