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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/*
* (C) 2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "../cli/cli.h"
#include "tests.h"
#include <iostream>
#include <sstream>
#include <string>
#include <set>
#include <deque>
#include <thread>
#include <future>
#include <cstdlib>
#include <botan/version.h>
#include <botan/loadstor.h>
#include <botan/hash.h>
#if defined(BOTAN_HAS_HMAC_DRBG)
#include <botan/hmac_drbg.h>
#endif
#if defined(BOTAN_HAS_SYSTEM_RNG)
#include <botan/system_rng.h>
#endif
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
#include <botan/auto_rng.h>
#endif
#if defined(BOTAN_HAS_OPENSSL)
#include <botan/internal/openssl.h>
#endif
namespace {
class Test_Runner : public Botan_CLI::Command
{
public:
Test_Runner()
: Command("test --threads=0 --run-long-tests --run-online-tests --test-runs=1 --drbg-seed= --data-dir="
" --pkcs11-lib= --provider= --log-success *suites") {}
std::string help_text() const override
{
std::ostringstream err;
const std::string& spec = cmd_spec();
err << "Usage: botan-test"
<< spec.substr(spec.find_first_of(' '), std::string::npos)
<< "\n\nAvailable test suites\n"
<< "----------------\n";
size_t line_len = 0;
for(auto const& test : Botan_Tests::Test::registered_tests())
{
err << test << " ";
line_len += test.size() + 1;
if(line_len > 64)
{
err << "\n";
line_len = 0;
}
}
if(line_len > 0)
{
err << "\n";
}
return err.str();
}
void go() override
{
const size_t threads = get_arg_sz("threads");
const std::string drbg_seed = get_arg("drbg-seed");
const bool log_success = flag_set("log-success");
const bool run_online_tests = flag_set("run-online-tests");
const bool run_long_tests = flag_set("run-long-tests");
const std::string data_dir = get_arg_or("data-dir", "src/tests/data");
const std::string pkcs11_lib = get_arg("pkcs11-lib");
const std::string provider = get_arg("provider");
const size_t runs = get_arg_sz("test-runs");
std::vector<std::string> req = get_arg_list("suites");
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", "x931_rng", "util"
};
std::set<std::string> all_others = Botan_Tests::Test::registered_tests();
if(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_CLI::CLI_Usage_Error("Unknown test suite: " + r);
}
}
}
output() << "Testing " << Botan::version_string() << "\n";
output() << "Starting tests";
if(threads > 1)
{
output() << " threads:" << threads;
}
if(!pkcs11_lib.empty())
{
output() << " pkcs11 library:" << pkcs11_lib;
}
Botan_Tests::Provider_Filter pf;
if(!provider.empty())
{
output() << " provider:" << provider;
pf.set(provider);
}
#if defined(BOTAN_HAS_OPENSSL)
if(provider.empty() || provider == "openssl")
{
ERR_load_crypto_strings();
}
#endif
std::unique_ptr<Botan::RandomNumberGenerator> rng;
#if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_64)
std::vector<uint8_t> seed = Botan::hex_decode(drbg_seed);
if(seed.empty())
{
const uint64_t ts = Botan_Tests::Test::timestamp();
seed.resize(8);
Botan::store_be(ts, seed.data());
}
output() << " rng:HMAC_DRBG with seed '" << Botan::hex_encode(seed) << "'";
// Expand out the seed to 512 bits to make the DRBG happy
std::unique_ptr<Botan::HashFunction> sha512(Botan::HashFunction::create("SHA-512"));
sha512->update(seed);
seed.resize(sha512->output_length());
sha512->final(seed.data());
std::unique_ptr<Botan::HMAC_DRBG> drbg(new Botan::HMAC_DRBG("SHA-384"));
drbg->initialize_with(seed.data(), seed.size());
rng.reset(new Botan::Serialized_RNG(drbg.release()));
#else
if(drbg_seed != "")
{
throw Botan_Tests::Test_Error("HMAC_DRBG disabled in build, cannot specify DRBG seed");
}
#if defined(BOTAN_HAS_SYSTEM_RNG)
output() << " rng:system";
rng.reset(new Botan::System_RNG);
#elif defined(BOTAN_HAS_AUTO_SEEDING_RNG)
output() << " rng:autoseeded";
rng.reset(new Botan::Serialized_RNG(new Botan::AutoSeeded_RNG));
#else
// last ditch fallback for RNG-less build
class Bogus_Fallback_RNG : public Botan::RandomNumberGenerator
{
public:
std::string name() const override
{
return "Bogus_Fallback_RNG";
}
void clear() override
{
/* ignored */
}
void randomize(uint8_t out[], size_t len) override
{
for(size_t i = 0; i != len; ++i)
{
out[i] = std::rand();
}
}
bool is_seeded() const override
{
return true;
}
void add_entropy(const uint8_t[], size_t) override
{
/* ignored */
}
};
rng.reset(new Bogus_Fallback_RNG);
#endif
#endif
output() << "\n";
Botan_Tests::Test::setup_tests(log_success, run_online_tests, run_long_tests,
data_dir, pkcs11_lib, pf, rng.get());
for(size_t i = 0; i != runs; ++i)
{
const size_t failed = run_tests(req, output(), threads);
// Throw so main returns an error
if(failed)
{
throw Botan_Tests::Test_Error("Test suite failure");
}
}
}
private:
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(verbose());
tests_failed += result.second.tests_failed();
tests_ran += result.second.tests_run();
}
return out.str();
}
size_t run_tests(const std::vector<std::string>& tests_to_run,
std::ostream& out,
size_t threads)
{
size_t tests_ran = 0, tests_failed = 0;
const uint64_t start_time = Botan_Tests::Test::timestamp();
if(threads <= 1)
{
for(auto const& test_name : tests_to_run)
{
try
{
out << test_name << ':' << std::endl;
const auto results = Botan_Tests::Test::run_test(test_name, false);
out << report_out(results, tests_failed, tests_ran) << std::flush;
}
catch(std::exception& e)
{
out << "Test " << test_name << " failed with exception " << e.what() << std::flush;
}
}
}
else
{
/*
We're not doing this in a particularly nice way, and variance in time is
high so commonly we'll 'run dry' by blocking on the first future. But
plain C++11 <thread> is missing a lot of tools we'd need (like
wait_for_any on a set of futures) and there is no point pulling in an
additional dependency just for this. In any case it helps somewhat
(50-100% speedup) and provides a proof of concept for parallel testing.
*/
typedef std::future<std::vector<Botan_Tests::Test::Result>> FutureResults;
std::deque<FutureResults> fut_results;
for(auto const& test_name : tests_to_run)
{
auto run_it = [test_name]() -> std::vector<Botan_Tests::Test::Result>
{
try
{
return Botan_Tests::Test::run_test(test_name, false);
}
catch(std::exception& e)
{
Botan_Tests::Test::Result r(test_name);
r.test_failure("Exception thrown", e.what());
return std::vector<Botan_Tests::Test::Result> {r};
}
};
fut_results.push_back(std::async(std::launch::async, run_it));
while(fut_results.size() > threads)
{
out << report_out(fut_results[0].get(), tests_failed, tests_ran) << std::flush;
fut_results.pop_front();
}
}
while(fut_results.size() > 0)
{
out << report_out(fut_results[0].get(), tests_failed, tests_ran) << std::flush;
fut_results.pop_front();
}
}
const uint64_t total_ns = Botan_Tests::Test::timestamp() - start_time;
out << "Tests complete ran " << tests_ran << " tests in "
<< Botan_Tests::Test::format_time(total_ns) << " ";
if(tests_failed > 0)
{
out << tests_failed << " tests failed";
}
else if(tests_ran > 0)
{
out << "all tests ok";
}
out << std::endl;
return tests_failed;
}
};
BOTAN_REGISTER_COMMAND("test", Test_Runner);
}
int main(int argc, char* argv[])
{
std::cerr << Botan::runtime_version_check(BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH);
try
{
std::unique_ptr<Botan_CLI::Command> cmd(Botan_CLI::Command::get_cmd("test"));
if(!cmd)
{
std::cout << "Unable to retrieve testing helper (program bug)\n"; // WTF
return 1;
}
std::vector<std::string> args(argv + 1, argv + argc);
return cmd->run(args);
}
catch(Botan::Exception& e)
{
std::cout << "Exiting with library exception " << e.what() << std::endl;
}
catch(std::exception& e)
{
std::cout << "Exiting with std exception " << e.what() << std::endl;
}
catch(...)
{
std::cout << "Exiting with unknown exception\n";
}
}
|