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

randombit / botan / 22039223523

15 Feb 2026 04:36PM UTC coverage: 90.043% (-0.01%) from 90.054%
22039223523

Pull #5342

github

web-flow
Merge 8e27129db into 6d81b0c0a
Pull Request #5342: Rename test_is_eq to test_arb_eq and require the type be printable

102325 of 113640 relevant lines covered (90.04%)

11415960.52 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,796✔
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, bool expected = true);
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) {
31✔
275
               auto produced_v = static_cast<std::underlying_type_t<E>>(produced);
31✔
276
               auto expected_v = static_cast<std::underlying_type_t<E>>(expected);
31✔
277
               return test_u64_eq(what, produced_v, expected_v);
29✔
278
            }
279

280
            template <typename T>
281
            bool test_not_null(std::string_view what, const T& ptr) {
1,736✔
282
               if(ptr == nullptr) {
1,736✔
283
                  return test_failure(what, "was null");
×
284
               } else {
285
                  return test_success("not null");
1,736✔
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, std::optional<uint8_t> a, std::optional<uint8_t> b);
352

353
#if defined(BOTAN_HAS_BIGINT)
354
            /* Test predicates for BigInt */
355
            bool test_bn_eq(std::string_view what, const BigInt& produced, const BigInt& expected);
356
            bool test_bn_ne(std::string_view what, const BigInt& produced, const BigInt& expected);
357
#endif
358

359
            /* Test predicates for binary strings */
360
            bool test_bin_ne(std::string_view what,
361
                             std::span<const uint8_t> produced,
362
                             std::span<const uint8_t> expected);
363

364
            bool test_bin_eq(std::string_view what,
365
                             std::span<const uint8_t> produced,
366
                             std::span<const uint8_t> expected);
367

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

370
            template <std::size_t N>
371
            bool test_bin_eq(std::string_view what,
1,212✔
372
                             const std::array<uint8_t, N>& produced,
373
                             const std::array<uint8_t, N>& expected) {
374
               return test_bin_eq(what, std::span{produced}, std::span{expected});
1,186✔
375
            }
376

377
         private:
378
            class ThrowExpectations {
379
               public:
380
                  explicit ThrowExpectations(std::function<void()> fn) : m_fn(std::move(fn)) {}
73,518✔
381

382
                  ThrowExpectations(const ThrowExpectations&) = delete;
383
                  ThrowExpectations& operator=(const ThrowExpectations&) = delete;
384
                  ThrowExpectations(ThrowExpectations&&) = default;
385
                  ThrowExpectations& operator=(ThrowExpectations&&) = default;
386

387
                  ~ThrowExpectations();
388

389
                  ThrowExpectations& expect_success();
390

391
                  ThrowExpectations& expect_message(std::string_view message);
392

393
                  template <typename ExT>
394
                  ThrowExpectations& expect_exception_type() {
62,048✔
395
                     assert_that_success_is_not_expected();
62,048✔
396

397
                     m_expected_exception_check_fn = [](const std::exception_ptr& e) {
124,096✔
398
                        try {
399
                           if(e) {
62,048✔
400
                              std::rethrow_exception(e);
124,096✔
401
                           }
402
                        } catch(const ExT&) {
62,048✔
403
                           return true;
404
                        } catch(...) {
2✔
405
                           return false;
406
                        }
407
                        return false;
408
                     };
409
                     return *this;
410
                  }
411

412
                  bool check(std::string_view test_name, Test::Result& result);
413

414
               private:
415
                  void assert_that_success_is_not_expected() const;
416

417
                  std::function<void()> m_fn;
418
                  std::optional<std::string> m_expected_message;
419
                  std::function<bool(std::exception_ptr)> m_expected_exception_check_fn;
420
                  bool m_expect_success = false;
421
                  bool m_consumed = false;
422
            };
423

424
         public:
425
            bool test_throws(std::string_view what, std::function<void()> fn);
426

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

429
            bool test_no_throw(std::string_view what, std::function<void()> fn);
430

431
            template <typename ExceptionT>
432
            bool test_throws(std::string_view what, std::function<void()> fn) {
62,033✔
433
               return ThrowExpectations(std::move(fn)).expect_exception_type<ExceptionT>().check(what, *this);
186,099✔
434
            }
435

436
            template <typename ExceptionT>
437
            bool test_throws(std::string_view what, std::string_view expected, std::function<void()> fn) {
15✔
438
               // clang-format off
439
               return ThrowExpectations(std::move(fn)).expect_exception_type<ExceptionT>().expect_message(expected).check(what, *this);
45✔
440
               // clang-format on
441
            }
442

443
            void set_ns_consumed(uint64_t ns) { m_ns_taken = ns; }
48,267✔
444

445
            void start_timer();
446
            void end_timer();
447

448
            void set_code_location(CodeLocation where) { m_where = where; }
50,724✔
449

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

452
         private:
453
            std::string m_who;
454
            std::optional<CodeLocation> m_where;
455
            uint64_t m_timestamp;
456
            uint64_t m_started = 0;
457
            uint64_t m_ns_taken = 0;
458
            size_t m_tests_passed = 0;
459
            std::vector<std::string> m_fail_log;
460
            std::vector<std::string> m_log;
461
      };
