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

randombit / botan / 21473889113

29 Jan 2026 10:04AM UTC coverage: 90.073% (-0.004%) from 90.077%
21473889113

Pull #5272

github

web-flow
Merge c2b23fb84 into 0d718b146
Pull Request #5272: Fix test categories display in botan-test --help

102103 of 113356 relevant lines covered (90.07%)

11616438.55 hits per line

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

79.17
/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
#if defined(BOTAN_HAS_ECC_GROUP)
50
   #include <botan/ec_group.h>
51
#endif
52

53
namespace Botan_Tests {
54

55
Test::Test() = default;
418✔
56

57
Test::~Test() = default;
563✔
58

59
Test::Result::Result(std::string who) : m_who(std::move(who)), m_timestamp(Test::timestamp()) {}
81,083✔
60

61
void Test::Result::merge(const Result& other, bool ignore_test_name) {
126,701✔
62
   if(who() != other.who()) {
126,701✔
63
      if(!ignore_test_name) {
73✔
64
         throw Test_Error("Merging tests from different sources");
×
65
      }
66

67
      // When deliberately merging results with different names, the code location is
68
      // likely inconsistent and must be discarded.
69
      m_where.reset();
73✔
70
   } else {
71
      m_where = other.m_where;
126,628✔
72
   }
73

74
   m_timestamp = std::min(m_timestamp, other.m_timestamp);
126,701✔
75
   m_ns_taken += other.m_ns_taken;
126,701✔
76
   m_tests_passed += other.m_tests_passed;
126,701✔
77
   m_fail_log.insert(m_fail_log.end(), other.m_fail_log.begin(), other.m_fail_log.end());
126,701✔
78
   m_log.insert(m_log.end(), other.m_log.begin(), other.m_log.end());
126,701✔
79
}
126,701✔
80

81
void Test::Result::start_timer() {
1,192✔
82
   if(m_started == 0) {
1,192✔
83
      m_started = Test::timestamp();
2,384✔
84
   }
85
}
1,192✔
86

87
void Test::Result::end_timer() {
1,304✔
88
   if(m_started > 0) {
1,304✔
89
      m_ns_taken += Test::timestamp() - m_started;
2,382✔
90
      m_started = 0;
1,191✔
91
   }
92
}
1,304✔
93

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

105
void Test::Result::note_missing(const std::string& whatever) {
275✔
106
   static std::set<std::string> s_already_seen;
275✔
107

108
   if(!s_already_seen.contains(whatever)) {
275✔
109
      test_note("Skipping tests due to missing " + whatever);
5✔
110
      s_already_seen.insert(whatever);
5✔
111
   }
112
}
275✔
113

114
bool Test::Result::ThrowExpectations::check(const std::string& test_name, Test::Result& result) {
67,674✔
115
   m_consumed = true;
67,674✔
116

117
   try {
67,674✔
118
      m_fn();
67,674✔
119
      if(!m_expect_success) {
1,646✔
120
         return result.test_failure(test_name + " failed to throw expected exception");
2✔
121
      }
122
   } catch(const std::exception& ex) {
66,028✔
123
      if(m_expect_success) {
66,026✔
124
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
2✔
125
      }
126
      if(m_expected_exception_check_fn && !m_expected_exception_check_fn(std::current_exception())) {
189,897✔
127
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
4✔
128
      }
129
      if(m_expected_message.has_value() && m_expected_message.value() != ex.what()) {
66,023✔
130
         return result.test_failure(test_name + " threw exception with unexpected message (expected: '" +
3✔
131
                                    m_expected_message.value() + "', got: '" + ex.what() + "')");
6✔
132
      }
133
   } catch(...) {
66,028✔
134
      if(m_expect_success || m_expected_exception_check_fn || m_expected_message.has_value()) {
2✔
135
         return result.test_failure(test_name + " threw unexpected unknown exception");
1✔
136
      }
137
   }
2✔
138

139
   return result.test_success(test_name + " behaved as expected");
135,334✔
140
}
141

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

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

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

154
bool Test::Result::test_success(const std::string& note) {
3,619,813✔
155
   if(Test::options().log_success()) {
3,619,681✔
156
      test_note(note);
×
157
   }
158
   ++m_tests_passed;
3,619,813✔
159
   return true;
775,453✔
160
}
161

162
bool Test::Result::test_failure(const std::string& what, const std::string& error) {
1✔
163
   return test_failure(who() + " " + what + " with error " + error);
4✔
164
}
165

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

171
bool Test::Result::test_failure(const std::string& err) {
25✔
172
   m_fail_log.push_back(err);
25✔
173

174
   if(Test::options().abort_on_first_fail() && m_who != "Failing Test") {
25✔
175
      std::abort();
×
176
   }
177
   return false;
25✔
178
}
179

180
namespace {
181

182
bool same_contents(const uint8_t x[], const uint8_t y[], size_t len) {
292,210✔
183
   return (len == 0) ? true : std::memcmp(x, y, len) == 0;
290,779✔
184
}
185

186
}  // namespace
187

188
bool Test::Result::test_ne(const std::string& what,
3,401✔
189
                           const uint8_t produced[],
190
                           size_t produced_len,
191
                           const uint8_t expected[],
192
                           size_t expected_len) {
193
   if(produced_len == expected_len && same_contents(produced, expected, expected_len)) {
3,401✔
194
      return test_failure(who() + ": " + what + " produced matching");
6✔
195
   }
196
   return test_success();
6,798✔
197
}
198

199
bool Test::Result::test_eq(const char* producer,
290,782✔
200
                           const std::string& what,
201
                           const uint8_t produced[],
202
                           size_t produced_size,
203
                           const uint8_t expected[],
204
                           size_t expected_size) {
205
   if(produced_size == expected_size && same_contents(produced, expected, expected_size)) {
290,782✔
206
      return test_success();
581,562✔
207
   }
208

209
   std::ostringstream err;
1✔
210

211
   err << who();
1✔
212

213
   if(producer != nullptr) {
1✔
214
      err << " producer '" << producer << "'";
×
215
   }
216

217
   err << " unexpected result for " << what;
1✔
218

219
   if(produced_size != expected_size) {
1✔
220
      err << " produced " << produced_size << " bytes expected " << expected_size;
1✔
221
   }
222

223
   std::vector<uint8_t> xor_diff(std::min(produced_size, expected_size));
2✔
224
   size_t bytes_different = 0;
1✔
225

226
   for(size_t i = 0; i != xor_diff.size(); ++i) {
4✔
227
      xor_diff[i] = produced[i] ^ expected[i];
3✔
228
      if(xor_diff[i] > 0) {
3✔
229
         bytes_different++;
3✔
230
      }
231
   }
232

233
   err << "\nProduced: " << Botan::hex_encode(produced, produced_size)
1✔
234
       << "\nExpected: " << Botan::hex_encode(expected, expected_size);
3✔
235

236
   if(bytes_different > 0) {
1✔
237
      err << "\nXOR Diff: " << Botan::hex_encode(xor_diff);
1✔
238
   }
239

240
   return test_failure(err.str());
1✔
241
}
1✔
242

243
bool Test::Result::test_is_nonempty(const std::string& what_is_it, const std::string& to_examine) {
38,813✔
244
   if(to_examine.empty()) {
38,813✔
245
      return test_failure(what_is_it + " was empty");
1✔
246
   }
247
   return test_success();
77,624✔
248
}
249

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

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

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

262
bool Test::Result::test_eq_sz(const std::string& what, size_t produced, size_t expected) {
45,789✔
263
   return test_is_eq(what, produced, expected);
45,789✔
264
}
265

266
bool Test::Result::test_eq(const std::string& what,
107✔
267
                           const Botan::OctetString& produced,
268
                           const Botan::OctetString& expected) {
269
   std::ostringstream out;
107✔
270
   out << m_who << " " << what;
107✔
271

272
   if(produced == expected) {
107✔
273
      out << " produced expected result " << produced.to_string();
214✔
274
      return test_success(out.str());
107✔
275
   } else {
276
      out << " produced unexpected result '" << produced.to_string() << "' expected '" << expected.to_string() << "'";
×
277
      return test_failure(out.str());
×
278
   }
279
}
107✔
280

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

289
   return test_success();
11,276✔
290
}
291

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

