log_test_helpers.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright (c) 2014-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "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_severity(int severity);
  23. int mock_saved_log_has_entry(void);
  24. int mock_saved_log_n_entries(void);
  25. void mock_dump_saved_logs(void);
  26. #define assert_log_predicate(predicate, failure_msg) \
  27. do { \
  28. if (!(predicate)) { \
  29. tt_fail_msg((failure_msg)); \
  30. mock_dump_saved_logs(); \
  31. TT_EXIT_TEST_FUNCTION; \
  32. } \
  33. } while (0)
  34. #define expect_log_msg(str) \
  35. assert_log_predicate(mock_saved_log_has_message(str), \
  36. "expected log to contain " # str);
  37. #define expect_log_msg_containing(str) \
  38. assert_log_predicate(mock_saved_log_has_message_containing(str), \
  39. "expected log to contain " # str);
  40. #define expect_log_msg_containing_either(str1, str2) \
  41. assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
  42. mock_saved_log_has_message_containing(str2), \
  43. "expected log to contain " # str1 " or " # str2);
  44. #define expect_log_msg_containing_either3(str1, str2, str3) \
  45. assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
  46. mock_saved_log_has_message_containing(str2) || \
  47. mock_saved_log_has_message_containing(str3), \
  48. "expected log to contain " # str1 " or " # str2 \
  49. " or " # str3);
  50. #define expect_log_msg_containing_either4(str1, str2, str3, str4) \
  51. assert_log_predicate(mock_saved_log_has_message_containing(str1) || \
  52. mock_saved_log_has_message_containing(str2) || \
  53. mock_saved_log_has_message_containing(str3) || \
  54. mock_saved_log_has_message_containing(str4), \
  55. "expected log to contain " # str1 " or " # str2 \
  56. " or " # str3 " or " # str4);
  57. #define expect_single_log_msg(str) \
  58. do { \
  59. \
  60. assert_log_predicate(mock_saved_log_has_message_containing(str) && \
  61. mock_saved_log_n_entries() == 1, \
  62. "expected log to contain exactly 1 message " # str); \
  63. } while (0);
  64. #define expect_single_log_msg_containing(str) \
  65. do { \
  66. assert_log_predicate(mock_saved_log_has_message_containing(str)&& \
  67. mock_saved_log_n_entries() == 1 , \
  68. "expected log to contain 1 message, containing " # str); \
  69. } while (0);
  70. #define expect_no_log_msg(str) \
  71. assert_log_predicate(!mock_saved_log_has_message(str), \
  72. "expected log to not contain " # str);
  73. #define expect_no_log_msg_containing(str) \
  74. assert_log_predicate(!mock_saved_log_has_message_containing(str), \
  75. "expected log to not contain " # str);
  76. #define expect_log_severity(severity) \
  77. assert_log_predicate(mock_saved_log_has_severity(severity), \
  78. "expected log to contain severity " # severity);
  79. #define expect_no_log_severity(severity) \
  80. assert_log_predicate(!mock_saved_log_has_severity(severity), \
  81. "expected log to not contain severity " # severity);
  82. #define expect_log_entry() \
  83. assert_log_predicate(mock_saved_log_has_entry(), \
  84. "expected log to contain entries");
  85. #define expect_no_log_entry() \
  86. assert_log_predicate(!mock_saved_log_has_entry(), \
  87. "expected log to not contain entries");
  88. #endif /* !defined(TOR_LOG_TEST_HELPERS_H) */