462

463
      virtual ~Test();
464

465
      Test();
466
      Test(const Test& other) = delete;
467
      Test(Test&& other) = default;
468
      Test& operator=(const Test& other) = delete;
469
      Test& operator=(Test&& other) = delete;
470

471
      virtual std::vector<Test::Result> run() = 0;
472

473
      virtual std::vector<std::string> possible_providers(const std::string& alg);
474

475
      void initialize(std::string test_name, CodeLocation location);
476

477
      const std::string& test_name() const { return m_test_name; }
23✔
478

479
      Botan::RandomNumberGenerator& rng() const;
480

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

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

495
      /// @p smoke_test are run first in an unfiltered test run
496
      static void register_test(const std::string& category,
497
                                const std::string& name,
498
                                bool smoke_test,
499
                                bool needs_serialization,
500
                                std::function<std::unique_ptr<Test>()> maker_fn);
501

502
      static std::vector<std::string> registered_tests();
503
      static std::vector<std::string> registered_test_categories();
504

505
      static std::vector<std::string> filter_registered_tests(const std::vector<std::string>& requested,
506
                                                              const std::vector<std::string>& to_be_skipped);
507

508
      static std::unique_ptr<Test> get_test(const std::string& test_name);
509
      static bool test_needs_serialization(const std::string& test_name);
510

511
      static std::string data_dir(const std::string& subdir);
512
      static std::vector<std::string> files_in_data_dir(const std::string& subdir);
513
      static std::string data_file(const std::string& file);
514
      static std::string data_file_as_temporary_copy(const std::string& what);
515

516
      static std::string format_time(uint64_t nanoseconds);
517

518
      static std::vector<uint8_t> mutate_vec(const std::vector<uint8_t>& v,
519
                                             Botan::RandomNumberGenerator& rng,
520
                                             bool maybe_resize = false,
521
                                             size_t min_offset = 0);
522

523
      static void set_test_options(const Test_Options& opts);
524

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

527
      static const Test_Options& options() { return m_opts; }
528

529
      static bool run_long_tests() { return options().run_long_tests(); }
2,871✔
530

531
      static bool run_memory_intensive_tests() { return options().run_memory_intensive_tests(); }
1✔
532

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

535
      static std::string temp_file_name(const std::string& basename);
536
      static bool copy_file(const std::string& from, const std::string& to);
537

538
      static std::vector<std::string> provider_filter(const std::vector<std::string>& providers);
539

540
      static std::string read_data_file(const std::string& path);
541
      static std::vector<uint8_t> read_binary_data_file(const std::string& path);
