summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-04-12 11:14:42 -0400
committerNick Mathewson <nickm@torproject.org>2018-04-12 11:14:42 -0400
commit037fb0c804efda988c5c05f0384fccd426f807d0 (patch)
tree47ad7dc1b097c96f38aff8b7d5cb7b7848f5b8cb /src/test
parenta51630cc9af96642b05b9d0db345ea2d7d3ce128 (diff)
parent31508a0abccfee1cda0869a9a7d22df74d6b67b7 (diff)
downloadtor-037fb0c804efda988c5c05f0384fccd426f807d0.tar.gz
tor-037fb0c804efda988c5c05f0384fccd426f807d0.zip
Merge branch 'maint-0.3.3'
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test_config.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/test/test_config.c b/src/test/test_config.c
index 3729c8aabb..1756f9040d 100644
--- a/src/test/test_config.c
+++ b/src/test/test_config.c
@@ -581,6 +581,22 @@ test_config_parse_transport_options_line(void *arg)
}
}
+/* Mocks needed for the compute_max_mem_in_queues test */
+static int get_total_system_memory_mock(size_t *mem_out);
+
+static size_t total_system_memory_output = 0;
+static int total_system_memory_return = 0;
+
+static int
+get_total_system_memory_mock(size_t *mem_out)
+{
+ if (! mem_out)
+ return -1;
+
+ *mem_out = total_system_memory_output;
+ return total_system_memory_return;
+}
+
/* Mocks needed for the transport plugin line test */
static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
@@ -5592,6 +5608,72 @@ test_config_include_opened_file_list(void *data)
tor_free(dir);
}
+static void
+test_config_compute_max_mem_in_queues(void *data)
+{
+#define GIGABYTE(x) (U64_LITERAL(x) << 30)
+#define MEGABYTE(x) (U64_LITERAL(x) << 20)
+ (void)data;
+ MOCK(get_total_system_memory, get_total_system_memory_mock);
+
+ /* We are unable to detect the amount of memory on the system. Tor will try
+ * to use some sensible default values for 64-bit and 32-bit systems. */
+ total_system_memory_return = -1;
+
+#if SIZEOF_VOID_P >= 8
+ /* We are on a 64-bit system. */
+ tt_int_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(8));
+#else
+ /* We are on a 32-bit system. */
+ tt_int_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(1));
+#endif
+
+ /* We are able to detect the amount of RAM on the system. */
+ total_system_memory_return = 0;
+
+ /* We are running on a system with one gigabyte of RAM. */
+ total_system_memory_output = GIGABYTE(1);
+
+ /* We have 0.75 * RAM available. */
+ tt_int_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
+ 3 * (GIGABYTE(1) / 4));
+
+ /* We are running on a tiny machine with 256 MB of RAM. */
+ total_system_memory_output = MEGABYTE(256);
+
+ /* We will now enforce a minimum of 256 MB of RAM available for the
+ * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192
+ * MB available. */
+ tt_int_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, MEGABYTE(256));
+
+ /* We are running on a machine with 8 GB of RAM. */
+ total_system_memory_output = GIGABYTE(8);
+
+ /* We will have 0.4 * RAM available. */
+ tt_int_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
+ 2 * (GIGABYTE(8) / 5));
+
+ /* We are running on a machine with 16 GB of RAM. */
+ total_system_memory_output = GIGABYTE(16);
+
+ /* We will have 0.4 * RAM available. */
+ tt_int_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
+ 2 * (GIGABYTE(16) / 5));
+
+ /* We are running on a machine with 32 GB of RAM. */
+ total_system_memory_output = GIGABYTE(32);
+
+ /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
+ tt_int_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
+ MAX_DEFAULT_MEMORY_QUEUE_SIZE);
+
+ done:
+ UNMOCK(get_total_system_memory);
+
+#undef GIGABYTE
+#undef MEGABYTE
+}
+
#define CONFIG_TEST(name, flags) \
{ #name, test_config_ ## name, flags, NULL, NULL }
@@ -5640,6 +5722,7 @@ struct testcase_t config_tests[] = {
CONFIG_TEST(check_bridge_distribution_setting_invalid, 0),
CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0),
CONFIG_TEST(include_opened_file_list, 0),
+ CONFIG_TEST(compute_max_mem_in_queues, 0),
END_OF_TESTCASES
};