test_circuitpadding.c 84 KB

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