test_compat_libevent.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 2010-2015, 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. #ifdef HAVE_EVENT2_EVENT_H
  9. #include <event2/event.h>
  10. #include <event2/thread.h>
  11. #ifdef USE_BUFFEREVENTS
  12. #include <event2/bufferevent.h>
  13. #endif
  14. #else
  15. #include <event.h>
  16. #endif
  17. #include "log_test_helpers.h"
  18. #define NS_MODULE compat_libevent
  19. static void
  20. test_compat_libevent_logging_callback(void *ignored)
  21. {
  22. (void)ignored;
  23. int previous_log = setup_capture_of_logs(LOG_DEBUG);
  24. libevent_logging_callback(_EVENT_LOG_DEBUG, "hello world");
  25. expect_log_msg("Message from libevent: hello world\n");
  26. expect_log_severity(LOG_DEBUG);
  27. mock_clean_saved_logs();
  28. libevent_logging_callback(_EVENT_LOG_MSG, "hello world another time");
  29. expect_log_msg("Message from libevent: hello world another time\n");
  30. expect_log_severity(LOG_INFO);
  31. mock_clean_saved_logs();
  32. libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
  33. expect_log_msg("Warning from libevent: hello world a third time\n");
  34. expect_log_severity(LOG_WARN);
  35. mock_clean_saved_logs();
  36. libevent_logging_callback(_EVENT_LOG_ERR, "hello world a fourth time");
  37. expect_log_msg("Error from libevent: hello world a fourth time\n");
  38. expect_log_severity(LOG_ERR);
  39. mock_clean_saved_logs();
  40. libevent_logging_callback(42, "hello world a fifth time");
  41. expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
  42. expect_log_severity(LOG_WARN);
  43. mock_clean_saved_logs();
  44. libevent_logging_callback(_EVENT_LOG_DEBUG,
  45. "012345678901234567890123456789"
  46. "012345678901234567890123456789"
  47. "012345678901234567890123456789"
  48. "012345678901234567890123456789"
  49. "012345678901234567890123456789"
  50. "012345678901234567890123456789"
  51. "012345678901234567890123456789"
  52. "012345678901234567890123456789"
  53. "012345678901234567890123456789"
  54. "012345678901234567890123456789"
  55. "012345678901234567890123456789"
  56. "012345678901234567890123456789"
  57. );
  58. expect_log_msg("Message from libevent: "
  59. "012345678901234567890123456789"
  60. "012345678901234567890123456789"
  61. "012345678901234567890123456789"
  62. "012345678901234567890123456789"
  63. "012345678901234567890123456789"
  64. "012345678901234567890123456789"
  65. "012345678901234567890123456789"
  66. "012345678901234567890123456789"
  67. "012345678901234567890123456789"
  68. "012345678901234567890123456789"
  69. "012345678901234567890123456789"
  70. "012345678901234567890123456789\n");
  71. expect_log_severity(LOG_DEBUG);
  72. mock_clean_saved_logs();
  73. libevent_logging_callback(42, "xxx\n");
  74. expect_log_msg("Message [42] from libevent: xxx\n");
  75. expect_log_severity(LOG_WARN);
  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. 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. // No way of verifying the result of this, it seems =/
  85. configure_libevent_logging();
  86. done:
  87. suppress_libevent_log_msg(NULL);
  88. teardown_capture_of_logs(previous_log);
  89. }
  90. static void
  91. test_compat_libevent_le_versions_compatibility(void *ignored)
  92. {
  93. (void)ignored;
  94. int res;
  95. res = le_versions_compatibility(LE_OTHER);
  96. tt_int_op(res, OP_EQ, 0);
  97. res = le_versions_compatibility(V_OLD(0,9,'c'));
  98. tt_int_op(res, OP_EQ, 1);
  99. res = le_versions_compatibility(V(1,3,98));
  100. tt_int_op(res, OP_EQ, 2);
  101. res = le_versions_compatibility(V(1,4,98));
  102. tt_int_op(res, OP_EQ, 3);
  103. res = le_versions_compatibility(V(1,5,0));
  104. tt_int_op(res, OP_EQ, 4);
  105. res = le_versions_compatibility(V(2,0,0));
  106. tt_int_op(res, OP_EQ, 4);
  107. res = le_versions_compatibility(V(2,0,2));
  108. tt_int_op(res, OP_EQ, 5);
  109. done:
  110. (void)0;
  111. }
  112. static void
  113. test_compat_libevent_tor_decode_libevent_version(void *ignored)
  114. {
  115. (void)ignored;
  116. le_version_t res;
  117. res = tor_decode_libevent_version("SOMETHING WRONG");
  118. tt_int_op(res, OP_EQ, LE_OTHER);
  119. res = tor_decode_libevent_version("1.4.11");
  120. tt_int_op(res, OP_EQ, V(1,4,11));
  121. res = tor_decode_libevent_version("1.4.12b-stable");
  122. tt_int_op(res, OP_EQ, V(1,4,12));
  123. res = tor_decode_libevent_version("1.4.17b_stable");
  124. tt_int_op(res, OP_EQ, V(1,4,17));
  125. res = tor_decode_libevent_version("1.4.12!stable");
  126. tt_int_op(res, OP_EQ, LE_OTHER);
  127. res = tor_decode_libevent_version("1.4.12b!stable");
  128. tt_int_op(res, OP_EQ, LE_OTHER);
  129. res = tor_decode_libevent_version("1.4.13-");
  130. tt_int_op(res, OP_EQ, V(1,4,13));
  131. res = tor_decode_libevent_version("1.4.14_");
  132. tt_int_op(res, OP_EQ, V(1,4,14));
  133. res = tor_decode_libevent_version("1.4.15c-");
  134. tt_int_op(res, OP_EQ, V(1,4,15));
  135. res = tor_decode_libevent_version("1.4.16c_");
  136. tt_int_op(res, OP_EQ, V(1,4,16));
  137. res = tor_decode_libevent_version("1.4.17-s");
  138. tt_int_op(res, OP_EQ, V(1,4,17));
  139. res = tor_decode_libevent_version("1.5");
  140. tt_int_op(res, OP_EQ, V(1,5,0));
  141. res = tor_decode_libevent_version("1.2");
  142. tt_int_op(res, OP_EQ, V(1,2,0));
  143. res = tor_decode_libevent_version("1.2-");
  144. tt_int_op(res, OP_EQ, LE_OTHER);
  145. res = tor_decode_libevent_version("1.6e");
  146. tt_int_op(res, OP_EQ, V_OLD(1,6,'e'));
  147. done:
  148. (void)0;
  149. }
  150. #if defined(LIBEVENT_VERSION)
  151. #define HEADER_VERSION LIBEVENT_VERSION
  152. #elif defined(_EVENT_VERSION)
  153. #define HEADER_VERSION _EVENT_VERSION
  154. #endif
  155. static void
  156. test_compat_libevent_header_version(void *ignored)
  157. {
  158. (void)ignored;
  159. const char *res;
  160. res = tor_libevent_get_header_version_str();
  161. tt_str_op(res, OP_EQ, HEADER_VERSION);
  162. done:
  163. (void)0;
  164. }
  165. struct testcase_t compat_libevent_tests[] = {
  166. { "logging_callback", test_compat_libevent_logging_callback,
  167. TT_FORK, NULL, NULL },
  168. { "le_versions_compatibility",
  169. test_compat_libevent_le_versions_compatibility, 0, NULL, NULL },
  170. { "tor_decode_libevent_version",
  171. test_compat_libevent_tor_decode_libevent_version, 0, NULL, NULL },
  172. { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
  173. END_OF_TESTCASES
  174. };