test_circuitpadding.c 81 KB

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