test_relaycell.c 19 KB

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