test_router.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_router.c
  5. * \brief Unittests for code in src/or/router.c
  6. **/
  7. #include "or.h"
  8. #include "hibernate.h"
  9. #include "log_test_helpers.h"
  10. #include "main.h"
  11. #include "rephist.h"
  12. #include "router.h"
  13. #include "test.h"
  14. static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
  15. static const routerinfo_t *
  16. mock_router_get_my_routerinfo(void)
  17. {
  18. return mock_router_get_my_routerinfo_result;
  19. }
  20. static long
  21. mock_get_uptime_3h(void)
  22. {
  23. return 3*60*60;
  24. }
  25. static long
  26. mock_get_uptime_1d(void)
  27. {
  28. return 24*60*60;
  29. }
  30. static int
  31. mock_rep_hist_bandwidth_assess(void)
  32. {
  33. return 20001;
  34. }
  35. static int
  36. mock_we_are_not_hibernating(void)
  37. {
  38. return 0;
  39. }
  40. static int
  41. mock_we_are_hibernating(void)
  42. {
  43. return 0;
  44. }
  45. static void
  46. test_router_check_descriptor_bandwidth_changed(void *arg)
  47. {
  48. (void)arg;
  49. routerinfo_t routerinfo;
  50. memset(&routerinfo, 0, sizeof(routerinfo));
  51. mock_router_get_my_routerinfo_result = NULL;
  52. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  53. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  54. mock_router_get_my_routerinfo_result = &routerinfo;
  55. /* When uptime is less than 24h, no previous bandwidth, no last_changed
  56. * Uptime: 10800, last_changed: 0, Previous bw: 0, Current bw: 0 */
  57. routerinfo.bandwidthcapacity = 0;
  58. MOCK(get_uptime, mock_get_uptime_3h);
  59. setup_full_capture_of_logs(LOG_INFO);
  60. check_descriptor_bandwidth_changed(time(NULL));
  61. expect_log_msg_not_containing(
  62. "Measured bandwidth has changed; rebuilding descriptor.");
  63. teardown_capture_of_logs();
  64. /* When uptime is less than 24h, previous bandwidth,
  65. * last_changed more than 3h ago
  66. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  67. routerinfo.bandwidthcapacity = 10000;
  68. setup_full_capture_of_logs(LOG_INFO);
  69. check_descriptor_bandwidth_changed(time(NULL));
  70. expect_log_msg_containing(
  71. "Measured bandwidth has changed; rebuilding descriptor.");
  72. teardown_capture_of_logs();
  73. /* When uptime is less than 24h, previous bandwidth,
  74. * last_changed more than 3h ago, and hibernating
  75. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  76. UNMOCK(we_are_hibernating);
  77. MOCK(we_are_hibernating, mock_we_are_hibernating);
  78. routerinfo.bandwidthcapacity = 10000;
  79. setup_full_capture_of_logs(LOG_INFO);
  80. check_descriptor_bandwidth_changed(time(NULL));
  81. expect_log_msg_not_containing(
  82. "Measured bandwidth has changed; rebuilding descriptor.");
  83. teardown_capture_of_logs();
  84. UNMOCK(we_are_hibernating);
  85. MOCK(we_are_hibernating, mock_we_are_not_hibernating);
  86. /* When uptime is less than 24h, last_changed is not more than 3h ago
  87. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 0 */
  88. setup_full_capture_of_logs(LOG_INFO);
  89. check_descriptor_bandwidth_changed(time(NULL));
  90. expect_log_msg_not_containing(
  91. "Measured bandwidth has changed; rebuilding descriptor.");
  92. teardown_capture_of_logs();
  93. /* When uptime is less than 24h and bandwidthcapacity does not change
  94. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 20001 */
  95. MOCK(rep_hist_bandwidth_assess, mock_rep_hist_bandwidth_assess);
  96. setup_full_capture_of_logs(LOG_INFO);
  97. check_descriptor_bandwidth_changed(time(NULL) + 6*60*60 + 1);
  98. expect_log_msg_containing(
  99. "Measured bandwidth has changed; rebuilding descriptor.");
  100. UNMOCK(get_uptime);
  101. UNMOCK(rep_hist_bandwidth_assess);
  102. teardown_capture_of_logs();
  103. /* When uptime is more than 24h */
  104. MOCK(get_uptime, mock_get_uptime_1d);
  105. setup_full_capture_of_logs(LOG_INFO);
  106. check_descriptor_bandwidth_changed(time(NULL));
  107. expect_log_msg_not_containing(
  108. "Measured bandwidth has changed; rebuilding descriptor.");
  109. teardown_capture_of_logs();
  110. done:
  111. UNMOCK(get_uptime);
  112. UNMOCK(router_get_my_routerinfo);
  113. UNMOCK(we_are_hibernating);
  114. }
  115. #define ROUTER_TEST(name, flags) \
  116. { #name, test_router_ ## name, flags, NULL, NULL }
  117. struct testcase_t router_tests[] = {
  118. ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
  119. END_OF_TESTCASES
  120. };