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