1 /* Copyright (c) 2019-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Fake circuits API for unit test.
9 #define CIRCUITBUILD_PRIVATE
10 #define CIRCUITLIST_PRIVATE
11 #define CRYPT_PATH_PRIVATE
13 #include "core/or/or.h"
15 #include "core/crypto/relay_crypto.h"
16 #include "core/or/channel.h"
17 #include "core/or/circuitbuild.h"
18 #include "core/or/circuitlist.h"
19 #include "core/or/circuitpadding.h"
20 #include "core/or/congestion_control_common.h"
21 #include "core/or/conflux_pool.h"
22 #include "core/or/conflux.h"
23 #include "core/or/crypt_path.h"
24 #include "core/or/relay.h"
25 #include "core/or/relay_crypto_st.h"
27 #include "test/fakecircs.h"
29 /** Return newly allocated OR circuit using the given nchan and pchan. It must
30 * be freed with the free_fake_orcirc(). */
32 new_fake_orcirc(channel_t
*nchan
, channel_t
*pchan
)
34 or_circuit_t
*orcirc
= NULL
;
35 circuit_t
*circ
= NULL
;
36 crypt_path_t tmp_cpath
;
37 char whatevs_key
[CPATH_KEY_MATERIAL_LEN
];
39 orcirc
= tor_malloc_zero(sizeof(*orcirc
));
40 circ
= &(orcirc
->base_
);
41 circ
->magic
= OR_CIRCUIT_MAGIC
;
43 circuit_set_n_circid_chan(circ
, get_unique_circ_id_by_chan(nchan
), nchan
);
44 cell_queue_init(&(circ
->n_chan_cells
));
47 circ
->circuit_blocked_on_n_chan
= 0;
48 circ
->circuit_blocked_on_p_chan
= 0;
49 circ
->n_delete_pending
= 0;
50 circ
->p_delete_pending
= 0;
51 circ
->received_destroy
= 0;
52 circ
->state
= CIRCUIT_STATE_OPEN
;
53 circ
->purpose
= CIRCUIT_PURPOSE_OR
;
54 circ
->package_window
= CIRCWINDOW_START_MAX
;
55 circ
->deliver_window
= CIRCWINDOW_START_MAX
;
56 circ
->n_chan_create_cell
= NULL
;
58 circuit_set_p_circid_chan(orcirc
, get_unique_circ_id_by_chan(pchan
), pchan
);
59 cell_queue_init(&(orcirc
->p_chan_cells
));
61 memset(&tmp_cpath
, 0, sizeof(tmp_cpath
));
62 if (cpath_init_circuit_crypto(&tmp_cpath
, whatevs_key
,
63 sizeof(whatevs_key
), 0, 0)<0) {
64 log_warn(LD_BUG
,"Circuit initialization failed");
67 orcirc
->crypto
= tmp_cpath
.pvt_crypto
;
72 /** Free fake OR circuit which MUST be created by new_fake_orcirc(). */
74 free_fake_orcirc(or_circuit_t
*orcirc
)
80 circuit_t
*circ
= TO_CIRCUIT(orcirc
);
82 relay_crypto_clear(&orcirc
->crypto
);
84 circpad_circuit_free_all_machineinfos(circ
);
86 if (orcirc
->p_chan
&& orcirc
->p_chan
->cmux
) {
87 circuitmux_detach_circuit(orcirc
->p_chan
->cmux
, circ
);
89 if (circ
->n_chan
&& circ
->n_chan
->cmux
) {
90 circuitmux_detach_circuit(circ
->n_chan
->cmux
, circ
);
93 conflux_circuit_about_to_free(circ
);
94 congestion_control_free(circ
->ccontrol
);