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