1 /* Copyright (c) 2001-2003, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2015, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
11 * \brief Macros and functions used by unit tests.
16 #define TT_EXIT_TEST_FUNCTION STMT_BEGIN goto done; STMT_END
17 #include "tinytest_macros.h"
20 #define PRETTY_FUNCTION __PRETTY_FUNCTION__
22 #define PRETTY_FUNCTION ""
25 /* As test_mem_op, but decodes 'hex' before comparing. There must be a
26 * local char* variable called mem_op_hex_tmp for this to work. */
27 #define test_mem_op_hex(expr1, op, hex) \
29 size_t length = strlen(hex); \
30 tor_free(mem_op_hex_tmp); \
31 mem_op_hex_tmp = tor_malloc(length/2); \
32 tor_assert((length&1)==0); \
33 base16_decode(mem_op_hex_tmp, length/2, hex, length); \
34 tt_mem_op(expr1, op, mem_op_hex_tmp, length/2); \
37 #define test_memeq_hex(expr1, hex) test_mem_op_hex(expr1, OP_EQ, hex)
39 #define tt_double_op(a,op,b) \
40 tt_assert_test_type(a,b,#a" "#op" "#b,double,(val1_ op val2_),"%g", \
41 TT_EXIT_TEST_FUNCTION)
43 /* Declare "double equal" in a sneaky way, so compiler won't complain about
44 * comparing floats with == or !=. Of course, only do this if you know what
46 #define tt_double_eq(a,b) \
48 tt_double_op((a), >=, (b)); \
49 tt_double_op((a), <=, (b)); \
53 #define U64_PRINTF_TYPE uint64_t
54 #define I64_PRINTF_TYPE int64_t
56 #define U64_PRINTF_TYPE unsigned long long
57 #define I64_PRINTF_TYPE long long
60 #define tt_size_op(a,op,b) \
61 tt_assert_test_fmt_type(a,b,#a" "#op" "#b,size_t,(val1_ op val2_), \
62 U64_PRINTF_TYPE, U64_FORMAT, \
63 {print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
65 #define tt_u64_op(a,op,b) \
66 tt_assert_test_fmt_type(a,b,#a" "#op" "#b,uint64_t,(val1_ op val2_), \
67 U64_PRINTF_TYPE, U64_FORMAT, \
68 {print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
70 #define tt_i64_op(a,op,b) \
71 tt_assert_test_fmt_type(a,b,#a" "#op" "#b,int64_t,(val1_ op val2_), \
72 I64_PRINTF_TYPE, I64_FORMAT, \
73 {print_ = (I64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
75 const char *get_fname(const char *name
);
76 crypto_pk_t
*pk_generate(int idx
);
78 #define US2_CONCAT_2__(a, b) a ## __ ## b
79 #define US_CONCAT_2__(a, b) a ## _ ## b
80 #define US_CONCAT_3__(a, b, c) a ## _ ## b ## _ ## c
81 #define US_CONCAT_2_(a, b) US_CONCAT_2__(a, b)
82 #define US_CONCAT_3_(a, b, c) US_CONCAT_3__(a, b, c)
85 * These macros are helpful for streamlining the authorship of several test
86 * cases that use mocks.
88 * The pattern is as follows.
89 * * Declare a top level namespace:
90 * #define NS_MODULE foo
92 * * For each test case you want to write, create a new submodule in the
93 * namespace. All mocks and other information should belong to a single
94 * submodule to avoid interference with other test cases.
95 * You can simply name the submodule after the function in the module you
97 * #define NS_SUBMODULE some_function
98 * or, if you're wanting to write several tests against the same function,
99 * ie., you are testing an aspect of that function, you can use:
100 * #define NS_SUBMODULE ASPECT(some_function, behavior)
102 * * Declare all the mocks you will use. The NS_DECL macro serves to declare
103 * the mock in the current namespace (defined by NS_MODULE and NS_SUBMODULE).
104 * It behaves like MOCK_DECL:
105 * NS_DECL(int, dependent_function, (void *));
106 * Here, dependent_function must be declared and implemented with the
107 * MOCK_DECL and MOCK_IMPL macros. The NS_DECL macro also defines an integer
108 * global for use for tracking how many times a mock was called, and can be
109 * accessed by CALLED(mock_name). For example, you might put
110 * CALLED(dependent_function)++;
113 * * Define a function called NS(main) that will contain the body of the
114 * test case. The NS macro can be used to reference a name in the current
117 * * In NS(main), indicate that a mock function in the current namespace,
118 * declared with NS_DECL is to override that in the global namespace,
119 * with the NS_MOCK macro:
120 * NS_MOCK(dependent_function)
122 * NS_UNMOCK(dependent_function)
124 * * Define the mocks with the NS macro, eg.,
126 * NS(dependent_function)(void *)
128 * CALLED(dependent_function)++;
131 * * In the struct testcase_t array, you can use the TEST_CASE and
132 * TEST_CASE_ASPECT macros to define the cases without having to do so
133 * explicitly nor without having to reset NS_SUBMODULE, eg.,
134 * struct testcase_t foo_tests[] = {
135 * TEST_CASE_ASPECT(some_function, behavior),
138 * which will define a test case named "some_function__behavior".
141 #define NAME_TEST_(name) #name
142 #define NAME_TEST(name) NAME_TEST_(name)
143 #define ASPECT(test_module, test_name) US2_CONCAT_2__(test_module, test_name)
144 #define TEST_CASE(function) \
146 NAME_TEST(function), \
147 NS_FULL(NS_MODULE, function, test_main), \
152 #define TEST_CASE_ASPECT(function, aspect) \
154 NAME_TEST(ASPECT(function, aspect)), \
155 NS_FULL(NS_MODULE, ASPECT(function, aspect), test_main), \
161 #define NS(name) US_CONCAT_3_(NS_MODULE, NS_SUBMODULE, name)
162 #define NS_FULL(module, submodule, name) US_CONCAT_3_(module, submodule, name)
164 #define CALLED(mock_name) US_CONCAT_2_(NS(mock_name), called)
165 #define NS_DECL(retval, mock_fn, args) \
166 static retval NS(mock_fn) args; int CALLED(mock_fn) = 0
167 #define NS_MOCK(name) MOCK(name, NS(name))
168 #define NS_UNMOCK(name) UNMOCK(name)
170 extern const struct testcase_setup_t passthrough_setup
;