test_pt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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 *arg)
  28. {
  29. char line[200];
  30. transport_t *transport = NULL;
  31. tor_addr_t test_addr;
  32. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  33. (void)arg;
  34. mp->conf_state = PT_PROTO_INFANT;
  35. mp->transports = smartlist_new();
  36. /* incomplete cmethod */
  37. strlcpy(line,"CMETHOD trebuchet",sizeof(line));
  38. tt_assert(parse_cmethod_line(line, mp) < 0);
  39. reset_mp(mp);
  40. /* wrong proxy type */
  41. strlcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999",sizeof(line));
  42. tt_assert(parse_cmethod_line(line, mp) < 0);
  43. reset_mp(mp);
  44. /* wrong addrport */
  45. strlcpy(line,"CMETHOD trebuchet socks4 abcd",sizeof(line));
  46. tt_assert(parse_cmethod_line(line, mp) < 0);
  47. reset_mp(mp);
  48. /* correct line */
  49. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  50. tt_assert(parse_cmethod_line(line, mp) == 0);
  51. tt_assert(smartlist_len(mp->transports) == 1);
  52. transport = smartlist_get(mp->transports, 0);
  53. /* test registered address of transport */
  54. tor_addr_parse(&test_addr, "127.0.0.1");
  55. tt_assert(tor_addr_eq(&test_addr, &transport->addr));
  56. /* test registered port of transport */
  57. tt_assert(transport->port == 1999);
  58. /* test registered SOCKS version of transport */
  59. tt_assert(transport->socks_version == PROXY_SOCKS5);
  60. /* test registered name of transport */
  61. tt_str_op(transport->name,OP_EQ, "trebuchet");
  62. reset_mp(mp);
  63. /* incomplete smethod */
  64. strlcpy(line,"SMETHOD trebuchet",sizeof(line));
  65. tt_assert(parse_smethod_line(line, mp) < 0);
  66. reset_mp(mp);
  67. /* wrong addr type */
  68. strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
  69. tt_assert(parse_smethod_line(line, mp) < 0);
  70. reset_mp(mp);
  71. /* cowwect */
  72. strlcpy(line,"SMETHOD trebuchy 127.0.0.2:2999",sizeof(line));
  73. tt_assert(parse_smethod_line(line, mp) == 0);
  74. tt_assert(smartlist_len(mp->transports) == 1);
  75. transport = smartlist_get(mp->transports, 0);
  76. /* test registered address of transport */
  77. tor_addr_parse(&test_addr, "127.0.0.2");
  78. tt_assert(tor_addr_eq(&test_addr, &transport->addr));
  79. /* test registered port of transport */
  80. tt_assert(transport->port == 2999);
  81. /* test registered name of transport */
  82. tt_str_op(transport->name,OP_EQ, "trebuchy");
  83. reset_mp(mp);
  84. /* Include some arguments. Good ones. */
  85. strlcpy(line,"SMETHOD trebuchet 127.0.0.1:9999 "
  86. "ARGS:counterweight=3,sling=snappy",
  87. sizeof(line));
  88. tt_assert(parse_smethod_line(line, mp) == 0);
  89. tt_int_op(1, OP_EQ, smartlist_len(mp->transports));
  90. {
  91. const transport_t *transport_ = smartlist_get(mp->transports, 0);
  92. tt_assert(transport_);
  93. tt_str_op(transport_->name, OP_EQ, "trebuchet");
  94. tt_int_op(transport_->port, OP_EQ, 9999);
  95. tt_str_op(fmt_addr(&transport_->addr), OP_EQ, "127.0.0.1");
  96. tt_str_op(transport_->extra_info_args, OP_EQ,
  97. "counterweight=3,sling=snappy");
  98. }
  99. reset_mp(mp);
  100. /* unsupported version */
  101. strlcpy(line,"VERSION 666",sizeof(line));
  102. tt_assert(parse_version(line, mp) < 0);
  103. /* incomplete VERSION */
  104. strlcpy(line,"VERSION ",sizeof(line));
  105. tt_assert(parse_version(line, mp) < 0);
  106. /* correct VERSION */
  107. strlcpy(line,"VERSION 1",sizeof(line));
  108. tt_assert(parse_version(line, mp) == 0);
  109. done:
  110. reset_mp(mp);
  111. smartlist_free(mp->transports);
  112. tor_free(mp);
  113. }
  114. static void
  115. test_pt_get_transport_options(void *arg)
  116. {
  117. char **execve_args;
  118. smartlist_t *transport_list = smartlist_new();
  119. managed_proxy_t *mp;
  120. or_options_t *options = get_options_mutable();
  121. char *opt_str = NULL;
  122. config_line_t *cl = NULL;
  123. (void)arg;
  124. execve_args = tor_malloc(sizeof(char*)*2);
  125. execve_args[0] = tor_strdup("cheeseshop");
  126. execve_args[1] = NULL;
  127. mp = managed_proxy_create(transport_list, execve_args, 1);
  128. tt_ptr_op(mp, OP_NE, NULL);
  129. opt_str = get_transport_options_for_server_proxy(mp);
  130. tt_ptr_op(opt_str, OP_EQ, NULL);
  131. smartlist_add(mp->transports_to_launch, tor_strdup("gruyere"));
  132. smartlist_add(mp->transports_to_launch, tor_strdup("roquefort"));
  133. smartlist_add(mp->transports_to_launch, tor_strdup("stnectaire"));
  134. tt_assert(options);
  135. cl = tor_malloc_zero(sizeof(config_line_t));
  136. cl->value = tor_strdup("gruyere melty=10 hardness=se;ven");
  137. options->ServerTransportOptions = cl;
  138. cl = tor_malloc_zero(sizeof(config_line_t));
  139. cl->value = tor_strdup("stnectaire melty=4 hardness=three");
  140. cl->next = options->ServerTransportOptions;
  141. options->ServerTransportOptions = cl;
  142. cl = tor_malloc_zero(sizeof(config_line_t));
  143. cl->value = tor_strdup("pepperjack melty=12 hardness=five");
  144. cl->next = options->ServerTransportOptions;
  145. options->ServerTransportOptions = cl;
  146. opt_str = get_transport_options_for_server_proxy(mp);
  147. tt_str_op(opt_str, OP_EQ,
  148. "gruyere:melty=10;gruyere:hardness=se\\;ven;"
  149. "stnectaire:melty=4;stnectaire:hardness=three");
  150. done:
  151. tor_free(opt_str);
  152. config_free_lines(cl);
  153. managed_proxy_destroy(mp, 0);
  154. smartlist_free(transport_list);
  155. }
  156. static void
  157. test_pt_protocol(void *arg)
  158. {
  159. char line[200];
  160. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  161. (void)arg;
  162. mp->conf_state = PT_PROTO_LAUNCHED;
  163. mp->transports = smartlist_new();
  164. mp->argv = tor_calloc(2, sizeof(char *));
  165. mp->argv[0] = tor_strdup("<testcase>");
  166. /* various wrong protocol runs: */
  167. strlcpy(line,"VERSION 1",sizeof(line));
  168. handle_proxy_line(line, mp);
  169. tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  170. strlcpy(line,"VERSION 1",sizeof(line));
  171. handle_proxy_line(line, mp);
  172. tt_assert(mp->conf_state == PT_PROTO_BROKEN);
  173. reset_mp(mp);
  174. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  175. handle_proxy_line(line, mp);
  176. tt_assert(mp->conf_state == PT_PROTO_BROKEN);
  177. reset_mp(mp);
  178. /* correct protocol run: */
  179. strlcpy(line,"VERSION 1",sizeof(line));
  180. handle_proxy_line(line, mp);
  181. tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  182. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  183. handle_proxy_line(line, mp);
  184. tt_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  185. strlcpy(line,"CMETHODS DONE",sizeof(line));
  186. handle_proxy_line(line, mp);
  187. tt_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  188. done:
  189. reset_mp(mp);
  190. smartlist_free(mp->transports);
  191. tor_free(mp->argv[0]);
  192. tor_free(mp->argv);
  193. tor_free(mp);
  194. }
  195. static void
  196. test_pt_get_extrainfo_string(void *arg)
  197. {
  198. managed_proxy_t *mp1 = NULL, *mp2 = NULL;
  199. char **argv1, **argv2;
  200. smartlist_t *t1 = smartlist_new(), *t2 = smartlist_new();
  201. int r;
  202. char *s = NULL;
  203. (void) arg;
  204. argv1 = tor_malloc_zero(sizeof(char*)*3);
  205. argv1[0] = tor_strdup("ewige");
  206. argv1[1] = tor_strdup("Blumenkraft");
  207. argv1[2] = NULL;
  208. argv2 = tor_malloc_zero(sizeof(char*)*4);
  209. argv2[0] = tor_strdup("und");
  210. argv2[1] = tor_strdup("ewige");
  211. argv2[2] = tor_strdup("Schlangenkraft");
  212. argv2[3] = NULL;
  213. mp1 = managed_proxy_create(t1, argv1, 1);
  214. mp2 = managed_proxy_create(t2, argv2, 1);
  215. r = parse_smethod_line("SMETHOD hagbard 127.0.0.1:5555", mp1);
  216. tt_int_op(r, OP_EQ, 0);
  217. r = parse_smethod_line("SMETHOD celine 127.0.0.1:1723 ARGS:card=no-enemy",
  218. mp2);
  219. tt_int_op(r, OP_EQ, 0);
  220. /* Force these proxies to look "completed" or they won't generate output. */
  221. mp1->conf_state = mp2->conf_state = PT_PROTO_COMPLETED;
  222. s = pt_get_extra_info_descriptor_string();
  223. tt_assert(s);
  224. tt_str_op(s, OP_EQ,
  225. "transport hagbard 127.0.0.1:5555\n"
  226. "transport celine 127.0.0.1:1723 card=no-enemy\n");
  227. done:
  228. /* XXXX clean up better */
  229. smartlist_free(t1);
  230. smartlist_free(t2);
  231. tor_free(s);
  232. }
  233. #ifdef _WIN32
  234. #define STDIN_HANDLE HANDLE
  235. #else
  236. #define STDIN_HANDLE FILE
  237. #endif
  238. static smartlist_t *
  239. tor_get_lines_from_handle_replacement(STDIN_HANDLE *handle,
  240. enum stream_status *stream_status_out)
  241. {
  242. static int times_called = 0;
  243. smartlist_t *retval_sl = smartlist_new();
  244. (void) handle;
  245. (void) stream_status_out;
  246. /* Generate some dummy CMETHOD lines the first 5 times. The 6th
  247. time, send 'CMETHODS DONE' to finish configuring the proxy. */
  248. if (times_called++ != 5) {
  249. smartlist_add_asprintf(retval_sl, "SMETHOD mock%d 127.0.0.1:555%d",
  250. times_called, times_called);
  251. } else {
  252. smartlist_add(retval_sl, tor_strdup("SMETHODS DONE"));
  253. }
  254. return retval_sl;
  255. }
  256. /* NOP mock */
  257. static void
  258. tor_process_handle_destroy_replacement(process_handle_t *process_handle,
  259. int also_terminate_process)
  260. {
  261. (void) process_handle;
  262. (void) also_terminate_process;
  263. }
  264. static or_state_t *dummy_state = NULL;
  265. static or_state_t *
  266. get_or_state_replacement(void)
  267. {
  268. return dummy_state;
  269. }
  270. static int controlevent_n = 0;
  271. static uint16_t controlevent_event = 0;
  272. static smartlist_t *controlevent_msgs = NULL;
  273. static void
  274. queue_control_event_string_replacement(uint16_t event, char *msg)
  275. {
  276. ++controlevent_n;
  277. controlevent_event = event;
  278. if (!controlevent_msgs)
  279. controlevent_msgs = smartlist_new();
  280. smartlist_add(controlevent_msgs, 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(queue_control_event_string,
  297. queue_control_event_string_replacement);
  298. control_testing_set_global_event_mask(EVENT_TRANSPORT_LAUNCHED);
  299. mp = tor_malloc_zero(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. tt_int_op(retval, OP_EQ, 0);
  314. /* check the number of registered transports */
  315. tt_assert(smartlist_len(mp->transports) == i+1);
  316. /* check that the mp is still waiting for transports */
  317. tt_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. tt_int_op(retval, OP_EQ, 1);
  323. /* check the mp state */
  324. tt_assert(mp->conf_state == PT_PROTO_COMPLETED);
  325. tt_int_op(controlevent_n, OP_EQ, 5);
  326. tt_int_op(controlevent_event, OP_EQ, EVENT_TRANSPORT_LAUNCHED);
  327. tt_int_op(smartlist_len(controlevent_msgs), OP_EQ, 5);
  328. smartlist_sort_strings(controlevent_msgs);
  329. tt_str_op(smartlist_get(controlevent_msgs, 0), OP_EQ,
  330. "650 TRANSPORT_LAUNCHED server mock1 127.0.0.1 5551\r\n");
  331. tt_str_op(smartlist_get(controlevent_msgs, 1), OP_EQ,
  332. "650 TRANSPORT_LAUNCHED server mock2 127.0.0.1 5552\r\n");
  333. tt_str_op(smartlist_get(controlevent_msgs, 2), OP_EQ,
  334. "650 TRANSPORT_LAUNCHED server mock3 127.0.0.1 5553\r\n");
  335. tt_str_op(smartlist_get(controlevent_msgs, 3), OP_EQ,
  336. "650 TRANSPORT_LAUNCHED server mock4 127.0.0.1 5554\r\n");
  337. tt_str_op(smartlist_get(controlevent_msgs, 4), OP_EQ,
  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. tt_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, OP_EQ, "mock1");
  353. tt_str_op(bindaddr, OP_EQ, "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(queue_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 = tor_strdup("192.0.2.1:1080");
  391. ret = tor_addr_port_lookup(options->Socks4Proxy,
  392. &options->Socks4ProxyAddr,
  393. &options->Socks4ProxyPort);
  394. tt_int_op(ret, OP_EQ, 0);
  395. uri = get_pt_proxy_uri();
  396. tt_str_op(uri, OP_EQ, "socks4a://192.0.2.1:1080");
  397. tor_free(uri);
  398. tor_free(options->Socks4Proxy);
  399. /* Test with a SOCKS5 proxy, no username/password. */
  400. options->Socks5Proxy = tor_strdup("192.0.2.1:1080");
  401. ret = tor_addr_port_lookup(options->Socks5Proxy,
  402. &options->Socks5ProxyAddr,
  403. &options->Socks5ProxyPort);
  404. tt_int_op(ret, OP_EQ, 0);
  405. uri = get_pt_proxy_uri();
  406. tt_str_op(uri, OP_EQ, "socks5://192.0.2.1:1080");
  407. tor_free(uri);
  408. /* Test with a SOCKS5 proxy, with username/password. */
  409. options->Socks5ProxyUsername = tor_strdup("hwest");
  410. options->Socks5ProxyPassword = tor_strdup("r34n1m470r");
  411. uri = get_pt_proxy_uri();
  412. tt_str_op(uri, OP_EQ, "socks5://hwest:r34n1m470r@192.0.2.1:1080");
  413. tor_free(uri);
  414. tor_free(options->Socks5Proxy);
  415. tor_free(options->Socks5ProxyUsername);
  416. tor_free(options->Socks5ProxyPassword);
  417. /* Test with a HTTPS proxy, no authenticator. */
  418. options->HTTPSProxy = tor_strdup("192.0.2.1:80");
  419. ret = tor_addr_port_lookup(options->HTTPSProxy,
  420. &options->HTTPSProxyAddr,
  421. &options->HTTPSProxyPort);
  422. tt_int_op(ret, OP_EQ, 0);
  423. uri = get_pt_proxy_uri();
  424. tt_str_op(uri, OP_EQ, "http://192.0.2.1:80");
  425. tor_free(uri);
  426. /* Test with a HTTPS proxy, with authenticator. */
  427. options->HTTPSProxyAuthenticator = tor_strdup("hwest:r34n1m470r");
  428. uri = get_pt_proxy_uri();
  429. tt_str_op(uri, OP_EQ, "http://hwest:r34n1m470r@192.0.2.1:80");
  430. tor_free(uri);
  431. tor_free(options->HTTPSProxy);
  432. tor_free(options->HTTPSProxyAuthenticator);
  433. /* Token nod to the fact that IPv6 exists. */
  434. options->Socks4Proxy = tor_strdup("[2001:db8::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://[2001:db8::1]:1080");
  441. tor_free(uri);
  442. tor_free(options->Socks4Proxy);
  443. done:
  444. if (uri)
  445. tor_free(uri);
  446. }
  447. #define PT_LEGACY(name) \
  448. { #name, test_pt_ ## name , 0, NULL, NULL }
  449. struct testcase_t pt_tests[] = {
  450. PT_LEGACY(parsing),
  451. PT_LEGACY(protocol),
  452. { "get_transport_options", test_pt_get_transport_options, TT_FORK,
  453. NULL, NULL },
  454. { "get_extrainfo_string", test_pt_get_extrainfo_string, TT_FORK,
  455. NULL, NULL },
  456. { "configure_proxy",test_pt_configure_proxy, TT_FORK,
  457. NULL, NULL },
  458. { "get_pt_proxy_uri", test_get_pt_proxy_uri, TT_FORK,
  459. NULL, NULL },
  460. END_OF_TESTCASES
  461. };