test_circuitpadding.c 81 KB

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