test_relaycell.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #include "or.h"
  6. #include "config.h"
  7. #include "connection.h"
  8. #include "connection_edge.h"
  9. #include "relay.h"
  10. #include "test.h"
  11. static int srm_ncalls;
  12. static entry_connection_t *srm_conn;
  13. static int srm_atype;
  14. static size_t srm_alen;
  15. static int srm_answer_is_set;
  16. static uint8_t srm_answer[512];
  17. static int srm_ttl;
  18. static time_t srm_expires;
  19. /* Mock replacement for connection_ap_hannshake_socks_resolved() */
  20. static void
  21. socks_resolved_mock(entry_connection_t *conn,
  22. int answer_type,
  23. size_t answer_len,
  24. const uint8_t *answer,
  25. int ttl,
  26. time_t expires)
  27. {
  28. srm_ncalls++;
  29. srm_conn = conn;
  30. srm_atype = answer_type;
  31. srm_alen = answer_len;
  32. if (answer) {
  33. memset(srm_answer, 0, sizeof(srm_answer));
  34. memcpy(srm_answer, answer, answer_len < 512 ? answer_len : 512);
  35. srm_answer_is_set = 1;
  36. } else {
  37. srm_answer_is_set = 0;
  38. }
  39. srm_ttl = ttl;
  40. srm_expires = expires;
  41. }
  42. static int mum_ncalls;
  43. static entry_connection_t *mum_conn;
  44. static int mum_endreason;
  45. /* Mock replacement for connection_mark_unattached_ap_() */
  46. static void
  47. mark_unattached_mock(entry_connection_t *conn, int endreason,
  48. int line, const char *file)
  49. {
  50. ++mum_ncalls;
  51. mum_conn = conn;
  52. mum_endreason = endreason;
  53. (void) line;
  54. (void) file;
  55. }
  56. /* Tests for connection_edge_process_resolved_cell().
  57. The point of ..process_resolved_cell() is to handle an incoming cell
  58. on an entry connection, and call connection_mark_unattached_ap() and/or
  59. connection_ap_handshake_socks_resolved().
  60. */
  61. static void
  62. test_relaycell_resolved(void *arg)
  63. {
  64. entry_connection_t *entryconn;
  65. edge_connection_t *edgeconn;
  66. cell_t cell;
  67. relay_header_t rh;
  68. int r;
  69. or_options_t *options = get_options_mutable();
  70. #define SET_CELL(s) do { \
  71. memset(&cell, 0, sizeof(cell)); \
  72. memset(&rh, 0, sizeof(rh)); \
  73. memcpy(cell.payload + RELAY_HEADER_SIZE, (s), sizeof((s))-1); \
  74. rh.length = sizeof((s))-1; \
  75. rh.command = RELAY_COMMAND_RESOLVED; \
  76. } while (0)
  77. #define MOCK_RESET() do { \
  78. srm_ncalls = mum_ncalls = 0; \
  79. } while (0)
  80. #define ASSERT_MARK_CALLED(reason) do { \
  81. tt_int_op(mum_ncalls, OP_EQ, 1); \
  82. tt_ptr_op(mum_conn, OP_EQ, entryconn); \
  83. tt_int_op(mum_endreason, OP_EQ, (reason)); \
  84. } while (0)
  85. #define ASSERT_RESOLVED_CALLED(atype, answer, ttl, expires) do { \
  86. tt_int_op(srm_ncalls, OP_EQ, 1); \
  87. tt_ptr_op(srm_conn, OP_EQ, entryconn); \
  88. tt_int_op(srm_atype, OP_EQ, (atype)); \
  89. if ((answer) != NULL) { \
  90. tt_int_op(srm_alen, OP_EQ, sizeof(answer)-1); \
  91. tt_int_op(srm_alen, OP_LT, 512); \
  92. tt_int_op(srm_answer_is_set, OP_EQ, 1); \
  93. tt_mem_op(srm_answer, OP_EQ, answer, sizeof(answer)-1); \
  94. } else { \
  95. tt_int_op(srm_answer_is_set, OP_EQ, 0); \
  96. } \
  97. tt_int_op(srm_ttl, OP_EQ, ttl); \
  98. tt_i64_op(srm_expires, OP_EQ, expires); \
  99. } while (0)
  100. (void)arg;
  101. MOCK(connection_mark_unattached_ap_, mark_unattached_mock);
  102. MOCK(connection_ap_handshake_socks_resolved, socks_resolved_mock);
  103. options->ClientDNSRejectInternalAddresses = 0;
  104. SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
  105. "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
  106. /* IPv4: 18.0.0.1, ttl 512 */
  107. "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00"
  108. /* IPv6: 2003::3, ttl 1024 */
  109. "\x06\x10"
  110. "\x20\x02\x00\x00\x00\x00\x00\x00"
  111. "\x00\x00\x00\x00\x00\x00\x00\x03"
  112. "\x00\x00\x04\x00");
  113. entryconn = entry_connection_new(CONN_TYPE_AP, AF_INET);
  114. edgeconn = ENTRY_TO_EDGE_CONN(entryconn);
  115. /* Try with connection in non-RESOLVE_WAIT state: cell gets ignored */
  116. MOCK_RESET();
  117. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  118. tt_int_op(r, OP_EQ, 0);
  119. tt_int_op(srm_ncalls, OP_EQ, 0);
  120. tt_int_op(mum_ncalls, OP_EQ, 0);
  121. /* Now put it in the right state. */
  122. ENTRY_TO_CONN(entryconn)->state = AP_CONN_STATE_RESOLVE_WAIT;
  123. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  124. entryconn->entry_cfg.ipv4_traffic = 1;
  125. entryconn->entry_cfg.ipv6_traffic = 1;
  126. entryconn->entry_cfg.prefer_ipv6 = 0;
  127. /* We prefer ipv4, so we should get the first ipv4 answer */
  128. MOCK_RESET();
  129. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  130. tt_int_op(r, OP_EQ, 0);
  131. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  132. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  133. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x7f\x00\x01\x02", 256, -1);
  134. /* But we may be discarding private answers. */
  135. MOCK_RESET();
  136. options->ClientDNSRejectInternalAddresses = 1;
  137. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  138. tt_int_op(r, OP_EQ, 0);
  139. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  140. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  141. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x12\x00\x00\x01", 512, -1);
  142. /* now prefer ipv6, and get the first ipv6 answer */
  143. entryconn->entry_cfg.prefer_ipv6 = 1;
  144. MOCK_RESET();
  145. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  146. tt_int_op(r, OP_EQ, 0);
  147. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  148. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  149. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV6,
  150. "\x20\x02\x00\x00\x00\x00\x00\x00"
  151. "\x00\x00\x00\x00\x00\x00\x00\x03",
  152. 1024, -1);
  153. /* With a cell that only has IPv4, we report IPv4 even if we prefer IPv6 */
  154. MOCK_RESET();
  155. SET_CELL("\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
  156. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  157. tt_int_op(r, OP_EQ, 0);
  158. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  159. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  160. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_IPV4, "\x12\x00\x00\x01", 512, -1);
  161. /* But if we don't allow IPv4, we report nothing if the cell contains only
  162. * ipv4 */
  163. MOCK_RESET();
  164. entryconn->entry_cfg.ipv4_traffic = 0;
  165. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  166. tt_int_op(r, OP_EQ, 0);
  167. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  168. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  169. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR, NULL, -1, -1);
  170. /* If we wanted hostnames, we report nothing, since we only had IPs. */
  171. MOCK_RESET();
  172. entryconn->entry_cfg.ipv4_traffic = 1;
  173. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  174. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  175. tt_int_op(r, OP_EQ, 0);
  176. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  177. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  178. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR, NULL, -1, -1);
  179. /* A hostname cell is fine though. */
  180. MOCK_RESET();
  181. SET_CELL("\x00\x0fwww.example.com\x00\x01\x00\x00");
  182. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  183. tt_int_op(r, OP_EQ, 0);
  184. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  185. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  186. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_HOSTNAME, "www.example.com", 65536, -1);
  187. /* error on malformed cell */
  188. MOCK_RESET();
  189. entryconn->socks_request->command = SOCKS_COMMAND_RESOLVE;
  190. SET_CELL("\x04\x04\x01\x02\x03\x04"); /* no ttl */
  191. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  192. tt_int_op(r, OP_EQ, 0);
  193. ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL);
  194. tt_int_op(srm_ncalls, OP_EQ, 0);
  195. /* error on all addresses private */
  196. MOCK_RESET();
  197. SET_CELL(/* IPv4: 127.0.1.2, ttl 256 */
  198. "\x04\x04\x7f\x00\x01\x02\x00\x00\x01\x00"
  199. /* IPv4: 192.168.1.1, ttl 256 */
  200. "\x04\x04\xc0\xa8\x01\x01\x00\x00\x01\x00");
  201. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  202. tt_int_op(r, OP_EQ, 0);
  203. ASSERT_MARK_CALLED(END_STREAM_REASON_TORPROTOCOL);
  204. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT, NULL, 0, TIME_MAX);
  205. /* Legit error code */
  206. MOCK_RESET();
  207. SET_CELL("\xf0\x15" "quiet and meaningless" "\x00\x00\x0f\xff");
  208. r = connection_edge_process_resolved_cell(edgeconn, &cell, &rh);
  209. tt_int_op(r, OP_EQ, 0);
  210. ASSERT_MARK_CALLED(END_STREAM_REASON_DONE|
  211. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  212. ASSERT_RESOLVED_CALLED(RESOLVED_TYPE_ERROR_TRANSIENT, NULL, -1, -1);
  213. done:
  214. UNMOCK(connection_mark_unattached_ap_);
  215. UNMOCK(connection_ap_handshake_socks_resolved);
  216. }
  217. struct testcase_t relaycell_tests[] = {
  218. { "resolved", test_relaycell_resolved, TT_FORK, NULL, NULL },
  219. END_OF_TESTCASES
  220. };