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

randombit / botan / 14120723934

28 Mar 2025 02:31AM UTC coverage: 91.539% (+0.004%) from 91.535%
14120723934

Pull #4798

github

web-flow
Merge db2c0eef1 into 70cd16046
Pull Request #4798: Move most architecture-specific logic out of CPUID and into a submodule

95384 of 104200 relevant lines covered (91.54%)

11667903.22 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) {
116,669✔
47
   if(who() != other.who()) {
116,669✔
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;
116,596✔
57
   }
58

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

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

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

79
void Test::Result::test_note(const std::string& note, const char* extra) {
2,752✔
80
   if(!note.empty()) {
2,752✔
81
      std::ostringstream out;
2,752✔
82
      out << who() << " " << note;
2,752✔
83
      if(extra) {
2,752✔
84
         out << ": " << extra;
12✔
85
      }
86
      m_log.push_back(out.str());
2,752✔
87
   }
2,752✔
88
}
2,752✔
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,909✔
100
   m_consumed = true;
47,909✔
101

102
   try {
47,909✔
103
      m_fn();
47,909✔
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,264✔
108
      if(m_expect_success) {
46,262✔
109
         return result.test_failure(test_name + " threw unexpected exception: " + ex.what());
2✔
110
      }
111
      if(m_expected_exception_type.has_value() && m_expected_exception_type.value() != typeid(ex)) {
46,261✔
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,259✔
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,264✔
119
      if(m_expect_success || m_expected_exception_type.has_value() || m_expected_message.has_value()) {
2✔
120
         return result.test_failure(test_name + " threw unexpected unknown exception");
1✔
121
      }
122
   }
2✔
123

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

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

131
bool Test::Result::test_throws(const std::string& what, const std::string& expected, const std::function<void()>& fn) {
172✔
132
   return ThrowExpectations(fn).expect_message(expected).check(what, *this);
344✔
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,307,449✔
140
   if(Test::options().log_success()) {
3,307,318✔
141
      test_note(note);
×
142
   }
143
   ++m_tests_passed;
3,307,449✔
144
   return true;
537,484✔
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) {
261,115✔
168
   return (len == 0) ? true : std::memcmp(x, y, len) == 0;
259,766✔
169
}
170

171
}  // namespace
172

173
bool Test::Result::test_ne(const std::string& what,
3,411✔
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,411✔
179
      return test_failure(who() + ": " + what + " produced matching");
6✔
180
   }
181
   return test_success();
6,818✔
182
}
183

184
bool Test::Result::test_eq(const char* producer,
259,697✔
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)) {
259,697✔
191
      return test_success();
519,392✔
192
   }
193

194
   std::ostringstream err;
1✔
195

196
   err << who();
1✔
197

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

294
   return test_success();
2,276,994✔
295
}
296

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

394
   return test_success();
672✔
395
}
396

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

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

407
   return *m_test_rng;
344,786✔
408
}
409

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

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

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

424
   return o.str();
2,838✔
425
}
1,419✔
426

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

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

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

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

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

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

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

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

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

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

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

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

481
namespace {
482

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

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

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

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

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

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

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

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

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

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

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

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

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

578
         return result;
1✔
579
      }
×
580

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

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

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

595
}  // namespace
596

597
// static Test:: functions
598

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

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

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

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

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

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

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

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

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

655
#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
656

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

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

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

667
   ::close(fd);
21✔
668

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

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

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

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

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

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

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

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

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

719
   return contents;
68✔
720
}
34✔
721

722
// static member variables of Test
723

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

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

734
namespace {
735

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

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

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

748
      bool is_seeded() const override { return true; }
261,826✔
749

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

755
         for(auto& byte : output) {
40,689,562✔
756
            byte = mix();
30,612,966✔
757
         }
758
      }
10,076,596✔
759

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

763
         for(char c : seed) {
7,560✔
764
            this->mix(static_cast<uint8_t>(c));
7,308✔
765
         }
766
         for(char c : test_name) {
4,495✔
767
            this->mix(static_cast<uint8_t>(c));
4,243✔
768
         }
769
      }
252✔
770

771
   private:
