test.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Copyright (c) 2001-2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, 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. #define DEBUG_SMARTLIST 1
  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. #define tt_size_op(a,op,b) \
  44. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,size_t,(val1_ op val2_), \
  45. size_t, "%"TOR_PRIuSZ, \
  46. {print_ = (size_t) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  47. #define tt_u64_op(a,op,b) \
  48. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,uint64_t,(val1_ op val2_), \
  49. uint64_t, "%"PRIu64, \
  50. {print_ = (uint64_t) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  51. #define tt_i64_op(a,op,b) \
  52. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,int64_t,(val1_ op val2_), \
  53. int64_t, "%"PRId64, \
  54. {print_ = (int64_t) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  55. /**
  56. * Declare that the test is done, even though no tt___op() calls were made.
  57. *
  58. * For use when you only want to test calling something, but not check
  59. * any values/pointers/etc afterwards.
  60. */
  61. #define tt_finished() 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_set_tests[];
  160. extern struct testcase_t address_tests[];
  161. extern struct testcase_t bridges_tests[];
  162. extern struct testcase_t btrack_tests[];
  163. extern struct testcase_t buffer_tests[];
  164. extern struct testcase_t bwmgt_tests[];
  165. extern struct testcase_t cell_format_tests[];
  166. extern struct testcase_t cell_queue_tests[];
  167. extern struct testcase_t channel_tests[];
  168. extern struct testcase_t channelpadding_tests[];
  169. extern struct testcase_t circuitpadding_tests[];
  170. extern struct testcase_t channeltls_tests[];
  171. extern struct testcase_t checkdir_tests[];
  172. extern struct testcase_t circuitbuild_tests[];
  173. extern struct testcase_t circuitlist_tests[];
  174. extern struct testcase_t circuitmux_tests[];
  175. extern struct testcase_t circuitstats_tests[];
  176. extern struct testcase_t circuituse_tests[];
  177. extern struct testcase_t compat_libevent_tests[];
  178. extern struct testcase_t config_tests[];
  179. extern struct testcase_t confmgr_tests[];
  180. extern struct testcase_t confparse_tests[];
  181. extern struct testcase_t connection_tests[];
  182. extern struct testcase_t conscache_tests[];
  183. extern struct testcase_t consdiff_tests[];
  184. extern struct testcase_t consdiffmgr_tests[];
  185. extern struct testcase_t container_tests[];
  186. extern struct testcase_t controller_event_tests[];
  187. extern struct testcase_t controller_tests[];
  188. extern struct testcase_t crypto_ope_tests[];
  189. extern struct testcase_t crypto_openssl_tests[];
  190. extern struct testcase_t crypto_rng_tests[];
  191. extern struct testcase_t crypto_tests[];
  192. extern struct testcase_t dir_handle_get_tests[];
  193. extern struct testcase_t dir_tests[];
  194. extern struct testcase_t dispatch_tests[];
  195. extern struct testcase_t dns_tests[];
  196. extern struct testcase_t dos_tests[];
  197. extern struct testcase_t entryconn_tests[];
  198. extern struct testcase_t entrynodes_tests[];
  199. extern struct testcase_t extorport_tests[];
  200. extern struct testcase_t geoip_tests[];
  201. extern struct testcase_t guardfraction_tests[];
  202. extern struct testcase_t handle_tests[];
  203. extern struct testcase_t hs_cache[];
  204. extern struct testcase_t hs_cell_tests[];
  205. extern struct testcase_t hs_client_tests[];
  206. extern struct testcase_t hs_common_tests[];
  207. extern struct testcase_t hs_config_tests[];
  208. extern struct testcase_t hs_control_tests[];
  209. extern struct testcase_t hs_descriptor[];
  210. extern struct testcase_t hs_dos_tests[];
  211. extern struct testcase_t hs_intropoint_tests[];
  212. extern struct testcase_t hs_ntor_tests[];
  213. extern struct testcase_t hs_service_tests[];
  214. extern struct testcase_t hs_tests[];
  215. extern struct testcase_t introduce_tests[];
  216. extern struct testcase_t keypin_tests[];
  217. extern struct testcase_t link_handshake_tests[];
  218. extern struct testcase_t logging_tests[];
  219. extern struct testcase_t mainloop_tests[];
  220. extern struct testcase_t microdesc_tests[];
  221. extern struct testcase_t namemap_tests[];
  222. extern struct testcase_t netinfo_tests[];
  223. extern struct testcase_t nodelist_tests[];
  224. extern struct testcase_t oom_tests[];
  225. extern struct testcase_t oos_tests[];
  226. extern struct testcase_t options_tests[];
  227. extern struct testcase_t parsecommon_tests[];
  228. extern struct testcase_t pem_tests[];
  229. extern struct testcase_t periodic_event_tests[];
  230. extern struct testcase_t policy_tests[];
  231. extern struct testcase_t prob_distr_tests[];
  232. extern struct testcase_t slow_stochastic_prob_distr_tests[];
  233. extern struct testcase_t procmon_tests[];
  234. extern struct testcase_t process_tests[];
  235. extern struct testcase_t proto_http_tests[];
  236. extern struct testcase_t proto_misc_tests[];
  237. extern struct testcase_t protover_tests[];
  238. extern struct testcase_t pt_tests[];
  239. extern struct testcase_t pubsub_build_tests[];
  240. extern struct testcase_t pubsub_msg_tests[];
  241. extern struct testcase_t relay_tests[];
  242. extern struct testcase_t relaycell_tests[];
  243. extern struct testcase_t relaycrypt_tests[];
  244. extern struct testcase_t rend_cache_tests[];
  245. extern struct testcase_t replaycache_tests[];
  246. extern struct testcase_t router_tests[];
  247. extern struct testcase_t routerkeys_tests[];
  248. extern struct testcase_t routerlist_tests[];
  249. extern struct testcase_t routerset_tests[];
  250. extern struct testcase_t scheduler_tests[];
  251. extern struct testcase_t sendme_tests[];
  252. extern struct testcase_t socks_tests[];
  253. extern struct testcase_t sr_tests[];
  254. extern struct testcase_t status_tests[];
  255. extern struct testcase_t storagedir_tests[];
  256. extern struct testcase_t thread_tests[];
  257. extern struct testcase_t token_bucket_tests[];
  258. extern struct testcase_t tortls_openssl_tests[];
  259. extern struct testcase_t tortls_tests[];
  260. extern struct testcase_t util_format_tests[];
  261. extern struct testcase_t util_process_tests[];
  262. extern struct testcase_t util_tests[];
  263. extern struct testcase_t voting_flags_tests[];
  264. extern struct testcase_t voting_schedule_tests[];
  265. extern struct testcase_t x509_tests[];
  266. extern struct testcase_t slow_crypto_tests[];
  267. extern struct testcase_t slow_process_tests[];
  268. extern struct testcase_t slow_ptr_tests[];
  269. extern struct testgroup_t testgroups[];
  270. extern const char AUTHORITY_CERT_1[];
  271. extern const char AUTHORITY_SIGNKEY_1[];
  272. extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
  273. extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
  274. extern const char AUTHORITY_CERT_2[];
  275. extern const char AUTHORITY_SIGNKEY_2[];
  276. extern const char AUTHORITY_SIGNKEY_B_DIGEST[];
  277. extern const char AUTHORITY_SIGNKEY_B_DIGEST256[];
  278. extern const char AUTHORITY_CERT_3[];
  279. extern const char AUTHORITY_SIGNKEY_3[];
  280. extern const char AUTHORITY_SIGNKEY_C_DIGEST[];
  281. extern const char AUTHORITY_SIGNKEY_C_DIGEST256[];
  282. #endif /* !defined(TOR_TEST_H) */