test.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Copyright (c) 2001-2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_TEST_H
  6. #define TOR_TEST_H
  7. /**
  8. * \file test.h
  9. * \brief Macros and functions used by unit tests.
  10. */
  11. #include "compat.h"
  12. #include "tinytest.h"
  13. #define TT_EXIT_TEST_FUNCTION STMT_BEGIN goto done; STMT_END
  14. #include "tinytest_macros.h"
  15. #ifdef __GNUC__
  16. #define PRETTY_FUNCTION __PRETTY_FUNCTION__
  17. #else
  18. #define PRETTY_FUNCTION ""
  19. #endif
  20. /* As test_mem_op, but decodes 'hex' before comparing. There must be a
  21. * local char* variable called mem_op_hex_tmp for this to work. */
  22. #define test_mem_op_hex(expr1, op, hex) \
  23. STMT_BEGIN \
  24. size_t length = strlen(hex); \
  25. tor_free(mem_op_hex_tmp); \
  26. mem_op_hex_tmp = tor_malloc(length/2); \
  27. tor_assert((length&1)==0); \
  28. base16_decode(mem_op_hex_tmp, length/2, hex, length); \
  29. tt_mem_op(expr1, op, mem_op_hex_tmp, length/2); \
  30. STMT_END
  31. #define test_memeq_hex(expr1, hex) test_mem_op_hex(expr1, OP_EQ, hex)
  32. #define tt_double_op(a,op,b) \
  33. tt_assert_test_type(a,b,#a" "#op" "#b,double,(val1_ op val2_),"%g", \
  34. TT_EXIT_TEST_FUNCTION)
  35. /* Declare "double equal" in a sneaky way, so compiler won't complain about
  36. * comparing floats with == or !=. Of course, only do this if you know what
  37. * you're doing. */
  38. #define tt_double_eq(a,b) \
  39. STMT_BEGIN \
  40. tt_double_op((a), >=, (b)); \
  41. tt_double_op((a), <=, (b)); \
  42. STMT_END
  43. #ifdef _MSC_VER
  44. #define U64_PRINTF_TYPE uint64_t
  45. #define I64_PRINTF_TYPE int64_t
  46. #else
  47. #define U64_PRINTF_TYPE unsigned long long
  48. #define I64_PRINTF_TYPE long long
  49. #endif
  50. #define tt_size_op(a,op,b) \
  51. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,size_t,(val1_ op val2_), \
  52. U64_PRINTF_TYPE, U64_FORMAT, \
  53. {print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  54. #define tt_u64_op(a,op,b) \
  55. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,uint64_t,(val1_ op val2_), \
  56. U64_PRINTF_TYPE, U64_FORMAT, \
  57. {print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  58. #define tt_i64_op(a,op,b) \
  59. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,int64_t,(val1_ op val2_), \
  60. I64_PRINTF_TYPE, I64_FORMAT, \
  61. {print_ = (I64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  62. const char *get_fname(const char *name);
  63. struct crypto_pk_t *pk_generate(int idx);
  64. #define US2_CONCAT_2__(a, b) a ## __ ## b
  65. #define US_CONCAT_2__(a, b) a ## _ ## b
  66. #define US_CONCAT_3__(a, b, c) a ## _ ## b ## _ ## c
  67. #define US_CONCAT_2_(a, b) US_CONCAT_2__(a, b)
  68. #define US_CONCAT_3_(a, b, c) US_CONCAT_3__(a, b, c)
  69. /*
  70. * These macros are helpful for streamlining the authorship of several test
  71. * cases that use mocks.
  72. *
  73. * The pattern is as follows.
  74. * * Declare a top level namespace:
  75. * #define NS_MODULE foo
  76. *
  77. * * For each test case you want to write, create a new submodule in the
  78. * namespace. All mocks and other information should belong to a single
  79. * submodule to avoid interference with other test cases.
  80. * You can simply name the submodule after the function in the module you
  81. * are testing:
  82. * #define NS_SUBMODULE some_function
  83. * or, if you're wanting to write several tests against the same function,
  84. * ie., you are testing an aspect of that function, you can use:
  85. * #define NS_SUBMODULE ASPECT(some_function, behavior)
  86. *
  87. * * Declare all the mocks you will use. The NS_DECL macro serves to declare
  88. * the mock in the current namespace (defined by NS_MODULE and NS_SUBMODULE).
  89. * It behaves like MOCK_DECL:
  90. * NS_DECL(int, dependent_function, (void *));
  91. * Here, dependent_function must be declared and implemented with the
  92. * MOCK_DECL and MOCK_IMPL macros. The NS_DECL macro also defines an integer
  93. * global for use for tracking how many times a mock was called, and can be
  94. * accessed by CALLED(mock_name). For example, you might put
  95. * CALLED(dependent_function)++;
  96. * in your mock body.
  97. *
  98. * * Define a function called NS(main) that will contain the body of the
  99. * test case. The NS macro can be used to reference a name in the current
  100. * namespace.
  101. *
  102. * * In NS(main), indicate that a mock function in the current namespace,
  103. * declared with NS_DECL is to override that in the global namespace,
  104. * with the NS_MOCK macro:
  105. * NS_MOCK(dependent_function)
  106. * Unmock with:
  107. * NS_UNMOCK(dependent_function)
  108. *
  109. * * Define the mocks with the NS macro, eg.,
  110. * int
  111. * NS(dependent_function)(void *)
  112. * {
  113. * CALLED(dependent_function)++;
  114. * }
  115. *
  116. * * In the struct testcase_t array, you can use the TEST_CASE and
  117. * TEST_CASE_ASPECT macros to define the cases without having to do so
  118. * explicitly nor without having to reset NS_SUBMODULE, eg.,
  119. * struct testcase_t foo_tests[] = {
  120. * TEST_CASE_ASPECT(some_function, behavior),
  121. * ...
  122. * END_OF_TESTCASES
  123. * which will define a test case named "some_function__behavior".
  124. */
  125. #define NAME_TEST_(name) #name
  126. #define NAME_TEST(name) NAME_TEST_(name)
  127. #define ASPECT(test_module, test_name) US2_CONCAT_2__(test_module, test_name)
  128. #define TEST_CASE(function) \
  129. { \
  130. NAME_TEST(function), \
  131. NS_FULL(NS_MODULE, function, test_main), \
  132. TT_FORK, \
  133. NULL, \
  134. NULL, \
  135. }
  136. #define TEST_CASE_ASPECT(function, aspect) \
  137. { \
  138. NAME_TEST(ASPECT(function, aspect)), \
  139. NS_FULL(NS_MODULE, ASPECT(function, aspect), test_main), \
  140. TT_FORK, \
  141. NULL, \
  142. NULL, \
  143. }
  144. #define NS(name) US_CONCAT_3_(NS_MODULE, NS_SUBMODULE, name)
  145. #define NS_FULL(module, submodule, name) US_CONCAT_3_(module, submodule, name)
  146. #define CALLED(mock_name) US_CONCAT_2_(NS(mock_name), called)
  147. #define NS_DECL(retval, mock_fn, args) \
  148. extern int CALLED(mock_fn); \
  149. static retval NS(mock_fn) args; int CALLED(mock_fn) = 0
  150. #define NS_MOCK(name) MOCK(name, NS(name))
  151. #define NS_UNMOCK(name) UNMOCK(name)
  152. extern const struct testcase_setup_t passthrough_setup;
  153. extern struct testcase_t accounting_tests[];
  154. extern struct testcase_t addr_tests[];
  155. extern struct testcase_t address_tests[];
  156. extern struct testcase_t buffer_tests[];
  157. extern struct testcase_t cell_format_tests[];
  158. extern struct testcase_t cell_queue_tests[];
  159. extern struct testcase_t channel_tests[];
  160. extern struct testcase_t channeltls_tests[];
  161. extern struct testcase_t checkdir_tests[];
  162. extern struct testcase_t circuitlist_tests[];
  163. extern struct testcase_t circuitmux_tests[];
  164. extern struct testcase_t compat_libevent_tests[];
  165. extern struct testcase_t config_tests[];
  166. extern struct testcase_t connection_tests[];
  167. extern struct testcase_t container_tests[];
  168. extern struct testcase_t controller_tests[];
  169. extern struct testcase_t controller_event_tests[];
  170. extern struct testcase_t crypto_tests[];
  171. extern struct testcase_t dir_tests[];
  172. extern struct testcase_t dir_handle_get_tests[];
  173. extern struct testcase_t entryconn_tests[];
  174. extern struct testcase_t entrynodes_tests[];
  175. extern struct testcase_t guardfraction_tests[];
  176. extern struct testcase_t extorport_tests[];
  177. extern struct testcase_t hs_tests[];
  178. extern struct testcase_t introduce_tests[];
  179. extern struct testcase_t keypin_tests[];
  180. extern struct testcase_t link_handshake_tests[];
  181. extern struct testcase_t logging_tests[];
  182. extern struct testcase_t microdesc_tests[];
  183. extern struct testcase_t nodelist_tests[];
  184. extern struct testcase_t oom_tests[];
  185. extern struct testcase_t oos_tests[];
  186. extern struct testcase_t options_tests[];
  187. extern struct testcase_t policy_tests[];
  188. extern struct testcase_t procmon_tests[];
  189. extern struct testcase_t pubsub_tests[];
  190. extern struct testcase_t pt_tests[];
  191. extern struct testcase_t relay_tests[];
  192. extern struct testcase_t relaycell_tests[];
  193. extern struct testcase_t rend_cache_tests[];
  194. extern struct testcase_t replaycache_tests[];
  195. extern struct testcase_t router_tests[];
  196. extern struct testcase_t routerkeys_tests[];
  197. extern struct testcase_t routerlist_tests[];
  198. extern struct testcase_t routerset_tests[];
  199. extern struct testcase_t scheduler_tests[];
  200. extern struct testcase_t socks_tests[];
  201. extern struct testcase_t status_tests[];
  202. extern struct testcase_t thread_tests[];
  203. extern struct testcase_t tortls_tests[];
  204. extern struct testcase_t util_tests[];
  205. extern struct testcase_t util_format_tests[];
  206. extern struct testcase_t util_process_tests[];
  207. extern struct testcase_t dns_tests[];
  208. extern struct testcase_t handle_tests[];
  209. extern struct testcase_t sr_tests[];
  210. extern struct testcase_t slow_crypto_tests[];
  211. extern struct testcase_t slow_util_tests[];
  212. extern struct testgroup_t testgroups[];
  213. extern const char AUTHORITY_CERT_1[];
  214. extern const char AUTHORITY_SIGNKEY_1[];
  215. extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
  216. extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
  217. extern const char AUTHORITY_CERT_2[];
  218. extern const char AUTHORITY_SIGNKEY_2[];
  219. extern const char AUTHORITY_SIGNKEY_B_DIGEST[];
  220. extern const char AUTHORITY_SIGNKEY_B_DIGEST256[];
  221. extern const char AUTHORITY_CERT_3[];
  222. extern const char AUTHORITY_SIGNKEY_3[];
  223. extern const char AUTHORITY_SIGNKEY_C_DIGEST[];
  224. extern const char AUTHORITY_SIGNKEY_C_DIGEST256[];
  225. #endif