aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-03-04 11:03:30 -0500
committerNick Mathewson <nickm@torproject.org>2014-03-04 11:03:30 -0500
commitab225aaf28d7a25a58b5f11f1c59ded80570b594 (patch)
tree2cef1ba108879d507296d6a089f9cebc5237fad8 /src/test
parentbfa0e022bc6ec629473d3dfb598cad698ceee256 (diff)
parentbb375442141b4a5b301212394ce3e106cb34daf2 (diff)
downloadtor-ab225aaf28d7a25a58b5f11f1c59ded80570b594.tar
tor-ab225aaf28d7a25a58b5f11f1c59ded80570b594.tar.gz
Merge branch 'bug10169_025_v2'
Conflicts: src/test/test.c
Diffstat (limited to 'src/test')
-rw-r--r--src/test/include.am1
-rw-r--r--src/test/test.c2
-rw-r--r--src/test/test_buffers.c265
-rw-r--r--src/test/test_oom.c348
4 files changed, 613 insertions, 3 deletions
diff --git a/src/test/include.am b/src/test/include.am
index e7aebac38..c6743a19b 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -32,6 +32,7 @@ src_test_test_SOURCES = \
src/test/test_introduce.c \
src/test/test_logging.c \
src/test/test_microdesc.c \
+ src/test/test_oom.c \
src/test/test_options.c \
src/test/test_pt.c \
src/test/test_replay.c \
diff --git a/src/test/test.c b/src/test/test.c
index 2529e2902..9ca3f29a3 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1630,6 +1630,7 @@ extern struct testcase_t backtrace_tests[];
extern struct testcase_t hs_tests[];
extern struct testcase_t nodelist_tests[];
extern struct testcase_t routerkeys_tests[];
+extern struct testcase_t oom_tests[];
static struct testgroup_t testgroups[] = {
{ "", test_array },
@@ -1656,6 +1657,7 @@ static struct testgroup_t testgroups[] = {
{ "hs/", hs_tests },
{ "nodelist/", nodelist_tests },
{ "routerkeys/", routerkeys_tests },
+ { "oom/", oom_tests },
END_OF_GROUPS
};
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index a009faa0b..a410770b0 100644
--- a/src/test/test_buffers.c
+++ b/src/test/test_buffers.c
@@ -193,7 +193,120 @@ test_buffers_basic(void *arg)
buf_free(buf);
if (buf2)
buf_free(buf2);
+ buf_shrink_freelists(1);
}
+
+static void
+test_buffer_pullup(void *arg)
+{
+ buf_t *buf;
+ char *stuff, *tmp;
+ const char *cp;
+ size_t sz;
+ (void)arg;
+ stuff = tor_malloc(16384);
+ tmp = tor_malloc(16384);
+
+ /* Note: this test doesn't check the nulterminate argument to buf_pullup,
+ since nothing actually uses it. We should remove it some time. */
+
+ buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
+
+ tt_assert(buf);
+ tt_int_op(buf_get_default_chunk_size(buf), ==, 4096);
+
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+
+ /* There are a bunch of cases for pullup. One is the trivial case. Let's
+ mess around with an empty buffer. */
+ buf_pullup(buf, 16, 1);
+ buf_get_first_chunk_data(buf, &cp, &sz);
+ tt_ptr_op(cp, ==, NULL);
+ tt_ptr_op(sz, ==, 0);
+
+ /* Let's make sure nothing got allocated */
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+
+ /* Case 1: everything puts into the first chunk with some moving. */
+
+ /* Let's add some data. */
+ crypto_rand(stuff, 16384);
+ write_to_buf(stuff, 3000, buf);
+ write_to_buf(stuff+3000, 3000, buf);
+ buf_get_first_chunk_data(buf, &cp, &sz);
+ tt_ptr_op(cp, !=, NULL);
+ tt_int_op(sz, <=, 4096);
+
+ /* Make room for 3000 bytes in the first chunk, so that the pullup-move code
+ * can get tested. */
+ tt_int_op(fetch_from_buf(tmp, 3000, buf), ==, 3000);
+ test_memeq(tmp, stuff, 3000);
+ buf_pullup(buf, 2048, 0);
+ assert_buf_ok(buf);
+ buf_get_first_chunk_data(buf, &cp, &sz);
+ tt_ptr_op(cp, !=, NULL);
+ tt_int_op(sz, >=, 2048);
+ test_memeq(cp, stuff+3000, 2048);
+ tt_int_op(3000, ==, buf_datalen(buf));
+ tt_int_op(fetch_from_buf(tmp, 3000, buf), ==, 0);
+ test_memeq(tmp, stuff+3000, 2048);
+
+ buf_free(buf);
+
+ /* Now try the large-chunk case. */
+ buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
+ write_to_buf(stuff, 4000, buf);
+ write_to_buf(stuff+4000, 4000, buf);
+ write_to_buf(stuff+8000, 4000, buf);
+ write_to_buf(stuff+12000, 4000, buf);
+ tt_int_op(buf_datalen(buf), ==, 16000);
+ buf_get_first_chunk_data(buf, &cp, &sz);
+ tt_ptr_op(cp, !=, NULL);
+ tt_int_op(sz, <=, 4096);
+
+ buf_pullup(buf, 12500, 0);
+ assert_buf_ok(buf);
+ buf_get_first_chunk_data(buf, &cp, &sz);
+ tt_ptr_op(cp, !=, NULL);
+ tt_int_op(sz, >=, 12500);
+ test_memeq(cp, stuff, 12500);
+ tt_int_op(buf_datalen(buf), ==, 16000);
+
+ fetch_from_buf(tmp, 12400, buf);
+ test_memeq(tmp, stuff, 12400);
+ tt_int_op(buf_datalen(buf), ==, 3600);
+ fetch_from_buf(tmp, 3500, buf);
+ test_memeq(tmp, stuff+12400, 3500);
+ fetch_from_buf(tmp, 100, buf);
+ test_memeq(tmp, stuff+15900, 10);
+
+ buf_free(buf);
+
+ /* Make sure that the pull-up-whole-buffer case works */
+ buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
+ write_to_buf(stuff, 4000, buf);
+ write_to_buf(stuff+4000, 4000, buf);
+ fetch_from_buf(tmp, 100, buf); /* dump 100 bytes from first chunk */
+ buf_pullup(buf, 16000, 0); /* Way too much. */
+ assert_buf_ok(buf);
+ buf_get_first_chunk_data(buf, &cp, &sz);
+ tt_ptr_op(cp, !=, NULL);
+ tt_int_op(sz, ==, 7900);
+ test_memeq(cp, stuff+100, 7900);
+
+ buf_free(buf);
+ buf = NULL;
+
+ buf_shrink_freelists(1);
+
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+ done:
+ buf_free(buf);
+ buf_shrink_freelists(1);
+ tor_free(stuff);
+ tor_free(tmp);
+}
+
static void
test_buffer_copy(void *arg)
{
@@ -257,6 +370,7 @@ test_buffer_copy(void *arg)
generic_buffer_free(buf);
if (buf2)
generic_buffer_free(buf2);
+ buf_shrink_freelists(1);
}
static void
@@ -331,12 +445,157 @@ test_buffer_ext_or_cmd(void *arg)
ext_or_cmd_free(cmd);
generic_buffer_free(buf);
tor_free(tmp);
+ buf_shrink_freelists(1);
+}
+
+static void
+test_buffer_allocation_tracking(void *arg)
+{
+ char *junk = tor_malloc(16384);
+ buf_t *buf1 = NULL, *buf2 = NULL;
+ int i;
+
+ (void)arg;
+
+ crypto_rand(junk, 16384);
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+
+ buf1 = buf_new();
+ tt_assert(buf1);
+ buf2 = buf_new();
+ tt_assert(buf2);
+
+ tt_int_op(buf_allocation(buf1), ==, 0);
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+
+ write_to_buf(junk, 4000, buf1);
+ write_to_buf(junk, 4000, buf1);
+ write_to_buf(junk, 4000, buf1);
+ write_to_buf(junk, 4000, buf1);
+ tt_int_op(buf_allocation(buf1), ==, 16384);
+ fetch_from_buf(junk, 100, buf1);
+ tt_int_op(buf_allocation(buf1), ==, 16384); /* still 4 4k chunks */
+
+ tt_int_op(buf_get_total_allocation(), ==, 16384);
+
+ fetch_from_buf(junk, 4096, buf1); /* drop a 1k chunk... */
+ tt_int_op(buf_allocation(buf1), ==, 3*4096); /* now 3 4k chunks */
+
+ tt_int_op(buf_get_total_allocation(), ==, 16384); /* that chunk went onto
+ the freelist. */
+
+ write_to_buf(junk, 4000, buf2);
+ tt_int_op(buf_allocation(buf2), ==, 4096); /* another 4k chunk. */
+ tt_int_op(buf_get_total_allocation(), ==, 16384); /* that chunk came from
+ the freelist. */
+ write_to_buf(junk, 4000, buf2);
+ tt_int_op(buf_allocation(buf2), ==, 8192); /* another 4k chunk. */
+ tt_int_op(buf_get_total_allocation(), ==, 5*4096); /* that chunk was new. */
+
+
+ /* Make a really huge buffer */
+ for (i = 0; i < 1000; ++i) {
+ write_to_buf(junk, 4000, buf2);
+ }
+ tt_int_op(buf_allocation(buf2), >=, 4008000);
+ tt_int_op(buf_get_total_allocation(), >=, 4008000);
+ buf_free(buf2);
+ buf2 = NULL;
+
+ tt_int_op(buf_get_total_allocation(), <, 4008000);
+ buf_shrink_freelists(1);
+ tt_int_op(buf_get_total_allocation(), ==, buf_allocation(buf1));
+ buf_free(buf1);
+ buf1 = NULL;
+ buf_shrink_freelists(1);
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+
+ done:
+ buf_free(buf1);
+ buf_free(buf2);
+ buf_shrink_freelists(1);
+}
+
+static void
+test_buffer_time_tracking(void *arg)
+{
+ buf_t *buf=NULL, *buf2=NULL;
+ struct timeval tv0;
+ const time_t START = 1389288246;
+ const uint32_t START_MSEC = (uint32_t) ((uint64_t)START * 1000);
+ int i;
+ char tmp[4096];
+ (void)arg;
+
+ crypto_rand(tmp, sizeof(tmp));
+
+ tv0.tv_sec = START;
+ tv0.tv_usec = 0;
+
+ buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
+ tt_assert(buf);
+
+ /* Empty buffer means the timestamp is 0. */
+ tt_int_op(0, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC));
+ tt_int_op(0, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
+
+ tor_gettimeofday_cache_set(&tv0);
+ write_to_buf("ABCDEFG", 7, buf);
+ tt_int_op(1000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
+
+ buf2 = buf_copy(buf);
+ tt_assert(buf2);
+ tt_int_op(1234, ==, buf_get_oldest_chunk_timestamp(buf2, START_MSEC+1234));
+
+ /* Now add more bytes; enough to overflow the first chunk. */
+ tv0.tv_usec += 123 * 1000;
+ tor_gettimeofday_cache_set(&tv0);
+ for (i = 0; i < 600; ++i)
+ write_to_buf("ABCDEFG", 7, buf);
+ tt_int_op(4207, ==, buf_datalen(buf));
+
+ /* The oldest bytes are still in the front. */
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
+
+ /* Once those bytes are dropped, the chunk is still on the first
+ * timestamp. */
+ fetch_from_buf(tmp, 100, buf);
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
+
+ /* But once we discard the whole first chunk, we get the data in the second
+ * chunk. */
+ fetch_from_buf(tmp, 4000, buf);
+ tt_int_op(107, ==, buf_datalen(buf));
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
+
+ /* This time we'll be grabbing a chunk from the freelist, and making sure
+ its time gets updated */
+ tv0.tv_sec += 5;
+ tv0.tv_usec = 617*1000;
+ tor_gettimeofday_cache_set(&tv0);
+ for (i = 0; i < 600; ++i)
+ write_to_buf("ABCDEFG", 7, buf);
+ tt_int_op(4307, ==, buf_datalen(buf));
+
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
+ fetch_from_buf(tmp, 4000, buf);
+ fetch_from_buf(tmp, 306, buf);
+ tt_int_op(0, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+5617));
+ tt_int_op(383, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+6000));
+
+ done:
+ buf_free(buf);
+ buf_free(buf2);
}
struct testcase_t buffer_tests[] = {
- { "basic", test_buffers_basic, 0, NULL, NULL },
- { "copy", test_buffer_copy, 0, NULL, NULL },
- { "ext_or_cmd", test_buffer_ext_or_cmd, 0, NULL, NULL },
+ { "basic", test_buffers_basic, TT_FORK, NULL, NULL },
+ { "copy", test_buffer_copy, TT_FORK, NULL, NULL },
+ { "pullup", test_buffer_pullup, TT_FORK, NULL, NULL },
+ { "ext_or_cmd", test_buffer_ext_or_cmd, TT_FORK, NULL, NULL },
+ { "allocation_tracking", test_buffer_allocation_tracking, TT_FORK,
+ NULL, NULL },
+ { "time_tracking", test_buffer_time_tracking, TT_FORK, NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/test_oom.c b/src/test/test_oom.c
new file mode 100644
index 000000000..cc6e53235
--- /dev/null
+++ b/src/test/test_oom.c
@@ -0,0 +1,348 @@
+/* Copyright (c) 2014, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/* Unit tests for OOM handling logic */
+
+#define RELAY_PRIVATE
+#define BUFFERS_PRIVATE
+#define CIRCUITLIST_PRIVATE
+#include "or.h"
+#include "buffers.h"
+#include "circuitlist.h"
+#include "compat_libevent.h"
+#include "connection.h"
+#include "config.h"
+#include "mempool.h"
+#include "relay.h"
+#include "test.h"
+
+/* small replacement mock for circuit_mark_for_close_ to avoid doing all
+ * the other bookkeeping that comes with marking circuits. */
+static void
+circuit_mark_for_close_dummy_(circuit_t *circ, int reason, int line,
+ const char *file)
+{
+ (void) reason;
+ if (circ->marked_for_close) {
+ TT_FAIL(("Circuit already marked for close at %s:%d, but we are marking "
+ "it again at %s:%d",
+ circ->marked_for_close_file, (int)circ->marked_for_close,
+ file, line));
+ }
+
+ circ->marked_for_close = line;
+ circ->marked_for_close_file = file;
+}
+
+static circuit_t *
+dummy_or_circuit_new(int n_p_cells, int n_n_cells)
+{
+ or_circuit_t *circ = or_circuit_new(0, NULL);
+ int i;
+ cell_t cell;
+
+ for (i=0; i < n_p_cells; ++i) {
+ crypto_rand((void*)&cell, sizeof(cell));
+ cell_queue_append_packed_copy(TO_CIRCUIT(circ), &circ->p_chan_cells,
+ 0, &cell, 1, 0);
+ }
+
+ for (i=0; i < n_n_cells; ++i) {
+ crypto_rand((void*)&cell, sizeof(cell));
+ cell_queue_append_packed_copy(TO_CIRCUIT(circ),
+ &TO_CIRCUIT(circ)->n_chan_cells,
+ 1, &cell, 1, 0);
+ }
+
+ TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_OR;
+ return TO_CIRCUIT(circ);
+}
+
+static circuit_t *
+dummy_origin_circuit_new(int n_cells)
+{
+ origin_circuit_t *circ = origin_circuit_new();
+ int i;
+ cell_t cell;
+
+ for (i=0; i < n_cells; ++i) {
+ crypto_rand((void*)&cell, sizeof(cell));
+ cell_queue_append_packed_copy(TO_CIRCUIT(circ),
+ &TO_CIRCUIT(circ)->n_chan_cells,
+ 1, &cell, 1, 0);
+ }
+
+ TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
+ return TO_CIRCUIT(circ);
+}
+
+static void
+add_bytes_to_buf(generic_buffer_t *buf, size_t n_bytes)
+{
+ char b[3000];
+
+ while (n_bytes) {
+ size_t this_add = n_bytes > sizeof(buf) ? sizeof(buf) : n_bytes;
+ crypto_rand(b, sizeof(b));
+ generic_buffer_add(buf, b, this_add);
+ n_bytes -= this_add;
+ }
+}
+
+static edge_connection_t *
+dummy_edge_conn_new(circuit_t *circ,
+ int type, size_t in_bytes, size_t out_bytes)
+{
+ edge_connection_t *conn;
+
+ if (type == CONN_TYPE_EXIT)
+ conn = edge_connection_new(type, AF_INET);
+ else
+ conn = ENTRY_TO_EDGE_CONN(entry_connection_new(type, AF_INET));
+
+ /* We add these bytes directly to the buffers, to avoid all the
+ * edge connection read/write machinery. */
+ add_bytes_to_buf(TO_CONN(conn)->inbuf, in_bytes);
+ add_bytes_to_buf(TO_CONN(conn)->outbuf, out_bytes);
+
+ conn->on_circuit = circ;
+ if (type == CONN_TYPE_EXIT) {
+ or_circuit_t *oc = TO_OR_CIRCUIT(circ);
+ conn->next_stream = oc->n_streams;
+ oc->n_streams = conn;
+ } else {
+ origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
+ conn->next_stream = oc->p_streams;
+ oc->p_streams = conn;
+ }
+
+ return conn;
+}
+
+/** Run unit tests for buffers.c */
+static void
+test_oom_circbuf(void *arg)
+{
+ or_options_t *options = get_options_mutable();
+ circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL;
+ struct timeval tv = { 1389631048, 0 };
+
+ (void) arg;
+
+ MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
+ init_cell_pool();
+
+ /* Far too low for real life. */
+ options->MaxMemInQueues = 256*packed_cell_mem_cost();
+ options->CellStatistics = 0;
+
+ tt_int_op(cell_queues_check_size(), ==, 0); /* We don't start out OOM. */
+ tt_int_op(cell_queues_get_total_allocation(), ==, 0);
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+
+ /* Now we're going to fake up some circuits and get them added to the global
+ circuit list. */
+ tv.tv_usec = 0;
+ tor_gettimeofday_cache_set(&tv);
+ c1 = dummy_origin_circuit_new(30);
+ tv.tv_usec = 10*1000;
+ tor_gettimeofday_cache_set(&tv);
+ c2 = dummy_or_circuit_new(20, 20);
+
+ tt_int_op(packed_cell_mem_cost(), ==,
+ sizeof(packed_cell_t) + MP_POOL_ITEM_OVERHEAD);
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * 70);
+ tt_int_op(cell_queues_check_size(), ==, 0); /* We are still not OOM */
+
+ tv.tv_usec = 20*1000;
+ tor_gettimeofday_cache_set(&tv);
+ c3 = dummy_or_circuit_new(100, 85);
+ tt_int_op(cell_queues_check_size(), ==, 0); /* We are still not OOM */
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * 255);
+
+ tv.tv_usec = 30*1000;
+ tor_gettimeofday_cache_set(&tv);
+ /* Adding this cell will trigger our OOM handler. */
+ c4 = dummy_or_circuit_new(2, 0);
+
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * 257);
+
+ tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
+
+ tt_assert(c1->marked_for_close);
+ tt_assert(! c2->marked_for_close);
+ tt_assert(! c3->marked_for_close);
+ tt_assert(! c4->marked_for_close);
+
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * (257 - 30));
+
+ circuit_free(c1);
+ tv.tv_usec = 0;
+ tor_gettimeofday_cache_set(&tv); /* go back in time */
+ c1 = dummy_or_circuit_new(90, 0);
+
+ tv.tv_usec = 40*1000; /* go back to the future */
+ tor_gettimeofday_cache_set(&tv);
+
+ tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
+
+ tt_assert(c1->marked_for_close);
+ tt_assert(! c2->marked_for_close);
+ tt_assert(! c3->marked_for_close);
+ tt_assert(! c4->marked_for_close);
+
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * (257 - 30));
+
+ done:
+ circuit_free(c1);
+ circuit_free(c2);
+ circuit_free(c3);
+ circuit_free(c4);
+
+ UNMOCK(circuit_mark_for_close_);
+}
+
+/** Run unit tests for buffers.c */
+static void
+test_oom_streambuf(void *arg)
+{
+ or_options_t *options = get_options_mutable();
+ circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL, *c5 = NULL;
+ struct timeval tv = { 1389641159, 0 };
+ uint32_t tvms;
+ int i;
+
+ (void) arg;
+
+ MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
+ init_cell_pool();
+
+ /* Far too low for real life. */
+ options->MaxMemInQueues = 81*packed_cell_mem_cost() + 4096 * 34;
+ options->CellStatistics = 0;
+
+ tt_int_op(cell_queues_check_size(), ==, 0); /* We don't start out OOM. */
+ tt_int_op(cell_queues_get_total_allocation(), ==, 0);
+ tt_int_op(buf_get_total_allocation(), ==, 0);
+
+ /* Start all circuits with a bit of data queued in cells */
+ tv.tv_usec = 500*1000; /* go halfway into the second. */
+ tor_gettimeofday_cache_set(&tv);
+ c1 = dummy_or_circuit_new(10,10);
+ tv.tv_usec = 510*1000;
+ tor_gettimeofday_cache_set(&tv);
+ c2 = dummy_origin_circuit_new(20);
+ tv.tv_usec = 520*1000;
+ tor_gettimeofday_cache_set(&tv);
+ c3 = dummy_or_circuit_new(20,20);
+ tv.tv_usec = 530*1000;
+ tor_gettimeofday_cache_set(&tv);
+ c4 = dummy_or_circuit_new(0,0);
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * 80);
+
+ tv.tv_usec = 600*1000;
+ tor_gettimeofday_cache_set(&tv);
+
+ /* Add some connections to c1...c4. */
+ for (i = 0; i < 4; ++i) {
+ edge_connection_t *ec;
+ /* link it to a circuit */
+ tv.tv_usec += 10*1000;
+ tor_gettimeofday_cache_set(&tv);
+ ec = dummy_edge_conn_new(c1, CONN_TYPE_EXIT, 1000, 1000);
+ tt_assert(ec);
+ tv.tv_usec += 10*1000;
+ tor_gettimeofday_cache_set(&tv);
+ ec = dummy_edge_conn_new(c2, CONN_TYPE_AP, 1000, 1000);
+ tt_assert(ec);
+ tv.tv_usec += 10*1000;
+ tor_gettimeofday_cache_set(&tv);
+ ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); /* Yes, 4 twice*/
+ tt_assert(ec);
+ tv.tv_usec += 10*1000;
+ tor_gettimeofday_cache_set(&tv);
+ ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
+ tt_assert(ec);
+ }
+
+ tv.tv_sec += 1;
+ tv.tv_usec = 0;
+ tvms = (uint32_t) tv_to_msec(&tv);
+
+ tt_int_op(circuit_max_queued_cell_age(c1, tvms), ==, 500);
+ tt_int_op(circuit_max_queued_cell_age(c2, tvms), ==, 490);
+ tt_int_op(circuit_max_queued_cell_age(c3, tvms), ==, 480);
+ tt_int_op(circuit_max_queued_cell_age(c4, tvms), ==, 0);
+
+ tt_int_op(circuit_max_queued_data_age(c1, tvms), ==, 390);
+ tt_int_op(circuit_max_queued_data_age(c2, tvms), ==, 380);
+ tt_int_op(circuit_max_queued_data_age(c3, tvms), ==, 0);
+ tt_int_op(circuit_max_queued_data_age(c4, tvms), ==, 370);
+
+ tt_int_op(circuit_max_queued_item_age(c1, tvms), ==, 500);
+ tt_int_op(circuit_max_queued_item_age(c2, tvms), ==, 490);
+ tt_int_op(circuit_max_queued_item_age(c3, tvms), ==, 480);
+ tt_int_op(circuit_max_queued_item_age(c4, tvms), ==, 370);
+
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * 80);
+ tt_int_op(buf_get_total_allocation(), ==, 4096*16*2);
+
+ /* Now give c4 a very old buffer of modest size */
+ {
+ edge_connection_t *ec;
+ tv.tv_sec -= 1;
+ tv.tv_usec = 0;
+ tor_gettimeofday_cache_set(&tv);
+ ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
+ tt_assert(ec);
+ }
+ tt_int_op(buf_get_total_allocation(), ==, 4096*17*2);
+ tt_int_op(circuit_max_queued_item_age(c4, tvms), ==, 1000);
+
+ tt_int_op(cell_queues_check_size(), ==, 0);
+
+ /* And run over the limit. */
+ tv.tv_usec = 800*1000;
+ tor_gettimeofday_cache_set(&tv);
+ c5 = dummy_or_circuit_new(0,5);
+
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * 85);
+ tt_int_op(buf_get_total_allocation(), ==, 4096*17*2);
+
+ tt_int_op(cell_queues_check_size(), ==, 1); /* We are now OOM */
+
+ /* C4 should have died. */
+ tt_assert(! c1->marked_for_close);
+ tt_assert(! c2->marked_for_close);
+ tt_assert(! c3->marked_for_close);
+ tt_assert(c4->marked_for_close);
+ tt_assert(! c5->marked_for_close);
+
+ tt_int_op(cell_queues_get_total_allocation(), ==,
+ packed_cell_mem_cost() * 85);
+ tt_int_op(buf_get_total_allocation(), ==, 4096*8*2);
+
+ done:
+ circuit_free(c1);
+ circuit_free(c2);
+ circuit_free(c3);
+ circuit_free(c4);
+ circuit_free(c5);
+
+ UNMOCK(circuit_mark_for_close_);
+}
+
+struct testcase_t oom_tests[] = {
+ { "circbuf", test_oom_circbuf, TT_FORK, NULL, NULL },
+ { "streambuf", test_oom_streambuf, TT_FORK, NULL, NULL },
+ END_OF_TESTCASES
+};
+