log_test_helpers.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (c) 2015-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define LOG_PRIVATE
  4. #include "torlog.h"
  5. #include "log_test_helpers.h"
  6. static smartlist_t *saved_logs = NULL;
  7. int
  8. setup_capture_of_logs(int new_level)
  9. {
  10. int previous_log = log_global_min_severity_;
  11. log_global_min_severity_ = new_level;
  12. mock_clean_saved_logs();
  13. saved_logs = smartlist_new();
  14. MOCK(logv, mock_saving_logv);
  15. return previous_log;
  16. }
  17. void
  18. teardown_capture_of_logs(int prev)
  19. {
  20. UNMOCK(logv);
  21. log_global_min_severity_ = prev;
  22. mock_clean_saved_logs();
  23. }
  24. void
  25. mock_clean_saved_logs(void)
  26. {
  27. if (!saved_logs)
  28. return;
  29. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  30. { tor_free(m->generated_msg); tor_free(m); });
  31. smartlist_free(saved_logs);
  32. saved_logs = NULL;
  33. }
  34. const smartlist_t *
  35. mock_saved_logs(void)
  36. {
  37. return saved_logs;
  38. }
  39. int
  40. mock_saved_log_has_message(const char *msg)
  41. {
  42. int has_msg = 0;
  43. if (saved_logs) {
  44. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  45. {
  46. if (msg && m->generated_msg &&
  47. !strcmp(msg, m->generated_msg)) {
  48. has_msg = 1;
  49. }
  50. });
  51. }
  52. return has_msg;
  53. }
  54. int
  55. mock_saved_log_has_message_containing(const char *msg)
  56. {
  57. if (saved_logs) {
  58. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  59. {
  60. if (msg && m->generated_msg &&
  61. strstr(m->generated_msg, msg)) {
  62. return 1;
  63. }
  64. });
  65. }
  66. return 0;
  67. }
  68. /* Do the saved logs have any messages with severity? */
  69. int
  70. mock_saved_log_has_severity(int severity)
  71. {
  72. int has_sev = 0;
  73. if (saved_logs) {
  74. SMARTLIST_FOREACH(saved_logs, mock_saved_log_entry_t *, m,
  75. {
  76. if (m->severity == severity) {
  77. has_sev = 1;
  78. }
  79. });
  80. }
  81. return has_sev;
  82. }
  83. /* Do the saved logs have any messages? */
  84. int
  85. mock_saved_log_has_entry(void)
  86. {
  87. if (saved_logs) {
  88. return smartlist_len(saved_logs) > 0;
  89. }
  90. return 0;
  91. }
  92. void
  93. mock_saving_logv(int severity, log_domain_mask_t domain,
  94. const char *funcname, const char *suffix,
  95. const char *format, va_list ap)
  96. {
  97. (void)domain;
  98. char *buf = tor_malloc_zero(10240);
  99. int n;
  100. n = tor_vsnprintf(buf,10240,format,ap);
  101. tor_assert(n < 10240-1);
  102. buf[n]='\n';
  103. buf[n+1]='\0';
  104. mock_saved_log_entry_t *e = tor_malloc_zero(sizeof(mock_saved_log_entry_t));
  105. e->severity = severity;
  106. e->funcname = funcname;
  107. e->suffix = suffix;
  108. e->format = format;
  109. e->generated_msg = tor_strdup(buf);
  110. tor_free(buf);
  111. if (!saved_logs)
  112. saved_logs = smartlist_new();
  113. smartlist_add(saved_logs, e);
  114. }