299
   return test_success();
2,043,270✔
300
}
301

302
bool Test::Result::test_gte(const std::string& what, size_t produced, size_t expected) {
1,142,430✔
303
   if(produced < expected) {
1,142,430✔
304
      std::ostringstream err;
1✔
305
      err << m_who;
1✔
306
      err << " " << what;
1✔
307
      err << " unexpected result " << produced << " < " << expected;
1✔
308
      return test_failure(err.str());
1✔
309
   }
1✔
310

311
   return test_success();
2,284,858✔
312
}
313

314
bool Test::Result::test_gt(const std::string& what, size_t produced, size_t expected) {
22,341✔
315
   if(produced <= expected) {
22,341✔
316
      std::ostringstream err;
×
317
      err << m_who;
×
318
      err << " " << what;
×
319
      err << " unexpected result " << produced << " <= " << expected;
×
320
      return test_failure(err.str());
×
321
   }
×
322

323
   return test_success();
44,682✔
324
}
325

326
bool Test::Result::test_ne(const std::string& what, const std::string& str1, const std::string& str2) {
26✔
327
   if(str1 != str2) {
26✔
328
      return test_success(str1 + " != " + str2);
50✔
329
   }
330

331
   return test_failure(who() + " " + what + " produced matching strings " + str1);
4✔
332
}
333

334
bool Test::Result::test_ne(const std::string& what, size_t produced, size_t expected) {
119✔
335
   if(produced != expected) {
119✔
336
      return test_success();
236✔
337
   }
338

339
   std::ostringstream err;
1✔
340
   err << who() << " " << what << " produced " << produced << " unexpected value";
1✔
341
   return test_failure(err.str());
1✔
342
}
1✔
343

344
#if defined(BOTAN_HAS_BIGINT)
345
bool Test::Result::test_eq(const std::string& what, const BigInt& produced, const BigInt& expected) {
252,639✔
346
   return test_is_eq(what, produced, expected);
252,639✔
347
}
348

349
bool Test::Result::test_ne(const std::string& what, const BigInt& produced, const BigInt& expected) {
97✔
350
   if(produced != expected) {
97✔
351
      return test_success();
192✔
352
   }
353

354
   std::ostringstream err;
1✔
355
   err << who() << " " << what << " produced " << produced << " prohibited value";
1✔
356
   return test_failure(err.str());
1✔
357
}
1✔
358
#endif
359

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

367
   std::ostringstream err;
×
368
   err << who() << " " << what << " a=(" << a.get_affine_x() << "," << a.get_affine_y() << ")"
×
369
       << " b=(" << b.get_affine_x() << "," << b.get_affine_y();
×
370
   return test_failure(err.str());
×
371
}
×
372
#endif
373

374
bool Test::Result::test_eq(const std::string& what, bool produced, bool expected) {
378,344✔
375
   return test_is_eq(what, produced, expected);
378,344✔
376
}
377

378
bool Test::Result::test_rc_init(const std::string& func, int rc) {
117✔
379
   if(rc == 0) {
117✔
380
      return test_success();
234✔
381
   } else {
382
      std::ostringstream msg;
×
383
      msg << m_who;
×
384
      msg << " " << func;
×
385

386
      // -40 is BOTAN_FFI_ERROR_NOT_IMPLEMENTED
387
      if(rc == -40) {
×
388
         msg << " returned not implemented";
×
389
      } else {
390
         msg << " unexpectedly failed with error code " << rc;
×
391
      }
392

393
      if(rc == -40) {
×
394
         this->test_note(msg.str());
×
395
      } else {
396
         this->test_failure(msg.str());
×
397
      }
398
      return false;
×
399
   }
×
400
}
401

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

411
   return test_success();
830✔
412
}
413

414
void Test::initialize(std::string test_name, CodeLocation location) {
418✔
415
   m_test_name = std::move(test_name);
418✔
416
   m_registration_location = location;
418✔
417
}
418✔
418

419
Botan::RandomNumberGenerator& Test::rng() const {
460,888✔
420
   if(!m_test_rng) {
460,888✔
421
      m_test_rng = Test::new_rng(m_test_name);
145✔
422
   }
423

424
   return *m_test_rng;
460,888✔
425
}
426

427
std::optional<std::string> Test::supported_ec_group_name(std::vector<std::string> preferred_groups) {
151✔
428
#if defined(BOTAN_HAS_ECC_GROUP)
429
   if(preferred_groups.empty()) {
151✔
430
      preferred_groups = {
149✔
431
         "secp256r1",
432
         "brainpool256r1",
433
         "secp384r1",
434
         "brainpool384r1",
435
         "secp521r1",
436
         "brainpool512r1",
437
      };
1,192✔
438
   }
439

440
   for(const auto& group : preferred_groups) {
151✔
441
      if(Botan::EC_Group::supports_named_group(group)) {
151✔
442
         return group;
151✔
443
      }
444
   }
445
#else
446
   BOTAN_UNUSED(preferred_groups);
447
#endif
448

449
   return std::nullopt;
×
450
}
298✔
451

