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

randombit / botan / 20300409060

17 Dec 2025 10:55AM UTC coverage: 90.526% (+0.2%) from 90.36%
20300409060

Pull #5167

github

web-flow
Merge b37a2cfb3 into 3d96b675e
Pull Request #5167: Changes to reduce unnecessary inclusions

101175 of 111763 relevant lines covered (90.53%)

12570909.12 hits per line

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

79.16
/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/rng.h>
11
#include <botan/internal/filesystem.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/loadstor.h>
14
#include <botan/internal/parsing.h>
15
#include <botan/internal/stl_util.h>
16
#include <botan/internal/target_info.h>
17
#include <chrono>
18
#include <deque>
19
#include <fstream>
20
#include <iomanip>
21
#include <set>
22
#include <sstream>
23
#include <unordered_set>
24

25
#if defined(BOTAN_HAS_BIGINT)
26
   #include <botan/bigint.h>
27
#endif
28

29
#if defined(BOTAN_HAS_CPUID)
30
   #include <botan/internal/cpuid.h>
31
#endif
32

33
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
34
   #include <botan/ec_point.h>
35
#endif
36

37
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
38
   #include <stdlib.h>
39
   #include <unistd.h>
40
#endif
41

42
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
43
   #include <version>
44
   #if defined(__cpp_lib_filesystem)
45
      #include <filesystem>
46
   #endif
47
#endif
48

