test_pt.c 16 KB

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