452
std::vector<uint8_t> Test::mutate_vec(const std::vector<uint8_t>& v,
96,686✔
453
                                      Botan::RandomNumberGenerator& rng,
454
                                      bool maybe_resize,
455
                                      size_t min_offset) {
456
   std::vector<uint8_t> r = v;
96,686✔
457

458
   if(maybe_resize && (r.empty() || rng.next_byte() < 32)) {
110,248✔
459
      // TODO: occasionally truncate, insert at random index
460
      const size_t add = 1 + (rng.next_byte() % 16);
2,180✔
461
      r.resize(r.size() + add);
2,180✔
462
      rng.randomize(&r[r.size() - add], add);
2,180✔
463
   }
464

465
   if(r.size() > min_offset) {
96,686✔
466
      const size_t offset = std::max<size_t>(min_offset, rng.next_byte() % r.size());
95,148✔
467
      const uint8_t perturb = rng.next_nonzero_byte();
95,148✔
468
      r[offset] ^= perturb;
95,148✔
469
   }
470

471
   return r;
96,686✔
472
}
×
473

474
std::vector<std::string> Test::possible_providers(const std::string& /*alg*/) {
×
475
   return Test::provider_filter({"base"});
×
476
}
477

478
//static
479
std::string Test::format_time(uint64_t nanoseconds) {
1,469✔
480
   std::ostringstream o;
1,469✔
481

482
   if(nanoseconds > 1000000000) {
1,469✔
483
      o << std::setprecision(2) << std::fixed << nanoseconds / 1000000000.0 << " sec";
154✔
484
   } else {
485
      o << std::setprecision(2) << std::fixed << nanoseconds / 1000000.0 << " msec";
1,315✔
486
   }
487

488
   return o.str();
2,938✔
489
}
1,469✔
490

491
Test::Result::Result(std::string who, const std::vector<Result>& downstream_results) : Result(std::move(who)) {
14✔
492
   for(const auto& result : downstream_results) {
82✔
493
      merge(result, true /* ignore non-matching test names */);
68✔
494
   }
495
}
14✔
496

497
// TODO: this should move to `StdoutReporter`
498
std::string Test::Result::result_string() const {
2,540✔
499
   const bool verbose = Test::options().verbose();
2,540✔
500

501
   if(tests_run() == 0 && !verbose) {
2,540✔
502
      return "";
20✔
503
   }
504

505
   std::ostringstream report;
2,520✔
506

507
   report << who() << " ran ";
2,520✔
508

509
   if(tests_run() == 0) {
2,520✔
510
      report << "ZERO";
×
511
   } else {
512
      report << tests_run();
2,520✔
513
   }
514
   report << " tests";
2,520✔
515

516
   if(m_ns_taken > 0) {
2,520✔
517
      report << " in " << format_time(m_ns_taken);
2,936✔
518
   }
519

520
   if(tests_failed() > 0) {
2,520✔
521
      report << " " << tests_failed() << " FAILED";
25✔
522
   } else {
523
      report << " all ok";
2,495✔
524
   }
525

526
   report << "\n";
2,520✔
527

528
   for(size_t i = 0; i != m_fail_log.size(); ++i) {
2,545✔
529
      report << "Failure " << (i + 1) << ": " << m_fail_log[i];
25✔
530
      if(m_where) {
25✔
531
         report << " (at " << m_where->path << ":" << m_where->line << ")";
×
532
      }
533
      report << "\n";
25✔
534
   }
535

536
   if(!m_fail_log.empty() || tests_run() == 0 || verbose) {
2,520✔
537
      for(size_t i = 0; i != m_log.size(); ++i) {
25✔
538
         report << "Note " << (i + 1) << ": " << m_log[i] << "\n";
×
539
      }
540
   }
541

542
   return report.str();
2,520✔
543
}
2,520✔
544

545
namespace {
546

547
class Test_Registry {
548
   public:
549
      static Test_Registry& instance() {
1,276✔
550
         static Test_Registry registry;
1,277✔
551
         return registry;
1,276✔
552
      }
553

554
      void register_test(const std::string& category,
418✔
555
                         const std::string& name,
556
                         bool smoke_test,
557
                         bool needs_serialization,
558
                         std::function<std::unique_ptr<Test>()> maker_fn) {
559
         if(m_tests.contains(name)) {
418✔
560
            throw Test_Error("Duplicate registration of test '" + name + "'");
×
561
         }
562

563
         if(m_tests.contains(category)) {
418✔
564
            throw Test_Error("'" + category + "' cannot be used as category, test exists");
×
565
         }
566

567
         if(m_categories.contains(name)) {
418✔
568
            throw Test_Error("'" + name + "' cannot be used as test name, category exists");
×
569
         }
570

571
         if(smoke_test) {
418✔
572
            m_smoke_tests.push_back(name);
10✔
573
         }
574

575
         if(needs_serialization) {
418✔
576
            m_mutexed_tests.push_back(name);
21✔
577
         }
578

579
         m_tests.emplace(name, std::move(maker_fn));
418✔
580
         m_categories.emplace(category, name);
418✔
581
      }
418✔
582

583
      std::unique_ptr<Test> get_test(const std::string& test_name) const {
418✔
584
         auto i = m_tests.find(test_name);
418✔
585
         if(i != m_tests.end()) {
418✔
586
            return i->second();
418✔
587
         }
588
         return nullptr;
×
589
      }
590

591
      std::vector<std::string> registered_tests() const {
×
592
         std::vector<std::string> s;
×
593
         s.reserve(m_tests.size());
×
594
         for(auto&& i : m_tests) {
×
595
            s.push_back(i.first);
×
596
         }
597
         return s;
×
598
      }
×
599

600
      std::vector<std::string> registered_test_categories() const {
×
601
         std::set<std::string> s;
×
602
         for(auto&& i : m_categories) {
×
603
            s.insert(i.first);
×
604
         }
605
         return std::vector<std::string>(s.begin(), s.end());
×
606
      }
×
607

608
      std::vector<std::string> filter_registered_tests(const std::vector<std::string>& requested,
1✔
609
                                                       const std::vector<std::string>& to_be_skipped) {
610
         std::vector<std::string> result;
1✔
611

612
         std::set<std::string> to_be_skipped_set(to_be_skipped.begin(), to_be_skipped.end());
1✔
613
         // TODO: this is O(n^2), but we have a relatively small number of tests.
614
         auto insert_if_not_exists_and_not_skipped = [&](const std::string& test_name) {
419✔
615
            if(!Botan::value_exists(result, test_name) && !to_be_skipped_set.contains(test_name)) {
836✔
616
               result.push_back(test_name);
408✔
617
            }
618
         };
419✔
619

620
         if(requested.empty()) {
1✔
621
            /*
622
            If nothing was requested on the command line, run everything. First
623
            run the "essentials" to smoke test, then everything else in
624
            alphabetical order.
625
            */
626
            result = m_smoke_tests;
1✔
627
            for(const auto& [test_name, _] : m_tests) {
419✔
628
               insert_if_not_exists_and_not_skipped(test_name);
418✔
629
            }
630
         } else {
631
            for(const auto& r : requested) {
×
632
               if(m_tests.contains(r)) {
×
633
                  insert_if_not_exists_and_not_skipped(r);
×
634
               } else if(auto elems = m_categories.equal_range(r); elems.first != m_categories.end()) {
×
635
                  for(; elems.first != elems.second; ++elems.first) {
×
636
                     insert_if_not_exists_and_not_skipped(elems.first->second);
×
637
                  }
638
               } else {
639
                  throw Test_Error("Unknown test suite or category: " + r);
×
640
               }
641
            }
642
         }
643

644
         return result;
1✔
645
      }
1✔
646

647
      bool needs_serialization(const std::string& test_name) const {
439✔
648
         return Botan::value_exists(m_mutexed_tests, test_name);
21✔
649
      }
650

651
   private:
652
      Test_Registry() = default;
1✔
653

654
   private:
655
      std::map<std::string, std::function<std::unique_ptr<Test>()>> m_tests;
656
      std::multimap<std::string, std::string> m_categories;
657
      std::vector<std::string> m_smoke_tests;
658
      std::vector<std::string> m_mutexed_tests;
659
};
660

661
}  // namespace
662

