test_relaycell.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /* Copyright (c) 2014-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* Unit tests for handling different kinds of relay cell */
  4. #define RELAY_PRIVATE
  5. #define CIRCUITLIST_PRIVATE
  6. #include "core/or/or.h"
  7. #include "core/mainloop/main.h"
  8. #include "app/config/config.h"
  9. #include "core/mainloop/connection.h"
  10. #include "lib/crypt_ops/crypto.h"
  11. #include "lib/crypt_ops/crypto_rand.h"
  12. #include "core/or/circuitbuild.h"
  13. #include "core/or/circuitlist.h"
  14. #include "core/or/connection_edge.h"
  15. #include "core/or/relay.h"
  16. #include "test/test.h"
  17. #include "test/log_test_helpers.h"
  18. #include "core/or/cell_st.h"
  19. #include "core/or/crypt_path_st.h"
  20. #include "core/or/entry_connection_st.h"
  21. #include "core/or/origin_circuit_st.h"
  22. #include "core/or/socks_request_st.h"
  23. #include "core/or/half_edge_st.h"
  24. static int srm_ncalls;
  25. static entry_connection_t *srm_conn;
  26. static int srm_atype;
  27. static size_t srm_alen;
  28. static int srm_answer_is_set;
  29. static uint8_t srm_answer[512];
  30. static int srm_ttl;
  31. static time_t srm_expires;
  32. void connection_free_minimal(connection_t*);
  33. int connected_cell_format_payload(uint8_t *payload_out,
  34. const tor_addr_t *addr,
  35. uint32_t ttl);
  36. int pathbias_count_valid_cells(origin_circuit_t *circ,
  37. cell_t *cell);
  38. half_edge_t *connection_half_edge_find_stream_id(
  39. const smartlist_t *half_conns,
  40. streamid_t stream_id);
  41. void connection_half_edge_add(const edge_connection_t *conn,
  42. origin_circuit_t *circ);
  43. int mock_send_command(streamid_t stream_id, circuit_t *circ,
  44. uint8_t relay_command, const char *payload,
  45. size_t payload_len, crypt_path_t *cpath_layer,
  46. const char *filename, int lineno);
  47. /* Mock replacement for connection_ap_hannshake_socks_resolved() */
  48. static void
  49. socks_resolved_mock(entry_connection_t *conn,
  50. int answer_type,
  51. size_t answer_len,
  52. const uint8_t *answer,
  53. int ttl,
  54. time_t expires)
  55. {
  56. srm_ncalls++;
  57. srm_conn = conn;
  58. srm_atype = answer_type;
  59. srm_alen = answer_len;
  60. if (answer) {
  61. memset(srm_answer, 0, sizeof(srm_answer));
  62. memcpy(srm_answer, answer, answer_len < 512 ? answer_len : 512);
  63. srm_answer_is_set = 1;
  64. } else {
  65. srm_answer_is_set = 0;
  66. }
  67. srm_ttl = ttl;
  68. srm_expires = expires;
  69. }
  70. static int mum_ncalls;
  71. static entry_connection_t *mum_conn;
  72. static int mum_endreason;
  73. /* Mock replacement for connection_mark_unattached_ap_() */
  74. static void
  75. mark_unattached_mock(entry_connection_t *conn, int endreason,
  76. int line, const char *file)
  77. {
  78. ++mum_ncalls;
  79. mum_conn = conn;
  80. mum_endreason = endreason;
  81. (void) line;
  82. (void) file;
  83. }
  84. /* Helper: Return a newly allocated and initialized origin circuit with
  85. * purpose and flags. A default HS identifier is set to an ed25519
  86. * authentication key for introduction point. */
  87. static origin_circuit_t *
  88. helper_create_origin_circuit(int purpose, int flags)
  89. {
  90. origin_circuit_t *circ = NULL;
  91. circ = origin_circuit_init(purpose, flags);
  92. tor_assert(circ);
  93. circ->cpath = tor_malloc_zero(sizeof(crypt_path_t));
  94. circ->cpath->magic = CRYPT_PATH_MAGIC;
  95. circ->cpath->state = CPATH_STATE_OPEN;
  96. circ->cpath->package_window = circuit_initial_package_window();
  97. circ->cpath->deliver_window = CIRCWINDOW_START;
  98. circ->cpath->prev = circ->cpath;
  99. /* Create a default HS identifier. */
  100. circ->hs_ident = tor_malloc_zero(sizeof(hs_ident_circuit_t));
  101. return circ;
  102. }
  103. static void
  104. mock_connection_mark_unattached_ap_(entry_connection_t *conn, int endreason,
  105. int line, const char *file)
  106. {
  107. (void) line;
  108. (void) file;
  109. conn->edge_.end_reason = endreason;
  110. }
  111. static void
  112. mock_mark_circ_for_close(circuit_t *circ, int reason, int line,
  113. const char *file)
  114. {
  115. (void)reason; (void)line; (void)file;
  116. circ->marked_for_close = 1;
  117. return;
  118. }
  119. static void
  120. mock_mark_for_close(connection_t *conn,
  121. int line, const char *file)
  122. {
  123. (void)line;
  124. (void)file;
  125. conn->marked_for_close = 1;
  126. return;
  127. }
  128. static void
  129. mock_start_reading(connection_t *conn)
  130. {
  131. (void)conn;
  132. return;
  133. }
  134. int
  135. mock_send_command(streamid_t stream_id, circuit_t *circ,
  136. uint8_t relay_command, const char *payload,
  137. size_t payload_len, crypt_path_t *cpath_layer,
  138. const char *filename, int lineno)
  139. {
  140. (void)stream_id; (void)circ;
  141. (void)relay_command; (void)payload;
  142. (void)payload_len; (void)cpath_layer;
  143. (void)filename; (void)lineno;
  144. return 0;
  145. }
  146. static entry_connection_t *
  147. fake_entry_conn(origin_circuit_t *oncirc, streamid_t id)
  148. {
  149. edge_connection_t *edgeconn;
  150. entry_connection_t *entryconn;
  151. entryconn = entry_connection_new(CONN_TYPE_AP, AF_INET);
  152. edgeconn = ENTRY_TO_EDGE_CONN(entryconn);
  153. edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT;
  154. edgeconn->deliver_window = STREAMWINDOW_START;
  155. edgeconn->package_window = STREAMWINDOW_START;
  156. edgeconn->stream_id = id;
  157. edgeconn->on_circuit = TO_CIRCUIT(oncirc);
  158. edgeconn->cpath_layer = oncirc->cpath;
  159. return entryconn;
  160. }
  161. #define PACK_CELL(id, cmd, body_s) do { \
  162. memset(&cell, 0, sizeof(cell)); \
  163. memset(&rh, 0, sizeof(rh)); \
  164. memcpy(cell.payload+RELAY_HEADER_SIZE, (body_s), sizeof((body_s))-1); \
  165. rh.length = sizeof((body_s))-1; \
  166. rh.command = (cmd); \
  167. rh.stream_id = (id); \
  168. relay_header_pack((uint8_t*)&cell.payload, &rh); \
  169. } while (0)
  170. #define ASSERT_COUNTED_BW() do { \
  171. tt_int_op(circ->n_delivered_read_circ_bw, OP_EQ, delivered+rh.length); \
  172. tt_int_op(circ->n_overhead_read_circ_bw, OP_EQ, \
  173. overhead+RELAY_PAYLOAD_SIZE-rh.length); \
  174. delivered = circ->n_delivered_read_circ_bw; \
  175. overhead = circ->n_overhead_read_circ_bw; \
  176. } while (0)
  177. #define ASSERT_UNCOUNTED_BW() do { \
  178. tt_int_op(circ->n_delivered_read_circ_bw, OP_EQ, delivered); \
  179. tt_int_op(circ->n_overhead_read_circ_bw, OP_EQ, overhead); \
  180. } while (0)
  181. static int
  182. subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
  183. {
  184. cell_t cell;
  185. relay_header_t rh;
  186. edge_connection_t *edgeconn;
  187. entry_connection_t *entryconn2=NULL;
  188. entry_connection_t *entryconn3=NULL;
  189. entry_connection_t *entryconn4=NULL;
  190. int delivered = circ->n_delivered_read_circ_bw;
  191. int overhead = circ->n_overhead_read_circ_bw;
  192. /* Make new entryconns */
  193. entryconn2 = fake_entry_conn(circ, init_id);
  194. entryconn2->socks_request->has_finished = 1;
  195. entryconn3 = fake_entry_conn(circ, init_id+1);
  196. entryconn3->socks_request->has_finished = 1;
  197. entryconn4 = fake_entry_conn(circ, init_id+2);
  198. entryconn4->socks_request->has_finished = 1;
  199. edgeconn = ENTRY_TO_EDGE_CONN(entryconn2);
  200. edgeconn->package_window = 23;
  201. edgeconn->base_.state = AP_CONN_STATE_OPEN;
  202. int data_cells = edgeconn->deliver_window;
  203. int sendme_cells = (STREAMWINDOW_START-edgeconn->package_window)
  204. /STREAMWINDOW_INCREMENT;
  205. ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
  206. ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
  207. connection_edge_reached_eof(edgeconn);
  208. /* Data cell not in the half-opened list */
  209. PACK_CELL(4000, RELAY_COMMAND_DATA, "Data1234");
  210. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  211. pathbias_count_valid_cells(circ, &cell);
  212. else
  213. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  214. circ->cpath);
  215. ASSERT_UNCOUNTED_BW();
  216. /* Sendme cell not in the half-opened list */
  217. PACK_CELL(4000, RELAY_COMMAND_SENDME, "Data1234");
  218. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  219. pathbias_count_valid_cells(circ, &cell);
  220. else
  221. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  222. circ->cpath);
  223. ASSERT_UNCOUNTED_BW();
  224. /* Connected cell not in the half-opened list */
  225. PACK_CELL(4000, RELAY_COMMAND_CONNECTED, "Data1234");
  226. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  227. pathbias_count_valid_cells(circ, &cell);
  228. else
  229. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  230. circ->cpath);
  231. ASSERT_UNCOUNTED_BW();
  232. /* Resolved cell not in the half-opened list */
  233. PACK_CELL(4000, RELAY_COMMAND_RESOLVED, "Data1234");
  234. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  235. pathbias_count_valid_cells(circ, &cell);
  236. else
  237. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  238. circ->cpath);
  239. ASSERT_UNCOUNTED_BW();
  240. /* Connected cell: not counted -- we were open */
  241. edgeconn = ENTRY_TO_EDGE_CONN(entryconn2);
  242. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_CONNECTED, "Data1234");
  243. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  244. pathbias_count_valid_cells(circ, &cell);
  245. else
  246. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  247. circ->cpath);
  248. ASSERT_UNCOUNTED_BW();
  249. /* DATA cells up to limit */
  250. while (data_cells > 0) {
  251. ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
  252. ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
  253. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
  254. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  255. pathbias_count_valid_cells(circ, &cell);
  256. else
  257. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  258. circ->cpath);
  259. ASSERT_COUNTED_BW();
  260. data_cells--;
  261. }
  262. ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
  263. ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
  264. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
  265. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  266. pathbias_count_valid_cells(circ, &cell);
  267. else
  268. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  269. circ->cpath);
  270. ASSERT_UNCOUNTED_BW();
  271. /* SENDME cells up to limit */
  272. while (sendme_cells > 0) {
  273. ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
  274. ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
  275. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
  276. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  277. pathbias_count_valid_cells(circ, &cell);
  278. else
  279. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  280. circ->cpath);
  281. ASSERT_COUNTED_BW();
  282. sendme_cells--;
  283. }
  284. ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
  285. ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
  286. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
  287. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  288. pathbias_count_valid_cells(circ, &cell);
  289. else
  290. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  291. circ->cpath);
  292. ASSERT_UNCOUNTED_BW();
  293. /* Only one END cell */
  294. ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
  295. ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
  296. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
  297. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  298. pathbias_count_valid_cells(circ, &cell);
  299. else
  300. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  301. circ->cpath);
  302. ASSERT_COUNTED_BW();
  303. ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
  304. ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
  305. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
  306. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  307. pathbias_count_valid_cells(circ, &cell);
  308. else
  309. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  310. circ->cpath);
  311. ASSERT_UNCOUNTED_BW();
  312. edgeconn = ENTRY_TO_EDGE_CONN(entryconn3);
  313. edgeconn->base_.state = AP_CONN_STATE_OPEN;
  314. ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
  315. ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
  316. /* sendme cell on open entryconn with full window */
  317. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
  318. int ret =
  319. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  320. circ->cpath);
  321. tt_int_op(ret, OP_EQ, -END_CIRC_REASON_TORPROTOCOL);
  322. ASSERT_UNCOUNTED_BW();
  323. /* connected cell on a after EOF */
  324. ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
  325. ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
  326. edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT;
  327. connection_edge_reached_eof(edgeconn);
  328. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_CONNECTED, "Data1234");
  329. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  330. pathbias_count_valid_cells(circ, &cell);
  331. else
  332. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  333. circ->cpath);
  334. ASSERT_COUNTED_BW();
  335. ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
  336. ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
  337. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_CONNECTED, "Data1234");
  338. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  339. pathbias_count_valid_cells(circ, &cell);
  340. else
  341. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  342. circ->cpath);
  343. ASSERT_UNCOUNTED_BW();
  344. /* DATA and SENDME after END cell */
  345. ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
  346. ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
  347. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
  348. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  349. pathbias_count_valid_cells(circ, &cell);
  350. else
  351. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  352. circ->cpath);
  353. ASSERT_COUNTED_BW();
  354. ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
  355. ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
  356. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
  357. ret =
  358. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  359. circ->cpath);
  360. tt_int_op(ret, OP_NE, -END_CIRC_REASON_TORPROTOCOL);
  361. ASSERT_UNCOUNTED_BW();
  362. ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
  363. ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
  364. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
  365. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  366. pathbias_count_valid_cells(circ, &cell);
  367. else
  368. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  369. circ->cpath);
  370. ASSERT_UNCOUNTED_BW();
  371. /* Resolved: 1 counted, more not */
  372. edgeconn = ENTRY_TO_EDGE_CONN(entryconn4);
  373. entryconn4->socks_request->command = SOCKS_COMMAND_RESOLVE;
  374. edgeconn->base_.state = AP_CONN_STATE_RESOLVE_WAIT;
  375. edgeconn->on_circuit = TO_CIRCUIT(circ);
  376. ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
  377. ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
  378. connection_edge_reached_eof(edgeconn);
  379. ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
  380. ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
  381. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_RESOLVED,
  382. "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
  383. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  384. pathbias_count_valid_cells(circ, &cell);
  385. else
  386. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  387. circ->cpath);
  388. ASSERT_COUNTED_BW();
  389. ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
  390. ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
  391. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_RESOLVED,
  392. "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
  393. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  394. circ->cpath);
  395. ASSERT_UNCOUNTED_BW();
  396. /* Data not counted after resolved */
  397. ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
  398. ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
  399. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
  400. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  401. pathbias_count_valid_cells(circ, &cell);
  402. else
  403. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  404. circ->cpath);
  405. ASSERT_UNCOUNTED_BW();
  406. /* End not counted after resolved */
  407. ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
  408. ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
  409. PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
  410. if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
  411. pathbias_count_valid_cells(circ, &cell);
  412. else
  413. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  414. circ->cpath);
  415. ASSERT_UNCOUNTED_BW();
  416. connection_free_minimal(ENTRY_TO_CONN(entryconn2));
  417. connection_free_minimal(ENTRY_TO_CONN(entryconn3));
  418. connection_free_minimal(ENTRY_TO_CONN(entryconn4));
  419. return 1;
  420. done:
  421. connection_free_minimal(ENTRY_TO_CONN(entryconn2));
  422. connection_free_minimal(ENTRY_TO_CONN(entryconn3));
  423. connection_free_minimal(ENTRY_TO_CONN(entryconn4));
  424. return 0;
  425. }
  426. static int
  427. halfstream_insert(origin_circuit_t *circ, edge_connection_t *edgeconn,
  428. streamid_t *streams, int num, int random)
  429. {
  430. int inserted = 0;
  431. /* Insert num random elements */
  432. while (inserted < num) {
  433. streamid_t id;
  434. if (random)
  435. id = (streamid_t)crypto_rand_int(65535)+1;
  436. else
  437. id = get_unique_stream_id_by_circ(circ);
  438. edgeconn->stream_id = id;
  439. /* Ensure it isn't there */
  440. if (connection_half_edge_find_stream_id(circ->half_streams, id)) {
  441. continue;
  442. }
  443. connection_half_edge_add(edgeconn, circ);
  444. if (streams)
  445. streams[inserted] = id;
  446. inserted++;
  447. }
  448. return inserted;
  449. }
  450. static void
  451. subtest_halfstream_insertremove(int num)
  452. {
  453. origin_circuit_t *circ =
  454. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_GENERAL, 0);
  455. edge_connection_t *edgeconn;
  456. entry_connection_t *entryconn;
  457. streamid_t *streams = tor_malloc_zero(num*sizeof(streamid_t));
  458. int i = 0;
  459. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  460. circ->cpath->deliver_window = CIRCWINDOW_START;
  461. entryconn = fake_entry_conn(circ, 23);
  462. edgeconn = ENTRY_TO_EDGE_CONN(entryconn);
  463. /* Explicity test all operations on an absent stream list */
  464. tt_int_op(connection_half_edge_is_valid_data(circ->half_streams,
  465. 23), OP_EQ, 0);
  466. tt_int_op(connection_half_edge_is_valid_connected(circ->half_streams,
  467. 23), OP_EQ, 0);
  468. tt_int_op(connection_half_edge_is_valid_sendme(circ->half_streams,
  469. 23), OP_EQ, 0);
  470. tt_int_op(connection_half_edge_is_valid_resolved(circ->half_streams,
  471. 23), OP_EQ, 0);
  472. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  473. 23), OP_EQ, 0);
  474. /* Insert a duplicate element; verify that other elements absent;
  475. * ensure removing it once works */
  476. edgeconn->stream_id = 23;
  477. connection_half_edge_add(edgeconn, circ);
  478. connection_half_edge_add(edgeconn, circ);
  479. connection_half_edge_add(edgeconn, circ);
  480. /* Verify that other elements absent */
  481. tt_int_op(connection_half_edge_is_valid_data(circ->half_streams,
  482. 22), OP_EQ, 0);
  483. tt_int_op(connection_half_edge_is_valid_connected(circ->half_streams,
  484. 22), OP_EQ, 0);
  485. tt_int_op(connection_half_edge_is_valid_sendme(circ->half_streams,
  486. 22), OP_EQ, 0);
  487. tt_int_op(connection_half_edge_is_valid_resolved(circ->half_streams,
  488. 22), OP_EQ, 0);
  489. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  490. 22), OP_EQ, 0);
  491. tt_int_op(connection_half_edge_is_valid_data(circ->half_streams,
  492. 24), OP_EQ, 0);
  493. tt_int_op(connection_half_edge_is_valid_connected(circ->half_streams,
  494. 24), OP_EQ, 0);
  495. tt_int_op(connection_half_edge_is_valid_sendme(circ->half_streams,
  496. 24), OP_EQ, 0);
  497. tt_int_op(connection_half_edge_is_valid_resolved(circ->half_streams,
  498. 24), OP_EQ, 0);
  499. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  500. 24), OP_EQ, 0);
  501. /* Verify we only remove it once */
  502. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  503. 23), OP_EQ, 1);
  504. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  505. 23), OP_EQ, 0);
  506. halfstream_insert(circ, edgeconn, streams, num, 1);
  507. /* Remove half of them */
  508. for (i = 0; i < num/2; i++) {
  509. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  510. streams[i]),
  511. OP_EQ, 1);
  512. }
  513. /* Verify first half of list is gone */
  514. for (i = 0; i < num/2; i++) {
  515. tt_ptr_op(connection_half_edge_find_stream_id(circ->half_streams,
  516. streams[i]),
  517. OP_EQ, NULL);
  518. }
  519. /* Verify second half of list is present */
  520. for (; i < num; i++) {
  521. tt_ptr_op(connection_half_edge_find_stream_id(circ->half_streams,
  522. streams[i]),
  523. OP_NE, NULL);
  524. }
  525. /* Remove other half. Verify list is empty. */
  526. for (i = num/2; i < num; i++) {
  527. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  528. streams[i]),
  529. OP_EQ, 1);
  530. }
  531. tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 0);
  532. /* Explicity test all operations on an empty stream list */
  533. tt_int_op(connection_half_edge_is_valid_data(circ->half_streams,
  534. 23), OP_EQ, 0);
  535. tt_int_op(connection_half_edge_is_valid_connected(circ->half_streams,
  536. 23), OP_EQ, 0);
  537. tt_int_op(connection_half_edge_is_valid_sendme(circ->half_streams,
  538. 23), OP_EQ, 0);
  539. tt_int_op(connection_half_edge_is_valid_resolved(circ->half_streams,
  540. 23), OP_EQ, 0);
  541. tt_int_op(connection_half_edge_is_valid_end(circ->half_streams,
  542. 23), OP_EQ, 0);
  543. /* For valgrind, leave some around then free the circ */
  544. halfstream_insert(circ, edgeconn, NULL, 10, 0);
  545. done:
  546. tor_free(streams);
  547. circuit_free_(TO_CIRCUIT(circ));
  548. connection_free_minimal(ENTRY_TO_CONN(entryconn));
  549. }
  550. static void
  551. test_halfstream_insertremove(void *arg)
  552. {
  553. (void)arg;
  554. /* Suppress the WARN message we generate in this test */
  555. setup_full_capture_of_logs(LOG_WARN);
  556. /* Test insertion and removal with a few different sizes */
  557. subtest_halfstream_insertremove(10);
  558. subtest_halfstream_insertremove(100);
  559. subtest_halfstream_insertremove(1000);
  560. }
  561. static void
  562. test_halfstream_wrap(void *arg)
  563. {
  564. origin_circuit_t *circ =
  565. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_GENERAL, 0);
  566. edge_connection_t *edgeconn;
  567. entry_connection_t *entryconn;
  568. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  569. circ->cpath->deliver_window = CIRCWINDOW_START;
  570. entryconn = fake_entry_conn(circ, 23);
  571. edgeconn = ENTRY_TO_EDGE_CONN(entryconn);
  572. (void)arg;
  573. /* Suppress the WARN message we generate in this test */
  574. setup_full_capture_of_logs(LOG_WARN);
  575. MOCK(connection_mark_for_close_internal_, mock_mark_for_close);
  576. /* Verify that get_unique_stream_id_by_circ() can wrap uint16_t */
  577. circ->next_stream_id = 65530;
  578. halfstream_insert(circ, edgeconn, NULL, 7, 0);
  579. tt_int_op(circ->next_stream_id, OP_EQ, 2);
  580. tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 7);
  581. /* Insert full-1 */
  582. halfstream_insert(circ, edgeconn, NULL,
  583. 65534-smartlist_len(circ->half_streams), 0);
  584. tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 65534);
  585. /* Verify that we can get_unique_stream_id_by_circ() successfully */
  586. edgeconn->stream_id = get_unique_stream_id_by_circ(circ);
  587. tt_int_op(edgeconn->stream_id, OP_NE, 0); /* 0 is failure */
  588. /* Insert an opened stream on the circ with that id */
  589. ENTRY_TO_CONN(entryconn)->marked_for_close = 0;
  590. ENTRY_TO_CONN(entryconn)->outbuf_flushlen = 0;
  591. edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT;
  592. circ->p_streams = edgeconn;
  593. /* Verify that get_unique_stream_id_by_circ() fails */
  594. tt_int_op(get_unique_stream_id_by_circ(circ), OP_EQ, 0); /* 0 is failure */
  595. /* eof the one opened stream. Verify it is now in half-closed */
  596. tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 65534);
  597. connection_edge_reached_eof(edgeconn);
  598. tt_int_op(smartlist_len(circ->half_streams), OP_EQ, 65535);
  599. /* Verify get_unique_stream_id_by_circ() fails due to full half-closed */
  600. circ->p_streams = NULL;
  601. tt_int_op(get_unique_stream_id_by_circ(circ), OP_EQ, 0); /* 0 is failure */
  602. done:
  603. circuit_free_(TO_CIRCUIT(circ));
  604. connection_free_minimal(ENTRY_TO_CONN(entryconn));
  605. UNMOCK(connection_mark_for_close_internal_);
  606. }
  607. static void
  608. test_circbw_relay(void *arg)
  609. {
  610. cell_t cell;
  611. relay_header_t rh;
  612. tor_addr_t addr;
  613. edge_connection_t *edgeconn;
  614. entry_connection_t *entryconn1=NULL;
  615. origin_circuit_t *circ;
  616. int delivered = 0;
  617. int overhead = 0;
  618. (void)arg;
  619. MOCK(connection_mark_unattached_ap_, mock_connection_mark_unattached_ap_);
  620. MOCK(connection_start_reading, mock_start_reading);
  621. MOCK(connection_mark_for_close_internal_, mock_mark_for_close);
  622. MOCK(relay_send_command_from_edge_, mock_send_command);
  623. MOCK(circuit_mark_for_close_, mock_mark_circ_for_close);
  624. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_C_GENERAL, 0);
  625. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  626. circ->cpath->deliver_window = CIRCWINDOW_START;
  627. entryconn1 = fake_entry_conn(circ, 1);
  628. edgeconn = ENTRY_TO_EDGE_CONN(entryconn1);
  629. /* Stream id 0: Not counted */
  630. PACK_CELL(0, RELAY_COMMAND_END, "Data1234");
  631. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  632. circ->cpath);
  633. ASSERT_UNCOUNTED_BW();
  634. /* Stream id 1: Counted */
  635. PACK_CELL(1, RELAY_COMMAND_END, "Data1234");
  636. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  637. circ->cpath);
  638. ASSERT_COUNTED_BW();
  639. /* Properly formatted connect cell: counted */
  640. PACK_CELL(1, RELAY_COMMAND_CONNECTED, "Data1234");
  641. tor_addr_parse(&addr, "30.40.50.60");
  642. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  643. &addr, 1024);
  644. relay_header_pack((uint8_t*)&cell.payload, &rh); \
  645. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  646. circ->cpath);
  647. ASSERT_COUNTED_BW();
  648. /* Properly formatted resolved cell in correct state: counted */
  649. edgeconn->base_.state = AP_CONN_STATE_RESOLVE_WAIT;
  650. entryconn1->socks_request->command = SOCKS_COMMAND_RESOLVE;
  651. edgeconn->on_circuit = TO_CIRCUIT(circ);
  652. PACK_CELL(1, RELAY_COMMAND_RESOLVED,
  653. "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
  654. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  655. circ->cpath);
  656. ASSERT_COUNTED_BW();
  657. edgeconn->base_.state = AP_CONN_STATE_OPEN;
  658. entryconn1->socks_request->has_finished = 1;
  659. /* Connected cell after open: not counted */
  660. PACK_CELL(1, RELAY_COMMAND_CONNECTED, "Data1234");
  661. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  662. circ->cpath);
  663. ASSERT_UNCOUNTED_BW();
  664. /* Resolved cell after open: not counted */
  665. PACK_CELL(1, RELAY_COMMAND_RESOLVED, "Data1234");
  666. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  667. circ->cpath);
  668. ASSERT_UNCOUNTED_BW();
  669. /* Drop cell: not counted */
  670. PACK_CELL(1, RELAY_COMMAND_DROP, "Data1234");
  671. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  672. circ->cpath);
  673. ASSERT_UNCOUNTED_BW();
  674. /* Data cell on stream 0: not counted */
  675. PACK_CELL(0, RELAY_COMMAND_DATA, "Data1234");
  676. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  677. circ->cpath);
  678. ASSERT_UNCOUNTED_BW();
  679. /* Data cell on open connection: counted */
  680. ENTRY_TO_CONN(entryconn1)->marked_for_close = 0;
  681. PACK_CELL(1, RELAY_COMMAND_DATA, "Data1234");
  682. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  683. circ->cpath);
  684. ASSERT_COUNTED_BW();
  685. /* Empty Data cell on open connection: not counted */
  686. ENTRY_TO_CONN(entryconn1)->marked_for_close = 0;
  687. PACK_CELL(1, RELAY_COMMAND_DATA, "");
  688. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  689. circ->cpath);
  690. ASSERT_UNCOUNTED_BW();
  691. /* Sendme on valid stream: counted */
  692. edgeconn->package_window -= STREAMWINDOW_INCREMENT;
  693. ENTRY_TO_CONN(entryconn1)->outbuf_flushlen = 0;
  694. PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
  695. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  696. circ->cpath);
  697. ASSERT_COUNTED_BW();
  698. /* Sendme on valid stream with full window: not counted */
  699. ENTRY_TO_CONN(entryconn1)->outbuf_flushlen = 0;
  700. PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
  701. edgeconn->package_window = STREAMWINDOW_START;
  702. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  703. circ->cpath);
  704. ASSERT_UNCOUNTED_BW();
  705. /* Sendme on unknown stream: not counted */
  706. ENTRY_TO_CONN(entryconn1)->outbuf_flushlen = 0;
  707. PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
  708. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  709. circ->cpath);
  710. ASSERT_UNCOUNTED_BW();
  711. /* Sendme on circuit with full window: not counted */
  712. PACK_CELL(0, RELAY_COMMAND_SENDME, "Data1234");
  713. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  714. circ->cpath);
  715. ASSERT_UNCOUNTED_BW();
  716. /* Sendme on circuit with non-full window: counted */
  717. PACK_CELL(0, RELAY_COMMAND_SENDME, "Data1234");
  718. circ->cpath->package_window = 900;
  719. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  720. circ->cpath);
  721. ASSERT_COUNTED_BW();
  722. /* Invalid extended cell: not counted */
  723. PACK_CELL(1, RELAY_COMMAND_EXTENDED2, "Data1234");
  724. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  725. circ->cpath);
  726. ASSERT_UNCOUNTED_BW();
  727. /* Invalid extended cell: not counted */
  728. PACK_CELL(1, RELAY_COMMAND_EXTENDED, "Data1234");
  729. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  730. circ->cpath);
  731. ASSERT_UNCOUNTED_BW();
  732. /* Invalid HS cell: not counted */
  733. PACK_CELL(1, RELAY_COMMAND_ESTABLISH_INTRO, "Data1234");
  734. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  735. circ->cpath);
  736. ASSERT_UNCOUNTED_BW();
  737. /* "Valid" HS cell in expected state: counted */
  738. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  739. PACK_CELL(1, RELAY_COMMAND_RENDEZVOUS_ESTABLISHED, "Data1234");
  740. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  741. circ->cpath);
  742. ASSERT_COUNTED_BW();
  743. /* End cell on non-closed connection: counted */
  744. PACK_CELL(1, RELAY_COMMAND_END, "Data1234");
  745. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  746. circ->cpath);
  747. ASSERT_COUNTED_BW();
  748. /* End cell on connection that already got one: not counted */
  749. PACK_CELL(1, RELAY_COMMAND_END, "Data1234");
  750. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  751. circ->cpath);
  752. ASSERT_UNCOUNTED_BW();
  753. /* Simulate closed stream on entryconn, then test: */
  754. if (!subtest_circbw_halfclosed(circ, 2))
  755. goto done;
  756. circ->base_.purpose = CIRCUIT_PURPOSE_PATH_BIAS_TESTING;
  757. if (!subtest_circbw_halfclosed(circ, 6))
  758. goto done;
  759. /* Path bias: truncated */
  760. tt_int_op(circ->base_.marked_for_close, OP_EQ, 0);
  761. PACK_CELL(0, RELAY_COMMAND_TRUNCATED, "Data1234");
  762. pathbias_count_valid_cells(circ, &cell);
  763. tt_int_op(circ->base_.marked_for_close, OP_EQ, 1);
  764. done:
  765. UNMOCK(connection_start_reading);
  766. UNMOCK(connection_mark_unattached_ap_);
  767. UNMOCK(connection_mark_for_close_internal_);
  768. UNMOCK(relay_send_command_from_edge_);
  769. UNMOCK(circuit_mark_for_close_);
  770. circuit_free_(TO_CIRCUIT(circ));
  771. connection_free_minimal(ENTRY_TO_CONN(entryconn1));
  772. }
  773. /* Tests for connection_edge_process_resolved_cell().
  774. The point of ..process_resolved_cell() is to handle an incoming cell
  775. on an entry connection, and call connection_mark_unattached_ap() and/or
  776. connection_ap_handshake_socks_resolved().
  777. */
  778. static void
  779. test_relaycell_resolved(void *arg)
  780. {
  781. entry_connection_t *entryconn;
  782. edge_connection_t *edgeconn;
  783. cell_t cell;
  784. relay_header_t rh;
  785. int r;
  786. or_options_t *options = get_options_mutable();
  787. #define SET_CELL(s) do { \
  788. memset(&cell, 0, sizeof(cell)); \
  789. memset(&rh, 0, sizeof(rh)); \
  790. memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
  791. rh.length = sizeof((s))-1; \
  792. rh.command = RELAY_COMMAND_RESOLVED; \
  793. } while (0)
  794. #define MOCK_RESET() do { \
  795. srm_ncalls = mum_ncalls = 0; \
  796. } while (0)
  797. #define ASSERT_MARK_CALLED(reason) do { \
  798. tt_int_op(mum_ncalls, OP_EQ, 1); \
  799. tt_ptr_op(mum_conn, OP_EQ, entryconn); \
  800. tt_int_op(mum_endreason, OP_EQ, (reason)); \
  801. } while (0)
  802. #define ASSERT_RESOLVED_CALLED(atype, answer, ttl, expires) do { \
  803. tt_int_op(srm_ncalls, OP_EQ, 1); \
  804. tt_ptr_op(srm_conn, OP_EQ, entryconn); \
  805. tt_int_op(srm_atype, OP_EQ, (atype)); \
  806. if ((answer) != NULL) { \
  807. tt_int_op(srm_alen, OP_EQ, sizeof(answer)-1); \
  808. tt_int_op(srm_alen, OP_LT, 512); \
  809. tt_int_op(srm_answer_is_set, OP_EQ, 1); \
  810. tt_mem_op(srm_answer, OP_EQ, answer, sizeof(answer)-1); \
  811. } else { \
  812. tt_int_op(srm_answer_is_set, OP_EQ, 0); \
  813. } \
  814. tt_int_op(srm_ttl, OP_EQ, ttl); \
  815. tt_i64_op(srm_expires, OP_EQ, expires); \
  816. } while (0)
  817. (void)arg;
  818. MOCK(connection_mark_unattached_ap_, mark_unattached_mock);
  819. MOCK(connection_ap_handshake_socks_resolved, socks_resolved_mock);
  820. options->ClientDNSRejectInternalAddresses = 0;
  821. SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
  822. "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
  823. /* IPv4: 18.0.0.1, ttl 512 */
  824. "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00"
  825. /* IPv6: 2003::3, ttl 1024 */
  826. "\x06\x10"
  827. "\x20\x02\x00\x00\x00\x00\x00\x00"
  828. "\x00\x00\x00\x00\x00\x00\x00\x03"
  829. "\x00\x00\x04\x00");
  830. entryconn = entry_connection_new(CONN_TYPE_AP, AF_INET);
  831. edgeconn = ENTRY_TO_EDGE_CONN(entryconn);
  832. /* Try with connection in non-RESOLVE_WAIT state: cell gets ignored */
  833. MOCK_RESET();
  834. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  835. tt_int_op(r, OP_EQ, 0);
  836. tt_int_op(srm_ncalls, OP_EQ, 0);
  837. tt_int_op(mum_ncalls, OP_EQ, 0);
  838. /* Now put it in the right state. */
  839. ENTRY_TO_CONN(entryconn)->state = AP_CONN_STATE_RESOLVE_WAIT;
  840. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  841. entryconn->entry_cfg.ipv4_traffic = 1;
  842. entryconn->entry_cfg.ipv6_traffic = 1;
  843. entryconn->entry_cfg.prefer_ipv6 = 0;
  844. /* We prefer ipv4, so we should get the first ipv4 answer */
  845. MOCK_RESET();
  846. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  847. tt_int_op(r, OP_EQ, 0);
  848. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  849. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  850. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x7f\x00\x01\x02", 256, -1);
  851. /* But we may be discarding private answers. */
  852. MOCK_RESET();
  853. options->ClientDNSRejectInternalAddresses = 1;
  854. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  855. tt_int_op(r, OP_EQ, 0);
  856. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  857. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  858. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x12\x00\x00\x01", 512, -1);
  859. /* now prefer ipv6, and get the first ipv6 answer */
  860. entryconn->entry_cfg.prefer_ipv6 = 1;
  861. MOCK_RESET();
  862. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  863. tt_int_op(r, OP_EQ, 0);
  864. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  865. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  866. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV6,
  867. "\x20\x02\x00\x00\x00\x00\x00\x00"
  868. "\x00\x00\x00\x00\x00\x00\x00\x03",
  869. 1024, -1);
  870. /* With a cell that only has IPv4, we report IPv4 even if we prefer IPv6 */
  871. MOCK_RESET();
  872. SET_CELL("\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
  873. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  874. tt_int_op(r, OP_EQ, 0);
  875. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  876. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  877. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x12\x00\x00\x01", 512, -1);
  878. /* But if we don't allow IPv4, we report nothing if the cell contains only
  879. * ipv4 */
  880. MOCK_RESET();
  881. entryconn->entry_cfg.ipv4_traffic = 0;
  882. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  883. tt_int_op(r, OP_EQ, 0);
  884. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  885. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  886. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR, NULL, -1, -1);
  887. /* If we wanted hostnames, we report nothing, since we only had IPs. */
  888. MOCK_RESET();
  889. entryconn->entry_cfg.ipv4_traffic = 1;
  890. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  891. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  892. tt_int_op(r, OP_EQ, 0);
  893. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  894. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  895. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR, NULL, -1, -1);
  896. /* A hostname cell is fine though. */
  897. MOCK_RESET();
  898. SET_CELL("\x00\x0fwww.example.com\x00\x01\x00\x00");
  899. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  900. tt_int_op(r, OP_EQ, 0);
  901. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  902. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  903. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_HOSTNAME, "www.example.com", 65536, -1);
  904. /* error on malformed cell */
  905. MOCK_RESET();
  906. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  907. SET_CELL("\x04\x04\x01\x02\x03\x04"); /* no ttl */
  908. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  909. tt_int_op(r, OP_EQ, 0);
  910. ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL);
  911. tt_int_op(srm_ncalls, OP_EQ, 0);
  912. /* error on all addresses private */
  913. MOCK_RESET();
  914. SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
  915. "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
  916. /* IPv4: 192.168.1.1, ttl 256 */
  917. "\x04\x04\xc0\xa8\x01\x01\x00\x00\x01\x00");
  918. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  919. tt_int_op(r, OP_EQ, 0);
  920. ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL);
  921. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT, NULL, 0, TIME_MAX);
  922. /* Legit error code */
  923. MOCK_RESET();
  924. SET_CELL("\xf0\x15" "quiet and meaningless" "\x00\x00\x0f\xff");
  925. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  926. tt_int_op(r, OP_EQ, 0);
  927. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  928. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  929. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT, NULL, -1, -1);
  930. done:
  931. UNMOCK(connection_mark_unattached_ap_);
  932. UNMOCK(connection_ap_handshake_socks_resolved);
  933. }
  934. struct testcase_t relaycell_tests[] = {
  935. { "resolved", test_relaycell_resolved, TT_FORK, NULL, NULL },
  936. { "circbw", test_circbw_relay, TT_FORK, NULL, NULL },
  937. { "halfstream", test_halfstream_insertremove, TT_FORK, NULL, NULL },
  938. { "streamwrap", test_halfstream_wrap, TT_FORK, NULL, NULL },
  939. END_OF_TESTCASES
  940. };