]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add test for check_descriptor_bandwidth_changed
authorjuga0 <juga@riseup.net>
Sat, 9 Jun 2018 10:34:41 +0000 (10:34 +0000)
committerNick Mathewson <nickm@torproject.org>
Sat, 1 Sep 2018 22:47:20 +0000 (18:47 -0400)
src/test/include.am
src/test/test_router.c [new file with mode: 0644]

index 0ee3d1169f681b44903ab25295b61b4b8a8d333b..7864d7d9fd3eff0cecf9b9807701d8b16513da79 100644 (file)
@@ -117,6 +117,7 @@ src_test_test_SOURCES = \
        src/test/test_relaycell.c \
        src/test/test_rendcache.c \
        src/test/test_replay.c \
+       src/test/test_router.c \
        src/test/test_routerkeys.c \
        src/test/test_routerlist.c \
        src/test/test_routerset.c \
diff --git a/src/test/test_router.c b/src/test/test_router.c
new file mode 100644 (file)
index 0000000..a4921da
--- /dev/null
@@ -0,0 +1,120 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information     */
+
+/**
+ * \file test_router.c
+ * \brief Unittests for code in src/or/router.c
+ **/
+
+#include "or.h"
+#include "hibernate.h"
+#include "log_test_helpers.h"
+#include "main.h"
+#include "rephist.h"
+#include "router.h"
+#include "test.h"
+
+static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
+
+static const routerinfo_t *
+mock_router_get_my_routerinfo(void)
+{
+  return mock_router_get_my_routerinfo_result;
+}
+
+static long
+mock_get_uptime_3h(void)
+{
+  return 3*60*60;
+}
+
+static long
+mock_get_uptime_1d(void)
+{
+  return 24*60*60;
+}
+
+static int
+mock_rep_hist_bandwidth_assess(void)
+{
+  return 20001;
+}
+
+static int
+mock_we_are_hibernating(void)
+{
+  return 0;
+}
+
+static void
+test_router_check_descriptor_bandwidth_changed(void *arg)
+{
+  (void)arg;
+  routerinfo_t routerinfo;
+  memset(&routerinfo, 0, sizeof(routerinfo));
+  mock_router_get_my_routerinfo_result = NULL;
+
+  MOCK(we_are_hibernating, mock_we_are_hibernating);
+  MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
+  mock_router_get_my_routerinfo_result = &routerinfo;
+
+  /* When uptime is less than 24h, no previous bandwidth, no last_changed
+   * Uptime: 10800, last_changed: 0, Previous bw: 0, Current bw: 0 */
+  routerinfo.bandwidthcapacity = 0;
+  MOCK(get_uptime, mock_get_uptime_3h);
+  setup_full_capture_of_logs(LOG_INFO);
+  check_descriptor_bandwidth_changed(time(NULL));
+  expect_log_msg_not_containing(
+     "Measured bandwidth has changed; rebuilding descriptor.");
+  teardown_capture_of_logs();
+
+  /* When uptime is less than 24h, previous bandwidth,
+   * last_changed more than 3h ago
+   * Uptime: 10800, last_changed: 0, Previous bw: 10000, Current bw: 0 */
+  routerinfo.bandwidthcapacity = 10000;
+  setup_full_capture_of_logs(LOG_INFO);
+  check_descriptor_bandwidth_changed(time(NULL));
+  expect_log_msg_containing(
+     "Measured bandwidth has changed; rebuilding descriptor.");
+  teardown_capture_of_logs();
+
+  /* When uptime is less than 24h, last_changed is not more than 3h ago
+   * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 0 */
+  setup_full_capture_of_logs(LOG_INFO);
+  check_descriptor_bandwidth_changed(time(NULL));
+  expect_log_msg_not_containing(
+     "Measured bandwidth has changed; rebuilding descriptor.");
+  teardown_capture_of_logs();
+
+  /* When uptime is less than 24h and bandwidthcapacity does not change
+   * Uptime: 10800, last_changed: x, Previous bw: 10000, Current bw: 20001 */
+  MOCK(rep_hist_bandwidth_assess, mock_rep_hist_bandwidth_assess);
+  setup_full_capture_of_logs(LOG_INFO);
+  check_descriptor_bandwidth_changed(time(NULL) + 6*60*60 + 1);
+  expect_log_msg_containing(
+     "Measured bandwidth has changed; rebuilding descriptor.");
+  UNMOCK(get_uptime);
+  UNMOCK(rep_hist_bandwidth_assess);
+  teardown_capture_of_logs();
+
+  /* When uptime is more than 24h */
+  MOCK(get_uptime, mock_get_uptime_1d);
+  setup_full_capture_of_logs(LOG_INFO);
+  check_descriptor_bandwidth_changed(time(NULL));
+  expect_log_msg_not_containing(
+     "Measured bandwidth has changed; rebuilding descriptor.");
+  UNMOCK(get_uptime);
+  teardown_capture_of_logs();
+
+ done:
+  UNMOCK(router_get_my_routerinfo);
+}
+
+#define ROUTER_TEST(name, flags)                          \
+  { #name, test_router_ ## name, flags, NULL, NULL }
+
+struct testcase_t router_tests[] = {
+  ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
+  END_OF_TESTCASES
+};
+