663
// static Test:: functions
664

665
//static
666
void Test::register_test(const std::string& category,
418✔
667
                         const std::string& name,
668
                         bool smoke_test,
669
                         bool needs_serialization,
670
                         std::function<std::unique_ptr<Test>()> maker_fn) {
671
   Test_Registry::instance().register_test(category, name, smoke_test, needs_serialization, std::move(maker_fn));
836✔
672
}
418✔
673

674
//static
675
uint64_t Test::timestamp() {
179,932✔
676
   auto now = std::chrono::system_clock::now().time_since_epoch();
179,932✔
677
   return std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
2,383✔
678
}
679

680
//static
681
std::vector<Test::Result> Test::flatten_result_lists(std::vector<std::vector<Test::Result>> result_lists) {
4✔
682
   std::vector<Test::Result> results;
4✔
683
   for(auto& result_list : result_lists) {
26✔
684
      for(auto& result : result_list) {
71✔
685
         results.emplace_back(std::move(result));
49✔
686
      }
687
   }
688
   return results;
4✔
689
}
×
690

691
//static
692
std::vector<std::string> Test::registered_tests() {
×
693
   return Test_Registry::instance().registered_tests();
×
694
}
695

696
//static
697
std::vector<std::string> Test::registered_test_categories() {
×
698
   return Test_Registry::instance().registered_test_categories();
×
699
}
700

701
//static
702
std::unique_ptr<Test> Test::get_test(const std::string& test_name) {
418✔
703
   return Test_Registry::instance().get_test(test_name);
418✔
704
}
705

706
//static
707
bool Test::test_needs_serialization(const std::string& test_name) {
418✔
708
   return Test_Registry::instance().needs_serialization(test_name);
418✔
709
}
710

711
//static
712
std::vector<std::string> Test::filter_registered_tests(const std::vector<std::string>& requested,
1✔
713
                                                       const std::vector<std::string>& to_be_skipped) {
714
   return Test_Registry::instance().filter_registered_tests(requested, to_be_skipped);
1✔
715
}
716

717
//static
718
std::string Test::temp_file_name(const std::string& basename) {
21✔
719
   // TODO add a --tmp-dir option to the tests to specify where these files go
720

721
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
722

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

726
   const int fd = ::mkstemp(mkstemp_basename.data());
21✔
727

728
   // error
729
   if(fd < 0) {
21✔
730
      return "";
×
731
   }
732

733
   ::close(fd);
21✔
734

735
   return mkstemp_basename;
21✔
736
#else
737
   // For now just create the temp in the current working directory
738
   return basename;
739
#endif
740
}
21✔
741

742
bool Test::copy_file(const std::string& from, const std::string& to) {
1✔
743
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) && defined(__cpp_lib_filesystem)
744
   std::error_code ec;  // don't throw, just return false on error
1✔
745
   return std::filesystem::copy_file(from, to, std::filesystem::copy_options::overwrite_existing, ec);
1✔
746
#else
747
   // TODO: implement fallbacks to POSIX or WIN32
748
   // ... but then again: it's 2023 and we're using C++20 :o)
749
   BOTAN_UNUSED(from, to);
750
   throw Botan::No_Filesystem_Access();
751
#endif
752
}
753

754
std::string Test::read_data_file(const std::string& path) {
38✔
755
   const std::string fsname = Test::data_file(path);
38✔
756
   std::ifstream file(fsname.c_str());
38✔
757
   if(!file.good()) {
38✔
758
      throw Test_Error("Error reading from " + fsname);
×
759
   }
760

761
   return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
76✔
762
}
38✔
763

764
std::vector<uint8_t> Test::read_binary_data_file(const std::string& path) {
34✔
765
   const std::string fsname = Test::data_file(path);
34✔
766
   std::ifstream file(fsname.c_str(), std::ios::binary);
34✔
767
   if(!file.good()) {
34✔
768
      throw Test_Error("Error reading from " + fsname);
×
769
   }
770

771
   std::vector<uint8_t> contents;
34✔
772

773
   while(file.good()) {
74✔
774
      std::vector<uint8_t> buf(4096);
40✔
775
      file.read(reinterpret_cast<char*>(buf.data()), buf.size());
40✔
776
      const size_t got = static_cast<size_t>(file.gcount());
40✔
777

778
      if(got == 0 && file.eof()) {
40✔
779
         break;
780
      }
781

782
      contents.insert(contents.end(), buf.data(), buf.data() + got);
40✔
783
   }
40✔
784

785
   return contents;
68✔
786
}
34✔
787