542

543
      static std::unique_ptr<Botan::RandomNumberGenerator> new_rng(std::string_view test_name);
544
      static std::shared_ptr<Botan::RandomNumberGenerator> new_shared_rng(std::string_view test_name);
545

546
      static std::string random_password(Botan::RandomNumberGenerator& rng);
547
      static size_t random_index(Botan::RandomNumberGenerator& rng, size_t max);
548
      static uint64_t timestamp();  // nanoseconds arbitrary epoch
549

550
      static std::vector<Test::Result> flatten_result_lists(std::vector<std::vector<Test::Result>> result_lists);
551

552
   private:
553
      static Test_Options m_opts;
554
      static std::string m_test_rng_seed;
555

556
      /// The string ID that was used to register this test
557
      std::string m_test_name;
558
      /// The source file location where the test was registered
559
      std::optional<CodeLocation> m_registration_location;
560
      /// The test-specific RNG state
561
      mutable std::unique_ptr<Botan::RandomNumberGenerator> m_test_rng;
562
};
563

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

583
// NOLINTBEGIN(*-macro-usage)
584

585
#define BOTAN_REGISTER_TEST(category, name, Test_Class) \
586
   /* NOLINTNEXTLINE(cert-err58-cpp) */                 \
587
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, false, false, {__FILE__, __LINE__})
588
#define BOTAN_REGISTER_SERIALIZED_TEST(category, name, Test_Class) \
589
   /* NOLINTNEXTLINE(cert-err58-cpp) */                            \
590
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, false, true, {__FILE__, __LINE__})
591
#define BOTAN_REGISTER_SMOKE_TEST(category, name, Test_Class) \
592
   /* NOLINTNEXTLINE(cert-err58-cpp) */                       \
593
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, true, false, {__FILE__, __LINE__})
594
#define BOTAN_REGISTER_SERIALIZED_SMOKE_TEST(category, name, Test_Class) \
595
   /* NOLINTNEXTLINE(cert-err58-cpp) */                                  \
596
   const TestClassRegistration<Test_Class> reg_##Test_Class##_tests(category, name, true, true, {__FILE__, __LINE__})
597

598
// NOLINTEND(*-macro-usage)
599

600
typedef Test::Result (*test_fn)();
601
typedef std::vector<Test::Result> (*test_fn_vec)();
602

603
class FnTest : public Test {
604
   private:
605
      using TestFnVariant = std::variant<test_fn, test_fn_vec>;
606

607
      template <typename TestFn>
608
      std::vector<TestFnVariant> make_variant_vector(TestFn fn) {
34✔
609
         using T = std::decay_t<decltype(fn)>;
610
         static_assert(std::is_same_v<T, test_fn> || std::is_same_v<T, test_fn_vec>,
611
                       "functions passed to BOTAN_REGISTER_TEST_FN must either return a "
612
                       "single Test::Result or a std::vector of Test::Result");
613
         return {fn};
34✔
614
      }
615

616
      template <typename TestFn, typename... TestFns>
617
      std::vector<TestFnVariant> make_variant_vector(const TestFn& fn, const TestFns&... fns) {
31✔
618
         auto functions = make_variant_vector(fns...);
31✔
619
         functions.emplace_back(fn);
31✔
620
         return functions;
31✔
621
      }
×
622

623
   public:
624
      template <typename... TestFns>
625
      explicit FnTest(TestFns... fns) : m_fns(make_variant_vector(fns...)) {}
34✔
626

627
      std::vector<Test::Result> run() override {
34✔
628
         std::vector<Test::Result> result;
34✔
629

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

646
         return result;
34✔
647
      }
×
648

649
   private:
650
      std::vector<TestFnVariant> m_fns;
651
};
652

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

670
// NOLINTBEGIN(*-macro-usage)
671