49
namespace Botan_Tests {
50

51
Test::Test() = default;
412✔
52

53
Test::~Test() = default;
557✔
54

55
Test::Result::Result(std::string who) : m_who(std::move(who)), m_timestamp(Test::timestamp()) {}
74,571✔
56

57
void Test::Result::merge(const Result& other, bool ignore_test_name) {
119,931✔
58
   if(who() != other.who()) {
119,931✔
59
      if(!ignore_test_name) {
73✔
60
         throw Test_Error("Merging tests from different sources");
×
61
      }
62

63
      // When deliberately merging results with different names, the code location is
64
      // likely inconsistent and must be discarded.
65
      m_where.reset();
73✔
66
   } else {
67
      m_where = other.m_where;
119,858✔
68
   }
69

70
   m_timestamp = std::min(m_timestamp, other.m_timestamp);
119,931✔
71
   m_ns_taken += other.m_ns_taken;
119,931✔
72
   m_tests_passed += other.m_tests_passed;
119,931✔
73
   m_fail_log.insert(m_fail_log.end(), other.m_fail_log.begin(), other.m_fail_log.end());
119,931✔
74
   m_log.insert(m_log.end(), other.m_log.begin(), other.m_log.end());
119,931✔
75
}
119,931✔
76

77
void Test::Result::start_timer() {
1,187✔
78
   if(m_started == 0) {
1,187✔
79
      m_started = Test::timestamp();
2,374✔
80
   }
81
}
1,187✔
82

83
void Test::Result::end_timer() {
1,299✔
84
   if(m_started > 0) {
1,299✔
85
      m_ns_taken += Test::timestamp() - m_started;
2,372✔
86
      m_started = 0;
1,186✔
87
   }
88
}
1,299✔
89

90
void Test::Result::test_note(const std::string& note, const char* extra) {
2,766✔
91
   if(!note.empty()) {
2,766✔
92
      std::ostringstream out;
2,766✔
93
      out << who() << " " << note;
2,766✔
94
      if(extra != nullptr) {
2,766✔
95
         out << ": " << extra;
12✔
96
      }
97
      m_log.push_back(out.str());
2,766✔
98
   }
2,766✔
99
}
2,766✔
100

101
void Test::Result::note_missing(const std::string& whatever) {
275✔
102
   static std::set<std::string> s_already_seen;
275✔
103

104
   if(!s_already_seen.contains(whatever)) {
275✔
105
      test_note("Skipping tests due to missing " + whatever);
5✔
106
      s_already_seen.insert(whatever);
5✔
107
   }
108
}
275✔
109

110
bool Test::Result::ThrowExpectations::check(const std::string& test_name, Test::Result& result) {
49,210✔
111
   m_consumed = true;
49,210✔
112

113
   try {
49,210✔
114
      m_fn();
49,210✔
115
      if(!m_expect_success) {
1,646✔
116
         return result.test_failure(test_name + " failed to throw expected exception");
2✔
117
      }
118
   } catch(const std::exception& ex) {
47,564✔
119
      if(m_expect_success) {
47,562✔
120
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
2✔
121
      }
122
      if(m_expected_exception_check_fn && !m_expected_exception_check_fn(std::current_exception())) {
134,505✔
123
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
4✔
124
      }
125
      if(m_expected_message.has_value() && m_expected_message.value() != ex.what()) {
47,559✔
126
         return result.test_failure(test_name + " threw exception with unexpected message (expected: '" +
3✔
127
                                    m_expected_message.value() + "', got: '" + ex.what() + "')");
6✔
128
      }
129
   } catch(...) {
47,564✔
130
      if(m_expect_success || m_expected_exception_check_fn || m_expected_message.has_value()) {
2✔
131
         return result.test_failure(test_name + " threw unexpected unknown exception");
1✔
132
      }
133
   }
2✔
134

135
   return result.test_success(test_name + " behaved as expected");
98,406✔
136
}
137

138
bool Test::Result::test_throws(const std::string& what, const std::function<void()>& fn) {
3,919✔
139
   return ThrowExpectations(fn).check(what, *this);
7,838✔
140
}
141

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

146
bool Test::Result::test_no_throw(const std::string& what, const std::function<void()>& fn) {
1,645✔
147
   return ThrowExpectations(fn).expect_success().check(what, *this);
3,290✔
148
}
149

150
bool Test::Result::test_success(const std::string& note) {
3,519,875✔
151
   if(Test::options().log_success()) {
3,519,743✔
152
      test_note(note);
×
153
   }
154
   ++m_tests_passed;
3,519,875✔
155
   return true;
723,889✔
156
}
157

158
bool Test::Result::test_failure(const std::string& what, const std::string& error) {
1✔
159
   return test_failure(who() + " " + what + " with error " + error);
4✔
160
}
161

162
void Test::Result::test_failure(const std::string& what, const uint8_t buf[], size_t buf_len) {
1✔
163
   test_failure(who() + ": " + what + " buf len " + std::to_string(buf_len) + " value " +
5✔
164
                Botan::hex_encode(buf, buf_len));
1✔
165
}
1✔
166

167
bool Test::Result::test_failure(const std::string& err) {
25✔
168
   m_fail_log.push_back(err);
25✔
169

170
   if(Test::options().abort_on_first_fail() && m_who != "Failing Test") {
25✔
171
      std::abort();
×
172
   }
173
   return false;
25✔
174
}
175

176
namespace {
177

178
bool same_contents(const uint8_t x[], const uint8_t y[], size_t len) {
274,452✔
179
   return (len == 0) ? true : std::memcmp(x, y, len) == 0;
273,081✔
180
}
181

182
}  // namespace
183

184
bool Test::Result::test_ne(const std::string& what,
3,408✔
185
                           const uint8_t produced[],
186
                           size_t produced_len,
187
                           const uint8_t expected[],
188
                           size_t expected_len) {
189
   if(produced_len == expected_len && same_contents(produced, expected, expected_len)) {
3,408✔
190
      return test_failure(who() + ": " + what + " produced matching");
6✔
191
   }
192
   return test_success();
6,812✔
193
}
194

195
bool Test::Result::test_eq(const char* producer,
273,040✔
196
                           const std::string& what,
197
                           const uint8_t produced[],
198
                           size_t produced_size,
199
                           const uint8_t expected[],
200
                           size_t expected_size) {
201
   if(produced_size == expected_size && same_contents(produced, expected, expected_size)) {
273,040✔
202
      return test_success();
546,078✔
203
   }
204

205
   std::ostringstream err;
1✔
206

207
   err << who();
1✔
208

209
   if(producer != nullptr) {
1✔
210
      err << " producer '" << producer << "'";
×
211
   }
212

213
   err << " unexpected result for " << what;
1✔
214

215
   if(produced_size != expected_size) {
1✔
216
      err << " produced " << produced_size << " bytes expected " << expected_size;
1✔
217
   }
218

219
   std::vector<uint8_t> xor_diff(std::min(produced_size, expected_size));
2✔
220
   size_t bytes_different = 0;
1✔
221

222
   for(size_t i = 0; i != xor_diff.size(); ++i) {
4✔
223
      xor_diff[i] = produced[i] ^ expected[i];
3✔
224
      if(xor_diff[i] > 0) {
3✔
225
         bytes_different++;
3✔
226
      }
227
   }
228

229
   err << "\nProduced: " << Botan::hex_encode(produced, produced_size)
1✔
230
       << "\nExpected: " << Botan::hex_encode(expected, expected_size);
3✔
231

232
   if(bytes_different > 0) {
1✔
233
      err << "\nXOR Diff: " << Botan::hex_encode(xor_diff);
1✔
234
   }
235

236
   return test_failure(err.str());
1✔
237
}
1✔
238

239
bool Test::Result::test_is_nonempty(const std::string& what_is_it, const std::string& to_examine) {
34,169✔
240
   if(to_examine.empty()) {
34,169✔
241
      return test_failure(what_is_it + " was empty");
1✔
242
   }
243
   return test_success();
68,336✔
244
}
245

246
bool Test::Result::test_eq(const std::string& what, const std::string& produced, const std::string& expected) {
73,010✔
247
   return test_is_eq(what, produced, expected);
73,010✔
248
}
249

250
bool Test::Result::test_eq(const std::string& what, const char* produced, const char* expected) {
29✔
251
   return test_is_eq(what, std::string(produced), std::string(expected));
29✔
252
}
253

254
bool Test::Result::test_eq(const std::string& what, size_t produced, size_t expected) {
136,303✔
255
   return test_is_eq(what, produced, expected);
136,303✔
256
}
257

258
bool Test::Result::test_eq_sz(const std::string& what, size_t produced, size_t expected) {
45,786✔
259
   return test_is_eq(what, produced, expected);
45,786✔
260
}
261

262
bool Test::Result::test_eq(const std::string& what,
107✔
263
                           const Botan::OctetString& produced,
264
                           const Botan::OctetString& expected) {
265
   std::ostringstream out;
107✔
266
   out << m_who << " " << what;
107✔
267

268
   if(produced == expected) {
107✔
269
      out << " produced expected result " << produced.to_string();
214✔
270
      return test_success(out.str());
107✔
271
   } else {
272
      out << " produced unexpected result '" << produced.to_string() << "' expected '" << expected.to_string() << "'";
×
273
      return test_failure(out.str());
×
274
   }
275
}
107✔
276

277
bool Test::Result::test_lt(const std::string& what, size_t produced, size_t expected) {
5,640✔
278
   if(produced >= expected) {
5,640✔
279
      std::ostringstream err;
1✔
280
      err << m_who << " " << what;
1✔
281
      err << " unexpected result " << produced << " >= " << expected;
1✔
282
      return test_failure(err.str());
1✔
283
   }
1✔
284

285
   return test_success();
11,278✔
286
}
287

288
bool Test::Result::test_lte(const std::string& what, size_t produced, size_t expected) {
1,021,636✔
289
   if(produced > expected) {
1,021,636✔
290
      std::ostringstream err;
1✔
291
      err << m_who << " " << what << " unexpected result " << produced << " > " << expected;
1✔
292
      return test_failure(err.str());
1✔
293
   }
1✔
294

295
   return test_success();
2,043,270✔
296
}
297

298
bool Test::Result::test_gte(const std::string& what, size_t produced, size_t expected) {
1,142,074✔
299
   if(produced < expected) {
1,142,074✔
300
      std::ostringstream err;
1✔
301
      err << m_who;
1✔
302
      err << " " << what;
1✔
303
      err << " unexpected result " << produced << " < " << expected;
1✔
304
      return test_failure(err.str());
1✔
305
   }
1✔
306

307
   return test_success();
2,284,146✔
308
}
309

310
bool Test::Result::test_gt(const std::string& what, size_t produced, size_t expected) {
14,957✔
311
   if(produced <= expected) {
14,957✔
312
      std::ostringstream err;
×
313
      err << m_who;
×
314
      err << " " << what;
×
315
      err << " unexpected result " << produced << " <= " << expected;
×
316
      return test_failure(err.str());
×
317
   }
×
318

319
   return test_success();
29,914✔
320
}
321

322
bool Test::Result::test_ne(const std::string& what, const std::string& str1, const std::string& str2) {
26✔
323
   if(str1 != str2) {
26✔
324
      return test_success(str1 + " != " + str2);
50✔
325
   }
326

327
   return test_failure(who() + " " + what + " produced matching strings " + str1);
4✔
328
}
329

330
bool Test::Result::test_ne(const std::string& what, size_t produced, size_t expected) {
118✔
331
   if(produced != expected) {
118✔
332
      return test_success();
234✔
333
   }
334

335
   std::ostringstream err;
1✔
336
   err << who() << " " << what << " produced " << produced << " unexpected value";
1✔
337
   return test_failure(err.str());
1✔
338
}
1✔
339

340
#if defined(BOTAN_HAS_BIGINT)
341
bool Test::Result::test_eq(const std::string& what, const BigInt& produced, const BigInt& expected) {
252,637✔
342
   return test_is_eq(what, produced, expected);
252,637✔
343
}
344

345
bool Test::Result::test_ne(const std::string& what, const BigInt& produced, const BigInt& expected) {
97✔
346
   if(produced != expected) {
97✔
347
      return test_success();
192✔
348
   }
349

350
   std::ostringstream err;
1✔
351
   err << who() << " " << what << " produced " << produced << " prohibited value";
1✔
352
   return test_failure(err.str());
1✔
353
}
1✔
354
#endif
355

356
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
357
bool Test::Result::test_eq(const std::string& what, const Botan::EC_Point& a, const Botan::EC_Point& b) {
3,248✔
358
   //return test_is_eq(what, a, b);
359
   if(a == b) {
3,248✔
360
      return test_success();
6,496✔
361
   }
362

363
   std::ostringstream err;
×
364
   err << who() << " " << what << " a=(" << a.get_affine_x() << "," << a.get_affine_y() << ")"
×
365
       << " b=(" << b.get_affine_x() << "," << b.get_affine_y();
×
366
   return test_failure(err.str());
×
367
}
×
368
#endif
369

370
bool Test::Result::test_eq(const std::string& what, bool produced, bool expected) {
349,011✔
371
   return test_is_eq(what, produced, expected);
349,011✔
372
}
373

374
bool Test::Result::test_rc_init(const std::string& func, int rc) {
110✔
375
   if(rc == 0) {
110✔
376
      return test_success();
220✔
377
   } else {
378
      std::ostringstream msg;
×
379
      msg << m_who;
×
380
      msg << " " << func;
×
381

382
      // -40 is BOTAN_FFI_ERROR_NOT_IMPLEMENTED
383
      if(rc == -40) {
×
384
         msg << " returned not implemented";
×
385
      } else {
386
         msg << " unexpectedly failed with error code " << rc;
×
387
      }
388

389
      if(rc == -40) {
×
390
         this->test_note(msg.str());
×
391
      } else {
392
         this->test_failure(msg.str());
×
393
      }
394
      return false;
×
395
   }
×
396
}
397

398
bool Test::Result::test_rc(const std::string& func, int expected, int rc) {
382✔
399
   if(expected != rc) {
382✔
400
      std::ostringstream err;
1✔
401
      err << m_who;
1✔
402
      err << " call to " << func << " unexpectedly returned " << rc;
1✔
403
      err << " but expecting " << expected;
1✔
404
      return test_failure(err.str());
1✔
405
   }
1✔
406

407
   return test_success();
762✔
408
}
409

410
void Test::initialize(std::string test_name, CodeLocation location) {
412✔
411
   m_test_name = std::move(test_name);
412✔
412
   m_registration_location = location;
412✔
413
}
412✔
414

415
Botan::RandomNumberGenerator& Test::rng() const {
454,302✔
416
   if(!m_test_rng) {
454,302✔
417
      m_test_rng = Test::new_rng(m_test_name);
145✔
418
   }
419

420
   return *m_test_rng;
454,302✔
421
}
422

423
std::vector<uint8_t> Test::mutate_vec(const std::vector<uint8_t>& v,
83,765✔
424
                                      Botan::RandomNumberGenerator& rng,
425
                                      bool maybe_resize,
426
                                      size_t min_offset) {
427
   std::vector<uint8_t> r = v;
83,765✔
428

429
   if(maybe_resize && (r.empty() || rng.next_byte() < 32)) {
93,693✔
430
      // TODO: occasionally truncate, insert at random index
431
      const size_t add = 1 + (rng.next_byte() % 16);
1,678✔
432
      r.resize(r.size() + add);
1,678✔
433
      rng.randomize(&r[r.size() - add], add);
1,678✔
434
   }
435

436
   if(r.size() > min_offset) {
83,765✔
437
      const size_t offset = std::max<size_t>(min_offset, rng.next_byte() % r.size());
82,343✔
438
      const uint8_t perturb = rng.next_nonzero_byte();
82,343✔
439
      r[offset] ^= perturb;
82,343✔
440
   }
441

442
   return r;
83,765✔
443
}
×
444

445
std::vector<std::string> Test::possible_providers(const std::string& /*alg*/) {
×
446
   return Test::provider_filter({"base"});
×
447
}
448

449
//static
450
std::string Test::format_time(uint64_t nanoseconds) {
1,465✔
451
   std::ostringstream o;
1,465✔
452

453
   if(nanoseconds > 1000000000) {
1,465✔
454
      o << std::setprecision(2) << std::fixed << nanoseconds / 1000000000.0 << " sec";
155✔
455
   } else {
456
      o << std::setprecision(2) << std::fixed << nanoseconds / 1000000.0 << " msec";
1,310✔
457
   }
458

459
   return o.str();
2,930✔
460
}
1,465✔
461

462
Test::Result::Result(std::string who, const std::vector<Result>& downstream_results) : Result(std::move(who)) {
14✔
463
   for(const auto& result : downstream_results) {
82✔
464
      merge(result, true /* ignore non-matching test names */);
68✔
465
   }
466
}
14✔
467

468
// TODO: this should move to `StdoutReporter`
469
std::string Test::Result::result_string() const {
2,533✔
470
   const bool verbose = Test::options().verbose();
2,533✔
471

472
   if(tests_run() == 0 && !verbose) {
2,533✔
473
      return "";
20✔
474
   }
475

476
   std::ostringstream report;
2,513✔
477

478
   report << who() << " ran ";
2,513✔
479

480
   if(tests_run() == 0) {
2,513✔
481
      report << "ZERO";
×
482
   } else {
483
      report << tests_run();
2,513✔
484
   }
485
   report << " tests";
2,513✔
486

487
   if(m_ns_taken > 0) {
2,513✔
488
      report << " in " << format_time(m_ns_taken);
2,928✔
489
   }
490

491
   if(tests_failed() > 0) {
2,513✔
492
      report << " " << tests_failed() << " FAILED";
25✔
493
   } else {
494
      report << " all ok";
2,488✔
495
   }
496

497
   report << "\n";
2,513✔
498

499
   for(size_t i = 0; i != m_fail_log.size(); ++i) {
2,538✔
500
      report << "Failure " << (i + 1) << ": " << m_fail_log[i];
25✔
501
      if(m_where) {
25✔
502
         report << " (at " << m_where->path << ":" << m_where->line << ")";
×
503
      }
504
      report << "\n";
25✔
505
   }
506

507
   if(!m_fail_log.empty() || tests_run() == 0 || verbose) {
2,513✔
508
      for(size_t i = 0; i != m_log.size(); ++i) {
25✔
509
         report << "Note " << (i + 1) << ": " << m_log[i] << "\n";
×
510
      }
511
   }
512

513
   return report.str();
2,513✔
514
}
2,513✔
515

516
namespace {
517

518
class Test_Registry {
519
   public:
520
      static Test_Registry& instance() {
1,254✔
521
         static Test_Registry registry;
1,255✔
522
         return registry;
1,254✔
523
      }
524

525
      void register_test(const std::string& category,
412✔
526
                         const std::string& name,
527
                         bool smoke_test,
528
                         bool needs_serialization,
529
                         std::function<std::unique_ptr<Test>()> maker_fn) {
530
         if(m_tests.contains(name)) {
412✔
531
            throw Test_Error("Duplicate registration of test '" + name + "'");
×
532
         }
533

534
         if(m_tests.contains(category)) {
412✔
535
            throw Test_Error("'" + category + "' cannot be used as category, test exists");
×
536
         }
537

538
         if(m_categories.contains(name)) {
412✔
539
            throw Test_Error("'" + name + "' cannot be used as test name, category exists");
×
540
         }
541

542
         if(smoke_test) {
412✔
543
            m_smoke_tests.push_back(name);
10✔
544
         }
545

546
         if(needs_serialization) {
412✔
547
            m_mutexed_tests.push_back(name);
21✔
548
         }
549

550
         m_tests.emplace(name, std::move(maker_fn));
412✔
551
         m_categories.emplace(category, name);
412✔
552
      }
412✔
553

554
      std::unique_ptr<Test> get_test(const std::string& test_name) const {
412✔
555
         auto i = m_tests.find(test_name);
412✔
556
         if(i != m_tests.end()) {
412✔
557
            return i->second();
412✔
558
         }
559
         return nullptr;
×
560
      }
561

562
      std::vector<std::string> registered_tests() const {
×
563
         std::vector<std::string> s;
×
564
         for(auto&& i : m_tests) {
×
565
            s.push_back(i.first);
×
566
         }
567
         return s;
×
568
      }
×
569

570
      std::vector<std::string> registered_test_categories() const {
×
571
         std::vector<std::string> s;
×
572
         for(auto&& i : m_categories) {
×
573
            s.push_back(i.first);
×
574
         }
575
         return s;
×
576
      }
×
577

578
      std::vector<std::string> filter_registered_tests(const std::vector<std::string>& requested,
1✔
579
                                                       const std::vector<std::string>& to_be_skipped) {
580
         std::vector<std::string> result;
1✔
581

582
         std::set<std::string> to_be_skipped_set(to_be_skipped.begin(), to_be_skipped.end());
1✔
583
         // TODO: this is O(n^2), but we have a relatively small number of tests.
584
         auto insert_if_not_exists_and_not_skipped = [&](const std::string& test_name) {
413✔
585
            if(!Botan::value_exists(result, test_name) && !to_be_skipped_set.contains(test_name)) {
412✔
586
               result.push_back(test_name);
402✔
587
            }
588
         };
413✔
589

590
         if(requested.empty()) {
1✔
591
            /*
592
            If nothing was requested on the command line, run everything. First
593
            run the "essentials" to smoke test, then everything else in
594
            alphabetical order.
595
            */
596
            result = m_smoke_tests;
1✔
597
            for(const auto& [test_name, _] : m_tests) {
413✔
598
               insert_if_not_exists_and_not_skipped(test_name);
412✔
599
            }
600
         } else {
601
            for(const auto& r : requested) {
×
602
               if(m_tests.contains(r)) {
×
603
                  insert_if_not_exists_and_not_skipped(r);
×
604
               } else if(auto elems = m_categories.equal_range(r); elems.first != m_categories.end()) {
×
605
                  for(; elems.first != elems.second; ++elems.first) {
×
606
                     insert_if_not_exists_and_not_skipped(elems.first->second);
×
607
                  }
608
               } else {
609
                  throw Test_Error("Unknown test suite or category: " + r);
×
610
               }
611
            }
612
         }
613

614
         return result;
1✔
615
      }
1✔
616

617
      bool needs_serialization(const std::string& test_name) const {
429✔
618
         return Botan::value_exists(m_mutexed_tests, test_name);
17✔
619
      }
620

621
   private:
622
      Test_Registry() = default;
1✔
623

624
   private:
625
      std::map<std::string, std::function<std::unique_ptr<Test>()>> m_tests;
626
      std::multimap<std::string, std::string> m_categories;
627
      std::vector<std::string> m_smoke_tests;
628
      std::vector<std::string> m_mutexed_tests;
629
};
630

631
}  // namespace
632

633
// static Test:: functions
634

635
//static
636
void Test::register_test(const std::string& category,
412✔
637
                         const std::string& name,
638
                         bool smoke_test,
639
                         bool needs_serialization,
640
                         std::function<std::unique_ptr<Test>()> maker_fn) {
641
   Test_Registry::instance().register_test(category, name, smoke_test, needs_serialization, std::move(maker_fn));
824✔
642
}
412✔
643

644
//static
645
uint64_t Test::timestamp() {
172,880✔
646
   auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
172,880✔
647
   return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
2,373✔
648
}
649

650
//static
651
std::vector<Test::Result> Test::flatten_result_lists(std::vector<std::vector<Test::Result>> result_lists) {
4✔
652
   std::vector<Test::Result> results;
4✔
653
   for(auto& result_list : result_lists) {
26✔
654
      for(auto& result : result_list) {
71✔
655
         results.emplace_back(std::move(result));
49✔
656
      }
657
   }
658
   return results;
4✔
659
}
×
660

661
//static
662
std::vector<std::string> Test::registered_tests() {
×
663
   return Test_Registry::instance().registered_tests();
×
664
}
665

666
//static
667
std::vector<std::string> Test::registered_test_categories() {
×
668
   return Test_Registry::instance().registered_test_categories();
×
669
}
670

671
//static
672
std::unique_ptr<Test> Test::get_test(const std::string& test_name) {
412✔
673
   return Test_Registry::instance().get_test(test_name);
412✔
674
}
675

676
//static
677
bool Test::test_needs_serialization(const std::string& test_name) {
412✔
678
   return Test_Registry::instance().needs_serialization(test_name);
412✔
679
}
680

681
//static
682
std::vector<std::string> Test::filter_registered_tests(const std::vector<std::string>& requested,
1✔
683
                                                       const std::vector<std::string>& to_be_skipped) {
684
   return Test_Registry::instance().filter_registered_tests(requested, to_be_skipped);
1✔
685
}
686

687
//static
688
std::string Test::temp_file_name(const std::string& basename) {
21✔
689
   // TODO add a --tmp-dir option to the tests to specify where these files go
690

691
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
692

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

696
   const int fd = ::mkstemp(mkstemp_basename.data());
21✔
697

698
   // error
699
   if(fd < 0) {
21✔
700
      return "";
×
701
   }
702

703
   ::close(fd);
21✔
704

705
   return mkstemp_basename;
21✔
706
#else
707
   // For now just create the temp in the current working directory
708
   return basename;
709
#endif
710
}
21✔
711

712
bool Test::copy_file(const std::string& from, const std::string& to) {
1✔
713
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) && defined(__cpp_lib_filesystem)
714
   std::error_code ec;  // don't throw, just return false on error
1✔
715
   return std::filesystem::copy_file(from, to, std::filesystem::copy_options::overwrite_existing, ec);
1✔
716
#else
717
   // TODO: implement fallbacks to POSIX or WIN32
718
   // ... but then again: it's 2023 and we're using C++20 :o)
719
   BOTAN_UNUSED(from, to);
720
   throw Botan::No_Filesystem_Access();
721
#endif
722
}
723

