test_periodic_event.c 11 KB

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