test_pt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define PT_PRIVATE
  7. #define UTIL_PRIVATE
  8. #define STATEFILE_PRIVATE
  9. #define CONTROL_EVENTS_PRIVATE
  10. #define PROCESS_PRIVATE
  11. #include "core/or/or.h"
  12. #include "app/config/config.h"
  13. #include "lib/confmgt/confparse.h"
  14. #include "feature/control/control.h"
  15. #include "feature/control/control_events.h"
  16. #include "feature/client/transports.h"
  17. #include "core/or/circuitbuild.h"
  18. #include "app/config/statefile.h"
  19. #include "test/test.h"
  20. #include "lib/encoding/confline.h"
  21. #include "lib/net/resolve.h"
  22. #include "lib/process/process.h"
  23. #include "app/config/or_state_st.h"
  24. #include "test/log_test_helpers.h"
  25. static void
  26. reset_mp(managed_proxy_t *mp)
  27. {
  28. mp->conf_state = PT_PROTO_LAUNCHED;
  29. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  30. smartlist_clear(mp->transports);
  31. }
  32. static void
  33. test_pt_parsing(void *arg)
  34. {
  35. char line[200];
  36. transport_t *transport = NULL;
  37. tor_addr_t test_addr;
  38. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  39. (void)arg;
  40. mp->conf_state = PT_PROTO_INFANT;
  41. mp->transports = smartlist_new();
  42. /* incomplete cmethod */
  43. strlcpy(line,"CMETHOD trebuchet",sizeof(line));
  44. tt_int_op(parse_cmethod_line(line, mp), OP_LT, 0);
  45. reset_mp(mp);
  46. /* wrong proxy type */
  47. strlcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999",sizeof(line));
  48. tt_int_op(parse_cmethod_line(line, mp), OP_LT, 0);
  49. reset_mp(mp);
  50. /* wrong addrport */
  51. strlcpy(line,"CMETHOD trebuchet socks4 abcd",sizeof(line));
  52. tt_int_op(parse_cmethod_line(line, mp), OP_LT, 0);
  53. reset_mp(mp);
  54. /* correct line */
  55. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  56. tt_int_op(parse_cmethod_line(line, mp), OP_EQ, 0);
  57. tt_int_op(smartlist_len(mp->transports), OP_EQ, 1);
  58. transport = smartlist_get(mp->transports, 0);
  59. /* test registered address of transport */
  60. tor_addr_parse(&test_addr, "127.0.0.1");
  61. tt_assert(tor_addr_eq(&test_addr, &transport->addr));
  62. /* test registered port of transport */
  63. tt_uint_op(transport->port, OP_EQ, 1999);
  64. /* test registered SOCKS version of transport */
  65. tt_int_op(transport->socks_version, OP_EQ, PROXY_SOCKS5);
  66. /* test registered name of transport */
  67. tt_str_op(transport->name,OP_EQ, "trebuchet");
  68. reset_mp(mp);
  69. /* incomplete smethod */
  70. strlcpy(line,"SMETHOD trebuchet",sizeof(line));
  71. tt_int_op(parse_smethod_line(line, mp), OP_LT, 0);
  72. reset_mp(mp);
  73. /* wrong addr type */
  74. strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
  75. tt_int_op(parse_smethod_line(line, mp), OP_LT, 0);
  76. reset_mp(mp);
  77. /* cowwect */
  78. strlcpy(line,"SMETHOD trebuchy 127.0.0.2:2999",sizeof(line));
  79. tt_int_op(parse_smethod_line(line, mp), OP_EQ, 0);
  80. tt_int_op(smartlist_len(mp->transports), OP_EQ, 1);
  81. transport = smartlist_get(mp->transports, 0);
  82. /* test registered address of transport */
  83. tor_addr_parse(&test_addr, "127.0.0.2");
  84. tt_assert(tor_addr_eq(&test_addr, &transport->addr));
  85. /* test registered port of transport */
  86. tt_uint_op(transport->port, OP_EQ, 2999);
  87. /* test registered name of transport */
  88. tt_str_op(transport->name,OP_EQ, "trebuchy");
  89. reset_mp(mp);
  90. /* Include some arguments. Good ones. */
  91. strlcpy(line,"SMETHOD trebuchet 127.0.0.1:9999 "
  92. "ARGS:counterweight=3,sling=snappy",
  93. sizeof(line));
  94. tt_int_op(parse_smethod_line(line, mp), OP_EQ, 0);
  95. tt_int_op(1, OP_EQ, smartlist_len(mp->transports));
  96. {
  97. const transport_t *transport_ = smartlist_get(mp->transports, 0);
  98. tt_assert(transport_);
  99. tt_str_op(transport_->name, OP_EQ, "trebuchet");
  100. tt_int_op(transport_->port, OP_EQ, 9999);
  101. tt_str_op(fmt_addr(&transport_->addr), OP_EQ, "127.0.0.1");
  102. tt_str_op(transport_->extra_info_args, OP_EQ,
  103. "counterweight=3,sling=snappy");
  104. }
  105. reset_mp(mp);
  106. /* unsupported version */
  107. strlcpy(line,"VERSION 666",sizeof(line));
  108. tt_int_op(parse_version(line, mp), OP_LT, 0);
  109. /* incomplete VERSION */
  110. strlcpy(line,"VERSION ",sizeof(line));
  111. tt_int_op(parse_version(line, mp), OP_LT, 0);
  112. /* correct VERSION */
  113. strlcpy(line,"VERSION 1",sizeof(line));
  114. tt_int_op(parse_version(line, mp), OP_EQ, 0);
  115. done:
  116. reset_mp(mp);
  117. smartlist_free(mp->transports);
  118. tor_free(mp);
  119. }
  120. static void
  121. test_pt_get_transport_options(void *arg)
  122. {
  123. char **execve_args;
  124. smartlist_t *transport_list = smartlist_new();
  125. managed_proxy_t *mp;
  126. or_options_t *options = get_options_mutable();
  127. char *opt_str = NULL;
  128. config_line_t *cl = NULL;
  129. (void)arg;
  130. execve_args = tor_malloc(sizeof(char*)*2);
  131. execve_args[0] = tor_strdup("cheeseshop");
  132. execve_args[1] = NULL;
  133. mp = managed_proxy_create(transport_list, execve_args, 1);
  134. tt_ptr_op(mp, OP_NE, NULL);
  135. opt_str = get_transport_options_for_server_proxy(mp);
  136. tt_ptr_op(opt_str, OP_EQ, NULL);
  137. smartlist_add_strdup(mp->transports_to_launch, "gruyere");
  138. smartlist_add_strdup(mp->transports_to_launch, "roquefort");
  139. smartlist_add_strdup(mp->transports_to_launch, "stnectaire");
  140. tt_assert(options);
  141. cl = tor_malloc_zero(sizeof(config_line_t));
  142. cl->value = tor_strdup("gruyere melty=10 hardness=se;ven");
  143. options->ServerTransportOptions = cl;
  144. cl = tor_malloc_zero(sizeof(config_line_t));
  145. cl->value = tor_strdup("stnectaire melty=4 hardness=three");
  146. cl->next = options->ServerTransportOptions;
  147. options->ServerTransportOptions = cl;
  148. cl = tor_malloc_zero(sizeof(config_line_t));
  149. cl->value = tor_strdup("pepperjack melty=12 hardness=five");
  150. cl->next = options->ServerTransportOptions;
  151. options->ServerTransportOptions = cl;
  152. opt_str = get_transport_options_for_server_proxy(mp);
  153. tt_str_op(opt_str, OP_EQ,
  154. "gruyere:melty=10;gruyere:hardness=se\\;ven;"
  155. "stnectaire:melty=4;stnectaire:hardness=three");
  156. done:
  157. tor_free(opt_str);
  158. config_free_lines(cl);
  159. managed_proxy_destroy(mp, 0);
  160. smartlist_free(transport_list);
  161. }
  162. static void
  163. test_pt_protocol(void *arg)
  164. {
  165. char line[200];
  166. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  167. (void)arg;
  168. mp->conf_state = PT_PROTO_LAUNCHED;
  169. mp->transports = smartlist_new();
  170. mp->argv = tor_calloc(2, sizeof(char *));
  171. mp->argv[0] = tor_strdup("<testcase>");
  172. /* various wrong protocol runs: */
  173. strlcpy(line,"VERSION 1",sizeof(line));
  174. handle_proxy_line(line, mp);
  175. tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  176. strlcpy(line,"VERSION 1",sizeof(line));
  177. handle_proxy_line(line, mp);
  178. tt_assert(mp->conf_state == PT_PROTO_BROKEN);
  179. reset_mp(mp);
  180. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  181. handle_proxy_line(line, mp);
  182. tt_assert(mp->conf_state == PT_PROTO_BROKEN);
  183. reset_mp(mp);
  184. /* correct protocol run: */
  185. strlcpy(line,"VERSION 1",sizeof(line));
  186. handle_proxy_line(line, mp);
  187. tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  188. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  189. handle_proxy_line(line, mp);
  190. tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  191. strlcpy(line,"CMETHODS DONE",sizeof(line));
  192. handle_proxy_line(line, mp);
  193. tt_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  194. done:
  195. reset_mp(mp);
  196. smartlist_free(mp->transports);
  197. tor_free(mp->argv[0]);
  198. tor_free(mp->argv);
  199. tor_free(mp);
  200. }
  201. static void
  202. test_pt_get_extrainfo_string(void *arg)
  203. {
  204. managed_proxy_t *mp1 = NULL, *mp2 = NULL;
  205. char **argv1, **argv2;
  206. smartlist_t *t1 = smartlist_new(), *t2 = smartlist_new();
  207. int r;
  208. char *s = NULL;
  209. (void) arg;
  210. argv1 = tor_malloc_zero(sizeof(char*)*3);
  211. argv1[0] = tor_strdup("ewige");
  212. argv1[1] = tor_strdup("Blumenkraft");
  213. argv1[2] = NULL;
  214. argv2 = tor_malloc_zero(sizeof(char*)*4);
  215. argv2[0] = tor_strdup("und");
  216. argv2[1] = tor_strdup("ewige");
  217. argv2[2] = tor_strdup("Schlangenkraft");
  218. argv2[3] = NULL;
  219. mp1 = managed_proxy_create(t1, argv1, 1);
  220. mp2 = managed_proxy_create(t2, argv2, 1);
  221. r = parse_smethod_line("SMETHOD hagbard 127.0.0.1:5555", mp1);
  222. tt_int_op(r, OP_EQ, 0);
  223. r = parse_smethod_line("SMETHOD celine 127.0.0.1:1723 ARGS:card=no-enemy",
  224. mp2);
  225. tt_int_op(r, OP_EQ, 0);
  226. /* Force these proxies to look "completed" or they won't generate output. */
  227. mp1->conf_state = mp2->conf_state = PT_PROTO_COMPLETED;
  228. s = pt_get_extra_info_descriptor_string();
  229. tt_assert(s);
  230. tt_str_op(s, OP_EQ,
  231. "transport hagbard 127.0.0.1:5555\n"
  232. "transport celine 127.0.0.1:1723 card=no-enemy\n");
  233. done:
  234. /* XXXX clean up better */
  235. smartlist_free(t1);
  236. smartlist_free(t2);
  237. tor_free(s);
  238. }
  239. static int
  240. process_read_stdout_replacement(process_t *process, buf_t *buffer)
  241. {
  242. (void)process;
  243. static int times_called = 0;
  244. /* Generate some dummy CMETHOD lines the first 5 times. The 6th
  245. time, send 'CMETHODS DONE' to finish configuring the proxy. */
  246. times_called++;
  247. if (times_called <= 5) {
  248. buf_add_printf(buffer, "SMETHOD mock%d 127.0.0.1:555%d\n",
  249. times_called, times_called);
  250. } else if (times_called <= 6) {
  251. buf_add_string(buffer, "SMETHODS DONE\n");
  252. } else if (times_called <= 7) {
  253. buf_add_string(buffer, "LOG SEVERITY=error MESSAGE=\"Oh noes, something "
  254. "bad happened. What do we do!?\"\n");
  255. buf_add_string(buffer, "LOG SEVERITY=warning MESSAGE=\"warning msg\"\n");
  256. buf_add_string(buffer, "LOG SEVERITY=notice MESSAGE=\"notice msg\"\n");
  257. buf_add_string(buffer, "LOG SEVERITY=info MESSAGE=\"info msg\"\n");
  258. buf_add_string(buffer, "LOG SEVERITY=debug MESSAGE=\"debug msg\"\n");
  259. } else if (times_called <= 8) {
  260. buf_add_string(buffer, "STATUS TRANSPORT=a K_1=a K_2=b K_3=\"foo bar\"\n");
  261. buf_add_string(buffer, "STATUS TRANSPORT=b K_1=a K_2=b K_3=\"foo bar\"\n");
  262. buf_add_string(buffer, "STATUS TRANSPORT=c K_1=a K_2=b K_3=\"foo bar\"\n");
  263. }
  264. return (int)buf_datalen(buffer);
  265. }
  266. static or_state_t *dummy_state = NULL;
  267. static or_state_t *
  268. get_or_state_replacement(void)
  269. {
  270. return dummy_state;
  271. }
  272. static int controlevent_n = 0;
  273. static uint16_t controlevent_event = 0;
  274. static smartlist_t *controlevent_msgs = NULL;
  275. static void
  276. queue_control_event_string_replacement(uint16_t event, char *msg)
  277. {
  278. ++controlevent_n;
  279. controlevent_event = event;
  280. if (!controlevent_msgs)
  281. controlevent_msgs = smartlist_new();
  282. smartlist_add(controlevent_msgs, msg);
  283. }
  284. /* Test the configure_proxy() function. */
  285. static void
  286. test_pt_configure_proxy(void *arg)
  287. {
  288. int i, retval;
  289. managed_proxy_t *mp = NULL;
  290. (void) arg;
  291. dummy_state = or_state_new();
  292. MOCK(process_read_stdout, process_read_stdout_replacement);
  293. MOCK(get_or_state,
  294. get_or_state_replacement);
  295. MOCK(queue_control_event_string,
  296. queue_control_event_string_replacement);
  297. control_testing_set_global_event_mask(EVENT_TRANSPORT_LAUNCHED);
  298. mp = tor_malloc_zero(sizeof(managed_proxy_t));
  299. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  300. mp->transports = smartlist_new();
  301. mp->transports_to_launch = smartlist_new();
  302. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  303. mp->argv[0] = tor_strdup("<testcase>");
  304. mp->is_server = 1;
  305. /* Configure the process. */
  306. mp->process = process_new("");
  307. process_set_stdout_read_callback(mp->process, managed_proxy_stdout_callback);
  308. process_set_data(mp->process, mp);
  309. /* Test the return value of configure_proxy() by calling it some
  310. times while it is uninitialized and then finally finalizing its
  311. configuration. */
  312. for (i = 0 ; i < 5 ; i++) {
  313. /* force a read from our mocked stdout reader. */
  314. process_notify_event_stdout(mp->process);
  315. /* try to configure our proxy. */
  316. retval = configure_proxy(mp);
  317. /* retval should be zero because proxy hasn't finished configuring yet */
  318. tt_int_op(retval, OP_EQ, 0);
  319. /* check the number of registered transports */
  320. tt_int_op(smartlist_len(mp->transports), OP_EQ, i+1);
  321. /* check that the mp is still waiting for transports */
  322. tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  323. }
  324. /* Get the SMETHOD DONE written to the process. */
  325. process_notify_event_stdout(mp->process);
  326. /* this last configure_proxy() should finalize the proxy configuration. */
  327. retval = configure_proxy(mp);
  328. /* retval should be 1 since the proxy finished configuring */
  329. tt_int_op(retval, OP_EQ, 1);
  330. /* check the mp state */
  331. tt_assert(mp->conf_state == PT_PROTO_COMPLETED);
  332. tt_int_op(controlevent_n, OP_EQ, 5);
  333. tt_int_op(controlevent_event, OP_EQ, EVENT_TRANSPORT_LAUNCHED);
  334. tt_int_op(smartlist_len(controlevent_msgs), OP_EQ, 5);
  335. smartlist_sort_strings(controlevent_msgs);
  336. tt_str_op(smartlist_get(controlevent_msgs, 0), OP_EQ,
  337. "650 TRANSPORT_LAUNCHED server mock1 127.0.0.1 5551\r\n");
  338. tt_str_op(smartlist_get(controlevent_msgs, 1), OP_EQ,
  339. "650 TRANSPORT_LAUNCHED server mock2 127.0.0.1 5552\r\n");
  340. tt_str_op(smartlist_get(controlevent_msgs, 2), OP_EQ,
  341. "650 TRANSPORT_LAUNCHED server mock3 127.0.0.1 5553\r\n");
  342. tt_str_op(smartlist_get(controlevent_msgs, 3), OP_EQ,
  343. "650 TRANSPORT_LAUNCHED server mock4 127.0.0.1 5554\r\n");
  344. tt_str_op(smartlist_get(controlevent_msgs, 4), OP_EQ,
  345. "650 TRANSPORT_LAUNCHED server mock5 127.0.0.1 5555\r\n");
  346. /* Get the log message out. */
  347. setup_full_capture_of_logs(LOG_ERR);
  348. process_notify_event_stdout(mp->process);
  349. expect_single_log_msg_containing("Oh noes, something bad happened");
  350. teardown_capture_of_logs();
  351. tt_int_op(controlevent_n, OP_EQ, 10);
  352. tt_int_op(controlevent_event, OP_EQ, EVENT_PT_LOG);
  353. tt_int_op(smartlist_len(controlevent_msgs), OP_EQ, 10);
  354. tt_str_op(smartlist_get(controlevent_msgs, 5), OP_EQ,
  355. "650 PT_LOG PT=<testcase> SEVERITY=error "
  356. "MESSAGE=\"Oh noes, "
  357. "something bad happened. What do we do!?\"\r\n");
  358. tt_str_op(smartlist_get(controlevent_msgs, 6), OP_EQ,
  359. "650 PT_LOG PT=<testcase> SEVERITY=warning "
  360. "MESSAGE=\"warning msg\"\r\n");
  361. tt_str_op(smartlist_get(controlevent_msgs, 7), OP_EQ,
  362. "650 PT_LOG PT=<testcase> SEVERITY=notice "
  363. "MESSAGE=\"notice msg\"\r\n");
  364. tt_str_op(smartlist_get(controlevent_msgs, 8), OP_EQ,
  365. "650 PT_LOG PT=<testcase> SEVERITY=info "
  366. "MESSAGE=\"info msg\"\r\n");
  367. tt_str_op(smartlist_get(controlevent_msgs, 9), OP_EQ,
  368. "650 PT_LOG PT=<testcase> SEVERITY=debug "
  369. "MESSAGE=\"debug msg\"\r\n");
  370. /* Get the STATUS messages out. */
  371. process_notify_event_stdout(mp->process);
  372. tt_int_op(controlevent_n, OP_EQ, 13);
  373. tt_int_op(controlevent_event, OP_EQ, EVENT_PT_STATUS);
  374. tt_int_op(smartlist_len(controlevent_msgs), OP_EQ, 13);
  375. tt_str_op(smartlist_get(controlevent_msgs, 10), OP_EQ,
  376. "650 PT_STATUS "
  377. "PT=<testcase> TRANSPORT=a K_1=a K_2=b K_3=\"foo bar\"\r\n");
  378. tt_str_op(smartlist_get(controlevent_msgs, 11), OP_EQ,
  379. "650 PT_STATUS "
  380. "PT=<testcase> TRANSPORT=b K_1=a K_2=b K_3=\"foo bar\"\r\n");
  381. tt_str_op(smartlist_get(controlevent_msgs, 12), OP_EQ,
  382. "650 PT_STATUS "
  383. "PT=<testcase> TRANSPORT=c K_1=a K_2=b K_3=\"foo bar\"\r\n");
  384. { /* check that the transport info were saved properly in the tor state */
  385. config_line_t *transport_in_state = NULL;
  386. smartlist_t *transport_info_sl = smartlist_new();
  387. char *name_of_transport = NULL;
  388. char *bindaddr = NULL;
  389. /* Get the bindaddr for "mock1" and check it against the bindaddr
  390. that the mocked tor_get_lines_from_handle() generated. */
  391. transport_in_state = get_transport_in_state_by_name("mock1");
  392. tt_assert(transport_in_state);
  393. smartlist_split_string(transport_info_sl, transport_in_state->value,
  394. NULL, 0, 0);
  395. name_of_transport = smartlist_get(transport_info_sl, 0);
  396. bindaddr = smartlist_get(transport_info_sl, 1);
  397. tt_str_op(name_of_transport, OP_EQ, "mock1");
  398. tt_str_op(bindaddr, OP_EQ, "127.0.0.1:5551");
  399. SMARTLIST_FOREACH(transport_info_sl, char *, cp, tor_free(cp));
  400. smartlist_free(transport_info_sl);
  401. }
  402. done:
  403. teardown_capture_of_logs();
  404. or_state_free(dummy_state);
  405. UNMOCK(process_read_stdout);
  406. UNMOCK(get_or_state);
  407. UNMOCK(queue_control_event_string);
  408. if (controlevent_msgs) {
  409. SMARTLIST_FOREACH(controlevent_msgs, char *, cp, tor_free(cp));
  410. smartlist_free(controlevent_msgs);
  411. controlevent_msgs = NULL;
  412. }
  413. if (mp->transports) {
  414. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  415. smartlist_free(mp->transports);
  416. }
  417. smartlist_free(mp->transports_to_launch);
  418. process_free(mp->process);
  419. tor_free(mp->argv[0]);
  420. tor_free(mp->argv);
  421. tor_free(mp);
  422. }
  423. /* Test the get_pt_proxy_uri() function. */
  424. static void
  425. test_get_pt_proxy_uri(void *arg)
  426. {
  427. or_options_t *options = get_options_mutable();
  428. char *uri = NULL;
  429. int ret;
  430. (void) arg;
  431. /* Test with no proxy. */
  432. uri = get_pt_proxy_uri();
  433. tt_ptr_op(uri, OP_EQ, NULL);
  434. /* Test with a SOCKS4 proxy. */
  435. options->Socks4Proxy = tor_strdup("192.0.2.1:1080");
  436. ret = tor_addr_port_lookup(options->Socks4Proxy,
  437. &options->Socks4ProxyAddr,
  438. &options->Socks4ProxyPort);
  439. tt_int_op(ret, OP_EQ, 0);
  440. uri = get_pt_proxy_uri();
  441. tt_str_op(uri, OP_EQ, "socks4a://192.0.2.1:1080");
  442. tor_free(uri);
  443. tor_free(options->Socks4Proxy);
  444. /* Test with a SOCKS5 proxy, no username/password. */
  445. options->Socks5Proxy = tor_strdup("192.0.2.1:1080");
  446. ret = tor_addr_port_lookup(options->Socks5Proxy,
  447. &options->Socks5ProxyAddr,
  448. &options->Socks5ProxyPort);
  449. tt_int_op(ret, OP_EQ, 0);
  450. uri = get_pt_proxy_uri();
  451. tt_str_op(uri, OP_EQ, "socks5://192.0.2.1:1080");
  452. tor_free(uri);
  453. /* Test with a SOCKS5 proxy, with username/password. */
  454. options->Socks5ProxyUsername = tor_strdup("hwest");
  455. options->Socks5ProxyPassword = tor_strdup("r34n1m470r");
  456. uri = get_pt_proxy_uri();
  457. tt_str_op(uri, OP_EQ, "socks5://hwest:r34n1m470r@192.0.2.1:1080");
  458. tor_free(uri);
  459. tor_free(options->Socks5Proxy);
  460. tor_free(options->Socks5ProxyUsername);
  461. tor_free(options->Socks5ProxyPassword);
  462. /* Test with a HTTPS proxy, no authenticator. */
  463. options->HTTPSProxy = tor_strdup("192.0.2.1:80");
  464. ret = tor_addr_port_lookup(options->HTTPSProxy,
  465. &options->HTTPSProxyAddr,
  466. &options->HTTPSProxyPort);
  467. tt_int_op(ret, OP_EQ, 0);
  468. uri = get_pt_proxy_uri();
  469. tt_str_op(uri, OP_EQ, "http://192.0.2.1:80");
  470. tor_free(uri);
  471. /* Test with a HTTPS proxy, with authenticator. */
  472. options->HTTPSProxyAuthenticator = tor_strdup("hwest:r34n1m470r");
  473. uri = get_pt_proxy_uri();
  474. tt_str_op(uri, OP_EQ, "http://hwest:r34n1m470r@192.0.2.1:80");
  475. tor_free(uri);
  476. tor_free(options->HTTPSProxy);
  477. tor_free(options->HTTPSProxyAuthenticator);
  478. /* Token nod to the fact that IPv6 exists. */
  479. options->Socks4Proxy = tor_strdup("[2001:db8::1]:1080");
  480. ret = tor_addr_port_lookup(options->Socks4Proxy,
  481. &options->Socks4ProxyAddr,
  482. &options->Socks4ProxyPort);
  483. tt_int_op(ret, OP_EQ, 0);
  484. uri = get_pt_proxy_uri();
  485. tt_str_op(uri, OP_EQ, "socks4a://[2001:db8::1]:1080");
  486. tor_free(uri);
  487. tor_free(options->Socks4Proxy);
  488. done:
  489. if (uri)
  490. tor_free(uri);
  491. }
  492. #define PT_LEGACY(name) \
  493. { #name, test_pt_ ## name , 0, NULL, NULL }
  494. struct testcase_t pt_tests[] = {
  495. PT_LEGACY(parsing),
  496. PT_LEGACY(protocol),
  497. { "get_transport_options", test_pt_get_transport_options, TT_FORK,
  498. NULL, NULL },
  499. { "get_extrainfo_string", test_pt_get_extrainfo_string, TT_FORK,
  500. NULL, NULL },
  501. { "configure_proxy",test_pt_configure_proxy, TT_FORK,
  502. NULL, NULL },
  503. { "get_pt_proxy_uri", test_get_pt_proxy_uri, TT_FORK,
  504. NULL, NULL },
  505. END_OF_TESTCASES
  506. };