test_relaycell.c 39 KB

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