test_compat_libevent.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright (c) 2010-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define COMPAT_LIBEVENT_PRIVATE
  4. #include "orconfig.h"
  5. #include "or.h"
  6. #include "test.h"
  7. #include "compat_libevent.h"
  8. #include <event2/event.h>
  9. #include <event2/thread.h>
  10. #ifdef USE_BUFFEREVENTS
  11. #include <event2/bufferevent.h>
  12. #endif
  13. #include "log_test_helpers.h"
  14. #define NS_MODULE compat_libevent
  15. static void
  16. test_compat_libevent_logging_callback(void *ignored)
  17. {
  18. (void)ignored;
  19. int previous_log = setup_capture_of_logs(LOG_DEBUG);
  20. libevent_logging_callback(_EVENT_LOG_DEBUG, "hello world");
  21. expect_log_msg("Message from libevent: hello world\n");
  22. expect_log_severity(LOG_DEBUG);
  23. mock_clean_saved_logs();
  24. libevent_logging_callback(_EVENT_LOG_MSG, "hello world another time");
  25. expect_log_msg("Message from libevent: hello world another time\n");
  26. expect_log_severity(LOG_INFO);
  27. mock_clean_saved_logs();
  28. libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
  29. expect_log_msg("Warning from libevent: hello world a third time\n");
  30. expect_log_severity(LOG_WARN);
  31. mock_clean_saved_logs();
  32. libevent_logging_callback(_EVENT_LOG_ERR, "hello world a fourth time");
  33. expect_log_msg("Error from libevent: hello world a fourth time\n");
  34. expect_log_severity(LOG_ERR);
  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. mock_clean_saved_logs();
  40. libevent_logging_callback(_EVENT_LOG_DEBUG,
  41. "012345678901234567890123456789"
  42. "012345678901234567890123456789"
  43. "012345678901234567890123456789"
  44. "012345678901234567890123456789"
  45. "012345678901234567890123456789"
  46. "012345678901234567890123456789"
  47. "012345678901234567890123456789"
  48. "012345678901234567890123456789"
  49. "012345678901234567890123456789"
  50. "012345678901234567890123456789"
  51. "012345678901234567890123456789"
  52. "012345678901234567890123456789"
  53. );
  54. expect_log_msg("Message from libevent: "
  55. "012345678901234567890123456789"
  56. "012345678901234567890123456789"
  57. "012345678901234567890123456789"
  58. "012345678901234567890123456789"
  59. "012345678901234567890123456789"
  60. "012345678901234567890123456789"
  61. "012345678901234567890123456789"
  62. "012345678901234567890123456789"
  63. "012345678901234567890123456789"
  64. "012345678901234567890123456789"
  65. "012345678901234567890123456789"
  66. "012345678901234567890123456789\n");
  67. expect_log_severity(LOG_DEBUG);
  68. mock_clean_saved_logs();
  69. libevent_logging_callback(42, "xxx\n");
  70. expect_log_msg("Message [42] from libevent: xxx\n");
  71. expect_log_severity(LOG_WARN);
  72. suppress_libevent_log_msg("something");
  73. mock_clean_saved_logs();
  74. libevent_logging_callback(_EVENT_LOG_MSG, "hello there");
  75. expect_log_msg("Message from libevent: hello there\n");
  76. expect_log_severity(LOG_INFO);
  77. mock_clean_saved_logs();
  78. libevent_logging_callback(_EVENT_LOG_MSG, "hello there something else");
  79. expect_no_log_msg("hello there something else");
  80. // No way of verifying the result of this, it seems =/
  81. configure_libevent_logging();
  82. done:
  83. suppress_libevent_log_msg(NULL);
  84. teardown_capture_of_logs(previous_log);
  85. }
  86. static void
  87. test_compat_libevent_header_version(void *ignored)
  88. {
  89. (void)ignored;
  90. const char *res;
  91. res = tor_libevent_get_header_version_str();
  92. tt_str_op(res, OP_EQ, LIBEVENT_VERSION);
  93. done:
  94. (void)0;
  95. }
  96. struct testcase_t compat_libevent_tests[] = {
  97. { "logging_callback", test_compat_libevent_logging_callback,
  98. TT_FORK, NULL, NULL },
  99. { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
  100. END_OF_TESTCASES
  101. };