772
      uint8_t mix(uint8_t input = 0) {
30,624,517✔
773
         m_x ^= input;
30,624,517✔
774
         m_x *= 0xF2E16957;
30,624,517✔
775
         m_x += 0xE50B590F;
30,624,517✔
776
         return static_cast<uint8_t>(m_x >> 27);
30,624,517✔
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) {
244✔
791
   return std::make_unique<Testsuite_RNG>(m_test_rng_seed, test_name);
244✔
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) {
621✔
801
   return options().data_dir() + "/" + file;
1,242✔
802
}
803

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

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

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

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

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

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

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

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

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

873
   return bin_list;
12✔
874
}
×
875

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

882
   try {
150,636✔
883
      if(i->second.starts_with("0x")) {
150,636✔
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);
150,636✔
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,372✔
903
   auto i = m_vars.find(key);
14,372✔
904
   if(i == m_vars.end()) {
14,372✔
905
      return def_value;
14,318✔
906
   }
907
   return i->second;
54✔
908
}
909

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

916
   if(i->second == "true") {
19✔
917
      return true;
918
   } else if(i->second == "false") {
11✔
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,507✔
926
   auto i = m_vars.find(key);
4,507✔
927
   if(i == m_vars.end()) {
4,507✔
928
      throw Test_Error("Test missing variable " + key);
×
929
   }
930
   return Botan::to_u32bit(i->second);
4,507✔
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 {
30,347✔
958
   auto i = m_vars.find(key);
30,347✔
959
   if(i == m_vars.end()) {
30,347✔
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 {
40,020✔
978
   auto i = m_vars.find(key);
40,020✔
979
   if(i == m_vars.end()) {
40,020✔
980
      return std::vector<uint8_t>();
28,343✔
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,577✔
991
   auto i = m_vars.find(key);
52,577✔
992
   if(i == m_vars.end()) {
52,577✔
993
      throw Test_Error("Test missing variable " + key);
×
994
   }
995
   return i->second;
52,577✔
996
}
997

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

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

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

1014
{
1015
   auto i = m_vars.find(key);
80✔
1016
   if(i == m_vars.end()) {
80✔
1017
      return def_value;
24✔
1018
   }
1019

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

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

1036
   std::vector<std::string> required_keys = Botan::split_on(required_keys_str, ',');
176✔
1037
   std::vector<std::string> optional_keys = Botan::split_on(optional_keys_str, ',');
176✔
1038

1039
   m_required_keys.insert(required_keys.begin(), required_keys.end());
176✔
1040
   m_optional_keys.insert(optional_keys.begin(), optional_keys.end());
176✔
1041
   m_output_key = required_keys.at(required_keys.size() - 1);
176✔
1042
}
176✔
1043

1044
std::string Text_Based_Test::get_next_line() {
154,652✔
1045
   while(true) {
154,902✔
1046
      if(m_cur == nullptr || m_cur->good() == false) {
154,902✔
1047
         if(m_srcs.empty()) {
437✔
1048
            if(m_first) {
352✔
1049
               if(m_data_src.ends_with(".vec")) {
176✔
1050
                  m_srcs.push_back(Test::data_file(m_data_src));
328✔
1051
               } else {
1052
                  const auto fs = Test::files_in_data_dir(m_data_src);
12✔
1053
                  m_srcs.assign(fs.begin(), fs.end());
12✔
1054
                  if(m_srcs.empty()) {
12✔
1055
                     throw Test_Error("Error reading test data dir " + m_data_src);
×
1056
                  }
1057
               }
12✔
1058

1059
               m_first = false;
176✔
1060
            } else {
1061
               return "";  // done
176✔
1062
            }
1063
         }
1064

1065
         m_cur = std::make_unique<std::ifstream>(m_srcs[0]);
346✔
1066
         m_cur_src_name = m_srcs[0];
261✔
1067

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

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

1080
         m_srcs.pop_front();
261✔
1081
      }
1082

1083
      while(m_cur->good()) {
224,429✔
1084
         std::string line;
224,179✔
1085
         std::getline(*m_cur, line);
224,179✔
1086

1087
         if(line.empty()) {
224,179✔
1088
            continue;
54,602✔
1089
         }
1090

1091
         if(line[0] == '#') {
169,577✔
1092
            if(line.starts_with("#test ")) {
15,117✔
1093
               return line;
16✔
1094
            } else {
1095
               continue;
15,101✔
1096
            }
1097
         }
1098

1099
         return line;
154,460✔
1100
      }
224,179✔
1101
   }
1102
}
1103

1104
namespace {
1105

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

1110
   const auto first_c = in.find_first_not_of(whitespace);
307,354✔
1111
   if(first_c == std::string::npos) {
307,354✔
1112
      return "";
1,004✔
1113
   }
1114

1115
   const auto last_c = in.find_last_not_of(whitespace);
306,350✔
1116

1117
   return in.substr(first_c, last_c - first_c + 1);
306,350✔
1118
}
1119

1120
std::vector<std::string> parse_cpuid_bits(const std::vector<std::string>& tok) {
16✔
1121
   std::vector<std::string> bits;
16✔
1122

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

1133
   return bits;
16✔
1134
}
×
1135

1136
}  // namespace
1137

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

1142
std::vector<Test::Result> Text_Based_Test::run() {
176✔
1143
   std::vector<Test::Result> results;
176✔
1144

1145
   std::string header, header_or_name = m_data_src;
176✔
1146
   VarMap vars;
176✔
1147
   size_t test_cnt = 0;
176✔
1148

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

1156
      if(line.starts_with("#test ")) {
154,476✔
1157
         std::vector<std::string> pragma_tokens = Botan::split_on(line.substr(6), ' ');
16✔
1158

1159
         if(pragma_tokens.empty()) {
16✔
1160
            throw Test_Error("Empty pragma found in " + m_cur_src_name);
×
1161
         }
1162

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

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

1171
         m_cpu_flags = parse_cpuid_bits(pragma_tokens);
16✔
1172

1173
         continue;
16✔
1174
      } else if(line[0] == '#') {
154,476✔
1175
         throw Test_Error("Unknown test pragma '" + line + "' in " + m_cur_src_name);
×
1176
      }
1177

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

1186
      const std::string test_id = "test " + std::to_string(test_cnt);
307,354✔
1187

1188
      auto equal_i = line.find_first_of('=');
153,677✔
1189

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

1195
      std::string key = strip_ws(std::string(line.begin(), line.begin() + equal_i - 1));
307,354✔
1196
      std::string val = strip_ws(std::string(line.begin() + equal_i + 1, line.end()));
307,354✔
1197

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

1203
      vars.add(key, val);
153,677✔
1204

1205
      if(key == m_output_key) {
153,677✔
1206
         try {
47,664✔
1207
            for(auto& req_key : m_required_keys) {
242,901✔
1208
               if(!vars.has_key(req_key)) {
390,474✔
1209
                  auto r =
×
1210
                     Test::Result::Failure(header_or_name, Botan::fmt("{} missing required key {}", test_id, req_key));
×
1211
                  results.push_back(r);
×
1212
               }
×
1213
            }
1214

1215
            if(skip_this_test(header, vars)) {
47,664✔
1216
               continue;
139✔
1217
            }
1218

1219
            ++test_cnt;
47,525✔
1220

1221
            uint64_t start = Test::timestamp();
47,525✔
1222

1223
            Test::Result result = run_one_test(header, vars);
47,525✔
1224
#if defined(BOTAN_HAS_CPUID)
1225
            if(!m_cpu_flags.empty()) {
47,525✔
1226
               for(const auto& cpuid_str : m_cpu_flags) {
19,481✔
1227
                  if(const auto bit = Botan::CPUID::Feature::from_string(cpuid_str)) {
13,866✔
1228
                     if(Botan::CPUID::has(*bit)) {
13,866✔
1229
                        Botan::CPUID::clear_cpuid_bit(*bit);
12,524✔
1230
                        // now re-run the test
1231
                        result.merge(run_one_test(header, vars));
12,524✔
1232
                     }
1233
                  }
1234
               }
1235
               Botan::CPUID::initialize();
5,615✔
1236
            }
1237
#endif
1238
            result.set_ns_consumed(Test::timestamp() - start);
47,525✔
1239

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

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

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

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

1266
            oss << "failed with exception '" << e.what() << "'";
×
1267

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

1271
         if(clear_between_callbacks()) {
47,525✔
1272
            vars.clear();
32,771✔
1273
         }
1274
      }
1275
   }
154,754✔
1276

1277
   if(results.empty()) {
176✔
1278
      return results;
1279
   }
1280

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

1288
   m_first = true;
176✔
1289

1290
   return results;
176✔
1291
}
176✔
1292

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

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

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

1306
   return result;
1✔
1307
}
×
1308

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

© 2025 Coveralls, Inc