test_circuitpadding.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179
  1. #define TOR_CHANNEL_INTERNAL_
  2. #define TOR_TIMERS_PRIVATE
  3. #define CIRCUITPADDING_PRIVATE
  4. #define CIRCUITPADDING_MACHINES_PRIVATE
  5. #define NETWORKSTATUS_PRIVATE
  6. #define CRYPT_PATH_PRIVATE
  7. #include "core/or/or.h"
  8. #include "test.h"
  9. #include "lib/testsupport/testsupport.h"
  10. #include "core/or/connection_or.h"
  11. #include "core/or/channel.h"
  12. #include "core/or/channeltls.h"
  13. #include "core/or/crypt_path.h"
  14. #include <event.h>
  15. #include "lib/evloop/compat_libevent.h"
  16. #include "lib/time/compat_time.h"
  17. #include "lib/defs/time.h"
  18. #include "core/or/relay.h"
  19. #include "core/or/circuitlist.h"
  20. #include "core/or/circuitbuild.h"
  21. #include "core/or/circuitpadding.h"
  22. #include "core/or/circuitpadding_machines.h"
  23. #include "core/mainloop/netstatus.h"
  24. #include "core/crypto/relay_crypto.h"
  25. #include "core/or/protover.h"
  26. #include "feature/nodelist/nodelist.h"
  27. #include "lib/evloop/compat_libevent.h"
  28. #include "app/config/config.h"
  29. #include "feature/nodelist/routerstatus_st.h"
  30. #include "feature/nodelist/networkstatus_st.h"
  31. #include "feature/nodelist/node_st.h"
  32. #include "core/or/cell_st.h"
  33. #include "core/or/crypt_path_st.h"
  34. #include "core/or/or_circuit_st.h"
  35. #include "core/or/origin_circuit_st.h"
  36. #include "test/rng_test_helpers.h"
  37. /* Start our monotime mocking at 1 second past whatever monotime_init()
  38. * thought the actual wall clock time was, for platforms with bad resolution
  39. * and weird timevalues during monotime_init() before mocking. */
  40. #define MONOTIME_MOCK_START (monotime_absolute_nsec()+\
  41. TOR_NSEC_PER_USEC*TOR_USEC_PER_SEC)
  42. extern smartlist_t *connection_array;
  43. void circuit_expire_old_circuits_clientside(void);
  44. circid_t get_unique_circ_id_by_chan(channel_t *chan);
  45. void helper_create_basic_machine(void);
  46. static void helper_create_conditional_machines(void);
  47. static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
  48. channel_t *new_fake_channel(void);
  49. void test_circuitpadding_negotiation(void *arg);
  50. void test_circuitpadding_wronghop(void *arg);
  51. void test_circuitpadding_conditions(void *arg);
  52. void test_circuitpadding_serialize(void *arg);
  53. void test_circuitpadding_rtt(void *arg);
  54. void test_circuitpadding_tokens(void *arg);
  55. void test_circuitpadding_state_length(void *arg);
  56. static void
  57. simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
  58. int padding);
  59. void free_fake_orcirc(circuit_t *circ);
  60. void free_fake_origin_circuit(origin_circuit_t *circ);
  61. static int deliver_negotiated = 1;
  62. static int64_t curr_mocked_time;
  63. static node_t padding_node;
  64. static node_t non_padding_node;
  65. static channel_t dummy_channel;
  66. static circpad_machine_spec_t circ_client_machine;
  67. static void
  68. timers_advance_and_run(int64_t msec_update)
  69. {
  70. curr_mocked_time += msec_update*TOR_NSEC_PER_MSEC;
  71. monotime_coarse_set_mock_time_nsec(curr_mocked_time);
  72. monotime_set_mock_time_nsec(curr_mocked_time);
  73. timers_run_pending();
  74. }
  75. static void
  76. nodes_init(void)
  77. {
  78. padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
  79. padding_node.rs->pv.supports_padding = 1;
  80. non_padding_node.rs = tor_malloc_zero(sizeof(routerstatus_t));
  81. non_padding_node.rs->pv.supports_padding = 0;
  82. }
  83. static void
  84. nodes_free(void)
  85. {
  86. tor_free(padding_node.rs);
  87. tor_free(non_padding_node.rs);
  88. }
  89. static const node_t *
  90. node_get_by_id_mock(const char *identity_digest)
  91. {
  92. if (identity_digest[0] == 1) {
  93. return &padding_node;
  94. } else if (identity_digest[0] == 0) {
  95. return &non_padding_node;
  96. }
  97. return NULL;
  98. }
  99. static const node_t *
  100. circuit_get_nth_node_mock(origin_circuit_t *circ, int hop)
  101. {
  102. (void) circ;
  103. (void) hop;
  104. return &padding_node;
  105. }
  106. static or_circuit_t *
  107. new_fake_orcirc(channel_t *nchan, channel_t *pchan)
  108. {
  109. or_circuit_t *orcirc = NULL;
  110. circuit_t *circ = NULL;
  111. crypt_path_t tmp_cpath;
  112. char whatevs_key[CPATH_KEY_MATERIAL_LEN];
  113. orcirc = tor_malloc_zero(sizeof(*orcirc));
  114. circ = &(orcirc->base_);
  115. circ->magic = OR_CIRCUIT_MAGIC;
  116. //circ->n_chan = nchan;
  117. circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
  118. cell_queue_init(&(circ->n_chan_cells));
  119. circ->n_hop = NULL;
  120. circ->streams_blocked_on_n_chan = 0;
  121. circ->streams_blocked_on_p_chan = 0;
  122. circ->n_delete_pending = 0;
  123. circ->p_delete_pending = 0;
  124. circ->received_destroy = 0;
  125. circ->state = CIRCUIT_STATE_OPEN;
  126. circ->purpose = CIRCUIT_PURPOSE_OR;
  127. circ->package_window = CIRCWINDOW_START_MAX;
  128. circ->deliver_window = CIRCWINDOW_START_MAX;
  129. circ->n_chan_create_cell = NULL;
  130. //orcirc->p_chan = pchan;
  131. orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
  132. cell_queue_init(&(orcirc->p_chan_cells));
  133. circuit_set_p_circid_chan(orcirc, orcirc->p_circ_id, pchan);
  134. circuit_set_n_circid_chan(circ, circ->n_circ_id, nchan);
  135. memset(&tmp_cpath, 0, sizeof(tmp_cpath));
  136. if (cpath_init_circuit_crypto(&tmp_cpath, whatevs_key,
  137. sizeof(whatevs_key), 0, 0)<0) {
  138. log_warn(LD_BUG,"Circuit initialization failed");
  139. return NULL;
  140. }
  141. orcirc->crypto = tmp_cpath.pvt_crypto;
  142. return orcirc;
  143. }
  144. void
  145. free_fake_orcirc(circuit_t *circ)
  146. {
  147. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  148. relay_crypto_clear(&orcirc->crypto);
  149. circpad_circuit_free_all_machineinfos(circ);
  150. tor_free(circ);
  151. }
  152. void
  153. free_fake_origin_circuit(origin_circuit_t *circ)
  154. {
  155. circpad_circuit_free_all_machineinfos(TO_CIRCUIT(circ));
  156. circuit_clear_cpath(circ);
  157. tor_free(circ);
  158. }
  159. void dummy_nop_timer(void);
  160. //static int dont_stop_libevent = 0;
  161. static circuit_t *client_side;
  162. static circuit_t *relay_side;
  163. static int n_client_cells = 0;
  164. static int n_relay_cells = 0;
  165. static int
  166. circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
  167. cell_direction_t cell_direction,
  168. crypt_path_t *layer_hint, streamid_t on_stream,
  169. const char *filename, int lineno);
  170. static void
  171. circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
  172. cell_direction_t direction);
  173. static void
  174. circuitmux_attach_circuit_mock(circuitmux_t *cmux, circuit_t *circ,
  175. cell_direction_t direction)
  176. {
  177. (void)cmux;
  178. (void)circ;
  179. (void)direction;
  180. return;
  181. }
  182. static int
  183. circuit_package_relay_cell_mock(cell_t *cell, circuit_t *circ,
  184. cell_direction_t cell_direction,
  185. crypt_path_t *layer_hint, streamid_t on_stream,
  186. const char *filename, int lineno)
  187. {
  188. (void)cell; (void)on_stream; (void)filename; (void)lineno;
  189. if (circ == client_side) {
  190. if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
  191. // Deliver to relay
  192. circpad_handle_padding_negotiate(relay_side, cell);
  193. } else {
  194. int is_target_hop = circpad_padding_is_from_expected_hop(circ,
  195. layer_hint);
  196. tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_OUT);
  197. tt_int_op(is_target_hop, OP_EQ, 1);
  198. // No need to pretend a padding cell was sent: This event is
  199. // now emitted internally when the circuitpadding code sends them.
  200. //circpad_cell_event_padding_sent(client_side);
  201. // Receive padding cell at middle
  202. circpad_deliver_recognized_relay_cell_events(relay_side,
  203. cell->payload[0], NULL);
  204. }
  205. n_client_cells++;
  206. } else if (circ == relay_side) {
  207. tt_int_op(cell_direction, OP_EQ, CELL_DIRECTION_IN);
  208. if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATED) {
  209. // XXX: blah need right layer_hint..
  210. if (deliver_negotiated)
  211. circpad_handle_padding_negotiated(client_side, cell,
  212. TO_ORIGIN_CIRCUIT(client_side)
  213. ->cpath->next);
  214. } else if (cell->payload[0] == RELAY_COMMAND_PADDING_NEGOTIATE) {
  215. circpad_handle_padding_negotiate(client_side, cell);
  216. } else {
  217. // No need to pretend a padding cell was sent: This event is
  218. // now emitted internally when the circuitpadding code sends them.
  219. //circpad_cell_event_padding_sent(relay_side);
  220. // Receive padding cell at client
  221. circpad_deliver_recognized_relay_cell_events(client_side,
  222. cell->payload[0],
  223. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  224. }
  225. n_relay_cells++;
  226. }
  227. done:
  228. timers_advance_and_run(1);
  229. return 0;
  230. }
  231. // Test reading and writing padding to strings (or options_t + consensus)
  232. void
  233. test_circuitpadding_serialize(void *arg)
  234. {
  235. (void)arg;
  236. }
  237. static signed_error_t
  238. circpad_send_command_to_hop_mock(origin_circuit_t *circ, uint8_t hopnum,
  239. uint8_t relay_command, const uint8_t *payload,
  240. ssize_t payload_len)
  241. {
  242. (void) circ;
  243. (void) hopnum;
  244. (void) relay_command;
  245. (void) payload;
  246. (void) payload_len;
  247. return 0;
  248. }
  249. void
  250. test_circuitpadding_rtt(void *arg)
  251. {
  252. /* Test Plan:
  253. *
  254. * 1. Test RTT measurement server side
  255. * a. test usage of measured RTT
  256. * 2. Test termination of RTT measurement
  257. * a. test non-update of RTT
  258. * 3. Test client side circuit and non-application of RTT..
  259. */
  260. circpad_delay_t rtt_estimate;
  261. int64_t actual_mocked_monotime_start;
  262. (void)arg;
  263. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  264. MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
  265. testing_enable_reproducible_rng();
  266. dummy_channel.cmux = circuitmux_alloc();
  267. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  268. client_side = TO_CIRCUIT(origin_circuit_new());
  269. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  270. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  271. monotime_init();
  272. monotime_enable_test_mocking();
  273. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  274. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  275. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  276. curr_mocked_time = actual_mocked_monotime_start;
  277. timers_initialize();
  278. circpad_machines_init();
  279. helper_create_basic_machine();
  280. MOCK(circuit_package_relay_cell,
  281. circuit_package_relay_cell_mock);
  282. client_side->padding_machine[0] = &circ_client_machine;
  283. client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
  284. 0);
  285. relay_side->padding_machine[0] = &circ_client_machine;
  286. relay_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,0);
  287. /* Test 1: Test measuring RTT */
  288. circpad_cell_event_nonpadding_received(relay_side);
  289. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
  290. timers_advance_and_run(20);
  291. circpad_cell_event_nonpadding_sent(relay_side);
  292. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  293. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 19000);
  294. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 30000);
  295. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  296. OP_EQ,
  297. relay_side->padding_info[0]->rtt_estimate_usec+
  298. circpad_machine_current_state(
  299. relay_side->padding_info[0])->histogram_edges[0]);
  300. circpad_cell_event_nonpadding_received(relay_side);
  301. circpad_cell_event_nonpadding_received(relay_side);
  302. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_NE, 0);
  303. timers_advance_and_run(20);
  304. circpad_cell_event_nonpadding_sent(relay_side);
  305. circpad_cell_event_nonpadding_sent(relay_side);
  306. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  307. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_GE, 20000);
  308. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_LE, 21000);
  309. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  310. OP_EQ,
  311. relay_side->padding_info[0]->rtt_estimate_usec+
  312. circpad_machine_current_state(
  313. relay_side->padding_info[0])->histogram_edges[0]);
  314. /* Test 2: Termination of RTT measurement (from the previous test) */
  315. tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  316. rtt_estimate = relay_side->padding_info[0]->rtt_estimate_usec;
  317. circpad_cell_event_nonpadding_received(relay_side);
  318. timers_advance_and_run(4);
  319. circpad_cell_event_nonpadding_sent(relay_side);
  320. tt_int_op(relay_side->padding_info[0]->rtt_estimate_usec, OP_EQ,
  321. rtt_estimate);
  322. tt_u64_op(relay_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  323. tt_int_op(relay_side->padding_info[0]->stop_rtt_update, OP_EQ, 1);
  324. tt_int_op(circpad_histogram_bin_to_usec(relay_side->padding_info[0], 0),
  325. OP_EQ,
  326. relay_side->padding_info[0]->rtt_estimate_usec+
  327. circpad_machine_current_state(
  328. relay_side->padding_info[0])->histogram_edges[0]);
  329. /* Test 3: Make sure client side machine properly ignores RTT */
  330. circpad_cell_event_nonpadding_received(client_side);
  331. tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  332. timers_advance_and_run(20);
  333. circpad_cell_event_nonpadding_sent(client_side);
  334. tt_u64_op(client_side->padding_info[0]->last_received_time_usec, OP_EQ, 0);
  335. tt_int_op(client_side->padding_info[0]->rtt_estimate_usec, OP_EQ, 0);
  336. tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
  337. OP_NE, client_side->padding_info[0]->rtt_estimate_usec);
  338. tt_int_op(circpad_histogram_bin_to_usec(client_side->padding_info[0], 0),
  339. OP_EQ,
  340. circpad_machine_current_state(
  341. client_side->padding_info[0])->histogram_edges[0]);
  342. done:
  343. free_fake_orcirc(relay_side);
  344. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  345. circuitmux_free(dummy_channel.cmux);
  346. timers_shutdown();
  347. monotime_disable_test_mocking();
  348. UNMOCK(circuit_package_relay_cell);
  349. UNMOCK(circuitmux_attach_circuit);
  350. tor_free(circ_client_machine.states);
  351. testing_disable_reproducible_rng();
  352. return;
  353. }
  354. void
  355. helper_create_basic_machine(void)
  356. {
  357. /* Start, burst */
  358. circpad_machine_states_init(&circ_client_machine, 2);
  359. circ_client_machine.name = "basic";
  360. circ_client_machine.states[CIRCPAD_STATE_START].
  361. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  362. circ_client_machine.states[CIRCPAD_STATE_START].use_rtt_estimate = 1;
  363. circ_client_machine.states[CIRCPAD_STATE_BURST].
  364. next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  365. circ_client_machine.states[CIRCPAD_STATE_BURST].
  366. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  367. circ_client_machine.states[CIRCPAD_STATE_BURST].
  368. next_state[CIRCPAD_EVENT_NONPADDING_SENT] = CIRCPAD_STATE_CANCEL;
  369. circ_client_machine.states[CIRCPAD_STATE_BURST].token_removal =
  370. CIRCPAD_TOKEN_REMOVAL_HIGHER;
  371. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_len = 5;
  372. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 500;
  373. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 2500;
  374. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 5000;
  375. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[3] = 10000;
  376. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[4] = 20000;
  377. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[0] = 1;
  378. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  379. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[2] = 2;
  380. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[3] = 2;
  381. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram[4] = 2;
  382. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_total_tokens = 7;
  383. circ_client_machine.states[CIRCPAD_STATE_BURST].use_rtt_estimate = 1;
  384. return;
  385. }
  386. #define BIG_HISTOGRAM_LEN 10
  387. /** Setup a machine with a big histogram */
  388. static void
  389. helper_create_machine_with_big_histogram(circpad_removal_t removal_strategy)
  390. {
  391. const int tokens_per_bin = 2;
  392. /* Start, burst */
  393. circpad_machine_states_init(&circ_client_machine, 2);
  394. circpad_state_t *burst_state =
  395. &circ_client_machine.states[CIRCPAD_STATE_BURST];
  396. circ_client_machine.states[CIRCPAD_STATE_START].
  397. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_BURST;
  398. burst_state->next_state[CIRCPAD_EVENT_PADDING_RECV] = CIRCPAD_STATE_BURST;
  399. burst_state->next_state[CIRCPAD_EVENT_NONPADDING_RECV] =CIRCPAD_STATE_BURST;
  400. burst_state->next_state[CIRCPAD_EVENT_NONPADDING_SENT] =CIRCPAD_STATE_CANCEL;
  401. burst_state->token_removal = CIRCPAD_TOKEN_REMOVAL_HIGHER;
  402. burst_state->histogram_len = BIG_HISTOGRAM_LEN;
  403. int n_tokens = 0;
  404. int i;
  405. for (i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  406. burst_state->histogram[i] = tokens_per_bin;
  407. n_tokens += tokens_per_bin;
  408. }
  409. burst_state->histogram_edges[0] = 0;
  410. burst_state->histogram_edges[1] = 1;
  411. burst_state->histogram_edges[2] = 7;
  412. burst_state->histogram_edges[3] = 15;
  413. burst_state->histogram_edges[4] = 31;
  414. burst_state->histogram_edges[5] = 62;
  415. burst_state->histogram_edges[6] = 125;
  416. burst_state->histogram_edges[7] = 250;
  417. burst_state->histogram_edges[8] = 500;
  418. burst_state->histogram_edges[9] = 1000;
  419. burst_state->histogram_total_tokens = n_tokens;
  420. burst_state->length_dist.type = CIRCPAD_DIST_UNIFORM;
  421. burst_state->length_dist.param1 = n_tokens;
  422. burst_state->length_dist.param2 = n_tokens;
  423. burst_state->max_length = n_tokens;
  424. burst_state->length_includes_nonpadding = 1;
  425. burst_state->use_rtt_estimate = 0;
  426. burst_state->token_removal = removal_strategy;
  427. }
  428. static circpad_decision_t
  429. circpad_machine_schedule_padding_mock(circpad_machine_runtime_t *mi)
  430. {
  431. (void)mi;
  432. return 0;
  433. }
  434. static uint64_t
  435. mock_monotime_absolute_usec(void)
  436. {
  437. return 100;
  438. }
  439. /** Test higher token removal strategy by bin */
  440. static void
  441. test_circuitpadding_token_removal_higher(void *arg)
  442. {
  443. circpad_machine_runtime_t *mi;
  444. (void)arg;
  445. /* Mock it up */
  446. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  447. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  448. testing_enable_reproducible_rng();
  449. /* Setup test environment (time etc.) */
  450. client_side = TO_CIRCUIT(origin_circuit_new());
  451. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  452. monotime_enable_test_mocking();
  453. /* Create test machine */
  454. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_HIGHER);
  455. client_side->padding_machine[0] = &circ_client_machine;
  456. client_side->padding_info[0] =
  457. circpad_circuit_machineinfo_new(client_side, 0);
  458. /* move the machine to the right state */
  459. circpad_cell_event_nonpadding_received(client_side);
  460. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  461. CIRCPAD_STATE_BURST);
  462. /* Get the machine and setup tokens */
  463. mi = client_side->padding_info[0];
  464. tt_assert(mi);
  465. /*************************************************************************/
  466. uint64_t current_time = monotime_absolute_usec();
  467. /* Test left boundaries of each histogram bin: */
  468. const circpad_delay_t bin_left_bounds[] =
  469. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  470. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  471. tt_uint_op(bin_left_bounds[i], OP_EQ,
  472. circpad_histogram_bin_to_usec(mi, i));
  473. }
  474. /* Test right boundaries of each histogram bin: */
  475. const circpad_delay_t bin_right_bounds[] =
  476. {0, 6, 14, 30, 61, 124, 249, 499, 999, CIRCPAD_DELAY_INFINITE-1};
  477. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  478. tt_uint_op(bin_right_bounds[i], OP_EQ,
  479. histogram_get_bin_upper_bound(mi, i));
  480. }
  481. /* Check that all bins have two tokens right now */
  482. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  483. tt_int_op(mi->histogram[i], OP_EQ, 2);
  484. }
  485. /* This is the right order to remove tokens from this histogram. That is, we
  486. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  487. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  488. * the same reason, then from the 5th, etc. */
  489. const int bin_removal_order[] = {4, 5, 6, 7, 8};
  490. unsigned i;
  491. /* Remove all tokens from all bins apart from the infinity bin */
  492. for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
  493. int bin_to_remove = bin_removal_order[i];
  494. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  495. i, bin_to_remove);
  496. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  497. mi->padding_scheduled_at_usec = current_time - 57;
  498. circpad_cell_event_nonpadding_sent(client_side);
  499. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  500. mi->padding_scheduled_at_usec = current_time - 57;
  501. circpad_cell_event_nonpadding_sent(client_side);
  502. /* Test that we cleaned out this bin. Don't do this in the case of the last
  503. bin since the tokens will get refilled */
  504. if (i != BIG_HISTOGRAM_LEN - 2) {
  505. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  506. }
  507. }
  508. /* Check that all lower bins are not touched */
  509. for (i=0; i < 4 ; i++) {
  510. tt_int_op(mi->histogram[i], OP_EQ, 2);
  511. }
  512. /* Test below the lowest bin, for coverage */
  513. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  514. CIRCPAD_STATE_BURST);
  515. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  516. mi->padding_scheduled_at_usec = current_time;
  517. circpad_cell_event_nonpadding_sent(client_side);
  518. tt_int_op(mi->histogram[0], OP_EQ, 1);
  519. done:
  520. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  521. monotime_disable_test_mocking();
  522. tor_free(circ_client_machine.states);
  523. testing_disable_reproducible_rng();
  524. }
  525. /** Test lower token removal strategy by bin */
  526. static void
  527. test_circuitpadding_token_removal_lower(void *arg)
  528. {
  529. circpad_machine_runtime_t *mi;
  530. (void)arg;
  531. /* Mock it up */
  532. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  533. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  534. testing_enable_reproducible_rng();
  535. /* Setup test environment (time etc.) */
  536. client_side = TO_CIRCUIT(origin_circuit_new());
  537. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  538. monotime_enable_test_mocking();
  539. /* Create test machine */
  540. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_LOWER);
  541. client_side->padding_machine[0] = &circ_client_machine;
  542. client_side->padding_info[0] =
  543. circpad_circuit_machineinfo_new(client_side, 0);
  544. /* move the machine to the right state */
  545. circpad_cell_event_nonpadding_received(client_side);
  546. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  547. CIRCPAD_STATE_BURST);
  548. /* Get the machine and setup tokens */
  549. mi = client_side->padding_info[0];
  550. tt_assert(mi);
  551. /*************************************************************************/
  552. uint64_t current_time = monotime_absolute_usec();
  553. /* Test left boundaries of each histogram bin: */
  554. const circpad_delay_t bin_left_bounds[] =
  555. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  556. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  557. tt_uint_op(bin_left_bounds[i], OP_EQ,
  558. circpad_histogram_bin_to_usec(mi, i));
  559. }
  560. /* Check that all bins have two tokens right now */
  561. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  562. tt_int_op(mi->histogram[i], OP_EQ, 2);
  563. }
  564. /* This is the right order to remove tokens from this histogram. That is, we
  565. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  566. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  567. * the same reason, then from the 5th, etc. */
  568. const int bin_removal_order[] = {4, 3, 2, 1, 0};
  569. unsigned i;
  570. /* Remove all tokens from all bins apart from the infinity bin */
  571. for (i = 0; i < sizeof(bin_removal_order)/sizeof(int) ; i++) {
  572. int bin_to_remove = bin_removal_order[i];
  573. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  574. i, bin_to_remove);
  575. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  576. mi->padding_scheduled_at_usec = current_time - 57;
  577. circpad_cell_event_nonpadding_sent(client_side);
  578. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  579. mi->padding_scheduled_at_usec = current_time - 57;
  580. circpad_cell_event_nonpadding_sent(client_side);
  581. /* Test that we cleaned out this bin. Don't do this in the case of the last
  582. bin since the tokens will get refilled */
  583. if (i != BIG_HISTOGRAM_LEN - 2) {
  584. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  585. }
  586. }
  587. /* Check that all higher bins are untouched */
  588. for (i = 5; i < BIG_HISTOGRAM_LEN ; i++) {
  589. tt_int_op(mi->histogram[i], OP_EQ, 2);
  590. }
  591. /* Test above the highest bin, for coverage */
  592. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  593. CIRCPAD_STATE_BURST);
  594. circ_client_machine.states[CIRCPAD_STATE_BURST].
  595. histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
  596. mi->padding_scheduled_at_usec = current_time - 29202;
  597. circpad_cell_event_nonpadding_sent(client_side);
  598. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  599. done:
  600. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  601. monotime_disable_test_mocking();
  602. tor_free(circ_client_machine.states);
  603. testing_disable_reproducible_rng();
  604. }
  605. /** Test closest token removal strategy by bin */
  606. static void
  607. test_circuitpadding_closest_token_removal(void *arg)
  608. {
  609. circpad_machine_runtime_t *mi;
  610. (void)arg;
  611. /* Mock it up */
  612. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  613. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  614. testing_enable_reproducible_rng();
  615. /* Setup test environment (time etc.) */
  616. client_side = TO_CIRCUIT(origin_circuit_new());
  617. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  618. monotime_enable_test_mocking();
  619. /* Create test machine */
  620. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST);
  621. client_side->padding_machine[0] = &circ_client_machine;
  622. client_side->padding_info[0] =
  623. circpad_circuit_machineinfo_new(client_side, 0);
  624. /* move the machine to the right state */
  625. circpad_cell_event_nonpadding_received(client_side);
  626. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  627. CIRCPAD_STATE_BURST);
  628. /* Get the machine and setup tokens */
  629. mi = client_side->padding_info[0];
  630. tt_assert(mi);
  631. /*************************************************************************/
  632. uint64_t current_time = monotime_absolute_usec();
  633. /* Test left boundaries of each histogram bin: */
  634. const circpad_delay_t bin_left_bounds[] =
  635. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  636. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  637. tt_uint_op(bin_left_bounds[i], OP_EQ,
  638. circpad_histogram_bin_to_usec(mi, i));
  639. }
  640. /* Check that all bins have two tokens right now */
  641. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  642. tt_int_op(mi->histogram[i], OP_EQ, 2);
  643. }
  644. /* This is the right order to remove tokens from this histogram. That is, we
  645. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  646. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  647. * the same reason, then from the 5th, etc. */
  648. const int bin_removal_order[] = {4, 3, 5, 2, 6, 1, 7, 0, 8, 9};
  649. /* Remove all tokens from all bins apart from the infinity bin */
  650. for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
  651. int bin_to_remove = bin_removal_order[i];
  652. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  653. i, bin_to_remove);
  654. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  655. mi->padding_scheduled_at_usec = current_time - 57;
  656. circpad_cell_event_nonpadding_sent(client_side);
  657. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  658. mi->padding_scheduled_at_usec = current_time - 57;
  659. circpad_cell_event_nonpadding_sent(client_side);
  660. /* Test that we cleaned out this bin. Don't do this in the case of the last
  661. bin since the tokens will get refilled */
  662. if (i != BIG_HISTOGRAM_LEN - 2) {
  663. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  664. }
  665. }
  666. /* Check that all bins have been refilled */
  667. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  668. tt_int_op(mi->histogram[i], OP_EQ, 2);
  669. }
  670. /* Test below the lowest bin, for coverage */
  671. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  672. CIRCPAD_STATE_BURST);
  673. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  674. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
  675. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
  676. mi->padding_scheduled_at_usec = current_time - 102;
  677. mi->histogram[0] = 0;
  678. circpad_cell_event_nonpadding_sent(client_side);
  679. tt_int_op(mi->histogram[1], OP_EQ, 1);
  680. /* Test above the highest bin, for coverage */
  681. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  682. CIRCPAD_STATE_BURST);
  683. mi->padding_scheduled_at_usec = current_time - 29202;
  684. circpad_cell_event_nonpadding_sent(client_side);
  685. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  686. done:
  687. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  688. monotime_disable_test_mocking();
  689. tor_free(circ_client_machine.states);
  690. testing_disable_reproducible_rng();
  691. }
  692. /** Test closest token removal strategy with usec */
  693. static void
  694. test_circuitpadding_closest_token_removal_usec(void *arg)
  695. {
  696. circpad_machine_runtime_t *mi;
  697. (void)arg;
  698. /* Mock it up */
  699. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  700. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  701. testing_enable_reproducible_rng();
  702. /* Setup test environment (time etc.) */
  703. client_side = TO_CIRCUIT(origin_circuit_new());
  704. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  705. monotime_enable_test_mocking();
  706. /* Create test machine */
  707. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_CLOSEST_USEC);
  708. client_side->padding_machine[0] = &circ_client_machine;
  709. client_side->padding_info[0] =
  710. circpad_circuit_machineinfo_new(client_side, 0);
  711. /* move the machine to the right state */
  712. circpad_cell_event_nonpadding_received(client_side);
  713. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  714. CIRCPAD_STATE_BURST);
  715. /* Get the machine and setup tokens */
  716. mi = client_side->padding_info[0];
  717. tt_assert(mi);
  718. /*************************************************************************/
  719. uint64_t current_time = monotime_absolute_usec();
  720. /* Test left boundaries of each histogram bin: */
  721. const circpad_delay_t bin_left_bounds[] =
  722. {0, 1, 7, 15, 31, 62, 125, 250, 500, 1000, CIRCPAD_DELAY_INFINITE};
  723. for (int i = 0; i <= BIG_HISTOGRAM_LEN ; i++) {
  724. tt_uint_op(bin_left_bounds[i], OP_EQ,
  725. circpad_histogram_bin_to_usec(mi, i));
  726. }
  727. /* XXX we want to test remove_token_exact and
  728. circpad_machine_remove_closest_token() with usec */
  729. /* Check that all bins have two tokens right now */
  730. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  731. tt_int_op(mi->histogram[i], OP_EQ, 2);
  732. }
  733. /* This is the right order to remove tokens from this histogram. That is, we
  734. * first remove tokens from the 4th bin since 57 usec is nearest to the 4th
  735. * bin midpoint (31 + (62-31)/2 == 46). Then we remove from the 3rd bin for
  736. * the same reason, then from the 5th, etc. */
  737. const int bin_removal_order[] = {4, 3, 5, 2, 1, 0, 6, 7, 8, 9};
  738. /* Remove all tokens from all bins apart from the infinity bin */
  739. for (int i = 0; i < BIG_HISTOGRAM_LEN-1 ; i++) {
  740. int bin_to_remove = bin_removal_order[i];
  741. log_debug(LD_GENERAL, "Testing that %d attempt removes %d bin",
  742. i, bin_to_remove);
  743. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 2);
  744. mi->padding_scheduled_at_usec = current_time - 57;
  745. circpad_cell_event_nonpadding_sent(client_side);
  746. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 1);
  747. mi->padding_scheduled_at_usec = current_time - 57;
  748. circpad_cell_event_nonpadding_sent(client_side);
  749. /* Test that we cleaned out this bin. Don't do this in the case of the last
  750. bin since the tokens will get refilled */
  751. if (i != BIG_HISTOGRAM_LEN - 2) {
  752. tt_int_op(mi->histogram[bin_to_remove], OP_EQ, 0);
  753. }
  754. }
  755. /* Check that all bins have been refilled */
  756. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  757. tt_int_op(mi->histogram[i], OP_EQ, 2);
  758. }
  759. /* Test below the lowest bin, for coverage */
  760. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  761. CIRCPAD_STATE_BURST);
  762. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[0] = 100;
  763. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[1] = 101;
  764. circ_client_machine.states[CIRCPAD_STATE_BURST].histogram_edges[2] = 120;
  765. mi->padding_scheduled_at_usec = current_time - 102;
  766. mi->histogram[0] = 0;
  767. circpad_cell_event_nonpadding_sent(client_side);
  768. tt_int_op(mi->histogram[1], OP_EQ, 1);
  769. /* Test above the highest bin, for coverage */
  770. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  771. CIRCPAD_STATE_BURST);
  772. circ_client_machine.states[CIRCPAD_STATE_BURST].
  773. histogram_edges[BIG_HISTOGRAM_LEN-2] = 100;
  774. mi->padding_scheduled_at_usec = current_time - 29202;
  775. circpad_cell_event_nonpadding_sent(client_side);
  776. tt_int_op(mi->histogram[BIG_HISTOGRAM_LEN-2], OP_EQ, 1);
  777. done:
  778. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  779. monotime_disable_test_mocking();
  780. tor_free(circ_client_machine.states);
  781. testing_disable_reproducible_rng();
  782. }
  783. /** Test closest token removal strategy with usec */
  784. static void
  785. test_circuitpadding_token_removal_exact(void *arg)
  786. {
  787. circpad_machine_runtime_t *mi;
  788. (void)arg;
  789. /* Mock it up */
  790. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  791. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  792. testing_enable_reproducible_rng();
  793. /* Setup test environment (time etc.) */
  794. client_side = TO_CIRCUIT(origin_circuit_new());
  795. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  796. monotime_enable_test_mocking();
  797. /* Create test machine */
  798. helper_create_machine_with_big_histogram(CIRCPAD_TOKEN_REMOVAL_EXACT);
  799. client_side->padding_machine[0] = &circ_client_machine;
  800. client_side->padding_info[0] =
  801. circpad_circuit_machineinfo_new(client_side, 0);
  802. /* move the machine to the right state */
  803. circpad_cell_event_nonpadding_received(client_side);
  804. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  805. CIRCPAD_STATE_BURST);
  806. /* Get the machine and setup tokens */
  807. mi = client_side->padding_info[0];
  808. tt_assert(mi);
  809. /**********************************************************************/
  810. uint64_t current_time = monotime_absolute_usec();
  811. /* Ensure that we will clear out bin #4 with this usec */
  812. mi->padding_scheduled_at_usec = current_time - 57;
  813. tt_int_op(mi->histogram[4], OP_EQ, 2);
  814. circpad_cell_event_nonpadding_sent(client_side);
  815. mi->padding_scheduled_at_usec = current_time - 57;
  816. tt_int_op(mi->histogram[4], OP_EQ, 1);
  817. circpad_cell_event_nonpadding_sent(client_side);
  818. tt_int_op(mi->histogram[4], OP_EQ, 0);
  819. /* Ensure that we will not remove any other tokens even tho we try to, since
  820. * this is what the exact strategy dictates */
  821. mi->padding_scheduled_at_usec = current_time - 57;
  822. circpad_cell_event_nonpadding_sent(client_side);
  823. for (int i = 0; i < BIG_HISTOGRAM_LEN ; i++) {
  824. if (i != 4) {
  825. tt_int_op(mi->histogram[i], OP_EQ, 2);
  826. }
  827. }
  828. done:
  829. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  830. monotime_disable_test_mocking();
  831. tor_free(circ_client_machine.states);
  832. testing_disable_reproducible_rng();
  833. }
  834. #undef BIG_HISTOGRAM_LEN
  835. void
  836. test_circuitpadding_tokens(void *arg)
  837. {
  838. const circpad_state_t *state;
  839. circpad_machine_runtime_t *mi;
  840. int64_t actual_mocked_monotime_start;
  841. (void)arg;
  842. testing_enable_reproducible_rng();
  843. /** Test plan:
  844. *
  845. * 1. Test symmetry between bin_to_usec and usec_to_bin
  846. * a. Test conversion
  847. * b. Test edge transitions (lower, upper)
  848. * 2. Test remove higher on an empty bin
  849. * a. Normal bin
  850. * b. Infinity bin
  851. * c. Bin 0
  852. * d. No higher
  853. * 3. Test remove lower
  854. * a. Normal bin
  855. * b. Bin 0
  856. * c. No lower
  857. * 4. Test remove closest
  858. * a. Closest lower
  859. * b. Closest higher
  860. * c. Closest 0
  861. * d. Closest Infinity
  862. */
  863. client_side = TO_CIRCUIT(origin_circuit_new());
  864. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  865. monotime_init();
  866. monotime_enable_test_mocking();
  867. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  868. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  869. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  870. curr_mocked_time = actual_mocked_monotime_start;
  871. /* This is needed so that we are not considered to be dormant */
  872. note_user_activity(20);
  873. timers_initialize();
  874. helper_create_basic_machine();
  875. client_side->padding_machine[0] = &circ_client_machine;
  876. client_side->padding_info[0] = circpad_circuit_machineinfo_new(client_side,
  877. 0);
  878. mi = client_side->padding_info[0];
  879. // Pretend a non-padding cell was sent
  880. circpad_cell_event_nonpadding_received(client_side);
  881. circpad_cell_event_nonpadding_sent(client_side);
  882. /* We have to save the infinity bin because one inf delay
  883. * could have been chosen when we transition to burst */
  884. circpad_hist_token_t inf_bin = mi->histogram[4];
  885. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  886. CIRCPAD_STATE_BURST);
  887. state = circpad_machine_current_state(client_side->padding_info[0]);
  888. // Test 0: convert bin->usec->bin
  889. // Bin 0+1 have different semantics
  890. for (int bin = 0; bin < 2; bin++) {
  891. circpad_delay_t usec =
  892. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  893. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  894. usec);
  895. tt_int_op(bin, OP_EQ, bin2);
  896. }
  897. for (int bin = 2; bin < state->histogram_len-1; bin++) {
  898. circpad_delay_t usec =
  899. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  900. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  901. usec);
  902. tt_int_op(bin, OP_EQ, bin2);
  903. /* Verify we round down */
  904. bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  905. usec+3);
  906. tt_int_op(bin, OP_EQ, bin2);
  907. bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  908. usec-1);
  909. tt_int_op(bin, OP_EQ, bin2+1);
  910. }
  911. // Test 1: converting usec->bin->usec->bin
  912. // Bin 0+1 have different semantics.
  913. for (circpad_delay_t i = 0; i <= state->histogram_edges[0]; i++) {
  914. int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  915. i);
  916. circpad_delay_t usec =
  917. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  918. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  919. usec);
  920. tt_int_op(bin, OP_EQ, bin2);
  921. tt_int_op(i, OP_LE, usec);
  922. }
  923. for (circpad_delay_t i = state->histogram_edges[0]+1;
  924. i <= state->histogram_edges[0] +
  925. state->histogram_edges[state->histogram_len-2]; i++) {
  926. int bin = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  927. i);
  928. circpad_delay_t usec =
  929. circpad_histogram_bin_to_usec(client_side->padding_info[0], bin);
  930. int bin2 = circpad_histogram_usec_to_bin(client_side->padding_info[0],
  931. usec);
  932. tt_int_op(bin, OP_EQ, bin2);
  933. tt_int_op(i, OP_GE, usec);
  934. }
  935. /* 2.a. Normal higher bin */
  936. {
  937. tt_int_op(mi->histogram[2], OP_EQ, 2);
  938. tt_int_op(mi->histogram[3], OP_EQ, 2);
  939. circpad_machine_remove_higher_token(mi,
  940. circpad_histogram_bin_to_usec(mi, 2)+1);
  941. tt_int_op(mi->histogram[3], OP_EQ, 2);
  942. tt_int_op(mi->histogram[2], OP_EQ, 1);
  943. circpad_machine_remove_higher_token(mi,
  944. circpad_histogram_bin_to_usec(mi, 2)+1);
  945. tt_int_op(mi->histogram[2], OP_EQ, 0);
  946. tt_int_op(mi->histogram[3], OP_EQ, 2);
  947. circpad_machine_remove_higher_token(mi,
  948. circpad_histogram_bin_to_usec(mi, 2)+1);
  949. circpad_machine_remove_higher_token(mi,
  950. circpad_histogram_bin_to_usec(mi, 2)+1);
  951. tt_int_op(mi->histogram[3], OP_EQ, 0);
  952. circpad_machine_remove_higher_token(mi,
  953. circpad_histogram_bin_to_usec(mi, 2)+1);
  954. tt_int_op(mi->histogram[3], OP_EQ, 0);
  955. }
  956. /* 2.b. Higher Infinity bin */
  957. {
  958. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  959. circpad_machine_remove_higher_token(mi,
  960. circpad_histogram_bin_to_usec(mi, 2)+1);
  961. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  962. /* Test past the infinity bin */
  963. circpad_machine_remove_higher_token(mi,
  964. circpad_histogram_bin_to_usec(mi, 5)+1000000);
  965. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  966. }
  967. /* 2.c. Bin 0 */
  968. {
  969. tt_int_op(mi->histogram[0], OP_EQ, 0);
  970. mi->histogram[0] = 1;
  971. circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
  972. tt_int_op(mi->histogram[0], OP_EQ, 0);
  973. }
  974. /* Drain the infinity bin and cause a refill */
  975. while (inf_bin != 0) {
  976. tt_int_op(mi->histogram[4], OP_EQ, inf_bin);
  977. circpad_cell_event_nonpadding_received(client_side);
  978. inf_bin--;
  979. }
  980. circpad_cell_event_nonpadding_sent(client_side);
  981. // We should have refilled here.
  982. tt_int_op(mi->histogram[4], OP_EQ, 2);
  983. /* 3.a. Bin 0 */
  984. {
  985. tt_int_op(mi->histogram[0], OP_EQ, 1);
  986. circpad_machine_remove_higher_token(mi, state->histogram_edges[0]/2);
  987. tt_int_op(mi->histogram[0], OP_EQ, 0);
  988. }
  989. /* 3.b. Test remove lower normal bin */
  990. {
  991. tt_int_op(mi->histogram[3], OP_EQ, 2);
  992. circpad_machine_remove_lower_token(mi,
  993. circpad_histogram_bin_to_usec(mi, 3)+1);
  994. circpad_machine_remove_lower_token(mi,
  995. circpad_histogram_bin_to_usec(mi, 3)+1);
  996. tt_int_op(mi->histogram[3], OP_EQ, 0);
  997. tt_int_op(mi->histogram[2], OP_EQ, 2);
  998. circpad_machine_remove_lower_token(mi,
  999. circpad_histogram_bin_to_usec(mi, 3)+1);
  1000. circpad_machine_remove_lower_token(mi,
  1001. circpad_histogram_bin_to_usec(mi, 3)+1);
  1002. /* 3.c. No lower */
  1003. circpad_machine_remove_lower_token(mi,
  1004. circpad_histogram_bin_to_usec(mi, 3)+1);
  1005. tt_int_op(mi->histogram[2], OP_EQ, 0);
  1006. }
  1007. /* 4. Test remove closest
  1008. * a. Closest lower
  1009. * b. Closest higher
  1010. * c. Closest 0
  1011. * d. Closest Infinity
  1012. */
  1013. circpad_machine_setup_tokens(mi);
  1014. tt_int_op(mi->histogram[2], OP_EQ, 2);
  1015. circpad_machine_remove_closest_token(mi,
  1016. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1017. circpad_machine_remove_closest_token(mi,
  1018. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1019. tt_int_op(mi->histogram[2], OP_EQ, 0);
  1020. tt_int_op(mi->histogram[3], OP_EQ, 2);
  1021. circpad_machine_remove_closest_token(mi,
  1022. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1023. circpad_machine_remove_closest_token(mi,
  1024. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1025. tt_int_op(mi->histogram[3], OP_EQ, 0);
  1026. tt_int_op(mi->histogram[0], OP_EQ, 1);
  1027. circpad_machine_remove_closest_token(mi,
  1028. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1029. tt_int_op(mi->histogram[0], OP_EQ, 0);
  1030. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1031. circpad_machine_remove_closest_token(mi,
  1032. circpad_histogram_bin_to_usec(mi, 2)+1, 0);
  1033. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1034. /* 5. Test remove closest usec
  1035. * a. Closest 0
  1036. * b. Closest lower (below midpoint)
  1037. * c. Closest higher (above midpoint)
  1038. * d. Closest Infinity
  1039. */
  1040. circpad_machine_setup_tokens(mi);
  1041. tt_int_op(mi->histogram[0], OP_EQ, 1);
  1042. circpad_machine_remove_closest_token(mi,
  1043. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1044. tt_int_op(mi->histogram[0], OP_EQ, 0);
  1045. tt_int_op(mi->histogram[2], OP_EQ, 2);
  1046. circpad_machine_remove_closest_token(mi,
  1047. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1048. circpad_machine_remove_closest_token(mi,
  1049. circpad_histogram_bin_to_usec(mi, 0)/3, 1);
  1050. tt_int_op(mi->histogram[2], OP_EQ, 0);
  1051. tt_int_op(mi->histogram[3], OP_EQ, 2);
  1052. circpad_machine_remove_closest_token(mi,
  1053. circpad_histogram_bin_to_usec(mi, 4), 1);
  1054. circpad_machine_remove_closest_token(mi,
  1055. circpad_histogram_bin_to_usec(mi, 4), 1);
  1056. tt_int_op(mi->histogram[3], OP_EQ, 0);
  1057. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1058. circpad_machine_remove_closest_token(mi,
  1059. circpad_histogram_bin_to_usec(mi, 4), 1);
  1060. circpad_machine_remove_closest_token(mi,
  1061. circpad_histogram_bin_to_usec(mi, 4), 1);
  1062. tt_int_op(mi->histogram[4], OP_EQ, 2);
  1063. // XXX: Need more coverage of the actual usec branches
  1064. done:
  1065. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1066. monotime_disable_test_mocking();
  1067. tor_free(circ_client_machine.states);
  1068. testing_disable_reproducible_rng();
  1069. }
  1070. void
  1071. test_circuitpadding_wronghop(void *arg)
  1072. {
  1073. /**
  1074. * Test plan:
  1075. * 1. Padding sent from hop 1 and 3 to client
  1076. * 2. Send negotiated from hop 1 and 3 to client
  1077. * 3. Garbled negotiated cell
  1078. * 4. Padding negotiate sent to client
  1079. * 5. Send negotiate stop command for unknown machine
  1080. * 6. Send negotiated to relay
  1081. * 7. Garbled padding negotiate cell
  1082. */
  1083. (void)arg;
  1084. uint32_t read_bw = 0, overhead_bw = 0;
  1085. cell_t cell;
  1086. signed_error_t ret;
  1087. origin_circuit_t *orig_client;
  1088. int64_t actual_mocked_monotime_start;
  1089. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1090. /* Mock this function so that our cell counting tests don't get confused by
  1091. * padding that gets sent by scheduled timers. */
  1092. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  1093. testing_enable_reproducible_rng();
  1094. client_side = TO_CIRCUIT(origin_circuit_new());
  1095. dummy_channel.cmux = circuitmux_alloc();
  1096. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel,
  1097. &dummy_channel));
  1098. orig_client = TO_ORIGIN_CIRCUIT(client_side);
  1099. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1100. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1101. nodes_init();
  1102. monotime_init();
  1103. monotime_enable_test_mocking();
  1104. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1105. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1106. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1107. curr_mocked_time = actual_mocked_monotime_start;
  1108. timers_initialize();
  1109. circpad_machines_init();
  1110. MOCK(node_get_by_id,
  1111. node_get_by_id_mock);
  1112. MOCK(circuit_package_relay_cell,
  1113. circuit_package_relay_cell_mock);
  1114. /* Build three hops */
  1115. simulate_single_hop_extend(client_side, relay_side, 1);
  1116. simulate_single_hop_extend(client_side, relay_side, 1);
  1117. simulate_single_hop_extend(client_side, relay_side, 1);
  1118. /* verify padding was negotiated */
  1119. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1120. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1121. /* verify echo was sent */
  1122. tt_int_op(n_relay_cells, OP_EQ, 1);
  1123. tt_int_op(n_client_cells, OP_EQ, 1);
  1124. read_bw = orig_client->n_delivered_read_circ_bw;
  1125. overhead_bw = orig_client->n_overhead_read_circ_bw;
  1126. /* 1. Test padding from first and third hop */
  1127. circpad_deliver_recognized_relay_cell_events(client_side,
  1128. RELAY_COMMAND_DROP,
  1129. TO_ORIGIN_CIRCUIT(client_side)->cpath);
  1130. tt_int_op(read_bw, OP_EQ,
  1131. orig_client->n_delivered_read_circ_bw);
  1132. tt_int_op(overhead_bw, OP_EQ,
  1133. orig_client->n_overhead_read_circ_bw);
  1134. circpad_deliver_recognized_relay_cell_events(client_side,
  1135. RELAY_COMMAND_DROP,
  1136. TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
  1137. tt_int_op(read_bw, OP_EQ,
  1138. orig_client->n_delivered_read_circ_bw);
  1139. tt_int_op(overhead_bw, OP_EQ,
  1140. orig_client->n_overhead_read_circ_bw);
  1141. circpad_deliver_recognized_relay_cell_events(client_side,
  1142. RELAY_COMMAND_DROP,
  1143. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1144. tt_int_op(read_bw, OP_EQ,
  1145. orig_client->n_delivered_read_circ_bw);
  1146. tt_int_op(overhead_bw, OP_LT,
  1147. orig_client->n_overhead_read_circ_bw);
  1148. /* 2. Test padding negotiated not handled from hops 1,3 */
  1149. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1150. TO_ORIGIN_CIRCUIT(client_side)->cpath);
  1151. tt_int_op(ret, OP_EQ, -1);
  1152. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1153. TO_ORIGIN_CIRCUIT(client_side)->cpath->next->next);
  1154. tt_int_op(ret, OP_EQ, -1);
  1155. /* 3. Garbled negotiated cell */
  1156. memset(&cell, 255, sizeof(cell));
  1157. ret = circpad_handle_padding_negotiated(client_side, &cell,
  1158. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1159. tt_int_op(ret, OP_EQ, -1);
  1160. /* 4. Test that negotiate is dropped at origin */
  1161. read_bw = orig_client->n_delivered_read_circ_bw;
  1162. overhead_bw = orig_client->n_overhead_read_circ_bw;
  1163. relay_send_command_from_edge(0, relay_side,
  1164. RELAY_COMMAND_PADDING_NEGOTIATE,
  1165. (void*)cell.payload,
  1166. (size_t)3, NULL);
  1167. tt_int_op(read_bw, OP_EQ,
  1168. orig_client->n_delivered_read_circ_bw);
  1169. tt_int_op(overhead_bw, OP_EQ,
  1170. orig_client->n_overhead_read_circ_bw);
  1171. tt_int_op(n_relay_cells, OP_EQ, 2);
  1172. tt_int_op(n_client_cells, OP_EQ, 1);
  1173. /* 5. Test that asking to stop the wrong machine does nothing */
  1174. circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
  1175. 255, 2, CIRCPAD_COMMAND_STOP);
  1176. tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
  1177. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  1178. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1179. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1180. tt_int_op(n_relay_cells, OP_EQ, 3);
  1181. tt_int_op(n_client_cells, OP_EQ, 2);
  1182. /* 6. Sending negotiated command to relay does nothing */
  1183. ret = circpad_handle_padding_negotiated(relay_side, &cell, NULL);
  1184. tt_int_op(ret, OP_EQ, -1);
  1185. /* 7. Test garbled negotated cell (bad command 255) */
  1186. memset(&cell, 0, sizeof(cell));
  1187. ret = circpad_handle_padding_negotiate(relay_side, &cell);
  1188. tt_int_op(ret, OP_EQ, -1);
  1189. tt_int_op(n_client_cells, OP_EQ, 2);
  1190. /* Test 2: Test no padding */
  1191. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1192. free_fake_orcirc(relay_side);
  1193. client_side = TO_CIRCUIT(origin_circuit_new());
  1194. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel,
  1195. &dummy_channel));
  1196. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1197. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1198. simulate_single_hop_extend(client_side, relay_side, 1);
  1199. simulate_single_hop_extend(client_side, relay_side, 0);
  1200. /* verify no padding was negotiated */
  1201. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1202. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1203. tt_int_op(n_relay_cells, OP_EQ, 3);
  1204. tt_int_op(n_client_cells, OP_EQ, 2);
  1205. /* verify no echo was sent */
  1206. tt_int_op(n_relay_cells, OP_EQ, 3);
  1207. tt_int_op(n_client_cells, OP_EQ, 2);
  1208. /* Finish circuit */
  1209. simulate_single_hop_extend(client_side, relay_side, 1);
  1210. /* Spoof padding negotiated on circuit with no padding */
  1211. circpad_padding_negotiated(relay_side,
  1212. CIRCPAD_MACHINE_CIRC_SETUP,
  1213. CIRCPAD_COMMAND_START,
  1214. CIRCPAD_RESPONSE_OK);
  1215. /* verify no padding was negotiated */
  1216. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1217. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1218. circpad_padding_negotiated(relay_side,
  1219. CIRCPAD_MACHINE_CIRC_SETUP,
  1220. CIRCPAD_COMMAND_START,
  1221. CIRCPAD_RESPONSE_ERR);
  1222. /* verify no padding was negotiated */
  1223. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1224. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1225. done:
  1226. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1227. free_fake_orcirc(relay_side);
  1228. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1229. circuitmux_free(dummy_channel.cmux);
  1230. monotime_disable_test_mocking();
  1231. UNMOCK(node_get_by_id);
  1232. UNMOCK(circuit_package_relay_cell);
  1233. UNMOCK(circuitmux_attach_circuit);
  1234. nodes_free();
  1235. testing_disable_reproducible_rng();
  1236. }
  1237. void
  1238. test_circuitpadding_negotiation(void *arg)
  1239. {
  1240. /**
  1241. * Test plan:
  1242. * 1. Test circuit where padding is supported by middle
  1243. * a. Make sure padding negotiation is sent
  1244. * b. Test padding negotiation delivery and parsing
  1245. * 2. Test circuit where padding is unsupported by middle
  1246. * a. Make sure padding negotiation is not sent
  1247. * 3. Test failure to negotiate a machine due to desync.
  1248. */
  1249. int64_t actual_mocked_monotime_start;
  1250. (void)arg;
  1251. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1252. client_side = TO_CIRCUIT(origin_circuit_new());
  1253. dummy_channel.cmux = circuitmux_alloc();
  1254. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1255. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1256. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1257. nodes_init();
  1258. monotime_init();
  1259. monotime_enable_test_mocking();
  1260. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1261. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1262. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1263. curr_mocked_time = actual_mocked_monotime_start;
  1264. timers_initialize();
  1265. circpad_machines_init();
  1266. MOCK(node_get_by_id,
  1267. node_get_by_id_mock);
  1268. MOCK(circuit_package_relay_cell,
  1269. circuit_package_relay_cell_mock);
  1270. /* Build two hops */
  1271. simulate_single_hop_extend(client_side, relay_side, 1);
  1272. simulate_single_hop_extend(client_side, relay_side, 1);
  1273. /* verify padding was negotiated */
  1274. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  1275. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1276. /* verify echo was sent */
  1277. tt_int_op(n_relay_cells, OP_EQ, 1);
  1278. tt_int_op(n_client_cells, OP_EQ, 1);
  1279. /* Finish circuit */
  1280. simulate_single_hop_extend(client_side, relay_side, 1);
  1281. /* Test 2: Test no padding */
  1282. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1283. free_fake_orcirc(relay_side);
  1284. client_side = TO_CIRCUIT(origin_circuit_new());
  1285. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1286. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1287. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1288. simulate_single_hop_extend(client_side, relay_side, 1);
  1289. simulate_single_hop_extend(client_side, relay_side, 0);
  1290. /* verify no padding was negotiated */
  1291. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1292. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1293. tt_int_op(n_relay_cells, OP_EQ, 1);
  1294. tt_int_op(n_client_cells, OP_EQ, 1);
  1295. /* verify no echo was sent */
  1296. tt_int_op(n_relay_cells, OP_EQ, 1);
  1297. tt_int_op(n_client_cells, OP_EQ, 1);
  1298. /* Finish circuit */
  1299. simulate_single_hop_extend(client_side, relay_side, 1);
  1300. /* Force negotiate padding. */
  1301. circpad_negotiate_padding(TO_ORIGIN_CIRCUIT(client_side),
  1302. CIRCPAD_MACHINE_CIRC_SETUP,
  1303. 2, CIRCPAD_COMMAND_START);
  1304. /* verify no padding was negotiated */
  1305. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1306. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1307. /* verify no echo was sent */
  1308. tt_int_op(n_relay_cells, OP_EQ, 1);
  1309. tt_int_op(n_client_cells, OP_EQ, 1);
  1310. /* 3. Test failure to negotiate a machine due to desync */
  1311. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1312. free_fake_orcirc(relay_side);
  1313. client_side = TO_CIRCUIT(origin_circuit_new());
  1314. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1315. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1316. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1317. SMARTLIST_FOREACH(relay_padding_machines,
  1318. circpad_machine_spec_t *,
  1319. m, tor_free(m->states); tor_free(m));
  1320. smartlist_free(relay_padding_machines);
  1321. relay_padding_machines = smartlist_new();
  1322. simulate_single_hop_extend(client_side, relay_side, 1);
  1323. simulate_single_hop_extend(client_side, relay_side, 1);
  1324. /* verify echo was sent */
  1325. tt_int_op(n_client_cells, OP_EQ, 2);
  1326. tt_int_op(n_relay_cells, OP_EQ, 2);
  1327. /* verify no padding was negotiated */
  1328. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1329. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1330. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1331. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1332. done:
  1333. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1334. free_fake_orcirc(relay_side);
  1335. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1336. circuitmux_free(dummy_channel.cmux);
  1337. monotime_disable_test_mocking();
  1338. UNMOCK(node_get_by_id);
  1339. UNMOCK(circuit_package_relay_cell);
  1340. UNMOCK(circuitmux_attach_circuit);
  1341. nodes_free();
  1342. }
  1343. static void
  1344. simulate_single_hop_extend(circuit_t *client, circuit_t *mid_relay,
  1345. int padding)
  1346. {
  1347. char whatevs_key[CPATH_KEY_MATERIAL_LEN];
  1348. char digest[DIGEST_LEN];
  1349. tor_addr_t addr;
  1350. // Pretend a non-padding cell was sent
  1351. circpad_cell_event_nonpadding_sent(client);
  1352. // Receive extend cell at middle
  1353. circpad_cell_event_nonpadding_received(mid_relay);
  1354. // Advance time a tiny bit so we can calculate an RTT
  1355. curr_mocked_time += 10 * TOR_NSEC_PER_MSEC;
  1356. monotime_coarse_set_mock_time_nsec(curr_mocked_time);
  1357. monotime_set_mock_time_nsec(curr_mocked_time);
  1358. // Receive extended cell at middle
  1359. circpad_cell_event_nonpadding_sent(mid_relay);
  1360. // Receive extended cell at first hop
  1361. circpad_cell_event_nonpadding_received(client);
  1362. // Add a hop to cpath
  1363. crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
  1364. cpath_extend_linked_list(&TO_ORIGIN_CIRCUIT(client)->cpath, hop);
  1365. hop->magic = CRYPT_PATH_MAGIC;
  1366. hop->state = CPATH_STATE_OPEN;
  1367. // add an extend info to indicate if this node supports padding or not.
  1368. // (set the first byte of the digest for our mocked node_get_by_id)
  1369. digest[0] = padding;
  1370. hop->extend_info = extend_info_new(
  1371. padding ? "padding" : "non-padding",
  1372. digest, NULL, NULL, NULL,
  1373. &addr, padding);
  1374. cpath_init_circuit_crypto(hop, whatevs_key, sizeof(whatevs_key), 0, 0);
  1375. hop->package_window = circuit_initial_package_window();
  1376. hop->deliver_window = CIRCWINDOW_START;
  1377. // Signal that the hop was added
  1378. circpad_machine_event_circ_added_hop(TO_ORIGIN_CIRCUIT(client));
  1379. }
  1380. static circpad_machine_spec_t *
  1381. helper_create_length_machine(void)
  1382. {
  1383. circpad_machine_spec_t *ret =
  1384. tor_malloc_zero(sizeof(circpad_machine_spec_t));
  1385. /* Start, burst */
  1386. circpad_machine_states_init(ret, 2);
  1387. ret->states[CIRCPAD_STATE_START].
  1388. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1389. ret->states[CIRCPAD_STATE_BURST].
  1390. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1391. ret->states[CIRCPAD_STATE_BURST].
  1392. next_state[CIRCPAD_EVENT_LENGTH_COUNT] = CIRCPAD_STATE_END;
  1393. ret->states[CIRCPAD_STATE_BURST].
  1394. next_state[CIRCPAD_EVENT_BINS_EMPTY] = CIRCPAD_STATE_END;
  1395. /* No token removal.. end via state_length only */
  1396. ret->states[CIRCPAD_STATE_BURST].token_removal =
  1397. CIRCPAD_TOKEN_REMOVAL_NONE;
  1398. /* Let's have this one end after 12 packets */
  1399. ret->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
  1400. ret->states[CIRCPAD_STATE_BURST].length_dist.param1 = 12;
  1401. ret->states[CIRCPAD_STATE_BURST].length_dist.param2 = 13;
  1402. ret->states[CIRCPAD_STATE_BURST].max_length = 12;
  1403. ret->states[CIRCPAD_STATE_BURST].histogram_len = 4;
  1404. ret->states[CIRCPAD_STATE_BURST].histogram_edges[0] = 0;
  1405. ret->states[CIRCPAD_STATE_BURST].histogram_edges[1] = 1;
  1406. ret->states[CIRCPAD_STATE_BURST].histogram_edges[2] = 1000000;
  1407. ret->states[CIRCPAD_STATE_BURST].histogram_edges[3] = 10000000;
  1408. ret->states[CIRCPAD_STATE_BURST].histogram[0] = 0;
  1409. ret->states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  1410. ret->states[CIRCPAD_STATE_BURST].histogram[2] = 6;
  1411. ret->states[CIRCPAD_STATE_BURST].histogram_total_tokens = 6;
  1412. ret->states[CIRCPAD_STATE_BURST].use_rtt_estimate = 0;
  1413. ret->states[CIRCPAD_STATE_BURST].length_includes_nonpadding = 0;
  1414. return ret;
  1415. }
  1416. static circpad_machine_spec_t *
  1417. helper_create_conditional_machine(void)
  1418. {
  1419. circpad_machine_spec_t *ret =
  1420. tor_malloc_zero(sizeof(circpad_machine_spec_t));
  1421. /* Start, burst */
  1422. circpad_machine_states_init(ret, 2);
  1423. ret->states[CIRCPAD_STATE_START].
  1424. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1425. ret->states[CIRCPAD_STATE_BURST].
  1426. next_state[CIRCPAD_EVENT_PADDING_SENT] = CIRCPAD_STATE_BURST;
  1427. ret->states[CIRCPAD_STATE_BURST].
  1428. next_state[CIRCPAD_EVENT_LENGTH_COUNT] = CIRCPAD_STATE_END;
  1429. /* Use EXACT removal strategy, otherwise setup_tokens() does not work */
  1430. ret->states[CIRCPAD_STATE_BURST].token_removal =
  1431. CIRCPAD_TOKEN_REMOVAL_EXACT;
  1432. ret->states[CIRCPAD_STATE_BURST].histogram_len = 3;
  1433. ret->states[CIRCPAD_STATE_BURST].histogram_edges[0] = 0;
  1434. ret->states[CIRCPAD_STATE_BURST].histogram_edges[1] = 1;
  1435. ret->states[CIRCPAD_STATE_BURST].histogram_edges[2] = 1000000;
  1436. ret->states[CIRCPAD_STATE_BURST].histogram[0] = 6;
  1437. ret->states[CIRCPAD_STATE_BURST].histogram[1] = 0;
  1438. ret->states[CIRCPAD_STATE_BURST].histogram[2] = 0;
  1439. ret->states[CIRCPAD_STATE_BURST].histogram_total_tokens = 6;
  1440. ret->states[CIRCPAD_STATE_BURST].use_rtt_estimate = 0;
  1441. ret->states[CIRCPAD_STATE_BURST].length_includes_nonpadding = 1;
  1442. return ret;
  1443. }
  1444. static void
  1445. helper_create_conditional_machines(void)
  1446. {
  1447. circpad_machine_spec_t *add = helper_create_conditional_machine();
  1448. if (!origin_padding_machines)
  1449. origin_padding_machines = smartlist_new();
  1450. if (!relay_padding_machines)
  1451. relay_padding_machines = smartlist_new();
  1452. add->machine_num = 2;
  1453. add->is_origin_side = 1;
  1454. add->should_negotiate_end = 1;
  1455. add->target_hopnum = 2;
  1456. /* Let's have this one end after 4 packets */
  1457. add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
  1458. add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
  1459. add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
  1460. add->states[CIRCPAD_STATE_BURST].max_length = 4;
  1461. add->conditions.requires_vanguards = 0;
  1462. add->conditions.min_hops = 2;
  1463. add->conditions.state_mask = CIRCPAD_CIRC_BUILDING|
  1464. CIRCPAD_CIRC_NO_STREAMS|CIRCPAD_CIRC_HAS_RELAY_EARLY;
  1465. add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
  1466. circpad_register_padding_machine(add, origin_padding_machines);
  1467. add = helper_create_conditional_machine();
  1468. add->machine_num = 3;
  1469. add->is_origin_side = 1;
  1470. add->should_negotiate_end = 1;
  1471. add->target_hopnum = 2;
  1472. /* Let's have this one end after 4 packets */
  1473. add->states[CIRCPAD_STATE_BURST].length_dist.type = CIRCPAD_DIST_UNIFORM;
  1474. add->states[CIRCPAD_STATE_BURST].length_dist.param1 = 4;
  1475. add->states[CIRCPAD_STATE_BURST].length_dist.param2 = 4;
  1476. add->states[CIRCPAD_STATE_BURST].max_length = 4;
  1477. add->conditions.requires_vanguards = 1;
  1478. add->conditions.min_hops = 3;
  1479. add->conditions.state_mask = CIRCPAD_CIRC_OPENED|
  1480. CIRCPAD_CIRC_STREAMS|CIRCPAD_CIRC_HAS_NO_RELAY_EARLY;
  1481. add->conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
  1482. circpad_register_padding_machine(add, origin_padding_machines);
  1483. add = helper_create_conditional_machine();
  1484. add->machine_num = 2;
  1485. circpad_register_padding_machine(add, relay_padding_machines);
  1486. add = helper_create_conditional_machine();
  1487. add->machine_num = 3;
  1488. circpad_register_padding_machine(add, relay_padding_machines);
  1489. }
  1490. void
  1491. test_circuitpadding_state_length(void *arg)
  1492. {
  1493. /**
  1494. * Test plan:
  1495. * * Explicitly test that with no token removal enabled, we hit
  1496. * the state length limit due to either padding, or non-padding.
  1497. * * Repeat test with an arbitrary token removal strategy, and
  1498. * verify that if we run out of tokens due to padding before we
  1499. * hit the state length, we still go to state end (all our
  1500. * token removal tests only test nonpadding token removal).
  1501. */
  1502. int64_t actual_mocked_monotime_start;
  1503. (void)arg;
  1504. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1505. MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
  1506. nodes_init();
  1507. dummy_channel.cmux = circuitmux_alloc();
  1508. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel,
  1509. &dummy_channel));
  1510. client_side = TO_CIRCUIT(origin_circuit_new());
  1511. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1512. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1513. monotime_init();
  1514. monotime_enable_test_mocking();
  1515. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1516. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1517. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1518. curr_mocked_time = actual_mocked_monotime_start;
  1519. /* This is needed so that we are not considered to be dormant */
  1520. note_user_activity(20);
  1521. timers_initialize();
  1522. circpad_machine_spec_t *client_machine =
  1523. helper_create_length_machine();
  1524. MOCK(circuit_package_relay_cell,
  1525. circuit_package_relay_cell_mock);
  1526. MOCK(node_get_by_id,
  1527. node_get_by_id_mock);
  1528. client_side->padding_machine[0] = client_machine;
  1529. client_side->padding_info[0] =
  1530. circpad_circuit_machineinfo_new(client_side, 0);
  1531. circpad_machine_runtime_t *mi = client_side->padding_info[0];
  1532. circpad_cell_event_padding_sent(client_side);
  1533. tt_i64_op(mi->state_length, OP_EQ, 12);
  1534. tt_ptr_op(mi->histogram, OP_EQ, NULL);
  1535. /* Verify that non-padding does not change our state length */
  1536. circpad_cell_event_nonpadding_sent(client_side);
  1537. tt_i64_op(mi->state_length, OP_EQ, 12);
  1538. /* verify that sending padding changes our state length */
  1539. for (uint64_t i = mi->state_length-1; i > 0; i--) {
  1540. circpad_send_padding_cell_for_callback(mi);
  1541. tt_i64_op(mi->state_length, OP_EQ, i);
  1542. }
  1543. circpad_send_padding_cell_for_callback(mi);
  1544. tt_i64_op(mi->state_length, OP_EQ, -1);
  1545. tt_int_op(mi->current_state, OP_EQ, CIRCPAD_STATE_END);
  1546. /* Restart machine */
  1547. mi->current_state = CIRCPAD_STATE_START;
  1548. /* Now, count nonpadding as part of the state length */
  1549. client_machine->states[CIRCPAD_STATE_BURST].length_includes_nonpadding = 1;
  1550. circpad_cell_event_padding_sent(client_side);
  1551. tt_i64_op(mi->state_length, OP_EQ, 12);
  1552. /* Verify that non-padding does change our state length now */
  1553. for (uint64_t i = mi->state_length-1; i > 0; i--) {
  1554. circpad_cell_event_nonpadding_sent(client_side);
  1555. tt_i64_op(mi->state_length, OP_EQ, i);
  1556. }
  1557. circpad_cell_event_nonpadding_sent(client_side);
  1558. tt_i64_op(mi->state_length, OP_EQ, -1);
  1559. tt_int_op(mi->current_state, OP_EQ, CIRCPAD_STATE_END);
  1560. /* Now, just test token removal when we send padding */
  1561. client_machine->states[CIRCPAD_STATE_BURST].token_removal =
  1562. CIRCPAD_TOKEN_REMOVAL_EXACT;
  1563. /* Restart machine */
  1564. mi->current_state = CIRCPAD_STATE_START;
  1565. circpad_cell_event_padding_sent(client_side);
  1566. tt_i64_op(mi->state_length, OP_EQ, 12);
  1567. tt_ptr_op(mi->histogram, OP_NE, NULL);
  1568. tt_int_op(mi->chosen_bin, OP_EQ, 2);
  1569. /* verify that sending padding changes our state length and
  1570. * our histogram now */
  1571. for (uint32_t i = mi->histogram[2]-1; i > 0; i--) {
  1572. circpad_send_padding_cell_for_callback(mi);
  1573. tt_int_op(mi->chosen_bin, OP_EQ, 2);
  1574. tt_int_op(mi->histogram[2], OP_EQ, i);
  1575. }
  1576. tt_i64_op(mi->state_length, OP_EQ, 7);
  1577. tt_int_op(mi->histogram[2], OP_EQ, 1);
  1578. circpad_send_padding_cell_for_callback(mi);
  1579. tt_int_op(mi->current_state, OP_EQ, CIRCPAD_STATE_END);
  1580. done:
  1581. tor_free(client_machine->states);
  1582. tor_free(client_machine);
  1583. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1584. free_fake_orcirc(relay_side);
  1585. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1586. circuitmux_free(dummy_channel.cmux);
  1587. timers_shutdown();
  1588. monotime_disable_test_mocking();
  1589. UNMOCK(circuit_package_relay_cell);
  1590. UNMOCK(circuitmux_attach_circuit);
  1591. UNMOCK(node_get_by_id);
  1592. return;
  1593. }
  1594. void
  1595. test_circuitpadding_conditions(void *arg)
  1596. {
  1597. /**
  1598. * Test plan:
  1599. * 0. Make a few origin and client machines with diff conditions
  1600. * * vanguards, purposes, has_opened circs, no relay early
  1601. * * Client side should_negotiate_end
  1602. * * Length limits
  1603. * 1. Test STATE_END transitions
  1604. * 2. Test new machine after end with same conditions
  1605. * 3. Test new machine due to changed conditions
  1606. * * Esp: built event, no relay early, no streams
  1607. * XXX: Diff test:
  1608. * 1. Test STATE_END with pending timers
  1609. * 2. Test marking a circuit before padding callback fires
  1610. * 3. Test freeing a circuit before padding callback fires
  1611. */
  1612. int64_t actual_mocked_monotime_start;
  1613. (void)arg;
  1614. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1615. testing_enable_reproducible_rng();
  1616. nodes_init();
  1617. dummy_channel.cmux = circuitmux_alloc();
  1618. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel,
  1619. &dummy_channel));
  1620. client_side = TO_CIRCUIT(origin_circuit_new());
  1621. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1622. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1623. monotime_init();
  1624. monotime_enable_test_mocking();
  1625. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1626. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1627. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1628. curr_mocked_time = actual_mocked_monotime_start;
  1629. /* This is needed so that we are not considered to be dormant */
  1630. note_user_activity(20);
  1631. timers_initialize();
  1632. helper_create_conditional_machines();
  1633. MOCK(circuit_package_relay_cell,
  1634. circuit_package_relay_cell_mock);
  1635. MOCK(node_get_by_id,
  1636. node_get_by_id_mock);
  1637. /* Simulate extend. This should result in the original machine getting
  1638. * added, since the circuit is not built */
  1639. simulate_single_hop_extend(client_side, relay_side, 1);
  1640. simulate_single_hop_extend(client_side, relay_side, 1);
  1641. /* Verify that machine #2 is added */
  1642. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1643. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1644. /* Deliver a padding cell to the client, to trigger burst state */
  1645. circpad_cell_event_padding_sent(client_side);
  1646. /* This should have trigger length shutdown condition on client.. */
  1647. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1648. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1649. /* Verify machine is gone from both sides */
  1650. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1651. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1652. /* Send another event.. verify machine gets re-added properly
  1653. * (test race with shutdown) */
  1654. simulate_single_hop_extend(client_side, relay_side, 1);
  1655. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1656. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1657. TO_ORIGIN_CIRCUIT(client_side)->p_streams = 0;
  1658. circpad_machine_event_circ_has_no_streams(TO_ORIGIN_CIRCUIT(client_side));
  1659. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1660. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1661. /* Now make the circuit opened and send built event */
  1662. TO_ORIGIN_CIRCUIT(client_side)->has_opened = 1;
  1663. circpad_machine_event_circ_built(TO_ORIGIN_CIRCUIT(client_side));
  1664. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1665. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1666. TO_ORIGIN_CIRCUIT(client_side)->remaining_relay_early_cells = 0;
  1667. circpad_machine_event_circ_has_no_relay_early(
  1668. TO_ORIGIN_CIRCUIT(client_side));
  1669. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1670. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  1671. get_options_mutable()->HSLayer2Nodes = (void*)1;
  1672. TO_ORIGIN_CIRCUIT(client_side)->p_streams = (void*)1;
  1673. circpad_machine_event_circ_has_streams(TO_ORIGIN_CIRCUIT(client_side));
  1674. /* Verify different machine is added */
  1675. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1676. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1677. /* Hold off on negotiated */
  1678. deliver_negotiated = 0;
  1679. /* Deliver a padding cell to the client, to trigger burst state */
  1680. circpad_cell_event_padding_sent(client_side);
  1681. /* This should have trigger length shutdown condition on client
  1682. * but not the response for the padding machine */
  1683. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1684. tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
  1685. /* Verify machine is gone from the relay (but negotiated not back yet */
  1686. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1687. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1688. /* Add another hop and verify it's back */
  1689. simulate_single_hop_extend(client_side, relay_side, 1);
  1690. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1691. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 3);
  1692. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  1693. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  1694. done:
  1695. /* XXX: Free everything */
  1696. testing_disable_reproducible_rng();
  1697. return;
  1698. }
  1699. /** Disabled unstable test until #29298 is implemented (see #29122) */
  1700. #if 0
  1701. void
  1702. test_circuitpadding_circuitsetup_machine(void *arg)
  1703. {
  1704. int64_t actual_mocked_monotime_start;
  1705. /**
  1706. * Test case plan:
  1707. *
  1708. * 1. Simulate a normal circuit setup pattern
  1709. * a. Application traffic
  1710. *
  1711. * FIXME: This should focus more on exercising the machine
  1712. * features rather than actual traffic patterns. For example,
  1713. * test cancellation and bins empty/refill
  1714. */
  1715. (void)arg;
  1716. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  1717. dummy_channel.cmux = circuitmux_alloc();
  1718. client_side = TO_CIRCUIT(origin_circuit_new());
  1719. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  1720. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  1721. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1722. nodes_init();
  1723. monotime_init();
  1724. monotime_enable_test_mocking();
  1725. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  1726. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  1727. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  1728. curr_mocked_time = actual_mocked_monotime_start;
  1729. timers_initialize();
  1730. circpad_machines_init();
  1731. MOCK(circuit_package_relay_cell,
  1732. circuit_package_relay_cell_mock);
  1733. MOCK(node_get_by_id,
  1734. node_get_by_id_mock);
  1735. /* Test case #1: Build a 3 hop circuit, then wait and let pad */
  1736. simulate_single_hop_extend(client_side, relay_side, 1);
  1737. simulate_single_hop_extend(client_side, relay_side, 1);
  1738. simulate_single_hop_extend(client_side, relay_side, 1);
  1739. tt_int_op(n_client_cells, OP_EQ, 1);
  1740. tt_int_op(n_relay_cells, OP_EQ, 1);
  1741. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1742. CIRCPAD_STATE_BURST);
  1743. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1744. CIRCPAD_STATE_BURST);
  1745. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1746. OP_NE, 0);
  1747. tt_int_op(relay_side->padding_info[0]->is_padding_timer_scheduled,
  1748. OP_EQ, 0);
  1749. timers_advance_and_run(2000);
  1750. tt_int_op(n_client_cells, OP_EQ, 2);
  1751. tt_int_op(n_relay_cells, OP_EQ, 1);
  1752. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1753. CIRCPAD_STATE_GAP);
  1754. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1755. OP_EQ, 0);
  1756. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1757. OP_NE, 0);
  1758. timers_advance_and_run(5000);
  1759. tt_int_op(n_client_cells, OP_EQ, 2);
  1760. tt_int_op(n_relay_cells, OP_EQ, 2);
  1761. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1762. OP_NE, 0);
  1763. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1764. OP_EQ, 0);
  1765. timers_advance_and_run(2000);
  1766. tt_int_op(n_client_cells, OP_EQ, 3);
  1767. tt_int_op(n_relay_cells, OP_EQ, 2);
  1768. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1769. OP_EQ, 0);
  1770. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1771. OP_NE, 0);
  1772. timers_advance_and_run(5000);
  1773. tt_int_op(n_client_cells, OP_EQ, 3);
  1774. tt_int_op(n_relay_cells, OP_EQ, 3);
  1775. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1776. OP_NE, 0);
  1777. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1778. OP_EQ, 0);
  1779. timers_advance_and_run(2000);
  1780. tt_int_op(n_client_cells, OP_EQ, 4);
  1781. tt_int_op(n_relay_cells, OP_EQ, 3);
  1782. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1783. OP_EQ, 0);
  1784. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1785. OP_NE, 0);
  1786. timers_advance_and_run(5000);
  1787. tt_int_op(n_client_cells, OP_EQ, 4);
  1788. tt_int_op(n_relay_cells, OP_EQ, 4);
  1789. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1790. OP_NE, 0);
  1791. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1792. OP_EQ, 0);
  1793. timers_advance_and_run(2000);
  1794. tt_int_op(n_client_cells, OP_EQ, 5);
  1795. tt_int_op(n_relay_cells, OP_EQ, 4);
  1796. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1797. OP_EQ, 0);
  1798. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1799. OP_NE, 0);
  1800. timers_advance_and_run(5000);
  1801. tt_int_op(n_client_cells, OP_EQ, 5);
  1802. tt_int_op(n_relay_cells, OP_EQ, 5);
  1803. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1804. OP_NE, 0);
  1805. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1806. OP_EQ, 0);
  1807. timers_advance_and_run(2000);
  1808. tt_int_op(n_client_cells, OP_EQ, 6);
  1809. tt_int_op(n_relay_cells, OP_EQ, 5);
  1810. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1811. OP_EQ, 0);
  1812. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1813. OP_NE, 0);
  1814. timers_advance_and_run(5000);
  1815. tt_int_op(n_client_cells, OP_EQ, 6);
  1816. tt_int_op(n_relay_cells, OP_EQ, 6);
  1817. tt_int_op(client_side->padding_info[0]->current_state,
  1818. OP_EQ, CIRCPAD_STATE_END);
  1819. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1820. OP_EQ, 0);
  1821. tt_int_op(relay_side->padding_info[0]->current_state,
  1822. OP_EQ, CIRCPAD_STATE_GAP);
  1823. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1824. OP_EQ, 0);
  1825. /* Verify we can't schedule padding in END state */
  1826. circpad_decision_t ret =
  1827. circpad_machine_schedule_padding(client_side->padding_info[0]);
  1828. tt_int_op(ret, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  1829. /* Simulate application traffic */
  1830. circpad_cell_event_nonpadding_sent(client_side);
  1831. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
  1832. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
  1833. circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
  1834. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1835. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1836. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1837. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1838. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1839. tt_int_op(n_client_cells, OP_EQ, 6);
  1840. tt_int_op(n_relay_cells, OP_EQ, 7);
  1841. // Test timer cancellation
  1842. simulate_single_hop_extend(client_side, relay_side, 1);
  1843. simulate_single_hop_extend(client_side, relay_side, 1);
  1844. timers_advance_and_run(5000);
  1845. circpad_cell_event_padding_received(client_side);
  1846. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1847. CIRCPAD_STATE_BURST);
  1848. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1849. CIRCPAD_STATE_GAP);
  1850. tt_int_op(n_client_cells, OP_EQ, 8);
  1851. tt_int_op(n_relay_cells, OP_EQ, 8);
  1852. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1853. OP_NE, 0);
  1854. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1855. OP_NE, 0);
  1856. /* Test timer cancel due to state rules */
  1857. circpad_cell_event_nonpadding_sent(client_side);
  1858. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1859. OP_EQ, 0);
  1860. circpad_cell_event_padding_received(client_side);
  1861. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1862. OP_NE, 0);
  1863. /* Simulate application traffic to cancel timer */
  1864. circpad_cell_event_nonpadding_sent(client_side);
  1865. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_OUT);
  1866. circpad_deliver_unrecognized_cell_events(relay_side, CELL_DIRECTION_IN);
  1867. circpad_deliver_recognized_relay_cell_events(client_side, RELAY_COMMAND_DATA,
  1868. TO_ORIGIN_CIRCUIT(client_side)->cpath->next);
  1869. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  1870. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  1871. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  1872. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  1873. /* No cells sent, except negotiate end from relay */
  1874. tt_int_op(n_client_cells, OP_EQ, 8);
  1875. tt_int_op(n_relay_cells, OP_EQ, 9);
  1876. /* Test mark for close and free */
  1877. simulate_single_hop_extend(client_side, relay_side, 1);
  1878. simulate_single_hop_extend(client_side, relay_side, 1);
  1879. timers_advance_and_run(5000);
  1880. circpad_cell_event_padding_received(client_side);
  1881. tt_int_op(n_client_cells, OP_EQ, 10);
  1882. tt_int_op(n_relay_cells, OP_EQ, 10);
  1883. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  1884. CIRCPAD_STATE_BURST);
  1885. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  1886. CIRCPAD_STATE_GAP);
  1887. tt_u64_op(client_side->padding_info[0]->padding_scheduled_at_usec,
  1888. OP_NE, 0);
  1889. tt_u64_op(relay_side->padding_info[0]->padding_scheduled_at_usec,
  1890. OP_NE, 0);
  1891. circuit_mark_for_close(client_side, END_CIRC_REASON_FLAG_REMOTE);
  1892. free_fake_orcirc(relay_side);
  1893. timers_advance_and_run(5000);
  1894. /* No cells sent */
  1895. tt_int_op(n_client_cells, OP_EQ, 10);
  1896. tt_int_op(n_relay_cells, OP_EQ, 10);
  1897. done:
  1898. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  1899. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  1900. circuitmux_free(dummy_channel.cmux);
  1901. timers_shutdown();
  1902. monotime_disable_test_mocking();
  1903. UNMOCK(circuit_package_relay_cell);
  1904. UNMOCK(circuitmux_attach_circuit);
  1905. return;
  1906. }
  1907. #endif
  1908. /** Helper function: Initializes a padding machine where every state uses the
  1909. * uniform probability distribution. */
  1910. static void
  1911. helper_circpad_circ_distribution_machine_setup(int min, int max)
  1912. {
  1913. circpad_machine_states_init(&circ_client_machine, 7);
  1914. circpad_state_t *zero_st = &circ_client_machine.states[0];
  1915. zero_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 1;
  1916. zero_st->iat_dist.type = CIRCPAD_DIST_UNIFORM;
  1917. /* param2 is upper bound, param1 is lower */
  1918. zero_st->iat_dist.param1 = min;
  1919. zero_st->iat_dist.param2 = max;
  1920. zero_st->dist_added_shift_usec = min;
  1921. zero_st->dist_max_sample_usec = max;
  1922. circpad_state_t *first_st = &circ_client_machine.states[1];
  1923. first_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 2;
  1924. first_st->iat_dist.type = CIRCPAD_DIST_LOGISTIC;
  1925. /* param1 is Mu, param2 is sigma. */
  1926. first_st->iat_dist.param1 = 9;
  1927. first_st->iat_dist.param2 = 3;
  1928. first_st->dist_added_shift_usec = min;
  1929. first_st->dist_max_sample_usec = max;
  1930. circpad_state_t *second_st = &circ_client_machine.states[2];
  1931. second_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 3;
  1932. second_st->iat_dist.type = CIRCPAD_DIST_LOG_LOGISTIC;
  1933. /* param1 is Alpha, param2 is 1.0/Beta */
  1934. second_st->iat_dist.param1 = 1;
  1935. second_st->iat_dist.param2 = 0.5;
  1936. second_st->dist_added_shift_usec = min;
  1937. second_st->dist_max_sample_usec = max;
  1938. circpad_state_t *third_st = &circ_client_machine.states[3];
  1939. third_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 4;
  1940. third_st->iat_dist.type = CIRCPAD_DIST_GEOMETRIC;
  1941. /* param1 is 'p' (success probability) */
  1942. third_st->iat_dist.param1 = 0.2;
  1943. third_st->dist_added_shift_usec = min;
  1944. third_st->dist_max_sample_usec = max;
  1945. circpad_state_t *fourth_st = &circ_client_machine.states[4];
  1946. fourth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 5;
  1947. fourth_st->iat_dist.type = CIRCPAD_DIST_WEIBULL;
  1948. /* param1 is k, param2 is Lambda */
  1949. fourth_st->iat_dist.param1 = 1.5;
  1950. fourth_st->iat_dist.param2 = 1;
  1951. fourth_st->dist_added_shift_usec = min;
  1952. fourth_st->dist_max_sample_usec = max;
  1953. circpad_state_t *fifth_st = &circ_client_machine.states[5];
  1954. fifth_st->next_state[CIRCPAD_EVENT_NONPADDING_RECV] = 6;
  1955. fifth_st->iat_dist.type = CIRCPAD_DIST_PARETO;
  1956. /* param1 is sigma, param2 is xi */
  1957. fifth_st->iat_dist.param1 = 1;
  1958. fifth_st->iat_dist.param2 = 5;
  1959. fifth_st->dist_added_shift_usec = min;
  1960. fifth_st->dist_max_sample_usec = max;
  1961. }
  1962. /** Simple test that the padding delays sampled from a uniform distribution
  1963. * actually faill within the uniform distribution range. */
  1964. static void
  1965. test_circuitpadding_sample_distribution(void *arg)
  1966. {
  1967. circpad_machine_runtime_t *mi;
  1968. int n_samples;
  1969. int n_states;
  1970. (void) arg;
  1971. /* mock this function so that we dont actually schedule any padding */
  1972. MOCK(circpad_machine_schedule_padding,
  1973. circpad_machine_schedule_padding_mock);
  1974. testing_enable_reproducible_rng();
  1975. /* Initialize a machine with multiple probability distributions */
  1976. circpad_machines_init();
  1977. helper_circpad_circ_distribution_machine_setup(0, 10);
  1978. /* Initialize machine and circuits */
  1979. client_side = TO_CIRCUIT(origin_circuit_new());
  1980. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1981. client_side->padding_machine[0] = &circ_client_machine;
  1982. client_side->padding_info[0] =
  1983. circpad_circuit_machineinfo_new(client_side, 0);
  1984. mi = client_side->padding_info[0];
  1985. /* For every state, sample a bunch of values from the distribution and ensure
  1986. * they fall within range. */
  1987. for (n_states = 0 ; n_states < 6; n_states++) {
  1988. /* Make sure we in the right state */
  1989. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ, n_states);
  1990. for (n_samples = 0; n_samples < 100; n_samples++) {
  1991. circpad_delay_t delay = circpad_machine_sample_delay(mi);
  1992. tt_int_op(delay, OP_GE, 0);
  1993. tt_int_op(delay, OP_LE, 10);
  1994. }
  1995. /* send a non-padding cell to move to the next machine state */
  1996. circpad_cell_event_nonpadding_received(client_side);
  1997. }
  1998. done:
  1999. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  2000. UNMOCK(circpad_machine_schedule_padding);
  2001. testing_disable_reproducible_rng();
  2002. }
  2003. static circpad_decision_t
  2004. circpad_machine_spec_transition_mock(circpad_machine_runtime_t *mi,
  2005. circpad_event_t event)
  2006. {
  2007. (void) mi;
  2008. (void) event;
  2009. return CIRCPAD_STATE_UNCHANGED;
  2010. }
  2011. /* Test per-machine padding rate limits */
  2012. static void
  2013. test_circuitpadding_machine_rate_limiting(void *arg)
  2014. {
  2015. (void) arg;
  2016. bool retval;
  2017. circpad_machine_runtime_t *mi;
  2018. int i;
  2019. /* Ignore machine transitions for the purposes of this function, we only
  2020. * really care about padding counts */
  2021. MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
  2022. MOCK(circpad_send_command_to_hop, circpad_send_command_to_hop_mock);
  2023. testing_enable_reproducible_rng();
  2024. /* Setup machine and circuits */
  2025. client_side = TO_CIRCUIT(origin_circuit_new());
  2026. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2027. helper_create_basic_machine();
  2028. client_side->padding_machine[0] = &circ_client_machine;
  2029. client_side->padding_info[0] =
  2030. circpad_circuit_machineinfo_new(client_side, 0);
  2031. mi = client_side->padding_info[0];
  2032. /* Set up the machine info so that we can get through the basic functions */
  2033. mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
  2034. /* First we are going to test the per-machine rate limits */
  2035. circ_client_machine.max_padding_percent = 50;
  2036. circ_client_machine.allowed_padding_count = 100;
  2037. /* Check padding limit, should be fine since we haven't sent anything yet. */
  2038. retval = circpad_machine_reached_padding_limit(mi);
  2039. tt_int_op(retval, OP_EQ, 0);
  2040. /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
  2041. * the rate limit will not trigger */
  2042. for (i=0;i<99;i++) {
  2043. circpad_send_padding_cell_for_callback(mi);
  2044. }
  2045. retval = circpad_machine_reached_padding_limit(mi);
  2046. tt_int_op(retval, OP_EQ, 0);
  2047. /* Now send another padding cell to pass circpad_global_allowed_cells=100,
  2048. and see that the limit will trigger */
  2049. circpad_send_padding_cell_for_callback(mi);
  2050. retval = circpad_machine_reached_padding_limit(mi);
  2051. tt_int_op(retval, OP_EQ, 1);
  2052. retval = circpad_machine_schedule_padding(mi);
  2053. tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  2054. /* Cover wrap */
  2055. for (;i<UINT16_MAX;i++) {
  2056. circpad_send_padding_cell_for_callback(mi);
  2057. }
  2058. tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/2+1);
  2059. tt_ptr_op(client_side->padding_info[0], OP_EQ, mi);
  2060. for (i=0;i<UINT16_MAX;i++) {
  2061. circpad_cell_event_nonpadding_sent(client_side);
  2062. }
  2063. tt_int_op(mi->nonpadding_sent, OP_EQ, UINT16_MAX/2);
  2064. tt_int_op(mi->padding_sent, OP_EQ, UINT16_MAX/4+1);
  2065. done:
  2066. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  2067. testing_disable_reproducible_rng();
  2068. }
  2069. /* Test global padding rate limits */
  2070. static void
  2071. test_circuitpadding_global_rate_limiting(void *arg)
  2072. {
  2073. (void) arg;
  2074. bool retval;
  2075. circpad_machine_runtime_t *mi;
  2076. int i;
  2077. int64_t actual_mocked_monotime_start;
  2078. /* Ignore machine transitions for the purposes of this function, we only
  2079. * really care about padding counts */
  2080. MOCK(circpad_machine_spec_transition, circpad_machine_spec_transition_mock);
  2081. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  2082. MOCK(circuit_package_relay_cell,
  2083. circuit_package_relay_cell_mock);
  2084. MOCK(monotime_absolute_usec, mock_monotime_absolute_usec);
  2085. testing_enable_reproducible_rng();
  2086. monotime_init();
  2087. monotime_enable_test_mocking();
  2088. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  2089. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  2090. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  2091. curr_mocked_time = actual_mocked_monotime_start;
  2092. timers_initialize();
  2093. client_side = TO_CIRCUIT(origin_circuit_new());
  2094. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2095. dummy_channel.cmux = circuitmux_alloc();
  2096. /* Setup machine and circuits */
  2097. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  2098. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  2099. helper_create_basic_machine();
  2100. relay_side->padding_machine[0] = &circ_client_machine;
  2101. relay_side->padding_info[0] =
  2102. circpad_circuit_machineinfo_new(relay_side, 0);
  2103. mi = relay_side->padding_info[0];
  2104. /* Set up the machine info so that we can get through the basic functions */
  2105. mi->state_length = CIRCPAD_STATE_LENGTH_INFINITE;
  2106. simulate_single_hop_extend(client_side, relay_side, 1);
  2107. simulate_single_hop_extend(client_side, relay_side, 1);
  2108. /* Now test the global limits by setting up the consensus */
  2109. networkstatus_t vote1;
  2110. vote1.net_params = smartlist_new();
  2111. smartlist_split_string(vote1.net_params,
  2112. "circpad_global_allowed_cells=100 circpad_global_max_padding_pct=50",
  2113. NULL, 0, 0);
  2114. /* Register global limits with the padding subsystem */
  2115. circpad_new_consensus_params(&vote1);
  2116. /* Check padding limit, should be fine since we haven't sent anything yet. */
  2117. retval = circpad_machine_reached_padding_limit(mi);
  2118. tt_int_op(retval, OP_EQ, 0);
  2119. /* Send 99 padding cells which is below circpad_global_allowed_cells=100, so
  2120. * the rate limit will not trigger */
  2121. for (i=0;i<99;i++) {
  2122. circpad_send_padding_cell_for_callback(mi);
  2123. }
  2124. retval = circpad_machine_reached_padding_limit(mi);
  2125. tt_int_op(retval, OP_EQ, 0);
  2126. /* Now send another padding cell to pass circpad_global_allowed_cells=100,
  2127. and see that the limit will trigger */
  2128. circpad_send_padding_cell_for_callback(mi);
  2129. retval = circpad_machine_reached_padding_limit(mi);
  2130. tt_int_op(retval, OP_EQ, 1);
  2131. retval = circpad_machine_schedule_padding(mi);
  2132. tt_int_op(retval, OP_EQ, CIRCPAD_STATE_UNCHANGED);
  2133. /* Now send 92 non-padding cells to get near the
  2134. * circpad_global_max_padding_pct=50 limit; in particular with 96 non-padding
  2135. * cells, the padding traffic is still 51% of total traffic so limit should
  2136. * trigger */
  2137. for (i=0;i<92;i++) {
  2138. circpad_cell_event_nonpadding_sent(relay_side);
  2139. }
  2140. retval = circpad_machine_reached_padding_limit(mi);
  2141. tt_int_op(retval, OP_EQ, 1);
  2142. /* Send another non-padding cell to bring the padding traffic to 50% of total
  2143. * traffic and get past the limit */
  2144. circpad_cell_event_nonpadding_sent(relay_side);
  2145. retval = circpad_machine_reached_padding_limit(mi);
  2146. tt_int_op(retval, OP_EQ, 0);
  2147. done:
  2148. free_fake_orcirc(relay_side);
  2149. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  2150. circuitmux_free(dummy_channel.cmux);
  2151. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  2152. smartlist_free(vote1.net_params);
  2153. testing_disable_reproducible_rng();
  2154. }
  2155. /* Test reduced and disabled padding */
  2156. static void
  2157. test_circuitpadding_reduce_disable(void *arg)
  2158. {
  2159. (void) arg;
  2160. int64_t actual_mocked_monotime_start;
  2161. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  2162. testing_enable_reproducible_rng();
  2163. nodes_init();
  2164. dummy_channel.cmux = circuitmux_alloc();
  2165. relay_side = (circuit_t *)new_fake_orcirc(&dummy_channel,
  2166. &dummy_channel);
  2167. client_side = (circuit_t *)origin_circuit_new();
  2168. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  2169. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2170. circpad_machines_init();
  2171. helper_create_conditional_machines();
  2172. monotime_init();
  2173. monotime_enable_test_mocking();
  2174. actual_mocked_monotime_start = MONOTIME_MOCK_START;
  2175. monotime_set_mock_time_nsec(actual_mocked_monotime_start);
  2176. monotime_coarse_set_mock_time_nsec(actual_mocked_monotime_start);
  2177. curr_mocked_time = actual_mocked_monotime_start;
  2178. timers_initialize();
  2179. /* This is needed so that we are not considered to be dormant */
  2180. note_user_activity(20);
  2181. MOCK(circuit_package_relay_cell,
  2182. circuit_package_relay_cell_mock);
  2183. MOCK(node_get_by_id,
  2184. node_get_by_id_mock);
  2185. /* Simulate extend. This should result in the original machine getting
  2186. * added, since the circuit is not built */
  2187. simulate_single_hop_extend(client_side, relay_side, 1);
  2188. simulate_single_hop_extend(client_side, relay_side, 1);
  2189. /* Verify that machine #2 is added */
  2190. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  2191. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  2192. /* Deliver a padding cell to the client, to trigger burst state */
  2193. circpad_cell_event_padding_sent(client_side);
  2194. /* This should have trigger length shutdown condition on client.. */
  2195. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  2196. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  2197. /* Verify machine is gone from both sides */
  2198. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  2199. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  2200. /* Now test the reduced padding machine by setting up the consensus */
  2201. networkstatus_t vote1;
  2202. vote1.net_params = smartlist_new();
  2203. smartlist_split_string(vote1.net_params,
  2204. "circpad_padding_reduced=1", NULL, 0, 0);
  2205. /* Register reduced padding machine with the padding subsystem */
  2206. circpad_new_consensus_params(&vote1);
  2207. simulate_single_hop_extend(client_side, relay_side, 1);
  2208. /* Verify that machine #0 is added */
  2209. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  2210. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  2211. tt_int_op(
  2212. circpad_machine_reached_padding_limit(client_side->padding_info[0]),
  2213. OP_EQ, 0);
  2214. tt_int_op(
  2215. circpad_machine_reached_padding_limit(relay_side->padding_info[0]),
  2216. OP_EQ, 0);
  2217. /* Test that machines get torn down when padding is disabled */
  2218. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  2219. smartlist_free(vote1.net_params);
  2220. vote1.net_params = smartlist_new();
  2221. smartlist_split_string(vote1.net_params,
  2222. "circpad_padding_disabled=1", NULL, 0, 0);
  2223. /* Register reduced padding machine with the padding subsystem */
  2224. circpad_new_consensus_params(&vote1);
  2225. tt_int_op(
  2226. circpad_machine_schedule_padding(client_side->padding_info[0]),
  2227. OP_EQ, CIRCPAD_STATE_UNCHANGED);
  2228. tt_int_op(
  2229. circpad_machine_schedule_padding(relay_side->padding_info[0]),
  2230. OP_EQ, CIRCPAD_STATE_UNCHANGED);
  2231. /* Signal that circuit is built: this event causes us to re-evaluate
  2232. * machine conditions (which don't apply because padding is disabled). */
  2233. circpad_machine_event_circ_built(TO_ORIGIN_CIRCUIT(client_side));
  2234. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  2235. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  2236. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  2237. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  2238. SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
  2239. smartlist_free(vote1.net_params);
  2240. vote1.net_params = NULL;
  2241. circpad_new_consensus_params(&vote1);
  2242. get_options_mutable()->ReducedCircuitPadding = 1;
  2243. simulate_single_hop_extend(client_side, relay_side, 1);
  2244. /* Verify that machine #0 is added */
  2245. tt_int_op(client_side->padding_machine[0]->machine_num, OP_EQ, 2);
  2246. tt_int_op(relay_side->padding_machine[0]->machine_num, OP_EQ, 2);
  2247. tt_int_op(
  2248. circpad_machine_reached_padding_limit(client_side->padding_info[0]),
  2249. OP_EQ, 0);
  2250. tt_int_op(
  2251. circpad_machine_reached_padding_limit(relay_side->padding_info[0]),
  2252. OP_EQ, 0);
  2253. get_options_mutable()->CircuitPadding = 0;
  2254. tt_int_op(
  2255. circpad_machine_schedule_padding(client_side->padding_info[0]),
  2256. OP_EQ, CIRCPAD_STATE_UNCHANGED);
  2257. tt_int_op(
  2258. circpad_machine_schedule_padding(relay_side->padding_info[0]),
  2259. OP_EQ, CIRCPAD_STATE_UNCHANGED);
  2260. /* Signal that circuit is built: this event causes us to re-evaluate
  2261. * machine conditions (which don't apply because padding is disabled). */
  2262. circpad_machine_event_circ_built(TO_ORIGIN_CIRCUIT(client_side));
  2263. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  2264. tt_ptr_op(client_side->padding_machine[0], OP_EQ, NULL);
  2265. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  2266. tt_ptr_op(relay_side->padding_machine[0], OP_EQ, NULL);
  2267. done:
  2268. free_fake_orcirc(relay_side);
  2269. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  2270. circuitmux_free(dummy_channel.cmux);
  2271. testing_disable_reproducible_rng();
  2272. }
  2273. /** Just a basic machine whose whole purpose is to reach the END state */
  2274. static void
  2275. helper_create_ender_machine(void)
  2276. {
  2277. /* Start, burst */
  2278. circpad_machine_states_init(&circ_client_machine, 2);
  2279. circ_client_machine.states[CIRCPAD_STATE_START].
  2280. next_state[CIRCPAD_EVENT_NONPADDING_RECV] = CIRCPAD_STATE_END;
  2281. circ_client_machine.conditions.state_mask = CIRCPAD_STATE_ALL;
  2282. circ_client_machine.conditions.purpose_mask = CIRCPAD_PURPOSE_ALL;
  2283. }
  2284. static time_t mocked_timeofday;
  2285. /** Set timeval to a mock date and time. This is necessary
  2286. * to make tor_gettimeofday() mockable. */
  2287. static void
  2288. mock_tor_gettimeofday(struct timeval *timeval)
  2289. {
  2290. timeval->tv_sec = mocked_timeofday;
  2291. timeval->tv_usec = 0;
  2292. }
  2293. /** Test manual managing of circuit lifetimes by the circuitpadding
  2294. * subsystem. In particular this test goes through all the cases of the
  2295. * circpad_marked_circuit_for_padding() function, via
  2296. * circuit_mark_for_close() as well as
  2297. * circuit_expire_old_circuits_clientside(). */
  2298. static void
  2299. test_circuitpadding_manage_circuit_lifetime(void *arg)
  2300. {
  2301. circpad_machine_runtime_t *mi;
  2302. (void) arg;
  2303. client_side = (circuit_t *)origin_circuit_new();
  2304. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2305. monotime_enable_test_mocking();
  2306. MOCK(tor_gettimeofday, mock_tor_gettimeofday);
  2307. mocked_timeofday = 23;
  2308. helper_create_ender_machine();
  2309. /* Enable manual circuit lifetime manage for this test */
  2310. circ_client_machine.manage_circ_lifetime = 1;
  2311. /* Test setup */
  2312. client_side->padding_machine[0] = &circ_client_machine;
  2313. client_side->padding_info[0] =
  2314. circpad_circuit_machineinfo_new(client_side, 0);
  2315. mi = client_side->padding_info[0];
  2316. tt_int_op(mi->current_state, OP_EQ, CIRCPAD_STATE_START);
  2317. /* Check that the circuit is not marked for close */
  2318. tt_int_op(client_side->marked_for_close, OP_EQ, 0);
  2319. tt_int_op(client_side->purpose, OP_EQ, CIRCUIT_PURPOSE_C_GENERAL);
  2320. /* Mark this circuit for close due to a remote reason */
  2321. circuit_mark_for_close(client_side,
  2322. END_CIRC_REASON_FLAG_REMOTE|END_CIRC_REASON_NONE);
  2323. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  2324. tt_int_op(client_side->marked_for_close, OP_NE, 0);
  2325. tt_int_op(client_side->purpose, OP_EQ, CIRCUIT_PURPOSE_C_GENERAL);
  2326. client_side->marked_for_close = 0;
  2327. /* Mark this circuit for close due to a protocol issue */
  2328. circuit_mark_for_close(client_side, END_CIRC_REASON_TORPROTOCOL);
  2329. tt_int_op(client_side->marked_for_close, OP_NE, 0);
  2330. tt_int_op(client_side->purpose, OP_EQ, CIRCUIT_PURPOSE_C_GENERAL);
  2331. client_side->marked_for_close = 0;
  2332. /* Mark a measurement circuit for close */
  2333. client_side->purpose = CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT;
  2334. circuit_mark_for_close(client_side, END_CIRC_REASON_NONE);
  2335. tt_int_op(client_side->marked_for_close, OP_NE, 0);
  2336. tt_int_op(client_side->purpose, OP_EQ, CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT);
  2337. client_side->marked_for_close = 0;
  2338. /* Mark a general circuit for close */
  2339. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2340. circuit_mark_for_close(client_side, END_CIRC_REASON_NONE);
  2341. /* Check that this circuit is still not marked for close since we are
  2342. * managing the lifetime manually, but the circuit was tagged as such by the
  2343. * circpadding subsystem */
  2344. tt_int_op(client_side->marked_for_close, OP_EQ, 0);
  2345. tt_int_op(client_side->purpose, OP_EQ, CIRCUIT_PURPOSE_C_CIRCUIT_PADDING);
  2346. /* We just tested case (1) from the comments of
  2347. * circpad_circuit_should_be_marked_for_close() */
  2348. /* Transition the machine to the END state but did not delete its machine */
  2349. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  2350. circpad_cell_event_nonpadding_received(client_side);
  2351. tt_int_op(mi->current_state, OP_EQ, CIRCPAD_STATE_END);
  2352. /* We just tested case (3) from the comments of
  2353. * circpad_circuit_should_be_marked_for_close().
  2354. * Now let's go for case (2). */
  2355. /* Reset the close mark */
  2356. client_side->marked_for_close = 0;
  2357. /* Mark this circuit for close */
  2358. circuit_mark_for_close(client_side, 0);
  2359. /* See that the circ got closed since we are already in END state */
  2360. tt_int_op(client_side->marked_for_close, OP_NE, 0);
  2361. /* We just tested case (2). Now let's see that case (4) is unreachable as
  2362. that comment claims */
  2363. /* First, reset all close marks and tags */
  2364. client_side->marked_for_close = 0;
  2365. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2366. /* Now re-create the ender machine so that we can transition to END again */
  2367. /* Free up some stuff first */
  2368. circpad_circuit_free_all_machineinfos(client_side);
  2369. tor_free(circ_client_machine.states);
  2370. helper_create_ender_machine();
  2371. client_side->padding_machine[0] = &circ_client_machine;
  2372. client_side->padding_info[0] =
  2373. circpad_circuit_machineinfo_new(client_side, 0);
  2374. mi = client_side->padding_info[0];
  2375. /* Check we are in START. */
  2376. tt_int_op(mi->current_state, OP_EQ, CIRCPAD_STATE_START);
  2377. /* Test that we don't expire this circuit yet */
  2378. client_side->timestamp_dirty = 0;
  2379. client_side->state = CIRCUIT_STATE_OPEN;
  2380. tor_gettimeofday(&client_side->timestamp_began);
  2381. TO_ORIGIN_CIRCUIT(client_side)->circuit_idle_timeout = 23;
  2382. mocked_timeofday += 24;
  2383. circuit_expire_old_circuits_clientside();
  2384. circuit_expire_old_circuits_clientside();
  2385. circuit_expire_old_circuits_clientside();
  2386. tt_int_op(client_side->timestamp_dirty, OP_NE, 0);
  2387. tt_int_op(client_side->marked_for_close, OP_EQ, 0);
  2388. tt_int_op(client_side->purpose, OP_EQ, CIRCUIT_PURPOSE_C_CIRCUIT_PADDING);
  2389. /* Runaway circpad test: if the machine does not transition to end,
  2390. * test that after CIRCPAD_DELAY_MAX_SECS, we get marked anyway */
  2391. mocked_timeofday = client_side->timestamp_dirty
  2392. + get_options()->MaxCircuitDirtiness + 2;
  2393. client_side->padding_info[0]->last_cell_time_sec =
  2394. approx_time()-(CIRCPAD_DELAY_MAX_SECS+10);
  2395. circuit_expire_old_circuits_clientside();
  2396. tt_int_op(client_side->marked_for_close, OP_NE, 0);
  2397. /* Test back to normal: if we had activity, we won't close */
  2398. client_side->padding_info[0]->last_cell_time_sec = approx_time();
  2399. client_side->marked_for_close = 0;
  2400. circuit_expire_old_circuits_clientside();
  2401. tt_int_op(client_side->marked_for_close, OP_EQ, 0);
  2402. /* Transition to END, but before we're past the dirty timer */
  2403. mocked_timeofday = client_side->timestamp_dirty;
  2404. circpad_cell_event_nonpadding_received(client_side);
  2405. tt_int_op(mi->current_state, OP_EQ, CIRCPAD_STATE_END);
  2406. /* Verify that the circuit was not closed. */
  2407. tt_int_op(client_side->marked_for_close, OP_EQ, 0);
  2408. /* Now that we are in END state, we can be closed by expiry, but via
  2409. * the timestamp_dirty path, not the idle path. So first test not dirty
  2410. * enough. */
  2411. mocked_timeofday = client_side->timestamp_dirty;
  2412. circuit_expire_old_circuits_clientside();
  2413. tt_int_op(client_side->marked_for_close, OP_EQ, 0);
  2414. mocked_timeofday = client_side->timestamp_dirty
  2415. + get_options()->MaxCircuitDirtiness + 2;
  2416. circuit_expire_old_circuits_clientside();
  2417. tt_int_op(client_side->marked_for_close, OP_NE, 0);
  2418. done:
  2419. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  2420. tor_free(circ_client_machine.states);
  2421. monotime_disable_test_mocking();
  2422. UNMOCK(tor_gettimeofday);
  2423. }
  2424. /** Helper for the test_circuitpadding_hs_machines test:
  2425. *
  2426. * - Create a client and relay circuit.
  2427. * - Setup right circuit purpose and attach a machine to the client circuit.
  2428. * - Verify that state transitions work as intended and state length gets
  2429. * enforced.
  2430. *
  2431. * This function is able to do this test both for intro and rend circuits
  2432. * depending on the value of <b>test_intro_circs</b>.
  2433. */
  2434. static void
  2435. helper_test_hs_machines(bool test_intro_circs)
  2436. {
  2437. /* Setup the circuits */
  2438. origin_circuit_t *origin_client_side = origin_circuit_new();
  2439. client_side = TO_CIRCUIT(origin_client_side);
  2440. client_side->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2441. dummy_channel.cmux = circuitmux_alloc();
  2442. relay_side = TO_CIRCUIT(new_fake_orcirc(&dummy_channel, &dummy_channel));
  2443. relay_side->purpose = CIRCUIT_PURPOSE_OR;
  2444. /* extend the client circ to two hops */
  2445. simulate_single_hop_extend(client_side, relay_side, 1);
  2446. simulate_single_hop_extend(client_side, relay_side, 1);
  2447. /* machines only apply on opened circuits */
  2448. origin_client_side->has_opened = 1;
  2449. /************************************/
  2450. /* Attaching the client machine now won't work here because of a wrong
  2451. * purpose */
  2452. tt_assert(!client_side->padding_machine[0]);
  2453. circpad_add_matching_machines(origin_client_side, origin_padding_machines);
  2454. tt_assert(!client_side->padding_machine[0]);
  2455. /* Change the purpose, see the machine getting attached */
  2456. client_side->purpose = test_intro_circs ?
  2457. CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT : CIRCUIT_PURPOSE_C_REND_JOINED;
  2458. circpad_add_matching_machines(origin_client_side, origin_padding_machines);
  2459. tt_ptr_op(client_side->padding_info[0], OP_NE, NULL);
  2460. tt_ptr_op(client_side->padding_machine[0], OP_NE, NULL);
  2461. tt_ptr_op(relay_side->padding_info[0], OP_NE, NULL);
  2462. tt_ptr_op(relay_side->padding_machine[0], OP_NE, NULL);
  2463. /* Verify that the right machine is attached */
  2464. tt_str_op(client_side->padding_machine[0]->name, OP_EQ,
  2465. test_intro_circs ? "client_ip_circ" : "client_rp_circ");
  2466. tt_str_op(relay_side->padding_machine[0]->name, OP_EQ,
  2467. test_intro_circs ? "relay_ip_circ": "relay_rp_circ");
  2468. /***********************************/
  2469. /* Intro machines are at START state, but rend machines have already skipped
  2470. * to OBFUSCATE_CIRC_SETUP because of the sent PADDING_NEGOTIATE. */
  2471. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  2472. CIRCPAD_STATE_OBFUSCATE_CIRC_SETUP);
  2473. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  2474. CIRCPAD_STATE_OBFUSCATE_CIRC_SETUP);
  2475. /*Send non-padding to move the machines from START to OBFUSCATE_CIRC_SETUP */
  2476. circpad_cell_event_nonpadding_received(client_side);
  2477. circpad_cell_event_nonpadding_received(relay_side);
  2478. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  2479. CIRCPAD_STATE_OBFUSCATE_CIRC_SETUP);
  2480. tt_int_op(relay_side->padding_info[0]->current_state, OP_EQ,
  2481. CIRCPAD_STATE_OBFUSCATE_CIRC_SETUP);
  2482. /* Check that the state lengths have been sampled and are within range */
  2483. circpad_machine_runtime_t *client_machine_runtime =
  2484. client_side->padding_info[0];
  2485. circpad_machine_runtime_t *relay_machine_runtime =
  2486. relay_side->padding_info[0];
  2487. if (test_intro_circs) {
  2488. /* on the client side, we don't send any padding so
  2489. * state length is not set */
  2490. tt_i64_op(client_machine_runtime->state_length, OP_EQ, -1);
  2491. /* relay side has state limits. check them */
  2492. tt_i64_op(relay_machine_runtime->state_length, OP_GE,
  2493. INTRO_MACHINE_MINIMUM_PADDING);
  2494. tt_i64_op(relay_machine_runtime->state_length, OP_LT,
  2495. INTRO_MACHINE_MAXIMUM_PADDING);
  2496. } else {
  2497. tt_i64_op(client_machine_runtime->state_length, OP_EQ, 1);
  2498. tt_i64_op(relay_machine_runtime->state_length, OP_EQ, 1);
  2499. }
  2500. if (test_intro_circs) {
  2501. int i;
  2502. /* Send state_length worth of padding from the relay and see that the
  2503. * client state goes to END */
  2504. for (i = (int) relay_machine_runtime->state_length ; i > 0 ; i--) {
  2505. circpad_send_padding_cell_for_callback(relay_machine_runtime);
  2506. }
  2507. /* See that the machine has been teared down after all the length has been
  2508. * exhausted (the padding info should now be null on both sides) */
  2509. tt_ptr_op(relay_side->padding_info[0], OP_EQ, NULL);
  2510. tt_ptr_op(client_side->padding_info[0], OP_EQ, NULL);
  2511. } else {
  2512. int i;
  2513. /* Send state_length worth of padding and see that the state goes to END */
  2514. for (i = (int) client_machine_runtime->state_length ; i > 0 ; i--) {
  2515. circpad_send_padding_cell_for_callback(client_machine_runtime);
  2516. }
  2517. /* See that the machine has been teared down after all the length has been
  2518. * exhausted. */
  2519. tt_int_op(client_side->padding_info[0]->current_state, OP_EQ,
  2520. CIRCPAD_STATE_END);
  2521. }
  2522. done:
  2523. free_fake_orcirc(relay_side);
  2524. circuitmux_detach_all_circuits(dummy_channel.cmux, NULL);
  2525. circuitmux_free(dummy_channel.cmux);
  2526. free_fake_origin_circuit(TO_ORIGIN_CIRCUIT(client_side));
  2527. }
  2528. /** Test that the HS circuit padding machines work as intended. */
  2529. static void
  2530. test_circuitpadding_hs_machines(void *arg)
  2531. {
  2532. (void)arg;
  2533. /* Test logic:
  2534. *
  2535. * 1) Register the HS machines, which aim to hide the presense of
  2536. * onion service traffic on the client-side
  2537. *
  2538. * 2) Call helper_test_hs_machines() to perform tests for the intro circuit
  2539. * machines and for the rend circuit machines.
  2540. */
  2541. MOCK(circuitmux_attach_circuit, circuitmux_attach_circuit_mock);
  2542. MOCK(circuit_package_relay_cell, circuit_package_relay_cell_mock);
  2543. MOCK(circuit_get_nth_node, circuit_get_nth_node_mock);
  2544. MOCK(circpad_machine_schedule_padding,circpad_machine_schedule_padding_mock);
  2545. origin_padding_machines = smartlist_new();
  2546. relay_padding_machines = smartlist_new();
  2547. nodes_init();
  2548. monotime_init();
  2549. monotime_enable_test_mocking();
  2550. monotime_set_mock_time_nsec(1*TOR_NSEC_PER_USEC);
  2551. monotime_coarse_set_mock_time_nsec(1*TOR_NSEC_PER_USEC);
  2552. curr_mocked_time = 1*TOR_NSEC_PER_USEC;
  2553. timers_initialize();
  2554. /* This is needed so that we are not considered to be dormant */
  2555. note_user_activity(20);
  2556. /************************************/
  2557. /* Register the HS machines */
  2558. circpad_machine_client_hide_intro_circuits(origin_padding_machines);
  2559. circpad_machine_client_hide_rend_circuits(origin_padding_machines);
  2560. circpad_machine_relay_hide_intro_circuits(relay_padding_machines);
  2561. circpad_machine_relay_hide_rend_circuits(relay_padding_machines);
  2562. /***********************************/
  2563. /* Do the tests for the intro circuit machines */
  2564. helper_test_hs_machines(true);
  2565. /* Do the tests for the rend circuit machines */
  2566. helper_test_hs_machines(false);
  2567. timers_shutdown();
  2568. monotime_disable_test_mocking();
  2569. SMARTLIST_FOREACH_BEGIN(origin_padding_machines,
  2570. circpad_machine_spec_t *, m) {
  2571. machine_spec_free(m);
  2572. } SMARTLIST_FOREACH_END(m);
  2573. SMARTLIST_FOREACH_BEGIN(relay_padding_machines,
  2574. circpad_machine_spec_t *, m) {
  2575. machine_spec_free(m);
  2576. } SMARTLIST_FOREACH_END(m);
  2577. smartlist_free(origin_padding_machines);
  2578. smartlist_free(relay_padding_machines);
  2579. UNMOCK(circuitmux_attach_circuit);
  2580. UNMOCK(circuit_package_relay_cell);
  2581. UNMOCK(circuit_get_nth_node);
  2582. UNMOCK(circpad_machine_schedule_padding);
  2583. }
  2584. #define TEST_CIRCUITPADDING(name, flags) \
  2585. { #name, test_##name, (flags), NULL, NULL }
  2586. struct testcase_t circuitpadding_tests[] = {
  2587. TEST_CIRCUITPADDING(circuitpadding_tokens, TT_FORK),
  2588. TEST_CIRCUITPADDING(circuitpadding_state_length, TT_FORK),
  2589. TEST_CIRCUITPADDING(circuitpadding_negotiation, TT_FORK),
  2590. TEST_CIRCUITPADDING(circuitpadding_wronghop, TT_FORK),
  2591. /** Disabled unstable test until #29298 is implemented (see #29122) */
  2592. // TEST_CIRCUITPADDING(circuitpadding_circuitsetup_machine, TT_FORK),
  2593. TEST_CIRCUITPADDING(circuitpadding_conditions, TT_FORK),
  2594. TEST_CIRCUITPADDING(circuitpadding_rtt, TT_FORK),
  2595. TEST_CIRCUITPADDING(circuitpadding_sample_distribution, TT_FORK),
  2596. TEST_CIRCUITPADDING(circuitpadding_machine_rate_limiting, TT_FORK),
  2597. TEST_CIRCUITPADDING(circuitpadding_global_rate_limiting, TT_FORK),
  2598. TEST_CIRCUITPADDING(circuitpadding_reduce_disable, TT_FORK),
  2599. TEST_CIRCUITPADDING(circuitpadding_token_removal_lower, TT_FORK),
  2600. TEST_CIRCUITPADDING(circuitpadding_token_removal_higher, TT_FORK),
  2601. TEST_CIRCUITPADDING(circuitpadding_closest_token_removal, TT_FORK),
  2602. TEST_CIRCUITPADDING(circuitpadding_closest_token_removal_usec, TT_FORK),
  2603. TEST_CIRCUITPADDING(circuitpadding_token_removal_exact, TT_FORK),
  2604. TEST_CIRCUITPADDING(circuitpadding_manage_circuit_lifetime, TT_FORK),
  2605. TEST_CIRCUITPADDING(circuitpadding_hs_machines, TT_FORK),
  2606. END_OF_TESTCASES
  2607. };