1 /* Copyright (c) 2014-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 #define CIRCUITBUILD_PRIVATE
6 #include "circuitbuild.h"
9 /* For init/free stuff */
10 #include "scheduler.h"
12 /* Test suite stuff */
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
);
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
;
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
));
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
));
54 test_relay_append_cell_to_circuit_queue(void *arg
)
56 channel_t
*nchan
= NULL
, *pchan
= NULL
;
57 or_circuit_t
*orcirc
= NULL
;
59 int old_count
, new_count
;
63 /* Make fake channels to be nchan and pchan for the circuit */
64 nchan
= new_fake_channel();
67 pchan
= new_fake_channel();
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
);
79 cell
= tor_malloc_zero(sizeof(cell_t
));
82 MOCK(scheduler_channel_has_waiting_cells
,
83 scheduler_channel_has_waiting_cells_mock
);
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 */
112 cell_queue_clear(&orcirc
->base_
.n_chan_cells
);
113 cell_queue_clear(&orcirc
->p_chan_cells
);
115 free_fake_channel(nchan
);
116 free_fake_channel(pchan
);
121 struct testcase_t relay_tests
[] = {
122 { "append_cell_to_circuit_queue", test_relay_append_cell_to_circuit_queue
,
123 TT_FORK
, NULL
, NULL
},