xref: /freebsd/crypto/openssl/test/README-dev.md (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy SchubertGuidelines for test developers
2*e0c4386eSCy Schubert==============================
3*e0c4386eSCy Schubert
4*e0c4386eSCy SchubertHow to add recipes
5*e0c4386eSCy Schubert------------------
6*e0c4386eSCy Schubert
7*e0c4386eSCy SchubertFor any test that you want to perform, you write a script located in
8*e0c4386eSCy Schubert`test/recipes/`, named `{nn}-test_{name}.t`,
9*e0c4386eSCy Schubertwhere `{nn}` is a two digit number and
10*e0c4386eSCy Schubert`{name}` is a unique name of your choice.
11*e0c4386eSCy Schubert
12*e0c4386eSCy SchubertPlease note that if a test involves a new testing executable, you will need to
13*e0c4386eSCy Schubertdo some additions in test/build.info. Please refer to the section
14*e0c4386eSCy Schubert["Changes to test/build.info"](README.md#changes-to-testbuildinfo) below.
15*e0c4386eSCy Schubert
16*e0c4386eSCy SchubertNaming conventions
17*e0c4386eSCy Schubert------------------
18*e0c4386eSCy Schubert
19*e0c4386eSCy SchubertA test executable is named `test/{name}test.c`
20*e0c4386eSCy Schubert
21*e0c4386eSCy SchubertA test recipe is named `test/recipes/{nn}-test_{name}.t`, where `{nn}` is a two
22*e0c4386eSCy Schubertdigit number and `{name}` is a unique name of your choice.
23*e0c4386eSCy Schubert
24*e0c4386eSCy SchubertThe number `{nn}` is (somewhat loosely) grouped as follows:
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert    00-04  sanity, internal and essential API tests
27*e0c4386eSCy Schubert    05-09  individual symmetric cipher algorithms
28*e0c4386eSCy Schubert    10-14  math (bignum)
29*e0c4386eSCy Schubert    15-19  individual asymmetric cipher algorithms
30*e0c4386eSCy Schubert    20-24  openssl commands (some otherwise not tested)
31*e0c4386eSCy Schubert    25-29  certificate forms, generation and verification
32*e0c4386eSCy Schubert    30-35  engine and evp
33*e0c4386eSCy Schubert    60-79  APIs:
34*e0c4386eSCy Schubert       60  X509 subsystem
35*e0c4386eSCy Schubert       61  BIO subsystem
36*e0c4386eSCy Schubert       65  CMP subsystem
37*e0c4386eSCy Schubert       70  PACKET layer
38*e0c4386eSCy Schubert    80-89  "larger" protocols (CA, CMS, OCSP, SSL, TSA)
39*e0c4386eSCy Schubert    90-98  misc
40*e0c4386eSCy Schubert    99     most time consuming tests [such as test_fuzz]
41*e0c4386eSCy Schubert
42*e0c4386eSCy SchubertA recipe that just runs a test executable
43*e0c4386eSCy Schubert-----------------------------------------
44*e0c4386eSCy Schubert
45*e0c4386eSCy SchubertA script that just runs a program looks like this:
46*e0c4386eSCy Schubert
47*e0c4386eSCy Schubert    #! /usr/bin/env perl
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert    use OpenSSL::Test::Simple;
50*e0c4386eSCy Schubert
51*e0c4386eSCy Schubert    simple_test("test_{name}", "{name}test", "{name}");
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert`{name}` is the unique name you have chosen for your test.
54*e0c4386eSCy Schubert
55*e0c4386eSCy SchubertThe second argument to `simple_test` is the test executable, and `simple_test`
56*e0c4386eSCy Schubertexpects it to be located in `test/`
57*e0c4386eSCy Schubert
58*e0c4386eSCy SchubertFor documentation on `OpenSSL::Test::Simple`,
59*e0c4386eSCy Schubertdo `perldoc util/perl/OpenSSL/Test/Simple.pm`.
60*e0c4386eSCy Schubert
61*e0c4386eSCy SchubertA recipe that runs a more complex test
62*e0c4386eSCy Schubert--------------------------------------
63*e0c4386eSCy Schubert
64*e0c4386eSCy SchubertFor more complex tests, you will need to read up on Test::More and
65*e0c4386eSCy SchubertOpenSSL::Test.  Test::More is normally preinstalled, do `man Test::More` for
66*e0c4386eSCy Schubertdocumentation.  For OpenSSL::Test, do `perldoc util/perl/OpenSSL/Test.pm`.
67*e0c4386eSCy Schubert
68*e0c4386eSCy SchubertA script to start from could be this:
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubert    #! /usr/bin/env perl
71*e0c4386eSCy Schubert
72*e0c4386eSCy Schubert    use strict;
73*e0c4386eSCy Schubert    use warnings;
74*e0c4386eSCy Schubert    use OpenSSL::Test;
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert    setup("test_{name}");
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert    plan tests => 2;                # The number of tests being performed
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert    ok(test1, "test1");
81*e0c4386eSCy Schubert    ok(test2, "test1");
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert    sub test1
84*e0c4386eSCy Schubert    {
85*e0c4386eSCy Schubert        # test feature 1
86*e0c4386eSCy Schubert    }
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubert    sub test2
89*e0c4386eSCy Schubert    {
90*e0c4386eSCy Schubert        # test feature 2
91*e0c4386eSCy Schubert    }
92*e0c4386eSCy Schubert
93*e0c4386eSCy SchubertChanges to test/build.info
94*e0c4386eSCy Schubert--------------------------
95*e0c4386eSCy Schubert
96*e0c4386eSCy SchubertWhenever a new test involves a new test executable you need to do the
97*e0c4386eSCy Schubertfollowing (at all times, replace {NAME} and {name} with the name of your
98*e0c4386eSCy Schuberttest):
99*e0c4386eSCy Schubert
100*e0c4386eSCy Schubert * add `{name}` to the list of programs under `PROGRAMS_NO_INST`
101*e0c4386eSCy Schubert
102*e0c4386eSCy Schubert * create a three line description of how to build the test, you will have
103*e0c4386eSCy Schubert   to modify the include paths and source files if you don't want to use the
104*e0c4386eSCy Schubert   basic test framework:
105*e0c4386eSCy Schubert
106*e0c4386eSCy Schubert       SOURCE[{name}]={name}.c
107*e0c4386eSCy Schubert       INCLUDE[{name}]=.. ../include ../apps/include
108*e0c4386eSCy Schubert       DEPEND[{name}]=../libcrypto libtestutil.a
109*e0c4386eSCy Schubert
110*e0c4386eSCy SchubertGeneric form of C test executables
111*e0c4386eSCy Schubert----------------------------------
112*e0c4386eSCy Schubert
113*e0c4386eSCy Schubert    #include "testutil.h"
114*e0c4386eSCy Schubert
115*e0c4386eSCy Schubert    static int my_test(void)
116*e0c4386eSCy Schubert    {
117*e0c4386eSCy Schubert        int testresult = 0;                 /* Assume the test will fail    */
118*e0c4386eSCy Schubert        int observed;
119*e0c4386eSCy Schubert
120*e0c4386eSCy Schubert        observed = function();              /* Call the code under test     */
121*e0c4386eSCy Schubert        if (!TEST_int_eq(observed, 2))      /* Check the result is correct  */
122*e0c4386eSCy Schubert            goto end;                       /* Exit on failure - optional   */
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert        testresult = 1;                     /* Mark the test case a success */
125*e0c4386eSCy Schubert    end:
126*e0c4386eSCy Schubert        cleanup();                          /* Any cleanup you require      */
127*e0c4386eSCy Schubert        return testresult;
128*e0c4386eSCy Schubert    }
129*e0c4386eSCy Schubert
130*e0c4386eSCy Schubert    int setup_tests(void)
131*e0c4386eSCy Schubert    {
132*e0c4386eSCy Schubert        ADD_TEST(my_test);                  /* Add each test separately     */
133*e0c4386eSCy Schubert        return 1;                           /* Indicates success.  Return 0 */
134*e0c4386eSCy Schubert                                            /* to produce an error with a   */
135*e0c4386eSCy Schubert                                            /* usage message and -1 for     */
136*e0c4386eSCy Schubert                                            /* failure to set up with no    */
137*e0c4386eSCy Schubert                                            /* usage message.               */
138*e0c4386eSCy Schubert    }
139*e0c4386eSCy Schubert
140*e0c4386eSCy SchubertYou should use the `TEST_xxx` macros provided by `testutil.h` to test all failure
141*e0c4386eSCy Schubertconditions.  These macros produce an error message in a standard format if the
142*e0c4386eSCy Schubertcondition is not met (and nothing if the condition is met).  Additional
143*e0c4386eSCy Schubertinformation can be presented with the `TEST_info` macro that takes a `printf`
144*e0c4386eSCy Schubertformat string and arguments.  `TEST_error` is useful for complicated conditions,
145*e0c4386eSCy Schubertit also takes a `printf` format string and argument.  In all cases the `TEST_xxx`
146*e0c4386eSCy Schubertmacros are guaranteed to evaluate their arguments exactly once.  This means
147*e0c4386eSCy Schubertthat expressions with side effects are allowed as parameters.  Thus,
148*e0c4386eSCy Schubert
149*e0c4386eSCy Schubert    if (!TEST_ptr(ptr = OPENSSL_malloc(..)))
150*e0c4386eSCy Schubert
151*e0c4386eSCy Schubertworks fine and can be used in place of:
152*e0c4386eSCy Schubert
153*e0c4386eSCy Schubert    ptr = OPENSSL_malloc(..);
154*e0c4386eSCy Schubert    if (!TEST_ptr(ptr))
155*e0c4386eSCy Schubert
156*e0c4386eSCy SchubertThe former produces a more meaningful message on failure than the latter.
157*e0c4386eSCy Schubert
158*e0c4386eSCy SchubertNote that the test infrastructure automatically sets up all required environment
159*e0c4386eSCy Schubertvariables (such as `OPENSSL_MODULES`, `OPENSSL_CONF`, etc.) for the tests.
160*e0c4386eSCy SchubertIndividual tests may choose to override the default settings as required.
161