788
// static member variables of Test
789

790
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
791
Test_Options Test::m_opts;
792
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
793
std::string Test::m_test_rng_seed;
794

795
//static
796
void Test::set_test_options(const Test_Options& opts) {
1✔
797
   m_opts = opts;
1✔
798
}
1✔
799

800
namespace {
801

802
/*
803
* This is a fast, simple, deterministic PRNG that's used for running
804
* the tests. It is not intended to be cryptographically secure.
805
*/
806
class Testsuite_RNG final : public Botan::RandomNumberGenerator {
8✔
807
   public:
808
      std::string name() const override { return "Testsuite_RNG"; }
×
809

810
      void clear() override { m_x = 0; }
×
811

812
      bool accepts_input() const override { return true; }
×
813

814
      bool is_seeded() const override { return true; }
277,383✔
815

816
      void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
10,330,769✔
817
         for(const auto byte : input) {
10,330,769✔
818
            mix(byte);
×
819
         }
820

821
         for(auto& byte : output) {
73,719,859✔
822
            byte = mix();
63,389,090✔
823
         }
824
      }
10,330,769✔
825

826
      Testsuite_RNG(std::string_view seed, std::string_view test_name) : m_x(0) {
276✔
827
         for(const char c : seed) {
8,280✔
828
            this->mix(static_cast<uint8_t>(c));
8,004✔
829
         }
830
         for(const char c : test_name) {
5,578✔
831
            this->mix(static_cast<uint8_t>(c));
5,302✔
832
         }
833
      }
276✔
834

835
   private:
836
      uint8_t mix(uint8_t input = 0) {
63,402,396✔
837
         m_x ^= input;
63,402,396✔
838
         m_x *= 0xF2E16957;
63,402,396✔
839
         m_x += 0xE50B590F;
63,402,396✔
840
         return static_cast<uint8_t>(m_x >> 27);
63,402,396✔
841
      }
842

843
      uint64_t m_x;
844
};
845

846
}  // namespace
847

848
//static
849
void Test::set_test_rng_seed(std::span<const uint8_t> seed, size_t epoch) {
1✔
850
   m_test_rng_seed = Botan::fmt("seed={} epoch={}", Botan::hex_encode(seed), epoch);
1✔
851
}
1✔
852

853
//static
854
std::unique_ptr<Botan::RandomNumberGenerator> Test::new_rng(std::string_view test_name) {
268✔
855
   return std::make_unique<Testsuite_RNG>(m_test_rng_seed, test_name);
268✔
856
}
857

858
//static
859
std::shared_ptr<Botan::RandomNumberGenerator> Test::new_shared_rng(std::string_view test_name) {
8✔
860
   return std::make_shared<Testsuite_RNG>(m_test_rng_seed, test_name);
8✔
861
}
862

863
//static
864
std::string Test::data_file(const std::string& file) {
772✔
865
   return options().data_dir() + "/" + file;
1,544✔
866
}
867

868
//static
869
std::string Test::data_dir(const std::string& subdir) {
1✔
870
   return options().data_dir() + "/" + subdir;
2✔
871
}
872

873
//static
874
std::vector<std::string> Test::files_in_data_dir(const std::string& subdir) {
393✔
875
   auto fs = Botan::get_files_recursive(options().data_dir() + "/" + subdir);
1,179✔
876
   if(fs.empty()) {
393✔
877
      throw Test_Error("Test::files_in_data_dir encountered empty subdir " + subdir);
×
878
   }
879
   return fs;
393✔
880
}
×
881

882
//static
883
std::string Test::data_file_as_temporary_copy(const std::string& what) {
1✔
884
   auto tmp_basename = what;
1✔
885
   std::replace(tmp_basename.begin(), tmp_basename.end(), '/', '_');
1✔
886
   auto temp_file = temp_file_name("tmp-" + tmp_basename);
1✔
887
   if(temp_file.empty()) {
1✔
888
      return "";
×
889
   }
890
   if(!Test::copy_file(data_file(what), temp_file)) {
1✔
891
      return "";
×
892
   }
893
   return temp_file;
1✔
894
}
1✔
895

896
//static
897
std::vector<std::string> Test::provider_filter(const std::vector<std::string>& providers) {
49,022✔
898
   if(m_opts.provider().empty()) {
49,022✔
899
      return providers;
49,022✔
900
   }
901
   for(auto&& provider : providers) {
×
902
      if(provider == m_opts.provider()) {
×
903
         return std::vector<std::string>{provider};
×
904
      }
905
   }
906
   return std::vector<std::string>{};
×
907
}
×
908

909
std::string Test::random_password(Botan::RandomNumberGenerator& rng) {
222✔
910
   const size_t len = 1 + rng.next_byte() % 32;
222✔
911
   return Botan::hex_encode(rng.random_vec(len));
444✔
912
}
913

914
size_t Test::random_index(Botan::RandomNumberGenerator& rng, size_t max) {
8,062✔
915
   return Botan::load_be(rng.random_array<8>()) % max;
8,062✔
916
}
917

918
void VarMap::clear() {
34,262✔
919
   m_vars.clear();
×
920
}
×
921

922
namespace {
923

924
bool varmap_pair_lt(const std::pair<std::string, std::string>& kv, const std::string& k) {
1,713,323✔
925
   return kv.first < k;
1,713,323✔
926
}
927

928
}  // namespace
929

930
void VarMap::add(const std::string& key, const std::string& value) {
156,191✔
931
   auto i = std::lower_bound(m_vars.begin(), m_vars.end(), key, varmap_pair_lt);
156,191✔
932

933
   if(i != m_vars.end() && i->first == key) {
156,191✔
934
      i->second = value;
44,405✔
935
   } else {
936
      m_vars.emplace(i, key, value);
111,786✔
937
   }
938
}
156,191✔
939

940
std::optional<std::string> VarMap::get_var(const std::string& key) const {
568,332✔
941
   auto i = std::lower_bound(m_vars.begin(), m_vars.end(), key, varmap_pair_lt);
568,332✔
942

943
   if(i != m_vars.end() && i->first == key) {
568,332✔
944
      return i->second;
498,477✔
945
   } else {
946
      return {};
69,855✔
947
   }
948
}
949

950
bool VarMap::has_key(const std::string& key) const {
213,534✔
951
   return get_var(key).has_value();
213,534✔
952
}
953

