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

randombit / botan / 22077577527

16 Feb 2026 09:09PM UTC coverage: 90.031% (-0.02%) from 90.047%
22077577527

push

github

web-flow
Merge pull request #5348 from randombit/jack/use-test-helpers

Improve use of test predicates

102329 of 113660 relevant lines covered (90.03%)

11467371.76 hits per line

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

86.58
/src/tests/tests.h
1
/*
2
* (C) 2014,2015,2026 Jack Lloyd
3
* (C) 2015 Simon Warta (Kullo GmbH)
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#ifndef BOTAN_TESTS_H_
9
#define BOTAN_TESTS_H_
10

11
/*
12
Warning: be very careful about adding any new includes here
13

14
Each include is parsed for every test file which can get quite expensive
15
*/
16

17
#include <botan/types.h>
18
#include <functional>
19
#include <memory>
20
#include <optional>
21
#include <span>
22
#include <stdexcept>
23
#include <string>
24
#include <variant>
25
#include <vector>
26

27
namespace Botan {
28

29
class RandomNumberGenerator;
30

31
#if defined(BOTAN_HAS_BIGINT)
32
class BigInt;
33
#endif
34

35
}  // namespace Botan
36

37
namespace Botan_Tests {
38

39
#if defined(BOTAN_HAS_BIGINT)
40
using Botan::BigInt;
41
#endif
42

43
class Test_Error : public std::runtime_error {
44
   public:
45
      explicit Test_Error(std::string_view what);
46
};
47

48
class Test_Aborted final : public Test_Error {
49
   public:
50
      explicit Test_Aborted(std::string_view what) : Test_Error(what) {}
×
51
};
52

53
class Test_Options {
54
   public:
55
      Test_Options() = default;
56

57
      Test_Options(const std::vector<std::string>& requested_tests,
1✔
58
                   const std::vector<std::string>& skip_tests,
59
                   const std::string& data_dir,
60
                   const std::string& pkcs11_lib,
61
                   const std::string& provider,
62
                   const std::string& tpm2_tcti_name,
63
                   const std::string& tpm2_tcti_conf,
64
                   size_t tpm2_persistent_rsa_handle,
65
                   size_t tpm2_persistent_ecc_handle,
66
                   const std::string& tpm2_persistent_auth_value,
67
                   const std::string& drbg_seed,
68
                   const std::string& xml_results_dir,
69
                   const std::vector<std::string>& report_properties,
70
                   size_t test_runs,
71
                   size_t test_threads,
72
                   bool verbose,
73
                   bool log_success,
74
                   bool run_online_tests,
75
                   bool run_long_tests,
76
                   bool run_memory_intensive_tests,
77
                   bool abort_on_first_fail,
78
                   bool no_stdout) :
1✔
79
            m_requested_tests(requested_tests),
1✔
80
            m_skip_tests(skip_tests),
1✔
81
            m_data_dir(data_dir),
1✔
82
            m_pkcs11_lib(pkcs11_lib),
1✔
83
            m_provider(provider),
1✔
84
            m_tpm2_tcti_name(tpm2_tcti_name),
1✔
85
            m_tpm2_tcti_conf(tpm2_tcti_conf),
1✔
86
            m_tpm2_persistent_rsa_handle(tpm2_persistent_rsa_handle),
1✔
87
            m_tpm2_persistent_ecc_handle(tpm2_persistent_ecc_handle),
1✔
88
            m_tpm2_persistent_auth_value(tpm2_persistent_auth_value),
1✔
89
            m_drbg_seed(drbg_seed),
1✔
90
            m_xml_results_dir(xml_results_dir),
1✔
91
            m_report_properties(report_properties),
1✔
92
            m_test_runs(test_runs),
1✔
93
            m_test_threads(test_threads),
1✔
94
            m_verbose(verbose),
1✔
95
            m_log_success(log_success),
1✔
96
            m_run_online_tests(run_online_tests),
1✔
97
            m_run_long_tests(run_long_tests),
1✔
98
            m_run_memory_intensive_tests(run_memory_intensive_tests),
1✔
99
            m_abort_on_first_fail(abort_on_first_fail),
1✔
100
            m_no_stdout(no_stdout) {}
1✔
101

102
      const std::vector<std::string>& requested_tests() const { return m_requested_tests; }
1✔
103

104
      const std::vector<std::string>& skip_tests() const { return m_skip_tests; }
1✔
105

106
      const std::string& data_dir() const { return m_data_dir; }
107

108
      const std::string& pkcs11_lib() const { return m_pkcs11_lib; }
3✔
109

110
      const std::string& provider() const { return m_provider; }
2✔
111

112
      const std::optional<std::string>& tpm2_tcti_name() const { return m_tpm2_tcti_name; }
113

114
      const std::optional<std::string>& tpm2_tcti_conf() const { return m_tpm2_tcti_conf; }
115

116
      uint32_t tpm2_persistent_rsa_handle() const { return static_cast<uint32_t>(m_tpm2_persistent_rsa_handle); }
2✔
117

118
      uint32_t tpm2_persistent_ecc_handle() const { return static_cast<uint32_t>(m_tpm2_persistent_ecc_handle); }
2✔
119

120
      const std::string& tpm2_persistent_auth_value() const { return m_tpm2_persistent_auth_value; }
121

122
      const std::string& drbg_seed() const { return m_drbg_seed; }
1✔
123

124
      const std::string& xml_results_dir() const { return m_xml_results_dir; }
1✔
125

126
      const std::vector<std::string>& report_properties() const { return m_report_properties; }
1✔
127

128
      size_t test_runs() const { return m_test_runs; }
4✔
129

130
      size_t test_threads() const { return m_test_threads; }
3✔
131

132
      bool log_success() const { return m_log_success; }
3,620,777✔
133

134
      bool run_online_tests() const { return m_run_online_tests; }
2✔
135

136
      bool run_long_tests() const { return m_run_long_tests; }
2,871✔
137

138
      bool run_memory_intensive_tests() const { return m_run_memory_intensive_tests; }
1✔
139

140
      bool abort_on_first_fail() const { return m_abort_on_first_fail; }
24✔
141

142
      bool no_stdout() const { return m_no_stdout; }
1✔
143

144
      bool verbose() const { return m_verbose; }
2,557✔
145

146
   private:
147
      std::vector<std::string> m_requested_tests;
148
      std::vector<std::string> m_skip_tests;
149
      std::string m_data_dir;
150
      std::string m_pkcs11_lib;
151
      std::string m_provider;
152
      std::optional<std::string> m_tpm2_tcti_name;
153
      std::optional<std::string> m_tpm2_tcti_conf;
154
      size_t m_tpm2_persistent_rsa_handle = 0;
155
      size_t m_tpm2_persistent_ecc_handle = 0;
156
      std::string m_tpm2_persistent_auth_value;
157
      std::string m_drbg_seed;
158
      std::string m_xml_results_dir;
159
      std::vector<std::string> m_report_properties;
160
      size_t m_test_runs = 0;
161
      size_t m_test_threads = 0;
162
      bool m_verbose = false;
163
      bool m_log_success = false;
164
      bool m_run_online_tests = false;
165
      bool m_run_long_tests = false;
166
      bool m_run_memory_intensive_tests = false;
167
      bool m_abort_on_first_fail = false;
168
      bool m_no_stdout = false;
169
};
170

171
/**
172
 * A code location consisting of the source file path and a line
173
 */
174
struct CodeLocation {
175
      const char* path;
176
      unsigned int line;
177
};
178

179
/*
180
* A generic test which returns a set of results when run.
181
* The tests may not all have the same type (for example test
182
* "block" returns results for "AES-128" and "AES-256").
183
*
184
* For most test cases you want Text_Based_Test derived below
185
*/
186
class Test {
187
   public:
188
      /*
189
      * Some number of test results, all associated with who()
190
      */
191
      class Result final {
192
         public:
193
            explicit Result(std::string_view who);
194

195
            /**
196
             * This 'consolidation constructor' creates a single test result from
197
             * a vector of downstream test result objects.
198
             */
199
            Result(std::string_view who, const std::vector<Result>& downstream_results);
200

201
            size_t tests_passed() const { return m_tests_passed; }
12,668✔
202

203
            size_t tests_failed() const { return m_fail_log.size(); }
69,004✔
204

205
            size_t tests_run() const { return tests_passed() + tests_failed(); }
12,668✔
206

207
            bool any_results() const { return tests_run() > 0; }
208

209
            const std::string& who() const { return m_who; }
236,130✔
210

211
            const std::vector<std::string>& failures() const { return m_fail_log; }
2,532✔
212

213
            const std::vector<std::string>& notes() const { return m_log; }
2,532✔
214

215
            std::optional<uint64_t> elapsed_time() const {
2,532✔
216
               if(m_ns_taken == 0) {
2,532✔
217
                  return std::nullopt;
218
               } else {
219
                  return m_ns_taken;
1,473✔
220
               }
221
            }
222

223
            // Nanoseconds since epoch
224
            uint64_t timestamp() const { return m_timestamp; }
2,532✔
225

226
            std::string result_string() const;
227

228
            static Result Failure(std::string_view who, std::string_view what) {
1✔
229
               Result r(who);
1✔
230
               r.test_failure(what);
1✔
231
               return r;
1✔
232
            }
×
233

234
            static Result Note(std::string_view who, std::string_view what) {
×
235
               Result r(who);
×
236
               r.test_note(what);
×
237
               return r;
×
238
            }
×
239

240
            void merge(const Result& other, bool ignore_test_name = false);
241

242
            /* Test reporting functions */
243

244
            void test_note(std::string_view note, const char* extra = nullptr);
245

246
            void test_note(std::string_view note, std::span<const uint8_t> context);
247

248
            void note_missing(std::string_view whatever);
249

250
            bool test_success(std::string_view note = "");
251

252
            bool test_failure(std::string err);
253

254
            bool test_failure(std::string_view err);
255

256
            bool test_failure(const char* err);
257

258
            bool test_failure(std::string_view what, std::string_view error);
259

260
            void test_failure(std::string_view what, const uint8_t buf[], size_t buf_len);
261

262
            void test_failure(std::string_view what, std::span<const uint8_t> context);
263

264
            /**
265
             * Require a condition, throw Test_Aborted otherwise
266
             * Note: works best when combined with CHECK scopes!
267
             */
268
            void require(std::string_view what, bool expr);
269

270
            /* Generic arbitrary equality check */
271

272
            template <typename E>
273
               requires std::is_enum_v<E>
274
            bool test_enum_eq(std::string_view what, const E& produced, const E& expected) {
61✔
275
               auto produced_v = static_cast<std::underlying_type_t<E>>(produced);
61✔
276
               auto expected_v = static_cast<std::underlying_type_t<E>>(expected);
61✔
277
               return test_u64_eq(what, produced_v, expected_v);
59✔
278
            }
279

280
            template <typename T>
281
            bool test_not_null(std::string_view what, const T& ptr) {
2,873✔
282
               if(ptr == nullptr) {
2,873✔
283
                  return test_failure(what, "was null");
×
284
               } else {
285
                  return test_success("not null");
2,873✔
286
               }
287
            }
288

289
            /* String comparison predicates */
290
            bool test_str_not_empty(std::string_view what, std::string_view produced);
291

292
            bool test_str_eq(std::string_view what, std::string_view produced, std::string_view expected);
293

294
            bool test_str_ne(std::string_view what, std::string_view produced, std::string_view expected);
295

296
            /* Test predicates on bool */
297
            bool test_bool_eq(std::string_view what, bool produced, bool expected);
298

299
            bool test_is_false(std::string_view what, bool produced);
300

301
            bool test_is_true(std::string_view what, bool produced);
302

303
            /* Test predicates on size_t */
304
            bool test_sz_eq(std::string_view what, size_t produced, size_t expected);
305
            bool test_sz_ne(std::string_view what, size_t produced, size_t expected);
306
            bool test_sz_lt(std::string_view what, size_t produced, size_t expected);
307
            bool test_sz_lte(std::string_view what, size_t produced, size_t expected);
308
            bool test_sz_gt(std::string_view what, size_t produced, size_t expected);
309
            bool test_sz_gte(std::string_view what, size_t produced, size_t expected);
310

311
            /* Type-hinted unsigned integer equality predicates */
312
            bool test_u8_eq(std::string_view what, uint8_t produced, uint8_t expected);
313
            bool test_u16_eq(std::string_view what, uint16_t produced, uint16_t expected);
314
            bool test_u32_eq(std::string_view what, uint32_t produced, uint32_t expected);
315
            bool test_u64_eq(std::string_view what, uint64_t produced, uint64_t expected);
316
            bool test_i16_eq(std::string_view what, int16_t produced, int16_t expected);
317
            bool test_i32_eq(std::string_view what, int32_t produced, int32_t expected);
318

319
            /* Prefer the versions that take a descriptor string above */
320
            bool test_u8_eq(uint8_t produced, uint8_t expected);
321
            bool test_u16_eq(uint16_t produced, uint16_t expected);
322
            bool test_u32_eq(uint32_t produced, uint32_t expected);
323
            bool test_u64_eq(uint64_t produced, uint64_t expected);
324

325
            /* Test predicates on integer return codes */
326
            bool test_rc_ok(std::string_view func, int rc);
327
            bool test_rc_fail(std::string_view func, std::string_view why, int rc);
328
            bool test_rc(std::string_view func, int rc, int expected);
329
            bool test_rc_init(std::string_view func, int rc);
330

331
            /* Test predicates on optional values */
332

333
            template <typename T>
334
            bool test_opt_not_null(std::string_view what, const std::optional<T>& val) {
10✔
335
               if(val == std::nullopt) {
10✔
336
                  return test_failure(what, "was nullopt");
×
337
               } else {
338
                  return test_success("not nullopt");
10✔
339
               }
340
            }
341

342
            template <typename T>
343
            bool test_opt_is_null(std::string_view what, const std::optional<T>& val) {
5✔
344
               if(val == std::nullopt) {
5✔
345
                  return test_success("was nullopt");
5✔
346
               } else {
347
                  return test_failure(what, "not nullopt");
×
348
               }
349
            }
350

351
            bool test_opt_u8_eq(std::string_view what,
352
                                std::optional<uint8_t> produced,
353
                                std::optional<uint8_t> expected);
354

355
            bool test_opt_u64_eq(std::string_view what,
356
                                 std::optional<uint64_t> produced,
357
                                 std::optional<uint64_t> expected);
358

359
#if defined(BOTAN_HAS_BIGINT)
360
            /* Test predicates for BigInt */
361
            bool test_bn_eq(std::string_view what, const BigInt& produced, const BigInt& expected);
362
            bool test_bn_ne(std::string_view what, const BigInt& produced, const BigInt& expected);
363
#endif
364

365
            /* Test predicates for binary strings */
366
            bool test_bin_ne(std::string_view what,
367
                             std::span<const uint8_t> produced,
368
                             std::span<const uint8_t> expected);
369

370
            bool test_bin_eq(std::string_view what,
371
                             std::span<const uint8_t> produced,
372
                             std::span<const uint8_t> expected);
373

374
            bool test_bin_eq(std::string_view what, std::span<const uint8_t> produced, std::string_view expected_hex);
375

376
            template <std::size_t N>
377
            bool test_bin_eq(std::string_view what,
1,212✔
378
                             const std::array<uint8_t, N>& produced,
379
                             const std::array<uint8_t, N>& expected) {
380
               return test_bin_eq(what, std::span{produced}, std::span{expected});
1,186✔
381
            }
382

383
         private:
384
            class ThrowExpectations {
385
               public:
386
                  explicit ThrowExpectations(std::function<void()> fn) : m_fn(std::move(fn)) {}
73,518✔
387

388
                  ThrowExpectations(const ThrowExpectations&) = delete;
389
                  ThrowExpectations& operator=(const ThrowExpectations&) = delete;
390
                  ThrowExpectations(ThrowExpectations&&) = default;
391
                  ThrowExpectations& operator=(ThrowExpectations&&) = default;
392

393
                  ~ThrowExpectations();
394

395
                  ThrowExpectations& expect_success();
396

397
                  ThrowExpectations& expect_message(std::string_view message);
398

399
                  template <typename ExT>
400
                  ThrowExpectations& expect_exception_type() {
62,048✔
401
                     assert_that_success_is_not_expected();
62,048✔
402

403
                     m_expected_exception_check_fn = [](const std::exception_ptr& e) {
124,096✔
404
                        try {
405
                           if(e) {
62,048✔
406
                              std::rethrow_exception(e);
124,096✔
407
                           }
408
                        } catch(const ExT&) {
62,048✔
409
                           return true;
410
                        } catch(...) {
2✔
411
                           return false;
412
                        }
413
                        return false;
414
                     };
415
                     return *this;
416
                  }
417

418
                  bool check(std::string_view test_name, Test::Result& result);
419

420
               private:
421
                  void assert_that_success_is_not_expected() const;
422

423
                  std::function<void()> m_fn;
424
                  std::optional<std::string> m_expected_message;
425
                  std::function<bool(std::exception_ptr)> m_expected_exception_check_fn;
426
                  bool m_expect_success = false;
427
                  bool m_consumed = false;
428
            };
429

430
         public:
431
            bool test_throws(std::string_view what, std::function<void()> fn);
432

433
            bool test_throws(std::string_view what, std::string_view expected, std::function<void()> fn);
434

435
            bool test_no_throw(std::string_view what, std::function<void()> fn);
436

437
            template <typename ExceptionT>
438
            bool test_throws(std::string_view what, std::function<void()> fn) {
62,033✔
439
               return ThrowExpectations(std::move(fn)).expect_exception_type<ExceptionT>().check(what, *this);
186,099✔
440
            }
441

442
            template <typename ExceptionT>
443
            bool test_throws(std::string_view what, std::string_view expected, std::function<void()> fn) {
15✔
444
               // clang-format off
445
               return ThrowExpectations(std::move(fn)).expect_exception_type<ExceptionT>().expect_message(expected).check(what, *this);
45✔
446
               // clang-format on
447
            }
448

449
            void set_ns_consumed(uint64_t ns) { m_ns_taken = ns; }
48,267✔
450

451
            void start_timer();
452
            void end_timer();
453

454
            void set_code_location(CodeLocation where) { m_where = where; }
50,724✔
455

456
            const std::optional<CodeLocation>& code_location() const { return m_where; }
53,256✔
457

458
         private:
459
            std::string m_who;
460
            std::optional<CodeLocation> m_where;
461
            uint64_t m_timestamp;
462
            uint64_t m_started = 0;
463
            uint64_t m_ns_taken = 0;
464
            size_t m_tests_passed = 0;
465
            std::vector<std::string> m_fail_log;
466
            std::vector<std::string> m_log;
467
      };
468

469
      virtual ~Test();
470

471
      Test();
472
      Test(const Test& other) = delete;
473
      Test(Test&& other) = default;
474
      Test& operator=(const Test& other) = delete;
475
      Test& operator=(Test&& other) = delete;
476

477
      virtual std::vector<Test::Result> run() = 0;
478

479
      virtual std::vector<std::string> possible_providers(const std::string& alg);
480

481
      void initialize(std::string test_name, CodeLocation location);
482

483
      const std::string& test_name() const { return m_test_name; }
23✔
484

485
      Botan::RandomNumberGenerator& rng() const;
486

487
      /**
488
       * Use this if a test needs some supported EC group but it is not relevant
489
       * which one exactly. This tries to find a commonly used group that is
490
       * both supported in this build and as small as possible (for test speed).
491
       *
492
       * If @p preferred_groups is non-empty, a group from that list is chosen
493
       *
494
       * @returns the name of a supported EC group, or std::nullopt if no
495
       *          supported EC group could be found for this build
496
       */
497
      static std::optional<std::string> supported_ec_group_name(std::vector<std::string> preferred_groups = {});
498

499
      const std::optional<CodeLocation>& registration_location() const { return m_registration_location; }
50,724✔
500

501
      /// @p smoke_test are run first in an unfiltered test run
502
      static void register_test(const std::string& category,
503
                                const std::string& name,
504
                                bool smoke_test,
505
                                bool needs_serialization,
506
                                std::function<std::unique_ptr<Test>()> maker_fn);
507

508
      static std::vector<std::string> registered_tests();
509
      static std::vector<std::string> registered_test_categories();
510

511
      static std::vector<std::string> filter_registered_tests(const std::vector<std::string>& requested,
512
                                                              const std::vector<std::string>& to_be_skipped);
513

514
      static std::unique_ptr<Test> get_test(const std::string& test_name);
515
      static bool test_needs_serialization(const std::string& test_name);
516

517
      static std::string data_dir(const std::string& subdir);
518
      static std::vector<std::string> files_in_data_dir(const std::string& subdir);
519
      static std::string data_file(const std::string& file);
520
      static std::string data_file_as_temporary_copy(const std::string& what);
521

522
      static std::string format_time(uint64_t nanoseconds);
523

524
      static std::vector<uint8_t> mutate_vec(const std::vector<uint8_t>& v,
525
                                             Botan::RandomNumberGenerator& rng,
526
                                             bool maybe_resize = false,
527
                                             size_t min_offset = 0);
528

529
      static void set_test_options(const Test_Options& opts);
530

531
      static void set_test_rng_seed(std::span<const uint8_t> seed, size_t epoch = 0);
532

533
      static const Test_Options& options() { return m_opts; }
534

535
      static bool run_long_tests() { return options().run_long_tests(); }
2,871✔
536

537
      static bool run_memory_intensive_tests() { return options().run_memory_intensive_tests(); }
1✔
538

539
      static const std::string& pkcs11_lib() { return options().pkcs11_lib(); }
50✔
540

541
      static std::string temp_file_name(const std::string& basename);
542
      static bool copy_file(const std::string& from, const std::string& to);
543

544
      static std::vector<std::string> provider_filter(const std::vector<std::string>& providers);
545

546
      static std::string read_data_file(const std::string& path);
547
      static std::vector<uint8_t> read_binary_data_file(const std::string& path);
548

549
      static std::unique_ptr<Botan::RandomNumberGenerator> new_rng(std::string_view test_name);
550
      static std::shared_ptr<Botan::RandomNumberGenerator> new_shared_rng(std::string_view test_name);
551

552
      static std::string random_password(Botan::RandomNumberGenerator& rng);
553
      static size_t random_index(Botan::RandomNumberGenerator& rng, size_t max);
554
      static uint64_t timestamp();  // nanoseconds arbitrary epoch
555

556
      static std::vector<Test::Result> flatten_result_lists(std::vector<std::vector<Test::Result>> result_lists);
557

558
   private:
559
      static Test_Options m_opts;
560
      static std::string m_test_rng_seed;
561

562
      /// The string ID that was used to register this test
563
      std::string m_test_name;
564
      /// The source file location where the test was registered
565
      std::optional<CodeLocation> m_registration_location;
566
      /// The test-specific RNG state
567
      mutable std::unique_ptr<Botan::RandomNumberGenerator> m_test_rng;
568
};
569

570
/*
571
* Register the test with the runner
572
*/
573
template <typename Test_Class>
574
class TestClassRegistration {
575
   public:
576
      TestClassRegistration(const std::string& category,
386✔
577
                            const std::string& name,
578
                            bool smoke_test,
579
                            bool needs_serialization,
580
                            const CodeLocation& registration_location) {
581
         Test::register_test(category, name, smoke_test, needs_serialization, [=] {
1,884✔
582
            auto test = std::make_unique<Test_Class>();
386✔
583
            test->initialize(name, registration_location);
772✔
584
            return test;
386✔
585
         });
×
586
      }
386✔
587
};
588

589
// NOLINTBEGIN(*-macro-usage)
590

591
#define BOTAN_REGISTER_TEST(category, name, Test_Class) \
592
   /* NOLINTNEXTLINE(cert-err58-cpp) */                 \
593
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, false, false, {__FILE__, __LINE__})
594
#define BOTAN_REGISTER_SERIALIZED_TEST(category, name, Test_Class) \
595
   /* NOLINTNEXTLINE(cert-err58-cpp) */                            \
