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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
/*
* (C) 2014,2015 Jack Lloyd
* (C) 2015 Simon Warta (Kullo GmbH)
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#ifndef BOTAN_TESTS_H_
#define BOTAN_TESTS_H_
#include <botan/build.h>
#include <botan/rng.h>
#include <botan/hex.h>
#include <botan/symkey.h>
#include <botan/cpuid.h>
#include <iosfwd>
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
namespace Botan {
#if defined(BOTAN_HAS_BIGINT)
class BigInt;
#endif
#if defined(BOTAN_HAS_EC_CURVE_GFP)
class PointGFp;
#endif
}
namespace Botan_Tests {
#if defined(BOTAN_HAS_BIGINT)
using Botan::BigInt;
#endif
class Test_Error final : public Botan::Exception
{
public:
explicit Test_Error(const std::string& what) : Exception("Test error", what) {}
};
class Test_Options
{
public:
Test_Options() = default;
Test_Options(const std::vector<std::string>& requested_tests,
const std::string& data_dir,
const std::string& pkcs11_lib,
const std::string& provider,
const std::string& drbg_seed,
size_t test_runs,
bool verbose,
bool log_success,
bool run_online_tests,
bool run_long_tests,
bool abort_on_first_fail,
bool undefined_behavior_allowed) :
m_requested_tests(requested_tests),
m_data_dir(data_dir),
m_pkcs11_lib(pkcs11_lib),
m_provider(provider),
m_drbg_seed(drbg_seed),
m_test_runs(test_runs),
m_verbose(verbose),
m_log_success(log_success),
m_run_online_tests(run_online_tests),
m_run_long_tests(run_long_tests),
m_abort_on_first_fail(abort_on_first_fail),
m_undefined_behavior_allowed(undefined_behavior_allowed)
{}
const std::vector<std::string>& requested_tests() const
{ return m_requested_tests; }
const std::string& data_dir() const { return m_data_dir; }
const std::string& pkcs11_lib() const { return m_pkcs11_lib; }
const std::string& provider() const { return m_provider; }
const std::string& drbg_seed() const { return m_drbg_seed; }
size_t test_runs() const { return m_test_runs; }
bool log_success() const { return m_log_success; }
bool run_online_tests() const { return m_run_online_tests; }
bool run_long_tests() const { return m_run_long_tests; }
bool abort_on_first_fail() const { return m_abort_on_first_fail; }
bool verbose() const { return m_verbose; }
bool undefined_behavior_allowed() const
{
#if defined(BOTAN_HAS_SANITIZER_UNDEFINED)
return m_undefined_behavior_allowed;
#else
BOTAN_UNUSED(m_undefined_behavior_allowed);
return true;
#endif
}
private:
std::vector<std::string> m_requested_tests;
std::string m_data_dir;
std::string m_pkcs11_lib;
std::string m_provider;
std::string m_drbg_seed;
size_t m_test_runs;
bool m_verbose;
bool m_log_success;
bool m_run_online_tests;
bool m_run_long_tests;
bool m_abort_on_first_fail;
bool m_undefined_behavior_allowed;
};
/*
* A generic test which returns a set of results when run.
* The tests may not all have the same type (for example test
* "block" returns results for "AES-128" and "AES-256").
*
* For most test cases you want Text_Based_Test derived below
*/
class Test
{
public:
/*
* Some number of test results, all associated with who()
*/
class Result final
{
public:
explicit Result(const std::string& who) : m_who(who) {}
size_t tests_passed() const
{
return m_tests_passed;
}
size_t tests_failed() const
{
return m_fail_log.size();
}
size_t tests_run() const
{
return tests_passed() + tests_failed();
}
bool any_results() const
{
return tests_run() > 0;
}
const std::string& who() const
{
return m_who;
}
std::string result_string() const;
static Result Failure(const std::string& who,
const std::string& what)
{
Result r(who);
r.test_failure(what);
return r;
}
static Result Note(const std::string& who,
const std::string& what)
{
Result r(who);
r.test_note(what);
return r;
}
static Result OfExpectedFailure(bool expecting_failure,
const Test::Result& result)
{
if(!expecting_failure)
{
return result;
}
if(result.tests_failed() == 0)
{
Result r = result;
r.test_failure("Expected this test to fail, but it did not");
return r;
}
else
{
Result r(result.who());
r.test_note("Got expected failure");
return r;
}
}
void merge(const Result& other);
void test_note(const std::string& note, const char* extra = nullptr);
template<typename Alloc>
void test_note(const std::string& who, const std::vector<uint8_t, Alloc>& vec)
{
const std::string hex = Botan::hex_encode(vec);
return test_note(who, hex.c_str());
}
void note_missing(const std::string& thing);
bool test_success(const std::string& note = "");
bool test_failure(const std::string& err);
bool test_failure(const std::string& what, const std::string& error);
void test_failure(const std::string& what, const uint8_t buf[], size_t buf_len);
template<typename Alloc>
void test_failure(const std::string& what, const std::vector<uint8_t, Alloc>& buf)
{
test_failure(what, buf.data(), buf.size());
}
bool confirm(const std::string& what, bool expr)
{
return test_eq(what, expr, true);
}
template<typename T>
bool test_is_eq(const T& produced, const T& expected)
{
return test_is_eq("comparison", produced, expected);
}
template<typename T>
bool test_is_eq(const std::string& what, const T& produced, const T& expected)
{
std::ostringstream out;
out << m_who << " " << what;
if(produced == expected)
{
out << " produced expected result " << produced;
return test_success(out.str());
}
else
{
out << " produced unexpected result '" << produced << "' expected '" << expected << "'";
return test_failure(out.str());
}
}
template<typename T>
bool test_not_null(const std::string& what, T* ptr)
{
if(ptr == nullptr)
return test_failure(what + " was null");
else
return test_success(what + " was not null");
}
bool test_eq(const std::string& what, const char* produced, const char* expected);
bool test_is_nonempty(const std::string& what_is_it, const std::string& to_examine);
bool test_eq(const std::string& what,
const std::string& produced,
const std::string& expected);
bool test_eq(const std::string& what, bool produced, bool expected);
bool test_eq(const std::string& what, size_t produced, size_t expected);
bool test_eq_sz(const std::string& what, size_t produced, size_t expected);
bool test_eq(const std::string& what,
Botan::OctetString produced,
Botan::OctetString expected);
template<typename I1, typename I2>
bool test_int_eq(I1 x, I2 y, const char* what)
{
return test_eq(what, static_cast<size_t>(x), static_cast<size_t>(y));
}
template<typename I1, typename I2>
bool test_int_eq(const std::string& what, I1 x, I2 y)
{
return test_eq(what.c_str(), static_cast<size_t>(x), static_cast<size_t>(y));
}
bool test_lt(const std::string& what, size_t produced, size_t expected);
bool test_lte(const std::string& what, size_t produced, size_t expected);
bool test_gte(const std::string& what, size_t produced, size_t expected);
template<typename T>
bool test_rc_ok(const std::string& func, T rc)
{
static_assert(std::is_integral<T>::value, "Integer required.");
if(rc != 0)
{
std::ostringstream err;
err << m_who;
err << " " << func;
err << " unexpectedly failed with error code " << rc;
return test_failure(err.str());
}
return test_success();
}
template<typename T>
bool test_rc_fail(const std::string& func, const std::string& why, T rc)
{
static_assert(std::is_integral<T>::value, "Integer required.");
if(rc == 0)
{
std::ostringstream err;
err << m_who;
err << " call to " << func << " unexpectedly succeeded";
err << " expecting failure because " << why;
return test_failure(err.str());
}
return test_success();
}
bool test_rc(const std::string& func, int expected, int rc);
bool test_ne(const std::string& what, size_t produced, size_t expected);
bool test_ne(const std::string& what, const std::string& str1, const std::string& str2);
#if defined(BOTAN_HAS_BIGINT)
bool test_eq(const std::string& what, const BigInt& produced, const BigInt& expected);
bool test_ne(const std::string& what, const BigInt& produced, const BigInt& expected);
#endif
#if defined(BOTAN_HAS_EC_CURVE_GFP)
bool test_eq(const std::string& what,
const Botan::PointGFp& a,
const Botan::PointGFp& b);
#endif
bool test_eq(const char* producer, const std::string& what,
const uint8_t produced[], size_t produced_len,
const uint8_t expected[], size_t expected_len);
bool test_ne(const std::string& what,
const uint8_t produced[], size_t produced_len,
const uint8_t expected[], size_t expected_len);
template<typename Alloc1, typename Alloc2>
bool test_eq(const std::string& what,
const std::vector<uint8_t, Alloc1>& produced,
const std::vector<uint8_t, Alloc2>& expected)
{
return test_eq(nullptr, what,
produced.data(), produced.size(),
expected.data(), expected.size());
}
template<typename Alloc1, typename Alloc2>
bool test_eq(const std::string& producer, const std::string& what,
const std::vector<uint8_t, Alloc1>& produced,
const std::vector<uint8_t, Alloc2>& expected)
{
return test_eq(producer.c_str(), what,
produced.data(), produced.size(),
expected.data(), expected.size());
}
template<typename Alloc>
bool test_eq(const std::string& what,
const std::vector<uint8_t, Alloc>& produced,
const char* expected_hex)
{
const std::vector<uint8_t> expected = Botan::hex_decode(expected_hex);
return test_eq(nullptr, what,
produced.data(), produced.size(),
expected.data(), expected.size());
}
template<typename Alloc1, typename Alloc2>
bool test_ne(const std::string& what,
const std::vector<uint8_t, Alloc1>& produced,
const std::vector<uint8_t, Alloc2>& expected)
{
return test_ne(what,
produced.data(), produced.size(),
expected.data(), expected.size());
}
bool test_throws(const std::string& what, std::function<void ()> fn);
bool test_throws(const std::string& what, const std::string& expected,
std::function<void ()> fn);
void set_ns_consumed(uint64_t ns)
{
m_ns_taken = ns;
}
void start_timer();
void end_timer();
private:
std::string m_who;
uint64_t m_started = 0;
uint64_t m_ns_taken = 0;
size_t m_tests_passed = 0;
std::vector<std::string> m_fail_log;
std::vector<std::string> m_log;
};
class Registration final
{
public:
Registration(const std::string& name, Test* test);
};
virtual ~Test() = default;
virtual std::vector<Test::Result> run() = 0;
virtual std::vector<std::string> possible_providers(const std::string&);
static std::map<std::string, std::unique_ptr<Test>>& global_registry();
static std::set<std::string> registered_tests();
static Test* get_test(const std::string& test_name);
static std::string data_file(const std::string& what);
static std::string format_time(uint64_t nanoseconds);
template<typename Alloc>
static std::vector<uint8_t, Alloc>
mutate_vec(const std::vector<uint8_t, Alloc>& v,
bool maybe_resize = false,
size_t min_offset = 0)
{
auto& rng = Test::rng();
std::vector<uint8_t, Alloc> r = v;
if(maybe_resize && (r.empty() || rng.next_byte() < 32))
{
// TODO: occasionally truncate, insert at random index
const size_t add = 1 + (rng.next_byte() % 16);
r.resize(r.size() + add);
rng.randomize(&r[r.size() - add], add);
}
if(r.size() > min_offset)
{
const size_t offset = std::max<size_t>(min_offset, rng.next_byte() % r.size());
const uint8_t perturb = rng.next_nonzero_byte();
r[offset] ^= perturb;
}
return r;
}
static void set_test_options(const Test_Options& opts);
static void set_test_rng(std::unique_ptr<Botan::RandomNumberGenerator> rng);
static const Test_Options& options() { return m_opts; }
static bool run_long_tests() { return options().run_long_tests(); }
static const std::string& data_dir() { return options().data_dir(); }
static const std::string& pkcs11_lib() { return options().pkcs11_lib(); }
static std::string temp_file_name(const std::string& basename);
static std::vector<std::string> provider_filter(const std::vector<std::string>& providers);
static std::string read_data_file(const std::string& path);
static std::vector<uint8_t> read_binary_data_file(const std::string& path);
static Botan::RandomNumberGenerator& rng();
static std::string random_password();
static uint64_t timestamp(); // nanoseconds arbitrary epoch
private:
static Test_Options m_opts;
static std::unique_ptr<Botan::RandomNumberGenerator> m_test_rng;
};
/*
* Register the test with the runner
*/
#define BOTAN_REGISTER_TEST(type, Test_Class) \
Test::Registration reg_ ## Test_Class ## _tests(type, new Test_Class)
class VarMap
{
public:
void clear() { m_vars.clear(); }
void add(const std::string& key, const std::string& value)
{
m_vars[key] = value;
}
bool has_key(const std::string& key) const
{
return m_vars.count(key) == 1;
}
bool get_req_bool(const std::string& key) const;
std::vector<uint8_t> get_req_bin(const std::string& key) const;
std::vector<uint8_t> get_opt_bin(const std::string& key) const;
#if defined(BOTAN_HAS_BIGINT)
Botan::BigInt get_req_bn(const std::string& key) const;
Botan::BigInt get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const;
#endif
std::string get_req_str(const std::string& key) const;
std::string get_opt_str(const std::string& key,
const std::string& def_value) const;
size_t get_req_sz(const std::string& key) const;
size_t get_opt_sz(const std::string& key, const size_t def_value) const;
private:
std::unordered_map<std::string, std::string> m_vars;
};
/*
* A test based on reading an input file which contains key/value pairs
* Special note: the last value in required_key (there must be at least
* one), is the output key. This triggers the callback.
*
* Calls run_one_test with the variables set. If an ini-style [header]
* is used in the file, then header will be set to that value. This allows
* splitting up tests between [valid] and [invalid] tests, or different
* related algorithms tested in the same file. Use the get_XXX functions
* on VarMap to retrieve formatted values.
*
* If most of your tests are text-based but you find yourself with a few
* odds-and-ends tests that you want to do, override run_final_tests which
* can test whatever it likes and returns a vector of Results.
*/
class Text_Based_Test : public Test
{
public:
Text_Based_Test(const std::string& input_file,
const std::string& required_keys,
const std::string& optional_keys = "");
virtual bool clear_between_callbacks() const
{
return true;
}
std::vector<Test::Result> run() override;
protected:
std::string get_next_line();
virtual Test::Result run_one_test(const std::string& header,
const VarMap& vars) = 0;
// Called before run_one_test
virtual bool skip_this_test(const std::string& header,
const VarMap& vars);
virtual std::vector<Test::Result> run_final_tests()
{
return std::vector<Test::Result>();
}
private:
std::string m_data_src;
std::set<std::string> m_required_keys;
std::set<std::string> m_optional_keys;
std::string m_output_key;
bool m_first = true;
std::unique_ptr<std::istream> m_cur;
std::string m_cur_src_name;
std::deque<std::string> m_srcs;
std::vector<Botan::CPUID::CPUID_bits> m_cpu_flags;
};
}
#endif
|