672
#define BOTAN_REGISTER_TEST_FN_IMPL(category, name, smoke_test, needs_serialization, fn0, ...) \
673
   /* NOLINTNEXTLINE(cert-err58-cpp) */                                                        \
674
   static const TestFnRegistration register_##fn0(                                             \
675
      category, name, smoke_test, needs_serialization, {__FILE__, __LINE__}, fn0 __VA_OPT__(, ) __VA_ARGS__)
676

677
#define BOTAN_REGISTER_TEST_FN(category, name, ...) \
678
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, false, false, __VA_ARGS__)
679
#define BOTAN_REGISTER_SMOKE_TEST_FN(category, name, ...) \
680
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, true, false, __VA_ARGS__)
681
#define BOTAN_REGISTER_SERIALIZED_TEST_FN(category, name, ...) \
682
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, false, true, __VA_ARGS__)
683
#define BOTAN_REGISTER_SERIALIZED_SMOKE_TEST_FN(category, name, ...) \
684
   BOTAN_REGISTER_TEST_FN_IMPL(category, name, true, true, __VA_ARGS__)
685

686
// NOLINTEND(*-macro-usage)
687

688
class VarMap {
181✔
689
   public:
690
      bool has_key(const std::string& key) const;
691

692
      bool get_req_bool(const std::string& key) const;
693

694
      std::vector<uint8_t> get_req_bin(const std::string& key) const;
695
      std::vector<uint8_t> get_opt_bin(const std::string& key) const;
696

697
      std::vector<std::vector<uint8_t>> get_req_bin_list(const std::string& key) const;
698

699
#if defined(BOTAN_HAS_BIGINT)
700
      Botan::BigInt get_req_bn(const std::string& key) const;
701
      Botan::BigInt get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const;
702
#endif
703

704
      std::string get_req_str(const std::string& key) const;
705
      std::string get_opt_str(const std::string& key, const std::string& def_value) const;
706

707
      size_t get_req_sz(const std::string& key) const;
708

709
      uint8_t get_req_u8(const std::string& key) const;
710
      uint32_t get_req_u32(const std::string& key) const;
711
      uint64_t get_req_u64(const std::string& key) const;
712

713
      size_t get_opt_sz(const std::string& key, size_t def_value) const;
714

715
      uint64_t get_opt_u64(const std::string& key, uint64_t def_value) const;
716

717
      void clear();
718

719
      void add(const std::string& key, const std::string& value);
720

721
   private:
722
      std::optional<std::string> get_var(const std::string& key) const;
723

724
      std::vector<std::pair<std::string, std::string>> m_vars;
725
};
726

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

748
      Text_Based_Test(const Text_Based_Test& other) = delete;
749
      Text_Based_Test(Text_Based_Test&& other) = default;
750
      Text_Based_Test& operator=(const Text_Based_Test& other) = delete;
751
      Text_Based_Test& operator=(Text_Based_Test&& other) = delete;
752

753
      ~Text_Based_Test() override;
754

755
      virtual bool clear_between_callbacks() const { return true; }
33,492✔
756

757
      std::vector<Test::Result> run() override;
758

759
   private:
760
      virtual Test::Result run_one_test(const std::string& header, const VarMap& vars) = 0;
761
      // Called before run_one_test
762
      virtual bool skip_this_test(const std::string& header, const VarMap& vars);
763

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

766
   private:
767
      class Text_Based_Test_Data;
768
      std::unique_ptr<Text_Based_Test_Data> m_data;
769
};
770

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

802
   try {
803
      check_fun(r);
508✔
804
   } catch(const Botan_Tests::Test_Aborted&) {
×
805
      // pass, failure was already noted in the responsible `require`
806
   } catch(const std::exception& ex) {
×
807
      r.test_failure("failed unexpectedly", ex.what());
×
808
   } catch(...) {
×
809
      r.test_failure("failed with unknown exception");
×
810
   }
811

812
   return r;
507✔
813
}
×
814

815
}  // namespace Botan_Tests
816

817
#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