954
std::string VarMap::get_req_str(const std::string& key) const {
259,571✔
955
   auto var = get_var(key);
259,571✔
956
   if(var) {
259,571✔
957
      return *var;
519,142✔
958
   } else {
959
      throw Test_Error("Test missing variable " + key);
×
960
   }
961
}
259,571✔
962

963
std::vector<std::vector<uint8_t>> VarMap::get_req_bin_list(const std::string& key) const {
12✔
964
   const auto var = get_req_str(key);
12✔
965

966
   std::vector<std::vector<uint8_t>> bin_list;
12✔
967

968
   for(auto&& part : Botan::split_on(var, ',')) {
62✔
969
      try {
50✔
970
         bin_list.push_back(Botan::hex_decode(part));
100✔
971
      } catch(std::exception& e) {
×
972
         std::ostringstream oss;
×
973
         oss << "Bad input '" << part << "'"
×
974
             << " in binary list key " << key << " - " << e.what();
×
975
         throw Test_Error(oss.str());
×
976
      }
×
977
   }
12✔
978

979
   return bin_list;
12✔
980
}
12✔
981

982
std::vector<uint8_t> VarMap::get_req_bin(const std::string& key) const {
163,670✔
983
   const auto var = get_req_str(key);
163,670✔
984

985
   try {
163,670✔
986
      if(var.starts_with("0x")) {
163,670✔
987
         if(var.size() % 2 == 0) {
×
988
            return Botan::hex_decode(var.substr(2));
×
989
         } else {
990
            std::string z = var;
×
991
            std::swap(z[0], z[1]);  // swap 0x to x0 then remove x
×
992
            return Botan::hex_decode(z.substr(1));
×
993
         }
×
994
      } else {
995
         return Botan::hex_decode(var);
163,670✔
996
      }
997
   } catch(std::exception& e) {
×
998
      std::ostringstream oss;
×
999
      oss << "Bad input '" << var << "'"
×
1000
          << " for key " << key << " - " << e.what();
×
1001
      throw Test_Error(oss.str());
×
1002
   }
×
1003
}
163,670✔
1004

1005
std::string VarMap::get_opt_str(const std::string& key, const std::string& def_value) const {
14,378✔
1006
   if(auto v = get_var(key)) {
14,378✔
1007
      return *v;
57✔
1008
   } else {
1009
      return def_value;
28,699✔
1010
   }
14,378✔
1011
}
1012

1013
bool VarMap::get_req_bool(const std::string& key) const {
39✔
1014
   const auto var = get_req_str(key);
39✔
1015

1016
   if(var == "true") {
39✔
1017
      return true;
1018
   } else if(var == "false") {
23✔
1019
      return false;
1020
   } else {
1021
      throw Test_Error("Invalid boolean for key '" + key + "' value '" + var + "'");
×
1022
   }
1023
}
39✔
1024

1025
size_t VarMap::get_req_sz(const std::string& key) const {
4,547✔
1026
   return Botan::to_u32bit(get_req_str(key));
4,547✔
1027
}
1028

1029
uint8_t VarMap::get_req_u8(const std::string& key) const {
17✔
1030
   const size_t s = this->get_req_sz(key);
17✔
1031
   if(s > 256) {
17✔
1032
      throw Test_Error("Invalid " + key + " expected uint8_t got " + std::to_string(s));
×
1033
   }
1034
   return static_cast<uint8_t>(s);
17✔
1035
}
1036

1037
uint32_t VarMap::get_req_u32(const std::string& key) const {
14✔
1038
   return static_cast<uint32_t>(get_req_sz(key));
14✔
1039
}
1040

1041
uint64_t VarMap::get_req_u64(const std::string& key) const {
17✔
1042
   const auto var = get_req_str(key);
17✔
1043
   try {
17✔
1044
      return std::stoull(var);
17✔
1045
   } catch(std::exception&) {
×
1046
      throw Test_Error("Invalid u64 value '" + var + "'");
×
1047
   }
×
1048
}
17✔
1049

1050
size_t VarMap::get_opt_sz(const std::string& key, const size_t def_value) const {
31,379✔
1051
   if(auto v = get_var(key)) {
31,379✔
1052
      return Botan::to_u32bit(*v);
12,244✔
1053
   } else {
1054
      return def_value;
1055
   }
31,379✔
1056
}
1057

1058
uint64_t VarMap::get_opt_u64(const std::string& key, const uint64_t def_value) const {
3,538✔
1059
   if(auto v = get_var(key)) {
3,538✔
1060
      try {
641✔
1061
         return std::stoull(*v);
3,538✔
1062
      } catch(std::exception&) {
×
1063
         throw Test_Error("Invalid u64 value '" + *v + "'");
×
1064
      }
×
1065
   } else {
1066
      return def_value;
1067
   }
3,538✔
1068
}
1069

1070
std::vector<uint8_t> VarMap::get_opt_bin(const std::string& key) const {
45,852✔
1071
   if(auto v = get_var(key)) {
45,852✔
1072
      try {
16,118✔
1073
         return Botan::hex_decode(*v);
16,118✔
1074
      } catch(std::exception&) {
×
1075
         throw Test_Error("Test invalid hex input '" + *v + "'" + +" for key " + key);
×
1076
      }
×
1077
   } else {
1078
      return {};
29,734✔
1079
   };
45,852✔
1080
}
1081

1082
#if defined(BOTAN_HAS_BIGINT)
1083
Botan::BigInt VarMap::get_req_bn(const std::string& key) const {
38,502✔
1084
   const auto var = get_req_str(key);
38,502✔
1085

1086
   try {
38,502✔
1087
      return Botan::BigInt(var);
38,502✔
1088
   } catch(std::exception&) {
×
1089
      throw Test_Error("Test invalid bigint input '" + var + "' for key " + key);
×
1090
   }
×
1091
}
38,502✔
1092

1093
Botan::BigInt VarMap::get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const {
80✔
1094
   if(auto v = get_var(key)) {
80✔
1095
      try {
56✔
1096
         return Botan::BigInt(*v);
56✔
1097
      } catch(std::exception&) {
×
1098
         throw Test_Error("Test invalid bigint input '" + *v + "' for key " + key);
×
1099
      }
×
1100
   } else {
1101
      return def_value;
24✔
1102
   }
80✔
1103
}
1104
#endif
1105

