test_router.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* Copyright (c) 2017, isis agora lovecruft */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file test_router.c
  6. * \brief Unittests for code in src/or/router.c
  7. **/
  8. #include "or.h"
  9. #include "config.h"
  10. #include "crypto_curve25519.h"
  11. #include "crypto_ed25519.h"
  12. #include "hibernate.h"
  13. #include "main.h"
  14. #include "rephist.h"
  15. #include "router.h"
  16. #include "routerlist.h"
  17. /* Test suite stuff */
  18. #include "test.h"
  19. #include "log_test_helpers.h"
  20. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  21. static routerinfo_t* mock_routerinfo;
  22. static const routerinfo_t*
  23. NS(router_get_my_routerinfo)(void)
  24. {
  25. crypto_pk_t* ident_key;
  26. crypto_pk_t* tap_key;
  27. time_t now;
  28. if (!mock_routerinfo) {
  29. /* Mock the published timestamp, otherwise router_dump_router_to_string()
  30. * will poop its pants. */
  31. time(&now);
  32. /* We'll need keys, or router_dump_router_to_string() would return NULL. */
  33. ident_key = pk_generate(0);
  34. tap_key = pk_generate(0);
  35. tor_assert(ident_key != NULL);
  36. tor_assert(tap_key != NULL);
  37. mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
  38. mock_routerinfo->nickname = tor_strdup("ConlonNancarrow");
  39. mock_routerinfo->addr = 123456789;
  40. mock_routerinfo->or_port = 443;
  41. mock_routerinfo->platform = tor_strdup("unittest");
  42. mock_routerinfo->cache_info.published_on = now;
  43. mock_routerinfo->identity_pkey = crypto_pk_dup_key(ident_key);
  44. mock_routerinfo->onion_pkey = crypto_pk_dup_key(tap_key);
  45. mock_routerinfo->bandwidthrate = 9001;
  46. mock_routerinfo->bandwidthburst = 9002;
  47. }
  48. return mock_routerinfo;
  49. }
  50. /* If no distribution option was set, then check_bridge_distribution_setting()
  51. * should have set it to "any". */
  52. static void
  53. test_router_dump_router_to_string_no_bridge_distribution_method(void *arg)
  54. {
  55. const char* needle = "bridge-distribution-request any";
  56. or_options_t* options = get_options_mutable();
  57. routerinfo_t* router = NULL;
  58. curve25519_keypair_t ntor_keypair;
  59. ed25519_keypair_t signing_keypair;
  60. char* desc = NULL;
  61. char* found = NULL;
  62. (void)arg;
  63. NS_MOCK(router_get_my_routerinfo);
  64. options->ORPort_set = 1;
  65. options->BridgeRelay = 1;
  66. /* Generate keys which router_dump_router_to_string() expects to exist. */
  67. tt_int_op(0, ==, curve25519_keypair_generate(&ntor_keypair, 0));
  68. tt_int_op(0, ==, ed25519_keypair_generate(&signing_keypair, 0));
  69. /* Set up part of our routerinfo_t so that we don't trigger any other
  70. * assertions in router_dump_router_to_string(). */
  71. router = (routerinfo_t*)router_get_my_routerinfo();
  72. tt_ptr_op(router, !=, NULL);
  73. router->onion_curve25519_pkey = &ntor_keypair.pubkey;
  74. /* Generate our server descriptor and ensure that the substring
  75. * "bridge-distribution-request any" occurs somewhere within it. */
  76. desc = router_dump_router_to_string(router,
  77. router->identity_pkey,
  78. router->onion_pkey,
  79. &ntor_keypair,
  80. &signing_keypair);
  81. tt_ptr_op(desc, !=, NULL);
  82. found = strstr(desc, needle);
  83. tt_ptr_op(found, !=, NULL);
  84. done:
  85. NS_UNMOCK(router_get_my_routerinfo);
  86. tor_free(desc);
  87. }
  88. static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
  89. static const routerinfo_t *
  90. mock_router_get_my_routerinfo(void)
  91. {
  92. return mock_router_get_my_routerinfo_result;
  93. }
  94. static long
  95. mock_get_uptime_3h(void)
  96. {
  97. return 3*60*60;
  98. }
  99. static long
  100. mock_get_uptime_1d(void)
  101. {
  102. return 24*60*60;
  103. }
  104. static int
  105. mock_rep_hist_bandwidth_assess(void)
  106. {
  107. return 20001;
  108. }
  109. static int
  110. mock_we_are_not_hibernating(void)
  111. {
  112. return 0;
  113. }
  114. static int
  115. mock_we_are_hibernating(void)
  116. {
  117. return 0;
  118. }
  119. static void
  120. test_router_check_descriptor_bandwidth_changed(void *arg)
  121. {
  122. (void)arg;
  123. routerinfo_t routerinfo;
  124. memset(&routerinfo, 0, sizeof(routerinfo));
  125. mock_router_get_my_routerinfo_result = NULL;
  126. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  127. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  128. mock_router_get_my_routerinfo_result = &routerinfo;
  129. /* When uptime is less than 24h, no previous bandwidth, no last_changed
  130. * Uptime: 10800, last_changed: 0, Previous bw: 0, Current bw: 0 */
  131. routerinfo.bandwidthcapacity = 0;
  132. MOCK(get_uptime, mock_get_uptime_3h);
  133. setup_full_capture_of_logs(LOG_INFO);
  134. check_descriptor_bandwidth_changed(time(NULL));
  135. expect_log_msg_not_containing(
  136. "Measured bandwidth has changed; rebuilding descriptor.");
  137. teardown_capture_of_logs();
  138. /* When uptime is less than 24h, previous bandwidth,
  139. * last_changed more than 3h ago
  140. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  141. routerinfo.bandwidthcapacity = 10000;
  142. setup_full_capture_of_logs(LOG_INFO);
  143. check_descriptor_bandwidth_changed(time(NULL));
  144. expect_log_msg_containing(
  145. "Measured bandwidth has changed; rebuilding descriptor.");
  146. teardown_capture_of_logs();
  147. /* When uptime is less than 24h, previous bandwidth,
  148. * last_changed more than 3h ago, and hibernating
  149. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  150. UNMOCK(we_are_hibernating);
  151. MOCK(we_are_hibernating, mock_we_are_hibernating);
  152. routerinfo.bandwidthcapacity = 10000;
  153. setup_full_capture_of_logs(LOG_INFO);
  154. check_descriptor_bandwidth_changed(time(NULL));
  155. expect_log_msg_not_containing(
  156. "Measured bandwidth has changed; rebuilding descriptor.");
  157. teardown_capture_of_logs();
  158. UNMOCK(we_are_hibernating);
  159. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  160. /* When uptime is less than 24h, last_changed is not more than 3h ago
  161. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 0 */
  162. setup_full_capture_of_logs(LOG_INFO);
  163. check_descriptor_bandwidth_changed(time(NULL));
  164. expect_log_msg_not_containing(
  165. "Measured bandwidth has changed; rebuilding descriptor.");
  166. teardown_capture_of_logs();
  167. /* When uptime is less than 24h and bandwidthcapacity does not change
  168. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 20001 */
  169. MOCK(rep_hist_bandwidth_assess, mock_rep_hist_bandwidth_assess);
  170. setup_full_capture_of_logs(LOG_INFO);
  171. check_descriptor_bandwidth_changed(time(NULL) + 6*60*60 + 1);
  172. expect_log_msg_containing(
  173. "Measured bandwidth has changed; rebuilding descriptor.");
  174. UNMOCK(get_uptime);
  175. UNMOCK(rep_hist_bandwidth_assess);
  176. teardown_capture_of_logs();
  177. /* When uptime is more than 24h */
  178. MOCK(get_uptime, mock_get_uptime_1d);
  179. setup_full_capture_of_logs(LOG_INFO);
  180. check_descriptor_bandwidth_changed(time(NULL));
  181. expect_log_msg_not_containing(
  182. "Measured bandwidth has changed; rebuilding descriptor.");
  183. teardown_capture_of_logs();
  184. done:
  185. UNMOCK(get_uptime);
  186. UNMOCK(router_get_my_routerinfo);
  187. UNMOCK(we_are_hibernating);
  188. }
  189. #define ROUTER_TEST(name, flags) \
  190. { #name, test_router_ ## name, flags, NULL, NULL }
  191. struct testcase_t router_tests[] = {
  192. ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
  193. ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
  194. END_OF_TESTCASES
  195. };