test_entryconn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /* Copyright (c) 2014-2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define CONNECTION_PRIVATE
  5. #define CONNECTION_EDGE_PRIVATE
  6. #include "or.h"
  7. #include "test.h"
  8. #include "addressmap.h"
  9. #include "config.h"
  10. #include "confparse.h"
  11. #include "connection.h"
  12. #include "connection_edge.h"
  13. static void *
  14. entryconn_rewrite_setup(const struct testcase_t *tc)
  15. {
  16. (void)tc;
  17. entry_connection_t *ec = entry_connection_new(CONN_TYPE_AP, AF_INET);
  18. addressmap_init();
  19. return ec;
  20. }
  21. static int
  22. entryconn_rewrite_teardown(const struct testcase_t *tc, void *arg)
  23. {
  24. (void)tc;
  25. entry_connection_t *ec = arg;
  26. if (ec)
  27. connection_free_(ENTRY_TO_CONN(ec));
  28. addressmap_free_all();
  29. return 1;
  30. }
  31. static struct testcase_setup_t test_rewrite_setup = {
  32. entryconn_rewrite_setup, entryconn_rewrite_teardown
  33. };
  34. /* Simple rewrite: no changes needed */
  35. static void
  36. test_entryconn_rewrite_basic(void *arg)
  37. {
  38. entry_connection_t *ec = arg;
  39. rewrite_result_t rr;
  40. tt_assert(ec->socks_request);
  41. strlcpy(ec->socks_request->address, "www.TORproject.org",
  42. sizeof(ec->socks_request->address));
  43. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  44. connection_ap_handshake_rewrite(ec, &rr);
  45. tt_int_op(rr.should_close, OP_EQ, 0);
  46. tt_int_op(rr.end_reason, OP_EQ, 0);
  47. tt_int_op(rr.automap, OP_EQ, 0);
  48. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  49. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  50. tt_str_op(rr.orig_address, OP_EQ, "www.torproject.org");
  51. tt_str_op(ec->socks_request->address, OP_EQ, "www.torproject.org");
  52. tt_str_op(ec->original_dest_address, OP_EQ, "www.torproject.org");
  53. done:
  54. ;
  55. }
  56. /* Rewrite but reject because of disallowed .exit */
  57. static void
  58. test_entryconn_rewrite_bad_dotexit(void *arg)
  59. {
  60. entry_connection_t *ec = arg;
  61. rewrite_result_t rr;
  62. get_options_mutable()->AllowDotExit = 0;
  63. tt_assert(ec->socks_request);
  64. strlcpy(ec->socks_request->address, "www.TORproject.org.foo.exit",
  65. sizeof(ec->socks_request->address));
  66. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  67. connection_ap_handshake_rewrite(ec, &rr);
  68. tt_int_op(rr.should_close, OP_EQ, 1);
  69. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_TORPROTOCOL);
  70. done:
  71. ;
  72. }
  73. /* Automap on resolve, connect to automapped address, resolve again and get
  74. * same answer. (IPv4) */
  75. static void
  76. test_entryconn_rewrite_automap_ipv4(void *arg)
  77. {
  78. entry_connection_t *ec = arg;
  79. entry_connection_t *ec2=NULL, *ec3=NULL;
  80. rewrite_result_t rr;
  81. char *msg = NULL;
  82. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  83. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  84. get_options_mutable()->AutomapHostsOnResolve = 1;
  85. get_options_mutable()->AutomapHostsSuffixes = smartlist_new();
  86. smartlist_add(get_options_mutable()->AutomapHostsSuffixes, tor_strdup("."));
  87. parse_virtual_addr_network("127.202.0.0/16", AF_INET, 0, &msg);
  88. /* Automap this on resolve. */
  89. strlcpy(ec->socks_request->address, "WWW.MIT.EDU",
  90. sizeof(ec->socks_request->address));
  91. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  92. connection_ap_handshake_rewrite(ec, &rr);
  93. tt_int_op(rr.automap, OP_EQ, 1);
  94. tt_int_op(rr.should_close, OP_EQ, 0);
  95. tt_int_op(rr.end_reason, OP_EQ, 0);
  96. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  97. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  98. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  99. tt_str_op(ec->original_dest_address, OP_EQ, "www.mit.edu");
  100. tt_assert(!strcmpstart(ec->socks_request->address,"127.202."));
  101. /* Connect to it and make sure we get the original address back. */
  102. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  103. sizeof(ec2->socks_request->address));
  104. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  105. connection_ap_handshake_rewrite(ec2, &rr);
  106. tt_int_op(rr.automap, OP_EQ, 0);
  107. tt_int_op(rr.should_close, OP_EQ, 0);
  108. tt_int_op(rr.end_reason, OP_EQ, 0);
  109. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  110. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  111. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  112. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  113. tt_str_op(ec2->socks_request->address, OP_EQ, "www.mit.edu");
  114. /* Resolve it again, make sure the answer is the same. */
  115. strlcpy(ec3->socks_request->address, "www.MIT.EDU",
  116. sizeof(ec3->socks_request->address));
  117. ec3->socks_request->command = SOCKS_COMMAND_RESOLVE;
  118. connection_ap_handshake_rewrite(ec3, &rr);
  119. tt_int_op(rr.automap, OP_EQ, 1);
  120. tt_int_op(rr.should_close, OP_EQ, 0);
  121. tt_int_op(rr.end_reason, OP_EQ, 0);
  122. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  123. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  124. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  125. tt_str_op(ec3->original_dest_address, OP_EQ, "www.mit.edu");
  126. tt_str_op(ec3->socks_request->address, OP_EQ,
  127. ec->socks_request->address);
  128. done:
  129. connection_free_(ENTRY_TO_CONN(ec2));
  130. connection_free_(ENTRY_TO_CONN(ec3));
  131. }
  132. /* Automap on resolve, connect to automapped address, resolve again and get
  133. * same answer. (IPv6) */
  134. static void
  135. test_entryconn_rewrite_automap_ipv6(void *arg)
  136. {
  137. (void)arg;
  138. entry_connection_t *ec =NULL;
  139. entry_connection_t *ec2=NULL, *ec3=NULL;
  140. rewrite_result_t rr;
  141. char *msg = NULL;
  142. ec = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  143. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  144. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  145. get_options_mutable()->AutomapHostsOnResolve = 1;
  146. get_options_mutable()->AutomapHostsSuffixes = smartlist_new();
  147. smartlist_add(get_options_mutable()->AutomapHostsSuffixes, tor_strdup("."));
  148. parse_virtual_addr_network("FE80::/32", AF_INET6, 0, &msg);
  149. /* Automap this on resolve. */
  150. strlcpy(ec->socks_request->address, "WWW.MIT.EDU",
  151. sizeof(ec->socks_request->address));
  152. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  153. connection_ap_handshake_rewrite(ec, &rr);
  154. tt_int_op(rr.automap, OP_EQ, 1);
  155. tt_int_op(rr.should_close, OP_EQ, 0);
  156. tt_int_op(rr.end_reason, OP_EQ, 0);
  157. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  158. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  159. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  160. tt_str_op(ec->original_dest_address, OP_EQ, "www.mit.edu");
  161. /* Yes, this [ should be here. */
  162. tt_assert(!strcmpstart(ec->socks_request->address,"[fe80:"));
  163. /* Connect to it and make sure we get the original address back. */
  164. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  165. sizeof(ec2->socks_request->address));
  166. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  167. connection_ap_handshake_rewrite(ec2, &rr);
  168. tt_int_op(rr.automap, OP_EQ, 0);
  169. tt_int_op(rr.should_close, OP_EQ, 0);
  170. tt_int_op(rr.end_reason, OP_EQ, 0);
  171. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  172. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  173. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  174. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  175. tt_str_op(ec2->socks_request->address, OP_EQ, "www.mit.edu");
  176. /* Resolve it again, make sure the answer is the same. */
  177. strlcpy(ec3->socks_request->address, "www.MIT.EDU",
  178. sizeof(ec3->socks_request->address));
  179. ec3->socks_request->command = SOCKS_COMMAND_RESOLVE;
  180. connection_ap_handshake_rewrite(ec3, &rr);
  181. tt_int_op(rr.automap, OP_EQ, 1);
  182. tt_int_op(rr.should_close, OP_EQ, 0);
  183. tt_int_op(rr.end_reason, OP_EQ, 0);
  184. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  185. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  186. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  187. tt_str_op(ec3->original_dest_address, OP_EQ, "www.mit.edu");
  188. tt_str_op(ec3->socks_request->address, OP_EQ,
  189. ec->socks_request->address);
  190. done:
  191. connection_free_(ENTRY_TO_CONN(ec));
  192. connection_free_(ENTRY_TO_CONN(ec2));
  193. connection_free_(ENTRY_TO_CONN(ec3));
  194. }
  195. #if 0
  196. /* FFFF not actually supported. */
  197. /* automap on resolve, reverse lookup. */
  198. static void
  199. test_entryconn_rewrite_automap_reverse(void *arg)
  200. {
  201. entry_connection_t *ec = arg;
  202. entry_connection_t *ec2=NULL;
  203. rewrite_result_t rr;
  204. char *msg = NULL;
  205. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  206. get_options_mutable()->AutomapHostsOnResolve = 1;
  207. get_options_mutable()->AutomapHostsSuffixes = smartlist_new();
  208. get_options_mutable()->SafeLogging_ = SAFELOG_SCRUB_NONE;
  209. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  210. tor_strdup(".bloom"));
  211. parse_virtual_addr_network("127.80.0.0/16", AF_INET, 0, &msg);
  212. /* Automap this on resolve. */
  213. strlcpy(ec->socks_request->address, "www.poldy.BLOOM",
  214. sizeof(ec->socks_request->address));
  215. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  216. connection_ap_handshake_rewrite(ec, &rr);
  217. tt_int_op(rr.automap, OP_EQ, 1);
  218. tt_int_op(rr.should_close, OP_EQ, 0);
  219. tt_int_op(rr.end_reason, OP_EQ, 0);
  220. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  221. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  222. tt_str_op(rr.orig_address, OP_EQ, "www.poldy.bloom");
  223. tt_str_op(ec->original_dest_address, OP_EQ, "www.poldy.bloom");
  224. tt_assert(!strcmpstart(ec->socks_request->address,"127.80."));
  225. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  226. sizeof(ec2->socks_request->address));
  227. ec2->use_cached_ipv4_answers = 1; // XXXX REMOVE. This is only there to hide a bug.
  228. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  229. connection_ap_handshake_rewrite(ec2, &rr);
  230. tt_int_op(rr.automap, OP_EQ, 0);
  231. tt_int_op(rr.should_close, OP_EQ, 1);
  232. tt_int_op(rr.end_reason, OP_EQ,
  233. END_STREAM_REASON_DONE|END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  234. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  235. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  236. done:
  237. connection_free_(ENTRY_TO_CONN(ec2));
  238. }
  239. #endif
  240. /* Rewrite because of cached DNS entry. */
  241. static void
  242. test_entryconn_rewrite_cached_dns_ipv4(void *arg)
  243. {
  244. entry_connection_t *ec = arg;
  245. rewrite_result_t rr;
  246. time_t expires = time(NULL) + 3600;
  247. entry_connection_t *ec2=NULL;
  248. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  249. addressmap_register("www.friendly.example.com",
  250. tor_strdup("240.240.241.241"),
  251. expires,
  252. ADDRMAPSRC_DNS,
  253. 0, 0);
  254. strlcpy(ec->socks_request->address, "www.friendly.example.com",
  255. sizeof(ec->socks_request->address));
  256. strlcpy(ec2->socks_request->address, "www.friendly.example.com",
  257. sizeof(ec2->socks_request->address));
  258. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  259. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  260. ec2->use_cached_ipv4_answers = 1; /* only ec2 gets this flag */
  261. connection_ap_handshake_rewrite(ec, &rr);
  262. tt_int_op(rr.automap, OP_EQ, 0);
  263. tt_int_op(rr.should_close, OP_EQ, 0);
  264. tt_int_op(rr.end_reason, OP_EQ, 0);
  265. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  266. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  267. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  268. tt_str_op(ec->socks_request->address, OP_EQ, "www.friendly.example.com");
  269. connection_ap_handshake_rewrite(ec2, &rr);
  270. tt_int_op(rr.automap, OP_EQ, 0);
  271. tt_int_op(rr.should_close, OP_EQ, 0);
  272. tt_int_op(rr.end_reason, OP_EQ, 0);
  273. tt_i64_op(rr.map_expires, OP_EQ, expires);
  274. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  275. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  276. tt_str_op(ec2->socks_request->address, OP_EQ, "240.240.241.241");
  277. done:
  278. connection_free_(ENTRY_TO_CONN(ec2));
  279. }
  280. /* Rewrite because of cached DNS entry. */
  281. static void
  282. test_entryconn_rewrite_cached_dns_ipv6(void *arg)
  283. {
  284. entry_connection_t *ec = NULL;
  285. rewrite_result_t rr;
  286. time_t expires = time(NULL) + 3600;
  287. entry_connection_t *ec2=NULL;
  288. (void)arg;
  289. ec = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  290. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  291. addressmap_register("www.friendly.example.com",
  292. tor_strdup("[::f00f]"),
  293. expires,
  294. ADDRMAPSRC_DNS,
  295. 0, 0);
  296. strlcpy(ec->socks_request->address, "www.friendly.example.com",
  297. sizeof(ec->socks_request->address));
  298. strlcpy(ec2->socks_request->address, "www.friendly.example.com",
  299. sizeof(ec2->socks_request->address));
  300. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  301. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  302. ec2->use_cached_ipv6_answers = 1; /* only ec2 gets this flag */
  303. connection_ap_handshake_rewrite(ec, &rr);
  304. tt_int_op(rr.automap, OP_EQ, 0);
  305. tt_int_op(rr.should_close, OP_EQ, 0);
  306. tt_int_op(rr.end_reason, OP_EQ, 0);
  307. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  308. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  309. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  310. tt_str_op(ec->socks_request->address, OP_EQ, "www.friendly.example.com");
  311. connection_ap_handshake_rewrite(ec2, &rr);
  312. tt_int_op(rr.automap, OP_EQ, 0);
  313. tt_int_op(rr.should_close, OP_EQ, 0);
  314. tt_int_op(rr.end_reason, OP_EQ, 0);
  315. tt_i64_op(rr.map_expires, OP_EQ, expires);
  316. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  317. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  318. tt_str_op(ec2->socks_request->address, OP_EQ, "[::f00f]");
  319. done:
  320. connection_free_(ENTRY_TO_CONN(ec));
  321. connection_free_(ENTRY_TO_CONN(ec2));
  322. }
  323. /* Fail to connect to unmapped address in virtual range. */
  324. static void
  325. test_entryconn_rewrite_unmapped_virtual(void *arg)
  326. {
  327. entry_connection_t *ec = arg;
  328. rewrite_result_t rr;
  329. entry_connection_t *ec2 = NULL;
  330. char *msg = NULL;
  331. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  332. parse_virtual_addr_network("18.202.0.0/16", AF_INET, 0, &msg);
  333. parse_virtual_addr_network("[ABCD::]/16", AF_INET6, 0, &msg);
  334. strlcpy(ec->socks_request->address, "18.202.5.5",
  335. sizeof(ec->socks_request->address));
  336. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  337. connection_ap_handshake_rewrite(ec, &rr);
  338. tt_int_op(rr.should_close, OP_EQ, 1);
  339. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_INTERNAL);
  340. tt_int_op(rr.automap, OP_EQ, 0);
  341. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  342. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  343. strlcpy(ec2->socks_request->address, "[ABCD:9::5314:9543]",
  344. sizeof(ec2->socks_request->address));
  345. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  346. connection_ap_handshake_rewrite(ec2, &rr);
  347. tt_int_op(rr.should_close, OP_EQ, 1);
  348. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_INTERNAL);
  349. tt_int_op(rr.automap, OP_EQ, 0);
  350. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  351. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  352. done:
  353. connection_free_(ENTRY_TO_CONN(ec2));
  354. }
  355. /* Rewrite because of mapaddress option */
  356. static void
  357. test_entryconn_rewrite_mapaddress(void *arg)
  358. {
  359. entry_connection_t *ec = arg;
  360. rewrite_result_t rr;
  361. config_line_append(&get_options_mutable()->AddressMap,
  362. "MapAddress", "meta metaobjects.example");
  363. config_register_addressmaps(get_options());
  364. strlcpy(ec->socks_request->address, "meta",
  365. sizeof(ec->socks_request->address));
  366. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  367. connection_ap_handshake_rewrite(ec, &rr);
  368. tt_int_op(rr.should_close, OP_EQ, 0);
  369. tt_int_op(rr.end_reason, OP_EQ, 0);
  370. tt_int_op(rr.automap, OP_EQ, 0);
  371. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  372. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  373. tt_str_op(ec->socks_request->address, OP_EQ, "metaobjects.example");
  374. done:
  375. ;
  376. }
  377. /* Reject reverse lookups of internal address. */
  378. static void
  379. test_entryconn_rewrite_reject_internal_reverse(void *arg)
  380. {
  381. entry_connection_t *ec = arg;
  382. rewrite_result_t rr;
  383. strlcpy(ec->socks_request->address, "10.0.0.1",
  384. sizeof(ec->socks_request->address));
  385. ec->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  386. connection_ap_handshake_rewrite(ec, &rr);
  387. tt_int_op(rr.should_close, OP_EQ, 1);
  388. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_SOCKSPROTOCOL |
  389. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  390. tt_int_op(rr.automap, OP_EQ, 0);
  391. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  392. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  393. done:
  394. ;
  395. }
  396. /* Rewrite into .exit because of virtual address mapping */
  397. static void
  398. test_entryconn_rewrite_automap_exit(void *arg)
  399. {
  400. entry_connection_t *ec = arg;
  401. entry_connection_t *ec2=NULL;
  402. rewrite_result_t rr;
  403. char *msg = NULL;
  404. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  405. get_options_mutable()->AutomapHostsOnResolve = 1;
  406. get_options_mutable()->AutomapHostsSuffixes = smartlist_new();
  407. get_options_mutable()->AllowDotExit = 1;
  408. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  409. tor_strdup(".EXIT"));
  410. parse_virtual_addr_network("127.1.0.0/16", AF_INET, 0, &msg);
  411. /* Automap this on resolve. */
  412. strlcpy(ec->socks_request->address, "website.example.exit",
  413. sizeof(ec->socks_request->address));
  414. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  415. connection_ap_handshake_rewrite(ec, &rr);
  416. tt_int_op(rr.automap, OP_EQ, 1);
  417. tt_int_op(rr.should_close, OP_EQ, 0);
  418. tt_int_op(rr.end_reason, OP_EQ, 0);
  419. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  420. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  421. tt_str_op(rr.orig_address, OP_EQ, "website.example.exit");
  422. tt_str_op(ec->original_dest_address, OP_EQ, "website.example.exit");
  423. tt_assert(!strcmpstart(ec->socks_request->address,"127.1."));
  424. /* Connect to it and make sure we get the original address back. */
  425. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  426. sizeof(ec2->socks_request->address));
  427. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  428. connection_ap_handshake_rewrite(ec2, &rr);
  429. tt_int_op(rr.automap, OP_EQ, 0);
  430. tt_int_op(rr.should_close, OP_EQ, 0);
  431. tt_int_op(rr.end_reason, OP_EQ, 0);
  432. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  433. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_AUTOMAP);
  434. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  435. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  436. tt_str_op(ec2->socks_request->address, OP_EQ, "website.example.exit");
  437. done:
  438. connection_free_(ENTRY_TO_CONN(ec2));
  439. }
  440. /* Rewrite into .exit because of mapaddress */
  441. static void
  442. test_entryconn_rewrite_mapaddress_exit(void *arg)
  443. {
  444. entry_connection_t *ec = arg;
  445. rewrite_result_t rr;
  446. config_line_append(&get_options_mutable()->AddressMap,
  447. "MapAddress", "*.example.com *.example.com.abc.exit");
  448. config_register_addressmaps(get_options());
  449. /* Automap this on resolve. */
  450. strlcpy(ec->socks_request->address, "abc.example.com",
  451. sizeof(ec->socks_request->address));
  452. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  453. connection_ap_handshake_rewrite(ec, &rr);
  454. tt_int_op(rr.automap, OP_EQ, 0);
  455. tt_int_op(rr.should_close, OP_EQ, 0);
  456. tt_int_op(rr.end_reason, OP_EQ, 0);
  457. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  458. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_TORRC);
  459. tt_str_op(rr.orig_address, OP_EQ, "abc.example.com");
  460. tt_str_op(ec->socks_request->address, OP_EQ, "abc.example.com.abc.exit");
  461. done:
  462. ;
  463. }
  464. /* Map foo.onion to longthing.onion, and also automap. */
  465. static void
  466. test_entryconn_rewrite_mapaddress_automap_onion(void *arg)
  467. {
  468. entry_connection_t *ec = arg;
  469. entry_connection_t *ec2 = NULL;
  470. entry_connection_t *ec3 = NULL;
  471. entry_connection_t *ec4 = NULL;
  472. rewrite_result_t rr;
  473. char *msg = NULL;
  474. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  475. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  476. ec4 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  477. get_options_mutable()->AutomapHostsOnResolve = 1;
  478. get_options_mutable()->AutomapHostsSuffixes = smartlist_new();
  479. get_options_mutable()->AllowDotExit = 1;
  480. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  481. tor_strdup(".onion"));
  482. parse_virtual_addr_network("192.168.0.0/16", AF_INET, 0, &msg);
  483. config_line_append(&get_options_mutable()->AddressMap,
  484. "MapAddress", "foo.onion abcdefghijklmnop.onion");
  485. config_register_addressmaps(get_options());
  486. /* Connect to foo.onion. */
  487. strlcpy(ec->socks_request->address, "foo.onion",
  488. sizeof(ec->socks_request->address));
  489. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  490. connection_ap_handshake_rewrite(ec, &rr);
  491. tt_int_op(rr.automap, OP_EQ, 0);
  492. tt_int_op(rr.should_close, OP_EQ, 0);
  493. tt_int_op(rr.end_reason, OP_EQ, 0);
  494. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  495. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  496. tt_str_op(rr.orig_address, OP_EQ, "foo.onion");
  497. tt_str_op(ec->socks_request->address, OP_EQ, "abcdefghijklmnop.onion");
  498. /* Okay, resolve foo.onion */
  499. strlcpy(ec2->socks_request->address, "foo.onion",
  500. sizeof(ec2->socks_request->address));
  501. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE;
  502. connection_ap_handshake_rewrite(ec2, &rr);
  503. tt_int_op(rr.automap, OP_EQ, 1);
  504. tt_int_op(rr.should_close, OP_EQ, 0);
  505. tt_int_op(rr.end_reason, OP_EQ, 0);
  506. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  507. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  508. tt_str_op(rr.orig_address, OP_EQ, "foo.onion");
  509. tt_assert(!strcmpstart(ec2->socks_request->address, "192.168."));
  510. /* Now connect */
  511. strlcpy(ec3->socks_request->address, ec2->socks_request->address,
  512. sizeof(ec3->socks_request->address));
  513. ec3->socks_request->command = SOCKS_COMMAND_CONNECT;
  514. connection_ap_handshake_rewrite(ec3, &rr);
  515. tt_int_op(rr.automap, OP_EQ, 0);
  516. tt_int_op(rr.should_close, OP_EQ, 0);
  517. tt_int_op(rr.end_reason, OP_EQ, 0);
  518. tt_assert(!strcmpstart(ec3->socks_request->address, "abcdefghijklmnop.onion"));
  519. /* Now resolve abcefghijklmnop.onion. */
  520. strlcpy(ec4->socks_request->address, "abcdefghijklmnop.onion",
  521. sizeof(ec4->socks_request->address));
  522. ec4->socks_request->command = SOCKS_COMMAND_RESOLVE;
  523. connection_ap_handshake_rewrite(ec4, &rr);
  524. tt_int_op(rr.automap, OP_EQ, 1);
  525. tt_int_op(rr.should_close, OP_EQ, 0);
  526. tt_int_op(rr.end_reason, OP_EQ, 0);
  527. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  528. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  529. tt_str_op(rr.orig_address, OP_EQ, "abcdefghijklmnop.onion");
  530. tt_assert(!strcmpstart(ec4->socks_request->address, "192.168."));
  531. /* XXXX doesn't work
  532. tt_str_op(ec4->socks_request->address, OP_EQ, ec2->socks_request->address);
  533. */
  534. done:
  535. connection_free_(ENTRY_TO_CONN(ec2));
  536. connection_free_(ENTRY_TO_CONN(ec3));
  537. connection_free_(ENTRY_TO_CONN(ec4));
  538. }
  539. #define REWRITE(name) \
  540. { #name, test_entryconn_##name, TT_FORK, &test_rewrite_setup, NULL }
  541. struct testcase_t entryconn_tests[] = {
  542. REWRITE(rewrite_basic),
  543. REWRITE(rewrite_bad_dotexit),
  544. REWRITE(rewrite_automap_ipv4),
  545. REWRITE(rewrite_automap_ipv6),
  546. // REWRITE(rewrite_automap_reverse),
  547. REWRITE(rewrite_cached_dns_ipv4),
  548. REWRITE(rewrite_cached_dns_ipv6),
  549. REWRITE(rewrite_unmapped_virtual),
  550. REWRITE(rewrite_mapaddress),
  551. REWRITE(rewrite_reject_internal_reverse),
  552. REWRITE(rewrite_automap_exit),
  553. REWRITE(rewrite_mapaddress_exit),
  554. REWRITE(rewrite_mapaddress_automap_onion),
  555. END_OF_TESTCASES
  556. };