596
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, false, true, {__FILE__, __LINE__})
597
#define BOTAN_REGISTER_SMOKE_TEST(category, name, Test_Class) \
598
   /* NOLINTNEXTLINE(cert-err58-cpp) */                       \
599
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, true, false, {__FILE__, __LINE__})
600
#define BOTAN_REGISTER_SERIALIZED_SMOKE_TEST(category, name, Test_Class) \
601
   /* NOLINTNEXTLINE(cert-err58-cpp) */                                  \
602
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, true, true, {__FILE__, __LINE__})
603

604
// NOLINTEND(*-macro-usage)
605

606
typedef Test::Result (*test_fn)();
607
typedef std::vector<Test::Result> (*test_fn_vec)();
608

609
class FnTest : public Test {
610
   private:
611
      using TestFnVariant = std::variant<test_fn, test_fn_vec>;
612

613
      template <typename TestFn>
614
      std::vector<TestFnVariant> make_variant_vector(TestFn fn) {
34✔
615
         using T = std::decay_t<decltype(fn)>;
616
         static_assert(std::is_same_v<T, test_fn> || std::is_same_v<T, test_fn_vec>,
617
                       "functions passed to BOTAN_REGISTER_TEST_FN must either return a "
618
                       "single Test::Result or a std::vector of Test::Result");
619
         return {fn};
34✔
620
      }
621

622
      template <typename TestFn, typename... TestFns>
623
      std::vector<TestFnVariant> make_variant_vector(const TestFn& fn, const TestFns&... fns) {
31✔
624
         auto functions = make_variant_vector(fns...);
31✔
625
         functions.emplace_back(fn);
31✔
626
         return functions;
31✔
627
      }
×
628

629
   public:
630
      template <typename... TestFns>
631
      explicit FnTest(TestFns... fns) : m_fns(make_variant_vector(fns...)) {}
34✔
632

633
      std::vector<Test::Result> run() override {
34✔
634
         std::vector<Test::Result> result;
34✔
635

636
         // TODO(Botan4) use std::ranges::reverse_view here once available (need newer Clang)
637
         // NOLINTNEXTLINE(modernize-loop-convert)
638
         for(auto fn_variant = m_fns.crbegin(); fn_variant != m_fns.crend(); ++fn_variant) {
99✔
639
            std::visit(
65✔
640
               [&](auto&& fn) {
130✔
641
                  using T = std::decay_t<decltype(fn)>;
642
                  if constexpr(std::is_same_v<T, test_fn>) {
643
                     result.emplace_back(fn());
7✔
644
                  } else {
645
                     const auto results = fn();
58✔
646
                     result.insert(result.end(), results.begin(), results.end());
58✔
647
                  }
58✔
648
               },
65✔
649
               *fn_variant);
65✔
650
         }
651

652
         return result;
34✔
653
      }
×
654

655
   private:
656
      std::vector<TestFnVariant> m_fns;
657
};
658

