test_router.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_hibernating(void)
  37. {
  38. return 0;
  39. }
  40. static void
  41. test_router_check_descriptor_bandwidth_changed(void *arg)
  42. {
  43. (void)arg;
  44. routerinfo_t routerinfo;
  45. memset(&routerinfo, 0, sizeof(routerinfo));
  46. mock_router_get_my_routerinfo_result = NULL;
  47. MOCK(we_are_hibernating, mock_we_are_hibernating);
  48. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  49. mock_router_get_my_routerinfo_result = &routerinfo;
  50. /* When uptime is less than 24h, no previous bandwidth, no last_changed
  51. * Uptime: 10800, last_changed: 0, Previous bw: 0, Current bw: 0 */
  52. routerinfo.bandwidthcapacity = 0;
  53. MOCK(get_uptime, mock_get_uptime_3h);
  54. setup_full_capture_of_logs(LOG_INFO);
  55. check_descriptor_bandwidth_changed(time(NULL));
  56. expect_log_msg_not_containing(
  57. "Measured bandwidth has changed; rebuilding descriptor.");
  58. teardown_capture_of_logs();
  59. /* When uptime is less than 24h, previous bandwidth,
  60. * last_changed more than 3h ago
  61. * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
  62. routerinfo.bandwidthcapacity = 10000;
  63. setup_full_capture_of_logs(LOG_INFO);
  64. check_descriptor_bandwidth_changed(time(NULL));
  65. expect_log_msg_containing(
  66. "Measured bandwidth has changed; rebuilding descriptor.");
  67. teardown_capture_of_logs();
  68. /* When uptime is less than 24h, last_changed is not more than 3h ago
  69. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 0 */
  70. setup_full_capture_of_logs(LOG_INFO);
  71. check_descriptor_bandwidth_changed(time(NULL));
  72. expect_log_msg_not_containing(
  73. "Measured bandwidth has changed; rebuilding descriptor.");
  74. teardown_capture_of_logs();
  75. /* When uptime is less than 24h and bandwidthcapacity does not change
  76. * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 20001 */
  77. MOCK(rep_hist_bandwidth_assess, mock_rep_hist_bandwidth_assess);
  78. setup_full_capture_of_logs(LOG_INFO);
  79. check_descriptor_bandwidth_changed(time(NULL) + 6*60*60 + 1);
  80. expect_log_msg_containing(
  81. "Measured bandwidth has changed; rebuilding descriptor.");
  82. UNMOCK(get_uptime);
  83. UNMOCK(rep_hist_bandwidth_assess);
  84. teardown_capture_of_logs();
  85. /* When uptime is more than 24h */
  86. MOCK(get_uptime, mock_get_uptime_1d);
  87. setup_full_capture_of_logs(LOG_INFO);
  88. check_descriptor_bandwidth_changed(time(NULL));
  89. expect_log_msg_not_containing(
  90. "Measured bandwidth has changed; rebuilding descriptor.");
  91. UNMOCK(get_uptime);
  92. teardown_capture_of_logs();
  93. done:
  94. UNMOCK(router_get_my_routerinfo);
  95. }
  96. #define ROUTER_TEST(name, flags) \
  97. { #name, test_router_ ## name, flags, NULL, NULL }
  98. struct testcase_t router_tests[] = {
  99. ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
  100. END_OF_TESTCASES
  101. };