xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/test/README (revision 4724848cf0da353df257f730694b7882798e5daf)
1*4724848cSchristosHow to add recipes
2*4724848cSchristos==================
3*4724848cSchristos
4*4724848cSchristosFor any test that you want to perform, you write a script located in
5*4724848cSchristostest/recipes/, named {nn}-test_{name}.t, where {nn} is a two digit number and
6*4724848cSchristos{name} is a unique name of your choice.
7*4724848cSchristos
8*4724848cSchristosPlease note that if a test involves a new testing executable, you will need to
9*4724848cSchristosdo some additions in test/Makefile.  More on this later.
10*4724848cSchristos
11*4724848cSchristos
12*4724848cSchristosNaming conventions
13*4724848cSchristos=================
14*4724848cSchristos
15*4724848cSchristosA test executable is named test/{name}test.c
16*4724848cSchristos
17*4724848cSchristosA test recipe is named test/recipes/{nn}-test_{name}.t, where {nn} is a two
18*4724848cSchristosdigit number and {name} is a unique name of your choice.
19*4724848cSchristos
20*4724848cSchristosThe number {nn} is (somewhat loosely) grouped as follows:
21*4724848cSchristos
22*4724848cSchristos00-04  sanity, internal and essential API tests
23*4724848cSchristos05-09  individual symmetric cipher algorithms
24*4724848cSchristos10-14  math (bignum)
25*4724848cSchristos15-19  individual asymmetric cipher algorithms
26*4724848cSchristos20-24  openssl commands (some otherwise not tested)
27*4724848cSchristos25-29  certificate forms, generation and verification
28*4724848cSchristos30-35  engine and evp
29*4724848cSchristos60-79  APIs
30*4724848cSchristos   70  PACKET layer
31*4724848cSchristos80-89  "larger" protocols (CA, CMS, OCSP, SSL, TSA)
32*4724848cSchristos90-98  misc
33*4724848cSchristos99     most time consuming tests [such as test_fuzz]
34*4724848cSchristos
35*4724848cSchristos
36*4724848cSchristosA recipe that just runs a test executable
37*4724848cSchristos=========================================
38*4724848cSchristos
39*4724848cSchristosA script that just runs a program looks like this:
40*4724848cSchristos
41*4724848cSchristos    #! /usr/bin/perl
42*4724848cSchristos
43*4724848cSchristos    use OpenSSL::Test::Simple;
44*4724848cSchristos
45*4724848cSchristos    simple_test("test_{name}", "{name}test", "{name}");
46*4724848cSchristos
47*4724848cSchristos{name} is the unique name you have chosen for your test.
48*4724848cSchristos
49*4724848cSchristosThe second argument to `simple_test' is the test executable, and `simple_test'
50*4724848cSchristosexpects it to be located in test/
51*4724848cSchristos
52*4724848cSchristosFor documentation on OpenSSL::Test::Simple, do
53*4724848cSchristos`perldoc util/perl/OpenSSL/Test/Simple.pm'.
54*4724848cSchristos
55*4724848cSchristos
56*4724848cSchristosA recipe that runs a more complex test
57*4724848cSchristos======================================
58*4724848cSchristos
59*4724848cSchristosFor more complex tests, you will need to read up on Test::More and
60*4724848cSchristosOpenSSL::Test.  Test::More is normally preinstalled, do `man Test::More' for
61*4724848cSchristosdocumentation.  For OpenSSL::Test, do `perldoc util/perl/OpenSSL/Test.pm'.
62*4724848cSchristos
63*4724848cSchristosA script to start from could be this:
64*4724848cSchristos
65*4724848cSchristos    #! /usr/bin/perl
66*4724848cSchristos
67*4724848cSchristos    use strict;
68*4724848cSchristos    use warnings;
69*4724848cSchristos    use OpenSSL::Test;
70*4724848cSchristos
71*4724848cSchristos    setup("test_{name}");
72*4724848cSchristos
73*4724848cSchristos    plan tests => 2;                # The number of tests being performed
74*4724848cSchristos
75*4724848cSchristos    ok(test1, "test1");
76*4724848cSchristos    ok(test2, "test1");
77*4724848cSchristos
78*4724848cSchristos    sub test1
79*4724848cSchristos    {
80*4724848cSchristos        # test feature 1
81*4724848cSchristos    }
82*4724848cSchristos
83*4724848cSchristos    sub test2
84*4724848cSchristos    {
85*4724848cSchristos        # test feature 2
86*4724848cSchristos    }
87*4724848cSchristos
88*4724848cSchristos
89*4724848cSchristosChanges to test/build.info
90*4724848cSchristos==========================
91*4724848cSchristos
92*4724848cSchristosWhenever a new test involves a new test executable you need to do the
93*4724848cSchristosfollowing (at all times, replace {NAME} and {name} with the name of your
94*4724848cSchristostest):
95*4724848cSchristos
96*4724848cSchristos* add {name} to the list of programs under PROGRAMS_NO_INST
97*4724848cSchristos
98*4724848cSchristos* create a three line description of how to build the test, you will have
99*4724848cSchristosto modify the include paths and source files if you don't want to use the
100*4724848cSchristosbasic test framework:
101*4724848cSchristos
102*4724848cSchristos    SOURCE[{name}]={name}.c
103*4724848cSchristos    INCLUDE[{name}]=.. ../include
104*4724848cSchristos    DEPEND[{name}]=../libcrypto libtestutil.a
105*4724848cSchristos
106*4724848cSchristosGeneric form of C test executables
107*4724848cSchristos==================================
108*4724848cSchristos
109*4724848cSchristos    #include "testutil.h"
110*4724848cSchristos
111*4724848cSchristos    static int my_test(void)
112*4724848cSchristos    {
113*4724848cSchristos        int testresult = 0;                 /* Assume the test will fail    */
114*4724848cSchristos        int observed;
115*4724848cSchristos
116*4724848cSchristos        observed = function();              /* Call the code under test     */
117*4724848cSchristos        if (!TEST_int_eq(observed, 2))      /* Check the result is correct  */
118*4724848cSchristos            goto end;                       /* Exit on failure - optional   */
119*4724848cSchristos
120*4724848cSchristos        testresult = 1;                     /* Mark the test case a success */
121*4724848cSchristos    end:
122*4724848cSchristos        cleanup();                          /* Any cleanup you require      */
123*4724848cSchristos        return testresult;
124*4724848cSchristos    }
125*4724848cSchristos
126*4724848cSchristos    int setup_tests(void)
127*4724848cSchristos    {
128*4724848cSchristos        ADD_TEST(my_test);                  /* Add each test separately     */
129*4724848cSchristos        return 1;                           /* Indicate success             */
130*4724848cSchristos    }
131*4724848cSchristos
132*4724848cSchristosYou should use the TEST_xxx macros provided by testutil.h to test all failure
133*4724848cSchristosconditions.  These macros produce an error message in a standard format if the
134*4724848cSchristoscondition is not met (and nothing if the condition is met).  Additional
135*4724848cSchristosinformation can be presented with the TEST_info macro that takes a printf
136*4724848cSchristosformat string and arguments.  TEST_error is useful for complicated conditions,
137*4724848cSchristosit also takes a printf format string and argument.  In all cases the TEST_xxx
138*4724848cSchristosmacros are guaranteed to evaluate their arguments exactly once.  This means
139*4724848cSchristosthat expressions with side effects are allowed as parameters.  Thus,
140*4724848cSchristos
141*4724848cSchristos    if (!TEST_ptr(ptr = OPENSSL_malloc(..)))
142*4724848cSchristos
143*4724848cSchristosworks fine and can be used in place of:
144*4724848cSchristos
145*4724848cSchristos    ptr = OPENSSL_malloc(..);
146*4724848cSchristos    if (!TEST_ptr(ptr))
147*4724848cSchristos
148*4724848cSchristosThe former produces a more meaningful message on failure than the latter.
149*4724848cSchristos
150