659
class TestFnRegistration {
660
   public:
661
      template <typename... TestFns>
662
      TestFnRegistration(const std::string& category,
34✔
663
                         const std::string& name,
664
                         bool smoke_test,
665
                         bool needs_serialization,
666
                         const CodeLocation& registration_location,
667
                         TestFns... fn) {
668
         Test::register_test(category, name, smoke_test, needs_serialization, [=] {
125✔
669
            auto test = std::make_unique<FnTest>(fn...);
34✔
670
            test->initialize(name, registration_location);
68✔
671
            return test;
34✔
672
         });
×
673
      }
34✔
674
};
675

676
// NOLINTBEGIN(*-macro-usage)
677

678
#define BOTAN_REGISTER_TEST_FN_IMPL(category, name, smoke_test, needs_serialization, fn0, ...) \
679
   /* NOLINTNEXTLINE(cert-err58-cpp) */                                                        \
680
   static const TestFnRegistration register_##fn0(                                             \
681
      category, name, smoke_test, needs_serialization, {__FILE__, __LINE__}, fn0 __VA_OPT__(, ) __VA_ARGS__)
682

683
#define BOTAN_REGISTER_TEST_FN(category, name, ...) \
684
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, false, false, __VA_ARGS__)
685
#define BOTAN_REGISTER_SMOKE_TEST_FN(category, name, ...) \
686
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, true, false, __VA_ARGS__)
687
#define BOTAN_REGISTER_SERIALIZED_TEST_FN(category, name, ...) \
688
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, false, true, __VA_ARGS__)
689
#define BOTAN_REGISTER_SERIALIZED_SMOKE_TEST_FN(category, name, ...) \
690
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, true, true, __VA_ARGS__)
691