724
std::string Test::read_data_file(const std::string& path) {
38✔
725
   const std::string fsname = Test::data_file(path);
38✔
726
   std::ifstream file(fsname.c_str());
38✔
727
   if(!file.good()) {
38✔
728
      throw Test_Error("Error reading from " + fsname);
×
729
   }
730

731
   return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
76✔
732
}
38✔
733

734
std::vector<uint8_t> Test::read_binary_data_file(const std::string& path) {
34✔
735
   const std::string fsname = Test::data_file(path);
34✔
736
   std::ifstream file(fsname.c_str(), std::ios::binary);
34✔
737
   if(!file.good()) {
34✔
738
      throw Test_Error("Error reading from " + fsname);
×
739
   }
740

741
   std::vector<uint8_t> contents;
34✔
742

743
   while(file.good()) {
74✔
744
      std::vector<uint8_t> buf(4096);
40✔
745
      file.read(reinterpret_cast<char*>(buf.data()), buf.size());
40✔
746
      const size_t got = static_cast<size_t>(file.gcount());
40✔
747

748
      if(got == 0 && file.eof()) {
40✔
749
         break;
750
      }
751

752
      contents.insert(contents.end(), buf.data(), buf.data() + got);
40✔
753
   }
40✔
754

755
   return contents;
68✔
756
}
34✔
757

