test_relaycell.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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_cipher.h"
  11. #include "core/or/circuitbuild.h"
  12. #include "core/or/circuitlist.h"
  13. #include "core/or/connection_edge.h"
  14. #include "core/or/relay.h"
  15. #include "test/test.h"
  16. #include "core/or/cell_st.h"
  17. #include "core/or/crypt_path_st.h"
  18. #include "core/or/entry_connection_st.h"
  19. #include "core/or/origin_circuit_st.h"
  20. #include "core/or/socks_request_st.h"
  21. static int srm_ncalls;
  22. static entry_connection_t *srm_conn;
  23. static int srm_atype;
  24. static size_t srm_alen;
  25. static int srm_answer_is_set;
  26. static uint8_t srm_answer[512];
  27. static int srm_ttl;
  28. static time_t srm_expires;
  29. void connection_free_minimal(connection_t*);
  30. int connected_cell_format_payload(uint8_t *payload_out,
  31. const tor_addr_t *addr,
  32. uint32_t ttl);
  33. /* Mock replacement for connection_ap_hannshake_socks_resolved() */
  34. static void
  35. socks_resolved_mock(entry_connection_t *conn,
  36. int answer_type,
  37. size_t answer_len,
  38. const uint8_t *answer,
  39. int ttl,
  40. time_t expires)
  41. {
  42. srm_ncalls++;
  43. srm_conn = conn;
  44. srm_atype = answer_type;
  45. srm_alen = answer_len;
  46. if (answer) {
  47. memset(srm_answer, 0, sizeof(srm_answer));
  48. memcpy(srm_answer, answer, answer_len < 512 ? answer_len : 512);
  49. srm_answer_is_set = 1;
  50. } else {
  51. srm_answer_is_set = 0;
  52. }
  53. srm_ttl = ttl;
  54. srm_expires = expires;
  55. }
  56. static int mum_ncalls;
  57. static entry_connection_t *mum_conn;
  58. static int mum_endreason;
  59. /* Mock replacement for connection_mark_unattached_ap_() */
  60. static void
  61. mark_unattached_mock(entry_connection_t *conn, int endreason,
  62. int line, const char *file)
  63. {
  64. ++mum_ncalls;
  65. mum_conn = conn;
  66. mum_endreason = endreason;
  67. (void) line;
  68. (void) file;
  69. }
  70. /* Helper: Return a newly allocated and initialized origin circuit with
  71. * purpose and flags. A default HS identifier is set to an ed25519
  72. * authentication key for introduction point. */
  73. static origin_circuit_t *
  74. helper_create_origin_circuit(int purpose, int flags)
  75. {
  76. origin_circuit_t *circ = NULL;
  77. circ = origin_circuit_init(purpose, flags);
  78. tor_assert(circ);
  79. circ->cpath = tor_malloc_zero(sizeof(crypt_path_t));
  80. circ->cpath->magic = CRYPT_PATH_MAGIC;
  81. circ->cpath->state = CPATH_STATE_OPEN;
  82. circ->cpath->package_window = circuit_initial_package_window();
  83. circ->cpath->deliver_window = CIRCWINDOW_START;
  84. circ->cpath->prev = circ->cpath;
  85. /* Create a default HS identifier. */
  86. circ->hs_ident = tor_malloc_zero(sizeof(hs_ident_circuit_t));
  87. return circ;
  88. }
  89. static void
  90. mock_connection_mark_unattached_ap_(entry_connection_t *conn, int endreason,
  91. int line, const char *file)
  92. {
  93. (void) line;
  94. (void) file;
  95. conn->edge_.end_reason = endreason;
  96. }
  97. static void
  98. mock_mark_for_close(connection_t *conn,
  99. int line, const char *file)
  100. {
  101. (void)line;
  102. (void)file;
  103. conn->marked_for_close = 1;
  104. return;
  105. }
  106. static void
  107. mock_start_reading(connection_t *conn)
  108. {
  109. (void)conn;
  110. return;
  111. }
  112. static void
  113. test_circbw_relay(void *arg)
  114. {
  115. cell_t cell;
  116. relay_header_t rh;
  117. tor_addr_t addr;
  118. edge_connection_t *edgeconn;
  119. entry_connection_t *entryconn;
  120. origin_circuit_t *circ;
  121. int delivered = 0;
  122. int overhead = 0;
  123. (void)arg;
  124. #define PACK_CELL(id, cmd, body_s) do { \
  125. memset(&cell, 0, sizeof(cell)); \
  126. memset(&rh, 0, sizeof(rh)); \
  127. memcpy(cell.payload+RELAY_HEADER_SIZE, (body_s), sizeof((body_s))-1); \
  128. rh.length = sizeof((body_s))-1; \
  129. rh.command = (cmd); \
  130. rh.stream_id = (id); \
  131. relay_header_pack((uint8_t*)&cell.payload, &rh); \
  132. } while (0)
  133. #define ASSERT_COUNTED_BW() do { \
  134. tt_int_op(circ->n_delivered_read_circ_bw, OP_EQ, delivered+rh.length); \
  135. tt_int_op(circ->n_overhead_read_circ_bw, OP_EQ, \
  136. overhead+RELAY_PAYLOAD_SIZE-rh.length); \
  137. delivered = circ->n_delivered_read_circ_bw; \
  138. overhead = circ->n_overhead_read_circ_bw; \
  139. } while (0)
  140. #define ASSERT_UNCOUNTED_BW() do { \
  141. tt_int_op(circ->n_delivered_read_circ_bw, OP_EQ, delivered); \
  142. tt_int_op(circ->n_overhead_read_circ_bw, OP_EQ, overhead); \
  143. } while (0)
  144. MOCK(connection_mark_unattached_ap_, mock_connection_mark_unattached_ap_);
  145. MOCK(connection_start_reading, mock_start_reading);
  146. MOCK(connection_mark_for_close_internal_, mock_mark_for_close);
  147. entryconn = entry_connection_new(CONN_TYPE_AP, AF_INET);
  148. edgeconn = ENTRY_TO_EDGE_CONN(entryconn);
  149. edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT;
  150. edgeconn->deliver_window = 1000;
  151. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_C_GENERAL, 0);
  152. edgeconn->cpath_layer = circ->cpath;
  153. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  154. circ->cpath->deliver_window = 1000;
  155. /* Stream id 0: Not counted */
  156. PACK_CELL(0, RELAY_COMMAND_END, "Data1234");
  157. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  158. circ->cpath);
  159. ASSERT_UNCOUNTED_BW();
  160. /* Stream id 1: Counted */
  161. PACK_CELL(1, RELAY_COMMAND_END, "Data1234");
  162. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  163. circ->cpath);
  164. ASSERT_COUNTED_BW();
  165. /* Properly formatted connect cell: counted */
  166. PACK_CELL(1, RELAY_COMMAND_CONNECTED, "Data1234");
  167. tor_addr_parse(&addr, "30.40.50.60");
  168. rh.length = connected_cell_format_payload(cell.payload+RELAY_HEADER_SIZE,
  169. &addr, 1024);
  170. relay_header_pack((uint8_t*)&cell.payload, &rh); \
  171. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  172. circ->cpath);
  173. ASSERT_COUNTED_BW();
  174. /* Properly formatted resolved cell in correct state: counted */
  175. edgeconn->base_.state = AP_CONN_STATE_RESOLVE_WAIT;
  176. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  177. edgeconn->on_circuit = TO_CIRCUIT(circ);
  178. PACK_CELL(1, RELAY_COMMAND_RESOLVED,
  179. "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
  180. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  181. circ->cpath);
  182. ASSERT_COUNTED_BW();
  183. edgeconn->base_.state = AP_CONN_STATE_OPEN;
  184. entryconn->socks_request->has_finished = 1;
  185. /* Connected cell after open: not counted */
  186. PACK_CELL(1, RELAY_COMMAND_CONNECTED, "Data1234");
  187. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  188. circ->cpath);
  189. ASSERT_UNCOUNTED_BW();
  190. /* Resolved cell after open: not counted */
  191. PACK_CELL(1, RELAY_COMMAND_RESOLVED, "Data1234");
  192. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  193. circ->cpath);
  194. ASSERT_UNCOUNTED_BW();
  195. /* Drop cell: not counted */
  196. PACK_CELL(1, RELAY_COMMAND_DROP, "Data1234");
  197. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  198. circ->cpath);
  199. ASSERT_UNCOUNTED_BW();
  200. /* Data cell on stream 0: not counted */
  201. PACK_CELL(1, RELAY_COMMAND_DATA, "Data1234");
  202. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  203. circ->cpath);
  204. ASSERT_UNCOUNTED_BW();
  205. /* Data cell on open connection: counted */
  206. ENTRY_TO_CONN(entryconn)->marked_for_close = 0;
  207. PACK_CELL(1, RELAY_COMMAND_DATA, "Data1234");
  208. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  209. circ->cpath);
  210. ASSERT_COUNTED_BW();
  211. /* Empty Data cell on open connection: not counted */
  212. ENTRY_TO_CONN(entryconn)->marked_for_close = 0;
  213. PACK_CELL(1, RELAY_COMMAND_DATA, "");
  214. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  215. circ->cpath);
  216. ASSERT_UNCOUNTED_BW();
  217. /* Sendme on valid stream: counted */
  218. ENTRY_TO_CONN(entryconn)->outbuf_flushlen = 0;
  219. PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
  220. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  221. circ->cpath);
  222. ASSERT_COUNTED_BW();
  223. /* Sendme on valid stream with full window: not counted */
  224. ENTRY_TO_CONN(entryconn)->outbuf_flushlen = 0;
  225. PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
  226. edgeconn->package_window = 500;
  227. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  228. circ->cpath);
  229. ASSERT_UNCOUNTED_BW();
  230. /* Sendme on unknown stream: not counted */
  231. ENTRY_TO_CONN(entryconn)->outbuf_flushlen = 0;
  232. PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
  233. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  234. circ->cpath);
  235. ASSERT_UNCOUNTED_BW();
  236. /* Sendme on circuit with full window: not counted */
  237. PACK_CELL(0, RELAY_COMMAND_SENDME, "Data1234");
  238. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  239. circ->cpath);
  240. ASSERT_UNCOUNTED_BW();
  241. /* Sendme on circuit with non-full window: counted */
  242. PACK_CELL(0, RELAY_COMMAND_SENDME, "Data1234");
  243. circ->cpath->package_window = 900;
  244. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  245. circ->cpath);
  246. ASSERT_COUNTED_BW();
  247. /* End cell on non-closed connection: counted */
  248. PACK_CELL(1, RELAY_COMMAND_END, "Data1234");
  249. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
  250. circ->cpath);
  251. ASSERT_COUNTED_BW();
  252. /* End cell on connection that already got one: not counted */
  253. PACK_CELL(1, RELAY_COMMAND_END, "Data1234");
  254. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  255. circ->cpath);
  256. ASSERT_UNCOUNTED_BW();
  257. /* Invalid extended cell: not counted */
  258. PACK_CELL(1, RELAY_COMMAND_EXTENDED2, "Data1234");
  259. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  260. circ->cpath);
  261. ASSERT_UNCOUNTED_BW();
  262. /* Invalid extended cell: not counted */
  263. PACK_CELL(1, RELAY_COMMAND_EXTENDED, "Data1234");
  264. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  265. circ->cpath);
  266. ASSERT_UNCOUNTED_BW();
  267. /* Invalid HS cell: not counted */
  268. PACK_CELL(1, RELAY_COMMAND_ESTABLISH_INTRO, "Data1234");
  269. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  270. circ->cpath);
  271. ASSERT_UNCOUNTED_BW();
  272. /* "Valid" HS cell in expected state: counted */
  273. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
  274. PACK_CELL(1, RELAY_COMMAND_RENDEZVOUS_ESTABLISHED, "Data1234");
  275. connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
  276. circ->cpath);
  277. ASSERT_COUNTED_BW();
  278. done:
  279. UNMOCK(connection_start_reading);
  280. UNMOCK(connection_mark_unattached_ap_);
  281. UNMOCK(connection_mark_for_close_internal_);
  282. circuit_free_(TO_CIRCUIT(circ));
  283. connection_free_minimal(ENTRY_TO_CONN(entryconn));
  284. }
  285. /* Tests for connection_edge_process_resolved_cell().
  286. The point of ..process_resolved_cell() is to handle an incoming cell
  287. on an entry connection, and call connection_mark_unattached_ap() and/or
  288. connection_ap_handshake_socks_resolved().
  289. */
  290. static void
  291. test_relaycell_resolved(void *arg)
  292. {
  293. entry_connection_t *entryconn;
  294. edge_connection_t *edgeconn;
  295. cell_t cell;
  296. relay_header_t rh;
  297. int r;
  298. or_options_t *options = get_options_mutable();
  299. #define SET_CELL(s) do { \
  300. memset(&cell, 0, sizeof(cell)); \
  301. memset(&rh, 0, sizeof(rh)); \
  302. memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
  303. rh.length = sizeof((s))-1; \
  304. rh.command = RELAY_COMMAND_RESOLVED; \
  305. } while (0)
  306. #define MOCK_RESET() do { \
  307. srm_ncalls = mum_ncalls = 0; \
  308. } while (0)
  309. #define ASSERT_MARK_CALLED(reason) do { \
  310. tt_int_op(mum_ncalls, OP_EQ, 1); \
  311. tt_ptr_op(mum_conn, OP_EQ, entryconn); \
  312. tt_int_op(mum_endreason, OP_EQ, (reason)); \
  313. } while (0)
  314. #define ASSERT_RESOLVED_CALLED(atype, answer, ttl, expires) do { \
  315. tt_int_op(srm_ncalls, OP_EQ, 1); \
  316. tt_ptr_op(srm_conn, OP_EQ, entryconn); \
  317. tt_int_op(srm_atype, OP_EQ, (atype)); \
  318. if ((answer) != NULL) { \
  319. tt_int_op(srm_alen, OP_EQ, sizeof(answer)-1); \
  320. tt_int_op(srm_alen, OP_LT, 512); \
  321. tt_int_op(srm_answer_is_set, OP_EQ, 1); \
  322. tt_mem_op(srm_answer, OP_EQ, answer, sizeof(answer)-1); \
  323. } else { \
  324. tt_int_op(srm_answer_is_set, OP_EQ, 0); \
  325. } \
  326. tt_int_op(srm_ttl, OP_EQ, ttl); \
  327. tt_i64_op(srm_expires, OP_EQ, expires); \
  328. } while (0)
  329. (void)arg;
  330. MOCK(connection_mark_unattached_ap_, mark_unattached_mock);
  331. MOCK(connection_ap_handshake_socks_resolved, socks_resolved_mock);
  332. options->ClientDNSRejectInternalAddresses = 0;
  333. SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
  334. "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
  335. /* IPv4: 18.0.0.1, ttl 512 */
  336. "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00"
  337. /* IPv6: 2003::3, ttl 1024 */
  338. "\x06\x10"
  339. "\x20\x02\x00\x00\x00\x00\x00\x00"
  340. "\x00\x00\x00\x00\x00\x00\x00\x03"
  341. "\x00\x00\x04\x00");
  342. entryconn = entry_connection_new(CONN_TYPE_AP, AF_INET);
  343. edgeconn = ENTRY_TO_EDGE_CONN(entryconn);
  344. /* Try with connection in non-RESOLVE_WAIT state: cell gets ignored */
  345. MOCK_RESET();
  346. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  347. tt_int_op(r, OP_EQ, 0);
  348. tt_int_op(srm_ncalls, OP_EQ, 0);
  349. tt_int_op(mum_ncalls, OP_EQ, 0);
  350. /* Now put it in the right state. */
  351. ENTRY_TO_CONN(entryconn)->state = AP_CONN_STATE_RESOLVE_WAIT;
  352. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  353. entryconn->entry_cfg.ipv4_traffic = 1;
  354. entryconn->entry_cfg.ipv6_traffic = 1;
  355. entryconn->entry_cfg.prefer_ipv6 = 0;
  356. /* We prefer ipv4, so we should get the first ipv4 answer */
  357. MOCK_RESET();
  358. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  359. tt_int_op(r, OP_EQ, 0);
  360. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  361. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  362. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x7f\x00\x01\x02", 256, -1);
  363. /* But we may be discarding private answers. */
  364. MOCK_RESET();
  365. options->ClientDNSRejectInternalAddresses = 1;
  366. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  367. tt_int_op(r, OP_EQ, 0);
  368. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  369. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  370. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x12\x00\x00\x01", 512, -1);
  371. /* now prefer ipv6, and get the first ipv6 answer */
  372. entryconn->entry_cfg.prefer_ipv6 = 1;
  373. MOCK_RESET();
  374. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  375. tt_int_op(r, OP_EQ, 0);
  376. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  377. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  378. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV6,
  379. "\x20\x02\x00\x00\x00\x00\x00\x00"
  380. "\x00\x00\x00\x00\x00\x00\x00\x03",
  381. 1024, -1);
  382. /* With a cell that only has IPv4, we report IPv4 even if we prefer IPv6 */
  383. MOCK_RESET();
  384. SET_CELL("\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
  385. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  386. tt_int_op(r, OP_EQ, 0);
  387. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  388. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  389. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x12\x00\x00\x01", 512, -1);
  390. /* But if we don't allow IPv4, we report nothing if the cell contains only
  391. * ipv4 */
  392. MOCK_RESET();
  393. entryconn->entry_cfg.ipv4_traffic = 0;
  394. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  395. tt_int_op(r, OP_EQ, 0);
  396. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  397. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  398. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR, NULL, -1, -1);
  399. /* If we wanted hostnames, we report nothing, since we only had IPs. */
  400. MOCK_RESET();
  401. entryconn->entry_cfg.ipv4_traffic = 1;
  402. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  403. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  404. tt_int_op(r, OP_EQ, 0);
  405. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  406. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  407. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR, NULL, -1, -1);
  408. /* A hostname cell is fine though. */
  409. MOCK_RESET();
  410. SET_CELL("\x00\x0fwww.example.com\x00\x01\x00\x00");
  411. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  412. tt_int_op(r, OP_EQ, 0);
  413. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  414. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  415. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_HOSTNAME, "www.example.com", 65536, -1);
  416. /* error on malformed cell */
  417. MOCK_RESET();
  418. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  419. SET_CELL("\x04\x04\x01\x02\x03\x04"); /* no ttl */
  420. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  421. tt_int_op(r, OP_EQ, 0);
  422. ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL);
  423. tt_int_op(srm_ncalls, OP_EQ, 0);
  424. /* error on all addresses private */
  425. MOCK_RESET();
  426. SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
  427. "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
  428. /* IPv4: 192.168.1.1, ttl 256 */
  429. "\x04\x04\xc0\xa8\x01\x01\x00\x00\x01\x00");
  430. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  431. tt_int_op(r, OP_EQ, 0);
  432. ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL);
  433. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT, NULL, 0, TIME_MAX);
  434. /* Legit error code */
  435. MOCK_RESET();
  436. SET_CELL("\xf0\x15" "quiet and meaningless" "\x00\x00\x0f\xff");
  437. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  438. tt_int_op(r, OP_EQ, 0);
  439. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  440. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  441. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT, NULL, -1, -1);
  442. done:
  443. UNMOCK(connection_mark_unattached_ap_);
  444. UNMOCK(connection_ap_handshake_socks_resolved);
  445. }
  446. struct testcase_t relaycell_tests[] = {
  447. { "resolved", test_relaycell_resolved, TT_FORK, NULL, NULL },
  448. { "circbw", test_circbw_relay, TT_FORK, NULL, NULL },
  449. END_OF_TESTCASES
  450. };