test_periodic_event.c 11 KB

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