src-test-test.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright (c) 2001-2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2010, 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_streq(expr1, expr2) tt_str_op((expr1), ==, (expr2))
  30. #define test_mem_op(expr1, op, expr2, len) \
  31. tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2, \
  32. const char *, \
  33. (memcmp(_val1, _val2, len) op 0), \
  34. char *, "%s", \
  35. { size_t printlen = (len)*2+1; \
  36. _print = tor_malloc(printlen); \
  37. base16_encode(_print, printlen, _value, \
  38. (len)); }, \
  39. { tor_free(_print); } \
  40. );
  41. #define test_memeq(expr1, expr2, len) test_mem_op((expr1), ==, (expr2), len)
  42. #define test_memneq(expr1, expr2, len) test_mem_op((expr1), !=, (expr2), len)
  43. /* As test_mem_op, but decodes 'hex' before comparing. There must be a
  44. * local char* variable called mem_op_hex_tmp for this to work. */
  45. #define test_mem_op_hex(expr1, op, hex) \
  46. STMT_BEGIN \
  47. size_t length = strlen(hex); \
  48. tor_free(mem_op_hex_tmp); \
  49. mem_op_hex_tmp = tor_malloc(length/2); \
  50. tor_assert((length&1)==0); \
  51. base16_decode(mem_op_hex_tmp, length/2, hex, length); \
  52. test_mem_op(expr1, op, mem_op_hex_tmp, length/2); \
  53. STMT_END
  54. #define test_memeq_hex(expr1, hex) test_mem_op_hex(expr1, ==, hex)
  55. const char *get_fname(const char *name);
  56. crypto_pk_env_t *pk_generate(int idx);
  57. void legacy_test_helper(void *data);
  58. extern const struct testcase_setup_t legacy_setup;
  59. #endif