test_circuitpadding.c 84 KB

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