1 /* 2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #ifndef HEADER_TESTUTIL_H 11 # define HEADER_TESTUTIL_H 12 13 #include <openssl/err.h> 14 15 /*- 16 * SETUP_TEST_FIXTURE and EXECUTE_TEST macros for test case functions. 17 * 18 * SETUP_TEST_FIXTURE will call set_up() to create a new TEST_FIXTURE_TYPE 19 * object called "fixture". It will also allocate the "result" variable used 20 * by EXECUTE_TEST. set_up() should take a const char* specifying the test 21 * case name and return a TEST_FIXTURE_TYPE by value. 22 * 23 * EXECUTE_TEST will pass fixture to execute_func() by value, call 24 * tear_down(), and return the result of execute_func(). execute_func() should 25 * take a TEST_FIXTURE_TYPE by value and return 1 on success and 0 on 26 * failure. 27 * 28 * Unit tests can define their own SETUP_TEST_FIXTURE and EXECUTE_TEST 29 * variations like so: 30 * 31 * #define SETUP_FOOBAR_TEST_FIXTURE()\ 32 * SETUP_TEST_FIXTURE(FOOBAR_TEST_FIXTURE, set_up_foobar) 33 * 34 * #define EXECUTE_FOOBAR_TEST()\ 35 * EXECUTE_TEST(execute_foobar, tear_down_foobar) 36 * 37 * Then test case functions can take the form: 38 * 39 * static int test_foobar_feature() 40 * { 41 * SETUP_FOOBAR_TEST_FIXTURE(); 42 * [...set individual members of fixture...] 43 * EXECUTE_FOOBAR_TEST(); 44 * } 45 */ 46 # define SETUP_TEST_FIXTURE(TEST_FIXTURE_TYPE, set_up)\ 47 TEST_FIXTURE_TYPE fixture = set_up(TEST_CASE_NAME); \ 48 int result = 0 49 50 # define EXECUTE_TEST(execute_func, tear_down)\ 51 result = execute_func(fixture);\ 52 tear_down(fixture);\ 53 return result 54 55 /* 56 * TEST_CASE_NAME is defined as the name of the test case function where 57 * possible; otherwise we get by with the file name and line number. 58 */ 59 # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L 60 # if defined(_MSC_VER) 61 # define TEST_CASE_NAME __FUNCTION__ 62 # else 63 # define testutil_stringify_helper(s) #s 64 # define testutil_stringify(s) testutil_stringify_helper(s) 65 # define TEST_CASE_NAME __FILE__ ":" testutil_stringify(__LINE__) 66 # endif /* _MSC_VER */ 67 # else 68 # define TEST_CASE_NAME __func__ 69 # endif /* __STDC_VERSION__ */ 70 71 /* 72 * In main(), call ADD_TEST to register each test case function, then call 73 * run_tests() to execute all tests and report the results. The result 74 * returned from run_tests() should be used as the return value for main(). 75 */ 76 # define ADD_TEST(test_function) add_test(#test_function, test_function) 77 78 /* 79 * Simple parameterized tests. Adds a test_function(idx) test for each 80 * 0 <= idx < num. 81 */ 82 # define ADD_ALL_TESTS(test_function, num) \ 83 add_all_tests(#test_function, test_function, num) 84 85 void add_test(const char *test_case_name, int (*test_fn) ()); 86 void add_all_tests(const char *test_case_name, int (*test_fn)(int idx), int num); 87 int run_tests(const char *test_prog_name); 88 89 /* 90 * Test assumption verification helpers. 91 */ 92 93 /* 94 * Returns 1 if |s1| and |s2| are both NULL or equal. 95 * Otherwise, returns 0 and pretty-prints diagnostics using |desc|. 96 */ 97 int strings_equal(const char *desc, const char *s1, const char *s2); 98 #endif /* HEADER_TESTUTIL_H */ 99 100 /* 101 * For "impossible" conditions such as malloc failures or bugs in test code, 102 * where continuing the test would be meaningless. Note that OPENSSL_assert 103 * is fatal, and is never compiled out. 104 */ 105 #define TEST_check(condition) \ 106 do { \ 107 if (!(condition)) { \ 108 ERR_print_errors_fp(stderr); \ 109 OPENSSL_assert(!#condition); \ 110 } \ 111 } while (0); 112