test_circuitpadding.c 83 KB

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