1106
class Text_Based_Test::Text_Based_Test_Data {
1107
   public:
1108
      Text_Based_Test_Data(const std::string& data_src,
180✔
1109
                           const std::string& required_keys_str,
1110
                           const std::string& optional_keys_str) :
180✔
1111
            m_data_src(data_src) {
360✔
1112
         if(required_keys_str.empty()) {
180✔
1113
            throw Test_Error("Invalid test spec");
×
1114
         }
1115

1116
         m_required_keys = Botan::split_on(required_keys_str, ',');
180✔
1117
         std::vector<std::string> optional_keys = Botan::split_on(optional_keys_str, ',');
180✔
1118

1119
         m_all_keys.insert(m_required_keys.begin(), m_required_keys.end());
180✔
1120
         m_all_keys.insert(optional_keys.begin(), optional_keys.end());
180✔
1121
         m_output_key = m_required_keys.at(m_required_keys.size() - 1);
180✔
1122
      }
180✔
1123

1124
      std::string get_next_line();
1125

1126
      const std::string& current_source_name() const { return m_cur_src_name; }
×
1127

1128
      bool known_key(const std::string& key) const;
1129

1130
      const std::vector<std::string>& required_keys() const { return m_required_keys; }
48,369✔
1131

1132
      const std::string& output_key() const { return m_output_key; }
156,191✔
1133

1134
      const std::vector<std::string>& cpu_flags() const { return m_cpu_flags; }
48,230✔
1135

1136
      void set_cpu_flags(std::vector<std::string> flags) { m_cpu_flags = std::move(flags); }
21✔
1137

1138
      const std::string& initial_data_src_name() const { return m_data_src; }
180✔
1139

1140
   private:
1141
      std::string m_data_src;
1142
      std::vector<std::string> m_required_keys;
1143
      std::unordered_set<std::string> m_all_keys;
1144
      std::string m_output_key;
1145

1146
      bool m_first = true;
1147
      std::unique_ptr<std::istream> m_cur;
1148
      std::string m_cur_src_name;
1149
      std::deque<std::string> m_srcs;
1150
      std::vector<std::string> m_cpu_flags;
1151
};
1152

1153
Text_Based_Test::Text_Based_Test(const std::string& data_src,
180✔
1154
                                 const std::string& required_keys,
1155
                                 const std::string& optional_keys) :
180✔
1156
      m_data(std::make_unique<Text_Based_Test_Data>(data_src, required_keys, optional_keys)) {}
180✔
1157

1158
Text_Based_Test::~Text_Based_Test() = default;
180✔
1159

1160
std::string Text_Based_Test::Text_Based_Test_Data::get_next_line() {
157,197✔
1161
   while(true) {
157,454✔
1162
      if(m_cur == nullptr || m_cur->good() == false) {
157,454✔
1163
         if(m_srcs.empty()) {
448✔
1164
            if(m_first) {
360✔
1165
               if(m_data_src.ends_with(".vec")) {
180✔
1166
                  m_srcs.push_back(Test::data_file(m_data_src));
336✔
1167
               } else {
1168
                  const auto fs = Test::files_in_data_dir(m_data_src);
12✔
1169
                  m_srcs.assign(fs.begin(), fs.end());
12✔
1170
                  if(m_srcs.empty()) {
12✔
1171
                     throw Test_Error("Error reading test data dir " + m_data_src);
×
1172
                  }
1173
               }
12✔
1174

1175
               m_first = false;
180✔
1176
            } else {
1177
               return "";  // done
180✔
1178
            }
1179
         }
1180

1181
         m_cur = std::make_unique<std::ifstream>(m_srcs[0]);
356✔
1182
         m_cur_src_name = m_srcs[0];
268✔
1183

1184
#if defined(BOTAN_HAS_CPUID)
1185
         // Reinit cpuid on new file if needed
1186
         if(m_cpu_flags.empty() == false) {
268✔
1187
            m_cpu_flags.clear();
19✔
1188
            Botan::CPUID::initialize();
19✔
1189
         }
1190
#endif
1191

1192
         if(!m_cur->good()) {
268✔
1193
            throw Test_Error("Could not open input file '" + m_cur_src_name);
×
1194
         }
1195

1196
         m_srcs.pop_front();
268✔
1197
      }
1198

1199
      while(m_cur->good()) {
227,817✔
1200
         std::string line;
227,560✔
1201
         std::getline(*m_cur, line);
227,560✔
1202

1203
         if(line.empty()) {
227,560✔
1204
            continue;
55,286✔
1205
         }
1206

1207
         if(line[0] == '#') {
172,274✔
1208
            if(line.starts_with("#test ")) {
15,278✔
1209
               return line;
21✔
1210
            } else {
1211
               continue;
15,257✔
1212
            }
1213
         }
1214

1215
         return line;
156,996✔
1216
      }
227,560✔
1217
   }
1218
}
1219

1220
bool Text_Based_Test::Text_Based_Test_Data::known_key(const std::string& key) const {
156,191✔
1221
   return m_all_keys.contains(key);
156,191✔
1222
}
1223

1224
namespace {
1225

1226
// strips leading and trailing but not internal whitespace
1227
std::string strip_ws(const std::string& in) {
312,382✔
1228
   const char* whitespace = " ";
312,382✔
1229

1230
   const auto first_c = in.find_first_not_of(whitespace);
312,382✔
1231
   if(first_c == std::string::npos) {
312,382✔
1232
      return "";
1,033✔
1233
   }
1234

1235
   const auto last_c = in.find_last_not_of(whitespace);
311,349✔
1236

1237
   return in.substr(first_c, last_c - first_c + 1);
311,349✔
1238
}
1239

1240
std::vector<std::string> parse_cpuid_bits(const std::vector<std::string>& tok) {
21✔
1241
   std::vector<std::string> bits;
21✔
1242

1243
#if defined(BOTAN_HAS_CPUID)
1244
   for(size_t i = 1; i < tok.size(); ++i) {
106✔
1245
      if(auto bit = Botan::CPUID::bit_from_string(tok[i])) {
85✔
1246
         bits.push_back(bit->to_string());
98✔
1247
      }
1248
   }
1249
#else
1250
   BOTAN_UNUSED(tok);
1251
#endif
1252

1253
   return bits;
21✔
1254
}
×
1255

1256
}  // namespace
1257

1258
bool Text_Based_Test::skip_this_test(const std::string& /*header*/, const VarMap& /*vars*/) {
32,590✔
1259
   return false;
32,590✔
1260
}
1261

