summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/doc/dev_ref/test_framework.rst
blob: 241c51bd1af3a47d4faaa56912d417c1ddd37696 (plain)
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
Test Framework
================

Botan uses a custom-built test framework. Some portions of it are
quite similar to assertion-based test frameworks such as Catch or
Gtest, but it also includes many features which are well suited for
testing cryptographic algorithms.

The intent is that the test framework and the test suite evolve
symbiotically; as a general rule of thumb if a new function would make
the implementation of just two distinct tests simpler, it is worth
adding to the framework on the assumption it will prove useful again.
Feel free to propose changes to the test system.

When writing a new test, there are three key classes that are used,
namely ``Test``, ``Test::Result``, and ``Text_Based_Test``. A ``Test``
(or ``Test_Based_Test``) runs and returns one or more ``Test::Result``.

Namespaces in Test
-------------------

The test code lives in a distinct namespace (``Botan_Tests``) and all
code in the tests which calls into the library should use the
namespace prefix ``Botan::`` rather than a ``using namespace``
declaration. This makes it easier to see where the test is actually
invoking the library, and makes it easier to reuse test code for
applications.

Test Data
-----------

The test framework is heavily data driven. As of this writing, there
is about 1 Mib of test code and 17 MiB of test data. For most (though
certainly not all) tests, it is better to add a data file representing
the input and outputs, and run the tests over it. Data driven tests
make adding or editing tests easier, for example by writing scripts
which produce new test data and output it in the expected format.

Test
--------

.. cpp:class:: Test

  .. cpp:function:: virtual std::vector<Test::Result> run() = 0

     This is the key function of a ``Test``: it executes and returns a
     list of results. Almost all other functions on ``Test`` are
     static functions which just serve as helper functions for ``run``.

  .. cpp:function:: static std::string read_data_file(const std::string& path)

     Return the contents of a data file and return it as a string.

  .. cpp:function:: static std::vector<uint8_t> read_binary_data_file(const std::string& path)

     Return the contents of a data file and return it as a vector of
     bytes.

  .. cpp:function:: static std::string data_file(const std::string& what)

     An alternative to ``read_data_file`` and ``read_binary_file``,
     use only as a last result, typically for library APIs which
     themselves accept a filename rather than a data blob.

  .. cpp:function:: static bool run_long_tests() const

     Returns true if the user gave option ``--run-long-tests``. Use
     this to gate particularly time-intensive tests.

  .. cpp:function:: static Botan::RandomNumberGenerator& rng()

     Returns a reference to a fast, not cryptographically secure
     random number generator. It is deterministicly seeded with the
     seed logged by the test runner, so it is possible to reproduce
     results in "random" tests.

Tests are registered using the macro ``BOTAN_REGISTER_TEST`` which
takes 2 arguments: the name of the test and the name of the test class.
For example given a ``Test`` instance named ``MyTest``, use::

  BOTAN_REGISTER_TEST("mytest", MyTest);

All test names should contain only lowercase letters, numbers, and
underscore.

Test::Result
-------------

.. cpp:class:: Test::Result

    A ``Test::Result`` records one or more tests on a particular topic
    (say "AES-128/CBC" or "ASN.1 date parsing"). Most of the test functions
    return true or false if the test was successful or not; this allows
    performing conditional blocks as a result of earlier tests::

      if(result.test_eq("first value", produced, expected))
         {
         // further tests that rely on the initial test being correct
         }

    Only the most commonly used functions on ``Test::Result`` are documented here,
    see the header ``tests.h`` for more.

    .. cpp:function:: Test::Result(const std::string& who)

       Create a test report on a particular topic. This will be displayed in the
       test results.

    .. cpp:function:: bool test_success()

       Report a test that was successful.

    .. cpp:function:: bool test_success(const std::string& note)

       Report a test that was successful, including some comment.

    .. cpp:function:: bool test_failure(const std::string& err)

       Report a test failure of some kind. The error string will be logged.

    .. cpp:function:: bool test_failure(const std::string& what, const std::string& error)

       Report a test failure of some kind, with a description of what failed and
       what the error was.

    .. cpp:function:: void test_failure(const std::string& what, const uint8_t buf[], size_t buf_len)

       Report a test failure due to some particular input, which is provided as
       arguments. Normally this is only used if the test was using some
       randomized input which unexpectedly failed, since if the input is
       hardcoded or from a file it is easier to just reference the test number.

    .. cpp:function:: bool test_eq(const std::string& what, const std::string& produced, const std::string& expected)

       Compare to strings for equality.

    .. cpp:function:: bool test_ne(const std::string& what, const std::string& produced, const std::string& expected)

       Compare to strings for non-equality.

    .. cpp:function:: 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)

       Compare two arrays for equality.

    .. cpp:function:: bool test_ne(const char* producer, const std::string& what, \
                                   const uint8_t produced[], size_t produced_len, \
                                   const uint8_t expected[], size_t expected_len)

       Compare two arrays for non-equality.

    .. cpp:function:: bool test_eq(const std::string& producer, const std::string& what, \
                                   const std::vector<uint8_t>& produced, \
                                   const std::vector<uint8_t>& expected)

       Compare two vectors for equality.

    .. cpp:function:: bool test_ne(const std::string& producer, const std::string& what, \
                                   const std::vector<uint8_t>& produced, \
                                   const std::vector<uint8_t>& expected)

       Compare two vectors for non-equality.

    .. cpp:function:: bool confirm(const std::string& what, bool expr)

       Test that some expression evaluates to ``true``.

    .. cpp:function:: template<typename T> bool test_not_null(const std::string& what, T* ptr)

       Verify that the pointer is not null.

    .. cpp:function:: bool test_lt(const std::string& what, size_t produced, size_t expected)

       Test that ``produced`` < ``expected``.

    .. cpp:function:: bool test_lte(const std::string& what, size_t produced, size_t expected)

       Test that ``produced`` <= ``expected``.

    .. cpp:function:: bool test_gt(const std::string& what, size_t produced, size_t expected)

       Test that ``produced`` > ``expected``.

    .. cpp:function:: bool test_gte(const std::string& what, size_t produced, size_t expected)

       Test that ``produced`` >= ``expected``.

    .. cpp:function:: bool test_throws(const std::string& what, std::function<void ()> fn)

       Call a function and verify it throws an exception of some kind.

    .. cpp:function:: bool test_throws(const std::string& what, const std::string& expected, std::function<void ()> fn)

       Call a function and verify it throws an exception of some kind
       and that the exception message exactly equals ``expected``.

