test_router.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/main.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. }
  50. return mock_routerinfo;
  51. }
  52. /* If no distribution option was set, then check_bridge_distribution_setting()
  53. * should have set it to "any". */
  54. static void
  55. test_router_dump_router_to_string_no_bridge_distribution_method(void *arg)
  56. {
  57. const char* needle = "bridge-distribution-request any";
  58. or_options_t* options = get_options_mutable();
  59. routerinfo_t* router = NULL;
  60. curve25519_keypair_t ntor_keypair;
  61. ed25519_keypair_t signing_keypair;
  62. char* desc = NULL;
  63. char* found = NULL;
  64. (void)arg;
  65. NS_MOCK(router_get_my_routerinfo);
  66. options->ORPort_set = 1;
  67. options->BridgeRelay = 1;
  68. /* Generate keys which router_dump_router_to_string() expects to exist. */
  69. tt_int_op(0, ==, curve25519_keypair_generate(&ntor_keypair, 0));
  70. tt_int_op(0, ==, ed25519_keypair_generate(&signing_keypair, 0));
  71. /* Set up part of our routerinfo_t so that we don't trigger any other
  72. * assertions in router_dump_router_to_string(). */
  73. router = (routerinfo_t*)router_get_my_routerinfo();
  74. tt_ptr_op(router, !=, NULL);
  75. router->onion_curve25519_pkey = &ntor_keypair.pubkey;
  76. /* Generate our server descriptor and ensure that the substring
  77. * "bridge-distribution-request any" occurs somewhere within it. */
  78. crypto_pk_t *onion_pkey = router_get_rsa_onion_pkey(router->onion_pkey,
  79. router->onion_pkey_len);
  80. desc = router_dump_router_to_string(router,
  81. router->identity_pkey,
  82. onion_pkey,
  83. &ntor_keypair,
  84. &signing_keypair);
  85. crypto_pk_free(onion_pkey);
  86. tt_ptr_op(desc, !=, NULL);
  87. found = strstr(desc, needle);
  88. tt_ptr_op(found, !=, NULL);
  89. done:
  90. NS_UNMOCK(router_get_my_routerinfo);
  91. tor_free(desc);
  92. }
  93. static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
  94. static const routerinfo_t *
  95. mock_router_get_my_routerinfo(void)
  96. {
  97. return mock_router_get_my_routerinfo_result;
  98. }
  99. static long
  100. mock_get_uptime_3h(void)
  101. {
  102. return 3*60*60;
  103. }
  104. static long
  105. mock_get_uptime_1d(void)
  106. {
  107. return 24*60*60;
  108. }
  109. static int
  110. mock_rep_hist_bandwidth_assess(void)
  111. {
  112. return 20001;
  113. }
  114. static int
  115. mock_we_are_not_hibernating(void)
  116. {
  117. return 0;
  118. }
  119. static int
  120. mock_we_are_hibernating(void)
  121. {
  122. return 0;
  123. }
  124. static void
  125. test_router_check_descriptor_bandwidth_changed(void *arg)
  126. {
  127. (void)arg;
  128. routerinfo_t routerinfo;
  129. memset(&routerinfo, 0, sizeof(routerinfo));
  130. mock_router_get_my_routerinfo_result = NULL;
  131. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  132. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  133. mock_router_get_my_routerinfo_result = &routerinfo;
  134. /* When uptime is less than 24h, no previous bandwidth, no last_changed
  135. * Uptime: 10800, last_changed: 0, Previous bw: 0, Current bw: 0 */
  136. routerinfo.bandwidthcapacity = 0;
  137. MOCK(get_uptime, mock_get_uptime_3h);
  138. setup_full_capture_of_logs(LOG_INFO);
  139. check_descriptor_bandwidth_changed(time(NULL));
  140. expect_log_msg_not_containing(
  141. "Measured bandwidth has changed; rebuilding descriptor.");
  142. teardown_capture_of_logs();
  143. /* When uptime is less than 24h, previous bandwidth,
  144. * last_changed more than 3h ago
  145. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  146. routerinfo.bandwidthcapacity = 10000;
  147. setup_full_capture_of_logs(LOG_INFO);
  148. check_descriptor_bandwidth_changed(time(NULL));
  149. expect_log_msg_containing(
  150. "Measured bandwidth has changed; rebuilding descriptor.");
  151. teardown_capture_of_logs();
  152. /* When uptime is less than 24h, previous bandwidth,
  153. * last_changed more than 3h ago, and hibernating
  154. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  155. UNMOCK(we_are_hibernating);
  156. MOCK(we_are_hibernating, mock_we_are_hibernating);
  157. routerinfo.bandwidthcapacity = 10000;
  158. setup_full_capture_of_logs(LOG_INFO);
  159. check_descriptor_bandwidth_changed(time(NULL));
  160. expect_log_msg_not_containing(
  161. "Measured bandwidth has changed; rebuilding descriptor.");
  162. teardown_capture_of_logs();
  163. UNMOCK(we_are_hibernating);
  164. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  165. /* When uptime is less than 24h, last_changed is not more than 3h ago
  166. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 0 */
  167. setup_full_capture_of_logs(LOG_INFO);
  168. check_descriptor_bandwidth_changed(time(NULL));
  169. expect_log_msg_not_containing(
  170. "Measured bandwidth has changed; rebuilding descriptor.");
  171. teardown_capture_of_logs();
  172. /* When uptime is less than 24h and bandwidthcapacity does not change
  173. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 20001 */
  174. MOCK(rep_hist_bandwidth_assess, mock_rep_hist_bandwidth_assess);
  175. setup_full_capture_of_logs(LOG_INFO);
  176. check_descriptor_bandwidth_changed(time(NULL) + 6*60*60 + 1);
  177. expect_log_msg_containing(
  178. "Measured bandwidth has changed; rebuilding descriptor.");
  179. UNMOCK(get_uptime);
  180. UNMOCK(rep_hist_bandwidth_assess);
  181. teardown_capture_of_logs();
  182. /* When uptime is more than 24h */
  183. MOCK(get_uptime, mock_get_uptime_1d);
  184. setup_full_capture_of_logs(LOG_INFO);
  185. check_descriptor_bandwidth_changed(time(NULL));
  186. expect_log_msg_not_containing(
  187. "Measured bandwidth has changed; rebuilding descriptor.");
  188. teardown_capture_of_logs();
  189. done:
  190. UNMOCK(get_uptime);
  191. UNMOCK(router_get_my_routerinfo);
  192. UNMOCK(we_are_hibernating);
  193. }
  194. #define ROUTER_TEST(name, flags) \
  195. { #name, test_router_ ## name, flags, NULL, NULL }
  196. struct testcase_t router_tests[] = {
  197. ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
  198. ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
  199. END_OF_TESTCASES
  200. };