758
// static member variables of Test
759

760
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
761
Test_Options Test::m_opts;
762
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
763
std::string Test::m_test_rng_seed;
764

765
//static
766
void Test::set_test_options(const Test_Options& opts) {
1✔
767
   m_opts = opts;
1✔
768
}
1✔
769

770
namespace {
771

772
/*
773
* This is a fast, simple, deterministic PRNG that's used for running
774
* the tests. It is not intended to be cryptographically secure.
775
*/
776
class Testsuite_RNG final : public Botan::RandomNumberGenerator {
8✔
777
   public:
778
      std::string name() const override { return "Testsuite_RNG"; }
×
779

780
      void clear() override { m_x = 0; }
×
781

782
      bool accepts_input() const override { return true; }
×
783

784
      bool is_seeded() const override { return true; }
276,779✔
785

786
      void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
10,174,560✔
787
         for(const auto byte : input) {
10,174,560✔
788
            mix(byte);
×
789
         }
790

791
         for(auto& byte : output) {
72,586,398✔
792
            byte = mix();
62,411,838✔
793
         }
794
      }
10,174,560✔
795

796
      Testsuite_RNG(std::string_view seed, std::string_view test_name) : m_x(0) {
276✔
797
         for(const char c : seed) {
8,280✔
798
            this->mix(static_cast<uint8_t>(c));
8,004✔
799
         }
800
         for(const char c : test_name) {
5,578✔
801
            this->mix(static_cast<uint8_t>(c));
5,302✔
802
         }
803
      }
276✔
804

805
   private:
806
      uint8_t mix(uint8_t input = 0) {
62,425,144✔
807
         m_x ^= input;
62,425,144✔
808
         m_x *= 0xF2E16957;
62,425,144✔
809
         m_x += 0xE50B590F;
62,425,144✔
810
         return static_cast<uint8_t>(m_x >> 27);
62,425,144✔
811
      }
812

813
      uint64_t m_x;
814
};
815

