test_pt.c 19 KB

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