test_compat_libevent.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "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. struct testcase_t compat_libevent_tests[] = {
  103. { "logging_callback", test_compat_libevent_logging_callback,
  104. TT_FORK, NULL, NULL },
  105. { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
  106. END_OF_TESTCASES
  107. };