1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419 |
- #define TOR_CHANNEL_INTERNAL_
- #define TOR_TIMERS_PRIVATE
- #define CIRCUITPADDING_PRIVATE
- #define NETWORKSTATUS_PRIVATE
- #include "core/or/or.h"
- #include "test.h"
- #include "lib/testsupport/testsupport.h"
- #include "core/or/connection_or.h"
- #include "core/or/channel.h"
- #include "core/or/channeltls.h"
- #include <event.h>
- #include "lib/evloop/compat_libevent.h"
- #include "lib/time/compat_time.h"
- #include "lib/defs/time.h"
- #include "core/or/relay.h"
- #include "core/or/circuitlist.h"
- #include "core/or/circuitbuild.h"
- #include "core/or/circuitpadding.h"
- #include "core/mainloop/netstatus.h"
- #include "core/crypto/relay_crypto.h"
- #include "core/or/protover.h"
- #include "feature/nodelist/nodelist.h"
- #include "lib/evloop/compat_libevent.h"
- #include "app/config/config.h"
- #include "feature/nodelist/routerstatus_st.h"
- #include "feature/nodelist/networkstatus_st.h"
- #include "feature/nodelist/node_st.h"
- #include "core/or/cell_st.h"
- #include "core/or/crypt_path_st.h"
- #include "core/or/or_circuit_st.h"
- #include "core/or/origin_circuit_st.h"
- /* Start our monotime mocking at 1 second past whatever monotime_init()
- * thought the actual wall clock time was, for platforms with bad resolution
- * and weird timevalues during monotime_init() before mocking. */
- #define MONOTIME_MOCK_START (monotime_absolute_nsec()+\
- TOR_NSEC_PER_USEC*TOR_USEC_PER_SEC)
- extern smartlist_t *connection_array;
- circid_t get_unique_circ_id_by_chan(channel_t *chan);
- void helper_create_basic_machine(void);
- static void helper_create_conditional_machines(void);
- static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
- channel_t *new_fake_channel(void);
- void test_circuitpadding_negotiation(void *arg);
- void test_circuitpadding_wronghop(void *arg);
- void test_circuitpadding_conditions(void *arg);
- void test_circuitpadding_serialize(void *arg);
- void test_circuitpadding_rtt(void *arg);
- void test_circuitpadding_tokens(void *arg);
- static void
- simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
- int padding);
- void free_fake_orcirc(circuit_t *circ);
- void free_fake_origin_circuit(origin_circuit_t *circ);
- static int deliver_negotiated = 1;
- static int64_t curr_mocked_time;
- static node_t padding_node;
- static node_t non_padding_node;
- static channel_t dummy_channel;
- static circpad_machine_spec_t circ_client_machine;
- static void
- timers_advance_and_run(int64_t msec_update)
- {
- curr_mocked_time += msec_update*TOR_NSEC_PER_MSEC;
- monotime_coarse_set_mock_time_nsec(curr_mocked_time);
- monotime_set_mock_time_nsec(curr_mocked_time);
- timers_run_pending();
- }
- static void
- nodes_init(void)
- {
- padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
- padding_node.rs->pv.supports_padding = 1;
- non_padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
- non_padding_node.rs->pv.supports_padding = 0;
- }
- static void
- nodes_free(void)
- {
- tor_free(padding_node.rs);
- tor_free(non_padding_node.rs);
- }
- static const node_t *
- node_get_by_id_mock(const char *identity_digest)
- {
- if (identity_digest[0] == 1) {
- return &padding_node;
- } else if (identity_digest[0] == 0) {
- return &non_padding_node;
- }
- return NULL;
- }
- static or_circuit_t *
- new_fake_orcirc(channel_t *nchan, channel_t *pchan)
- {
- or_circuit_t *orcirc = NULL;
- circuit_t *circ = NULL;
- crypt_path_t tmp_cpath;
- char whatevs_key[CPATH_KEY_MATERIAL_LEN];
- orcirc = tor_malloc_zero(sizeof(*orcirc));
- circ = &(orcirc->base_);
- circ->magic = OR_CIRCUIT_MAGIC;
- //circ->n_chan = nchan;
- circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
- cell_queue_init(&(circ->n_chan_cells));
- circ->n_hop = NULL;
- circ->streams_blocked_on_n_chan = 0;
- circ->streams_blocked_on_p_chan = 0;
- circ->n_delete_pending = 0;
- circ->p_delete_pending = 0;
- circ->received_destroy = 0;
- circ->state = CIRCUIT_STATE_OPEN;
- circ->purpose = CIRCUIT_PURPOSE_OR;
- circ->package_window = CIRCWINDOW_START_MAX;
- circ->deliver_window = CIRCWINDOW_START_MAX;
- circ->n_chan_create_cell = NULL;
- //orcirc->p_chan = pchan;
- orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
- cell_queue_init(&(orcirc->p_chan_cells));
- circuit_set_p_circid_chan(orcirc, orcirc->p_circ_id, pchan);
- circuit_set_n_circid_chan(circ, circ->n_circ_id, nchan);
- memset(&tmp_cpath, 0, sizeof(tmp_cpath));
- if (circuit_init_cpath_crypto(&tmp_cpath, whatevs_key,
- sizeof(whatevs_key), 0, 0)<0) {
- log_warn(LD_BUG,"Circuit initialization failed");
- return NULL;
- }
- orcirc->crypto = tmp_cpath.crypto;
- return orcirc;
- }
- void
- free_fake_orcirc(circuit_t *circ)
- {
- or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
- relay_crypto_clear(&orcirc->crypto);
- circpad_circuit_free_all_machineinfos(circ);
- tor_free(circ);
- }
- void
- free_fake_origin_circuit(origin_circuit_t *circ)
- {
- circpad_circuit_free_all_machineinfos(TO_CIRCUIT(circ));
- circuit_clear_cpath(circ);
- tor_free(circ);
- }
- void dummy_nop_timer(void);
- //static int dont_stop_libevent = 0;
- static circuit_t *client_side;
- static circuit_t *relay_side;
- static int n_client_cells = 0;
- static int n_relay_cells = 0;
- static int
- circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
- cell_direction_t cell_direction,
- crypt_path_t *layer_hint, streamid_t on_stream,
- const char *filename, int lineno);
- static void
- circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
- cell_direction_t direction);
- static void
- circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
- cell_direction_t direction)
- {
- (void)cmux;
- (void)circ;
- (void)direction;
- return;
- }
- static int
- circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
- cell_direction_t cell_direction,
- crypt_path_t *layer_hint, streamid_t on_stream,
- const char *filename, int lineno)
- {
- (void)cell; (void)on_stream; (void)filename; (void)lineno;
- if (circ == client_side) {
- if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
- // Deliver to relay
- circpad_handle_padding_negotiate(relay_side, cell);
- } else {
- int is_target_hop = circpad_padding_is_from_expected_hop(circ,
- layer_hint);
- tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_OUT);
- tt_int_op(is_target_hop, OP_EQ, 1);
- // No need to pretend a padding cell was sent: This event is
- // now emitted internally when the circuitpadding code sends them.
- //circpad_cell_event_padding_sent(client_side);
- // Receive padding cell at middle
- circpad_deliver_recognized_relay_cell_events(relay_side,
- cell->payload[0], NULL);
- }
- n_client_cells++;
- } else if (circ == relay_side) {
- tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_IN);
- if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATED) {
- // XXX: blah need right layer_hint..
- if (deliver_negotiated)
- circpad_handle_padding_negotiated(client_side, cell,
- TO_ORIGIN_CIRCUIT(client_side)
- ->cpath->next);
- } else if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
- circpad_handle_padding_negotiate(client_side, cell);
- } else {
- // No need to pretend a padding cell was sent: This event is
- // now emitted internally when the circuitpadding code sends them.
- //circpad_cell_event_padding_sent(relay_side);
- // Receive padding cell at client
- circpad_deliver_recognized_relay_cell_events(client_side,
- cell->payload[0],
- TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
- }
- n_relay_cells++;
- }
- done:
- timers_advance_and_run(1);
- return 0;
- }
- // Test reading and writing padding to strings (or options_t + consensus)
- void
- test_circuitpadding_serialize(void *arg)
- {
- (void)arg;
- }
- static signed_error_t
- circpad_send_command_to_hop_mock(origin_circuit_t *circ, uint8_t hopnum,
- uint8_t relay_command, const uint8_t *payload,
- ssize_t payload_len)
- {
- (void) circ;
- (void) hopnum;
- (void) relay_command;
- (void) payload;
- (void) payload_len;
- return 0;
- }
- void
- test_circuitpadding_rtt(void *arg)
- {
- /* Test Plan:
- *
- * 1. Test RTT measurement server side
- * a. test usage of measured RTT
- * 2. Test termination of RTT measurement
- * a. test non-update of RTT
- * 3. Test client side circuit and non-application of RTT..
- */
- circpad_delay_t rtt_estimate;
- int64_t actual_mocked_monotime_start;
- (void)arg;
- MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
- MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
- dummy_channel.cmux = circuitmux_alloc();
- relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
- client_side = TO_CIRCUIT(origin_circuit_new());
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_init();
- monotime_enable_test_mocking();
- actual_mocked_monotime_start = MONOTIME_MOCK_START;
- monotime_set_mock_time_nsec(actual_mocked_monotime_start);
- monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
- curr_mocked_time = actual_mocked_monotime_start;
- timers_initialize();
- circpad_machines_init();
- helper_create_basic_machine();
- MOCK(circuit_package_relay_cell,
- circuit_package_relay_cell_mock);
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
- 0);
- relay_side->padding_machine[0] = &circ_client_machine;
- relay_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,0);
- /* Test 1: Test measuring RTT */
- circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
- tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
- timers_advance_and_run(20);
- circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
- tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
- tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 19000);
- tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 30000);
- tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
- OP_EQ,
- relay_side->padding_info[0]->rtt_estimate_usec+
- circpad_machine_current_state(
- relay_side->padding_info[0])->histogram_edges[0]);
- circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
- circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
- tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
- timers_advance_and_run(20);
- circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
- circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
- tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
- tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 20000);
- tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 21000);
- tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
- OP_EQ,
- relay_side->padding_info[0]->rtt_estimate_usec+
- circpad_machine_current_state(
- relay_side->padding_info[0])->histogram_edges[0]);
- /* Test 2: Termination of RTT measurement (from the previous test) */
- tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
- rtt_estimate = relay_side->padding_info[0]->rtt_estimate_usec;
- circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
- timers_advance_and_run(4);
- circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
- tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_EQ,
- rtt_estimate);
- tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
- tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
- tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
- OP_EQ,
- relay_side->padding_info[0]->rtt_estimate_usec+
- circpad_machine_current_state(
- relay_side->padding_info[0])->histogram_edges[0]);
- /* Test 3: Make sure client side machine properly ignores RTT */
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
- timers_advance_and_run(20);
- circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
- tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
- tt_int_op(client_side->padding_info[0]->rtt_estimate_usec, OP_EQ, 0);
- tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
- OP_NE, client_side->padding_info[0]->rtt_estimate_usec);
- tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
- OP_EQ,
- circpad_machine_current_state(
- client_side->padding_info[0])->histogram_edges[0]);
- done:
- free_fake_orcirc(relay_side);
- circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
- circuitmux_free(dummy_channel.cmux);
- timers_shutdown();
- monotime_disable_test_mocking();
- UNMOCK(circuit_package_relay_cell);
- UNMOCK(circuitmux_attach_circuit);
- tor_free(circ_client_machine.states);
- return;
- }
- void
- helper_create_basic_machine(void)
- {
- /* Start, burst */
- circpad_machine_states_init(&circ_client_machine, 2);
- circ_client_machine.states[CIRCPAD_STATE_START].
- next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
- circ_client_machine.states[CIRCPAD_STATE_BURST].
- next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
- circ_client_machine.states[CIRCPAD_STATE_BURST].
- next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
- circ_client_machine.states[CIRCPAD_STATE_BURST].
- next_state[CIRCPAD_EVENT_NONPADDING_SENT] = CIRCPAD_STATE_CANCEL;
- circ_client_machine.states[CIRCPAD_STATE_BURST].token_removal =
- CIRCPAD_TOKEN_REMOVAL_HIGHER;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_len = 5;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 500;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 2500;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 5000;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[3] = 10000;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[4] = 20000;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[0] = 1;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[1] = 0;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[2] = 2;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[3] = 2;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[4] = 2;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_total_tokens = 7;
- circ_client_machine.states[CIRCPAD_STATE_BURST].use_rtt_estimate = 1;
- return;
- }
- #define BIG_HISTOGRAM_LEN 10
- /** Setup a machine with a big histogram */
- static void
- helper_create_machine_with_big_histogram(circpad_removal_t removal_strategy)
- {
- const int tokens_per_bin = 2;
- /* Start, burst */
- circpad_machine_states_init(&circ_client_machine, 2);
- circpad_state_t *burst_state =
- &circ_client_machine.states[CIRCPAD_STATE_BURST];
- circ_client_machine.states[CIRCPAD_STATE_START].
- next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
- burst_state->next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
- burst_state->next_state[CIRCPAD_EVENT_NONPADDING_RECV] =CIRCPAD_STATE_BURST;
- burst_state->next_state[CIRCPAD_EVENT_NONPADDING_SENT] =CIRCPAD_STATE_CANCEL;
- burst_state->token_removal = CIRCPAD_TOKEN_REMOVAL_HIGHER;
- burst_state->histogram_len = BIG_HISTOGRAM_LEN;
- int n_tokens = 0;
- int i;
- for (i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- burst_state->histogram[i] = tokens_per_bin;
- n_tokens += tokens_per_bin;
- }
- burst_state->histogram_edges[0] = 0;
- burst_state->histogram_edges[1] = 1;
- burst_state->histogram_edges[2] = 7;
- burst_state->histogram_edges[3] = 15;
- burst_state->histogram_edges[4] = 31;
- burst_state->histogram_edges[5] = 62;
- burst_state->histogram_edges[6] = 125;
- burst_state->histogram_edges[7] = 250;
- burst_state->histogram_edges[8] = 500;
- burst_state->histogram_edges[9] = 1000;
- burst_state->histogram_total_tokens = n_tokens;
- burst_state->length_dist.type = CIRCPAD_DIST_UNIFORM;
- burst_state->length_dist.param1 = n_tokens;
- burst_state->length_dist.param2 = n_tokens;
- burst_state->max_length = n_tokens;
- burst_state->length_includes_nonpadding = 1;
- burst_state->use_rtt_estimate = 0;
- burst_state->token_removal = removal_strategy;
- }
- static circpad_decision_t
- circpad_machine_schedule_padding_mock(circpad_machine_runtime_t *mi)
- {
- (void)mi;
- return 0;
- }
- static uint64_t
- mock_monotime_absolute_usec(void)
- {
- return 100;
- }
- /** Test higher token removal strategy by bin */
- static void
- test_circuitpadding_token_removal_higher(void *arg)
- {
- circpad_machine_runtime_t *mi;
- (void)arg;
- /* Mock it up */
- MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
- MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
- /* Setup test environment (time etc.) */
- client_side = (circuit_t *)origin_circuit_new();
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_enable_test_mocking();
- /* Create test machine */
- helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_HIGHER);
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] =
- circpad_circuit_machineinfo_new(client_side, 0);
- /* move the machine to the right state */
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- /* Get the machine and setup tokens */
- mi = client_side->padding_info[0];
- tt_assert(mi);
- /*************************************************************************/
- uint64_t current_time = monotime_absolute_usec();
- /* Test left boundaries of each histogram bin: */
- const circpad_delay_t bin_left_bounds[] =
- {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
- for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
- tt_uint_op(bin_left_bounds[i], OP_EQ,
- circpad_histogram_bin_to_usec(mi, i));
- }
- /* Test right boundaries of each histogram bin: */
- const circpad_delay_t bin_right_bounds[] =
- {0, 6, 14, 30, 61, 124, 249, 499, 999, CIRCPAD_DELAY_INFINITE-1};
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_uint_op(bin_right_bounds[i], OP_EQ,
- histogram_get_bin_upper_bound(mi, i));
- }
- /* Check that all bins have two tokens right now */
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* This is the right order to remove tokens from this histogram. That is, we
- * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
- * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
- * the same reason, then from the 5th, etc. */
- const int bin_removal_order[] = {4, 5, 6, 7, 8};
- unsigned i;
- /* Remove all tokens from all bins apart from the infinity bin */
- for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
- int bin_to_remove = bin_removal_order[i];
- log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
- i, bin_to_remove);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- /* Test that we cleaned out this bin. Don't do this in the case of the last
- bin since the tokens will get refilled */
- if (i != BIG_HISTOGRAM_LEN - 2) {
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
- }
- }
- /* Check that all lower bins are not touched */
- for (i=0; i < 4 ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* Test below the lowest bin, for coverage */
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
- mi->padding_scheduled_at_usec = current_time;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[0], OP_EQ, 1);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- monotime_disable_test_mocking();
- tor_free(circ_client_machine.states);
- }
- /** Test lower token removal strategy by bin */
- static void
- test_circuitpadding_token_removal_lower(void *arg)
- {
- circpad_machine_runtime_t *mi;
- (void)arg;
- /* Mock it up */
- MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
- MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
- /* Setup test environment (time etc.) */
- client_side = (circuit_t *)origin_circuit_new();
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_enable_test_mocking();
- /* Create test machine */
- helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_LOWER);
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] =
- circpad_circuit_machineinfo_new(client_side, 0);
- /* move the machine to the right state */
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- /* Get the machine and setup tokens */
- mi = client_side->padding_info[0];
- tt_assert(mi);
- /*************************************************************************/
- uint64_t current_time = monotime_absolute_usec();
- /* Test left boundaries of each histogram bin: */
- const circpad_delay_t bin_left_bounds[] =
- {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
- for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
- tt_uint_op(bin_left_bounds[i], OP_EQ,
- circpad_histogram_bin_to_usec(mi, i));
- }
- /* Check that all bins have two tokens right now */
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* This is the right order to remove tokens from this histogram. That is, we
- * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
- * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
- * the same reason, then from the 5th, etc. */
- const int bin_removal_order[] = {4, 3, 2, 1, 0};
- unsigned i;
- /* Remove all tokens from all bins apart from the infinity bin */
- for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
- int bin_to_remove = bin_removal_order[i];
- log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
- i, bin_to_remove);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- /* Test that we cleaned out this bin. Don't do this in the case of the last
- bin since the tokens will get refilled */
- if (i != BIG_HISTOGRAM_LEN - 2) {
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
- }
- }
- /* Check that all higher bins are untouched */
- for (i = 5; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* Test above the highest bin, for coverage */
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- circ_client_machine.states[CIRCPAD_STATE_BURST].
- histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
- mi->padding_scheduled_at_usec = current_time - 29202;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- monotime_disable_test_mocking();
- tor_free(circ_client_machine.states);
- }
- /** Test closest token removal strategy by bin */
- static void
- test_circuitpadding_closest_token_removal(void *arg)
- {
- circpad_machine_runtime_t *mi;
- (void)arg;
- /* Mock it up */
- MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
- MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
- /* Setup test environment (time etc.) */
- client_side = (circuit_t *)origin_circuit_new();
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_enable_test_mocking();
- /* Create test machine */
- helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST);
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] =
- circpad_circuit_machineinfo_new(client_side, 0);
- /* move the machine to the right state */
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- /* Get the machine and setup tokens */
- mi = client_side->padding_info[0];
- tt_assert(mi);
- /*************************************************************************/
- uint64_t current_time = monotime_absolute_usec();
- /* Test left boundaries of each histogram bin: */
- const circpad_delay_t bin_left_bounds[] =
- {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
- for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
- tt_uint_op(bin_left_bounds[i], OP_EQ,
- circpad_histogram_bin_to_usec(mi, i));
- }
- /* Check that all bins have two tokens right now */
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* This is the right order to remove tokens from this histogram. That is, we
- * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
- * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
- * the same reason, then from the 5th, etc. */
- const int bin_removal_order[] = {4, 3, 5, 2, 6, 1, 7, 0, 8, 9};
- /* Remove all tokens from all bins apart from the infinity bin */
- for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
- int bin_to_remove = bin_removal_order[i];
- log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
- i, bin_to_remove);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- /* Test that we cleaned out this bin. Don't do this in the case of the last
- bin since the tokens will get refilled */
- if (i != BIG_HISTOGRAM_LEN - 2) {
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
- }
- }
- /* Check that all bins have been refilled */
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* Test below the lowest bin, for coverage */
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
- mi->padding_scheduled_at_usec = current_time - 102;
- mi->histogram[0] = 0;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[1], OP_EQ, 1);
- /* Test above the highest bin, for coverage */
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- mi->padding_scheduled_at_usec = current_time - 29202;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- monotime_disable_test_mocking();
- tor_free(circ_client_machine.states);
- }
- /** Test closest token removal strategy with usec */
- static void
- test_circuitpadding_closest_token_removal_usec(void *arg)
- {
- circpad_machine_runtime_t *mi;
- (void)arg;
- /* Mock it up */
- MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
- MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
- /* Setup test environment (time etc.) */
- client_side = (circuit_t *)origin_circuit_new();
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_enable_test_mocking();
- /* Create test machine */
- helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST_USEC);
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] =
- circpad_circuit_machineinfo_new(client_side, 0);
- /* move the machine to the right state */
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- /* Get the machine and setup tokens */
- mi = client_side->padding_info[0];
- tt_assert(mi);
- /*************************************************************************/
- uint64_t current_time = monotime_absolute_usec();
- /* Test left boundaries of each histogram bin: */
- const circpad_delay_t bin_left_bounds[] =
- {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
- for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
- tt_uint_op(bin_left_bounds[i], OP_EQ,
- circpad_histogram_bin_to_usec(mi, i));
- }
- /* XXX we want to test remove_token_exact and
- circpad_machine_remove_closest_token() with usec */
- /* Check that all bins have two tokens right now */
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* This is the right order to remove tokens from this histogram. That is, we
- * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
- * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
- * the same reason, then from the 5th, etc. */
- const int bin_removal_order[] = {4, 3, 5, 2, 1, 0, 6, 7, 8, 9};
- /* Remove all tokens from all bins apart from the infinity bin */
- for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
- int bin_to_remove = bin_removal_order[i];
- log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
- i, bin_to_remove);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- /* Test that we cleaned out this bin. Don't do this in the case of the last
- bin since the tokens will get refilled */
- if (i != BIG_HISTOGRAM_LEN - 2) {
- tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
- }
- }
- /* Check that all bins have been refilled */
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- /* Test below the lowest bin, for coverage */
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
- circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
- mi->padding_scheduled_at_usec = current_time - 102;
- mi->histogram[0] = 0;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[1], OP_EQ, 1);
- /* Test above the highest bin, for coverage */
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- circ_client_machine.states[CIRCPAD_STATE_BURST].
- histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
- mi->padding_scheduled_at_usec = current_time - 29202;
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- monotime_disable_test_mocking();
- tor_free(circ_client_machine.states);
- }
- /** Test closest token removal strategy with usec */
- static void
- test_circuitpadding_token_removal_exact(void *arg)
- {
- circpad_machine_runtime_t *mi;
- (void)arg;
- /* Mock it up */
- MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
- MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
- /* Setup test environment (time etc.) */
- client_side = (circuit_t *)origin_circuit_new();
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_enable_test_mocking();
- /* Create test machine */
- helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_EXACT);
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] =
- circpad_circuit_machineinfo_new(client_side, 0);
- /* move the machine to the right state */
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- /* Get the machine and setup tokens */
- mi = client_side->padding_info[0];
- tt_assert(mi);
- /**********************************************************************/
- uint64_t current_time = monotime_absolute_usec();
- /* Ensure that we will clear out bin #4 with this usec */
- mi->padding_scheduled_at_usec = current_time - 57;
- tt_int_op(mi->histogram[4], OP_EQ, 2);
- circpad_machine_remove_token(mi);
- mi->padding_scheduled_at_usec = current_time - 57;
- tt_int_op(mi->histogram[4], OP_EQ, 1);
- circpad_machine_remove_token(mi);
- tt_int_op(mi->histogram[4], OP_EQ, 0);
- /* Ensure that we will not remove any other tokens even tho we try to, since
- * this is what the exact strategy dictates */
- mi->padding_scheduled_at_usec = current_time - 57;
- circpad_machine_remove_token(mi);
- for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
- if (i != 4) {
- tt_int_op(mi->histogram[i], OP_EQ, 2);
- }
- }
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- monotime_disable_test_mocking();
- tor_free(circ_client_machine.states);
- }
- #undef BIG_HISTOGRAM_LEN
- void
- test_circuitpadding_tokens(void *arg)
- {
- const circpad_state_t *state;
- circpad_machine_runtime_t *mi;
- int64_t actual_mocked_monotime_start;
- (void)arg;
- /** Test plan:
- *
- * 1. Test symmetry between bin_to_usec and usec_to_bin
- * a. Test conversion
- * b. Test edge transitions (lower, upper)
- * 2. Test remove higher on an empty bin
- * a. Normal bin
- * b. Infinity bin
- * c. Bin 0
- * d. No higher
- * 3. Test remove lower
- * a. Normal bin
- * b. Bin 0
- * c. No lower
- * 4. Test remove closest
- * a. Closest lower
- * b. Closest higher
- * c. Closest 0
- * d. Closest Infinity
- */
- client_side = TO_CIRCUIT(origin_circuit_new());
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_init();
- monotime_enable_test_mocking();
- actual_mocked_monotime_start = MONOTIME_MOCK_START;
- monotime_set_mock_time_nsec(actual_mocked_monotime_start);
- monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
- curr_mocked_time = actual_mocked_monotime_start;
- /* This is needed so that we are not considered to be dormant */
- note_user_activity(20);
- timers_initialize();
- helper_create_basic_machine();
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
- 0);
- mi = client_side->padding_info[0];
- // Pretend a non-padding cell was sent
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
- /* We have to save the infinity bin because one inf delay
- * could have been chosen when we transition to burst */
- circpad_hist_token_t inf_bin = mi->histogram[4];
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- state = circpad_machine_current_state(client_side->padding_info[0]);
- // Test 0: convert bin->usec->bin
- // Bin 0+1 have different semantics
- for (int bin = 0; bin < 2; bin++) {
- circpad_delay_t usec =
- circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
- int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- usec);
- tt_int_op(bin, OP_EQ, bin2);
- }
- for (int bin = 2; bin < state->histogram_len-1; bin++) {
- circpad_delay_t usec =
- circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
- int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- usec);
- tt_int_op(bin, OP_EQ, bin2);
- /* Verify we round down */
- bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- usec+3);
- tt_int_op(bin, OP_EQ, bin2);
- bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- usec-1);
- tt_int_op(bin, OP_EQ, bin2+1);
- }
- // Test 1: converting usec->bin->usec->bin
- // Bin 0+1 have different semantics.
- for (circpad_delay_t i = 0; i <= state->histogram_edges[0]; i++) {
- int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- i);
- circpad_delay_t usec =
- circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
- int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- usec);
- tt_int_op(bin, OP_EQ, bin2);
- tt_int_op(i, OP_LE, usec);
- }
- for (circpad_delay_t i = state->histogram_edges[0]+1;
- i <= state->histogram_edges[0] +
- state->histogram_edges[state->histogram_len-2]; i++) {
- int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- i);
- circpad_delay_t usec =
- circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
- int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
- usec);
- tt_int_op(bin, OP_EQ, bin2);
- tt_int_op(i, OP_GE, usec);
- }
- /* 2.a. Normal higher bin */
- {
- tt_int_op(mi->histogram[2], OP_EQ, 2);
- tt_int_op(mi->histogram[3], OP_EQ, 2);
- circpad_machine_remove_higher_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1);
- tt_int_op(mi->histogram[3], OP_EQ, 2);
- tt_int_op(mi->histogram[2], OP_EQ, 1);
- circpad_machine_remove_higher_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1);
- tt_int_op(mi->histogram[2], OP_EQ, 0);
- tt_int_op(mi->histogram[3], OP_EQ, 2);
- circpad_machine_remove_higher_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1);
- circpad_machine_remove_higher_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1);
- tt_int_op(mi->histogram[3], OP_EQ, 0);
- circpad_machine_remove_higher_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1);
- tt_int_op(mi->histogram[3], OP_EQ, 0);
- }
- /* 2.b. Higher Infinity bin */
- {
- tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
- circpad_machine_remove_higher_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1);
- tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
- /* Test past the infinity bin */
- circpad_machine_remove_higher_token(mi,
- circpad_histogram_bin_to_usec(mi, 5)+1000000);
- tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
- }
- /* 2.c. Bin 0 */
- {
- tt_int_op(mi->histogram[0], OP_EQ, 0);
- mi->histogram[0] = 1;
- circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
- tt_int_op(mi->histogram[0], OP_EQ, 0);
- }
- /* Drain the infinity bin and cause a refill */
- while (inf_bin != 0) {
- tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- inf_bin--;
- }
- circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
- // We should have refilled here.
- tt_int_op(mi->histogram[4], OP_EQ, 2);
- /* 3.a. Bin 0 */
- {
- tt_int_op(mi->histogram[0], OP_EQ, 1);
- circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
- tt_int_op(mi->histogram[0], OP_EQ, 0);
- }
- /* 3.b. Test remove lower normal bin */
- {
- tt_int_op(mi->histogram[3], OP_EQ, 2);
- circpad_machine_remove_lower_token(mi,
- circpad_histogram_bin_to_usec(mi, 3)+1);
- circpad_machine_remove_lower_token(mi,
- circpad_histogram_bin_to_usec(mi, 3)+1);
- tt_int_op(mi->histogram[3], OP_EQ, 0);
- tt_int_op(mi->histogram[2], OP_EQ, 2);
- circpad_machine_remove_lower_token(mi,
- circpad_histogram_bin_to_usec(mi, 3)+1);
- circpad_machine_remove_lower_token(mi,
- circpad_histogram_bin_to_usec(mi, 3)+1);
- /* 3.c. No lower */
- circpad_machine_remove_lower_token(mi,
- circpad_histogram_bin_to_usec(mi, 3)+1);
- tt_int_op(mi->histogram[2], OP_EQ, 0);
- }
- /* 4. Test remove closest
- * a. Closest lower
- * b. Closest higher
- * c. Closest 0
- * d. Closest Infinity
- */
- circpad_machine_setup_tokens(mi);
- tt_int_op(mi->histogram[2], OP_EQ, 2);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1, 0);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1, 0);
- tt_int_op(mi->histogram[2], OP_EQ, 0);
- tt_int_op(mi->histogram[3], OP_EQ, 2);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1, 0);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1, 0);
- tt_int_op(mi->histogram[3], OP_EQ, 0);
- tt_int_op(mi->histogram[0], OP_EQ, 1);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1, 0);
- tt_int_op(mi->histogram[0], OP_EQ, 0);
- tt_int_op(mi->histogram[4], OP_EQ, 2);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 2)+1, 0);
- tt_int_op(mi->histogram[4], OP_EQ, 2);
- /* 5. Test remove closest usec
- * a. Closest 0
- * b. Closest lower (below midpoint)
- * c. Closest higher (above midpoint)
- * d. Closest Infinity
- */
- circpad_machine_setup_tokens(mi);
- tt_int_op(mi->histogram[0], OP_EQ, 1);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 0)/3, 1);
- tt_int_op(mi->histogram[0], OP_EQ, 0);
- tt_int_op(mi->histogram[2], OP_EQ, 2);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 0)/3, 1);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 0)/3, 1);
- tt_int_op(mi->histogram[2], OP_EQ, 0);
- tt_int_op(mi->histogram[3], OP_EQ, 2);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 4), 1);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 4), 1);
- tt_int_op(mi->histogram[3], OP_EQ, 0);
- tt_int_op(mi->histogram[4], OP_EQ, 2);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 4), 1);
- circpad_machine_remove_closest_token(mi,
- circpad_histogram_bin_to_usec(mi, 4), 1);
- tt_int_op(mi->histogram[4], OP_EQ, 2);
- // XXX: Need more coverage of the actual usec branches
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- monotime_disable_test_mocking();
- tor_free(circ_client_machine.states);
- }
- void
- test_circuitpadding_wronghop(void *arg)
- {
- /**
- * Test plan:
- * 1. Padding sent from hop 1 and 3 to client
- * 2. Send negotiated from hop 1 and 3 to client
- * 3. Garbled negotiated cell
- * 4. Padding negotiate sent to client
- * 5. Send negotiate stop command for unknown machine
- * 6. Send negotiated to relay
- * 7. Garbled padding negotiate cell
- */
- (void)arg;
- uint32_t read_bw = 0, overhead_bw = 0;
- cell_t cell;
- signed_error_t ret;
- origin_circuit_t *orig_client;
- int64_t actual_mocked_monotime_start;
- MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
- /* Mock this function so that our cell counting tests don't get confused by
- * padding that gets sent by scheduled timers. */
- MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
- client_side = (circuit_t *)origin_circuit_new();
- dummy_channel.cmux = circuitmux_alloc();
- relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
- &dummy_channel);
- orig_client = TO_ORIGIN_CIRCUIT(client_side);
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- nodes_init();
- monotime_init();
- monotime_enable_test_mocking();
- actual_mocked_monotime_start = MONOTIME_MOCK_START;
- monotime_set_mock_time_nsec(actual_mocked_monotime_start);
- monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
- curr_mocked_time = actual_mocked_monotime_start;
- timers_initialize();
- circpad_machines_init();
- MOCK(node_get_by_id,
- node_get_by_id_mock);
- MOCK(circuit_package_relay_cell,
- circuit_package_relay_cell_mock);
- /* Build three hops */
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* verify padding was negotiated */
- tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
- tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
- /* verify echo was sent */
- tt_int_op(n_relay_cells, OP_EQ, 1);
- tt_int_op(n_client_cells, OP_EQ, 1);
- read_bw = orig_client->n_delivered_read_circ_bw;
- overhead_bw = orig_client->n_overhead_read_circ_bw;
- /* 1. Test padding from first and third hop */
- circpad_deliver_recognized_relay_cell_events(client_side,
- RELAY_COMMAND_DROP,
- TO_ORIGIN_CIRCUIT(client_side)->cpath);
- tt_int_op(read_bw, OP_EQ,
- orig_client->n_delivered_read_circ_bw);
- tt_int_op(overhead_bw, OP_EQ,
- orig_client->n_overhead_read_circ_bw);
- circpad_deliver_recognized_relay_cell_events(client_side,
- RELAY_COMMAND_DROP,
- TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
- tt_int_op(read_bw, OP_EQ,
- orig_client->n_delivered_read_circ_bw);
- tt_int_op(overhead_bw, OP_EQ,
- orig_client->n_overhead_read_circ_bw);
- circpad_deliver_recognized_relay_cell_events(client_side,
- RELAY_COMMAND_DROP,
- TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
- tt_int_op(read_bw, OP_EQ,
- orig_client->n_delivered_read_circ_bw);
- tt_int_op(overhead_bw, OP_LT,
- orig_client->n_overhead_read_circ_bw);
- /* 2. Test padding negotiated not handled from hops 1,3 */
- ret = circpad_handle_padding_negotiated(client_side, &cell,
- TO_ORIGIN_CIRCUIT(client_side)->cpath);
- tt_int_op(ret, OP_EQ, -1);
- ret = circpad_handle_padding_negotiated(client_side, &cell,
- TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
- tt_int_op(ret, OP_EQ, -1);
- /* 3. Garbled negotiated cell */
- memset(&cell, 255, sizeof(cell));
- ret = circpad_handle_padding_negotiated(client_side, &cell,
- TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
- tt_int_op(ret, OP_EQ, -1);
- /* 4. Test that negotiate is dropped at origin */
- read_bw = orig_client->n_delivered_read_circ_bw;
- overhead_bw = orig_client->n_overhead_read_circ_bw;
- relay_send_command_from_edge(0, relay_side,
- RELAY_COMMAND_PADDING_NEGOTIATE,
- (void*)cell.payload,
- (size_t)3, NULL);
- tt_int_op(read_bw, OP_EQ,
- orig_client->n_delivered_read_circ_bw);
- tt_int_op(overhead_bw, OP_EQ,
- orig_client->n_overhead_read_circ_bw);
- tt_int_op(n_relay_cells, OP_EQ, 2);
- tt_int_op(n_client_cells, OP_EQ, 1);
- /* 5. Test that asking to stop the wrong machine does nothing */
- circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
- 255, 2, CIRCPAD_COMMAND_STOP);
- tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
- tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
- tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
- tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
- tt_int_op(n_relay_cells, OP_EQ, 3);
- tt_int_op(n_client_cells, OP_EQ, 2);
- /* 6. Sending negotiated command to relay does nothing */
- ret = circpad_handle_padding_negotiated(relay_side, &cell, NULL);
- tt_int_op(ret, OP_EQ, -1);
- /* 7. Test garbled negotated cell (bad command 255) */
- memset(&cell, 0, sizeof(cell));
- ret = circpad_handle_padding_negotiate(relay_side, &cell);
- tt_int_op(ret, OP_EQ, -1);
- tt_int_op(n_client_cells, OP_EQ, 2);
- /* Test 2: Test no padding */
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- free_fake_orcirc(relay_side);
- client_side = (circuit_t *)origin_circuit_new();
- relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
- &dummy_channel);
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 0);
- /* verify no padding was negotiated */
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- tt_int_op(n_relay_cells, OP_EQ, 3);
- tt_int_op(n_client_cells, OP_EQ, 2);
- /* verify no echo was sent */
- tt_int_op(n_relay_cells, OP_EQ, 3);
- tt_int_op(n_client_cells, OP_EQ, 2);
- /* Finish circuit */
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* Spoof padding negotiated on circuit with no padding */
- circpad_padding_negotiated(relay_side,
- CIRCPAD_MACHINE_CIRC_SETUP,
- CIRCPAD_COMMAND_START,
- CIRCPAD_RESPONSE_OK);
- /* verify no padding was negotiated */
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- circpad_padding_negotiated(relay_side,
- CIRCPAD_MACHINE_CIRC_SETUP,
- CIRCPAD_COMMAND_START,
- CIRCPAD_RESPONSE_ERR);
- /* verify no padding was negotiated */
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- free_fake_orcirc(relay_side);
- circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
- circuitmux_free(dummy_channel.cmux);
- monotime_disable_test_mocking();
- UNMOCK(node_get_by_id);
- UNMOCK(circuit_package_relay_cell);
- UNMOCK(circuitmux_attach_circuit);
- nodes_free();
- }
- void
- test_circuitpadding_negotiation(void *arg)
- {
- /**
- * Test plan:
- * 1. Test circuit where padding is supported by middle
- * a. Make sure padding negotiation is sent
- * b. Test padding negotiation delivery and parsing
- * 2. Test circuit where padding is unsupported by middle
- * a. Make sure padding negotiation is not sent
- * 3. Test failure to negotiate a machine due to desync.
- */
- int64_t actual_mocked_monotime_start;
- (void)arg;
- MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
- client_side = TO_CIRCUIT(origin_circuit_new());
- dummy_channel.cmux = circuitmux_alloc();
- relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- nodes_init();
- monotime_init();
- monotime_enable_test_mocking();
- actual_mocked_monotime_start = MONOTIME_MOCK_START;
- monotime_set_mock_time_nsec(actual_mocked_monotime_start);
- monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
- curr_mocked_time = actual_mocked_monotime_start;
- timers_initialize();
- circpad_machines_init();
- MOCK(node_get_by_id,
- node_get_by_id_mock);
- MOCK(circuit_package_relay_cell,
- circuit_package_relay_cell_mock);
- /* Build two hops */
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* verify padding was negotiated */
- tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
- tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
- /* verify echo was sent */
- tt_int_op(n_relay_cells, OP_EQ, 1);
- tt_int_op(n_client_cells, OP_EQ, 1);
- /* Finish circuit */
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* Test 2: Test no padding */
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- free_fake_orcirc(relay_side);
- client_side = TO_CIRCUIT(origin_circuit_new());
- relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 0);
- /* verify no padding was negotiated */
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- tt_int_op(n_relay_cells, OP_EQ, 1);
- tt_int_op(n_client_cells, OP_EQ, 1);
- /* verify no echo was sent */
- tt_int_op(n_relay_cells, OP_EQ, 1);
- tt_int_op(n_client_cells, OP_EQ, 1);
- /* Finish circuit */
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* Force negotiate padding. */
- circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
- CIRCPAD_MACHINE_CIRC_SETUP,
- 2, CIRCPAD_COMMAND_START);
- /* verify no padding was negotiated */
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- /* verify no echo was sent */
- tt_int_op(n_relay_cells, OP_EQ, 1);
- tt_int_op(n_client_cells, OP_EQ, 1);
- /* 3. Test failure to negotiate a machine due to desync */
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- free_fake_orcirc(relay_side);
- client_side = TO_CIRCUIT(origin_circuit_new());
- relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- SMARTLIST_FOREACH(relay_padding_machines,
- circpad_machine_spec_t *,
- m, tor_free(m->states); tor_free(m));
- smartlist_free(relay_padding_machines);
- relay_padding_machines = smartlist_new();
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* verify echo was sent */
- tt_int_op(n_client_cells, OP_EQ, 2);
- tt_int_op(n_relay_cells, OP_EQ, 2);
- /* verify no padding was negotiated */
- tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- free_fake_orcirc(relay_side);
- circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
- circuitmux_free(dummy_channel.cmux);
- monotime_disable_test_mocking();
- UNMOCK(node_get_by_id);
- UNMOCK(circuit_package_relay_cell);
- UNMOCK(circuitmux_attach_circuit);
- nodes_free();
- }
- static void
- simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
- int padding)
- {
- char whatevs_key[CPATH_KEY_MATERIAL_LEN];
- char digest[DIGEST_LEN];
- tor_addr_t addr;
- // Pretend a non-padding cell was sent
- circpad_cell_event_nonpadding_sent((circuit_t*)client);
- // Receive extend cell at middle
- circpad_cell_event_nonpadding_received((circuit_t*)mid_relay);
- // Advance time a tiny bit so we can calculate an RTT
- curr_mocked_time += 10 * TOR_NSEC_PER_MSEC;
- monotime_coarse_set_mock_time_nsec(curr_mocked_time);
- monotime_set_mock_time_nsec(curr_mocked_time);
- // Receive extended cell at middle
- circpad_cell_event_nonpadding_sent((circuit_t*)mid_relay);
- // Receive extended cell at first hop
- circpad_cell_event_nonpadding_received((circuit_t*)client);
- // Add a hop to cpath
- crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
- onion_append_to_cpath(&TO_ORIGIN_CIRCUIT(client)->cpath, hop);
- hop->magic = CRYPT_PATH_MAGIC;
- hop->state = CPATH_STATE_OPEN;
- // add an extend info to indicate if this node supports padding or not.
- // (set the first byte of the digest for our mocked node_get_by_id)
- digest[0] = padding;
- hop->extend_info = extend_info_new(
- padding ? "padding" : "non-padding",
- digest, NULL, NULL, NULL,
- &addr, padding);
- circuit_init_cpath_crypto(hop, whatevs_key, sizeof(whatevs_key), 0, 0);
- hop->package_window = circuit_initial_package_window();
- hop->deliver_window = CIRCWINDOW_START;
- // Signal that the hop was added
- circpad_machine_event_circ_added_hop(TO_ORIGIN_CIRCUIT(client));
- }
- static circpad_machine_spec_t *
- helper_create_conditional_machine(void)
- {
- circpad_machine_spec_t *ret =
- tor_malloc_zero(sizeof(circpad_machine_spec_t));
- /* Start, burst */
- circpad_machine_states_init(ret, 2);
- ret->states[CIRCPAD_STATE_START].
- next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
- ret->states[CIRCPAD_STATE_BURST].
- next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
- ret->states[CIRCPAD_STATE_BURST].
- next_state[CIRCPAD_EVENT_LENGTH_COUNT] = CIRCPAD_STATE_END;
- /* Use EXACT removal strategy, otherwise setup_tokens() does not work */
- ret->states[CIRCPAD_STATE_BURST].token_removal =
- CIRCPAD_TOKEN_REMOVAL_EXACT;
- ret->states[CIRCPAD_STATE_BURST].histogram_len = 3;
- ret->states[CIRCPAD_STATE_BURST].histogram_edges[0] = 0;
- ret->states[CIRCPAD_STATE_BURST].histogram_edges[1] = 1;
- ret->states[CIRCPAD_STATE_BURST].histogram_edges[2] = 1000000;
- ret->states[CIRCPAD_STATE_BURST].histogram[0] = 6;
- ret->states[CIRCPAD_STATE_BURST].histogram[1] = 0;
- ret->states[CIRCPAD_STATE_BURST].histogram[2] = 0;
- ret->states[CIRCPAD_STATE_BURST].histogram_total_tokens = 6;
- ret->states[CIRCPAD_STATE_BURST].use_rtt_estimate = 0;
- ret->states[CIRCPAD_STATE_BURST].length_includes_nonpadding = 1;
- return ret;
- }
- static void
- helper_create_conditional_machines(void)
- {
- circpad_machine_spec_t *add = helper_create_conditional_machine();
- origin_padding_machines = smartlist_new();
- relay_padding_machines = smartlist_new();
- add->machine_num = 2;
- add->is_origin_side = 1;
- add->should_negotiate_end = 1;
- add->target_hopnum = 2;
- /* Let's have this one end after 4 packets */
- add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
- add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
- add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
- add->states[CIRCPAD_STATE_BURST].max_length = 4;
- add->conditions.requires_vanguards = 0;
- add->conditions.min_hops = 2;
- add->conditions.state_mask = CIRCPAD_CIRC_BUILDING|
- CIRCPAD_CIRC_NO_STREAMS|CIRCPAD_CIRC_HAS_RELAY_EARLY;
- add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
- register_padding_machine(add, origin_padding_machines);
- add = helper_create_conditional_machine();
- add->machine_num = 3;
- add->is_origin_side = 1;
- add->should_negotiate_end = 1;
- add->target_hopnum = 2;
- /* Let's have this one end after 4 packets */
- add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
- add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
- add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
- add->states[CIRCPAD_STATE_BURST].max_length = 4;
- add->conditions.requires_vanguards = 1;
- add->conditions.min_hops = 3;
- add->conditions.state_mask = CIRCPAD_CIRC_OPENED|
- CIRCPAD_CIRC_STREAMS|CIRCPAD_CIRC_HAS_NO_RELAY_EARLY;
- add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
- register_padding_machine(add, origin_padding_machines);
- add = helper_create_conditional_machine();
- add->machine_num = 2;
- register_padding_machine(add, relay_padding_machines);
- add = helper_create_conditional_machine();
- add->machine_num = 3;
- register_padding_machine(add, relay_padding_machines);
- }
- void
- test_circuitpadding_conditions(void *arg)
- {
- /**
- * Test plan:
- * 0. Make a few origin and client machines with diff conditions
- * * vanguards, purposes, has_opened circs, no relay early
- * * Client side should_negotiate_end
- * * Length limits
- * 1. Test STATE_END transitions
- * 2. Test new machine after end with same conditions
- * 3. Test new machine due to changed conditions
- * * Esp: built event, no relay early, no streams
- * XXX: Diff test:
- * 1. Test STATE_END with pending timers
- * 2. Test marking a circuit before padding callback fires
- * 3. Test freeing a circuit before padding callback fires
- */
- int64_t actual_mocked_monotime_start;
- (void)arg;
- MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
- nodes_init();
- dummy_channel.cmux = circuitmux_alloc();
- relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
- &dummy_channel);
- client_side = (circuit_t *)origin_circuit_new();
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- monotime_init();
- monotime_enable_test_mocking();
- actual_mocked_monotime_start = MONOTIME_MOCK_START;
- monotime_set_mock_time_nsec(actual_mocked_monotime_start);
- monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
- curr_mocked_time = actual_mocked_monotime_start;
- /* This is needed so that we are not considered to be dormant */
- note_user_activity(20);
- timers_initialize();
- helper_create_conditional_machines();
- MOCK(circuit_package_relay_cell,
- circuit_package_relay_cell_mock);
- MOCK(node_get_by_id,
- node_get_by_id_mock);
- /* Simulate extend. This should result in the original machine getting
- * added, since the circuit is not built */
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* Verify that machine #2 is added */
- tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
- tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
- /* Deliver a padding cell to the client, to trigger burst state */
- circpad_cell_event_padding_sent(client_side);
- /* This should have trigger length shutdown condition on client.. */
- tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- /* Verify machine is gone from both sides */
- tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- /* Send another event.. verify machine gets re-added properly
- * (test race with shutdown) */
- simulate_single_hop_extend(client_side, relay_side, 1);
- tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
- tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
- TO_ORIGIN_CIRCUIT(client_side)->p_streams = 0;
- circpad_machine_event_circ_has_no_streams(TO_ORIGIN_CIRCUIT(client_side));
- tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
- tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
- /* Now make the circuit opened and send built event */
- TO_ORIGIN_CIRCUIT(client_side)->has_opened = 1;
- circpad_machine_event_circ_built(TO_ORIGIN_CIRCUIT(client_side));
- tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
- tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
- TO_ORIGIN_CIRCUIT(client_side)->remaining_relay_early_cells = 0;
- circpad_machine_event_circ_has_no_relay_early(
- TO_ORIGIN_CIRCUIT(client_side));
- tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
- tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
- get_options_mutable()->HSLayer2Nodes = (void*)1;
- TO_ORIGIN_CIRCUIT(client_side)->p_streams = (void*)1;
- circpad_machine_event_circ_has_streams(TO_ORIGIN_CIRCUIT(client_side));
- /* Verify different machine is added */
- tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
- tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
- /* Hold off on negotiated */
- deliver_negotiated = 0;
- /* Deliver a padding cell to the client, to trigger burst state */
- circpad_cell_event_padding_sent(client_side);
- /* This should have trigger length shutdown condition on client
- * but not the response for the padding machine */
- tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
- /* Verify machine is gone from the relay (but negotiated not back yet */
- tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- /* Add another hop and verify it's back */
- simulate_single_hop_extend(client_side, relay_side, 1);
- tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
- tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
- tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
- tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
- done:
- /* XXX: Free everything */
- return;
- }
- /** Disabled unstable test until #29298 is implemented (see #29122) */
- #if 0
- void
- test_circuitpadding_circuitsetup_machine(void *arg)
- {
- int64_t actual_mocked_monotime_start;
- /**
- * Test case plan:
- *
- * 1. Simulate a normal circuit setup pattern
- * a. Application traffic
- *
- * FIXME: This should focus more on exercising the machine
- * features rather than actual traffic patterns. For example,
- * test cancellation and bins empty/refill
- */
- (void)arg;
- MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
- dummy_channel.cmux = circuitmux_alloc();
- client_side = TO_CIRCUIT(origin_circuit_new());
- relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- nodes_init();
- monotime_init();
- monotime_enable_test_mocking();
- actual_mocked_monotime_start = MONOTIME_MOCK_START;
- monotime_set_mock_time_nsec(actual_mocked_monotime_start);
- monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
- curr_mocked_time = actual_mocked_monotime_start;
- timers_initialize();
- circpad_machines_init();
- MOCK(circuit_package_relay_cell,
- circuit_package_relay_cell_mock);
- MOCK(node_get_by_id,
- node_get_by_id_mock);
- /* Test case #1: Build a 3 hop circuit, then wait and let pad */
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- tt_int_op(n_client_cells, OP_EQ, 1);
- tt_int_op(n_relay_cells, OP_EQ, 1);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- tt_int_op(relay_side->padding_info[0]->is_padding_timer_scheduled,
- OP_EQ, 0);
- timers_advance_and_run(2000);
- tt_int_op(n_client_cells, OP_EQ, 2);
- tt_int_op(n_relay_cells, OP_EQ, 1);
- tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_GAP);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- timers_advance_and_run(5000);
- tt_int_op(n_client_cells, OP_EQ, 2);
- tt_int_op(n_relay_cells, OP_EQ, 2);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- timers_advance_and_run(2000);
- tt_int_op(n_client_cells, OP_EQ, 3);
- tt_int_op(n_relay_cells, OP_EQ, 2);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- timers_advance_and_run(5000);
- tt_int_op(n_client_cells, OP_EQ, 3);
- tt_int_op(n_relay_cells, OP_EQ, 3);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- timers_advance_and_run(2000);
- tt_int_op(n_client_cells, OP_EQ, 4);
- tt_int_op(n_relay_cells, OP_EQ, 3);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- timers_advance_and_run(5000);
- tt_int_op(n_client_cells, OP_EQ, 4);
- tt_int_op(n_relay_cells, OP_EQ, 4);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- timers_advance_and_run(2000);
- tt_int_op(n_client_cells, OP_EQ, 5);
- tt_int_op(n_relay_cells, OP_EQ, 4);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- timers_advance_and_run(5000);
- tt_int_op(n_client_cells, OP_EQ, 5);
- tt_int_op(n_relay_cells, OP_EQ, 5);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- timers_advance_and_run(2000);
- tt_int_op(n_client_cells, OP_EQ, 6);
- tt_int_op(n_relay_cells, OP_EQ, 5);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- timers_advance_and_run(5000);
- tt_int_op(n_client_cells, OP_EQ, 6);
- tt_int_op(n_relay_cells, OP_EQ, 6);
- tt_int_op(client_side->padding_info[0]->current_state,
- OP_EQ, CIRCPAD_STATE_END);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- tt_int_op(relay_side->padding_info[0]->current_state,
- OP_EQ, CIRCPAD_STATE_GAP);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- /* Verify we can't schedule padding in END state */
- circpad_decision_t ret =
- circpad_machine_schedule_padding(client_side->padding_info[0]);
- tt_int_op(ret, OP_EQ, CIRCPAD_STATE_UNCHANGED);
- /* Simulate application traffic */
- circpad_cell_event_nonpadding_sent(client_side);
- circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
- circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
- circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
- TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
- tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- tt_int_op(n_client_cells, OP_EQ, 6);
- tt_int_op(n_relay_cells, OP_EQ, 7);
- // Test timer cancellation
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- timers_advance_and_run(5000);
- circpad_cell_event_padding_received(client_side);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_GAP);
- tt_int_op(n_client_cells, OP_EQ, 8);
- tt_int_op(n_relay_cells, OP_EQ, 8);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- /* Test timer cancel due to state rules */
- circpad_cell_event_nonpadding_sent(client_side);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_EQ, 0);
- circpad_cell_event_padding_received(client_side);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- /* Simulate application traffic to cancel timer */
- circpad_cell_event_nonpadding_sent(client_side);
- circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
- circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
- circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
- TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
- tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
- tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
- /* No cells sent, except negotiate end from relay */
- tt_int_op(n_client_cells, OP_EQ, 8);
- tt_int_op(n_relay_cells, OP_EQ, 9);
- /* Test mark for close and free */
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- timers_advance_and_run(5000);
- circpad_cell_event_padding_received(client_side);
- tt_int_op(n_client_cells, OP_EQ, 10);
- tt_int_op(n_relay_cells, OP_EQ, 10);
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_BURST);
- tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
- CIRCPAD_STATE_GAP);
- tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
- OP_NE, 0);
- circuit_mark_for_close(client_side, END_CIRC_REASON_FLAG_REMOTE);
- free_fake_orcirc(relay_side);
- timers_advance_and_run(5000);
- /* No cells sent */
- tt_int_op(n_client_cells, OP_EQ, 10);
- tt_int_op(n_relay_cells, OP_EQ, 10);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
- circuitmux_free(dummy_channel.cmux);
- timers_shutdown();
- monotime_disable_test_mocking();
- UNMOCK(circuit_package_relay_cell);
- UNMOCK(circuitmux_attach_circuit);
- return;
- }
- #endif
- /** Helper function: Initializes a padding machine where every state uses the
- * uniform probability distribution. */
- static void
- helper_circpad_circ_distribution_machine_setup(int min, int max)
- {
- circpad_machine_states_init(&circ_client_machine, 7);
- circpad_state_t *zero_st = &circ_client_machine.states[0];
- zero_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 1;
- zero_st->iat_dist.type = CIRCPAD_DIST_UNIFORM;
- /* param2 is upper bound, param1 is lower */
- zero_st->iat_dist.param1 = min;
- zero_st->iat_dist.param2 = max;
- zero_st->dist_added_shift_usec = min;
- zero_st->dist_max_sample_usec = max;
- circpad_state_t *first_st = &circ_client_machine.states[1];
- first_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 2;
- first_st->iat_dist.type = CIRCPAD_DIST_LOGISTIC;
- /* param1 is Mu, param2 is sigma. */
- first_st->iat_dist.param1 = 9;
- first_st->iat_dist.param2 = 3;
- first_st->dist_added_shift_usec = min;
- first_st->dist_max_sample_usec = max;
- circpad_state_t *second_st = &circ_client_machine.states[2];
- second_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 3;
- second_st->iat_dist.type = CIRCPAD_DIST_LOG_LOGISTIC;
- /* param1 is Alpha, param2 is 1.0/Beta */
- second_st->iat_dist.param1 = 1;
- second_st->iat_dist.param2 = 0.5;
- second_st->dist_added_shift_usec = min;
- second_st->dist_max_sample_usec = max;
- circpad_state_t *third_st = &circ_client_machine.states[3];
- third_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 4;
- third_st->iat_dist.type = CIRCPAD_DIST_GEOMETRIC;
- /* param1 is 'p' (success probability) */
- third_st->iat_dist.param1 = 0.2;
- third_st->dist_added_shift_usec = min;
- third_st->dist_max_sample_usec = max;
- circpad_state_t *fourth_st = &circ_client_machine.states[4];
- fourth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 5;
- fourth_st->iat_dist.type = CIRCPAD_DIST_WEIBULL;
- /* param1 is k, param2 is Lambda */
- fourth_st->iat_dist.param1 = 1.5;
- fourth_st->iat_dist.param2 = 1;
- fourth_st->dist_added_shift_usec = min;
- fourth_st->dist_max_sample_usec = max;
- circpad_state_t *fifth_st = &circ_client_machine.states[5];
- fifth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 6;
- fifth_st->iat_dist.type = CIRCPAD_DIST_PARETO;
- /* param1 is sigma, param2 is xi */
- fifth_st->iat_dist.param1 = 1;
- fifth_st->iat_dist.param2 = 5;
- fifth_st->dist_added_shift_usec = min;
- fifth_st->dist_max_sample_usec = max;
- }
- /** Simple test that the padding delays sampled from a uniform distribution
- * actually faill within the uniform distribution range. */
- static void
- test_circuitpadding_sample_distribution(void *arg)
- {
- circpad_machine_runtime_t *mi;
- int n_samples;
- int n_states;
- (void) arg;
- /* mock this function so that we dont actually schedule any padding */
- MOCK(circpad_machine_schedule_padding,
- circpad_machine_schedule_padding_mock);
- /* Initialize a machine with multiple probability distributions */
- circpad_machines_init();
- helper_circpad_circ_distribution_machine_setup(0, 10);
- /* Initialize machine and circuits */
- client_side = TO_CIRCUIT(origin_circuit_new());
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] =
- circpad_circuit_machineinfo_new(client_side, 0);
- mi = client_side->padding_info[0];
- /* For every state, sample a bunch of values from the distribution and ensure
- * they fall within range. */
- for (n_states = 0 ; n_states < 6; n_states++) {
- /* Make sure we in the right state */
- tt_int_op(client_side->padding_info[0]->current_state, OP_EQ, n_states);
- for (n_samples = 0; n_samples < 100; n_samples++) {
- circpad_delay_t delay = circpad_machine_sample_delay(mi);
- tt_int_op(delay, OP_GE, 0);
- tt_int_op(delay, OP_LE, 10);
- }
- /* send a non-padding cell to move to the next machine state */
- circpad_cell_event_nonpadding_received((circuit_t*)client_side);
- }
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- UNMOCK(circpad_machine_schedule_padding);
- }
- static circpad_decision_t
- circpad_machine_spec_transition_mock(circpad_machine_runtime_t *mi,
- circpad_event_t event)
- {
- (void) mi;
- (void) event;
- return CIRCPAD_STATE_UNCHANGED;
- }
- /* Test per-machine padding rate limits */
- static void
- test_circuitpadding_machine_rate_limiting(void *arg)
- {
- (void) arg;
- bool retval;
- circpad_machine_runtime_t *mi;
- int i;
- /* Ignore machine transitions for the purposes of this function, we only
- * really care about padding counts */
- MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
- MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
- /* Setup machine and circuits */
- client_side = TO_CIRCUIT(origin_circuit_new());
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- helper_create_basic_machine();
- client_side->padding_machine[0] = &circ_client_machine;
- client_side->padding_info[0] =
- circpad_circuit_machineinfo_new(client_side, 0);
- mi = client_side->padding_info[0];
- /* Set up the machine info so that we can get through the basic functions */
- mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
- /* First we are going to test the per-machine rate limits */
- circ_client_machine.max_padding_percent = 50;
- circ_client_machine.allowed_padding_count = 100;
- /* Check padding limit, should be fine since we haven't sent anything yet. */
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 0);
- /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
- * the rate limit will not trigger */
- for (i=0;i<99;i++) {
- circpad_send_padding_cell_for_callback(mi);
- }
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 0);
- /* Now send another padding cell to pass circpad_global_allowed_cells=100,
- and see that the limit will trigger */
- circpad_send_padding_cell_for_callback(mi);
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 1);
- retval = circpad_machine_schedule_padding(mi);
- tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
- /* Cover wrap */
- for (;i<UINT16_MAX;i++) {
- circpad_send_padding_cell_for_callback(mi);
- }
- tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/2+1);
- tt_ptr_op(client_side->padding_info[0], OP_EQ, mi);
- for (i=0;i<UINT16_MAX;i++) {
- circpad_cell_event_nonpadding_sent(client_side);
- }
- tt_int_op(mi->nonpadding_sent, OP_EQ, UINT16_MAX/2);
- tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/4+1);
- done:
- free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
- }
- /* Test global padding rate limits */
- static void
- test_circuitpadding_global_rate_limiting(void *arg)
- {
- (void) arg;
- bool retval;
- circpad_machine_runtime_t *mi;
- int i;
- int64_t actual_mocked_monotime_start;
- /* Ignore machine transitions for the purposes of this function, we only
- * really care about padding counts */
- MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
- MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
- MOCK(circuit_package_relay_cell,
- circuit_package_relay_cell_mock);
- MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
- monotime_init();
- monotime_enable_test_mocking();
- actual_mocked_monotime_start = MONOTIME_MOCK_START;
- monotime_set_mock_time_nsec(actual_mocked_monotime_start);
- monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
- curr_mocked_time = actual_mocked_monotime_start;
- timers_initialize();
- client_side = (circuit_t *)origin_circuit_new();
- client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
- dummy_channel.cmux = circuitmux_alloc();
- /* Setup machine and circuits */
- relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel, &dummy_channel);
- relay_side->purpose = CIRCUIT_PURPOSE_OR;
- helper_create_basic_machine();
- relay_side->padding_machine[0] = &circ_client_machine;
- relay_side->padding_info[0] =
- circpad_circuit_machineinfo_new(relay_side, 0);
- mi = relay_side->padding_info[0];
- /* Set up the machine info so that we can get through the basic functions */
- mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
- simulate_single_hop_extend(client_side, relay_side, 1);
- simulate_single_hop_extend(client_side, relay_side, 1);
- /* Now test the global limits by setting up the consensus */
- networkstatus_t vote1;
- vote1.net_params = smartlist_new();
- smartlist_split_string(vote1.net_params,
- "circpad_global_allowed_cells=100 circpad_global_max_padding_pct=50",
- NULL, 0, 0);
- /* Register global limits with the padding subsystem */
- circpad_new_consensus_params(&vote1);
- /* Check padding limit, should be fine since we haven't sent anything yet. */
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 0);
- /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
- * the rate limit will not trigger */
- for (i=0;i<99;i++) {
- circpad_send_padding_cell_for_callback(mi);
- }
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 0);
- /* Now send another padding cell to pass circpad_global_allowed_cells=100,
- and see that the limit will trigger */
- circpad_send_padding_cell_for_callback(mi);
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 1);
- retval = circpad_machine_schedule_padding(mi);
- tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
- /* Now send 92 non-padding cells to get near the
- * circpad_global_max_padding_pct=50 limit; in particular with 96 non-padding
- * cells, the padding traffic is still 51% of total traffic so limit should
- * trigger */
- for (i=0;i<92;i++) {
- circpad_cell_event_nonpadding_sent(relay_side);
- }
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 1);
- /* Send another non-padding cell to bring the padding traffic to 50% of total
- * traffic and get past the limit */
- circpad_cell_event_nonpadding_sent(relay_side);
- retval = circpad_machine_reached_padding_limit(mi);
- tt_int_op(retval, OP_EQ, 0);
- done:
- free_fake_orcirc(relay_side);
- circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
- circuitmux_free(dummy_channel.cmux);
- SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
- smartlist_free(vote1.net_params);
- }
- #define TEST_CIRCUITPADDING(name, flags) \
- { #name, test_##name, (flags), NULL, NULL }
- struct testcase_t circuitpadding_tests[] = {
- TEST_CIRCUITPADDING(circuitpadding_tokens, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_negotiation, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_wronghop, TT_FORK),
- /** Disabled unstable test until #29298 is implemented (see #29122) */
- // TEST_CIRCUITPADDING(circuitpadding_circuitsetup_machine, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_conditions, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_rtt, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_sample_distribution, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_machine_rate_limiting, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_global_rate_limiting, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_token_removal_lower, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_token_removal_higher, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_closest_token_removal, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_closest_token_removal_usec, TT_FORK),
- TEST_CIRCUITPADDING(circuitpadding_token_removal_exact, TT_FORK),
- END_OF_TESTCASES
- };
|