test.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (c) 2001-2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, 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. #define test_fail_msg(msg) TT_DIE((msg))
  21. #define test_fail() test_fail_msg("Assertion failed.")
  22. #define test_assert(expr) tt_assert(expr)
  23. #define test_eq(expr1, expr2) tt_int_op((expr1), ==, (expr2))
  24. #define test_eq_ptr(expr1, expr2) tt_ptr_op((expr1), ==, (expr2))
  25. #define test_neq(expr1, expr2) tt_int_op((expr1), !=, (expr2))
  26. #define test_neq_ptr(expr1, expr2) tt_ptr_op((expr1), !=, (expr2))
  27. #define test_streq(expr1, expr2) tt_str_op((expr1), ==, (expr2))
  28. #define test_strneq(expr1, expr2) tt_str_op((expr1), !=, (expr2))
  29. #define test_mem_op(expr1, op, expr2, len) \
  30. tt_mem_op((expr1), op, (expr2), (len))
  31. #define test_memeq(expr1, expr2, len) test_mem_op((expr1), ==, (expr2), len)
  32. #define test_memneq(expr1, expr2, len) test_mem_op((expr1), !=, (expr2), len)
  33. /* As test_mem_op, but decodes 'hex' before comparing. There must be a
  34. * local char* variable called mem_op_hex_tmp for this to work. */
  35. #define test_mem_op_hex(expr1, op, hex) \
  36. STMT_BEGIN \
  37. size_t length = strlen(hex); \
  38. tor_free(mem_op_hex_tmp); \
  39. mem_op_hex_tmp = tor_malloc(length/2); \
  40. tor_assert((length&1)==0); \
  41. base16_decode(mem_op_hex_tmp, length/2, hex, length); \
  42. test_mem_op(expr1, op, mem_op_hex_tmp, length/2); \
  43. STMT_END
  44. #define test_memeq_hex(expr1, hex) test_mem_op_hex(expr1, ==, hex)
  45. #define tt_double_op(a,op,b) \
  46. tt_assert_test_type(a,b,#a" "#op" "#b,double,(val1_ op val2_),"%f", \
  47. TT_EXIT_TEST_FUNCTION)
  48. #ifdef _MSC_VER
  49. #define U64_PRINTF_TYPE uint64_t
  50. #define I64_PRINTF_TYPE int64_t
  51. #else
  52. #define U64_PRINTF_TYPE unsigned long long
  53. #define I64_PRINTF_TYPE long long
  54. #endif
  55. #define tt_size_op(a,op,b) \
  56. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,size_t,(val1_ op val2_), \
  57. U64_PRINTF_TYPE, U64_FORMAT, \
  58. {print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  59. #define tt_u64_op(a,op,b) \
  60. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,uint64_t,(val1_ op val2_), \
  61. U64_PRINTF_TYPE, U64_FORMAT, \
  62. {print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  63. #define tt_i64_op(a,op,b) \
  64. tt_assert_test_fmt_type(a,b,#a" "#op" "#b,int64_t,(val1_ op val2_), \
  65. I64_PRINTF_TYPE, I64_FORMAT, \
  66. {print_ = (I64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
  67. const char *get_fname(const char *name);
  68. crypto_pk_t *pk_generate(int idx);
  69. void legacy_test_helper(void *data);
  70. extern const struct testcase_setup_t legacy_setup;
  71. #define US2_CONCAT_2__(a, b) a ## __ ## b
  72. #define US_CONCAT_2__(a, b) a ## _ ## b
  73. #define US_CONCAT_3__(a, b, c) a ## _ ## b ## _ ## c
  74. #define US_CONCAT_2_(a, b) US_CONCAT_2__(a, b)
  75. #define US_CONCAT_3_(a, b, c) US_CONCAT_3__(a, b, c)
  76. /*
  77. * These macros are helpful for streamlining the authorship of several test
  78. * cases that use mocks.
  79. *
  80. * The pattern is as follows.
  81. * * Declare a top level namespace:
  82. * #define NS_MODULE foo
  83. *
  84. * * For each test case you want to write, create a new submodule in the
  85. * namespace. All mocks and other information should belong to a single
  86. * submodule to avoid interference with other test cases.
  87. * You can simply name the submodule after the function in the module you
  88. * are testing:
  89. * #define NS_SUBMODULE some_function
  90. * or, if you're wanting to write several tests against the same function,
  91. * ie., you are testing an aspect of that function, you can use:
  92. * #define NS_SUBMODULE ASPECT(some_function, behavior)
  93. *
  94. * * Declare all the mocks you will use. The NS_DECL macro serves to declare
  95. * the mock in the current namespace (defined by NS_MODULE and NS_SUBMODULE).
  96. * It behaves like MOCK_DECL:
  97. * NS_DECL(int, dependent_function, (void *));
  98. * Here, dependent_function must be declared and implemented with the
  99. * MOCK_DECL and MOCK_IMPL macros. The NS_DECL macro also defines an integer
  100. * global for use for tracking how many times a mock was called, and can be
  101. * accessed by CALLED(mock_name). For example, you might put
  102. * CALLED(dependent_function)++;
  103. * in your mock body.
  104. *
  105. * * Define a function called NS(main) that will contain the body of the
  106. * test case. The NS macro can be used to reference a name in the current
  107. * namespace.
  108. *
  109. * * In NS(main), indicate that a mock function in the current namespace,
  110. * declared with NS_DECL is to override that in the global namespace,
  111. * with the NS_MOCK macro:
  112. * NS_MOCK(dependent_function)
  113. * Unmock with:
  114. * NS_UNMOCK(dependent_function)
  115. *
  116. * * Define the mocks with the NS macro, eg.,
  117. * int
  118. * NS(dependent_function)(void *)
  119. * {
  120. * CALLED(dependent_function)++;
  121. * }
  122. *
  123. * * In the struct testcase_t array, you can use the TEST_CASE and
  124. * TEST_CASE_ASPECT macros to define the cases without having to do so
  125. * explicitly nor without having to reset NS_SUBMODULE, eg.,
  126. * struct testcase_t foo_tests[] = {
  127. * TEST_CASE_ASPECT(some_function, behavior),
  128. * ...
  129. * END_OF_TESTCASES
  130. * which will define a test case named "some_function__behavior".
  131. */
  132. #define NAME_TEST_(name) #name
  133. #define NAME_TEST(name) NAME_TEST_(name)
  134. #define ASPECT(test_module, test_name) US2_CONCAT_2__(test_module, test_name)
  135. #define TEST_CASE(function) \
  136. { \
  137. NAME_TEST(function), \
  138. NS_FULL(NS_MODULE, function, test_main), \
  139. TT_FORK, \
  140. NULL, \
  141. NULL, \
  142. }
  143. #define TEST_CASE_ASPECT(function, aspect) \
  144. { \
  145. NAME_TEST(ASPECT(function, aspect)), \
  146. NS_FULL(NS_MODULE, ASPECT(function, aspect), test_main), \
  147. TT_FORK, \
  148. NULL, \
  149. NULL, \
  150. }
  151. #define NS(name) US_CONCAT_3_(NS_MODULE, NS_SUBMODULE, name)
  152. #define NS_FULL(module, submodule, name) US_CONCAT_3_(module, submodule, name)
  153. #define CALLED(mock_name) US_CONCAT_2_(NS(mock_name), called)
  154. #define NS_DECL(retval, mock_fn, args) \
  155. static retval NS(mock_fn) args; int CALLED(mock_fn) = 0
  156. #define NS_MOCK(name) MOCK(name, NS(name))
  157. #define NS_UNMOCK(name) UNMOCK(name)
  158. #endif