test_pt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #include "or.h"
  10. #include "config.h"
  11. #include "confparse.h"
  12. #include "transports.h"
  13. #include "circuitbuild.h"
  14. #include "util.h"
  15. #include "statefile.h"
  16. #include "test.h"
  17. static void
  18. reset_mp(managed_proxy_t *mp)
  19. {
  20. mp->conf_state = PT_PROTO_LAUNCHED;
  21. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  22. smartlist_clear(mp->transports);
  23. }
  24. static void
  25. test_pt_parsing(void)
  26. {
  27. char line[200];
  28. transport_t *transport = NULL;
  29. tor_addr_t test_addr;
  30. managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
  31. mp->conf_state = PT_PROTO_INFANT;
  32. mp->transports = smartlist_new();
  33. /* incomplete cmethod */
  34. strlcpy(line,"CMETHOD trebuchet",sizeof(line));
  35. test_assert(parse_cmethod_line(line, mp) < 0);
  36. reset_mp(mp);
  37. /* wrong proxy type */
  38. strlcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999",sizeof(line));
  39. test_assert(parse_cmethod_line(line, mp) < 0);
  40. reset_mp(mp);
  41. /* wrong addrport */
  42. strlcpy(line,"CMETHOD trebuchet socks4 abcd",sizeof(line));
  43. test_assert(parse_cmethod_line(line, mp) < 0);
  44. reset_mp(mp);
  45. /* correct line */
  46. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  47. test_assert(parse_cmethod_line(line, mp) == 0);
  48. test_assert(smartlist_len(mp->transports) == 1);
  49. transport = smartlist_get(mp->transports, 0);
  50. /* test registered address of transport */
  51. tor_addr_parse(&test_addr, "127.0.0.1");
  52. test_assert(tor_addr_eq(&test_addr, &transport->addr));
  53. /* test registered port of transport */
  54. test_assert(transport->port == 1999);
  55. /* test registered SOCKS version of transport */
  56. test_assert(transport->socks_version == PROXY_SOCKS5);
  57. /* test registered name of transport */
  58. test_streq(transport->name, "trebuchet");
  59. reset_mp(mp);
  60. /* incomplete smethod */
  61. strlcpy(line,"SMETHOD trebuchet",sizeof(line));
  62. test_assert(parse_smethod_line(line, mp) < 0);
  63. reset_mp(mp);
  64. /* wrong addr type */
  65. strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
  66. test_assert(parse_smethod_line(line, mp) < 0);
  67. reset_mp(mp);
  68. /* cowwect */
  69. strlcpy(line,"SMETHOD trebuchy 127.0.0.2:2999",sizeof(line));
  70. test_assert(parse_smethod_line(line, mp) == 0);
  71. test_assert(smartlist_len(mp->transports) == 1);
  72. transport = smartlist_get(mp->transports, 0);
  73. /* test registered address of transport */
  74. tor_addr_parse(&test_addr, "127.0.0.2");
  75. test_assert(tor_addr_eq(&test_addr, &transport->addr));
  76. /* test registered port of transport */
  77. test_assert(transport->port == 2999);
  78. /* test registered name of transport */
  79. test_streq(transport->name, "trebuchy");
  80. reset_mp(mp);
  81. /* Include some arguments. Good ones. */
  82. strlcpy(line,"SMETHOD trebuchet 127.0.0.1:9999 "
  83. "ARGS:counterweight=3,sling=snappy",
  84. sizeof(line));
  85. test_assert(parse_smethod_line(line, mp) == 0);
  86. tt_int_op(1, ==, smartlist_len(mp->transports));
  87. {
  88. const transport_t *transport = smartlist_get(mp->transports, 0);
  89. tt_assert(transport);
  90. tt_str_op(transport->name, ==, "trebuchet");
  91. tt_int_op(transport->port, ==, 9999);
  92. tt_str_op(fmt_addr(&transport->addr), ==, "127.0.0.1");
  93. tt_str_op(transport->extra_info_args, ==,
  94. "counterweight=3,sling=snappy");
  95. }
  96. reset_mp(mp);
  97. /* unsupported version */
  98. strlcpy(line,"VERSION 666",sizeof(line));
  99. test_assert(parse_version(line, mp) < 0);
  100. /* incomplete VERSION */
  101. strlcpy(line,"VERSION ",sizeof(line));
  102. test_assert(parse_version(line, mp) < 0);
  103. /* correct VERSION */
  104. strlcpy(line,"VERSION 1",sizeof(line));
  105. test_assert(parse_version(line, mp) == 0);
  106. done:
  107. tor_free(mp);
  108. }
  109. static void
  110. test_pt_get_transport_options(void *arg)
  111. {
  112. char **execve_args;
  113. smartlist_t *transport_list = smartlist_new();
  114. managed_proxy_t *mp;
  115. or_options_t *options = get_options_mutable();
  116. char *opt_str = NULL;
  117. config_line_t *cl = NULL;
  118. (void)arg;
  119. execve_args = tor_malloc(sizeof(char*)*2);
  120. execve_args[0] = tor_strdup("cheeseshop");
  121. execve_args[1] = NULL;
  122. mp = managed_proxy_create(transport_list, execve_args, 1);
  123. tt_ptr_op(mp, !=, NULL);
  124. opt_str = get_transport_options_for_server_proxy(mp);
  125. tt_ptr_op(opt_str, ==, NULL);
  126. smartlist_add(mp->transports_to_launch, tor_strdup("gruyere"));
  127. smartlist_add(mp->transports_to_launch, tor_strdup("roquefort"));
  128. smartlist_add(mp->transports_to_launch, tor_strdup("stnectaire"));
  129. tt_assert(options);
  130. cl = tor_malloc_zero(sizeof(config_line_t));
  131. cl->value = tor_strdup("gruyere melty=10 hardness=se;ven");
  132. options->ServerTransportOptions = cl;
  133. cl = tor_malloc_zero(sizeof(config_line_t));
  134. cl->value = tor_strdup("stnectaire melty=4 hardness=three");
  135. cl->next = options->ServerTransportOptions;
  136. options->ServerTransportOptions = cl;
  137. cl = tor_malloc_zero(sizeof(config_line_t));
  138. cl->value = tor_strdup("pepperjack melty=12 hardness=five");
  139. cl->next = options->ServerTransportOptions;
  140. options->ServerTransportOptions = cl;
  141. opt_str = get_transport_options_for_server_proxy(mp);
  142. tt_str_op(opt_str, ==,
  143. "gruyere:melty=10;gruyere:hardness=se\\;ven;"
  144. "stnectaire:melty=4;stnectaire:hardness=three");
  145. done:
  146. tor_free(opt_str);
  147. config_free_lines(cl);
  148. managed_proxy_destroy(mp, 0);
  149. smartlist_free(transport_list);
  150. }
  151. static void
  152. test_pt_protocol(void)
  153. {
  154. char line[200];
  155. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  156. mp->conf_state = PT_PROTO_LAUNCHED;
  157. mp->transports = smartlist_new();
  158. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  159. mp->argv[0] = tor_strdup("<testcase>");
  160. /* various wrong protocol runs: */
  161. strlcpy(line,"VERSION 1",sizeof(line));
  162. handle_proxy_line(line, mp);
  163. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  164. strlcpy(line,"VERSION 1",sizeof(line));
  165. handle_proxy_line(line, mp);
  166. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  167. reset_mp(mp);
  168. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  169. handle_proxy_line(line, mp);
  170. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  171. reset_mp(mp);
  172. /* correct protocol run: */
  173. strlcpy(line,"VERSION 1",sizeof(line));
  174. handle_proxy_line(line, mp);
  175. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  176. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  177. handle_proxy_line(line, mp);
  178. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  179. strlcpy(line,"CMETHODS DONE",sizeof(line));
  180. handle_proxy_line(line, mp);
  181. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  182. done:
  183. tor_free(mp);
  184. }
  185. static void
  186. test_pt_get_extrainfo_string(void *arg)
  187. {
  188. managed_proxy_t *mp1 = NULL, *mp2 = NULL;
  189. char **argv1, **argv2;
  190. smartlist_t *t1 = smartlist_new(), *t2 = smartlist_new();
  191. int r;
  192. char *s = NULL;
  193. (void) arg;
  194. argv1 = tor_malloc_zero(sizeof(char*)*3);
  195. argv1[0] = tor_strdup("ewige");
  196. argv1[1] = tor_strdup("Blumenkraft");
  197. argv1[2] = NULL;
  198. argv2 = tor_malloc_zero(sizeof(char*)*4);
  199. argv2[0] = tor_strdup("und");
  200. argv2[1] = tor_strdup("ewige");
  201. argv2[2] = tor_strdup("Schlangenkraft");
  202. argv2[3] = NULL;
  203. mp1 = managed_proxy_create(t1, argv1, 1);
  204. mp2 = managed_proxy_create(t2, argv2, 1);
  205. r = parse_smethod_line("SMETHOD hagbard 127.0.0.1:5555", mp1);
  206. tt_int_op(r, ==, 0);
  207. r = parse_smethod_line("SMETHOD celine 127.0.0.1:1723 ARGS:card=no-enemy",
  208. mp2);
  209. tt_int_op(r, ==, 0);
  210. /* Force these proxies to look "completed" or they won't generate output. */
  211. mp1->conf_state = mp2->conf_state = PT_PROTO_COMPLETED;
  212. s = pt_get_extra_info_descriptor_string();
  213. tt_assert(s);
  214. tt_str_op(s, ==,
  215. "transport hagbard 127.0.0.1:5555\n"
  216. "transport celine 127.0.0.1:1723 card=no-enemy\n");
  217. done:
  218. /* XXXX clean up better */
  219. smartlist_free(t1);
  220. smartlist_free(t2);
  221. tor_free(s);
  222. }
  223. #ifdef _WIN32
  224. #define STDIN_HANDLE HANDLE
  225. #else
  226. #define STDIN_HANDLE FILE
  227. #endif
  228. static smartlist_t *
  229. tor_get_lines_from_handle_replacement(STDIN_HANDLE *handle,
  230. enum stream_status *stream_status_out)
  231. {
  232. static int times_called = 0;
  233. smartlist_t *retval_sl = smartlist_new();
  234. (void) handle;
  235. (void) stream_status_out;
  236. /* Generate some dummy CMETHOD lines the first 5 times. The 6th
  237. time, send 'CMETHODS DONE' to finish configuring the proxy. */
  238. if (times_called++ != 5) {
  239. smartlist_add_asprintf(retval_sl, "SMETHOD mock%d 127.0.0.1:555%d",
  240. times_called, times_called);
  241. } else {
  242. smartlist_add(retval_sl, tor_strdup("SMETHODS DONE"));
  243. }
  244. return retval_sl;
  245. }
  246. /* NOP mock */
  247. static void
  248. tor_process_handle_destroy_replacement(process_handle_t *process_handle,
  249. int also_terminate_process)
  250. {
  251. (void) process_handle;
  252. (void) also_terminate_process;
  253. }
  254. static or_state_t *dummy_state = NULL;
  255. static or_state_t *
  256. get_or_state_replacement(void)
  257. {
  258. return dummy_state;
  259. }
  260. /* Test the configure_proxy() function. */
  261. static void
  262. test_pt_configure_proxy(void *arg)
  263. {
  264. int i, retval;
  265. managed_proxy_t *mp = NULL;
  266. (void) arg;
  267. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  268. MOCK(tor_get_lines_from_handle,
  269. tor_get_lines_from_handle_replacement);
  270. MOCK(tor_process_handle_destroy,
  271. tor_process_handle_destroy_replacement);
  272. MOCK(get_or_state,
  273. get_or_state_replacement);
  274. mp = tor_malloc(sizeof(managed_proxy_t));
  275. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  276. mp->transports = smartlist_new();
  277. mp->transports_to_launch = smartlist_new();
  278. mp->process_handle = tor_malloc_zero(sizeof(process_handle_t));
  279. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  280. mp->argv[0] = tor_strdup("<testcase>");
  281. mp->is_server = 1;
  282. /* Test the return value of configure_proxy() by calling it some
  283. times while it is uninitialized and then finally finalizing its
  284. configuration. */
  285. for (i = 0 ; i < 5 ; i++) {
  286. retval = configure_proxy(mp);
  287. /* retval should be zero because proxy hasn't finished configuring yet */
  288. test_assert(retval == 0);
  289. /* check the number of registered transports */
  290. test_assert(smartlist_len(mp->transports) == i+1);
  291. /* check that the mp is still waiting for transports */
  292. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  293. }
  294. /* this last configure_proxy() should finalize the proxy configuration. */
  295. retval = configure_proxy(mp);
  296. /* retval should be 1 since the proxy finished configuring */
  297. test_assert(retval == 1);
  298. /* check the mp state */
  299. test_assert(mp->conf_state == PT_PROTO_COMPLETED);
  300. { /* check that the transport info were saved properly in the tor state */
  301. config_line_t *transport_in_state = NULL;
  302. smartlist_t *transport_info_sl = smartlist_new();
  303. char *name_of_transport = NULL;
  304. char *bindaddr = NULL;
  305. /* Get the bindaddr for "mock1" and check it against the bindaddr
  306. that the mocked tor_get_lines_from_handle() generated. */
  307. transport_in_state = get_transport_in_state_by_name("mock1");
  308. test_assert(transport_in_state);
  309. smartlist_split_string(transport_info_sl, transport_in_state->value,
  310. NULL, 0, 0);
  311. name_of_transport = smartlist_get(transport_info_sl, 0);
  312. bindaddr = smartlist_get(transport_info_sl, 1);
  313. tt_str_op(name_of_transport, ==, "mock1");
  314. tt_str_op(bindaddr, ==, "127.0.0.1:5551");
  315. SMARTLIST_FOREACH(transport_info_sl, char *, cp, tor_free(cp));
  316. smartlist_free(transport_info_sl);
  317. }
  318. done:
  319. tor_free(dummy_state);
  320. UNMOCK(tor_get_lines_from_handle);
  321. UNMOCK(tor_process_handle_destroy);
  322. }
  323. #define PT_LEGACY(name) \
  324. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  325. struct testcase_t pt_tests[] = {
  326. PT_LEGACY(parsing),
  327. PT_LEGACY(protocol),
  328. { "get_transport_options", test_pt_get_transport_options, TT_FORK,
  329. NULL, NULL },
  330. { "get_extrainfo_string", test_pt_get_extrainfo_string, TT_FORK,
  331. NULL, NULL },
  332. { "configure_proxy",test_pt_configure_proxy, TT_FORK,
  333. NULL, NULL },
  334. END_OF_TESTCASES
  335. };