test_circuitpadding.c 81 KB

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