692
// NOLINTEND(*-macro-usage)
693

694
class VarMap {
181✔
695
   public:
696
      bool has_key(const std::string& key) const;
697

698
      bool get_req_bool(const std::string& key) const;
699

700
      std::vector<uint8_t> get_req_bin(const std::string& key) const;
701
      std::vector<uint8_t> get_opt_bin(const std::string& key) const;
702

703
      std::vector<std::vector<uint8_t>> get_req_bin_list(const std::string& key) const;
704

705
#if defined(BOTAN_HAS_BIGINT)
706
      Botan::BigInt get_req_bn(const std::string& key) const;
707
      Botan::BigInt get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const;
708
#endif
709

710
      std::string get_req_str(const std::string& key) const;
711
      std::string get_opt_str(const std::string& key, const std::string& def_value) const;
712

713
      size_t get_req_sz(const std::string& key) const;
714

715
      uint8_t get_req_u8(const std::string& key) const;
716
      uint32_t get_req_u32(const std::string& key) const;
717
      uint64_t get_req_u64(const std::string& key) const;
718

719
      size_t get_opt_sz(const std::string& key, size_t def_value) const;
720

721
      uint64_t get_opt_u64(const std::string& key, uint64_t def_value) const;
722

723
      void clear();
724

725
      void add(const std::string& key, const std::string& value);
726

727
   private:
728
      std::optional<std::string> get_var(const std::string& key) const;
729

730
      std::vector<std::pair<std::string, std::string>> m_vars;
731
};
732

