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

randombit / botan / 16250409945

13 Jul 2025 02:59PM UTC coverage: 90.617% (-0.003%) from 90.62%
16250409945

push

github

web-flow
Merge pull request #4985 from randombit/jack/fix-clang-tidy-cppcoreguidelines-prefer-member-initializer

Enable and fix clang-tidy warning cppcoreguidelines-prefer-member-initializer

99526 of 109831 relevant lines covered (90.62%)

12277545.17 hits per line

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

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

7
#include "tests.h"
8

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

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

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

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

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

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

44
namespace Botan_Tests {
45

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

165
namespace {
166

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

171
}  // namespace
172

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

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

194
   std::ostringstream err;
1✔
195

196
   err << who();
1✔
197

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

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

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

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

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

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

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

225
   return test_failure(err.str());
1✔
226
}
1✔
227

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

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

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

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

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

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

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

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

274
   return test_success();
11,278✔
275
}
276

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

284
   return test_success();
2,043,270✔
285
}
286

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

296
   return test_success();
2,284,142✔
297
}
298

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

308
   return test_success();
28,946✔
309
}
310

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

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

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

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

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

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

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

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

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

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

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

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

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

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

396
   return test_success();
740✔
397
}
398

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

404
Botan::RandomNumberGenerator& Test::rng() const {
452,430✔
405
   if(!m_test_rng) {
452,430✔
406
      m_test_rng = Test::new_rng(m_test_name);
144✔
407
   }
408

409
   return *m_test_rng;
452,430✔
410
}
411

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

416
//static
417
std::string Test::format_time(uint64_t nanoseconds) {
1,456✔
418
   std::ostringstream o;
1,456✔
419

420
   if(nanoseconds > 1000000000) {
1,456✔
421
      o << std::setprecision(2) << std::fixed << nanoseconds / 1000000000.0 << " sec";
159✔
422
   } else {
423
      o << std::setprecision(2) << std::fixed << nanoseconds / 1000000.0 << " msec";
1,297✔
424
   }
425

426
   return o.str();
2,912✔
427
}
1,456✔
428

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

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

439
   if(tests_run() == 0 && !verbose) {
2,505✔
440
      return "";
20✔
441
   }
442

443
   std::ostringstream report;
2,485✔
444

445
   report << who() << " ran ";
2,485✔
446

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

454
   if(m_ns_taken > 0) {
2,485✔
455
      report << " in " << format_time(m_ns_taken);
2,910✔
456
   }
457

458
   if(tests_failed()) {
2,485✔
459
      report << " " << tests_failed() << " FAILED";
25✔
460
   } else {
461
      report << " all ok";
2,460✔
462
   }
463

464
   report << "\n";
2,485✔
465

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

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

480
   return report.str();
2,485✔
481
}
2,485✔
482

483
namespace {
484

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

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

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

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

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

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

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

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

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

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

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

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

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

580
         return result;
1✔
581
      }
×
582

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

587
   private:
588
      Test_Registry() = default;
1✔
589

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

597
}  // namespace
598

599
// static Test:: functions
600

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

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

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

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

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

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

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

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

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

657
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
658

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

662
   int fd = ::mkstemp(mkstemp_basename.data());
21✔
663

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

669
   ::close(fd);
21✔
670

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

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

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

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

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

707
   std::vector<uint8_t> contents;
34✔
708

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

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

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

721
   return contents;
68✔
722
}
34✔
723

724
// static member variables of Test
725

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

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

736
namespace {
737

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

746
      void clear() override { m_x = 0; }
×
747

748
      bool accepts_input() const override { return true; }
×
749

750
      bool is_seeded() const override { return true; }
278,296✔
751

752
      void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
10,244,101✔
753
         for(const auto byte : input) {
10,244,101✔
754
            mix(byte);
×
755
         }
756

757
         for(auto& byte : output) {
73,269,613✔
758
            byte = mix();
63,025,512✔
759
         }
760
      }
10,244,101✔
761

762
      Testsuite_RNG(std::string_view seed, std::string_view test_name) : m_x(0) {
274✔
763
         for(char c : seed) {
8,220✔
764
            this->mix(static_cast<uint8_t>(c));
7,946✔
765
         }
766
         for(char c : test_name) {
5,546✔
767
            this->mix(static_cast<uint8_t>(c));
5,272✔
768
         }
769
      }
274✔
770

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

779
      uint64_t m_x;
780
};
781

782
}  // namespace
783

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

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

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

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

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

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

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

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

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

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

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

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

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

873
   return bin_list;
12✔
874
}
×
875

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1097
         return line;
154,749✔
1098
      }
224,541✔
1099
   }
1100
}
1101

1102
namespace {
1103

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

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

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

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

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

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

1131
   return bits;
17✔
1132
}
×
1133

1134
}  // namespace
1135

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

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

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

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

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

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

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

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

1169
         m_cpu_flags = parse_cpuid_bits(pragma_tokens);
17✔
1170

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

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

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

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

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

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

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

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

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

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

1217
            ++test_cnt;
47,626✔
1218

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

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

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

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

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

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

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

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

1269
         if(clear_between_callbacks()) {
47,626✔
1270
            vars.clear();
32,854✔
1271
         }
1272
      }
1273
   }
155,044✔
1274

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

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

1286
   m_first = true;
180✔
1287

1288
   return results;
180✔
1289
}
180✔
1290

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

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

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

1304
   return result;
1✔
1305
}
×
1306

1307
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc