changelog typo fix
[tor.git] / src / test / test_oom.c
blob2569b6e00f769f7f217960b3aad4150fe0c85df2
1 /* Copyright (c) 2014-2016, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /* Unit tests for OOM handling logic */
6 #define RELAY_PRIVATE
7 #define BUFFERS_PRIVATE
8 #define CIRCUITLIST_PRIVATE
9 #define CONNECTION_PRIVATE
10 #include "or.h"
11 #include "buffers.h"
12 #include "circuitlist.h"
13 #include "compat_libevent.h"
14 #include "connection.h"
15 #include "config.h"
16 #include "relay.h"
17 #include "test.h"
19 /* small replacement mock for circuit_mark_for_close_ to avoid doing all
20 * the other bookkeeping that comes with marking circuits. */
21 static void
22 circuit_mark_for_close_dummy_(circuit_t *circ, int reason, int line,
23 const char *file)
25 (void) reason;
26 if (circ->marked_for_close) {
27 TT_FAIL(("Circuit already marked for close at %s:%d, but we are marking "
28 "it again at %s:%d",
29 circ->marked_for_close_file, (int)circ->marked_for_close,
30 file, line));
33 circ->marked_for_close = line;
34 circ->marked_for_close_file = file;
37 static circuit_t *
38 dummy_or_circuit_new(int n_p_cells, int n_n_cells)
40 or_circuit_t *circ = or_circuit_new(0, NULL);
41 int i;
42 cell_t cell;
44 for (i=0; i < n_p_cells; ++i) {
45 crypto_rand((void*)&cell, sizeof(cell));
46 cell_queue_append_packed_copy(TO_CIRCUIT(circ), &circ->p_chan_cells,
47 0, &cell, 1, 0);
50 for (i=0; i < n_n_cells; ++i) {
51 crypto_rand((void*)&cell, sizeof(cell));
52 cell_queue_append_packed_copy(TO_CIRCUIT(circ),
53 &TO_CIRCUIT(circ)->n_chan_cells,
54 1, &cell, 1, 0);
57 TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_OR;
58 return TO_CIRCUIT(circ);
61 static circuit_t *
62 dummy_origin_circuit_new(int n_cells)
64 origin_circuit_t *circ = origin_circuit_new();
65 int i;
66 cell_t cell;
68 for (i=0; i < n_cells; ++i) {
69 crypto_rand((void*)&cell, sizeof(cell));
70 cell_queue_append_packed_copy(TO_CIRCUIT(circ),
71 &TO_CIRCUIT(circ)->n_chan_cells,
72 1, &cell, 1, 0);
75 TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
76 return TO_CIRCUIT(circ);
79 static void
80 add_bytes_to_buf(generic_buffer_t *buf, size_t n_bytes)
82 char b[3000];
84 while (n_bytes) {
85 size_t this_add = n_bytes > sizeof(b) ? sizeof(b) : n_bytes;
86 crypto_rand(b, this_add);
87 generic_buffer_add(buf, b, this_add);
88 n_bytes -= this_add;
92 static edge_connection_t *
93 dummy_edge_conn_new(circuit_t *circ,
94 int type, size_t in_bytes, size_t out_bytes)
96 edge_connection_t *conn;
97 generic_buffer_t *inbuf, *outbuf;
99 if (type == CONN_TYPE_EXIT)
100 conn = edge_connection_new(type, AF_INET);
101 else
102 conn = ENTRY_TO_EDGE_CONN(entry_connection_new(type, AF_INET));
104 #ifdef USE_BUFFEREVENTS
105 inbuf = bufferevent_get_input(TO_CONN(conn)->bufev);
106 outbuf = bufferevent_get_output(TO_CONN(conn)->bufev);
107 #else
108 inbuf = TO_CONN(conn)->inbuf;
109 outbuf = TO_CONN(conn)->outbuf;
110 #endif
112 /* We add these bytes directly to the buffers, to avoid all the
113 * edge connection read/write machinery. */
114 add_bytes_to_buf(inbuf, in_bytes);
115 add_bytes_to_buf(outbuf, out_bytes);
117 conn->on_circuit = circ;
118 if (type == CONN_TYPE_EXIT) {
119 or_circuit_t *oc = TO_OR_CIRCUIT(circ);
120 conn->next_stream = oc->n_streams;
121 oc->n_streams = conn;
122 } else {
123 origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
124 conn->next_stream = oc->p_streams;
125 oc->p_streams = conn;
128 return conn;
131 /** Run unit tests for buffers.c */
132 static void
133 test_oom_circbuf(void *arg)
135 or_options_t *options = get_options_mutable();
136 circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL;
137 struct timeval tv = { 1389631048, 0 };
139 (void) arg;
141 MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
143 /* Far too low for real life. */
144 options->MaxMemInQueues = 256*packed_cell_mem_cost();
145 options->CellStatistics = 0;
147 tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We don't start out OOM. */
148 tt_int_op(cell_queues_get_total_allocation(), OP_EQ, 0);
149 tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
151 /* Now we're going to fake up some circuits and get them added to the global
152 circuit list. */
153 tv.tv_usec = 0;
154 tor_gettimeofday_cache_set(&tv);
155 c1 = dummy_origin_circuit_new(30);
156 tv.tv_usec = 10*1000;
157 tor_gettimeofday_cache_set(&tv);
158 c2 = dummy_or_circuit_new(20, 20);
160 tt_int_op(packed_cell_mem_cost(), OP_EQ,
161 sizeof(packed_cell_t));
162 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
163 packed_cell_mem_cost() * 70);
164 tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We are still not OOM */
166 tv.tv_usec = 20*1000;
167 tor_gettimeofday_cache_set(&tv);
168 c3 = dummy_or_circuit_new(100, 85);
169 tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We are still not OOM */
170 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
171 packed_cell_mem_cost() * 255);
173 tv.tv_usec = 30*1000;
174 tor_gettimeofday_cache_set(&tv);
175 /* Adding this cell will trigger our OOM handler. */
176 c4 = dummy_or_circuit_new(2, 0);
178 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
179 packed_cell_mem_cost() * 257);
181 tt_int_op(cell_queues_check_size(), OP_EQ, 1); /* We are now OOM */
183 tt_assert(c1->marked_for_close);
184 tt_assert(! c2->marked_for_close);
185 tt_assert(! c3->marked_for_close);
186 tt_assert(! c4->marked_for_close);
188 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
189 packed_cell_mem_cost() * (257 - 30));
191 circuit_free(c1);
192 tv.tv_usec = 0;
193 tor_gettimeofday_cache_set(&tv); /* go back in time */
194 c1 = dummy_or_circuit_new(90, 0);
196 tv.tv_usec = 40*1000; /* go back to the future */
197 tor_gettimeofday_cache_set(&tv);
199 tt_int_op(cell_queues_check_size(), OP_EQ, 1); /* We are now OOM */
201 tt_assert(c1->marked_for_close);
202 tt_assert(! c2->marked_for_close);
203 tt_assert(! c3->marked_for_close);
204 tt_assert(! c4->marked_for_close);
206 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
207 packed_cell_mem_cost() * (257 - 30));
209 done:
210 circuit_free(c1);
211 circuit_free(c2);
212 circuit_free(c3);
213 circuit_free(c4);
215 UNMOCK(circuit_mark_for_close_);
218 /** Run unit tests for buffers.c */
219 static void
220 test_oom_streambuf(void *arg)
222 or_options_t *options = get_options_mutable();
223 circuit_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *c4 = NULL, *c5 = NULL;
224 struct timeval tv = { 1389641159, 0 };
225 uint32_t tvms;
226 int i;
227 smartlist_t *edgeconns = smartlist_new();
229 (void) arg;
231 MOCK(circuit_mark_for_close_, circuit_mark_for_close_dummy_);
233 /* Far too low for real life. */
234 options->MaxMemInQueues = 81*packed_cell_mem_cost() + 4096 * 34;
235 options->CellStatistics = 0;
237 tt_int_op(cell_queues_check_size(), OP_EQ, 0); /* We don't start out OOM. */
238 tt_int_op(cell_queues_get_total_allocation(), OP_EQ, 0);
239 tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
241 /* Start all circuits with a bit of data queued in cells */
242 tv.tv_usec = 500*1000; /* go halfway into the second. */
243 tor_gettimeofday_cache_set(&tv);
244 c1 = dummy_or_circuit_new(10,10);
245 tv.tv_usec = 510*1000;
246 tor_gettimeofday_cache_set(&tv);
247 c2 = dummy_origin_circuit_new(20);
248 tv.tv_usec = 520*1000;
249 tor_gettimeofday_cache_set(&tv);
250 c3 = dummy_or_circuit_new(20,20);
251 tv.tv_usec = 530*1000;
252 tor_gettimeofday_cache_set(&tv);
253 c4 = dummy_or_circuit_new(0,0);
254 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
255 packed_cell_mem_cost() * 80);
257 tv.tv_usec = 600*1000;
258 tor_gettimeofday_cache_set(&tv);
260 /* Add some connections to c1...c4. */
261 for (i = 0; i < 4; ++i) {
262 edge_connection_t *ec;
263 /* link it to a circuit */
264 tv.tv_usec += 10*1000;
265 tor_gettimeofday_cache_set(&tv);
266 ec = dummy_edge_conn_new(c1, CONN_TYPE_EXIT, 1000, 1000);
267 tt_assert(ec);
268 smartlist_add(edgeconns, ec);
269 tv.tv_usec += 10*1000;
270 tor_gettimeofday_cache_set(&tv);
271 ec = dummy_edge_conn_new(c2, CONN_TYPE_AP, 1000, 1000);
272 tt_assert(ec);
273 smartlist_add(edgeconns, ec);
274 tv.tv_usec += 10*1000;
275 tor_gettimeofday_cache_set(&tv);
276 ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); /* Yes, 4 twice*/
277 tt_assert(ec);
278 smartlist_add(edgeconns, ec);
279 tv.tv_usec += 10*1000;
280 tor_gettimeofday_cache_set(&tv);
281 ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
282 smartlist_add(edgeconns, ec);
283 tt_assert(ec);
286 tv.tv_sec += 1;
287 tv.tv_usec = 0;
288 tvms = (uint32_t) tv_to_msec(&tv);
290 tt_int_op(circuit_max_queued_cell_age(c1, tvms), OP_EQ, 500);
291 tt_int_op(circuit_max_queued_cell_age(c2, tvms), OP_EQ, 490);
292 tt_int_op(circuit_max_queued_cell_age(c3, tvms), OP_EQ, 480);
293 tt_int_op(circuit_max_queued_cell_age(c4, tvms), OP_EQ, 0);
295 tt_int_op(circuit_max_queued_data_age(c1, tvms), OP_EQ, 390);
296 tt_int_op(circuit_max_queued_data_age(c2, tvms), OP_EQ, 380);
297 tt_int_op(circuit_max_queued_data_age(c3, tvms), OP_EQ, 0);
298 tt_int_op(circuit_max_queued_data_age(c4, tvms), OP_EQ, 370);
300 tt_int_op(circuit_max_queued_item_age(c1, tvms), OP_EQ, 500);
301 tt_int_op(circuit_max_queued_item_age(c2, tvms), OP_EQ, 490);
302 tt_int_op(circuit_max_queued_item_age(c3, tvms), OP_EQ, 480);
303 tt_int_op(circuit_max_queued_item_age(c4, tvms), OP_EQ, 370);
305 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
306 packed_cell_mem_cost() * 80);
307 tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*16*2);
309 /* Now give c4 a very old buffer of modest size */
311 edge_connection_t *ec;
312 tv.tv_sec -= 1;
313 tv.tv_usec = 0;
314 tor_gettimeofday_cache_set(&tv);
315 ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
316 tt_assert(ec);
317 smartlist_add(edgeconns, ec);
319 tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*17*2);
320 tt_int_op(circuit_max_queued_item_age(c4, tvms), OP_EQ, 1000);
322 tt_int_op(cell_queues_check_size(), OP_EQ, 0);
324 /* And run over the limit. */
325 tv.tv_usec = 800*1000;
326 tor_gettimeofday_cache_set(&tv);
327 c5 = dummy_or_circuit_new(0,5);
329 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
330 packed_cell_mem_cost() * 85);
331 tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*17*2);
333 tt_int_op(cell_queues_check_size(), OP_EQ, 1); /* We are now OOM */
335 /* C4 should have died. */
336 tt_assert(! c1->marked_for_close);
337 tt_assert(! c2->marked_for_close);
338 tt_assert(! c3->marked_for_close);
339 tt_assert(c4->marked_for_close);
340 tt_assert(! c5->marked_for_close);
342 tt_int_op(cell_queues_get_total_allocation(), OP_EQ,
343 packed_cell_mem_cost() * 85);
344 tt_int_op(buf_get_total_allocation(), OP_EQ, 4096*8*2);
346 done:
347 circuit_free(c1);
348 circuit_free(c2);
349 circuit_free(c3);
350 circuit_free(c4);
351 circuit_free(c5);
353 SMARTLIST_FOREACH(edgeconns, edge_connection_t *, ec,
354 connection_free_(TO_CONN(ec)));
355 smartlist_free(edgeconns);
357 UNMOCK(circuit_mark_for_close_);
360 struct testcase_t oom_tests[] = {
361 { "circbuf", test_oom_circbuf, TT_FORK, NULL, NULL },
362 { "streambuf", test_oom_streambuf, TT_FORK, NULL, NULL },
363 END_OF_TESTCASES