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

randombit / botan / 21396248871

27 Jan 2026 11:57AM UTC coverage: 90.071% (-0.002%) from 90.073%
21396248871

Pull #5266

github

web-flow
Merge 277923e43 into 0d718b146
Pull Request #5266: Avoid compiling test_simd.cpp with -m enabling flags

102109 of 113365 relevant lines covered (90.07%)

11431253.16 hits per line

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

76.24
/src/tests/runner/test_runner.cpp
1
/*
2
* (C) 2017 Jack Lloyd
3
* (C) 2022 René Meusel, Rohde & Schwarz Cybersecurity
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "../tests.h"
9

10
#include "test_runner.h"
11
#include "test_stdout_reporter.h"
12
#include "test_xml_reporter.h"
13

14
#include <botan/version.h>
15
#include <botan/internal/loadstor.h>
16

17
#if defined(BOTAN_HAS_CPUID)
18
   #include <botan/internal/cpuid.h>
19
#endif
20

21
#if defined(BOTAN_HAS_THREAD_UTILS)
22
   #include <botan/internal/rwlock.h>
23
   #include <botan/internal/thread_pool.h>
24
#endif
25

26
#include <shared_mutex>
27

28
namespace Botan_Tests {
29

30
Test_Runner::Test_Runner(std::ostream& out) : m_output(out) {}
1✔
31

32
Test_Runner::~Test_Runner() = default;
1✔
33

34
bool Test_Runner::run(const Test_Options& options) {
1✔
35
   if(!options.no_stdout()) {
1✔
36
      m_reporters.emplace_back(std::make_unique<StdoutReporter>(options, output()));
2✔
37
   }
38
   if(!options.xml_results_dir().empty()) {
1✔
39
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
40
      m_reporters.emplace_back(std::make_unique<XmlReporter>(options, options.xml_results_dir()));
2✔
41
#else
42
      output() << "Generating test report files is not supported on this platform\n";
43
#endif
44
   }
45

46
   auto req = Botan_Tests::Test::filter_registered_tests(options.requested_tests(), options.skip_tests());
1✔
47

48
   // TODO: Test runner should not be aware of certain test's environmental requirements.
49
   if(options.pkcs11_lib().empty()) {
1✔
50
      // do not run pkcs11 tests by default unless pkcs11-lib set
51
      for(auto iter = req.begin(); iter != req.end();) {
×
52
         if((*iter).find("pkcs11") != std::string::npos) {
×
53
            iter = req.erase(iter);
×
54
         } else {
55
            ++iter;
×
56
         }
57
      }
58
   }
59

60
   if(req.empty()) {
1✔
61
      throw Test_Error("No tests to run");
×
62
   }
63

64
   std::vector<uint8_t> seed = Botan::hex_decode(options.drbg_seed());
1✔
65
   if(seed.empty()) {
1✔
66
      const uint64_t ts = Botan_Tests::Test::timestamp();
1✔
67
      seed.resize(8);
1✔
68
      Botan::store_be(ts, seed.data());
1✔
69
   }
70

71
   for(auto& reporter : m_reporters) {
3✔
72
#if defined(BOTAN_HAS_CPUID)
73
      const std::string cpuid = Botan::CPUID::to_string();
2✔
74
      if(!cpuid.empty()) {
2✔
75
         reporter->set_property("CPU flags", cpuid);
4✔
76
      }
77
#endif
78

79
      if(!options.pkcs11_lib().empty()) {
2✔
80
         reporter->set_property("pkcs11 library", options.pkcs11_lib());
4✔
81
      }
82

83
      if(!options.provider().empty()) {
2✔
84
         reporter->set_property("provider", options.provider());
×
85
      }
86

87
      reporter->set_property("drbg_seed", Botan::hex_encode(seed));
4✔
88
   }
2✔
89

90
   Botan_Tests::Test::set_test_options(options);
1✔
91

92
   for(size_t i = 0; i != options.test_runs(); ++i) {
2✔
93
      Botan_Tests::Test::set_test_rng_seed(seed, i);
1✔
94

95
      for(const auto& reporter : m_reporters) {
3✔
96
         reporter->next_test_run();
2✔
97
      }
98

99
      const bool passed =
1✔
100
         (options.test_threads() == 1) ? run_tests(req) : run_tests_multithreaded(req, options.test_threads());
1✔
101

102
      for(const auto& reporter : m_reporters) {
3✔
103
         reporter->render();
2✔
104
      }
105

106
      if(!passed) {
1✔
107
         return false;
108
      }
109
   }
110

111
   return true;
112
}
1✔
113

114
namespace {
115

116
std::vector<Test::Result> run_a_test(const std::string& test_name) {
418✔
117
   std::vector<Test::Result> results;
418✔
118

119
   try {
418✔
120
      if(std::unique_ptr<Test> test = Test::get_test(test_name)) {
418✔
121
         std::vector<Test::Result> test_results = test->run();
418✔
122
         for(auto& result : test_results) {
51,091✔
123
            if(!result.code_location() && test->registration_location()) {
50,673✔
124
               // If a test result has no specific code location associated to it,
125
               // we fall back to the test case's registration location.
126
               result.set_code_location(test->registration_location().value());
50,673✔
127
            }
128
         }
129
         results.insert(results.end(), test_results.begin(), test_results.end());
418✔
130
      } else {
418✔
131
         results.push_back(Test::Result::Note(test_name, "Test missing or unavailable"));
×
132
      }
418✔
133
   } catch(std::exception& e) {
×
134
      results.push_back(Test::Result::Failure(test_name, e.what()));
×
135
   } catch(...) {
×
136
      results.push_back(Test::Result::Failure(test_name, "unknown exception"));
×
137
   }
×
138

139
   return results;
418✔
140
}
×
141

142
bool all_passed(const std::vector<Test::Result>& results) {
418✔
143
   return std::all_of(results.begin(), results.end(), [](const auto& r) { return r.tests_failed() == 0; });
13,077✔
144
}
145

146
}  // namespace
147

148
bool Test_Runner::run_tests_multithreaded(const std::vector<std::string>& tests_to_run, size_t test_threads) {
1✔
149
   // If 0 then we let thread pool select the count
150
   BOTAN_ASSERT_NOMSG(test_threads != 1);
1✔
151

152
#if !defined(BOTAN_HAS_THREAD_UTILS)
153
   output() << "Running tests in multiple threads not enabled in this build\n";
154
   return run_tests(tests_to_run);
155

156
#else
157
   Botan::Thread_Pool pool(test_threads);
1✔
158
   Botan::RWLock rwlock;
1✔
159

160
   std::vector<std::future<std::vector<Test::Result>>> fut_results;
1✔
161

162
   auto run_test_exclusive = [&](const std::string& test_name) {
22✔
163
      const std::unique_lock lk(rwlock);
21✔
164
      return run_a_test(test_name);
21✔
165
   };
21✔
166

167
   auto run_test_shared = [&](const std::string& test_name) {
398✔
168
      const std::shared_lock lk(rwlock);
397✔
169
      return run_a_test(test_name);
397✔
170
   };
397✔
171

172
   for(const auto& test_name : tests_to_run) {
419✔
173
      if(Test::test_needs_serialization(test_name)) {
418✔
174
         fut_results.push_back(pool.run(run_test_exclusive, test_name));
42✔
175
      } else {
176
         fut_results.push_back(pool.run(run_test_shared, test_name));
794✔
177
      }
178
   }
179

180
   bool passed = true;
181
   for(size_t i = 0; i != fut_results.size(); ++i) {
419✔
182
      for(auto& reporter : m_reporters) {
1,254✔
183
         reporter->waiting_for_next_results(tests_to_run[i]);
836✔
184
      }
185
      const auto results = fut_results[i].get();
418✔
186
      for(auto& reporter : m_reporters) {
1,254✔
187
         reporter->record(tests_to_run[i], results);
836✔
188
      }
189
      passed &= all_passed(results);
418✔
190
   }
418✔
191

192
   pool.shutdown();
1✔
193

194
   return passed;
1✔
195
#endif
196
}
2✔
197

198
bool Test_Runner::run_tests(const std::vector<std::string>& tests_to_run) {
×
199
   bool passed = true;
×
200
   for(const auto& test_name : tests_to_run) {
×
201
      for(auto& reporter : m_reporters) {
×
202
         reporter->waiting_for_next_results(test_name);
×
203
      }
204
      const auto results = run_a_test(test_name);
×
205

206
      for(auto& reporter : m_reporters) {
×
207
         reporter->record(test_name, results);
×
208
      }
209
      passed &= all_passed(results);
×
210
   }
×
211

212
   return passed;
×
213
}
214

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

© 2026 Coveralls, Inc