test_periodic_event.c 10 KB

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