test_compat_libevent.c 8.3 KB

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