test_router.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #define ROUTER_PRIVATE
  9. #include "core/or/or.h"
  10. #include "app/config/config.h"
  11. #include "core/mainloop/mainloop.h"
  12. #include "feature/hibernate/hibernate.h"
  13. #include "feature/nodelist/networkstatus.h"
  14. #include "feature/nodelist/networkstatus_st.h"
  15. #include "feature/nodelist/routerinfo_st.h"
  16. #include "feature/nodelist/routerlist.h"
  17. #include "feature/nodelist/routerstatus_st.h"
  18. #include "feature/relay/router.h"
  19. #include "feature/stats/rephist.h"
  20. #include "lib/crypt_ops/crypto_curve25519.h"
  21. #include "lib/crypt_ops/crypto_ed25519.h"
  22. /* Test suite stuff */
  23. #include "test/test.h"
  24. #include "test/log_test_helpers.h"
  25. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  26. static routerinfo_t* mock_routerinfo;
  27. static const routerinfo_t*
  28. NS(router_get_my_routerinfo)(void)
  29. {
  30. crypto_pk_t* ident_key;
  31. crypto_pk_t* tap_key;
  32. time_t now;
  33. if (!mock_routerinfo) {
  34. /* Mock the published timestamp, otherwise router_dump_router_to_string()
  35. * will poop its pants. */
  36. time(&now);
  37. /* We'll need keys, or router_dump_router_to_string() would return NULL. */
  38. ident_key = pk_generate(0);
  39. tap_key = pk_generate(0);
  40. tor_assert(ident_key != NULL);
  41. tor_assert(tap_key != NULL);
  42. mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
  43. mock_routerinfo->nickname = tor_strdup("ConlonNancarrow");
  44. mock_routerinfo->addr = 123456789;
  45. mock_routerinfo->or_port = 443;
  46. mock_routerinfo->platform = tor_strdup("unittest");
  47. mock_routerinfo->cache_info.published_on = now;
  48. mock_routerinfo->identity_pkey = crypto_pk_dup_key(ident_key);
  49. router_set_rsa_onion_pkey(tap_key, &mock_routerinfo->onion_pkey,
  50. &mock_routerinfo->onion_pkey_len);
  51. mock_routerinfo->bandwidthrate = 9001;
  52. mock_routerinfo->bandwidthburst = 9002;
  53. crypto_pk_free(ident_key);
  54. crypto_pk_free(tap_key);
  55. }
  56. return mock_routerinfo;
  57. }
  58. /* If no distribution option was set, then check_bridge_distribution_setting()
  59. * should have set it to "any". */
  60. static void
  61. test_router_dump_router_to_string_no_bridge_distribution_method(void *arg)
  62. {
  63. const char* needle = "bridge-distribution-request any";
  64. or_options_t* options = get_options_mutable();
  65. routerinfo_t* router = NULL;
  66. curve25519_keypair_t ntor_keypair;
  67. ed25519_keypair_t signing_keypair;
  68. char* desc = NULL;
  69. char* found = NULL;
  70. (void)arg;
  71. NS_MOCK(router_get_my_routerinfo);
  72. options->ORPort_set = 1;
  73. options->BridgeRelay = 1;
  74. /* Generate keys which router_dump_router_to_string() expects to exist. */
  75. tt_int_op(0, ==, curve25519_keypair_generate(&ntor_keypair, 0));
  76. tt_int_op(0, ==, ed25519_keypair_generate(&signing_keypair, 0));
  77. /* Set up part of our routerinfo_t so that we don't trigger any other
  78. * assertions in router_dump_router_to_string(). */
  79. router = (routerinfo_t*)router_get_my_routerinfo();
  80. tt_ptr_op(router, !=, NULL);
  81. router->onion_curve25519_pkey = &ntor_keypair.pubkey;
  82. /* Generate our server descriptor and ensure that the substring
  83. * "bridge-distribution-request any" occurs somewhere within it. */
  84. crypto_pk_t *onion_pkey = router_get_rsa_onion_pkey(router->onion_pkey,
  85. router->onion_pkey_len);
  86. desc = router_dump_router_to_string(router,
  87. router->identity_pkey,
  88. onion_pkey,
  89. &ntor_keypair,
  90. &signing_keypair);
  91. crypto_pk_free(onion_pkey);
  92. tt_ptr_op(desc, !=, NULL);
  93. found = strstr(desc, needle);
  94. tt_ptr_op(found, !=, NULL);
  95. done:
  96. NS_UNMOCK(router_get_my_routerinfo);
  97. tor_free(desc);
  98. }
  99. static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
  100. static const routerinfo_t *
  101. mock_router_get_my_routerinfo(void)
  102. {
  103. return mock_router_get_my_routerinfo_result;
  104. }
  105. static long
  106. mock_get_uptime_3h(void)
  107. {
  108. return 3*60*60;
  109. }
  110. static long
  111. mock_get_uptime_1d(void)
  112. {
  113. return 24*60*60;
  114. }
  115. static int
  116. mock_rep_hist_bandwidth_assess(void)
  117. {
  118. return 20001;
  119. }
  120. static int
  121. mock_we_are_not_hibernating(void)
  122. {
  123. return 0;
  124. }
  125. static int
  126. mock_we_are_hibernating(void)
  127. {
  128. return 0;
  129. }
  130. static void
  131. test_router_check_descriptor_bandwidth_changed(void *arg)
  132. {
  133. (void)arg;
  134. routerinfo_t routerinfo;
  135. memset(&routerinfo, 0, sizeof(routerinfo));
  136. mock_router_get_my_routerinfo_result = NULL;
  137. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  138. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  139. mock_router_get_my_routerinfo_result = &routerinfo;
  140. /* When uptime is less than 24h, no previous bandwidth, no last_changed
  141. * Uptime: 10800, last_changed: 0, Previous bw: 0, Current bw: 0 */
  142. routerinfo.bandwidthcapacity = 0;
  143. MOCK(get_uptime, mock_get_uptime_3h);
  144. setup_full_capture_of_logs(LOG_INFO);
  145. check_descriptor_bandwidth_changed(time(NULL));
  146. expect_log_msg_not_containing(
  147. "Measured bandwidth has changed; rebuilding descriptor.");
  148. teardown_capture_of_logs();
  149. /* When uptime is less than 24h, previous bandwidth,
  150. * last_changed more than 3h ago
  151. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  152. routerinfo.bandwidthcapacity = 10000;
  153. setup_full_capture_of_logs(LOG_INFO);
  154. check_descriptor_bandwidth_changed(time(NULL));
  155. expect_log_msg_containing(
  156. "Measured bandwidth has changed; rebuilding descriptor.");
  157. teardown_capture_of_logs();
  158. /* When uptime is less than 24h, previous bandwidth,
  159. * last_changed more than 3h ago, and hibernating
  160. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  161. UNMOCK(we_are_hibernating);
  162. MOCK(we_are_hibernating, mock_we_are_hibernating);
  163. routerinfo.bandwidthcapacity = 10000;
  164. setup_full_capture_of_logs(LOG_INFO);
  165. check_descriptor_bandwidth_changed(time(NULL));
  166. expect_log_msg_not_containing(
  167. "Measured bandwidth has changed; rebuilding descriptor.");
  168. teardown_capture_of_logs();
  169. UNMOCK(we_are_hibernating);
  170. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  171. /* When uptime is less than 24h, last_changed is not more than 3h ago
  172. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 0 */
  173. setup_full_capture_of_logs(LOG_INFO);
  174. check_descriptor_bandwidth_changed(time(NULL));
  175. expect_log_msg_not_containing(
  176. "Measured bandwidth has changed; rebuilding descriptor.");
  177. teardown_capture_of_logs();
  178. /* When uptime is less than 24h and bandwidthcapacity does not change
  179. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 20001 */
  180. MOCK(rep_hist_bandwidth_assess, mock_rep_hist_bandwidth_assess);
  181. setup_full_capture_of_logs(LOG_INFO);
  182. check_descriptor_bandwidth_changed(time(NULL) + 6*60*60 + 1);
  183. expect_log_msg_containing(
  184. "Measured bandwidth has changed; rebuilding descriptor.");
  185. UNMOCK(get_uptime);
  186. UNMOCK(rep_hist_bandwidth_assess);
  187. teardown_capture_of_logs();
  188. /* When uptime is more than 24h */
  189. MOCK(get_uptime, mock_get_uptime_1d);
  190. setup_full_capture_of_logs(LOG_INFO);
  191. check_descriptor_bandwidth_changed(time(NULL));
  192. expect_log_msg_not_containing(
  193. "Measured bandwidth has changed; rebuilding descriptor.");
  194. teardown_capture_of_logs();
  195. done:
  196. UNMOCK(get_uptime);
  197. UNMOCK(router_get_my_routerinfo);
  198. UNMOCK(we_are_hibernating);
  199. }
  200. static networkstatus_t *mock_ns = NULL;
  201. static networkstatus_t *
  202. mock_networkstatus_get_live_consensus(time_t now)
  203. {
  204. (void)now;
  205. return mock_ns;
  206. }
  207. static routerstatus_t *mock_rs = NULL;
  208. static const routerstatus_t *
  209. mock_networkstatus_vote_find_entry(networkstatus_t *ns, const char *digest)
  210. {
  211. (void)ns;
  212. (void)digest;
  213. return mock_rs;
  214. }
  215. static void
  216. test_router_mark_if_too_old(void *arg)
  217. {
  218. (void)arg;
  219. time_t now = approx_time();
  220. MOCK(networkstatus_get_live_consensus,
  221. mock_networkstatus_get_live_consensus);
  222. MOCK(networkstatus_vote_find_entry, mock_networkstatus_vote_find_entry);
  223. routerstatus_t rs;
  224. networkstatus_t ns;
  225. memset(&rs, 0, sizeof(rs));
  226. memset(&ns, 0, sizeof(ns));
  227. mock_ns = &ns;
  228. mock_ns->valid_after = now-3600;
  229. mock_rs = &rs;
  230. mock_rs->published_on = now - 10;
  231. // no reason to mark this time.
  232. desc_clean_since = now-10;
  233. desc_dirty_reason = NULL;
  234. mark_my_descriptor_dirty_if_too_old(now);
  235. tt_i64_op(desc_clean_since, OP_EQ, now-10);
  236. // Doesn't appear in consensus? Still don't mark it.
  237. mock_ns = NULL;
  238. mark_my_descriptor_dirty_if_too_old(now);
  239. tt_i64_op(desc_clean_since, OP_EQ, now-10);
  240. mock_ns = &ns;
  241. // No new descriptor in a long time? Mark it.
  242. desc_clean_since = now - 3600 * 96;
  243. mark_my_descriptor_dirty_if_too_old(now);
  244. tt_i64_op(desc_clean_since, OP_EQ, 0);
  245. tt_str_op(desc_dirty_reason, OP_EQ, "time for new descriptor");
  246. // Version in consensus published a long time ago? We won't mark it
  247. // if it's been clean for only a short time.
  248. desc_clean_since = now - 10;
  249. desc_dirty_reason = NULL;
  250. mock_rs->published_on = now - 3600 * 96;
  251. mark_my_descriptor_dirty_if_too_old(now);
  252. tt_i64_op(desc_clean_since, OP_EQ, now - 10);
  253. // ... but if it's been clean a while, we mark.
  254. desc_clean_since = now - 2 * 3600;
  255. mark_my_descriptor_dirty_if_too_old(now);
  256. tt_i64_op(desc_clean_since, OP_EQ, 0);
  257. tt_str_op(desc_dirty_reason, OP_EQ,
  258. "version listed in consensus is quite old");
  259. // same deal if we're marked stale.
  260. desc_clean_since = now - 2 * 3600;
  261. desc_dirty_reason = NULL;
  262. mock_rs->published_on = now - 10;
  263. mock_rs->is_staledesc = 1;
  264. mark_my_descriptor_dirty_if_too_old(now);
  265. tt_i64_op(desc_clean_since, OP_EQ, 0);
  266. tt_str_op(desc_dirty_reason, OP_EQ,
  267. "listed as stale in consensus");
  268. // same deal if we're absent from the consensus.
  269. desc_clean_since = now - 2 * 3600;
  270. desc_dirty_reason = NULL;
  271. mock_rs = NULL;
  272. mark_my_descriptor_dirty_if_too_old(now);
  273. tt_i64_op(desc_clean_since, OP_EQ, 0);
  274. tt_str_op(desc_dirty_reason, OP_EQ,
  275. "not listed in consensus");
  276. done:
  277. UNMOCK(networkstatus_get_live_consensus);
  278. UNMOCK(networkstatus_vote_find_entry);
  279. }
  280. #define ROUTER_TEST(name, flags) \
  281. { #name, test_router_ ## name, flags, NULL, NULL }
  282. struct testcase_t router_tests[] = {
  283. ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
  284. ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
  285. ROUTER_TEST(mark_if_too_old, TT_FORK),
  286. END_OF_TESTCASES
  287. };