733
/*
734
* A test based on reading an input file which contains key/value pairs
735
* Special note: the last value in required_key (there must be at least
736
* one), is the output key. This triggers the callback.
737
*
738
* Calls run_one_test with the variables set. If an ini-style [header]
739
* is used in the file, then header will be set to that value. This allows
740
* splitting up tests between [valid] and [invalid] tests, or different
741
* related algorithms tested in the same file. Use the get_XXX functions
742
* on VarMap to retrieve formatted values.
743
*
744
* If most of your tests are text-based but you find yourself with a few
745
* odds-and-ends tests that you want to do, override run_final_tests which
746
* can test whatever it likes and returns a vector of Results.
747
*/
748
class Text_Based_Test : public Test {
749
   public:
750
      Text_Based_Test(const std::string& data_src,
751
                      const std::string& required_keys_str,
752
                      const std::string& optional_keys_str = "");
753

754
      Text_Based_Test(const Text_Based_Test& other) = delete;
755
      Text_Based_Test(Text_Based_Test&& other) = default;
756
      Text_Based_Test& operator=(const Text_Based_Test& other) = delete;
757
      Text_Based_Test& operator=(Text_Based_Test&& other) = delete;
758

759
      ~Text_Based_Test() override;
760

761
      virtual bool clear_between_callbacks() const { return true; }
33,492✔
762

763
      std::vector<Test::Result> run() override;
764

765
   private:
766
      virtual Test::Result run_one_test(const std::string& header, const VarMap& vars) = 0;
767
      // Called before run_one_test
768
      virtual bool skip_this_test(const std::string& header, const VarMap& vars);
769

770
      virtual std::vector<Test::Result> run_final_tests() { return std::vector<Test::Result>(); }
172✔
771

772
   private:
773
      class Text_Based_Test_Data;
774
      std::unique_ptr<Text_Based_Test_Data> m_data;
775
};
776

