log_test_helpers.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (c) 2014-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "core/or/or.h"
  4. #ifndef TOR_LOG_TEST_HELPERS_H
  5. #define TOR_LOG_TEST_HELPERS_H
  6. /** An element of mock_saved_logs(); records the log element that we
  7. * received. */
  8. typedef struct mock_saved_log_entry_t {
  9. int severity;
  10. const char *funcname;
  11. const char *suffix;
  12. const char *format;
  13. char *generated_msg;
  14. } mock_saved_log_entry_t;
  15. void mock_clean_saved_logs(void);
  16. const smartlist_t *mock_saved_logs(void);
  17. void setup_capture_of_logs(int new_level);
  18. void setup_full_capture_of_logs(int new_level);
  19. void teardown_capture_of_logs(void);
  20. int mock_saved_log_has_message(const char *msg);
  21. int mock_saved_log_has_message_containing(const char *msg);
  22. int mock_saved_log_has_message_not_containing(const char *msg);
  23. int mock_saved_log_has_severity(int severity);
  24. int mock_saved_log_has_entry(void);
  25. int mock_saved_log_n_entries(void);
  26. void mock_dump_saved_logs(void);
  27. #define assert_log_predicate(predicate, failure_msg) \
  28. do { \
  29. if (!(predicate)) { \
  30. TT_FAIL(failure_msg); \
  31. mock_dump_saved_logs(); \
  32. TT_EXIT_TEST_FUNCTION; \
  33. } \
  34. } while (0)
  35. #define expect_log_msg(str) \
  36. assert_log_predicate(mock_saved_log_has_message(str), \
  37. ("expected log to contain \"%s\"", str));
  38. #define expect_log_msg_containing(str) \
  39. assert_log_predicate(mock_saved_log_has_message_containing(str), \
  40. ("expected log to contain \"%s\"", str));
  41. #define expect_log_msg_not_containing(str) \
  42. assert_log_predicate(mock_saved_log_has_message_not_containing(str), \
  43. ("expected log to not contain \"%s\"", str));
  44. #define expect_log_msg_containing_either(str1, str2) \
  45. assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
  46. mock_saved_log_has_message_containing(str2), \
  47. ("expected log to contain \"%s\" or \"%s\"", str1, str2));
  48. #define expect_log_msg_containing_either3(str1, str2, str3) \
  49. assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
  50. mock_saved_log_has_message_containing(str2) || \
  51. mock_saved_log_has_message_containing(str3), \
  52. ("expected log to contain \"%s\" or \"%s\" or \"%s\"", \
  53. str1, str2, str3))
  54. #define expect_log_msg_containing_either4(str1, str2, str3, str4) \
  55. assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
  56. mock_saved_log_has_message_containing(str2) || \
  57. mock_saved_log_has_message_containing(str3) || \
  58. mock_saved_log_has_message_containing(str4), \
  59. ("expected log to contain \"%s\" or \"%s\" or \"%s\" or \"%s\"", \
  60. str1, str2, str3, str4))
  61. #define expect_single_log_msg(str) \
  62. do { \
  63. \
  64. assert_log_predicate(mock_saved_log_has_message_containing(str) && \
  65. mock_saved_log_n_entries() == 1, \
  66. ("expected log to contain exactly 1 message \"%s\"", \
  67. str)); \
  68. } while (0);
  69. #define expect_single_log_msg_containing(str) \
  70. do { \
  71. assert_log_predicate(mock_saved_log_has_message_containing(str)&& \
  72. mock_saved_log_n_entries() == 1 , \
  73. ("expected log to contain 1 message, containing \"%s\"",\
  74. str)); \
  75. } while (0);
  76. #define expect_no_log_msg(str) \
  77. assert_log_predicate(!mock_saved_log_has_message(str), \
  78. ("expected log to not contain \"%s\"",str));
  79. #define expect_no_log_msg_containing(str) \
  80. assert_log_predicate(!mock_saved_log_has_message_containing(str), \
  81. ("expected log to not contain \"%s\"", str));
  82. #define expect_log_severity(severity) \
  83. assert_log_predicate(mock_saved_log_has_severity(severity), \
  84. ("expected log to contain severity " # severity));
  85. #define expect_no_log_severity(severity) \
  86. assert_log_predicate(!mock_saved_log_has_severity(severity), \
  87. ("expected log to not contain severity " # severity));
  88. #define expect_log_entry() \
  89. assert_log_predicate(mock_saved_log_has_entry(), \
  90. ("expected log to contain entries"));
  91. #define expect_no_log_entry() \
  92. assert_log_predicate(!mock_saved_log_has_entry(), \
  93. ("expected log to not contain entries"));
  94. #endif /* !defined(TOR_LOG_TEST_HELPERS_H) */