test_circuitpadding.c 74 KB

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