test_compat_libevent.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "log_test_helpers.h"
  11. #define NS_MODULE compat_libevent
  12. static void
  13. test_compat_libevent_logging_callback(void *ignored)
  14. {
  15. (void)ignored;
  16. int previous_log = setup_capture_of_logs(LOG_DEBUG);
  17. libevent_logging_callback(_EVENT_LOG_DEBUG, "hello world");
  18. expect_log_msg("Message from libevent: hello world\n");
  19. expect_log_severity(LOG_DEBUG);
  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. mock_clean_saved_logs();
  25. libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
  26. expect_log_msg("Warning from libevent: hello world a third time\n");
  27. expect_log_severity(LOG_WARN);
  28. mock_clean_saved_logs();
  29. libevent_logging_callback(_EVENT_LOG_ERR, "hello world a fourth time");
  30. expect_log_msg("Error from libevent: hello world a fourth time\n");
  31. expect_log_severity(LOG_ERR);
  32. mock_clean_saved_logs();
  33. libevent_logging_callback(42, "hello world a fifth time");
  34. expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
  35. expect_log_severity(LOG_WARN);
  36. mock_clean_saved_logs();
  37. libevent_logging_callback(_EVENT_LOG_DEBUG,
  38. "012345678901234567890123456789"
  39. "012345678901234567890123456789"
  40. "012345678901234567890123456789"
  41. "012345678901234567890123456789"
  42. "012345678901234567890123456789"
  43. "012345678901234567890123456789"
  44. "012345678901234567890123456789"
  45. "012345678901234567890123456789"
  46. "012345678901234567890123456789"
  47. "012345678901234567890123456789"
  48. "012345678901234567890123456789"
  49. "012345678901234567890123456789"
  50. );
  51. expect_log_msg("Message from libevent: "
  52. "012345678901234567890123456789"
  53. "012345678901234567890123456789"
  54. "012345678901234567890123456789"
  55. "012345678901234567890123456789"
  56. "012345678901234567890123456789"
  57. "012345678901234567890123456789"
  58. "012345678901234567890123456789"
  59. "012345678901234567890123456789"
  60. "012345678901234567890123456789"
  61. "012345678901234567890123456789"
  62. "012345678901234567890123456789"
  63. "012345678901234567890123456789\n");
  64. expect_log_severity(LOG_DEBUG);
  65. mock_clean_saved_logs();
  66. libevent_logging_callback(42, "xxx\n");
  67. expect_log_msg("Message [42] from libevent: xxx\n");
  68. expect_log_severity(LOG_WARN);
  69. suppress_libevent_log_msg("something");
  70. mock_clean_saved_logs();
  71. libevent_logging_callback(_EVENT_LOG_MSG, "hello there");
  72. expect_log_msg("Message from libevent: hello there\n");
  73. expect_log_severity(LOG_INFO);
  74. mock_clean_saved_logs();
  75. libevent_logging_callback(_EVENT_LOG_MSG, "hello there something else");
  76. expect_no_log_msg("hello there something else");
  77. // No way of verifying the result of this, it seems =/
  78. configure_libevent_logging();
  79. done:
  80. suppress_libevent_log_msg(NULL);
  81. teardown_capture_of_logs(previous_log);
  82. }
  83. static void
  84. test_compat_libevent_header_version(void *ignored)
  85. {
  86. (void)ignored;
  87. const char *res;
  88. res = tor_libevent_get_header_version_str();
  89. tt_str_op(res, OP_EQ, LIBEVENT_VERSION);
  90. done:
  91. (void)0;
  92. }
  93. struct testcase_t compat_libevent_tests[] = {
  94. { "logging_callback", test_compat_libevent_logging_callback,
  95. TT_FORK, NULL, NULL },
  96. { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
  97. END_OF_TESTCASES
  98. };