816
}  // namespace
817

818
//static
819
void Test::set_test_rng_seed(std::span<const uint8_t> seed, size_t epoch) {
1✔
820
   m_test_rng_seed = Botan::fmt("seed={} epoch={}", Botan::hex_encode(seed), epoch);
1✔
821
}
1✔
822

823
//static
824
std::unique_ptr<Botan::RandomNumberGenerator> Test::new_rng(std::string_view test_name) {
268✔
825
   return std::make_unique<Testsuite_RNG>(m_test_rng_seed, test_name);
268✔
826
}
827

828
//static
829
std::shared_ptr<Botan::RandomNumberGenerator> Test::new_shared_rng(std::string_view test_name) {
8✔
830
   return std::make_shared<Testsuite_RNG>(m_test_rng_seed, test_name);
8✔
831
}
832

833
//static
834
std::string Test::data_file(const std::string& file) {
759✔
835
   return options().data_dir() + "/" + file;
1,518✔
836
}
837

838
//static
839
std::string Test::data_dir(const std::string& subdir) {
1✔
840
   return options().data_dir() + "/" + subdir;
2✔
841
}
842

843
//static
844
std::vector<std::string> Test::files_in_data_dir(const std::string& subdir) {
393✔
845
   auto fs = Botan::get_files_recursive(options().data_dir() + "/" + subdir);
1,179✔
846
   if(fs.empty()) {
393✔
847
      throw Test_Error("Test::files_in_data_dir encountered empty subdir " + subdir);
×
848
   }
849
   return fs;
393✔
850
}
×
851

852
//static
853
std::string Test::data_file_as_temporary_copy(const std::string& what) {
1✔
854
   auto tmp_basename = what;
1✔
855
   std::replace(tmp_basename.begin(), tmp_basename.end(), '/', '_');
1✔
856
   auto temp_file = temp_file_name("tmp-" + tmp_basename);
1✔
857
   if(temp_file.empty()) {
1✔
858
      return "";
×
859
   }
860
   if(!Test::copy_file(data_file(what), temp_file)) {
1✔
861
      return "";
×
862
   }
863
   return temp_file;
1✔
864
}
1✔
865

866
//static
867
std::vector<std::string> Test::provider_filter(const std::vector<std::string>& providers) {
48,067✔
868
   if(m_opts.provider().empty()) {
48,067✔
869
      return providers;
48,067✔
870
   }
871
   for(auto&& provider : providers) {
×
872
      if(provider == m_opts.provider()) {
×
873
         return std::vector<std::string>{provider};
×
874
      }
875
   }
876
   return std::vector<std::string>{};
×
877
}
×
878

879
std::string Test::random_password(Botan::RandomNumberGenerator& rng) {
222✔
880
   const size_t len = 1 + rng.next_byte() % 32;
222✔
881
   return Botan::hex_encode(rng.random_vec(len));
444✔
882
}
883

884
size_t Test::random_index(Botan::RandomNumberGenerator& rng, size_t max) {
8,062✔
885
   return Botan::load_be(rng.random_array<8>()) % max;
8,062✔
886
}
887

