test_circuitpadding.c 83 KB

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