test_compat_libevent.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (c) 2010-2017, 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. setup_full_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. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  21. mock_clean_saved_logs();
  22. libevent_logging_callback(_EVENT_LOG_MSG, "hello world another time");
  23. expect_log_msg("Message from libevent: hello world another time\n");
  24. expect_log_severity(LOG_INFO);
  25. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  26. mock_clean_saved_logs();
  27. libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
  28. expect_log_msg("Warning from libevent: hello world a third time\n");
  29. expect_log_severity(LOG_WARN);
  30. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  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. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  36. mock_clean_saved_logs();
  37. libevent_logging_callback(42, "hello world a fifth time");
  38. expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
  39. expect_log_severity(LOG_WARN);
  40. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  41. mock_clean_saved_logs();
  42. libevent_logging_callback(_EVENT_LOG_DEBUG,
  43. "012345678901234567890123456789"
  44. "012345678901234567890123456789"
  45. "012345678901234567890123456789"
  46. "012345678901234567890123456789"
  47. "012345678901234567890123456789"
  48. "012345678901234567890123456789"
  49. "012345678901234567890123456789"
  50. "012345678901234567890123456789"
  51. "012345678901234567890123456789"
  52. "012345678901234567890123456789"
  53. "012345678901234567890123456789"
  54. "012345678901234567890123456789"
  55. );
  56. expect_log_msg("Message from libevent: "
  57. "012345678901234567890123456789"
  58. "012345678901234567890123456789"
  59. "012345678901234567890123456789"
  60. "012345678901234567890123456789"
  61. "012345678901234567890123456789"
  62. "012345678901234567890123456789"
  63. "012345678901234567890123456789"
  64. "012345678901234567890123456789"
  65. "012345678901234567890123456789"
  66. "012345678901234567890123456789"
  67. "012345678901234567890123456789"
  68. "012345678901234567890123456789\n");
  69. expect_log_severity(LOG_DEBUG);
  70. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  71. mock_clean_saved_logs();
  72. libevent_logging_callback(42, "xxx\n");
  73. expect_log_msg("Message [42] from libevent: xxx\n");
  74. expect_log_severity(LOG_WARN);
  75. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  76. suppress_libevent_log_msg("something");
  77. mock_clean_saved_logs();
  78. libevent_logging_callback(_EVENT_LOG_MSG, "hello there");
  79. expect_log_msg("Message from libevent: hello there\n");
  80. expect_log_severity(LOG_INFO);
  81. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
  82. mock_clean_saved_logs();
  83. libevent_logging_callback(_EVENT_LOG_MSG, "hello there something else");
  84. expect_no_log_msg("hello there something else");
  85. if (mock_saved_logs())
  86. tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 0);
  87. // No way of verifying the result of this, it seems =/
  88. configure_libevent_logging();
  89. done:
  90. suppress_libevent_log_msg(NULL);
  91. teardown_capture_of_logs();
  92. }
  93. static void
  94. test_compat_libevent_header_version(void *ignored)
  95. {
  96. (void)ignored;
  97. const char *res;
  98. res = tor_libevent_get_header_version_str();
  99. tt_str_op(res, OP_EQ, LIBEVENT_VERSION);
  100. done:
  101. (void)0;
  102. }
  103. struct testcase_t compat_libevent_tests[] = {
  104. { "logging_callback", test_compat_libevent_logging_callback,
  105. TT_FORK, NULL, NULL },
  106. { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
  107. END_OF_TESTCASES
  108. };