888
void VarMap::clear() {
33,997✔
889
   m_vars.clear();
×
890
}
×
891

892
namespace {
893

894
bool varmap_pair_lt(const std::pair<std::string, std::string>& kv, const std::string& k) {
1,676,643✔
895
   return kv.first < k;
1,676,643✔
896
}
897

898
}  // namespace
899

900
void VarMap::add(const std::string& key, const std::string& value) {
154,995✔
901
   auto i = std::lower_bound(m_vars.begin(), m_vars.end(), key, varmap_pair_lt);
154,995✔
902

903
   if(i != m_vars.end() && i->first == key) {
154,995✔
904
      i->second = value;
44,401✔
905
   } else {
906
      m_vars.emplace(i, key, value);
110,594✔
907
   }
908
}
154,995✔
909

910
std::optional<std::string> VarMap::get_var(const std::string& key) const {
555,198✔
911
   auto i = std::lower_bound(m_vars.begin(), m_vars.end(), key, varmap_pair_lt);
555,198✔
912

913
   if(i != m_vars.end() && i->first == key) {
555,198✔
914
      return i->second;
485,432✔
915
   } else {
916
      return {};
69,766✔
917
   }
918
}
919

920
bool VarMap::has_key(const std::string& key) const {
212,738✔
921
   return get_var(key).has_value();
212,738✔
922
}
923

924
std::string VarMap::get_req_str(const std::string& key) const {
251,523✔
925
   auto var = get_var(key);
251,523✔
926
   if(var) {
251,523✔
927
      return *var;
503,046✔
928
   } else {
929
      throw Test_Error("Test missing variable " + key);
×
930
   }
931
}
251,523✔
932

933
std::vector<std::vector<uint8_t>> VarMap::get_req_bin_list(const std::string& key) const {
12✔
934
   const auto var = get_req_str(key);
12✔
935

936
   std::vector<std::vector<uint8_t>> bin_list;
12✔
937

938
   for(auto&& part : Botan::split_on(var, ',')) {
62✔
939
      try {
50✔
940
         bin_list.push_back(Botan::hex_decode(part));
100✔
941
      } catch(std::exception& e) {
×
942
         std::ostringstream oss;
×
943
         oss << "Bad input '" << part << "'"
×
944
             << " in binary list key " << key << " - " << e.what();
×
945
         throw Test_Error(oss.str());
×
946
      }
×
947
   }
12✔
948

949
   return bin_list;
12✔
950
}
12✔
951

952
std::vector<uint8_t> VarMap::get_req_bin(const std::string& key) const {
155,623✔
953
   const auto var = get_req_str(key);
155,623✔
954

955
   try {
155,623✔
956
      if(var.starts_with("0x")) {
155,623✔
957
         if(var.size() % 2 == 0) {
×
958
            return Botan::hex_decode(var.substr(2));
×
959
         } else {
960
            std::string z = var;
×
961
            std::swap(z[0], z[1]);  // swap 0x to x0 then remove x
×
962
            return Botan::hex_decode(z.substr(1));
×
963
         }
×
964
      } else {
965
         return Botan::hex_decode(var);
155,623✔
966
      }
967
   } catch(std::exception& e) {
×
968
      std::ostringstream oss;
×
969
      oss << "Bad input '" << var << "'"
×
970
          << " for key " << key << " - " << e.what();
×
971
      throw Test_Error(oss.str());
×
972
   }
×
973
}
155,623✔
974

975
std::string VarMap::get_opt_str(const std::string& key, const std::string& def_value) const {
14,376✔
976
   if(auto v = get_var(key)) {
14,376✔
977
      return *v;
55✔
978
   } else {
979
      return def_value;
28,697✔
980
   }
14,376✔
981
}
982

983
bool VarMap::get_req_bool(const std::string& key) const {
39✔
984
   const auto var = get_req_str(key);
39✔
985

986
   if(var == "true") {
39✔
987
      return true;
988
   } else if(var == "false") {
23✔
989
      return false;
990
   } else {
991
      throw Test_Error("Invalid boolean for key '" + key + "' value '" + var + "'");
×
992
   }
993
}
39✔
994

995
size_t VarMap::get_req_sz(const std::string& key) const {
4,547✔
996
   return Botan::to_u32bit(get_req_str(key));
4,547✔
997
}
998

999
uint8_t VarMap::get_req_u8(const std::string& key) const {
17✔
1000
   const size_t s = this->get_req_sz(key);
17✔
1001
   if(s > 256) {
17✔
1002
      throw Test_Error("Invalid " + key + " expected uint8_t got " + std::to_string(s));
×
1003
   }
1004
   return static_cast<uint8_t>(s);
17✔
1005
}
1006

1007
uint32_t VarMap::get_req_u32(const std::string& key) const {
14✔
1008
   return static_cast<uint32_t>(get_req_sz(key));
14✔
1009
}
1010

1011
uint64_t VarMap::get_req_u64(const std::string& key) const {
17✔
1012
   const auto var = get_req_str(key);
17✔
1013
   try {
17✔
1014
      return std::stoull(var);
17✔
1015
   } catch(std::exception&) {
×
1016
      throw Test_Error("Invalid u64 value '" + var + "'");
×
1017
   }
×
1018
}
17✔
1019

1020
size_t VarMap::get_opt_sz(const std::string& key, const size_t def_value) const {
31,379✔
1021
   if(auto v = get_var(key)) {
31,379✔
1022
      return Botan::to_u32bit(*v);
12,244✔
1023
   } else {
1024
      return def_value;
1025
   }
31,379✔
1026
}
1027

1028
uint64_t VarMap::get_opt_u64(const std::string& key, const uint64_t def_value) const {
3,538✔
1029
   if(auto v = get_var(key)) {
3,538✔
1030
      try {
641✔
1031
         return std::stoull(*v);
3,538✔
1032
      } catch(std::exception&) {
×
1033
         throw Test_Error("Invalid u64 value '" + *v + "'");
×
1034
      }
×
1035
   } else {
1036
      return def_value;
1037
   }
3,538✔
1038
}
1039

1040
std::vector<uint8_t> VarMap::get_opt_bin(const std::string& key) const {
41,564✔
1041
   if(auto v = get_var(key)) {
41,564✔
1042
      try {
11,919✔
1043
         return Botan::hex_decode(*v);
11,919✔
1044
      } catch(std::exception&) {
×
1045
         throw Test_Error("Test invalid hex input '" + *v + "'" + +" for key " + key);
×
1046
      }
×
1047
   } else {
1048
      return {};
29,645✔
1049
   };
41,564✔
1050
}
1051