1262
std::vector<Test::Result> Text_Based_Test::run() {
180✔
1263
   std::vector<Test::Result> results;
180✔
1264

1265
   std::string header;
180✔
1266
   std::string header_or_name = m_data->initial_data_src_name();
180✔
1267
   VarMap vars;
180✔
1268
   size_t test_cnt = 0;
180✔
1269

1270
   while(true) {
157,197✔
1271
      const std::string line = m_data->get_next_line();
157,197✔
1272
      if(line.empty()) {
157,197✔
1273
         // EOF
1274
         break;
1275
      }
1276

1277
      if(line.starts_with("#test ")) {
157,017✔
1278
         std::vector<std::string> pragma_tokens = Botan::split_on(line.substr(6), ' ');
21✔
1279

1280
         if(pragma_tokens.empty()) {
21✔
1281
            throw Test_Error("Empty pragma found in " + m_data->current_source_name());
×
1282
         }
1283

1284
         if(pragma_tokens[0] != "cpuid") {
21✔
1285
            throw Test_Error("Unknown test pragma '" + line + "' in " + m_data->current_source_name());
×
1286
         }
1287

1288
         if(!Test_Registry::instance().needs_serialization(this->test_name())) {
42✔
1289
            throw Test_Error(Botan::fmt("'{}' used cpuid control but is not serialized", this->test_name()));
×
1290
         }
1291

1292
         m_data->set_cpu_flags(parse_cpuid_bits(pragma_tokens));
42✔
1293

1294
         continue;
21✔
1295
      } else if(line[0] == '#') {
157,017✔
1296
         throw Test_Error("Unknown test pragma '" + line + "' in " + m_data->current_source_name());
×
1297
      }
1298

1299
      if(line[0] == '[' && line[line.size() - 1] == ']') {
156,996✔
1300
         header = line.substr(1, line.size() - 2);
805✔
1301
         header_or_name = header;
805✔
1302
         test_cnt = 0;
805✔
1303
         vars.clear();
805✔
1304
         continue;
805✔
1305
      }
1306

1307
      const std::string test_id = "test " + std::to_string(test_cnt);
312,382✔
1308

1309
      auto equal_i = line.find_first_of('=');
156,191✔
1310

1311
      if(equal_i == std::string::npos) {
156,191✔
1312
         results.push_back(Test::Result::Failure(header_or_name, "invalid input '" + line + "'"));
×
1313
         continue;
×
1314
      }
1315

1316
      const std::string key = strip_ws(std::string(line.begin(), line.begin() + equal_i - 1));
312,382✔
1317
      const std::string val = strip_ws(std::string(line.begin() + equal_i + 1, line.end()));
312,382✔
1318

1319
      if(!m_data->known_key(key)) {
156,191✔
1320
         auto r = Test::Result::Failure(header_or_name, Botan::fmt("{} failed unknown key {}", test_id, key));
×
1321
         results.push_back(r);
×
1322
      }
×
1323

1324
      vars.add(key, val);
156,191✔
1325

1326
      if(key == m_data->output_key()) {
156,191✔
1327
         try {
48,369✔
1328
            for(const auto& req_key : m_data->required_keys()) {
245,495✔
1329
               if(!vars.has_key(req_key)) {
197,126✔
1330
                  auto r =
×
1331
                     Test::Result::Failure(header_or_name, Botan::fmt("{} missing required key {}", test_id, req_key));
×
1332
                  results.push_back(r);
×
1333
               }
×
1334
            }
1335

1336
            if(skip_this_test(header, vars)) {
48,369✔
1337
               continue;
139✔
1338
            }
1339

1340
            ++test_cnt;
48,230✔
1341

1342
            const uint64_t start = Test::timestamp();
48,230✔
1343

1344
            Test::Result result = run_one_test(header, vars);
48,230✔
1345
#if defined(BOTAN_HAS_CPUID)
1346
            if(!m_data->cpu_flags().empty()) {
48,230✔
1347
               for(const auto& cpuid_str : m_data->cpu_flags()) {
30,613✔
1348
                  if(const auto bit = Botan::CPUID::Feature::from_string(cpuid_str)) {
21,671✔
1349
                     if(Botan::CPUID::has(*bit)) {
21,671✔
1350
                        Botan::CPUID::clear_cpuid_bit(*bit);
16,571✔
1351
                        // now re-run the test
1352
                        result.merge(run_one_test(header, vars));
16,571✔
1353
                     }
1354
                  }
1355
               }
1356
               Botan::CPUID::initialize();
8,942✔
1357
            }
1358
#endif
1359
            result.set_ns_consumed(Test::timestamp() - start);
48,230✔
1360

1361
            if(result.tests_failed() > 0) {
48,230✔
1362
               std::ostringstream oss;
×
1363
               oss << "Test # " << test_cnt << " ";
×
1364
               if(!header.empty()) {
×
1365
                  oss << header << " ";
×
1366
               }
1367
               oss << "failed ";
×
1368

1369
               for(const auto& k : m_data->required_keys()) {
×
1370
                  oss << k << "=" << vars.get_req_str(k) << " ";
×
1371
               }
1372

1373
               result.test_note(oss.str());
×
1374
            }
×
1375
            results.push_back(result);
48,230✔
1376
         } catch(std::exception& e) {
48,230✔
1377
            std::ostringstream oss;
×
1378
            oss << "Test # " << test_cnt << " ";
×
1379
            if(!header.empty()) {
×
1380
               oss << header << " ";
×
1381
            }
1382

1383
            for(const auto& k : m_data->required_keys()) {
×
1384
               oss << k << "=" << vars.get_req_str(k) << " ";
×
1385
            }
1386

1387
            oss << "failed with exception '" << e.what() << "'";
×
1388

1389
            results.push_back(Test::Result::Failure(header_or_name, oss.str()));
×
1390
         }
×
1391

1392
         if(clear_between_callbacks()) {
48,230✔
1393
            vars.clear();
189,509✔
1394
         }
1395
      }
1396
   }
157,295✔
1397

1398
   if(results.empty()) {
180✔
1399
      return results;
1400
   }
1401

1402
   try {
180✔
1403
      std::vector<Test::Result> final_tests = run_final_tests();
180✔
1404
      results.insert(results.end(), final_tests.begin(), final_tests.end());
180✔
1405
   } catch(std::exception& e) {
180✔
1406
      results.push_back(Test::Result::Failure(header_or_name, "run_final_tests exception " + std::string(e.what())));
×
1407
   }
×
1408

1409
   return results;
1410
}
180✔
1411

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