test_pt.c 11 KB

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