Prop210: Refactor connection_get_* to produce lists and counts
[tor.git] / src / test / test_relay.c
blob6081956d46cf144f04d4bccdf689ee4c27519110
1 /* Copyright (c) 2014-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "or.h"
5 #define CIRCUITBUILD_PRIVATE
6 #include "circuitbuild.h"
7 #define RELAY_PRIVATE
8 #include "relay.h"
9 /* For init/free stuff */
10 #include "scheduler.h"
12 /* Test suite stuff */
13 #include "test.h"
14 #include "fakechans.h"
16 static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
18 static void test_relay_append_cell_to_circuit_queue(void *arg);
20 static or_circuit_t *
21 new_fake_orcirc(channel_t *nchan, channel_t *pchan)
23 or_circuit_t *orcirc = NULL;
24 circuit_t *circ = NULL;
26 orcirc = tor_malloc_zero(sizeof(*orcirc));
27 circ = &(orcirc->base_);
28 circ->magic = OR_CIRCUIT_MAGIC;
30 circ->n_chan = nchan;
31 circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
32 circ->n_mux = NULL; /* ?? */
33 cell_queue_init(&(circ->n_chan_cells));
34 circ->n_hop = NULL;
35 circ->streams_blocked_on_n_chan = 0;
36 circ->streams_blocked_on_p_chan = 0;
37 circ->n_delete_pending = 0;
38 circ->p_delete_pending = 0;
39 circ->received_destroy = 0;
40 circ->state = CIRCUIT_STATE_OPEN;
41 circ->purpose = CIRCUIT_PURPOSE_OR;
42 circ->package_window = CIRCWINDOW_START_MAX;
43 circ->deliver_window = CIRCWINDOW_START_MAX;
44 circ->n_chan_create_cell = NULL;
46 orcirc->p_chan = pchan;
47 orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
48 cell_queue_init(&(orcirc->p_chan_cells));
50 return orcirc;
53 static void
54 test_relay_append_cell_to_circuit_queue(void *arg)
56 channel_t *nchan = NULL, *pchan = NULL;
57 or_circuit_t *orcirc = NULL;
58 cell_t *cell = NULL;
59 int old_count, new_count;
61 (void)arg;
63 /* Make fake channels to be nchan and pchan for the circuit */
64 nchan = new_fake_channel();
65 tt_assert(nchan);
67 pchan = new_fake_channel();
68 tt_assert(pchan);
70 /* We'll need chans with working cmuxes */
71 nchan->cmux = circuitmux_alloc();
72 pchan->cmux = circuitmux_alloc();
74 /* Make a fake orcirc */
75 orcirc = new_fake_orcirc(nchan, pchan);
76 tt_assert(orcirc);
78 /* Make a cell */
79 cell = tor_malloc_zero(sizeof(cell_t));
80 make_fake_cell(cell);
82 MOCK(scheduler_channel_has_waiting_cells,
83 scheduler_channel_has_waiting_cells_mock);
85 /* Append it */
86 old_count = get_mock_scheduler_has_waiting_cells_count();
87 append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), nchan, cell,
88 CELL_DIRECTION_OUT, 0);
89 new_count = get_mock_scheduler_has_waiting_cells_count();
90 tt_int_op(new_count, ==, old_count + 1);
92 /* Now try the reverse direction */
93 old_count = get_mock_scheduler_has_waiting_cells_count();
94 append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), pchan, cell,
95 CELL_DIRECTION_IN, 0);
96 new_count = get_mock_scheduler_has_waiting_cells_count();
97 tt_int_op(new_count, ==, old_count + 1);
99 UNMOCK(scheduler_channel_has_waiting_cells);
101 /* Get rid of the fake channels */
102 MOCK(scheduler_release_channel, scheduler_release_channel_mock);
103 channel_mark_for_close(nchan);
104 channel_mark_for_close(pchan);
105 UNMOCK(scheduler_release_channel);
107 /* Shut down channels */
108 channel_free_all();
110 done:
111 tor_free(cell);
112 cell_queue_clear(&orcirc->base_.n_chan_cells);
113 cell_queue_clear(&orcirc->p_chan_cells);
114 tor_free(orcirc);
115 free_fake_channel(nchan);
116 free_fake_channel(pchan);
118 return;
121 struct testcase_t relay_tests[] = {
122 { "append_cell_to_circuit_queue", test_relay_append_cell_to_circuit_queue,
123 TT_FORK, NULL, NULL },
124 END_OF_TESTCASES