test_pt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. tor_free(mp);
  110. }
  111. static void
  112. test_pt_get_transport_options(void *arg)
  113. {
  114. char **execve_args;
  115. smartlist_t *transport_list = smartlist_new();
  116. managed_proxy_t *mp;
  117. or_options_t *options = get_options_mutable();
  118. char *opt_str = NULL;
  119. config_line_t *cl = NULL;
  120. (void)arg;
  121. execve_args = tor_malloc(sizeof(char*)*2);
  122. execve_args[0] = tor_strdup("cheeseshop");
  123. execve_args[1] = NULL;
  124. mp = managed_proxy_create(transport_list, execve_args, 1);
  125. tt_ptr_op(mp, !=, NULL);
  126. opt_str = get_transport_options_for_server_proxy(mp);
  127. tt_ptr_op(opt_str, ==, NULL);
  128. smartlist_add(mp->transports_to_launch, tor_strdup("gruyere"));
  129. smartlist_add(mp->transports_to_launch, tor_strdup("roquefort"));
  130. smartlist_add(mp->transports_to_launch, tor_strdup("stnectaire"));
  131. tt_assert(options);
  132. cl = tor_malloc_zero(sizeof(config_line_t));
  133. cl->value = tor_strdup("gruyere melty=10 hardness=se;ven");
  134. options->ServerTransportOptions = cl;
  135. cl = tor_malloc_zero(sizeof(config_line_t));
  136. cl->value = tor_strdup("stnectaire melty=4 hardness=three");
  137. cl->next = options->ServerTransportOptions;
  138. options->ServerTransportOptions = cl;
  139. cl = tor_malloc_zero(sizeof(config_line_t));
  140. cl->value = tor_strdup("pepperjack melty=12 hardness=five");
  141. cl->next = options->ServerTransportOptions;
  142. options->ServerTransportOptions = cl;
  143. opt_str = get_transport_options_for_server_proxy(mp);
  144. tt_str_op(opt_str, ==,
  145. "gruyere:melty=10;gruyere:hardness=se\\;ven;"
  146. "stnectaire:melty=4;stnectaire:hardness=three");
  147. done:
  148. tor_free(opt_str);
  149. config_free_lines(cl);
  150. managed_proxy_destroy(mp, 0);
  151. smartlist_free(transport_list);
  152. }
  153. static void
  154. test_pt_protocol(void)
  155. {
  156. char line[200];
  157. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  158. mp->conf_state = PT_PROTO_LAUNCHED;
  159. mp->transports = smartlist_new();
  160. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  161. mp->argv[0] = tor_strdup("<testcase>");
  162. /* various wrong protocol runs: */
  163. strlcpy(line,"VERSION 1",sizeof(line));
  164. handle_proxy_line(line, mp);
  165. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  166. strlcpy(line,"VERSION 1",sizeof(line));
  167. handle_proxy_line(line, mp);
  168. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  169. reset_mp(mp);
  170. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  171. handle_proxy_line(line, mp);
  172. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  173. reset_mp(mp);
  174. /* correct protocol run: */
  175. strlcpy(line,"VERSION 1",sizeof(line));
  176. handle_proxy_line(line, mp);
  177. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  178. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  179. handle_proxy_line(line, mp);
  180. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  181. strlcpy(line,"CMETHODS DONE",sizeof(line));
  182. handle_proxy_line(line, mp);
  183. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  184. done:
  185. tor_free(mp);
  186. }
  187. static void
  188. test_pt_get_extrainfo_string(void *arg)
  189. {
  190. managed_proxy_t *mp1 = NULL, *mp2 = NULL;
  191. char **argv1, **argv2;
  192. smartlist_t *t1 = smartlist_new(), *t2 = smartlist_new();
  193. int r;
  194. char *s = NULL;
  195. (void) arg;
  196. argv1 = tor_malloc_zero(sizeof(char*)*3);
  197. argv1[0] = tor_strdup("ewige");
  198. argv1[1] = tor_strdup("Blumenkraft");
  199. argv1[2] = NULL;
  200. argv2 = tor_malloc_zero(sizeof(char*)*4);
  201. argv2[0] = tor_strdup("und");
  202. argv2[1] = tor_strdup("ewige");
  203. argv2[2] = tor_strdup("Schlangenkraft");
  204. argv2[3] = NULL;
  205. mp1 = managed_proxy_create(t1, argv1, 1);
  206. mp2 = managed_proxy_create(t2, argv2, 1);
  207. r = parse_smethod_line("SMETHOD hagbard 127.0.0.1:5555", mp1);
  208. tt_int_op(r, ==, 0);
  209. r = parse_smethod_line("SMETHOD celine 127.0.0.1:1723 ARGS:card=no-enemy",
  210. mp2);
  211. tt_int_op(r, ==, 0);
  212. /* Force these proxies to look "completed" or they won't generate output. */
  213. mp1->conf_state = mp2->conf_state = PT_PROTO_COMPLETED;
  214. s = pt_get_extra_info_descriptor_string();
  215. tt_assert(s);
  216. tt_str_op(s, ==,
  217. "transport hagbard 127.0.0.1:5555\n"
  218. "transport celine 127.0.0.1:1723 card=no-enemy\n");
  219. done:
  220. /* XXXX clean up better */
  221. smartlist_free(t1);
  222. smartlist_free(t2);
  223. tor_free(s);
  224. }
  225. #ifdef _WIN32
  226. #define STDIN_HANDLE HANDLE
  227. #else
  228. #define STDIN_HANDLE FILE
  229. #endif
  230. static smartlist_t *
  231. tor_get_lines_from_handle_replacement(STDIN_HANDLE *handle,
  232. enum stream_status *stream_status_out)
  233. {
  234. static int times_called = 0;
  235. smartlist_t *retval_sl = smartlist_new();
  236. (void) handle;
  237. (void) stream_status_out;
  238. /* Generate some dummy CMETHOD lines the first 5 times. The 6th
  239. time, send 'CMETHODS DONE' to finish configuring the proxy. */
  240. if (times_called++ != 5) {
  241. smartlist_add_asprintf(retval_sl, "SMETHOD mock%d 127.0.0.1:555%d",
  242. times_called, times_called);
  243. } else {
  244. smartlist_add(retval_sl, tor_strdup("SMETHODS DONE"));
  245. }
  246. return retval_sl;
  247. }
  248. /* NOP mock */
  249. static void
  250. tor_process_handle_destroy_replacement(process_handle_t *process_handle,
  251. int also_terminate_process)
  252. {
  253. (void) process_handle;
  254. (void) also_terminate_process;
  255. }
  256. static or_state_t *dummy_state = NULL;
  257. static or_state_t *
  258. get_or_state_replacement(void)
  259. {
  260. return dummy_state;
  261. }
  262. static int controlevent_n = 0;
  263. static uint16_t controlevent_event = 0;
  264. static smartlist_t *controlevent_msgs = NULL;
  265. static void
  266. send_control_event_string_replacement(uint16_t event, event_format_t which,
  267. const char *msg)
  268. {
  269. (void) which;
  270. ++controlevent_n;
  271. controlevent_event = event;
  272. if (!controlevent_msgs)
  273. controlevent_msgs = smartlist_new();
  274. smartlist_add(controlevent_msgs, tor_strdup(msg));
  275. }
  276. /* Test the configure_proxy() function. */
  277. static void
  278. test_pt_configure_proxy(void *arg)
  279. {
  280. int i, retval;
  281. managed_proxy_t *mp = NULL;
  282. (void) arg;
  283. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  284. MOCK(tor_get_lines_from_handle,
  285. tor_get_lines_from_handle_replacement);
  286. MOCK(tor_process_handle_destroy,
  287. tor_process_handle_destroy_replacement);
  288. MOCK(get_or_state,
  289. get_or_state_replacement);
  290. MOCK(send_control_event_string,
  291. send_control_event_string_replacement);
  292. control_testing_set_global_event_mask(EVENT_TRANSPORT_LAUNCHED);
  293. mp = tor_malloc(sizeof(managed_proxy_t));
  294. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  295. mp->transports = smartlist_new();
  296. mp->transports_to_launch = smartlist_new();
  297. mp->process_handle = tor_malloc_zero(sizeof(process_handle_t));
  298. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  299. mp->argv[0] = tor_strdup("<testcase>");
  300. mp->is_server = 1;
  301. /* Test the return value of configure_proxy() by calling it some
  302. times while it is uninitialized and then finally finalizing its
  303. configuration. */
  304. for (i = 0 ; i < 5 ; i++) {
  305. retval = configure_proxy(mp);
  306. /* retval should be zero because proxy hasn't finished configuring yet */
  307. test_assert(retval == 0);
  308. /* check the number of registered transports */
  309. test_assert(smartlist_len(mp->transports) == i+1);
  310. /* check that the mp is still waiting for transports */
  311. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  312. }
  313. /* this last configure_proxy() should finalize the proxy configuration. */
  314. retval = configure_proxy(mp);
  315. /* retval should be 1 since the proxy finished configuring */
  316. test_assert(retval == 1);
  317. /* check the mp state */
  318. test_assert(mp->conf_state == PT_PROTO_COMPLETED);
  319. tt_int_op(controlevent_n, ==, 5);
  320. tt_int_op(controlevent_event, ==, EVENT_TRANSPORT_LAUNCHED);
  321. tt_int_op(smartlist_len(controlevent_msgs), ==, 5);
  322. smartlist_sort_strings(controlevent_msgs);
  323. tt_str_op(smartlist_get(controlevent_msgs, 0), ==,
  324. "650 TRANSPORT_LAUNCHED server mock1 127.0.0.1 5551\r\n");
  325. tt_str_op(smartlist_get(controlevent_msgs, 1), ==,
  326. "650 TRANSPORT_LAUNCHED server mock2 127.0.0.1 5552\r\n");
  327. tt_str_op(smartlist_get(controlevent_msgs, 2), ==,
  328. "650 TRANSPORT_LAUNCHED server mock3 127.0.0.1 5553\r\n");
  329. tt_str_op(smartlist_get(controlevent_msgs, 3), ==,
  330. "650 TRANSPORT_LAUNCHED server mock4 127.0.0.1 5554\r\n");
  331. tt_str_op(smartlist_get(controlevent_msgs, 4), ==,
  332. "650 TRANSPORT_LAUNCHED server mock5 127.0.0.1 5555\r\n");
  333. { /* check that the transport info were saved properly in the tor state */
  334. config_line_t *transport_in_state = NULL;
  335. smartlist_t *transport_info_sl = smartlist_new();
  336. char *name_of_transport = NULL;
  337. char *bindaddr = NULL;
  338. /* Get the bindaddr for "mock1" and check it against the bindaddr
  339. that the mocked tor_get_lines_from_handle() generated. */
  340. transport_in_state = get_transport_in_state_by_name("mock1");
  341. test_assert(transport_in_state);
  342. smartlist_split_string(transport_info_sl, transport_in_state->value,
  343. NULL, 0, 0);
  344. name_of_transport = smartlist_get(transport_info_sl, 0);
  345. bindaddr = smartlist_get(transport_info_sl, 1);
  346. tt_str_op(name_of_transport, ==, "mock1");
  347. tt_str_op(bindaddr, ==, "127.0.0.1:5551");
  348. SMARTLIST_FOREACH(transport_info_sl, char *, cp, tor_free(cp));
  349. smartlist_free(transport_info_sl);
  350. }
  351. done:
  352. tor_free(dummy_state);
  353. UNMOCK(tor_get_lines_from_handle);
  354. UNMOCK(tor_process_handle_destroy);
  355. UNMOCK(get_or_state);
  356. UNMOCK(send_control_event_string);
  357. if (controlevent_msgs) {
  358. SMARTLIST_FOREACH(controlevent_msgs, char *, cp, tor_free(cp));
  359. smartlist_free(controlevent_msgs);
  360. controlevent_msgs = NULL;
  361. }
  362. }
  363. #define PT_LEGACY(name) \
  364. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  365. struct testcase_t pt_tests[] = {
  366. PT_LEGACY(parsing),
  367. PT_LEGACY(protocol),
  368. { "get_transport_options", test_pt_get_transport_options, TT_FORK,
  369. NULL, NULL },
  370. { "get_extrainfo_string", test_pt_get_extrainfo_string, TT_FORK,
  371. NULL, NULL },
  372. { "configure_proxy",test_pt_configure_proxy, TT_FORK,
  373. NULL, NULL },
  374. END_OF_TESTCASES
  375. };