1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* (C) 2017 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "test_runner.h"
#include "tests.h"
#include <botan/version.h>
#include <botan/rotate.h>
#include <botan/loadstor.h>
#include <botan/cpuid.h>
namespace Botan_Tests {
Test_Runner::Test_Runner(std::ostream& out) : m_output(out) {}
namespace {
/*
* This is a fast, simple, deterministic PRNG that's used for running
* the tests. It is not intended to be cryptographically secure.
*/
class Testsuite_RNG final : public Botan::RandomNumberGenerator
{
public:
std::string name() const override { return "Testsuite_RNG"; }
void clear() override
{
m_a = m_b = m_c = m_d = 0;
}
bool accepts_input() const override { return true; }
void add_entropy(const uint8_t data[], size_t len) override
{
for(size_t i = 0; i != len; ++i)
{
m_a ^= data[i];
m_b ^= i;
mix();
}
}
bool is_seeded() const override
{
return true;
}
void randomize(uint8_t out[], size_t len) override
{
for(size_t i = 0; i != len; ++i)
{
out[i] = static_cast<uint8_t>(m_a);
mix();
}
}
Testsuite_RNG(const std::vector<uint8_t>& seed, size_t test_counter = 0)
{
m_d = static_cast<uint32_t>(test_counter);
add_entropy(seed.data(), seed.size());
}
private:
void mix()
{
const size_t ROUNDS = 3;
for(size_t i = 0; i != ROUNDS; ++i)
{
m_a += static_cast<uint32_t>(i);
m_a = Botan::rotl<9>(m_a);
m_b ^= m_a;
m_d ^= m_c;
m_a += m_d;
m_c += m_b;
m_c = Botan::rotl<23>(m_c);
}
}
uint32_t m_a = 0, m_b = 0, m_c = 0, m_d = 0;
};
}
int Test_Runner::run(const Test_Options& opts)
{
std::vector<std::string> req = opts.requested_tests();
if(req.empty())
{
/*
If nothing was requested on the command line, run everything. First
run the "essentials" to smoke test, then everything else in
alphabetical order.
*/
req = {"block", "stream", "hash", "mac", "modes", "aead",
"kdf", "pbkdf", "hmac_drbg", "util"
};
std::set<std::string> all_others = Botan_Tests::Test::registered_tests();
if(opts.pkcs11_lib().empty())
{
// do not run pkcs11 tests by default unless pkcs11-lib set
for(std::set<std::string>::iterator iter = all_others.begin(); iter != all_others.end();)
{
if((*iter).find("pkcs11") != std::string::npos)
{
iter = all_others.erase(iter);
}
else
{
++iter;
}
}
}
for(auto f : req)
{
all_others.erase(f);
}
req.insert(req.end(), all_others.begin(), all_others.end());
}
else if(req.size() == 1 && req.at(0) == "pkcs11")
{
req = {"pkcs11-manage", "pkcs11-module", "pkcs11-slot", "pkcs11-session", "pkcs11-object", "pkcs11-rsa",
"pkcs11-ecdsa", "pkcs11-ecdh", "pkcs11-rng", "pkcs11-x509"
};
}
else
{
std::set<std::string> all = Botan_Tests::Test::registered_tests();
for(auto const& r : req)
{
if(all.find(r) == all.end())
{
throw Botan_Tests::Test_Error("Unknown test suite: " + r);
}
}
}
output() << "Testing " << Botan::version_string() << "\n";
const std::string cpuid = Botan::CPUID::to_string();
if(cpuid.size() > 0)
output() << "CPU flags: " << cpuid << "\n";
output() << "Starting tests";
if(!opts.pkcs11_lib().empty())
{
output() << " pkcs11 library:" << opts.pkcs11_lib();
}
if(!opts.provider().empty())
{
output() << " provider:" << opts.provider();
}
std::vector<uint8_t> seed = Botan::hex_decode(opts.drbg_seed());
if(seed.empty())
{
const uint64_t ts = Botan_Tests::Test::timestamp();
seed.resize(8);
Botan::store_be(ts, seed.data());
}
output() << " drbg_seed:" << Botan::hex_encode(seed) << "\n";
Botan_Tests::Test::set_test_options(opts);
for(size_t i = 0; i != opts.test_runs(); ++i)
{
std::unique_ptr<Botan::RandomNumberGenerator> rng =
std::unique_ptr<Botan::RandomNumberGenerator>(new Testsuite_RNG(seed, i));
Botan_Tests::Test::set_test_rng(std::move(rng));
const size_t failed = run_tests(req, i, opts.test_runs());
if(failed > 0)
return static_cast<int>(failed);
}
return 0;
}
namespace {
std::string report_out(const std::vector<Botan_Tests::Test::Result>& results,
size_t& tests_failed,
size_t& tests_ran)
{
std::ostringstream out;
std::map<std::string, Botan_Tests::Test::Result> combined;
for(auto const& result : results)
{
const std::string who = result.who();
auto i = combined.find(who);
if(i == combined.end())
{
combined.insert(std::make_pair(who, Botan_Tests::Test::Result(who)));
i = combined.find(who);
}
i->second.merge(result);
}
for(auto const& result : combined)
{
out << result.second.result_string();
tests_failed += result.second.tests_failed();
tests_ran += result.second.tests_run();
}
return out.str();
}
}
size_t Test_Runner::run_tests(const std::vector<std::string>& tests_to_run,
size_t test_run,
size_t tot_test_runs)
{
size_t tests_ran = 0, tests_failed = 0;
const uint64_t start_time = Botan_Tests::Test::timestamp();
for(auto const& test_name : tests_to_run)
{
output() << test_name << ':' << std::endl;
std::vector<Test::Result> results;
try
{
if(test_name == "simd_32" && Botan::CPUID::has_simd_32() == false)
{
results.push_back(Test::Result::Note(test_name, "SIMD not available on this platform"));
}
else if(Test* test = Test::get_test(test_name))
{
std::vector<Test::Result> test_results = test->run();
results.insert(results.end(), test_results.begin(), test_results.end());
}
else
{
results.push_back(Test::Result::Note(test_name, "Test missing or unavailable"));
}
}
catch(std::exception& e)
{
results.push_back(Test::Result::Failure(test_name, e.what()));
}
catch(...)
{
results.push_back(Test::Result::Failure(test_name, "unknown exception"));
}
output() << report_out(results, tests_failed, tests_ran) << std::flush;
}
const uint64_t total_ns = Botan_Tests::Test::timestamp() - start_time;
if(test_run == 0 && tot_test_runs == 1)
output() << "Tests";
else
output() << "Test run " << (1+test_run) << "/" << tot_test_runs;
output() << " complete ran " << tests_ran << " tests in "
<< Botan_Tests::Test::format_time(total_ns) << " ";
if(tests_failed > 0)
{
output() << tests_failed << " tests failed";
}
else if(tests_ran > 0)
{
output() << "all tests ok";
}
output() << std::endl;
return tests_failed;
}
}
|