test_periodic_event.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_periodic_event.c
  5. * \brief Test the periodic events that Tor uses for different roles. They are
  6. * part of the libevent mainloop
  7. */
  8. #define CONFIG_PRIVATE
  9. #define HS_SERVICE_PRIVATE
  10. #define MAINLOOP_PRIVATE
  11. #include "test/test.h"
  12. #include "test/test_helpers.h"
  13. #include "core/or/or.h"
  14. #include "app/config/config.h"
  15. #include "feature/hibernate/hibernate.h"
  16. #include "feature/hs/hs_service.h"
  17. #include "core/mainloop/mainloop.h"
  18. #include "core/mainloop/periodic.h"
  19. /** Helper function: This is replaced in some tests for the event callbacks so
  20. * we don't actually go into the code path of those callbacks. */
  21. static int
  22. dumb_event_fn(time_t now, const or_options_t *options)
  23. {
  24. (void) now;
  25. (void) options;
  26. /* Will get rescheduled in 300 seconds. It just can't be 0. */
  27. return 300;
  28. }
  29. static void
  30. register_dummy_hidden_service(hs_service_t *service)
  31. {
  32. memset(service, 0, sizeof(hs_service_t));
  33. memset(&service->keys.identity_pk, 'A', sizeof(service->keys.identity_pk));
  34. (void) register_service(get_hs_service_map(), service);
  35. }
  36. static void
  37. test_pe_initialize(void *arg)
  38. {
  39. (void) arg;
  40. /* Initialize the events but the callback won't get called since we would
  41. * need to run the main loop and then wait for a second delaying the unit
  42. * tests. Instead, we'll test the callback work indepedently elsewhere. */
  43. initialize_periodic_events();
  44. /* Validate that all events have been set up. */
  45. for (int i = 0; periodic_events[i].name; ++i) {
  46. periodic_event_item_t *item = &periodic_events[i];
  47. tt_assert(item->ev);
  48. tt_assert(item->fn);
  49. tt_u64_op(item->last_action_time, OP_EQ, 0);
  50. /* Every event must have role(s) assign to it. This is done statically. */
  51. tt_u64_op(item->roles, OP_NE, 0);
  52. tt_uint_op(periodic_event_is_enabled(item), OP_EQ, 0);
  53. }
  54. done:
  55. teardown_periodic_events();
  56. }
  57. static void
  58. test_pe_launch(void *arg)
  59. {
  60. hs_service_t service, *to_remove = NULL;
  61. or_options_t *options;
  62. (void) arg;
  63. hs_init();
  64. /* We need to put tor in hibernation live state so the events requiring
  65. * network gets enabled. */
  66. consider_hibernation(time(NULL));
  67. /* Hack: We'll set a dumb fn() of each events so they don't get called when
  68. * dispatching them. We just want to test the state of the callbacks, not
  69. * the whole code path. */
  70. for (int i = 0; periodic_events[i].name; ++i) {
  71. periodic_event_item_t *item = &periodic_events[i];
  72. item->fn = dumb_event_fn;
  73. }
  74. options = get_options_mutable();
  75. options->SocksPort_set = 1;
  76. periodic_events_on_new_options(options);
  77. #if 0
  78. /* Lets make sure that before intialization, we can't scan the periodic
  79. * events list and launch them. Lets try by being a Client. */
  80. /* XXXX We make sure these events are initialized now way earlier than we
  81. * did before. */
  82. for (int i = 0; periodic_events[i].name; ++i) {
  83. periodic_event_item_t *item = &periodic_events[i];
  84. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 0);
  85. }
  86. #endif
  87. initialize_periodic_events();
  88. /* Now that we've initialized, rescan the list to launch. */
  89. periodic_events_on_new_options(options);
  90. for (int i = 0; periodic_events[i].name; ++i) {
  91. periodic_event_item_t *item = &periodic_events[i];
  92. if (item->roles & PERIODIC_EVENT_ROLE_CLIENT) {
  93. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 1);
  94. } else {
  95. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 0);
  96. }
  97. // enabled or not, the event has not yet been run.
  98. tt_u64_op(item->last_action_time, OP_EQ, 0);
  99. }
  100. /* Remove Client but become a Relay. */
  101. options->SocksPort_set = 0;
  102. options->ORPort_set = 1;
  103. periodic_events_on_new_options(options);
  104. unsigned roles = get_my_roles(options);
  105. tt_uint_op(roles, OP_EQ,
  106. PERIODIC_EVENT_ROLE_RELAY|PERIODIC_EVENT_ROLE_DIRSERVER);
  107. for (int i = 0; periodic_events[i].name; ++i) {
  108. periodic_event_item_t *item = &periodic_events[i];
  109. /* Only Client role should be disabled. */
  110. if (item->roles == PERIODIC_EVENT_ROLE_CLIENT) {
  111. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 0);
  112. }
  113. if (item->roles & PERIODIC_EVENT_ROLE_RELAY) {
  114. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 1);
  115. }
  116. /* Non Relay role should be disabled, except for Dirserver. */
  117. if (!(item->roles & roles)) {
  118. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 0);
  119. }
  120. }
  121. /* Disable everything and we'll enable them ALL. */
  122. options->SocksPort_set = 0;
  123. options->ORPort_set = 0;
  124. periodic_events_on_new_options(options);
  125. for (int i = 0; periodic_events[i].name; ++i) {
  126. periodic_event_item_t *item = &periodic_events[i];
  127. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 0);
  128. }
  129. /* Enable everything. */
  130. options->SocksPort_set = 1; options->ORPort_set = 1;
  131. options->BridgeRelay = 1; options->AuthoritativeDir = 1;
  132. options->V3AuthoritativeDir = 1; options->BridgeAuthoritativeDir = 1;
  133. register_dummy_hidden_service(&service);
  134. periodic_events_on_new_options(options);
  135. /* Note down the reference because we need to remove this service from the
  136. * global list before the hs_free_all() call so it doesn't try to free
  137. * memory on the stack. Furthermore, we can't remove it now else it will
  138. * trigger a rescan of the event disabling the HS service event. */
  139. to_remove = &service;
  140. for (int i = 0; periodic_events[i].name; ++i) {
  141. periodic_event_item_t *item = &periodic_events[i];
  142. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 1);
  143. }
  144. done:
  145. if (to_remove) {
  146. remove_service(get_hs_service_map(), to_remove);
  147. }
  148. hs_free_all();
  149. }
  150. static void
  151. test_pe_get_roles(void *arg)
  152. {
  153. int roles;
  154. (void) arg;
  155. /* Just so the HS global map exists. */
  156. hs_init();
  157. or_options_t *options = get_options_mutable();
  158. tt_assert(options);
  159. /* Nothing configured, should be no roles. */
  160. roles = get_my_roles(options);
  161. tt_int_op(roles, OP_EQ, 0);
  162. /* Indicate we have a SocksPort, roles should be come Client. */
  163. options->SocksPort_set = 1;
  164. roles = get_my_roles(options);
  165. tt_int_op(roles, OP_EQ, PERIODIC_EVENT_ROLE_CLIENT);
  166. /* Now, we'll add a ORPort so should now be a Relay + Client. */
  167. options->ORPort_set = 1;
  168. roles = get_my_roles(options);
  169. tt_int_op(roles, OP_EQ,
  170. (PERIODIC_EVENT_ROLE_CLIENT | PERIODIC_EVENT_ROLE_RELAY |
  171. PERIODIC_EVENT_ROLE_DIRSERVER));
  172. /* Now add a Bridge. */
  173. options->BridgeRelay = 1;
  174. roles = get_my_roles(options);
  175. tt_int_op(roles, OP_EQ,
  176. (PERIODIC_EVENT_ROLE_CLIENT | PERIODIC_EVENT_ROLE_RELAY |
  177. PERIODIC_EVENT_ROLE_BRIDGE | PERIODIC_EVENT_ROLE_DIRSERVER));
  178. tt_assert(roles & PERIODIC_EVENT_ROLE_ROUTER);
  179. /* Unset client so we can solely test Router role. */
  180. options->SocksPort_set = 0;
  181. roles = get_my_roles(options);
  182. tt_int_op(roles, OP_EQ,
  183. PERIODIC_EVENT_ROLE_ROUTER | PERIODIC_EVENT_ROLE_DIRSERVER);
  184. /* Reset options so we can test authorities. */
  185. options->SocksPort_set = 0;
  186. options->ORPort_set = 0;
  187. options->BridgeRelay = 0;
  188. roles = get_my_roles(options);
  189. tt_int_op(roles, OP_EQ, 0);
  190. /* Now upgrade to Dirauth. */
  191. options->DirPort_set = 1;
  192. options->AuthoritativeDir = 1;
  193. options->V3AuthoritativeDir = 1;
  194. roles = get_my_roles(options);
  195. tt_int_op(roles, OP_EQ,
  196. PERIODIC_EVENT_ROLE_DIRAUTH|PERIODIC_EVENT_ROLE_DIRSERVER);
  197. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  198. /* Now Bridge Authority. */
  199. options->V3AuthoritativeDir = 0;
  200. options->BridgeAuthoritativeDir = 1;
  201. roles = get_my_roles(options);
  202. tt_int_op(roles, OP_EQ,
  203. PERIODIC_EVENT_ROLE_BRIDGEAUTH|PERIODIC_EVENT_ROLE_DIRSERVER);
  204. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  205. /* Move that bridge auth to become a relay. */
  206. options->ORPort_set = 1;
  207. roles = get_my_roles(options);
  208. tt_int_op(roles, OP_EQ,
  209. (PERIODIC_EVENT_ROLE_BRIDGEAUTH | PERIODIC_EVENT_ROLE_RELAY
  210. | PERIODIC_EVENT_ROLE_DIRSERVER));
  211. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  212. /* And now an Hidden service. */
  213. hs_service_t service;
  214. register_dummy_hidden_service(&service);
  215. roles = get_my_roles(options);
  216. /* Remove it now so the hs_free_all() doesn't try to free stack memory. */
  217. remove_service(get_hs_service_map(), &service);
  218. tt_int_op(roles, OP_EQ,
  219. (PERIODIC_EVENT_ROLE_BRIDGEAUTH | PERIODIC_EVENT_ROLE_RELAY |
  220. PERIODIC_EVENT_ROLE_HS_SERVICE | PERIODIC_EVENT_ROLE_DIRSERVER));
  221. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  222. done:
  223. hs_free_all();
  224. }
  225. static void
  226. test_pe_hs_service(void *arg)
  227. {
  228. hs_service_t service, *to_remove = NULL;
  229. (void) arg;
  230. hs_init();
  231. /* We need to put tor in hibernation live state so the events requiring
  232. * network gets enabled. */
  233. consider_hibernation(time(NULL));
  234. /* Initialize the events so we can enable them */
  235. initialize_periodic_events();
  236. /* Hack: We'll set a dumb fn() of each events so they don't get called when
  237. * dispatching them. We just want to test the state of the callbacks, not
  238. * the whole code path. */
  239. for (int i = 0; periodic_events[i].name; ++i) {
  240. periodic_event_item_t *item = &periodic_events[i];
  241. item->fn = dumb_event_fn;
  242. }
  243. /* This should trigger a rescan of the list and enable the HS service
  244. * events. */
  245. register_dummy_hidden_service(&service);
  246. /* Note down the reference because we need to remove this service from the
  247. * global list before the hs_free_all() call so it doesn't try to free
  248. * memory on the stack. Furthermore, we can't remove it now else it will
  249. * trigger a rescan of the event disabling the HS service event. */
  250. to_remove = &service;
  251. for (int i = 0; periodic_events[i].name; ++i) {
  252. periodic_event_item_t *item = &periodic_events[i];
  253. if (item->roles & PERIODIC_EVENT_ROLE_HS_SERVICE) {
  254. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 1);
  255. }
  256. }
  257. to_remove = NULL;
  258. /* Remove the service from the global map, it should trigger a rescan and
  259. * disable the HS service events. */
  260. remove_service(get_hs_service_map(), &service);
  261. for (int i = 0; periodic_events[i].name; ++i) {
  262. periodic_event_item_t *item = &periodic_events[i];
  263. if (item->roles & PERIODIC_EVENT_ROLE_HS_SERVICE) {
  264. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 0);
  265. }
  266. }
  267. done:
  268. if (to_remove) {
  269. remove_service(get_hs_service_map(), to_remove);
  270. }
  271. hs_free_all();
  272. }
  273. #define PE_TEST(name) \
  274. { #name, test_pe_## name , TT_FORK, NULL, NULL }
  275. struct testcase_t periodic_event_tests[] = {
  276. PE_TEST(initialize),
  277. PE_TEST(launch),
  278. PE_TEST(get_roles),
  279. PE_TEST(hs_service),
  280. END_OF_TESTCASES
  281. };