test_entryconn.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. smartlist_add(get_options_mutable()->AutomapHostsSuffixes, tor_strdup("."));
  86. parse_virtual_addr_network("127.202.0.0/16", AF_INET, 0, &msg);
  87. /* Automap this on resolve. */
  88. strlcpy(ec->socks_request->address, "WWW.MIT.EDU",
  89. sizeof(ec->socks_request->address));
  90. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  91. connection_ap_handshake_rewrite(ec, &rr);
  92. tt_int_op(rr.automap, OP_EQ, 1);
  93. tt_int_op(rr.should_close, OP_EQ, 0);
  94. tt_int_op(rr.end_reason, OP_EQ, 0);
  95. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  96. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  97. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  98. tt_str_op(ec->original_dest_address, OP_EQ, "www.mit.edu");
  99. tt_assert(!strcmpstart(ec->socks_request->address,"127.202."));
  100. /* Connect to it and make sure we get the original address back. */
  101. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  102. sizeof(ec2->socks_request->address));
  103. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  104. connection_ap_handshake_rewrite(ec2, &rr);
  105. tt_int_op(rr.automap, OP_EQ, 0);
  106. tt_int_op(rr.should_close, OP_EQ, 0);
  107. tt_int_op(rr.end_reason, OP_EQ, 0);
  108. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  109. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  110. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  111. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  112. tt_str_op(ec2->socks_request->address, OP_EQ, "www.mit.edu");
  113. /* Resolve it again, make sure the answer is the same. */
  114. strlcpy(ec3->socks_request->address, "www.MIT.EDU",
  115. sizeof(ec3->socks_request->address));
  116. ec3->socks_request->command = SOCKS_COMMAND_RESOLVE;
  117. connection_ap_handshake_rewrite(ec3, &rr);
  118. tt_int_op(rr.automap, OP_EQ, 1);
  119. tt_int_op(rr.should_close, OP_EQ, 0);
  120. tt_int_op(rr.end_reason, OP_EQ, 0);
  121. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  122. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  123. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  124. tt_str_op(ec3->original_dest_address, OP_EQ, "www.mit.edu");
  125. tt_str_op(ec3->socks_request->address, OP_EQ,
  126. ec->socks_request->address);
  127. done:
  128. connection_free_(ENTRY_TO_CONN(ec2));
  129. connection_free_(ENTRY_TO_CONN(ec3));
  130. }
  131. /* Automap on resolve, connect to automapped address, resolve again and get
  132. * same answer. (IPv6) */
  133. static void
  134. test_entryconn_rewrite_automap_ipv6(void *arg)
  135. {
  136. (void)arg;
  137. entry_connection_t *ec =NULL;
  138. entry_connection_t *ec2=NULL, *ec3=NULL;
  139. rewrite_result_t rr;
  140. char *msg = NULL;
  141. ec = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  142. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  143. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  144. get_options_mutable()->AutomapHostsOnResolve = 1;
  145. smartlist_add(get_options_mutable()->AutomapHostsSuffixes, tor_strdup("."));
  146. parse_virtual_addr_network("FE80::/32", AF_INET6, 0, &msg);
  147. /* Automap this on resolve. */
  148. strlcpy(ec->socks_request->address, "WWW.MIT.EDU",
  149. sizeof(ec->socks_request->address));
  150. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  151. connection_ap_handshake_rewrite(ec, &rr);
  152. tt_int_op(rr.automap, OP_EQ, 1);
  153. tt_int_op(rr.should_close, OP_EQ, 0);
  154. tt_int_op(rr.end_reason, OP_EQ, 0);
  155. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  156. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  157. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  158. tt_str_op(ec->original_dest_address, OP_EQ, "www.mit.edu");
  159. /* Yes, this [ should be here. */
  160. tt_assert(!strcmpstart(ec->socks_request->address,"[fe80:"));
  161. /* Connect to it and make sure we get the original address back. */
  162. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  163. sizeof(ec2->socks_request->address));
  164. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  165. connection_ap_handshake_rewrite(ec2, &rr);
  166. tt_int_op(rr.automap, OP_EQ, 0);
  167. tt_int_op(rr.should_close, OP_EQ, 0);
  168. tt_int_op(rr.end_reason, OP_EQ, 0);
  169. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  170. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  171. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  172. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  173. tt_str_op(ec2->socks_request->address, OP_EQ, "www.mit.edu");
  174. /* Resolve it again, make sure the answer is the same. */
  175. strlcpy(ec3->socks_request->address, "www.MIT.EDU",
  176. sizeof(ec3->socks_request->address));
  177. ec3->socks_request->command = SOCKS_COMMAND_RESOLVE;
  178. connection_ap_handshake_rewrite(ec3, &rr);
  179. tt_int_op(rr.automap, OP_EQ, 1);
  180. tt_int_op(rr.should_close, OP_EQ, 0);
  181. tt_int_op(rr.end_reason, OP_EQ, 0);
  182. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  183. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  184. tt_str_op(rr.orig_address, OP_EQ, "www.mit.edu");
  185. tt_str_op(ec3->original_dest_address, OP_EQ, "www.mit.edu");
  186. tt_str_op(ec3->socks_request->address, OP_EQ,
  187. ec->socks_request->address);
  188. done:
  189. connection_free_(ENTRY_TO_CONN(ec));
  190. connection_free_(ENTRY_TO_CONN(ec2));
  191. connection_free_(ENTRY_TO_CONN(ec3));
  192. }
  193. #if 0
  194. /* FFFF not actually supported. */
  195. /* automap on resolve, reverse lookup. */
  196. static void
  197. test_entryconn_rewrite_automap_reverse(void *arg)
  198. {
  199. entry_connection_t *ec = arg;
  200. entry_connection_t *ec2=NULL;
  201. rewrite_result_t rr;
  202. char *msg = NULL;
  203. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  204. get_options_mutable()->AutomapHostsOnResolve = 1;
  205. get_options_mutable()->SafeLogging_ = SAFELOG_SCRUB_NONE;
  206. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  207. tor_strdup(".bloom"));
  208. parse_virtual_addr_network("127.80.0.0/16", AF_INET, 0, &msg);
  209. /* Automap this on resolve. */
  210. strlcpy(ec->socks_request->address, "www.poldy.BLOOM",
  211. sizeof(ec->socks_request->address));
  212. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  213. connection_ap_handshake_rewrite(ec, &rr);
  214. tt_int_op(rr.automap, OP_EQ, 1);
  215. tt_int_op(rr.should_close, OP_EQ, 0);
  216. tt_int_op(rr.end_reason, OP_EQ, 0);
  217. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  218. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  219. tt_str_op(rr.orig_address, OP_EQ, "www.poldy.bloom");
  220. tt_str_op(ec->original_dest_address, OP_EQ, "www.poldy.bloom");
  221. tt_assert(!strcmpstart(ec->socks_request->address,"127.80."));
  222. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  223. sizeof(ec2->socks_request->address));
  224. ec2->use_cached_ipv4_answers = 1; // XXXX REMOVE. This is only there to hide a bug.
  225. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  226. connection_ap_handshake_rewrite(ec2, &rr);
  227. tt_int_op(rr.automap, OP_EQ, 0);
  228. tt_int_op(rr.should_close, OP_EQ, 1);
  229. tt_int_op(rr.end_reason, OP_EQ,
  230. END_STREAM_REASON_DONE|END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  231. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  232. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  233. done:
  234. connection_free_(ENTRY_TO_CONN(ec2));
  235. }
  236. #endif
  237. /* Rewrite because of cached DNS entry. */
  238. static void
  239. test_entryconn_rewrite_cached_dns_ipv4(void *arg)
  240. {
  241. entry_connection_t *ec = arg;
  242. rewrite_result_t rr;
  243. time_t expires = time(NULL) + 3600;
  244. entry_connection_t *ec2=NULL;
  245. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  246. addressmap_register("www.friendly.example.com",
  247. tor_strdup("240.240.241.241"),
  248. expires,
  249. ADDRMAPSRC_DNS,
  250. 0, 0);
  251. strlcpy(ec->socks_request->address, "www.friendly.example.com",
  252. sizeof(ec->socks_request->address));
  253. strlcpy(ec2->socks_request->address, "www.friendly.example.com",
  254. sizeof(ec2->socks_request->address));
  255. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  256. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  257. ec2->use_cached_ipv4_answers = 1; /* only ec2 gets this flag */
  258. connection_ap_handshake_rewrite(ec, &rr);
  259. tt_int_op(rr.automap, OP_EQ, 0);
  260. tt_int_op(rr.should_close, OP_EQ, 0);
  261. tt_int_op(rr.end_reason, OP_EQ, 0);
  262. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  263. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  264. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  265. tt_str_op(ec->socks_request->address, OP_EQ, "www.friendly.example.com");
  266. connection_ap_handshake_rewrite(ec2, &rr);
  267. tt_int_op(rr.automap, OP_EQ, 0);
  268. tt_int_op(rr.should_close, OP_EQ, 0);
  269. tt_int_op(rr.end_reason, OP_EQ, 0);
  270. tt_i64_op(rr.map_expires, OP_EQ, expires);
  271. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  272. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  273. tt_str_op(ec2->socks_request->address, OP_EQ, "240.240.241.241");
  274. done:
  275. connection_free_(ENTRY_TO_CONN(ec2));
  276. }
  277. /* Rewrite because of cached DNS entry. */
  278. static void
  279. test_entryconn_rewrite_cached_dns_ipv6(void *arg)
  280. {
  281. entry_connection_t *ec = NULL;
  282. rewrite_result_t rr;
  283. time_t expires = time(NULL) + 3600;
  284. entry_connection_t *ec2=NULL;
  285. (void)arg;
  286. ec = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  287. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  288. addressmap_register("www.friendly.example.com",
  289. tor_strdup("[::f00f]"),
  290. expires,
  291. ADDRMAPSRC_DNS,
  292. 0, 0);
  293. strlcpy(ec->socks_request->address, "www.friendly.example.com",
  294. sizeof(ec->socks_request->address));
  295. strlcpy(ec2->socks_request->address, "www.friendly.example.com",
  296. sizeof(ec2->socks_request->address));
  297. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  298. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  299. ec2->use_cached_ipv6_answers = 1; /* only ec2 gets this flag */
  300. connection_ap_handshake_rewrite(ec, &rr);
  301. tt_int_op(rr.automap, OP_EQ, 0);
  302. tt_int_op(rr.should_close, OP_EQ, 0);
  303. tt_int_op(rr.end_reason, OP_EQ, 0);
  304. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  305. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  306. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  307. tt_str_op(ec->socks_request->address, OP_EQ, "www.friendly.example.com");
  308. connection_ap_handshake_rewrite(ec2, &rr);
  309. tt_int_op(rr.automap, OP_EQ, 0);
  310. tt_int_op(rr.should_close, OP_EQ, 0);
  311. tt_int_op(rr.end_reason, OP_EQ, 0);
  312. tt_i64_op(rr.map_expires, OP_EQ, expires);
  313. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  314. tt_str_op(rr.orig_address, OP_EQ, "www.friendly.example.com");
  315. tt_str_op(ec2->socks_request->address, OP_EQ, "[::f00f]");
  316. done:
  317. connection_free_(ENTRY_TO_CONN(ec));
  318. connection_free_(ENTRY_TO_CONN(ec2));
  319. }
  320. /* Fail to connect to unmapped address in virtual range. */
  321. static void
  322. test_entryconn_rewrite_unmapped_virtual(void *arg)
  323. {
  324. entry_connection_t *ec = arg;
  325. rewrite_result_t rr;
  326. entry_connection_t *ec2 = NULL;
  327. char *msg = NULL;
  328. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET6);
  329. parse_virtual_addr_network("18.202.0.0/16", AF_INET, 0, &msg);
  330. parse_virtual_addr_network("[ABCD::]/16", AF_INET6, 0, &msg);
  331. strlcpy(ec->socks_request->address, "18.202.5.5",
  332. sizeof(ec->socks_request->address));
  333. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  334. connection_ap_handshake_rewrite(ec, &rr);
  335. tt_int_op(rr.should_close, OP_EQ, 1);
  336. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_INTERNAL);
  337. tt_int_op(rr.automap, OP_EQ, 0);
  338. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  339. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  340. strlcpy(ec2->socks_request->address, "[ABCD:9::5314:9543]",
  341. sizeof(ec2->socks_request->address));
  342. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  343. connection_ap_handshake_rewrite(ec2, &rr);
  344. tt_int_op(rr.should_close, OP_EQ, 1);
  345. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_INTERNAL);
  346. tt_int_op(rr.automap, OP_EQ, 0);
  347. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  348. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  349. done:
  350. connection_free_(ENTRY_TO_CONN(ec2));
  351. }
  352. /* Rewrite because of mapaddress option */
  353. static void
  354. test_entryconn_rewrite_mapaddress(void *arg)
  355. {
  356. entry_connection_t *ec = arg;
  357. rewrite_result_t rr;
  358. config_line_append(&get_options_mutable()->AddressMap,
  359. "MapAddress", "meta metaobjects.example");
  360. config_register_addressmaps(get_options());
  361. strlcpy(ec->socks_request->address, "meta",
  362. sizeof(ec->socks_request->address));
  363. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  364. connection_ap_handshake_rewrite(ec, &rr);
  365. tt_int_op(rr.should_close, OP_EQ, 0);
  366. tt_int_op(rr.end_reason, OP_EQ, 0);
  367. tt_int_op(rr.automap, OP_EQ, 0);
  368. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  369. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  370. tt_str_op(ec->socks_request->address, OP_EQ, "metaobjects.example");
  371. done:
  372. ;
  373. }
  374. /* Reject reverse lookups of internal address. */
  375. static void
  376. test_entryconn_rewrite_reject_internal_reverse(void *arg)
  377. {
  378. entry_connection_t *ec = arg;
  379. rewrite_result_t rr;
  380. strlcpy(ec->socks_request->address, "10.0.0.1",
  381. sizeof(ec->socks_request->address));
  382. ec->socks_request->command = SOCKS_COMMAND_RESOLVE_PTR;
  383. connection_ap_handshake_rewrite(ec, &rr);
  384. tt_int_op(rr.should_close, OP_EQ, 1);
  385. tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_SOCKSPROTOCOL |
  386. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  387. tt_int_op(rr.automap, OP_EQ, 0);
  388. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  389. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  390. done:
  391. ;
  392. }
  393. /* Rewrite into .exit because of virtual address mapping */
  394. static void
  395. test_entryconn_rewrite_automap_exit(void *arg)
  396. {
  397. entry_connection_t *ec = arg;
  398. entry_connection_t *ec2=NULL;
  399. rewrite_result_t rr;
  400. char *msg = NULL;
  401. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  402. get_options_mutable()->AutomapHostsOnResolve = 1;
  403. get_options_mutable()->AllowDotExit = 1;
  404. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  405. tor_strdup(".EXIT"));
  406. parse_virtual_addr_network("127.1.0.0/16", AF_INET, 0, &msg);
  407. /* Automap this on resolve. */
  408. strlcpy(ec->socks_request->address, "website.example.exit",
  409. sizeof(ec->socks_request->address));
  410. ec->socks_request->command = SOCKS_COMMAND_RESOLVE;
  411. connection_ap_handshake_rewrite(ec, &rr);
  412. tt_int_op(rr.automap, OP_EQ, 1);
  413. tt_int_op(rr.should_close, OP_EQ, 0);
  414. tt_int_op(rr.end_reason, OP_EQ, 0);
  415. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  416. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  417. tt_str_op(rr.orig_address, OP_EQ, "website.example.exit");
  418. tt_str_op(ec->original_dest_address, OP_EQ, "website.example.exit");
  419. tt_assert(!strcmpstart(ec->socks_request->address,"127.1."));
  420. /* Connect to it and make sure we get the original address back. */
  421. strlcpy(ec2->socks_request->address, ec->socks_request->address,
  422. sizeof(ec2->socks_request->address));
  423. ec2->socks_request->command = SOCKS_COMMAND_CONNECT;
  424. connection_ap_handshake_rewrite(ec2, &rr);
  425. tt_int_op(rr.automap, OP_EQ, 0);
  426. tt_int_op(rr.should_close, OP_EQ, 0);
  427. tt_int_op(rr.end_reason, OP_EQ, 0);
  428. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  429. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_AUTOMAP);
  430. tt_str_op(rr.orig_address, OP_EQ, ec->socks_request->address);
  431. tt_str_op(ec2->original_dest_address, OP_EQ, ec->socks_request->address);
  432. tt_str_op(ec2->socks_request->address, OP_EQ, "website.example.exit");
  433. done:
  434. connection_free_(ENTRY_TO_CONN(ec2));
  435. }
  436. /* Rewrite into .exit because of mapaddress */
  437. static void
  438. test_entryconn_rewrite_mapaddress_exit(void *arg)
  439. {
  440. entry_connection_t *ec = arg;
  441. rewrite_result_t rr;
  442. config_line_append(&get_options_mutable()->AddressMap,
  443. "MapAddress", "*.example.com *.example.com.abc.exit");
  444. config_register_addressmaps(get_options());
  445. /* Automap this on resolve. */
  446. strlcpy(ec->socks_request->address, "abc.example.com",
  447. sizeof(ec->socks_request->address));
  448. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  449. connection_ap_handshake_rewrite(ec, &rr);
  450. tt_int_op(rr.automap, OP_EQ, 0);
  451. tt_int_op(rr.should_close, OP_EQ, 0);
  452. tt_int_op(rr.end_reason, OP_EQ, 0);
  453. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  454. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_TORRC);
  455. tt_str_op(rr.orig_address, OP_EQ, "abc.example.com");
  456. tt_str_op(ec->socks_request->address, OP_EQ, "abc.example.com.abc.exit");
  457. done:
  458. ;
  459. }
  460. /* Map foo.onion to longthing.onion, and also automap. */
  461. static void
  462. test_entryconn_rewrite_mapaddress_automap_onion(void *arg)
  463. {
  464. entry_connection_t *ec = arg;
  465. entry_connection_t *ec2 = NULL;
  466. entry_connection_t *ec3 = NULL;
  467. entry_connection_t *ec4 = NULL;
  468. rewrite_result_t rr;
  469. char *msg = NULL;
  470. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  471. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  472. ec4 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  473. get_options_mutable()->AutomapHostsOnResolve = 1;
  474. get_options_mutable()->AllowDotExit = 1;
  475. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  476. tor_strdup(".onion"));
  477. parse_virtual_addr_network("192.168.0.0/16", AF_INET, 0, &msg);
  478. config_line_append(&get_options_mutable()->AddressMap,
  479. "MapAddress", "foo.onion abcdefghijklmnop.onion");
  480. config_register_addressmaps(get_options());
  481. /* Connect to foo.onion. */
  482. strlcpy(ec->socks_request->address, "foo.onion",
  483. sizeof(ec->socks_request->address));
  484. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  485. connection_ap_handshake_rewrite(ec, &rr);
  486. tt_int_op(rr.automap, OP_EQ, 0);
  487. tt_int_op(rr.should_close, OP_EQ, 0);
  488. tt_int_op(rr.end_reason, OP_EQ, 0);
  489. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  490. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  491. tt_str_op(rr.orig_address, OP_EQ, "foo.onion");
  492. tt_str_op(ec->socks_request->address, OP_EQ, "abcdefghijklmnop.onion");
  493. /* Okay, resolve foo.onion */
  494. strlcpy(ec2->socks_request->address, "foo.onion",
  495. sizeof(ec2->socks_request->address));
  496. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE;
  497. connection_ap_handshake_rewrite(ec2, &rr);
  498. tt_int_op(rr.automap, OP_EQ, 1);
  499. tt_int_op(rr.should_close, OP_EQ, 0);
  500. tt_int_op(rr.end_reason, OP_EQ, 0);
  501. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  502. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  503. tt_str_op(rr.orig_address, OP_EQ, "foo.onion");
  504. tt_assert(!strcmpstart(ec2->socks_request->address, "192.168."));
  505. /* Now connect */
  506. strlcpy(ec3->socks_request->address, ec2->socks_request->address,
  507. sizeof(ec3->socks_request->address));
  508. ec3->socks_request->command = SOCKS_COMMAND_CONNECT;
  509. connection_ap_handshake_rewrite(ec3, &rr);
  510. tt_int_op(rr.automap, OP_EQ, 0);
  511. tt_int_op(rr.should_close, OP_EQ, 0);
  512. tt_int_op(rr.end_reason, OP_EQ, 0);
  513. tt_assert(!strcmpstart(ec3->socks_request->address, "abcdefghijklmnop.onion"));
  514. /* Now resolve abcefghijklmnop.onion. */
  515. strlcpy(ec4->socks_request->address, "abcdefghijklmnop.onion",
  516. sizeof(ec4->socks_request->address));
  517. ec4->socks_request->command = SOCKS_COMMAND_RESOLVE;
  518. connection_ap_handshake_rewrite(ec4, &rr);
  519. tt_int_op(rr.automap, OP_EQ, 1);
  520. tt_int_op(rr.should_close, OP_EQ, 0);
  521. tt_int_op(rr.end_reason, OP_EQ, 0);
  522. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  523. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  524. tt_str_op(rr.orig_address, OP_EQ, "abcdefghijklmnop.onion");
  525. tt_assert(!strcmpstart(ec4->socks_request->address, "192.168."));
  526. /* XXXX doesn't work
  527. tt_str_op(ec4->socks_request->address, OP_EQ, ec2->socks_request->address);
  528. */
  529. done:
  530. connection_free_(ENTRY_TO_CONN(ec2));
  531. connection_free_(ENTRY_TO_CONN(ec3));
  532. connection_free_(ENTRY_TO_CONN(ec4));
  533. }
  534. static void
  535. test_entryconn_rewrite_mapaddress_automap_onion_common(entry_connection_t *ec,
  536. int map_to_onion,
  537. int map_to_address)
  538. {
  539. entry_connection_t *ec2 = NULL;
  540. entry_connection_t *ec3 = NULL;
  541. rewrite_result_t rr;
  542. ec2 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  543. ec3 = entry_connection_new(CONN_TYPE_AP, AF_INET);
  544. /* Connect to irc.example.com */
  545. strlcpy(ec->socks_request->address, "irc.example.com",
  546. sizeof(ec->socks_request->address));
  547. ec->socks_request->command = SOCKS_COMMAND_CONNECT;
  548. connection_ap_handshake_rewrite(ec, &rr);
  549. tt_int_op(rr.automap, OP_EQ, 0);
  550. tt_int_op(rr.should_close, OP_EQ, 0);
  551. tt_int_op(rr.end_reason, OP_EQ, 0);
  552. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  553. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  554. tt_str_op(rr.orig_address, OP_EQ, "irc.example.com");
  555. tt_str_op(ec->socks_request->address, OP_EQ,
  556. map_to_onion ? "abcdefghijklmnop.onion" : "irc.example.com");
  557. /* Okay, resolve irc.example.com */
  558. strlcpy(ec2->socks_request->address, "irc.example.com",
  559. sizeof(ec2->socks_request->address));
  560. ec2->socks_request->command = SOCKS_COMMAND_RESOLVE;
  561. connection_ap_handshake_rewrite(ec2, &rr);
  562. tt_int_op(rr.automap, OP_EQ, map_to_onion && map_to_address);
  563. tt_int_op(rr.should_close, OP_EQ, 0);
  564. tt_int_op(rr.end_reason, OP_EQ, 0);
  565. tt_i64_op(rr.map_expires, OP_EQ, TIME_MAX);
  566. tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
  567. tt_str_op(rr.orig_address, OP_EQ, "irc.example.com");
  568. if (map_to_onion && map_to_address)
  569. tt_assert(!strcmpstart(ec2->socks_request->address, "192.168."));
  570. /* Now connect */
  571. strlcpy(ec3->socks_request->address, ec2->socks_request->address,
  572. sizeof(ec3->socks_request->address));
  573. ec3->socks_request->command = SOCKS_COMMAND_CONNECT;
  574. connection_ap_handshake_rewrite(ec3, &rr);
  575. tt_int_op(rr.automap, OP_EQ, 0);
  576. tt_int_op(rr.should_close, OP_EQ, 0);
  577. tt_int_op(rr.end_reason, OP_EQ, 0);
  578. if (map_to_onion)
  579. tt_assert(!strcmpstart(ec3->socks_request->address,
  580. "abcdefghijklmnop.onion"));
  581. done:
  582. connection_free_(ENTRY_TO_CONN(ec2));
  583. connection_free_(ENTRY_TO_CONN(ec3));
  584. }
  585. /* This time is the same, but we start with a mapping from a non-onion
  586. * address. */
  587. static void
  588. test_entryconn_rewrite_mapaddress_automap_onion2(void *arg)
  589. {
  590. char *msg = NULL;
  591. get_options_mutable()->AutomapHostsOnResolve = 1;
  592. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  593. tor_strdup(".onion"));
  594. parse_virtual_addr_network("192.168.0.0/16", AF_INET, 0, &msg);
  595. config_line_append(&get_options_mutable()->AddressMap,
  596. "MapAddress", "irc.example.com abcdefghijklmnop.onion");
  597. config_register_addressmaps(get_options());
  598. test_entryconn_rewrite_mapaddress_automap_onion_common(arg, 1, 1);
  599. }
  600. /* Same as above, with automapped turned off */
  601. static void
  602. test_entryconn_rewrite_mapaddress_automap_onion3(void *arg)
  603. {
  604. config_line_append(&get_options_mutable()->AddressMap,
  605. "MapAddress", "irc.example.com abcdefghijklmnop.onion");
  606. config_register_addressmaps(get_options());
  607. test_entryconn_rewrite_mapaddress_automap_onion_common(arg, 1, 0);
  608. }
  609. /* As above, with no mapping. */
  610. static void
  611. test_entryconn_rewrite_mapaddress_automap_onion4(void *arg)
  612. {
  613. char *msg = NULL;
  614. get_options_mutable()->AutomapHostsOnResolve = 1;
  615. smartlist_add(get_options_mutable()->AutomapHostsSuffixes,
  616. tor_strdup(".onion"));
  617. parse_virtual_addr_network("192.168.0.0/16", AF_INET, 0, &msg);
  618. test_entryconn_rewrite_mapaddress_automap_onion_common(arg, 0, 1);
  619. }
  620. #define REWRITE(name) \
  621. { #name, test_entryconn_##name, TT_FORK, &test_rewrite_setup, NULL }
  622. struct testcase_t entryconn_tests[] = {
  623. REWRITE(rewrite_basic),
  624. REWRITE(rewrite_bad_dotexit),
  625. REWRITE(rewrite_automap_ipv4),
  626. REWRITE(rewrite_automap_ipv6),
  627. // REWRITE(rewrite_automap_reverse),
  628. REWRITE(rewrite_cached_dns_ipv4),
  629. REWRITE(rewrite_cached_dns_ipv6),
  630. REWRITE(rewrite_unmapped_virtual),
  631. REWRITE(rewrite_mapaddress),
  632. REWRITE(rewrite_reject_internal_reverse),
  633. REWRITE(rewrite_automap_exit),
  634. REWRITE(rewrite_mapaddress_exit),
  635. REWRITE(rewrite_mapaddress_automap_onion),
  636. REWRITE(rewrite_mapaddress_automap_onion2),
  637. REWRITE(rewrite_mapaddress_automap_onion3),
  638. REWRITE(rewrite_mapaddress_automap_onion4),
  639. END_OF_TESTCASES
  640. };