test_router.c 7.5 KB

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