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