test_compat_libevent.c 7.5 KB

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