1052
#if defined(BOTAN_HAS_BIGINT)
1053
Botan::BigInt VarMap::get_req_bn(const std::string& key) const {
38,502✔
1054
   const auto var = get_req_str(key);
38,502✔
1055

1056
   try {
38,502✔
1057
      return Botan::BigInt(var);
38,502✔
1058
   } catch(std::exception&) {
×
1059
      throw Test_Error("Test invalid bigint input '" + var + "' for key " + key);
×
1060
   }
×
1061
}
38,502✔
1062

1063
Botan::BigInt VarMap::get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const {
80✔
1064
   if(auto v = get_var(key)) {
80✔
1065
      try {
56✔
1066
         return Botan::BigInt(*v);
56✔
1067
      } catch(std::exception&) {
×
1068
         throw Test_Error("Test invalid bigint input '" + *v + "' for key " + key);
×
1069
      }
×
1070
   } else {
1071
      return def_value;
24✔
1072
   }
80✔
1073
}
1074
#endif
1075

1076
class Text_Based_Test::Text_Based_Test_Data {
1077
   public:
1078
      Text_Based_Test_Data(const std::string& data_src,
180✔
1079
                           const std::string& required_keys_str,
1080
                           const std::string& optional_keys_str) :
180✔
1081
            m_data_src(data_src) {
360✔
1082
         if(required_keys_str.empty()) {
180✔
1083
            throw Test_Error("Invalid test spec");
×
1084
         }
1085

1086
         m_required_keys = Botan::split_on(required_keys_str, ',');
180✔
1087
         std::vector<std::string> optional_keys = Botan::split_on(optional_keys_str, ',');
180✔
1088

1089
         m_all_keys.insert(m_required_keys.begin(), m_required_keys.end());
180✔
1090
         m_all_keys.insert(optional_keys.begin(), optional_keys.end());
180✔
1091
         m_output_key = m_required_keys.at(m_required_keys.size() - 1);
180✔
1092
      }
180✔
1093

1094
      std::string get_next_line();
1095

1096
      const std::string& current_source_name() const { return m_cur_src_name; }
×
1097

1098
      bool known_key(const std::string& key) const;
1099

1100
      const std::vector<std::string>& required_keys() const { return m_required_keys; }
48,104✔
1101

1102
      const std::string& output_key() const { return m_output_key; }
154,995✔
1103

1104
      const std::vector<std::string>& cpu_flags() const { return m_cpu_flags; }
47,965✔
1105

1106
      void set_cpu_flags(std::vector<std::string> flags) { m_cpu_flags = std::move(flags); }
17✔
1107

1108
      const std::string& initial_data_src_name() const { return m_data_src; }
180✔
1109

1110
   private:
1111
      std::string m_data_src;
1112
      std::vector<std::string> m_required_keys;
1113
      std::unordered_set<std::string> m_all_keys;
1114
      std::string m_output_key;
1115

1116
      bool m_first = true;
1117
      std::unique_ptr<std::istream> m_cur;
1118
      std::string m_cur_src_name;
1119
      std::deque<std::string> m_srcs;
1120
      std::vector<std::string> m_cpu_flags;
1121
};
1122

1123
Text_Based_Test::Text_Based_Test(const std::string& data_src,
180✔
1124
                                 const std::string& required_keys,
1125
                                 const std::string& optional_keys) :
180✔
1126
      m_data(std::make_unique<Text_Based_Test_Data>(data_src, required_keys, optional_keys)) {}
180✔
1127

1128
Text_Based_Test::~Text_Based_Test() = default;
180✔
1129

1130
std::string Text_Based_Test::Text_Based_Test_Data::get_next_line() {
155,996✔
1131
   while(true) {
156,253✔
1132
      if(m_cur == nullptr || m_cur->good() == false) {
156,253✔
1133
         if(m_srcs.empty()) {
448✔
1134
            if(m_first) {
360✔
1135
               if(m_data_src.ends_with(".vec")) {
180✔
1136
                  m_srcs.push_back(Test::data_file(m_data_src));
336✔
1137
               } else {
1138
                  const auto fs = Test::files_in_data_dir(m_data_src);
12✔
1139
                  m_srcs.assign(fs.begin(), fs.end());
12✔
1140
                  if(m_srcs.empty()) {
12✔
1141
                     throw Test_Error("Error reading test data dir " + m_data_src);
×
1142
                  }
1143
               }
12✔
1144

1145
               m_first = false;
180✔
1146
            } else {
1147
               return "";  // done
180✔
1148
            }
1149
         }
1150

1151
         m_cur = std::make_unique<std::ifstream>(m_srcs[0]);
356✔
1152
         m_cur_src_name = m_srcs[0];
268✔
1153

1154
#if defined(BOTAN_HAS_CPUID)
1155
         // Reinit cpuid on new file if needed
1156
         if(m_cpu_flags.empty() == false) {
268✔
1157
            m_cpu_flags.clear();
15✔
1158
            Botan::CPUID::initialize();
15✔
1159
         }
1160
#endif
1161

1162
         if(!m_cur->good()) {
268✔
1163
            throw Test_Error("Could not open input file '" + m_cur_src_name);
×
1164
         }
1165

1166
         m_srcs.pop_front();
268✔
1167
      }
1168

1169
      while(m_cur->good()) {
226,207✔
1170
         std::string line;
225,950✔
1171
         std::getline(*m_cur, line);
225,950✔
1172

1173
         if(line.empty()) {
225,950✔
1174
            continue;
55,013✔
1175
         }
1176

1177
         if(line[0] == '#') {
170,937✔
1178
            if(line.starts_with("#test ")) {
15,138✔
1179
               return line;
17✔
1180
            } else {
1181
               continue;
15,121✔
1182
            }
1183
         }
1184

1185
         return line;
155,799✔
1186
      }
225,950✔
1187
   }
1188
}
1189

1190
bool Text_Based_Test::Text_Based_Test_Data::known_key(const std::string& key) const {
154,995✔
1191
   return m_all_keys.contains(key);
154,995✔
1192
}
1193

1194
namespace {
1195

1196
// strips leading and trailing but not internal whitespace
1197
std::string strip_ws(const std::string& in) {
309,990✔
1198
   const char* whitespace = " ";
309,990✔
1199

1200
   const auto first_c = in.find_first_not_of(whitespace);
309,990✔
1201
   if(first_c == std::string::npos) {
309,990✔
1202
      return "";
1,033✔
1203
   }
1204

1205
   const auto last_c = in.find_last_not_of(whitespace);
308,957✔
1206

1207
   return in.substr(first_c, last_c - first_c + 1);
308,957✔
1208
}
1209

1210
std::vector<std::string> parse_cpuid_bits(const std::vector<std::string>& tok) {
17✔
1211
   std::vector<std::string> bits;
17✔
1212

1213
#if defined(BOTAN_HAS_CPUID)
1214
   for(size_t i = 1; i < tok.size(); ++i) {
82✔
1215
      if(auto bit = Botan::CPUID::bit_from_string(tok[i])) {
65✔
1216
         bits.push_back(bit->to_string());
78✔
1217
      }
1218
   }
1219
#else
1220
   BOTAN_UNUSED(tok);
1221
#endif
1222

1223
   return bits;
17✔
1224
}
×
1225

1226
}  // namespace
1227

