test_compat_libevent.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (c) 2010-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define COMPAT_LIBEVENT_PRIVATE
  4. #include "orconfig.h"
  5. #include "or/or.h"
  6. #include "test/test.h"
  7. #include "common/compat_libevent.h"
  8. #include <event2/event.h>
  9. #include "test/log_test_helpers.h"
  10. #define NS_MODULE compat_libevent
  11. static void
  12. test_compat_libevent_logging_callback(void *ignored)
  13. {
  14. (void)ignored;
  15. setup_full_capture_of_logs(LOG_DEBUG);
  16. libevent_logging_callback(_EVENT_LOG_DEBUG, "hello world");
  17. expect_log_msg("Message from libevent: hello world\n");
  18. expect_log_severity(LOG_DEBUG);
  19. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  20. mock_clean_saved_logs();
  21. libevent_logging_callback(_EVENT_LOG_MSG, "hello world another time");
  22. expect_log_msg("Message from libevent: hello world another time\n");
  23. expect_log_severity(LOG_INFO);
  24. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  25. mock_clean_saved_logs();
  26. libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
  27. expect_log_msg("Warning from libevent: hello world a third time\n");
  28. expect_log_severity(LOG_WARN);
  29. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  30. mock_clean_saved_logs();
  31. libevent_logging_callback(_EVENT_LOG_ERR, "hello world a fourth time");
  32. expect_log_msg("Error from libevent: hello world a fourth time\n");
  33. expect_log_severity(LOG_ERR);
  34. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  35. mock_clean_saved_logs();
  36. libevent_logging_callback(42, "hello world a fifth time");
  37. expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
  38. expect_log_severity(LOG_WARN);
  39. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  40. mock_clean_saved_logs();
  41. libevent_logging_callback(_EVENT_LOG_DEBUG,
  42. "012345678901234567890123456789"
  43. "012345678901234567890123456789"
  44. "012345678901234567890123456789"
  45. "012345678901234567890123456789"
  46. "012345678901234567890123456789"
  47. "012345678901234567890123456789"
  48. "012345678901234567890123456789"
  49. "012345678901234567890123456789"
  50. "012345678901234567890123456789"
  51. "012345678901234567890123456789"
  52. "012345678901234567890123456789"
  53. "012345678901234567890123456789"
  54. );
  55. expect_log_msg("Message from libevent: "
  56. "012345678901234567890123456789"
  57. "012345678901234567890123456789"
  58. "012345678901234567890123456789"
  59. "012345678901234567890123456789"
  60. "012345678901234567890123456789"
  61. "012345678901234567890123456789"
  62. "012345678901234567890123456789"
  63. "012345678901234567890123456789"
  64. "012345678901234567890123456789"
  65. "012345678901234567890123456789"
  66. "012345678901234567890123456789"
  67. "012345678901234567890123456789\n");
  68. expect_log_severity(LOG_DEBUG);
  69. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  70. mock_clean_saved_logs();
  71. libevent_logging_callback(42, "xxx\n");
  72. expect_log_msg("Message [42] from libevent: xxx\n");
  73. expect_log_severity(LOG_WARN);
  74. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  75. suppress_libevent_log_msg("something");
  76. mock_clean_saved_logs();
  77. libevent_logging_callback(_EVENT_LOG_MSG, "hello there");
  78. expect_log_msg("Message from libevent: hello there\n");
  79. expect_log_severity(LOG_INFO);
  80. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  81. mock_clean_saved_logs();
  82. libevent_logging_callback(_EVENT_LOG_MSG, "hello there something else");
  83. expect_no_log_msg("hello there something else");
  84. if (mock_saved_logs())
  85. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 0);
  86. // No way of verifying the result of this, it seems =/
  87. configure_libevent_logging();
  88. done:
  89. suppress_libevent_log_msg(NULL);
  90. teardown_capture_of_logs();
  91. }
  92. static void
  93. test_compat_libevent_header_version(void *ignored)
  94. {
  95. (void)ignored;
  96. const char *res;
  97. res = tor_libevent_get_header_version_str();
  98. tt_str_op(res, OP_EQ, LIBEVENT_VERSION);
  99. done:
  100. (void)0;
  101. }
  102. /* Test for postloop events */
  103. /* Event callback to increment a counter. */
  104. static void
  105. increment_int_counter_cb(periodic_timer_t *timer, void *arg)
  106. {
  107. (void)timer;
  108. int *ctr = arg;
  109. ++*ctr;
  110. }
  111. static int activated_counter = 0;
  112. /* Mainloop event callback to activate another mainloop event */
  113. static void
  114. activate_event_cb(mainloop_event_t *ev, void *arg)
  115. {
  116. (void)ev;
  117. mainloop_event_t **other_event = arg;
  118. mainloop_event_activate(*other_event);
  119. ++activated_counter;
  120. }
  121. static void
  122. test_compat_libevent_postloop_events(void *arg)
  123. {
  124. (void)arg;
  125. mainloop_event_t *a = NULL, *b = NULL;
  126. periodic_timer_t *timed = NULL;
  127. tor_libevent_postfork();
  128. /* If postloop events don't work, then these events will activate one
  129. * another ad infinitum and, and the periodic event will never occur. */
  130. b = mainloop_event_postloop_new(activate_event_cb, &a);
  131. a = mainloop_event_postloop_new(activate_event_cb, &b);
  132. int counter = 0;
  133. struct timeval fifty_ms = { 0, 10 * 1000 };
  134. timed = periodic_timer_new(tor_libevent_get_base(), &fifty_ms,
  135. increment_int_counter_cb, &counter);
  136. mainloop_event_activate(a);
  137. int r;
  138. do {
  139. r = tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
  140. if (r == -1)
  141. break;
  142. } while (counter < 5);
  143. tt_int_op(activated_counter, OP_GE, 2);
  144. done:
  145. mainloop_event_free(a);
  146. mainloop_event_free(b);
  147. periodic_timer_free(timed);
  148. }
  149. struct testcase_t compat_libevent_tests[] = {
  150. { "logging_callback", test_compat_libevent_logging_callback,
  151. TT_FORK, NULL, NULL },
  152. { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
  153. { "postloop_events", test_compat_libevent_postloop_events,
  154. TT_FORK, NULL, NULL },
  155. END_OF_TESTCASES
  156. };