test_circuitpadding.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419
  1. #define TOR_CHANNEL_INTERNAL_
  2. #define TOR_TIMERS_PRIVATE
  3. #define CIRCUITPADDING_PRIVATE
  4. #define NETWORKSTATUS_PRIVATE
  5. #include "core/or/or.h"
  6. #include "test.h"
  7. #include "lib/testsupport/testsupport.h"
  8. #include "core/or/connection_or.h"
  9. #include "core/or/channel.h"
  10. #include "core/or/channeltls.h"
  11. #include <event.h>
  12. #include "lib/evloop/compat_libevent.h"
  13. #include "lib/time/compat_time.h"
  14. #include "lib/defs/time.h"
  15. #include "core/or/relay.h"
  16. #include "core/or/circuitlist.h"
  17. #include "core/or/circuitbuild.h"
  18. #include "core/or/circuitpadding.h"
  19. #include "core/mainloop/netstatus.h"
  20. #include "core/crypto/relay_crypto.h"
  21. #include "core/or/protover.h"
  22. #include "feature/nodelist/nodelist.h"
  23. #include "lib/evloop/compat_libevent.h"
  24. #include "app/config/config.h"
  25. #include "feature/nodelist/routerstatus_st.h"
  26. #include "feature/nodelist/networkstatus_st.h"
  27. #include "feature/nodelist/node_st.h"
  28. #include "core/or/cell_st.h"
  29. #include "core/or/crypt_path_st.h"
  30. #include "core/or/or_circuit_st.h"
  31. #include "core/or/origin_circuit_st.h"
  32. /* Start our monotime mocking at 1 second past whatever monotime_init()
  33. * thought the actual wall clock time was, for platforms with bad resolution
  34. * and weird timevalues during monotime_init() before mocking. */
  35. #define MONOTIME_MOCK_START (monotime_absolute_nsec()+\
  36. TOR_NSEC_PER_USEC*TOR_USEC_PER_SEC)
  37. extern smartlist_t *connection_array;
  38. circid_t get_unique_circ_id_by_chan(channel_t *chan);
  39. void helper_create_basic_machine(void);
  40. static void helper_create_conditional_machines(void);
  41. static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
  42. channel_t *new_fake_channel(void);
  43. void test_circuitpadding_negotiation(void *arg);
  44. void test_circuitpadding_wronghop(void *arg);
  45. void test_circuitpadding_conditions(void *arg);
  46. void test_circuitpadding_serialize(void *arg);
  47. void test_circuitpadding_rtt(void *arg);
  48. void test_circuitpadding_tokens(void *arg);
  49. static void
  50. simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
  51. int padding);
  52. void free_fake_orcirc(circuit_t *circ);
  53. void free_fake_origin_circuit(origin_circuit_t *circ);
  54. static int deliver_negotiated = 1;
  55. static int64_t curr_mocked_time;
  56. static node_t padding_node;
  57. static node_t non_padding_node;
  58. static channel_t dummy_channel;
  59. static circpad_machine_spec_t circ_client_machine;
  60. static void
  61. timers_advance_and_run(int64_t msec_update)
  62. {
  63. curr_mocked_time += msec_update*TOR_NSEC_PER_MSEC;
  64. monotime_coarse_set_mock_time_nsec(curr_mocked_time);
  65. monotime_set_mock_time_nsec(curr_mocked_time);
  66. timers_run_pending();
  67. }
  68. static void
  69. nodes_init(void)
  70. {
  71. padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
  72. padding_node.rs->pv.supports_padding = 1;
  73. non_padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
  74. non_padding_node.rs->pv.supports_padding = 0;
  75. }
  76. static void
  77. nodes_free(void)
  78. {
  79. tor_free(padding_node.rs);
  80. tor_free(non_padding_node.rs);
  81. }
  82. static const node_t *
  83. node_get_by_id_mock(const char *identity_digest)
  84. {
  85. if (identity_digest[0] == 1) {
  86. return &padding_node;
  87. } else if (identity_digest[0] == 0) {
  88. return &non_padding_node;
  89. }
  90. return NULL;
  91. }
  92. static or_circuit_t *
  93. new_fake_orcirc(channel_t *nchan, channel_t *pchan)
  94. {
  95. or_circuit_t *orcirc = NULL;
  96. circuit_t *circ = NULL;
  97. crypt_path_t tmp_cpath;
  98. char whatevs_key[CPATH_KEY_MATERIAL_LEN];
  99. orcirc = tor_malloc_zero(sizeof(*orcirc));
  100. circ = &(orcirc->base_);
  101. circ->magic = OR_CIRCUIT_MAGIC;
  102. //circ->n_chan = nchan;
  103. circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
  104. cell_queue_init(&(circ->n_chan_cells));
  105. circ->n_hop = NULL;
  106. circ->streams_blocked_on_n_chan = 0;
  107. circ->streams_blocked_on_p_chan = 0;
  108. circ->n_delete_pending = 0;
  109. circ->p_delete_pending = 0;
  110. circ->received_destroy = 0;
  111. circ->state = CIRCUIT_STATE_OPEN;
  112. circ->purpose = CIRCUIT_PURPOSE_OR;
  113. circ->package_window = CIRCWINDOW_START_MAX;
  114. circ->deliver_window = CIRCWINDOW_START_MAX;
  115. circ->n_chan_create_cell = NULL;
  116. //orcirc->p_chan = pchan;
  117. orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
  118. cell_queue_init(&(orcirc->p_chan_cells));
  119. circuit_set_p_circid_chan(orcirc, orcirc->p_circ_id, pchan);
  120. circuit_set_n_circid_chan(circ, circ->n_circ_id, nchan);
  121. memset(&tmp_cpath, 0, sizeof(tmp_cpath));
  122. if (circuit_init_cpath_crypto(&tmp_cpath, whatevs_key,
  123. sizeof(whatevs_key), 0, 0)<0) {
  124. log_warn(LD_BUG,"Circuit initialization failed");
  125. return NULL;
  126. }
  127. orcirc->crypto = tmp_cpath.crypto;
  128. return orcirc;
  129. }
  130. void
  131. free_fake_orcirc(circuit_t *circ)
  132. {
  133. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  134. relay_crypto_clear(&orcirc->crypto);
  135. circpad_circuit_free_all_machineinfos(circ);
  136. tor_free(circ);
  137. }
  138. void
  139. free_fake_origin_circuit(origin_circuit_t *circ)
  140. {
  141. circpad_circuit_free_all_machineinfos(TO_CIRCUIT(circ));
  142. circuit_clear_cpath(circ);
  143. tor_free(circ);
  144. }
  145. void dummy_nop_timer(void);
  146. //static int dont_stop_libevent = 0;
  147. static circuit_t *client_side;
  148. static circuit_t *relay_side;
  149. static int n_client_cells = 0;
  150. static int n_relay_cells = 0;
  151. static int
  152. circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
  153. cell_direction_t cell_direction,
  154. crypt_path_t *layer_hint, streamid_t on_stream,
  155. const char *filename, int lineno);
  156. static void
  157. circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
  158. cell_direction_t direction);
  159. static void
  160. circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
  161. cell_direction_t direction)
  162. {
  163. (void)cmux;
  164. (void)circ;
  165. (void)direction;
  166. return;
  167. }
  168. static int
  169. circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
  170. cell_direction_t cell_direction,
  171. crypt_path_t *layer_hint, streamid_t on_stream,
  172. const char *filename, int lineno)
  173. {
  174. (void)cell; (void)on_stream; (void)filename; (void)lineno;
  175. if (circ == client_side) {
  176. if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
  177. // Deliver to relay
  178. circpad_handle_padding_negotiate(relay_side, cell);
  179. } else {
  180. int is_target_hop = circpad_padding_is_from_expected_hop(circ,
  181. layer_hint);
  182. tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_OUT);
  183. tt_int_op(is_target_hop, OP_EQ, 1);
  184. // No need to pretend a padding cell was sent: This event is
  185. // now emitted internally when the circuitpadding code sends them.
  186. //circpad_cell_event_padding_sent(client_side);
  187. // Receive padding cell at middle
  188. circpad_deliver_recognized_relay_cell_events(relay_side,
  189. cell->payload[0], NULL);
  190. }
  191. n_client_cells++;
  192. } else if (circ == relay_side) {
  193. tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_IN);
  194. if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATED) {
  195. // XXX: blah need right layer_hint..
  196. if (deliver_negotiated)
  197. circpad_handle_padding_negotiated(client_side, cell,
  198. TO_ORIGIN_CIRCUIT(client_side)
  199. ->cpath->next);
  200. } else if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
  201. circpad_handle_padding_negotiate(client_side, cell);
  202. } else {
  203. // No need to pretend a padding cell was sent: This event is
  204. // now emitted internally when the circuitpadding code sends them.
  205. //circpad_cell_event_padding_sent(relay_side);
  206. // Receive padding cell at client
  207. circpad_deliver_recognized_relay_cell_events(client_side,
  208. cell->payload[0],
  209. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  210. }
  211. n_relay_cells++;
  212. }
  213. done:
  214. timers_advance_and_run(1);
  215. return 0;
  216. }
  217. // Test reading and writing padding to strings (or options_t + consensus)
  218. void
  219. test_circuitpadding_serialize(void *arg)
  220. {
  221. (void)arg;
  222. }
  223. static signed_error_t
  224. circpad_send_command_to_hop_mock(origin_circuit_t *circ, uint8_t hopnum,
  225. uint8_t relay_command, const uint8_t *payload,
  226. ssize_t payload_len)
  227. {
  228. (void) circ;
  229. (void) hopnum;
  230. (void) relay_command;
  231. (void) payload;
  232. (void) payload_len;
  233. return 0;
  234. }
  235. void
  236. test_circuitpadding_rtt(void *arg)
  237. {
  238. /* Test Plan:
  239. *
  240. * 1. Test RTT measurement server side
  241. * a. test usage of measured RTT
  242. * 2. Test termination of RTT measurement
  243. * a. test non-update of RTT
  244. * 3. Test client side circuit and non-application of RTT..
  245. */
  246. circpad_delay_t rtt_estimate;
  247. int64_t actual_mocked_monotime_start;
  248. (void)arg;
  249. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  250. MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
  251. dummy_channel.cmux = circuitmux_alloc();
  252. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  253. client_side = TO_CIRCUIT(origin_circuit_new());
  254. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  255. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  256. monotime_init();
  257. monotime_enable_test_mocking();
  258. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  259. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  260. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  261. curr_mocked_time = actual_mocked_monotime_start;
  262. timers_initialize();
  263. circpad_machines_init();
  264. helper_create_basic_machine();
  265. MOCK(circuit_package_relay_cell,
  266. circuit_package_relay_cell_mock);
  267. client_side->padding_machine[0] = &circ_client_machine;
  268. client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
  269. 0);
  270. relay_side->padding_machine[0] = &circ_client_machine;
  271. relay_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,0);
  272. /* Test 1: Test measuring RTT */
  273. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  274. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
  275. timers_advance_and_run(20);
  276. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  277. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  278. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 19000);
  279. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 30000);
  280. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  281. OP_EQ,
  282. relay_side->padding_info[0]->rtt_estimate_usec+
  283. circpad_machine_current_state(
  284. relay_side->padding_info[0])->histogram_edges[0]);
  285. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  286. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  287. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
  288. timers_advance_and_run(20);
  289. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  290. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  291. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  292. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 20000);
  293. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 21000);
  294. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  295. OP_EQ,
  296. relay_side->padding_info[0]->rtt_estimate_usec+
  297. circpad_machine_current_state(
  298. relay_side->padding_info[0])->histogram_edges[0]);
  299. /* Test 2: Termination of RTT measurement (from the previous test) */
  300. tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  301. rtt_estimate = relay_side->padding_info[0]->rtt_estimate_usec;
  302. circpad_cell_event_nonpadding_received((circuit_t*)relay_side);
  303. timers_advance_and_run(4);
  304. circpad_cell_event_nonpadding_sent((circuit_t*)relay_side);
  305. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_EQ,
  306. rtt_estimate);
  307. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  308. tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  309. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  310. OP_EQ,
  311. relay_side->padding_info[0]->rtt_estimate_usec+
  312. circpad_machine_current_state(
  313. relay_side->padding_info[0])->histogram_edges[0]);
  314. /* Test 3: Make sure client side machine properly ignores RTT */
  315. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  316. tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  317. timers_advance_and_run(20);
  318. circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
  319. tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  320. tt_int_op(client_side->padding_info[0]->rtt_estimate_usec, OP_EQ, 0);
  321. tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
  322. OP_NE, client_side->padding_info[0]->rtt_estimate_usec);
  323. tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
  324. OP_EQ,
  325. circpad_machine_current_state(
  326. client_side->padding_info[0])->histogram_edges[0]);
  327. done:
  328. free_fake_orcirc(relay_side);
  329. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  330. circuitmux_free(dummy_channel.cmux);
  331. timers_shutdown();
  332. monotime_disable_test_mocking();
  333. UNMOCK(circuit_package_relay_cell);
  334. UNMOCK(circuitmux_attach_circuit);
  335. tor_free(circ_client_machine.states);
  336. return;
  337. }
  338. void
  339. helper_create_basic_machine(void)
  340. {
  341. /* Start, burst */
  342. circpad_machine_states_init(&circ_client_machine, 2);
  343. circ_client_machine.states[CIRCPAD_STATE_START].
  344. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  345. circ_client_machine.states[CIRCPAD_STATE_BURST].
  346. next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  347. circ_client_machine.states[CIRCPAD_STATE_BURST].
  348. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  349. circ_client_machine.states[CIRCPAD_STATE_BURST].
  350. next_state[CIRCPAD_EVENT_NONPADDING_SENT] = CIRCPAD_STATE_CANCEL;
  351. circ_client_machine.states[CIRCPAD_STATE_BURST].token_removal =
  352. CIRCPAD_TOKEN_REMOVAL_HIGHER;
  353. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_len = 5;
  354. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 500;
  355. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 2500;
  356. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 5000;
  357. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[3] = 10000;
  358. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[4] = 20000;
  359. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[0] = 1;
  360. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  361. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[2] = 2;
  362. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[3] = 2;
  363. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[4] = 2;
  364. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_total_tokens = 7;
  365. circ_client_machine.states[CIRCPAD_STATE_BURST].use_rtt_estimate = 1;
  366. return;
  367. }
  368. #define BIG_HISTOGRAM_LEN 10
  369. /** Setup a machine with a big histogram */
  370. static void
  371. helper_create_machine_with_big_histogram(circpad_removal_t removal_strategy)
  372. {
  373. const int tokens_per_bin = 2;
  374. /* Start, burst */
  375. circpad_machine_states_init(&circ_client_machine, 2);
  376. circpad_state_t *burst_state =
  377. &circ_client_machine.states[CIRCPAD_STATE_BURST];
  378. circ_client_machine.states[CIRCPAD_STATE_START].
  379. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  380. burst_state->next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  381. burst_state->next_state[CIRCPAD_EVENT_NONPADDING_RECV] =CIRCPAD_STATE_BURST;
  382. burst_state->next_state[CIRCPAD_EVENT_NONPADDING_SENT] =CIRCPAD_STATE_CANCEL;
  383. burst_state->token_removal = CIRCPAD_TOKEN_REMOVAL_HIGHER;
  384. burst_state->histogram_len = BIG_HISTOGRAM_LEN;
  385. int n_tokens = 0;
  386. int i;
  387. for (i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  388. burst_state->histogram[i] = tokens_per_bin;
  389. n_tokens += tokens_per_bin;
  390. }
  391. burst_state->histogram_edges[0] = 0;
  392. burst_state->histogram_edges[1] = 1;
  393. burst_state->histogram_edges[2] = 7;
  394. burst_state->histogram_edges[3] = 15;
  395. burst_state->histogram_edges[4] = 31;
  396. burst_state->histogram_edges[5] = 62;
  397. burst_state->histogram_edges[6] = 125;
  398. burst_state->histogram_edges[7] = 250;
  399. burst_state->histogram_edges[8] = 500;
  400. burst_state->histogram_edges[9] = 1000;
  401. burst_state->histogram_total_tokens = n_tokens;
  402. burst_state->length_dist.type = CIRCPAD_DIST_UNIFORM;
  403. burst_state->length_dist.param1 = n_tokens;
  404. burst_state->length_dist.param2 = n_tokens;
  405. burst_state->max_length = n_tokens;
  406. burst_state->length_includes_nonpadding = 1;
  407. burst_state->use_rtt_estimate = 0;
  408. burst_state->token_removal = removal_strategy;
  409. }
  410. static circpad_decision_t
  411. circpad_machine_schedule_padding_mock(circpad_machine_runtime_t *mi)
  412. {
  413. (void)mi;
  414. return 0;
  415. }
  416. static uint64_t
  417. mock_monotime_absolute_usec(void)
  418. {
  419. return 100;
  420. }
  421. /** Test higher token removal strategy by bin */
  422. static void
  423. test_circuitpadding_token_removal_higher(void *arg)
  424. {
  425. circpad_machine_runtime_t *mi;
  426. (void)arg;
  427. /* Mock it up */
  428. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  429. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  430. /* Setup test environment (time etc.) */
  431. client_side = (circuit_t *)origin_circuit_new();
  432. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  433. monotime_enable_test_mocking();
  434. /* Create test machine */
  435. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_HIGHER);
  436. client_side->padding_machine[0] = &circ_client_machine;
  437. client_side->padding_info[0] =
  438. circpad_circuit_machineinfo_new(client_side, 0);
  439. /* move the machine to the right state */
  440. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  441. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  442. CIRCPAD_STATE_BURST);
  443. /* Get the machine and setup tokens */
  444. mi = client_side->padding_info[0];
  445. tt_assert(mi);
  446. /*************************************************************************/
  447. uint64_t current_time = monotime_absolute_usec();
  448. /* Test left boundaries of each histogram bin: */
  449. const circpad_delay_t bin_left_bounds[] =
  450. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  451. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  452. tt_uint_op(bin_left_bounds[i], OP_EQ,
  453. circpad_histogram_bin_to_usec(mi, i));
  454. }
  455. /* Test right boundaries of each histogram bin: */
  456. const circpad_delay_t bin_right_bounds[] =
  457. {0, 6, 14, 30, 61, 124, 249, 499, 999, CIRCPAD_DELAY_INFINITE-1};
  458. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  459. tt_uint_op(bin_right_bounds[i], OP_EQ,
  460. histogram_get_bin_upper_bound(mi, i));
  461. }
  462. /* Check that all bins have two tokens right now */
  463. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  464. tt_int_op(mi->histogram[i], OP_EQ, 2);
  465. }
  466. /* This is the right order to remove tokens from this histogram. That is, we
  467. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  468. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  469. * the same reason, then from the 5th, etc. */
  470. const int bin_removal_order[] = {4, 5, 6, 7, 8};
  471. unsigned i;
  472. /* Remove all tokens from all bins apart from the infinity bin */
  473. for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
  474. int bin_to_remove = bin_removal_order[i];
  475. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  476. i, bin_to_remove);
  477. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  478. mi->padding_scheduled_at_usec = current_time - 57;
  479. circpad_machine_remove_token(mi);
  480. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  481. mi->padding_scheduled_at_usec = current_time - 57;
  482. circpad_machine_remove_token(mi);
  483. /* Test that we cleaned out this bin. Don't do this in the case of the last
  484. bin since the tokens will get refilled */
  485. if (i != BIG_HISTOGRAM_LEN - 2) {
  486. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  487. }
  488. }
  489. /* Check that all lower bins are not touched */
  490. for (i=0; i < 4 ; i++) {
  491. tt_int_op(mi->histogram[i], OP_EQ, 2);
  492. }
  493. /* Test below the lowest bin, for coverage */
  494. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  495. CIRCPAD_STATE_BURST);
  496. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  497. mi->padding_scheduled_at_usec = current_time;
  498. circpad_machine_remove_token(mi);
  499. tt_int_op(mi->histogram[0], OP_EQ, 1);
  500. done:
  501. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  502. monotime_disable_test_mocking();
  503. tor_free(circ_client_machine.states);
  504. }
  505. /** Test lower token removal strategy by bin */
  506. static void
  507. test_circuitpadding_token_removal_lower(void *arg)
  508. {
  509. circpad_machine_runtime_t *mi;
  510. (void)arg;
  511. /* Mock it up */
  512. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  513. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  514. /* Setup test environment (time etc.) */
  515. client_side = (circuit_t *)origin_circuit_new();
  516. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  517. monotime_enable_test_mocking();
  518. /* Create test machine */
  519. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_LOWER);
  520. client_side->padding_machine[0] = &circ_client_machine;
  521. client_side->padding_info[0] =
  522. circpad_circuit_machineinfo_new(client_side, 0);
  523. /* move the machine to the right state */
  524. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  525. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  526. CIRCPAD_STATE_BURST);
  527. /* Get the machine and setup tokens */
  528. mi = client_side->padding_info[0];
  529. tt_assert(mi);
  530. /*************************************************************************/
  531. uint64_t current_time = monotime_absolute_usec();
  532. /* Test left boundaries of each histogram bin: */
  533. const circpad_delay_t bin_left_bounds[] =
  534. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  535. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  536. tt_uint_op(bin_left_bounds[i], OP_EQ,
  537. circpad_histogram_bin_to_usec(mi, i));
  538. }
  539. /* Check that all bins have two tokens right now */
  540. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  541. tt_int_op(mi->histogram[i], OP_EQ, 2);
  542. }
  543. /* This is the right order to remove tokens from this histogram. That is, we
  544. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  545. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  546. * the same reason, then from the 5th, etc. */
  547. const int bin_removal_order[] = {4, 3, 2, 1, 0};
  548. unsigned i;
  549. /* Remove all tokens from all bins apart from the infinity bin */
  550. for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
  551. int bin_to_remove = bin_removal_order[i];
  552. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  553. i, bin_to_remove);
  554. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  555. mi->padding_scheduled_at_usec = current_time - 57;
  556. circpad_machine_remove_token(mi);
  557. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  558. mi->padding_scheduled_at_usec = current_time - 57;
  559. circpad_machine_remove_token(mi);
  560. /* Test that we cleaned out this bin. Don't do this in the case of the last
  561. bin since the tokens will get refilled */
  562. if (i != BIG_HISTOGRAM_LEN - 2) {
  563. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  564. }
  565. }
  566. /* Check that all higher bins are untouched */
  567. for (i = 5; i < BIG_HISTOGRAM_LEN ; i++) {
  568. tt_int_op(mi->histogram[i], OP_EQ, 2);
  569. }
  570. /* Test above the highest bin, for coverage */
  571. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  572. CIRCPAD_STATE_BURST);
  573. circ_client_machine.states[CIRCPAD_STATE_BURST].
  574. histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
  575. mi->padding_scheduled_at_usec = current_time - 29202;
  576. circpad_machine_remove_token(mi);
  577. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  578. done:
  579. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  580. monotime_disable_test_mocking();
  581. tor_free(circ_client_machine.states);
  582. }
  583. /** Test closest token removal strategy by bin */
  584. static void
  585. test_circuitpadding_closest_token_removal(void *arg)
  586. {
  587. circpad_machine_runtime_t *mi;
  588. (void)arg;
  589. /* Mock it up */
  590. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  591. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  592. /* Setup test environment (time etc.) */
  593. client_side = (circuit_t *)origin_circuit_new();
  594. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  595. monotime_enable_test_mocking();
  596. /* Create test machine */
  597. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST);
  598. client_side->padding_machine[0] = &circ_client_machine;
  599. client_side->padding_info[0] =
  600. circpad_circuit_machineinfo_new(client_side, 0);
  601. /* move the machine to the right state */
  602. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  603. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  604. CIRCPAD_STATE_BURST);
  605. /* Get the machine and setup tokens */
  606. mi = client_side->padding_info[0];
  607. tt_assert(mi);
  608. /*************************************************************************/
  609. uint64_t current_time = monotime_absolute_usec();
  610. /* Test left boundaries of each histogram bin: */
  611. const circpad_delay_t bin_left_bounds[] =
  612. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  613. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  614. tt_uint_op(bin_left_bounds[i], OP_EQ,
  615. circpad_histogram_bin_to_usec(mi, i));
  616. }
  617. /* Check that all bins have two tokens right now */
  618. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  619. tt_int_op(mi->histogram[i], OP_EQ, 2);
  620. }
  621. /* This is the right order to remove tokens from this histogram. That is, we
  622. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  623. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  624. * the same reason, then from the 5th, etc. */
  625. const int bin_removal_order[] = {4, 3, 5, 2, 6, 1, 7, 0, 8, 9};
  626. /* Remove all tokens from all bins apart from the infinity bin */
  627. for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
  628. int bin_to_remove = bin_removal_order[i];
  629. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  630. i, bin_to_remove);
  631. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  632. mi->padding_scheduled_at_usec = current_time - 57;
  633. circpad_machine_remove_token(mi);
  634. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  635. mi->padding_scheduled_at_usec = current_time - 57;
  636. circpad_machine_remove_token(mi);
  637. /* Test that we cleaned out this bin. Don't do this in the case of the last
  638. bin since the tokens will get refilled */
  639. if (i != BIG_HISTOGRAM_LEN - 2) {
  640. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  641. }
  642. }
  643. /* Check that all bins have been refilled */
  644. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  645. tt_int_op(mi->histogram[i], OP_EQ, 2);
  646. }
  647. /* Test below the lowest bin, for coverage */
  648. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  649. CIRCPAD_STATE_BURST);
  650. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  651. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
  652. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
  653. mi->padding_scheduled_at_usec = current_time - 102;
  654. mi->histogram[0] = 0;
  655. circpad_machine_remove_token(mi);
  656. tt_int_op(mi->histogram[1], OP_EQ, 1);
  657. /* Test above the highest bin, for coverage */
  658. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  659. CIRCPAD_STATE_BURST);
  660. mi->padding_scheduled_at_usec = current_time - 29202;
  661. circpad_machine_remove_token(mi);
  662. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  663. done:
  664. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  665. monotime_disable_test_mocking();
  666. tor_free(circ_client_machine.states);
  667. }
  668. /** Test closest token removal strategy with usec */
  669. static void
  670. test_circuitpadding_closest_token_removal_usec(void *arg)
  671. {
  672. circpad_machine_runtime_t *mi;
  673. (void)arg;
  674. /* Mock it up */
  675. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  676. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  677. /* Setup test environment (time etc.) */
  678. client_side = (circuit_t *)origin_circuit_new();
  679. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  680. monotime_enable_test_mocking();
  681. /* Create test machine */
  682. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST_USEC);
  683. client_side->padding_machine[0] = &circ_client_machine;
  684. client_side->padding_info[0] =
  685. circpad_circuit_machineinfo_new(client_side, 0);
  686. /* move the machine to the right state */
  687. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  688. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  689. CIRCPAD_STATE_BURST);
  690. /* Get the machine and setup tokens */
  691. mi = client_side->padding_info[0];
  692. tt_assert(mi);
  693. /*************************************************************************/
  694. uint64_t current_time = monotime_absolute_usec();
  695. /* Test left boundaries of each histogram bin: */
  696. const circpad_delay_t bin_left_bounds[] =
  697. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  698. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  699. tt_uint_op(bin_left_bounds[i], OP_EQ,
  700. circpad_histogram_bin_to_usec(mi, i));
  701. }
  702. /* XXX we want to test remove_token_exact and
  703. circpad_machine_remove_closest_token() with usec */
  704. /* Check that all bins have two tokens right now */
  705. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  706. tt_int_op(mi->histogram[i], OP_EQ, 2);
  707. }
  708. /* This is the right order to remove tokens from this histogram. That is, we
  709. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  710. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  711. * the same reason, then from the 5th, etc. */
  712. const int bin_removal_order[] = {4, 3, 5, 2, 1, 0, 6, 7, 8, 9};
  713. /* Remove all tokens from all bins apart from the infinity bin */
  714. for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
  715. int bin_to_remove = bin_removal_order[i];
  716. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  717. i, bin_to_remove);
  718. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  719. mi->padding_scheduled_at_usec = current_time - 57;
  720. circpad_machine_remove_token(mi);
  721. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  722. mi->padding_scheduled_at_usec = current_time - 57;
  723. circpad_machine_remove_token(mi);
  724. /* Test that we cleaned out this bin. Don't do this in the case of the last
  725. bin since the tokens will get refilled */
  726. if (i != BIG_HISTOGRAM_LEN - 2) {
  727. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  728. }
  729. }
  730. /* Check that all bins have been refilled */
  731. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  732. tt_int_op(mi->histogram[i], OP_EQ, 2);
  733. }
  734. /* Test below the lowest bin, for coverage */
  735. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  736. CIRCPAD_STATE_BURST);
  737. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  738. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
  739. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
  740. mi->padding_scheduled_at_usec = current_time - 102;
  741. mi->histogram[0] = 0;
  742. circpad_machine_remove_token(mi);
  743. tt_int_op(mi->histogram[1], OP_EQ, 1);
  744. /* Test above the highest bin, for coverage */
  745. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  746. CIRCPAD_STATE_BURST);
  747. circ_client_machine.states[CIRCPAD_STATE_BURST].
  748. histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
  749. mi->padding_scheduled_at_usec = current_time - 29202;
  750. circpad_machine_remove_token(mi);
  751. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  752. done:
  753. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  754. monotime_disable_test_mocking();
  755. tor_free(circ_client_machine.states);
  756. }
  757. /** Test closest token removal strategy with usec */
  758. static void
  759. test_circuitpadding_token_removal_exact(void *arg)
  760. {
  761. circpad_machine_runtime_t *mi;
  762. (void)arg;
  763. /* Mock it up */
  764. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  765. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  766. /* Setup test environment (time etc.) */
  767. client_side = (circuit_t *)origin_circuit_new();
  768. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  769. monotime_enable_test_mocking();
  770. /* Create test machine */
  771. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_EXACT);
  772. client_side->padding_machine[0] = &circ_client_machine;
  773. client_side->padding_info[0] =
  774. circpad_circuit_machineinfo_new(client_side, 0);
  775. /* move the machine to the right state */
  776. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  777. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  778. CIRCPAD_STATE_BURST);
  779. /* Get the machine and setup tokens */
  780. mi = client_side->padding_info[0];
  781. tt_assert(mi);
  782. /**********************************************************************/
  783. uint64_t current_time = monotime_absolute_usec();
  784. /* Ensure that we will clear out bin #4 with this usec */
  785. mi->padding_scheduled_at_usec = current_time - 57;
  786. tt_int_op(mi->histogram[4], OP_EQ, 2);
  787. circpad_machine_remove_token(mi);
  788. mi->padding_scheduled_at_usec = current_time - 57;
  789. tt_int_op(mi->histogram[4], OP_EQ, 1);
  790. circpad_machine_remove_token(mi);
  791. tt_int_op(mi->histogram[4], OP_EQ, 0);
  792. /* Ensure that we will not remove any other tokens even tho we try to, since
  793. * this is what the exact strategy dictates */
  794. mi->padding_scheduled_at_usec = current_time - 57;
  795. circpad_machine_remove_token(mi);
  796. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  797. if (i != 4) {
  798. tt_int_op(mi->histogram[i], OP_EQ, 2);
  799. }
  800. }
  801. done:
  802. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  803. monotime_disable_test_mocking();
  804. tor_free(circ_client_machine.states);
  805. }
  806. #undef BIG_HISTOGRAM_LEN
  807. void
  808. test_circuitpadding_tokens(void *arg)
  809. {
  810. const circpad_state_t *state;
  811. circpad_machine_runtime_t *mi;
  812. int64_t actual_mocked_monotime_start;
  813. (void)arg;
  814. /** Test plan:
  815. *
  816. * 1. Test symmetry between bin_to_usec and usec_to_bin
  817. * a. Test conversion
  818. * b. Test edge transitions (lower, upper)
  819. * 2. Test remove higher on an empty bin
  820. * a. Normal bin
  821. * b. Infinity bin
  822. * c. Bin 0
  823. * d. No higher
  824. * 3. Test remove lower
  825. * a. Normal bin
  826. * b. Bin 0
  827. * c. No lower
  828. * 4. Test remove closest
  829. * a. Closest lower
  830. * b. Closest higher
  831. * c. Closest 0
  832. * d. Closest Infinity
  833. */
  834. client_side = TO_CIRCUIT(origin_circuit_new());
  835. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  836. monotime_init();
  837. monotime_enable_test_mocking();
  838. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  839. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  840. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  841. curr_mocked_time = actual_mocked_monotime_start;
  842. /* This is needed so that we are not considered to be dormant */
  843. note_user_activity(20);
  844. timers_initialize();
  845. helper_create_basic_machine();
  846. client_side->padding_machine[0] = &circ_client_machine;
  847. client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
  848. 0);
  849. mi = client_side->padding_info[0];
  850. // Pretend a non-padding cell was sent
  851. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  852. circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
  853. /* We have to save the infinity bin because one inf delay
  854. * could have been chosen when we transition to burst */
  855. circpad_hist_token_t inf_bin = mi->histogram[4];
  856. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  857. CIRCPAD_STATE_BURST);
  858. state = circpad_machine_current_state(client_side->padding_info[0]);
  859. // Test 0: convert bin->usec->bin
  860. // Bin 0+1 have different semantics
  861. for (int bin = 0; bin < 2; bin++) {
  862. circpad_delay_t usec =
  863. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  864. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  865. usec);
  866. tt_int_op(bin, OP_EQ, bin2);
  867. }
  868. for (int bin = 2; bin < state->histogram_len-1; bin++) {
  869. circpad_delay_t usec =
  870. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  871. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  872. usec);
  873. tt_int_op(bin, OP_EQ, bin2);
  874. /* Verify we round down */
  875. bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  876. usec+3);
  877. tt_int_op(bin, OP_EQ, bin2);
  878. bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  879. usec-1);
  880. tt_int_op(bin, OP_EQ, bin2+1);
  881. }
  882. // Test 1: converting usec->bin->usec->bin
  883. // Bin 0+1 have different semantics.
  884. for (circpad_delay_t i = 0; i <= state->histogram_edges[0]; i++) {
  885. int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  886. i);
  887. circpad_delay_t usec =
  888. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  889. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  890. usec);
  891. tt_int_op(bin, OP_EQ, bin2);
  892. tt_int_op(i, OP_LE, usec);
  893. }
  894. for (circpad_delay_t i = state->histogram_edges[0]+1;
  895. i <= state->histogram_edges[0] +
  896. state->histogram_edges[state->histogram_len-2]; i++) {
  897. int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  898. i);
  899. circpad_delay_t usec =
  900. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  901. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  902. usec);
  903. tt_int_op(bin, OP_EQ, bin2);
  904. tt_int_op(i, OP_GE, usec);
  905. }
  906. /* 2.a. Normal higher bin */
  907. {
  908. tt_int_op(mi->histogram[2], OP_EQ, 2);
  909. tt_int_op(mi->histogram[3], OP_EQ, 2);
  910. circpad_machine_remove_higher_token(mi,
  911. circpad_histogram_bin_to_usec(mi, 2)+1);
  912. tt_int_op(mi->histogram[3], OP_EQ, 2);
  913. tt_int_op(mi->histogram[2], OP_EQ, 1);
  914. circpad_machine_remove_higher_token(mi,
  915. circpad_histogram_bin_to_usec(mi, 2)+1);
  916. tt_int_op(mi->histogram[2], OP_EQ, 0);
  917. tt_int_op(mi->histogram[3], OP_EQ, 2);
  918. circpad_machine_remove_higher_token(mi,
  919. circpad_histogram_bin_to_usec(mi, 2)+1);
  920. circpad_machine_remove_higher_token(mi,
  921. circpad_histogram_bin_to_usec(mi, 2)+1);
  922. tt_int_op(mi->histogram[3], OP_EQ, 0);
  923. circpad_machine_remove_higher_token(mi,
  924. circpad_histogram_bin_to_usec(mi, 2)+1);
  925. tt_int_op(mi->histogram[3], OP_EQ, 0);
  926. }
  927. /* 2.b. Higher Infinity bin */
  928. {
  929. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  930. circpad_machine_remove_higher_token(mi,
  931. circpad_histogram_bin_to_usec(mi, 2)+1);
  932. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  933. /* Test past the infinity bin */
  934. circpad_machine_remove_higher_token(mi,
  935. circpad_histogram_bin_to_usec(mi, 5)+1000000);
  936. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  937. }
  938. /* 2.c. Bin 0 */
  939. {
  940. tt_int_op(mi->histogram[0], OP_EQ, 0);
  941. mi->histogram[0] = 1;
  942. circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
  943. tt_int_op(mi->histogram[0], OP_EQ, 0);
  944. }
  945. /* Drain the infinity bin and cause a refill */
  946. while (inf_bin != 0) {
  947. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  948. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  949. inf_bin--;
  950. }
  951. circpad_cell_event_nonpadding_sent((circuit_t*)client_side);
  952. // We should have refilled here.
  953. tt_int_op(mi->histogram[4], OP_EQ, 2);
  954. /* 3.a. Bin 0 */
  955. {
  956. tt_int_op(mi->histogram[0], OP_EQ, 1);
  957. circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
  958. tt_int_op(mi->histogram[0], OP_EQ, 0);
  959. }
  960. /* 3.b. Test remove lower normal bin */
  961. {
  962. tt_int_op(mi->histogram[3], OP_EQ, 2);
  963. circpad_machine_remove_lower_token(mi,
  964. circpad_histogram_bin_to_usec(mi, 3)+1);
  965. circpad_machine_remove_lower_token(mi,
  966. circpad_histogram_bin_to_usec(mi, 3)+1);
  967. tt_int_op(mi->histogram[3], OP_EQ, 0);
  968. tt_int_op(mi->histogram[2], OP_EQ, 2);
  969. circpad_machine_remove_lower_token(mi,
  970. circpad_histogram_bin_to_usec(mi, 3)+1);
  971. circpad_machine_remove_lower_token(mi,
  972. circpad_histogram_bin_to_usec(mi, 3)+1);
  973. /* 3.c. No lower */
  974. circpad_machine_remove_lower_token(mi,
  975. circpad_histogram_bin_to_usec(mi, 3)+1);
  976. tt_int_op(mi->histogram[2], OP_EQ, 0);
  977. }
  978. /* 4. Test remove closest
  979. * a. Closest lower
  980. * b. Closest higher
  981. * c. Closest 0
  982. * d. Closest Infinity
  983. */
  984. circpad_machine_setup_tokens(mi);
  985. tt_int_op(mi->histogram[2], OP_EQ, 2);
  986. circpad_machine_remove_closest_token(mi,
  987. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  988. circpad_machine_remove_closest_token(mi,
  989. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  990. tt_int_op(mi->histogram[2], OP_EQ, 0);
  991. tt_int_op(mi->histogram[3], OP_EQ, 2);
  992. circpad_machine_remove_closest_token(mi,
  993. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  994. circpad_machine_remove_closest_token(mi,
  995. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  996. tt_int_op(mi->histogram[3], OP_EQ, 0);
  997. tt_int_op(mi->histogram[0], OP_EQ, 1);
  998. circpad_machine_remove_closest_token(mi,
  999. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1000. tt_int_op(mi->histogram[0], OP_EQ, 0);
  1001. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1002. circpad_machine_remove_closest_token(mi,
  1003. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1004. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1005. /* 5. Test remove closest usec
  1006. * a. Closest 0
  1007. * b. Closest lower (below midpoint)
  1008. * c. Closest higher (above midpoint)
  1009. * d. Closest Infinity
  1010. */
  1011. circpad_machine_setup_tokens(mi);
  1012. tt_int_op(mi->histogram[0], OP_EQ, 1);
  1013. circpad_machine_remove_closest_token(mi,
  1014. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1015. tt_int_op(mi->histogram[0], OP_EQ, 0);
  1016. tt_int_op(mi->histogram[2], OP_EQ, 2);
  1017. circpad_machine_remove_closest_token(mi,
  1018. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1019. circpad_machine_remove_closest_token(mi,
  1020. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1021. tt_int_op(mi->histogram[2], OP_EQ, 0);
  1022. tt_int_op(mi->histogram[3], OP_EQ, 2);
  1023. circpad_machine_remove_closest_token(mi,
  1024. circpad_histogram_bin_to_usec(mi, 4), 1);
  1025. circpad_machine_remove_closest_token(mi,
  1026. circpad_histogram_bin_to_usec(mi, 4), 1);
  1027. tt_int_op(mi->histogram[3], OP_EQ, 0);
  1028. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1029. circpad_machine_remove_closest_token(mi,
  1030. circpad_histogram_bin_to_usec(mi, 4), 1);
  1031. circpad_machine_remove_closest_token(mi,
  1032. circpad_histogram_bin_to_usec(mi, 4), 1);
  1033. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1034. // XXX: Need more coverage of the actual usec branches
  1035. done:
  1036. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1037. monotime_disable_test_mocking();
  1038. tor_free(circ_client_machine.states);
  1039. }
  1040. void
  1041. test_circuitpadding_wronghop(void *arg)
  1042. {
  1043. /**
  1044. * Test plan:
  1045. * 1. Padding sent from hop 1 and 3 to client
  1046. * 2. Send negotiated from hop 1 and 3 to client
  1047. * 3. Garbled negotiated cell
  1048. * 4. Padding negotiate sent to client
  1049. * 5. Send negotiate stop command for unknown machine
  1050. * 6. Send negotiated to relay
  1051. * 7. Garbled padding negotiate cell
  1052. */
  1053. (void)arg;
  1054. uint32_t read_bw = 0, overhead_bw = 0;
  1055. cell_t cell;
  1056. signed_error_t ret;
  1057. origin_circuit_t *orig_client;
  1058. int64_t actual_mocked_monotime_start;
  1059. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1060. /* Mock this function so that our cell counting tests don't get confused by
  1061. * padding that gets sent by scheduled timers. */
  1062. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  1063. client_side = (circuit_t *)origin_circuit_new();
  1064. dummy_channel.cmux = circuitmux_alloc();
  1065. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
  1066. &dummy_channel);
  1067. orig_client = TO_ORIGIN_CIRCUIT(client_side);
  1068. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1069. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1070. nodes_init();
  1071. monotime_init();
  1072. monotime_enable_test_mocking();
  1073. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1074. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1075. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1076. curr_mocked_time = actual_mocked_monotime_start;
  1077. timers_initialize();
  1078. circpad_machines_init();
  1079. MOCK(node_get_by_id,
  1080. node_get_by_id_mock);
  1081. MOCK(circuit_package_relay_cell,
  1082. circuit_package_relay_cell_mock);
  1083. /* Build three hops */
  1084. simulate_single_hop_extend(client_side, relay_side, 1);
  1085. simulate_single_hop_extend(client_side, relay_side, 1);
  1086. simulate_single_hop_extend(client_side, relay_side, 1);
  1087. /* verify padding was negotiated */
  1088. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1089. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1090. /* verify echo was sent */
  1091. tt_int_op(n_relay_cells, OP_EQ, 1);
  1092. tt_int_op(n_client_cells, OP_EQ, 1);
  1093. read_bw = orig_client->n_delivered_read_circ_bw;
  1094. overhead_bw = orig_client->n_overhead_read_circ_bw;
  1095. /* 1. Test padding from first and third hop */
  1096. circpad_deliver_recognized_relay_cell_events(client_side,
  1097. RELAY_COMMAND_DROP,
  1098. TO_ORIGIN_CIRCUIT(client_side)->cpath);
  1099. tt_int_op(read_bw, OP_EQ,
  1100. orig_client->n_delivered_read_circ_bw);
  1101. tt_int_op(overhead_bw, OP_EQ,
  1102. orig_client->n_overhead_read_circ_bw);
  1103. circpad_deliver_recognized_relay_cell_events(client_side,
  1104. RELAY_COMMAND_DROP,
  1105. TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
  1106. tt_int_op(read_bw, OP_EQ,
  1107. orig_client->n_delivered_read_circ_bw);
  1108. tt_int_op(overhead_bw, OP_EQ,
  1109. orig_client->n_overhead_read_circ_bw);
  1110. circpad_deliver_recognized_relay_cell_events(client_side,
  1111. RELAY_COMMAND_DROP,
  1112. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1113. tt_int_op(read_bw, OP_EQ,
  1114. orig_client->n_delivered_read_circ_bw);
  1115. tt_int_op(overhead_bw, OP_LT,
  1116. orig_client->n_overhead_read_circ_bw);
  1117. /* 2. Test padding negotiated not handled from hops 1,3 */
  1118. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1119. TO_ORIGIN_CIRCUIT(client_side)->cpath);
  1120. tt_int_op(ret, OP_EQ, -1);
  1121. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1122. TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
  1123. tt_int_op(ret, OP_EQ, -1);
  1124. /* 3. Garbled negotiated cell */
  1125. memset(&cell, 255, sizeof(cell));
  1126. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1127. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1128. tt_int_op(ret, OP_EQ, -1);
  1129. /* 4. Test that negotiate is dropped at origin */
  1130. read_bw = orig_client->n_delivered_read_circ_bw;
  1131. overhead_bw = orig_client->n_overhead_read_circ_bw;
  1132. relay_send_command_from_edge(0, relay_side,
  1133. RELAY_COMMAND_PADDING_NEGOTIATE,
  1134. (void*)cell.payload,
  1135. (size_t)3, NULL);
  1136. tt_int_op(read_bw, OP_EQ,
  1137. orig_client->n_delivered_read_circ_bw);
  1138. tt_int_op(overhead_bw, OP_EQ,
  1139. orig_client->n_overhead_read_circ_bw);
  1140. tt_int_op(n_relay_cells, OP_EQ, 2);
  1141. tt_int_op(n_client_cells, OP_EQ, 1);
  1142. /* 5. Test that asking to stop the wrong machine does nothing */
  1143. circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
  1144. 255, 2, CIRCPAD_COMMAND_STOP);
  1145. tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
  1146. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  1147. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1148. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1149. tt_int_op(n_relay_cells, OP_EQ, 3);
  1150. tt_int_op(n_client_cells, OP_EQ, 2);
  1151. /* 6. Sending negotiated command to relay does nothing */
  1152. ret = circpad_handle_padding_negotiated(relay_side, &cell, NULL);
  1153. tt_int_op(ret, OP_EQ, -1);
  1154. /* 7. Test garbled negotated cell (bad command 255) */
  1155. memset(&cell, 0, sizeof(cell));
  1156. ret = circpad_handle_padding_negotiate(relay_side, &cell);
  1157. tt_int_op(ret, OP_EQ, -1);
  1158. tt_int_op(n_client_cells, OP_EQ, 2);
  1159. /* Test 2: Test no padding */
  1160. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1161. free_fake_orcirc(relay_side);
  1162. client_side = (circuit_t *)origin_circuit_new();
  1163. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
  1164. &dummy_channel);
  1165. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1166. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1167. simulate_single_hop_extend(client_side, relay_side, 1);
  1168. simulate_single_hop_extend(client_side, relay_side, 0);
  1169. /* verify no padding was negotiated */
  1170. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1171. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1172. tt_int_op(n_relay_cells, OP_EQ, 3);
  1173. tt_int_op(n_client_cells, OP_EQ, 2);
  1174. /* verify no echo was sent */
  1175. tt_int_op(n_relay_cells, OP_EQ, 3);
  1176. tt_int_op(n_client_cells, OP_EQ, 2);
  1177. /* Finish circuit */
  1178. simulate_single_hop_extend(client_side, relay_side, 1);
  1179. /* Spoof padding negotiated on circuit with no padding */
  1180. circpad_padding_negotiated(relay_side,
  1181. CIRCPAD_MACHINE_CIRC_SETUP,
  1182. CIRCPAD_COMMAND_START,
  1183. CIRCPAD_RESPONSE_OK);
  1184. /* verify no padding was negotiated */
  1185. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1186. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1187. circpad_padding_negotiated(relay_side,
  1188. CIRCPAD_MACHINE_CIRC_SETUP,
  1189. CIRCPAD_COMMAND_START,
  1190. CIRCPAD_RESPONSE_ERR);
  1191. /* verify no padding was negotiated */
  1192. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1193. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1194. done:
  1195. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1196. free_fake_orcirc(relay_side);
  1197. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1198. circuitmux_free(dummy_channel.cmux);
  1199. monotime_disable_test_mocking();
  1200. UNMOCK(node_get_by_id);
  1201. UNMOCK(circuit_package_relay_cell);
  1202. UNMOCK(circuitmux_attach_circuit);
  1203. nodes_free();
  1204. }
  1205. void
  1206. test_circuitpadding_negotiation(void *arg)
  1207. {
  1208. /**
  1209. * Test plan:
  1210. * 1. Test circuit where padding is supported by middle
  1211. * a. Make sure padding negotiation is sent
  1212. * b. Test padding negotiation delivery and parsing
  1213. * 2. Test circuit where padding is unsupported by middle
  1214. * a. Make sure padding negotiation is not sent
  1215. * 3. Test failure to negotiate a machine due to desync.
  1216. */
  1217. int64_t actual_mocked_monotime_start;
  1218. (void)arg;
  1219. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1220. client_side = TO_CIRCUIT(origin_circuit_new());
  1221. dummy_channel.cmux = circuitmux_alloc();
  1222. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1223. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1224. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1225. nodes_init();
  1226. monotime_init();
  1227. monotime_enable_test_mocking();
  1228. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1229. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1230. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1231. curr_mocked_time = actual_mocked_monotime_start;
  1232. timers_initialize();
  1233. circpad_machines_init();
  1234. MOCK(node_get_by_id,
  1235. node_get_by_id_mock);
  1236. MOCK(circuit_package_relay_cell,
  1237. circuit_package_relay_cell_mock);
  1238. /* Build two hops */
  1239. simulate_single_hop_extend(client_side, relay_side, 1);
  1240. simulate_single_hop_extend(client_side, relay_side, 1);
  1241. /* verify padding was negotiated */
  1242. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1243. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1244. /* verify echo was sent */
  1245. tt_int_op(n_relay_cells, OP_EQ, 1);
  1246. tt_int_op(n_client_cells, OP_EQ, 1);
  1247. /* Finish circuit */
  1248. simulate_single_hop_extend(client_side, relay_side, 1);
  1249. /* Test 2: Test no padding */
  1250. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1251. free_fake_orcirc(relay_side);
  1252. client_side = TO_CIRCUIT(origin_circuit_new());
  1253. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1254. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1255. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1256. simulate_single_hop_extend(client_side, relay_side, 1);
  1257. simulate_single_hop_extend(client_side, relay_side, 0);
  1258. /* verify no padding was negotiated */
  1259. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1260. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1261. tt_int_op(n_relay_cells, OP_EQ, 1);
  1262. tt_int_op(n_client_cells, OP_EQ, 1);
  1263. /* verify no echo was sent */
  1264. tt_int_op(n_relay_cells, OP_EQ, 1);
  1265. tt_int_op(n_client_cells, OP_EQ, 1);
  1266. /* Finish circuit */
  1267. simulate_single_hop_extend(client_side, relay_side, 1);
  1268. /* Force negotiate padding. */
  1269. circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
  1270. CIRCPAD_MACHINE_CIRC_SETUP,
  1271. 2, CIRCPAD_COMMAND_START);
  1272. /* verify no padding was negotiated */
  1273. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1274. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1275. /* verify no echo was sent */
  1276. tt_int_op(n_relay_cells, OP_EQ, 1);
  1277. tt_int_op(n_client_cells, OP_EQ, 1);
  1278. /* 3. Test failure to negotiate a machine due to desync */
  1279. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1280. free_fake_orcirc(relay_side);
  1281. client_side = TO_CIRCUIT(origin_circuit_new());
  1282. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1283. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1284. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1285. SMARTLIST_FOREACH(relay_padding_machines,
  1286. circpad_machine_spec_t *,
  1287. m, tor_free(m->states); tor_free(m));
  1288. smartlist_free(relay_padding_machines);
  1289. relay_padding_machines = smartlist_new();
  1290. simulate_single_hop_extend(client_side, relay_side, 1);
  1291. simulate_single_hop_extend(client_side, relay_side, 1);
  1292. /* verify echo was sent */
  1293. tt_int_op(n_client_cells, OP_EQ, 2);
  1294. tt_int_op(n_relay_cells, OP_EQ, 2);
  1295. /* verify no padding was negotiated */
  1296. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1297. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1298. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1299. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1300. done:
  1301. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1302. free_fake_orcirc(relay_side);
  1303. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1304. circuitmux_free(dummy_channel.cmux);
  1305. monotime_disable_test_mocking();
  1306. UNMOCK(node_get_by_id);
  1307. UNMOCK(circuit_package_relay_cell);
  1308. UNMOCK(circuitmux_attach_circuit);
  1309. nodes_free();
  1310. }
  1311. static void
  1312. simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
  1313. int padding)
  1314. {
  1315. char whatevs_key[CPATH_KEY_MATERIAL_LEN];
  1316. char digest[DIGEST_LEN];
  1317. tor_addr_t addr;
  1318. // Pretend a non-padding cell was sent
  1319. circpad_cell_event_nonpadding_sent((circuit_t*)client);
  1320. // Receive extend cell at middle
  1321. circpad_cell_event_nonpadding_received((circuit_t*)mid_relay);
  1322. // Advance time a tiny bit so we can calculate an RTT
  1323. curr_mocked_time += 10 * TOR_NSEC_PER_MSEC;
  1324. monotime_coarse_set_mock_time_nsec(curr_mocked_time);
  1325. monotime_set_mock_time_nsec(curr_mocked_time);
  1326. // Receive extended cell at middle
  1327. circpad_cell_event_nonpadding_sent((circuit_t*)mid_relay);
  1328. // Receive extended cell at first hop
  1329. circpad_cell_event_nonpadding_received((circuit_t*)client);
  1330. // Add a hop to cpath
  1331. crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
  1332. onion_append_to_cpath(&TO_ORIGIN_CIRCUIT(client)->cpath, hop);
  1333. hop->magic = CRYPT_PATH_MAGIC;
  1334. hop->state = CPATH_STATE_OPEN;
  1335. // add an extend info to indicate if this node supports padding or not.
  1336. // (set the first byte of the digest for our mocked node_get_by_id)
  1337. digest[0] = padding;
  1338. hop->extend_info = extend_info_new(
  1339. padding ? "padding" : "non-padding",
  1340. digest, NULL, NULL, NULL,
  1341. &addr, padding);
  1342. circuit_init_cpath_crypto(hop, whatevs_key, sizeof(whatevs_key), 0, 0);
  1343. hop->package_window = circuit_initial_package_window();
  1344. hop->deliver_window = CIRCWINDOW_START;
  1345. // Signal that the hop was added
  1346. circpad_machine_event_circ_added_hop(TO_ORIGIN_CIRCUIT(client));
  1347. }
  1348. static circpad_machine_spec_t *
  1349. helper_create_conditional_machine(void)
  1350. {
  1351. circpad_machine_spec_t *ret =
  1352. tor_malloc_zero(sizeof(circpad_machine_spec_t));
  1353. /* Start, burst */
  1354. circpad_machine_states_init(ret, 2);
  1355. ret->states[CIRCPAD_STATE_START].
  1356. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1357. ret->states[CIRCPAD_STATE_BURST].
  1358. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1359. ret->states[CIRCPAD_STATE_BURST].
  1360. next_state[CIRCPAD_EVENT_LENGTH_COUNT] = CIRCPAD_STATE_END;
  1361. /* Use EXACT removal strategy, otherwise setup_tokens() does not work */
  1362. ret->states[CIRCPAD_STATE_BURST].token_removal =
  1363. CIRCPAD_TOKEN_REMOVAL_EXACT;
  1364. ret->states[CIRCPAD_STATE_BURST].histogram_len = 3;
  1365. ret->states[CIRCPAD_STATE_BURST].histogram_edges[0] = 0;
  1366. ret->states[CIRCPAD_STATE_BURST].histogram_edges[1] = 1;
  1367. ret->states[CIRCPAD_STATE_BURST].histogram_edges[2] = 1000000;
  1368. ret->states[CIRCPAD_STATE_BURST].histogram[0] = 6;
  1369. ret->states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  1370. ret->states[CIRCPAD_STATE_BURST].histogram[2] = 0;
  1371. ret->states[CIRCPAD_STATE_BURST].histogram_total_tokens = 6;
  1372. ret->states[CIRCPAD_STATE_BURST].use_rtt_estimate = 0;
  1373. ret->states[CIRCPAD_STATE_BURST].length_includes_nonpadding = 1;
  1374. return ret;
  1375. }
  1376. static void
  1377. helper_create_conditional_machines(void)
  1378. {
  1379. circpad_machine_spec_t *add = helper_create_conditional_machine();
  1380. origin_padding_machines = smartlist_new();
  1381. relay_padding_machines = smartlist_new();
  1382. add->machine_num = 2;
  1383. add->is_origin_side = 1;
  1384. add->should_negotiate_end = 1;
  1385. add->target_hopnum = 2;
  1386. /* Let's have this one end after 4 packets */
  1387. add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
  1388. add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
  1389. add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
  1390. add->states[CIRCPAD_STATE_BURST].max_length = 4;
  1391. add->conditions.requires_vanguards = 0;
  1392. add->conditions.min_hops = 2;
  1393. add->conditions.state_mask = CIRCPAD_CIRC_BUILDING|
  1394. CIRCPAD_CIRC_NO_STREAMS|CIRCPAD_CIRC_HAS_RELAY_EARLY;
  1395. add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
  1396. register_padding_machine(add, origin_padding_machines);
  1397. add = helper_create_conditional_machine();
  1398. add->machine_num = 3;
  1399. add->is_origin_side = 1;
  1400. add->should_negotiate_end = 1;
  1401. add->target_hopnum = 2;
  1402. /* Let's have this one end after 4 packets */
  1403. add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
  1404. add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
  1405. add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
  1406. add->states[CIRCPAD_STATE_BURST].max_length = 4;
  1407. add->conditions.requires_vanguards = 1;
  1408. add->conditions.min_hops = 3;
  1409. add->conditions.state_mask = CIRCPAD_CIRC_OPENED|
  1410. CIRCPAD_CIRC_STREAMS|CIRCPAD_CIRC_HAS_NO_RELAY_EARLY;
  1411. add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
  1412. register_padding_machine(add, origin_padding_machines);
  1413. add = helper_create_conditional_machine();
  1414. add->machine_num = 2;
  1415. register_padding_machine(add, relay_padding_machines);
  1416. add = helper_create_conditional_machine();
  1417. add->machine_num = 3;
  1418. register_padding_machine(add, relay_padding_machines);
  1419. }
  1420. void
  1421. test_circuitpadding_conditions(void *arg)
  1422. {
  1423. /**
  1424. * Test plan:
  1425. * 0. Make a few origin and client machines with diff conditions
  1426. * * vanguards, purposes, has_opened circs, no relay early
  1427. * * Client side should_negotiate_end
  1428. * * Length limits
  1429. * 1. Test STATE_END transitions
  1430. * 2. Test new machine after end with same conditions
  1431. * 3. Test new machine due to changed conditions
  1432. * * Esp: built event, no relay early, no streams
  1433. * XXX: Diff test:
  1434. * 1. Test STATE_END with pending timers
  1435. * 2. Test marking a circuit before padding callback fires
  1436. * 3. Test freeing a circuit before padding callback fires
  1437. */
  1438. int64_t actual_mocked_monotime_start;
  1439. (void)arg;
  1440. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1441. nodes_init();
  1442. dummy_channel.cmux = circuitmux_alloc();
  1443. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
  1444. &dummy_channel);
  1445. client_side = (circuit_t *)origin_circuit_new();
  1446. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1447. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1448. monotime_init();
  1449. monotime_enable_test_mocking();
  1450. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1451. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1452. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1453. curr_mocked_time = actual_mocked_monotime_start;
  1454. /* This is needed so that we are not considered to be dormant */
  1455. note_user_activity(20);
  1456. timers_initialize();
  1457. helper_create_conditional_machines();
  1458. MOCK(circuit_package_relay_cell,
  1459. circuit_package_relay_cell_mock);
  1460. MOCK(node_get_by_id,
  1461. node_get_by_id_mock);
  1462. /* Simulate extend. This should result in the original machine getting
  1463. * added, since the circuit is not built */
  1464. simulate_single_hop_extend(client_side, relay_side, 1);
  1465. simulate_single_hop_extend(client_side, relay_side, 1);
  1466. /* Verify that machine #2 is added */
  1467. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1468. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1469. /* Deliver a padding cell to the client, to trigger burst state */
  1470. circpad_cell_event_padding_sent(client_side);
  1471. /* This should have trigger length shutdown condition on client.. */
  1472. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1473. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1474. /* Verify machine is gone from both sides */
  1475. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1476. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1477. /* Send another event.. verify machine gets re-added properly
  1478. * (test race with shutdown) */
  1479. simulate_single_hop_extend(client_side, relay_side, 1);
  1480. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1481. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1482. TO_ORIGIN_CIRCUIT(client_side)->p_streams = 0;
  1483. circpad_machine_event_circ_has_no_streams(TO_ORIGIN_CIRCUIT(client_side));
  1484. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1485. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1486. /* Now make the circuit opened and send built event */
  1487. TO_ORIGIN_CIRCUIT(client_side)->has_opened = 1;
  1488. circpad_machine_event_circ_built(TO_ORIGIN_CIRCUIT(client_side));
  1489. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1490. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1491. TO_ORIGIN_CIRCUIT(client_side)->remaining_relay_early_cells = 0;
  1492. circpad_machine_event_circ_has_no_relay_early(
  1493. TO_ORIGIN_CIRCUIT(client_side));
  1494. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1495. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1496. get_options_mutable()->HSLayer2Nodes = (void*)1;
  1497. TO_ORIGIN_CIRCUIT(client_side)->p_streams = (void*)1;
  1498. circpad_machine_event_circ_has_streams(TO_ORIGIN_CIRCUIT(client_side));
  1499. /* Verify different machine is added */
  1500. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1501. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1502. /* Hold off on negotiated */
  1503. deliver_negotiated = 0;
  1504. /* Deliver a padding cell to the client, to trigger burst state */
  1505. circpad_cell_event_padding_sent(client_side);
  1506. /* This should have trigger length shutdown condition on client
  1507. * but not the response for the padding machine */
  1508. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1509. tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
  1510. /* Verify machine is gone from the relay (but negotiated not back yet */
  1511. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1512. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1513. /* Add another hop and verify it's back */
  1514. simulate_single_hop_extend(client_side, relay_side, 1);
  1515. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1516. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1517. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  1518. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1519. done:
  1520. /* XXX: Free everything */
  1521. return;
  1522. }
  1523. /** Disabled unstable test until #29298 is implemented (see #29122) */
  1524. #if 0
  1525. void
  1526. test_circuitpadding_circuitsetup_machine(void *arg)
  1527. {
  1528. int64_t actual_mocked_monotime_start;
  1529. /**
  1530. * Test case plan:
  1531. *
  1532. * 1. Simulate a normal circuit setup pattern
  1533. * a. Application traffic
  1534. *
  1535. * FIXME: This should focus more on exercising the machine
  1536. * features rather than actual traffic patterns. For example,
  1537. * test cancellation and bins empty/refill
  1538. */
  1539. (void)arg;
  1540. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1541. dummy_channel.cmux = circuitmux_alloc();
  1542. client_side = TO_CIRCUIT(origin_circuit_new());
  1543. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1544. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1545. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1546. nodes_init();
  1547. monotime_init();
  1548. monotime_enable_test_mocking();
  1549. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1550. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1551. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1552. curr_mocked_time = actual_mocked_monotime_start;
  1553. timers_initialize();
  1554. circpad_machines_init();
  1555. MOCK(circuit_package_relay_cell,
  1556. circuit_package_relay_cell_mock);
  1557. MOCK(node_get_by_id,
  1558. node_get_by_id_mock);
  1559. /* Test case #1: Build a 3 hop circuit, then wait and let pad */
  1560. simulate_single_hop_extend(client_side, relay_side, 1);
  1561. simulate_single_hop_extend(client_side, relay_side, 1);
  1562. simulate_single_hop_extend(client_side, relay_side, 1);
  1563. tt_int_op(n_client_cells, OP_EQ, 1);
  1564. tt_int_op(n_relay_cells, OP_EQ, 1);
  1565. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1566. CIRCPAD_STATE_BURST);
  1567. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1568. CIRCPAD_STATE_BURST);
  1569. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1570. OP_NE, 0);
  1571. tt_int_op(relay_side->padding_info[0]->is_padding_timer_scheduled,
  1572. OP_EQ, 0);
  1573. timers_advance_and_run(2000);
  1574. tt_int_op(n_client_cells, OP_EQ, 2);
  1575. tt_int_op(n_relay_cells, OP_EQ, 1);
  1576. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1577. CIRCPAD_STATE_GAP);
  1578. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1579. OP_EQ, 0);
  1580. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1581. OP_NE, 0);
  1582. timers_advance_and_run(5000);
  1583. tt_int_op(n_client_cells, OP_EQ, 2);
  1584. tt_int_op(n_relay_cells, OP_EQ, 2);
  1585. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1586. OP_NE, 0);
  1587. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1588. OP_EQ, 0);
  1589. timers_advance_and_run(2000);
  1590. tt_int_op(n_client_cells, OP_EQ, 3);
  1591. tt_int_op(n_relay_cells, OP_EQ, 2);
  1592. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1593. OP_EQ, 0);
  1594. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1595. OP_NE, 0);
  1596. timers_advance_and_run(5000);
  1597. tt_int_op(n_client_cells, OP_EQ, 3);
  1598. tt_int_op(n_relay_cells, OP_EQ, 3);
  1599. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1600. OP_NE, 0);
  1601. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1602. OP_EQ, 0);
  1603. timers_advance_and_run(2000);
  1604. tt_int_op(n_client_cells, OP_EQ, 4);
  1605. tt_int_op(n_relay_cells, OP_EQ, 3);
  1606. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1607. OP_EQ, 0);
  1608. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1609. OP_NE, 0);
  1610. timers_advance_and_run(5000);
  1611. tt_int_op(n_client_cells, OP_EQ, 4);
  1612. tt_int_op(n_relay_cells, OP_EQ, 4);
  1613. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1614. OP_NE, 0);
  1615. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1616. OP_EQ, 0);
  1617. timers_advance_and_run(2000);
  1618. tt_int_op(n_client_cells, OP_EQ, 5);
  1619. tt_int_op(n_relay_cells, OP_EQ, 4);
  1620. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1621. OP_EQ, 0);
  1622. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1623. OP_NE, 0);
  1624. timers_advance_and_run(5000);
  1625. tt_int_op(n_client_cells, OP_EQ, 5);
  1626. tt_int_op(n_relay_cells, OP_EQ, 5);
  1627. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1628. OP_NE, 0);
  1629. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1630. OP_EQ, 0);
  1631. timers_advance_and_run(2000);
  1632. tt_int_op(n_client_cells, OP_EQ, 6);
  1633. tt_int_op(n_relay_cells, OP_EQ, 5);
  1634. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1635. OP_EQ, 0);
  1636. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1637. OP_NE, 0);
  1638. timers_advance_and_run(5000);
  1639. tt_int_op(n_client_cells, OP_EQ, 6);
  1640. tt_int_op(n_relay_cells, OP_EQ, 6);
  1641. tt_int_op(client_side->padding_info[0]->current_state,
  1642. OP_EQ, CIRCPAD_STATE_END);
  1643. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1644. OP_EQ, 0);
  1645. tt_int_op(relay_side->padding_info[0]->current_state,
  1646. OP_EQ, CIRCPAD_STATE_GAP);
  1647. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1648. OP_EQ, 0);
  1649. /* Verify we can't schedule padding in END state */
  1650. circpad_decision_t ret =
  1651. circpad_machine_schedule_padding(client_side->padding_info[0]);
  1652. tt_int_op(ret, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  1653. /* Simulate application traffic */
  1654. circpad_cell_event_nonpadding_sent(client_side);
  1655. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
  1656. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
  1657. circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
  1658. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1659. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1660. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1661. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1662. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1663. tt_int_op(n_client_cells, OP_EQ, 6);
  1664. tt_int_op(n_relay_cells, OP_EQ, 7);
  1665. // Test timer cancellation
  1666. simulate_single_hop_extend(client_side, relay_side, 1);
  1667. simulate_single_hop_extend(client_side, relay_side, 1);
  1668. timers_advance_and_run(5000);
  1669. circpad_cell_event_padding_received(client_side);
  1670. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1671. CIRCPAD_STATE_BURST);
  1672. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1673. CIRCPAD_STATE_GAP);
  1674. tt_int_op(n_client_cells, OP_EQ, 8);
  1675. tt_int_op(n_relay_cells, OP_EQ, 8);
  1676. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1677. OP_NE, 0);
  1678. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1679. OP_NE, 0);
  1680. /* Test timer cancel due to state rules */
  1681. circpad_cell_event_nonpadding_sent(client_side);
  1682. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1683. OP_EQ, 0);
  1684. circpad_cell_event_padding_received(client_side);
  1685. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1686. OP_NE, 0);
  1687. /* Simulate application traffic to cancel timer */
  1688. circpad_cell_event_nonpadding_sent(client_side);
  1689. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
  1690. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
  1691. circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
  1692. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1693. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1694. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1695. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1696. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1697. /* No cells sent, except negotiate end from relay */
  1698. tt_int_op(n_client_cells, OP_EQ, 8);
  1699. tt_int_op(n_relay_cells, OP_EQ, 9);
  1700. /* Test mark for close and free */
  1701. simulate_single_hop_extend(client_side, relay_side, 1);
  1702. simulate_single_hop_extend(client_side, relay_side, 1);
  1703. timers_advance_and_run(5000);
  1704. circpad_cell_event_padding_received(client_side);
  1705. tt_int_op(n_client_cells, OP_EQ, 10);
  1706. tt_int_op(n_relay_cells, OP_EQ, 10);
  1707. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1708. CIRCPAD_STATE_BURST);
  1709. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1710. CIRCPAD_STATE_GAP);
  1711. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1712. OP_NE, 0);
  1713. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1714. OP_NE, 0);
  1715. circuit_mark_for_close(client_side, END_CIRC_REASON_FLAG_REMOTE);
  1716. free_fake_orcirc(relay_side);
  1717. timers_advance_and_run(5000);
  1718. /* No cells sent */
  1719. tt_int_op(n_client_cells, OP_EQ, 10);
  1720. tt_int_op(n_relay_cells, OP_EQ, 10);
  1721. done:
  1722. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1723. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1724. circuitmux_free(dummy_channel.cmux);
  1725. timers_shutdown();
  1726. monotime_disable_test_mocking();
  1727. UNMOCK(circuit_package_relay_cell);
  1728. UNMOCK(circuitmux_attach_circuit);
  1729. return;
  1730. }
  1731. #endif
  1732. /** Helper function: Initializes a padding machine where every state uses the
  1733. * uniform probability distribution. */
  1734. static void
  1735. helper_circpad_circ_distribution_machine_setup(int min, int max)
  1736. {
  1737. circpad_machine_states_init(&circ_client_machine, 7);
  1738. circpad_state_t *zero_st = &circ_client_machine.states[0];
  1739. zero_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 1;
  1740. zero_st->iat_dist.type = CIRCPAD_DIST_UNIFORM;
  1741. /* param2 is upper bound, param1 is lower */
  1742. zero_st->iat_dist.param1 = min;
  1743. zero_st->iat_dist.param2 = max;
  1744. zero_st->dist_added_shift_usec = min;
  1745. zero_st->dist_max_sample_usec = max;
  1746. circpad_state_t *first_st = &circ_client_machine.states[1];
  1747. first_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 2;
  1748. first_st->iat_dist.type = CIRCPAD_DIST_LOGISTIC;
  1749. /* param1 is Mu, param2 is sigma. */
  1750. first_st->iat_dist.param1 = 9;
  1751. first_st->iat_dist.param2 = 3;
  1752. first_st->dist_added_shift_usec = min;
  1753. first_st->dist_max_sample_usec = max;
  1754. circpad_state_t *second_st = &circ_client_machine.states[2];
  1755. second_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 3;
  1756. second_st->iat_dist.type = CIRCPAD_DIST_LOG_LOGISTIC;
  1757. /* param1 is Alpha, param2 is 1.0/Beta */
  1758. second_st->iat_dist.param1 = 1;
  1759. second_st->iat_dist.param2 = 0.5;
  1760. second_st->dist_added_shift_usec = min;
  1761. second_st->dist_max_sample_usec = max;
  1762. circpad_state_t *third_st = &circ_client_machine.states[3];
  1763. third_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 4;
  1764. third_st->iat_dist.type = CIRCPAD_DIST_GEOMETRIC;
  1765. /* param1 is 'p' (success probability) */
  1766. third_st->iat_dist.param1 = 0.2;
  1767. third_st->dist_added_shift_usec = min;
  1768. third_st->dist_max_sample_usec = max;
  1769. circpad_state_t *fourth_st = &circ_client_machine.states[4];
  1770. fourth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 5;
  1771. fourth_st->iat_dist.type = CIRCPAD_DIST_WEIBULL;
  1772. /* param1 is k, param2 is Lambda */
  1773. fourth_st->iat_dist.param1 = 1.5;
  1774. fourth_st->iat_dist.param2 = 1;
  1775. fourth_st->dist_added_shift_usec = min;
  1776. fourth_st->dist_max_sample_usec = max;
  1777. circpad_state_t *fifth_st = &circ_client_machine.states[5];
  1778. fifth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 6;
  1779. fifth_st->iat_dist.type = CIRCPAD_DIST_PARETO;
  1780. /* param1 is sigma, param2 is xi */
  1781. fifth_st->iat_dist.param1 = 1;
  1782. fifth_st->iat_dist.param2 = 5;
  1783. fifth_st->dist_added_shift_usec = min;
  1784. fifth_st->dist_max_sample_usec = max;
  1785. }
  1786. /** Simple test that the padding delays sampled from a uniform distribution
  1787. * actually faill within the uniform distribution range. */
  1788. static void
  1789. test_circuitpadding_sample_distribution(void *arg)
  1790. {
  1791. circpad_machine_runtime_t *mi;
  1792. int n_samples;
  1793. int n_states;
  1794. (void) arg;
  1795. /* mock this function so that we dont actually schedule any padding */
  1796. MOCK(circpad_machine_schedule_padding,
  1797. circpad_machine_schedule_padding_mock);
  1798. /* Initialize a machine with multiple probability distributions */
  1799. circpad_machines_init();
  1800. helper_circpad_circ_distribution_machine_setup(0, 10);
  1801. /* Initialize machine and circuits */
  1802. client_side = TO_CIRCUIT(origin_circuit_new());
  1803. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1804. client_side->padding_machine[0] = &circ_client_machine;
  1805. client_side->padding_info[0] =
  1806. circpad_circuit_machineinfo_new(client_side, 0);
  1807. mi = client_side->padding_info[0];
  1808. /* For every state, sample a bunch of values from the distribution and ensure
  1809. * they fall within range. */
  1810. for (n_states = 0 ; n_states < 6; n_states++) {
  1811. /* Make sure we in the right state */
  1812. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ, n_states);
  1813. for (n_samples = 0; n_samples < 100; n_samples++) {
  1814. circpad_delay_t delay = circpad_machine_sample_delay(mi);
  1815. tt_int_op(delay, OP_GE, 0);
  1816. tt_int_op(delay, OP_LE, 10);
  1817. }
  1818. /* send a non-padding cell to move to the next machine state */
  1819. circpad_cell_event_nonpadding_received((circuit_t*)client_side);
  1820. }
  1821. done:
  1822. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1823. UNMOCK(circpad_machine_schedule_padding);
  1824. }
  1825. static circpad_decision_t
  1826. circpad_machine_spec_transition_mock(circpad_machine_runtime_t *mi,
  1827. circpad_event_t event)
  1828. {
  1829. (void) mi;
  1830. (void) event;
  1831. return CIRCPAD_STATE_UNCHANGED;
  1832. }
  1833. /* Test per-machine padding rate limits */
  1834. static void
  1835. test_circuitpadding_machine_rate_limiting(void *arg)
  1836. {
  1837. (void) arg;
  1838. bool retval;
  1839. circpad_machine_runtime_t *mi;
  1840. int i;
  1841. /* Ignore machine transitions for the purposes of this function, we only
  1842. * really care about padding counts */
  1843. MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
  1844. MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
  1845. /* Setup machine and circuits */
  1846. client_side = TO_CIRCUIT(origin_circuit_new());
  1847. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1848. helper_create_basic_machine();
  1849. client_side->padding_machine[0] = &circ_client_machine;
  1850. client_side->padding_info[0] =
  1851. circpad_circuit_machineinfo_new(client_side, 0);
  1852. mi = client_side->padding_info[0];
  1853. /* Set up the machine info so that we can get through the basic functions */
  1854. mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
  1855. /* First we are going to test the per-machine rate limits */
  1856. circ_client_machine.max_padding_percent = 50;
  1857. circ_client_machine.allowed_padding_count = 100;
  1858. /* Check padding limit, should be fine since we haven't sent anything yet. */
  1859. retval = circpad_machine_reached_padding_limit(mi);
  1860. tt_int_op(retval, OP_EQ, 0);
  1861. /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
  1862. * the rate limit will not trigger */
  1863. for (i=0;i<99;i++) {
  1864. circpad_send_padding_cell_for_callback(mi);
  1865. }
  1866. retval = circpad_machine_reached_padding_limit(mi);
  1867. tt_int_op(retval, OP_EQ, 0);
  1868. /* Now send another padding cell to pass circpad_global_allowed_cells=100,
  1869. and see that the limit will trigger */
  1870. circpad_send_padding_cell_for_callback(mi);
  1871. retval = circpad_machine_reached_padding_limit(mi);
  1872. tt_int_op(retval, OP_EQ, 1);
  1873. retval = circpad_machine_schedule_padding(mi);
  1874. tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  1875. /* Cover wrap */
  1876. for (;i<UINT16_MAX;i++) {
  1877. circpad_send_padding_cell_for_callback(mi);
  1878. }
  1879. tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/2+1);
  1880. tt_ptr_op(client_side->padding_info[0], OP_EQ, mi);
  1881. for (i=0;i<UINT16_MAX;i++) {
  1882. circpad_cell_event_nonpadding_sent(client_side);
  1883. }
  1884. tt_int_op(mi->nonpadding_sent, OP_EQ, UINT16_MAX/2);
  1885. tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/4+1);
  1886. done:
  1887. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1888. }
  1889. /* Test global padding rate limits */
  1890. static void
  1891. test_circuitpadding_global_rate_limiting(void *arg)
  1892. {
  1893. (void) arg;
  1894. bool retval;
  1895. circpad_machine_runtime_t *mi;
  1896. int i;
  1897. int64_t actual_mocked_monotime_start;
  1898. /* Ignore machine transitions for the purposes of this function, we only
  1899. * really care about padding counts */
  1900. MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
  1901. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1902. MOCK(circuit_package_relay_cell,
  1903. circuit_package_relay_cell_mock);
  1904. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  1905. monotime_init();
  1906. monotime_enable_test_mocking();
  1907. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1908. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1909. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1910. curr_mocked_time = actual_mocked_monotime_start;
  1911. timers_initialize();
  1912. client_side = (circuit_t *)origin_circuit_new();
  1913. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1914. dummy_channel.cmux = circuitmux_alloc();
  1915. /* Setup machine and circuits */
  1916. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel, &dummy_channel);
  1917. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1918. helper_create_basic_machine();
  1919. relay_side->padding_machine[0] = &circ_client_machine;
  1920. relay_side->padding_info[0] =
  1921. circpad_circuit_machineinfo_new(relay_side, 0);
  1922. mi = relay_side->padding_info[0];
  1923. /* Set up the machine info so that we can get through the basic functions */
  1924. mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
  1925. simulate_single_hop_extend(client_side, relay_side, 1);
  1926. simulate_single_hop_extend(client_side, relay_side, 1);
  1927. /* Now test the global limits by setting up the consensus */
  1928. networkstatus_t vote1;
  1929. vote1.net_params = smartlist_new();
  1930. smartlist_split_string(vote1.net_params,
  1931. "circpad_global_allowed_cells=100 circpad_global_max_padding_pct=50",
  1932. NULL, 0, 0);
  1933. /* Register global limits with the padding subsystem */
  1934. circpad_new_consensus_params(&vote1);
  1935. /* Check padding limit, should be fine since we haven't sent anything yet. */
  1936. retval = circpad_machine_reached_padding_limit(mi);
  1937. tt_int_op(retval, OP_EQ, 0);
  1938. /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
  1939. * the rate limit will not trigger */
  1940. for (i=0;i<99;i++) {
  1941. circpad_send_padding_cell_for_callback(mi);
  1942. }
  1943. retval = circpad_machine_reached_padding_limit(mi);
  1944. tt_int_op(retval, OP_EQ, 0);
  1945. /* Now send another padding cell to pass circpad_global_allowed_cells=100,
  1946. and see that the limit will trigger */
  1947. circpad_send_padding_cell_for_callback(mi);
  1948. retval = circpad_machine_reached_padding_limit(mi);
  1949. tt_int_op(retval, OP_EQ, 1);
  1950. retval = circpad_machine_schedule_padding(mi);
  1951. tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  1952. /* Now send 92 non-padding cells to get near the
  1953. * circpad_global_max_padding_pct=50 limit; in particular with 96 non-padding
  1954. * cells, the padding traffic is still 51% of total traffic so limit should
  1955. * trigger */
  1956. for (i=0;i<92;i++) {
  1957. circpad_cell_event_nonpadding_sent(relay_side);
  1958. }
  1959. retval = circpad_machine_reached_padding_limit(mi);
  1960. tt_int_op(retval, OP_EQ, 1);
  1961. /* Send another non-padding cell to bring the padding traffic to 50% of total
  1962. * traffic and get past the limit */
  1963. circpad_cell_event_nonpadding_sent(relay_side);
  1964. retval = circpad_machine_reached_padding_limit(mi);
  1965. tt_int_op(retval, OP_EQ, 0);
  1966. done:
  1967. free_fake_orcirc(relay_side);
  1968. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1969. circuitmux_free(dummy_channel.cmux);
  1970. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  1971. smartlist_free(vote1.net_params);
  1972. }
  1973. #define TEST_CIRCUITPADDING(name, flags) \
  1974. { #name, test_##name, (flags), NULL, NULL }
  1975. struct testcase_t circuitpadding_tests[] = {
  1976. TEST_CIRCUITPADDING(circuitpadding_tokens, TT_FORK),
  1977. TEST_CIRCUITPADDING(circuitpadding_negotiation, TT_FORK),
  1978. TEST_CIRCUITPADDING(circuitpadding_wronghop, TT_FORK),
  1979. /** Disabled unstable test until #29298 is implemented (see #29122) */
  1980. // TEST_CIRCUITPADDING(circuitpadding_circuitsetup_machine, TT_FORK),
  1981. TEST_CIRCUITPADDING(circuitpadding_conditions, TT_FORK),
  1982. TEST_CIRCUITPADDING(circuitpadding_rtt, TT_FORK),
  1983. TEST_CIRCUITPADDING(circuitpadding_sample_distribution, TT_FORK),
  1984. TEST_CIRCUITPADDING(circuitpadding_machine_rate_limiting, TT_FORK),
  1985. TEST_CIRCUITPADDING(circuitpadding_global_rate_limiting, TT_FORK),
  1986. TEST_CIRCUITPADDING(circuitpadding_token_removal_lower, TT_FORK),
  1987. TEST_CIRCUITPADDING(circuitpadding_token_removal_higher, TT_FORK),
  1988. TEST_CIRCUITPADDING(circuitpadding_closest_token_removal, TT_FORK),
  1989. TEST_CIRCUITPADDING(circuitpadding_closest_token_removal_usec, TT_FORK),
  1990. TEST_CIRCUITPADDING(circuitpadding_token_removal_exact, TT_FORK),
  1991. END_OF_TESTCASES
  1992. };