test_circuitpadding.c 82 KB

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