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