Text_Based_Test
-----------------

A ``Text_Based_Text`` runs tests that are produced from a text file
with a particular format which looks somewhat like an INI-file::

  # Comments begin with # and continue to end of line
  [Header]
  # Test 1
  Key1 = Value1
  Key2 = Value2

  # Test 2
  Key1 = Value1
  Key2 = Value2

.. cpp:class:: VarMap

  An object of this type is passed to each invocation of the text-based test.
  It is used to access the test variables. All access takes a key, which is
  one of the strings which was passed to the constructor of ``Text_Based_Text``.
  Accesses are either required (``get_req_foo``), in which case an exception is
  throwing if the key is not set, or optional (``get_opt_foo``) in which case
  the test provides a default value which is returned if the key was not set
  for this particular instance of the test.

  .. cpp:function:: std::vector<uint8_t> get_req_bin(const std::string& key) const

     Return a required binary string. The input is assumed to be hex encoded.

  .. cpp:function:: std::vector<uint8_t> get_opt_bin(const std::string& key) const

     Return an optional binary string. The input is assumed to be hex encoded.

  .. cpp:function:: std::vector<std::vector<uint8_t>> get_req_bin_list(const std::string& key) const

  .. cpp:function:: Botan::BigInt get_req_bn(const std::string& key) const

     Return a required BigInt. The input can be decimal or (with "0x" prefix) hex encoded.

  .. cpp:function:: Botan::BigInt get_opt_bn(const std::string& key, const Botan::BigInt& def_value) const

     Return an optional BigInt. The input can be decimal or (with "0x" prefix) hex encoded.

  .. cpp:function:: std::string get_req_str(const std::string& key) const

     Return a required text string.

  .. cpp:function:: std::string get_opt_str(const std::string& key, const std::string& def_value) const

     Return an optional text string.

  .. cpp:function:: size_t get_req_sz(const std::string& key) const

     Return a required integer. The input should be decimal.

  .. cpp:function:: size_t get_opt_sz(const std::string& key, const size_t def_value) const

     Return an optional integer. The input should be decimal.

.. cpp:class:: Text_Based_Test : public Test

  .. cpp:function:: Text_Based_Test(const std::string& input_file, \
                    const std::string& required_keys, \
                    const std::string& optional_keys = "")

     This constructor is

     .. note::
        The final element of required_keys is the "output key", that is
        the key which signifies the boundary between one test and the next.
        When this key is seen, ``run_one_test`` will be invoked. In the
        test input file, this key must always appear least for any particular
        test. All the other keys may appear in any order.

  .. cpp:function:: Test::Result run_one_test(const std::string& header, \
                    const VarMap& vars)

     Runs a single test and returns the result of it. The ``header``
     parameter gives the value (if any) set in a ``[Header]`` block.
     This can be useful to distinguish several types of tests within a
     single file, for example "[Valid]" and "[Invalid]".

  .. cpp:function:: bool clear_between_callbacks() const

     By default this function returns ``false``. If it returns
     ``true``, then when processing the data in the file, variables
     are not cleared between tests. This can be useful when several
     tests all use some common parameters.

Test Runner
-------------

If you are simply writing a new test there should be no need to modify
the runner, however it can be useful to be aware of its abilities.

The runner can run tests concurrently across many cores. By default single
threaded execution is used, but you can use ``--test-threads`` option to
specify the number of threads to use. If you use ``--test-threads=0`` then
the runner will probe the number of active CPUs and use that (but limited
to at most 16). If you want to run across many cores on a large machine,
explicitly specify a thread count. The speedup is close to linear.

The RNG used in the tests is deterministic, and the seed is logged for each
execution. You can cause the random sequence to repeat using ``--drbg-seed``
option.

.. note::
   Currently the RNG is seeded just once at the start of execution. So you
   must run the exact same sequence of tests as the original test run in
   order to get reproducible results.

If you are trying to track down a bug that happens only occasionally, two very
useful options are ``--test-runs`` and ``--abort-on-first-fail``. The first
takes an integer and runs the specified test cases that many times. The second
causes abort to be called on the very first failed test. This is sometimes
useful when tracing a memory corruption bug.