1228
bool Text_Based_Test::skip_this_test(const std::string& /*header*/, const VarMap& /*vars*/) {
32,325✔
1229
   return false;
32,325✔
1230
}
1231

1232
std::vector<Test::Result> Text_Based_Test::run() {
180✔
1233
   std::vector<Test::Result> results;
180✔
1234

1235
   std::string header;
180✔
1236
   std::string header_or_name = m_data->initial_data_src_name();
180✔
1237
   VarMap vars;
180✔
1238
   size_t test_cnt = 0;
180✔
1239

1240
   while(true) {
155,996✔
1241
      const std::string line = m_data->get_next_line();
155,996✔
1242
      if(line.empty()) {
155,996✔
1243
         // EOF
1244
         break;
1245
      }
1246

1247
      if(line.starts_with("#test ")) {
155,816✔
1248
         std::vector<std::string> pragma_tokens = Botan::split_on(line.substr(6), ' ');
17✔
1249

1250
         if(pragma_tokens.empty()) {
17✔
1251
            throw Test_Error("Empty pragma found in " + m_data->current_source_name());
×
1252
         }
1253

1254
         if(pragma_tokens[0] != "cpuid") {
17✔
1255
            throw Test_Error("Unknown test pragma '" + line + "' in " + m_data->current_source_name());
×
1256
         }
1257

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

1262
         m_data->set_cpu_flags(parse_cpuid_bits(pragma_tokens));
34✔
1263

1264
         continue;
17✔
1265
      } else if(line[0] == '#') {
155,816✔
1266
         throw Test_Error("Unknown test pragma '" + line + "' in " + m_data->current_source_name());
×
1267
      }
1268

1269
      if(line[0] == '[' && line[line.size() - 1] == ']') {
155,799✔
1270
         header = line.substr(1, line.size() - 2);
804✔
1271
         header_or_name = header;
804✔
1272
         test_cnt = 0;
804✔
1273
         vars.clear();
804✔
1274
         continue;
804✔
1275
      }
1276

1277
      const std::string test_id = "test " + std::to_string(test_cnt);
309,990✔
1278

1279
      auto equal_i = line.find_first_of('=');
154,995✔
1280

1281
      if(equal_i == std::string::npos) {
154,995✔
1282
         results.push_back(Test::Result::Failure(header_or_name, "invalid input '" + line + "'"));
×
1283
         continue;
×
1284
      }
1285

1286
      const std::string key = strip_ws(std::string(line.begin(), line.begin() + equal_i - 1));
309,990✔
1287
      const std::string val = strip_ws(std::string(line.begin() + equal_i + 1, line.end()));
309,990✔
1288

1289
      if(!m_data->known_key(key)) {
154,995✔
1290
         auto r = Test::Result::Failure(header_or_name, Botan::fmt("{} failed unknown key {}", test_id, key));
×
1291
         results.push_back(r);
×
1292
      }
×
1293

1294
      vars.add(key, val);
154,995✔
1295

1296
      if(key == m_data->output_key()) {
154,995✔
1297
         try {
48,104✔
1298
            for(const auto& req_key : m_data->required_keys()) {
244,434✔
1299
               if(!vars.has_key(req_key)) {
196,330✔
1300
                  auto r =
×
1301
                     Test::Result::Failure(header_or_name, Botan::fmt("{} missing required key {}", test_id, req_key));
×
1302
                  results.push_back(r);
×
1303
               }
×
1304
            }
1305

1306
            if(skip_this_test(header, vars)) {
48,104✔
1307
               continue;
139✔
1308
            }
1309

1310
            ++test_cnt;
47,965✔
1311

1312
            const uint64_t start = Test::timestamp();
47,965✔
1313

1314
            Test::Result result = run_one_test(header, vars);
47,965✔
1315
#if defined(BOTAN_HAS_CPUID)
1316
            if(!m_data->cpu_flags().empty()) {
47,965✔
1317
               for(const auto& cpuid_str : m_data->cpu_flags()) {
23,221✔
1318
                  if(const auto bit = Botan::CPUID::Feature::from_string(cpuid_str)) {
16,559✔
1319
                     if(Botan::CPUID::has(*bit)) {
16,559✔
1320
                        Botan::CPUID::clear_cpuid_bit(*bit);
14,035✔
1321
                        // now re-run the test
1322
                        result.merge(run_one_test(header, vars));
14,035✔
1323
                     }
1324
                  }
1325
               }
1326
               Botan::CPUID::initialize();
6,662✔
1327
            }
1328
#endif
1329
            result.set_ns_consumed(Test::timestamp() - start);
47,965✔
1330

1331
            if(result.tests_failed() > 0) {
47,965✔
1332
               std::ostringstream oss;
×
1333
               oss << "Test # " << test_cnt << " ";
×
1334
               if(!header.empty()) {
×
1335
                  oss << header << " ";
×
1336
               }
1337
               oss << "failed ";
×
1338

1339
               for(const auto& k : m_data->required_keys()) {
×
1340
                  oss << k << "=" << vars.get_req_str(k) << " ";
×
1341
               }
1342

1343
               result.test_note(oss.str());
×
1344
            }
×
1345
            results.push_back(result);
47,965✔
1346
         } catch(std::exception& e) {
47,965✔
1347
            std::ostringstream oss;
×
1348
            oss << "Test # " << test_cnt << " ";
×
1349
            if(!header.empty()) {
×
1350
               oss << header << " ";
×
1351
            }
1352

1353
            for(const auto& k : m_data->required_keys()) {
×
1354
               oss << k << "=" << vars.get_req_str(k) << " ";
×
1355
            }
1356

1357
            oss << "failed with exception '" << e.what() << "'";
×
1358

1359
            results.push_back(Test::Result::Failure(header_or_name, oss.str()));
×
1360
         }
×
1361

1362
         if(clear_between_callbacks()) {
47,965✔
1363
            vars.clear();
188,049✔
1364
         }
1365
      }
1366
   }
156,094✔
1367

1368
   if(results.empty()) {
180✔
1369
      return results;
1370
   }
1371

1372
   try {
180✔
1373
      std::vector<Test::Result> final_tests = run_final_tests();
180✔
1374
      results.insert(results.end(), final_tests.begin(), final_tests.end());
180✔
1375
   } catch(std::exception& e) {
180✔
1376
      results.push_back(Test::Result::Failure(header_or_name, "run_final_tests exception " + std::string(e.what())));
×
1377
   }
×
1378

1379
   return results;
1380
}
180✔
1381

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