test_pt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #include "or.h"
  8. #include "config.h"
  9. #include "confparse.h"
  10. #include "transports.h"
  11. #include "circuitbuild.h"
  12. #include "test.h"
  13. static void
  14. reset_mp(managed_proxy_t *mp)
  15. {
  16. mp->conf_state = PT_PROTO_LAUNCHED;
  17. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  18. smartlist_clear(mp->transports);
  19. }
  20. static void
  21. test_pt_parsing(void)
  22. {
  23. char line[200];
  24. transport_t *transport = NULL;
  25. tor_addr_t test_addr;
  26. managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
  27. mp->conf_state = PT_PROTO_INFANT;
  28. mp->transports = smartlist_new();
  29. /* incomplete cmethod */
  30. strlcpy(line,"CMETHOD trebuchet",sizeof(line));
  31. test_assert(parse_cmethod_line(line, mp) < 0);
  32. reset_mp(mp);
  33. /* wrong proxy type */
  34. strlcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999",sizeof(line));
  35. test_assert(parse_cmethod_line(line, mp) < 0);
  36. reset_mp(mp);
  37. /* wrong addrport */
  38. strlcpy(line,"CMETHOD trebuchet socks4 abcd",sizeof(line));
  39. test_assert(parse_cmethod_line(line, mp) < 0);
  40. reset_mp(mp);
  41. /* correct line */
  42. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  43. test_assert(parse_cmethod_line(line, mp) == 0);
  44. test_assert(smartlist_len(mp->transports) == 1);
  45. transport = smartlist_get(mp->transports, 0);
  46. /* test registered address of transport */
  47. tor_addr_parse(&test_addr, "127.0.0.1");
  48. test_assert(tor_addr_eq(&test_addr, &transport->addr));
  49. /* test registered port of transport */
  50. test_assert(transport->port == 1999);
  51. /* test registered SOCKS version of transport */
  52. test_assert(transport->socks_version == PROXY_SOCKS5);
  53. /* test registered name of transport */
  54. test_streq(transport->name, "trebuchet");
  55. reset_mp(mp);
  56. /* incomplete smethod */
  57. strlcpy(line,"SMETHOD trebuchet",sizeof(line));
  58. test_assert(parse_smethod_line(line, mp) < 0);
  59. reset_mp(mp);
  60. /* wrong addr type */
  61. strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
  62. test_assert(parse_smethod_line(line, mp) < 0);
  63. reset_mp(mp);
  64. /* cowwect */
  65. strlcpy(line,"SMETHOD trebuchy 127.0.0.2:2999",sizeof(line));
  66. test_assert(parse_smethod_line(line, mp) == 0);
  67. test_assert(smartlist_len(mp->transports) == 1);
  68. transport = smartlist_get(mp->transports, 0);
  69. /* test registered address of transport */
  70. tor_addr_parse(&test_addr, "127.0.0.2");
  71. test_assert(tor_addr_eq(&test_addr, &transport->addr));
  72. /* test registered port of transport */
  73. test_assert(transport->port == 2999);
  74. /* test registered name of transport */
  75. test_streq(transport->name, "trebuchy");
  76. reset_mp(mp);
  77. /* Include some arguments. Good ones. */
  78. strlcpy(line,"SMETHOD trebuchet 127.0.0.1:9999 ARGS:counterweight=3,sling=snappy",
  79. sizeof(line));
  80. test_assert(parse_smethod_line(line, mp) == 0);
  81. tt_int_op(1, ==, smartlist_len(mp->transports));
  82. {
  83. const transport_t *transport = smartlist_get(mp->transports, 0);
  84. tt_assert(transport);
  85. tt_str_op(transport->name, ==, "trebuchet");
  86. tt_int_op(transport->port, ==, 9999);
  87. tt_str_op(fmt_addr(&transport->addr), ==, "127.0.0.1");
  88. tt_str_op(transport->extra_info_args, ==,
  89. "counterweight=3,sling=snappy");
  90. }
  91. reset_mp(mp);
  92. /* unsupported version */
  93. strlcpy(line,"VERSION 666",sizeof(line));
  94. test_assert(parse_version(line, mp) < 0);
  95. /* incomplete VERSION */
  96. strlcpy(line,"VERSION ",sizeof(line));
  97. test_assert(parse_version(line, mp) < 0);
  98. /* correct VERSION */
  99. strlcpy(line,"VERSION 1",sizeof(line));
  100. test_assert(parse_version(line, mp) == 0);
  101. done:
  102. tor_free(mp);
  103. }
  104. static void
  105. test_pt_get_transport_options(void *arg)
  106. {
  107. char **execve_args;
  108. smartlist_t *transport_list = smartlist_new();
  109. managed_proxy_t *mp;
  110. or_options_t *options = get_options_mutable();
  111. char *opt_str = NULL;
  112. config_line_t *cl = NULL;
  113. (void)arg;
  114. execve_args = tor_malloc(sizeof(char*)*2);
  115. execve_args[0] = tor_strdup("cheeseshop");
  116. execve_args[1] = NULL;
  117. mp = managed_proxy_create(transport_list, execve_args, 1);
  118. tt_ptr_op(mp, !=, NULL);
  119. opt_str = get_transport_options_for_server_proxy(mp);
  120. tt_ptr_op(opt_str, ==, NULL);
  121. smartlist_add(mp->transports_to_launch, tor_strdup("gruyere"));
  122. smartlist_add(mp->transports_to_launch, tor_strdup("roquefort"));
  123. smartlist_add(mp->transports_to_launch, tor_strdup("stnectaire"));
  124. tt_assert(options);
  125. cl = tor_malloc_zero(sizeof(config_line_t));
  126. cl->value = tor_strdup("gruyere melty=10 hardness=se;ven");
  127. options->ServerTransportOptions = cl;
  128. cl = tor_malloc_zero(sizeof(config_line_t));
  129. cl->value = tor_strdup("stnectaire melty=4 hardness=three");
  130. cl->next = options->ServerTransportOptions;
  131. options->ServerTransportOptions = cl;
  132. cl = tor_malloc_zero(sizeof(config_line_t));
  133. cl->value = tor_strdup("pepperjack melty=12 hardness=five");
  134. cl->next = options->ServerTransportOptions;
  135. options->ServerTransportOptions = cl;
  136. opt_str = get_transport_options_for_server_proxy(mp);
  137. tt_str_op(opt_str, ==,
  138. "gruyere:melty=10;gruyere:hardness=se\\;ven;"
  139. "stnectaire:melty=4;stnectaire:hardness=three");
  140. done:
  141. tor_free(opt_str);
  142. config_free_lines(cl);
  143. managed_proxy_destroy(mp, 0);
  144. smartlist_free(transport_list);
  145. }
  146. static void
  147. test_pt_protocol(void)
  148. {
  149. char line[200];
  150. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  151. mp->conf_state = PT_PROTO_LAUNCHED;
  152. mp->transports = smartlist_new();
  153. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  154. mp->argv[0] = tor_strdup("<testcase>");
  155. /* various wrong protocol runs: */
  156. strlcpy(line,"VERSION 1",sizeof(line));
  157. handle_proxy_line(line, mp);
  158. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  159. strlcpy(line,"VERSION 1",sizeof(line));
  160. handle_proxy_line(line, mp);
  161. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  162. reset_mp(mp);
  163. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  164. handle_proxy_line(line, mp);
  165. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  166. reset_mp(mp);
  167. /* correct protocol run: */
  168. strlcpy(line,"VERSION 1",sizeof(line));
  169. handle_proxy_line(line, mp);
  170. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  171. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  172. handle_proxy_line(line, mp);
  173. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  174. strlcpy(line,"CMETHODS DONE",sizeof(line));
  175. handle_proxy_line(line, mp);
  176. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  177. done:
  178. tor_free(mp);
  179. }
  180. static void
  181. test_pt_get_extrainfo_string(void *arg)
  182. {
  183. managed_proxy_t *mp1 = NULL, *mp2 = NULL;
  184. char **argv1, **argv2;
  185. smartlist_t *t1 = smartlist_new(), *t2 = smartlist_new();
  186. int r;
  187. char *s = NULL;
  188. (void) arg;
  189. argv1 = tor_malloc_zero(sizeof(char*)*3);
  190. argv1[0] = tor_strdup("ewige");
  191. argv1[1] = tor_strdup("Blumenkraft");
  192. argv1[2] = NULL;
  193. argv2 = tor_malloc_zero(sizeof(char*)*4);
  194. argv2[0] = tor_strdup("und");
  195. argv2[1] = tor_strdup("ewige");
  196. argv2[2] = tor_strdup("Schlangenkraft");
  197. argv2[3] = NULL;
  198. mp1 = managed_proxy_create(t1, argv1, 1);
  199. mp2 = managed_proxy_create(t2, argv2, 1);
  200. r = parse_smethod_line("SMETHOD hagbard 127.0.0.1:5555", mp1);
  201. tt_int_op(r, ==, 0);
  202. r = parse_smethod_line("SMETHOD celine 127.0.0.1:1723 ARGS:card=no-enemy",
  203. mp2);
  204. tt_int_op(r, ==, 0);
  205. /* Force these proxies to look "completed" or they won't generate output. */
  206. mp1->conf_state = mp2->conf_state = PT_PROTO_COMPLETED;
  207. s = pt_get_extra_info_descriptor_string();
  208. tt_assert(s);
  209. tt_str_op(s, ==,
  210. "transport hagbard 127.0.0.1:5555\n"
  211. "transport celine 127.0.0.1:1723 card=no-enemy\n");
  212. done:
  213. /* XXXX clean up better */
  214. smartlist_free(t1);
  215. smartlist_free(t2);
  216. tor_free(s);
  217. }
  218. #define PT_LEGACY(name) \
  219. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  220. struct testcase_t pt_tests[] = {
  221. PT_LEGACY(parsing),
  222. PT_LEGACY(protocol),
  223. { "get_transport_options", test_pt_get_transport_options, TT_FORK,
  224. NULL, NULL },
  225. { "get_extrainfo_string", test_pt_get_extrainfo_string, TT_FORK,
  226. NULL, NULL },
  227. END_OF_TESTCASES
  228. };