test_periodic_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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,
  150. (item->roles != PERIODIC_EVENT_ROLE_CONTROLEV));
  151. }
  152. done:
  153. if (to_remove) {
  154. remove_service(get_hs_service_map(), to_remove);
  155. }
  156. hs_free_all();
  157. }
  158. static void
  159. test_pe_get_roles(void *arg)
  160. {
  161. int roles;
  162. (void) arg;
  163. /* Just so the HS global map exists. */
  164. hs_init();
  165. or_options_t *options = get_options_mutable();
  166. tt_assert(options);
  167. const int ALL = PERIODIC_EVENT_ROLE_ALL;
  168. /* Nothing configured, should be no roles. */
  169. tt_assert(net_is_disabled());
  170. roles = get_my_roles(options);
  171. tt_int_op(roles, OP_EQ, ALL);
  172. /* Indicate we have a SocksPort, roles should be come Client. */
  173. options->SocksPort_set = 1;
  174. roles = get_my_roles(options);
  175. tt_int_op(roles, OP_EQ, PERIODIC_EVENT_ROLE_CLIENT|ALL);
  176. /* Now, we'll add a ORPort so should now be a Relay + Client. */
  177. options->ORPort_set = 1;
  178. roles = get_my_roles(options);
  179. tt_int_op(roles, OP_EQ,
  180. (PERIODIC_EVENT_ROLE_CLIENT | PERIODIC_EVENT_ROLE_RELAY |
  181. PERIODIC_EVENT_ROLE_DIRSERVER | ALL));
  182. /* Now add a Bridge. */
  183. options->BridgeRelay = 1;
  184. roles = get_my_roles(options);
  185. tt_int_op(roles, OP_EQ,
  186. (PERIODIC_EVENT_ROLE_CLIENT | PERIODIC_EVENT_ROLE_RELAY |
  187. PERIODIC_EVENT_ROLE_BRIDGE | PERIODIC_EVENT_ROLE_DIRSERVER |
  188. ALL));
  189. tt_assert(roles & PERIODIC_EVENT_ROLE_ROUTER);
  190. /* Unset client so we can solely test Router role. */
  191. options->SocksPort_set = 0;
  192. roles = get_my_roles(options);
  193. tt_int_op(roles, OP_EQ,
  194. PERIODIC_EVENT_ROLE_ROUTER | PERIODIC_EVENT_ROLE_DIRSERVER |
  195. ALL);
  196. /* Reset options so we can test authorities. */
  197. options->SocksPort_set = 0;
  198. options->ORPort_set = 0;
  199. options->BridgeRelay = 0;
  200. roles = get_my_roles(options);
  201. tt_int_op(roles, OP_EQ, ALL);
  202. /* Now upgrade to Dirauth. */
  203. options->DirPort_set = 1;
  204. options->AuthoritativeDir = 1;
  205. options->V3AuthoritativeDir = 1;
  206. roles = get_my_roles(options);
  207. tt_int_op(roles, OP_EQ,
  208. PERIODIC_EVENT_ROLE_DIRAUTH|PERIODIC_EVENT_ROLE_DIRSERVER|ALL);
  209. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  210. /* Now Bridge Authority. */
  211. options->V3AuthoritativeDir = 0;
  212. options->BridgeAuthoritativeDir = 1;
  213. roles = get_my_roles(options);
  214. tt_int_op(roles, OP_EQ,
  215. PERIODIC_EVENT_ROLE_BRIDGEAUTH|PERIODIC_EVENT_ROLE_DIRSERVER|ALL);
  216. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  217. /* Move that bridge auth to become a relay. */
  218. options->ORPort_set = 1;
  219. roles = get_my_roles(options);
  220. tt_int_op(roles, OP_EQ,
  221. (PERIODIC_EVENT_ROLE_BRIDGEAUTH | PERIODIC_EVENT_ROLE_RELAY
  222. | PERIODIC_EVENT_ROLE_DIRSERVER|ALL));
  223. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  224. /* And now an Hidden service. */
  225. hs_service_t service;
  226. register_dummy_hidden_service(&service);
  227. roles = get_my_roles(options);
  228. /* Remove it now so the hs_free_all() doesn't try to free stack memory. */
  229. remove_service(get_hs_service_map(), &service);
  230. tt_int_op(roles, OP_EQ,
  231. (PERIODIC_EVENT_ROLE_BRIDGEAUTH | PERIODIC_EVENT_ROLE_RELAY |
  232. PERIODIC_EVENT_ROLE_HS_SERVICE | PERIODIC_EVENT_ROLE_DIRSERVER |
  233. ALL));
  234. tt_assert(roles & PERIODIC_EVENT_ROLE_AUTHORITIES);
  235. done:
  236. hs_free_all();
  237. }
  238. static void
  239. test_pe_hs_service(void *arg)
  240. {
  241. hs_service_t service, *to_remove = NULL;
  242. (void) arg;
  243. hs_init();
  244. /* We need to put tor in hibernation live state so the events requiring
  245. * network gets enabled. */
  246. consider_hibernation(time(NULL));
  247. /* Initialize the events so we can enable them */
  248. initialize_periodic_events();
  249. /* Hack: We'll set a dumb fn() of each events so they don't get called when
  250. * dispatching them. We just want to test the state of the callbacks, not
  251. * the whole code path. */
  252. for (int i = 0; periodic_events[i].name; ++i) {
  253. periodic_event_item_t *item = &periodic_events[i];
  254. item->fn = dumb_event_fn;
  255. }
  256. /* This should trigger a rescan of the list and enable the HS service
  257. * events. */
  258. register_dummy_hidden_service(&service);
  259. /* Note down the reference because we need to remove this service from the
  260. * global list before the hs_free_all() call so it doesn't try to free
  261. * memory on the stack. Furthermore, we can't remove it now else it will
  262. * trigger a rescan of the event disabling the HS service event. */
  263. to_remove = &service;
  264. for (int i = 0; periodic_events[i].name; ++i) {
  265. periodic_event_item_t *item = &periodic_events[i];
  266. if (item->roles & PERIODIC_EVENT_ROLE_HS_SERVICE) {
  267. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 1);
  268. }
  269. }
  270. to_remove = NULL;
  271. /* Remove the service from the global map, it should trigger a rescan and
  272. * disable the HS service events. */
  273. remove_service(get_hs_service_map(), &service);
  274. for (int i = 0; periodic_events[i].name; ++i) {
  275. periodic_event_item_t *item = &periodic_events[i];
  276. if (item->roles & PERIODIC_EVENT_ROLE_HS_SERVICE) {
  277. tt_int_op(periodic_event_is_enabled(item), OP_EQ, 0);
  278. }
  279. }
  280. done:
  281. if (to_remove) {
  282. remove_service(get_hs_service_map(), to_remove);
  283. }
  284. hs_free_all();
  285. }
  286. #define PE_TEST(name) \
  287. { #name, test_pe_## name , TT_FORK, NULL, NULL }
  288. struct testcase_t periodic_event_tests[] = {
  289. PE_TEST(initialize),
  290. PE_TEST(launch),
  291. PE_TEST(get_roles),
  292. PE_TEST(hs_service),
  293. END_OF_TESTCASES
  294. };