test.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright (c) 2001-2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, 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), OP_GE, (b)); \
  41. tt_double_op((a), OP_LE, (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 /* defined(_MSC_VER) */
  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. const char *get_fname_rnd(const char *name);
  64. struct crypto_pk_t *pk_generate(int idx);
  65. void init_pregenerated_keys(void);
  66. void free_pregenerated_keys(void);
  67. #define US2_CONCAT_2__(a, b) a ## __ ## b
  68. #define US_CONCAT_2__(a, b) a ## _ ## b
  69. #define US_CONCAT_3__(a, b, c) a ## _ ## b ## _ ## c
  70. #define US_CONCAT_2_(a, b) US_CONCAT_2__(a, b)
  71. #define US_CONCAT_3_(a, b, c) US_CONCAT_3__(a, b, c)
  72. /*
  73. * These macros are helpful for streamlining the authorship of several test
  74. * cases that use mocks.
  75. *
  76. * The pattern is as follows.
  77. * * Declare a top level namespace:
  78. * #define NS_MODULE foo
  79. *
  80. * * For each test case you want to write, create a new submodule in the
  81. * namespace. All mocks and other information should belong to a single
  82. * submodule to avoid interference with other test cases.
  83. * You can simply name the submodule after the function in the module you
  84. * are testing:
  85. * #define NS_SUBMODULE some_function
  86. * or, if you're wanting to write several tests against the same function,
  87. * ie., you are testing an aspect of that function, you can use:
  88. * #define NS_SUBMODULE ASPECT(some_function, behavior)
  89. *
  90. * * Declare all the mocks you will use. The NS_DECL macro serves to declare
  91. * the mock in the current namespace (defined by NS_MODULE and NS_SUBMODULE).
  92. * It behaves like MOCK_DECL:
  93. * NS_DECL(int, dependent_function, (void *));
  94. * Here, dependent_function must be declared and implemented with the
  95. * MOCK_DECL and MOCK_IMPL macros. The NS_DECL macro also defines an integer
  96. * global for use for tracking how many times a mock was called, and can be
  97. * accessed by CALLED(mock_name). For example, you might put
  98. * CALLED(dependent_function)++;
  99. * in your mock body.
  100. *
  101. * * Define a function called NS(main) that will contain the body of the
  102. * test case. The NS macro can be used to reference a name in the current
  103. * namespace.
  104. *
  105. * * In NS(main), indicate that a mock function in the current namespace,
  106. * declared with NS_DECL is to override that in the global namespace,
  107. * with the NS_MOCK macro:
  108. * NS_MOCK(dependent_function)
  109. * Unmock with:
  110. * NS_UNMOCK(dependent_function)
  111. *
  112. * * Define the mocks with the NS macro, eg.,
  113. * int
  114. * NS(dependent_function)(void *)
  115. * {
  116. * CALLED(dependent_function)++;
  117. * }
  118. *
  119. * * In the struct testcase_t array, you can use the TEST_CASE and
  120. * TEST_CASE_ASPECT macros to define the cases without having to do so
  121. * explicitly nor without having to reset NS_SUBMODULE, eg.,
  122. * struct testcase_t foo_tests[] = {
  123. * TEST_CASE_ASPECT(some_function, behavior),
  124. * ...
  125. * END_OF_TESTCASES
  126. * which will define a test case named "some_function__behavior".
  127. */
  128. #define NAME_TEST_(name) #name
  129. #define NAME_TEST(name) NAME_TEST_(name)
  130. #define ASPECT(test_module, test_name) US2_CONCAT_2__(test_module, test_name)
  131. #define TEST_CASE(function) \
  132. { \
  133. NAME_TEST(function), \
  134. NS_FULL(NS_MODULE, function, test_main), \
  135. TT_FORK, \
  136. NULL, \
  137. NULL, \
  138. }
  139. #define TEST_CASE_ASPECT(function, aspect) \
  140. { \
  141. NAME_TEST(ASPECT(function, aspect)), \
  142. NS_FULL(NS_MODULE, ASPECT(function, aspect), test_main), \
  143. TT_FORK, \
  144. NULL, \
  145. NULL, \
  146. }
  147. #define NS(name) US_CONCAT_3_(NS_MODULE, NS_SUBMODULE, name)
  148. #define NS_FULL(module, submodule, name) US_CONCAT_3_(module, submodule, name)
  149. #define CALLED(mock_name) US_CONCAT_2_(NS(mock_name), called)
  150. #define NS_DECL(retval, mock_fn, args) \
  151. extern int CALLED(mock_fn); \
  152. static retval NS(mock_fn) args; int CALLED(mock_fn) = 0
  153. #define NS_MOCK(name) MOCK(name, NS(name))
  154. #define NS_UNMOCK(name) UNMOCK(name)
  155. extern const struct testcase_setup_t passthrough_setup;
  156. extern const struct testcase_setup_t ed25519_test_setup;
  157. extern struct testcase_t accounting_tests[];
  158. extern struct testcase_t addr_tests[];
  159. extern struct testcase_t address_tests[];
  160. extern struct testcase_t buffer_tests[];
  161. extern struct testcase_t cell_format_tests[];
  162. extern struct testcase_t cell_queue_tests[];
  163. extern struct testcase_t channel_tests[];
  164. extern struct testcase_t channelpadding_tests[];
  165. extern struct testcase_t channeltls_tests[];
  166. extern struct testcase_t checkdir_tests[];
  167. extern struct testcase_t circuitbuild_tests[];
  168. extern struct testcase_t circuitlist_tests[];
  169. extern struct testcase_t circuitmux_tests[];
  170. extern struct testcase_t circuituse_tests[];
  171. extern struct testcase_t circuitstats_tests[];
  172. extern struct testcase_t compat_libevent_tests[];
  173. extern struct testcase_t config_tests[];
  174. extern struct testcase_t connection_tests[];
  175. extern struct testcase_t conscache_tests[];
  176. extern struct testcase_t consdiff_tests[];
  177. extern struct testcase_t consdiffmgr_tests[];
  178. extern struct testcase_t container_tests[];
  179. extern struct testcase_t controller_tests[];
  180. extern struct testcase_t controller_event_tests[];
  181. extern struct testcase_t crypto_tests[];
  182. extern struct testcase_t crypto_openssl_tests[];
  183. extern struct testcase_t dir_tests[];
  184. extern struct testcase_t dir_handle_get_tests[];
  185. extern struct testcase_t dos_tests[];
  186. extern struct testcase_t entryconn_tests[];
  187. extern struct testcase_t entrynodes_tests[];
  188. extern struct testcase_t guardfraction_tests[];
  189. extern struct testcase_t extorport_tests[];
  190. extern struct testcase_t hs_tests[];
  191. extern struct testcase_t hs_cache[];
  192. extern struct testcase_t hs_cell_tests[];
  193. extern struct testcase_t hs_common_tests[];
  194. extern struct testcase_t hs_config_tests[];
  195. extern struct testcase_t hs_control_tests[];
  196. extern struct testcase_t hs_descriptor[];
  197. extern struct testcase_t hs_ntor_tests[];
  198. extern struct testcase_t hs_service_tests[];
  199. extern struct testcase_t hs_client_tests[];
  200. extern struct testcase_t hs_intropoint_tests[];
  201. extern struct testcase_t introduce_tests[];
  202. extern struct testcase_t keypin_tests[];
  203. extern struct testcase_t link_handshake_tests[];
  204. extern struct testcase_t logging_tests[];
  205. extern struct testcase_t microdesc_tests[];
  206. extern struct testcase_t nodelist_tests[];
  207. extern struct testcase_t oom_tests[];
  208. extern struct testcase_t oos_tests[];
  209. extern struct testcase_t options_tests[];
  210. extern struct testcase_t policy_tests[];
  211. extern struct testcase_t procmon_tests[];
  212. extern struct testcase_t proto_http_tests[];
  213. extern struct testcase_t proto_misc_tests[];
  214. extern struct testcase_t protover_tests[];
  215. extern struct testcase_t pubsub_tests[];
  216. extern struct testcase_t pt_tests[];
  217. extern struct testcase_t relay_tests[];
  218. extern struct testcase_t relaycell_tests[];
  219. extern struct testcase_t rend_cache_tests[];
  220. extern struct testcase_t replaycache_tests[];
  221. extern struct testcase_t router_tests[];
  222. extern struct testcase_t routerkeys_tests[];
  223. extern struct testcase_t routerlist_tests[];
  224. extern struct testcase_t routerset_tests[];
  225. extern struct testcase_t scheduler_tests[];
  226. extern struct testcase_t storagedir_tests[];
  227. extern struct testcase_t socks_tests[];
  228. extern struct testcase_t status_tests[];
  229. extern struct testcase_t thread_tests[];
  230. extern struct testcase_t tortls_tests[];
  231. extern struct testcase_t util_tests[];
  232. extern struct testcase_t util_format_tests[];
  233. extern struct testcase_t util_process_tests[];
  234. extern struct testcase_t dns_tests[];
  235. extern struct testcase_t handle_tests[];
  236. extern struct testcase_t sr_tests[];
  237. extern struct testcase_t slow_crypto_tests[];
  238. extern struct testcase_t slow_util_tests[];
  239. extern struct testgroup_t testgroups[];
  240. extern const char AUTHORITY_CERT_1[];
  241. extern const char AUTHORITY_SIGNKEY_1[];
  242. extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
  243. extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
  244. extern const char AUTHORITY_CERT_2[];
  245. extern const char AUTHORITY_SIGNKEY_2[];
  246. extern const char AUTHORITY_SIGNKEY_B_DIGEST[];
  247. extern const char AUTHORITY_SIGNKEY_B_DIGEST256[];
  248. extern const char AUTHORITY_CERT_3[];
  249. extern const char AUTHORITY_SIGNKEY_3[];
  250. extern const char AUTHORITY_SIGNKEY_C_DIGEST[];
  251. extern const char AUTHORITY_SIGNKEY_C_DIGEST256[];
  252. #endif /* !defined(TOR_TEST_H) */