777
/**
778
 * This is a convenience wrapper to write small self-contained and in particular
779
 * exception-safe unit tests. If some (unexpected) exception is thrown in one of
780
 * the CHECK-scopes, it will fail the particular test gracefully with a human-
781
 * understandable failure output.
782
 *
783
 * Example Usage:
784
 *
785
 * ```
786
 * std::vector<Test::Result> test_something()
787
 *    {
788
 *    return
789
 *       {
790
 *       CHECK("some unit test name", [](Test::Result& r)
791
 *          {
792
 *          r.test_is_true("some observation", 1+1 == 2);
793
 *          }),
794
 *       CHECK("some other unit test name", [](Test::Result& r)
795
 *          {
796
 *          // ...
797
 *          })
798
 *       };
799
 *    }
800
 *
801
 * BOTAN_REGISTER_TEST_FN("some_category", "some_test_name", test_something);
802
 * ```
803
 */
804
template <typename FunT>
805
Test::Result CHECK(const char* name, FunT check_fun) {
508✔
806
   Botan_Tests::Test::Result r(name);
508✔
807

808
   try {
809
      check_fun(r);
508✔
810
   } catch(const Botan_Tests::Test_Aborted&) {
×
811
      // pass, failure was already noted in the responsible `require`
812
   } catch(const std::exception& ex) {
×
813
      r.test_failure("failed unexpectedly", ex.what());
×
814
   } catch(...) {
×
815
      r.test_failure("failed with unknown exception");
×
816
   }
817

818
   return r;
507✔
819
}
×
820

821
}  // namespace Botan_Tests
822

823
#endif
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