test_pt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "
  79. "ARGS:counterweight=3,sling=snappy",
  80. sizeof(line));
  81. test_assert(parse_smethod_line(line, mp) == 0);
  82. tt_int_op(1, ==, smartlist_len(mp->transports));
  83. {
  84. const transport_t *transport = smartlist_get(mp->transports, 0);
  85. tt_assert(transport);
  86. tt_str_op(transport->name, ==, "trebuchet");
  87. tt_int_op(transport->port, ==, 9999);
  88. tt_str_op(fmt_addr(&transport->addr), ==, "127.0.0.1");
  89. tt_str_op(transport->extra_info_args, ==,
  90. "counterweight=3,sling=snappy");
  91. }
  92. reset_mp(mp);
  93. /* unsupported version */
  94. strlcpy(line,"VERSION 666",sizeof(line));
  95. test_assert(parse_version(line, mp) < 0);
  96. /* incomplete VERSION */
  97. strlcpy(line,"VERSION ",sizeof(line));
  98. test_assert(parse_version(line, mp) < 0);
  99. /* correct VERSION */
  100. strlcpy(line,"VERSION 1",sizeof(line));
  101. test_assert(parse_version(line, mp) == 0);
  102. done:
  103. tor_free(mp);
  104. }
  105. static void
  106. test_pt_get_transport_options(void *arg)
  107. {
  108. char **execve_args;
  109. smartlist_t *transport_list = smartlist_new();
  110. managed_proxy_t *mp;
  111. or_options_t *options = get_options_mutable();
  112. char *opt_str = NULL;
  113. config_line_t *cl = NULL;
  114. (void)arg;
  115. execve_args = tor_malloc(sizeof(char*)*2);
  116. execve_args[0] = tor_strdup("cheeseshop");
  117. execve_args[1] = NULL;
  118. mp = managed_proxy_create(transport_list, execve_args, 1);
  119. tt_ptr_op(mp, !=, NULL);
  120. opt_str = get_transport_options_for_server_proxy(mp);
  121. tt_ptr_op(opt_str, ==, NULL);
  122. smartlist_add(mp->transports_to_launch, tor_strdup("gruyere"));
  123. smartlist_add(mp->transports_to_launch, tor_strdup("roquefort"));
  124. smartlist_add(mp->transports_to_launch, tor_strdup("stnectaire"));
  125. tt_assert(options);
  126. cl = tor_malloc_zero(sizeof(config_line_t));
  127. cl->value = tor_strdup("gruyere melty=10 hardness=se;ven");
  128. options->ServerTransportOptions = cl;
  129. cl = tor_malloc_zero(sizeof(config_line_t));
  130. cl->value = tor_strdup("stnectaire melty=4 hardness=three");
  131. cl->next = options->ServerTransportOptions;
  132. options->ServerTransportOptions = cl;
  133. cl = tor_malloc_zero(sizeof(config_line_t));
  134. cl->value = tor_strdup("pepperjack melty=12 hardness=five");
  135. cl->next = options->ServerTransportOptions;
  136. options->ServerTransportOptions = cl;
  137. opt_str = get_transport_options_for_server_proxy(mp);
  138. tt_str_op(opt_str, ==,
  139. "gruyere:melty=10;gruyere:hardness=se\\;ven;"
  140. "stnectaire:melty=4;stnectaire:hardness=three");
  141. done:
  142. tor_free(opt_str);
  143. config_free_lines(cl);
  144. managed_proxy_destroy(mp, 0);
  145. smartlist_free(transport_list);
  146. }
  147. static void
  148. test_pt_protocol(void)
  149. {
  150. char line[200];
  151. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  152. mp->conf_state = PT_PROTO_LAUNCHED;
  153. mp->transports = smartlist_new();
  154. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  155. mp->argv[0] = tor_strdup("<testcase>");
  156. /* various wrong protocol runs: */
  157. strlcpy(line,"VERSION 1",sizeof(line));
  158. handle_proxy_line(line, mp);
  159. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  160. strlcpy(line,"VERSION 1",sizeof(line));
  161. handle_proxy_line(line, mp);
  162. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  163. reset_mp(mp);
  164. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  165. handle_proxy_line(line, mp);
  166. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  167. reset_mp(mp);
  168. /* correct protocol run: */
  169. strlcpy(line,"VERSION 1",sizeof(line));
  170. handle_proxy_line(line, mp);
  171. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  172. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  173. handle_proxy_line(line, mp);
  174. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  175. strlcpy(line,"CMETHODS DONE",sizeof(line));
  176. handle_proxy_line(line, mp);
  177. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  178. done:
  179. tor_free(mp);
  180. }
  181. static void
  182. test_pt_get_extrainfo_string(void *arg)
  183. {
  184. managed_proxy_t *mp1 = NULL, *mp2 = NULL;
  185. char **argv1, **argv2;
  186. smartlist_t *t1 = smartlist_new(), *t2 = smartlist_new();
  187. int r;
  188. char *s = NULL;
  189. (void) arg;
  190. argv1 = tor_malloc_zero(sizeof(char*)*3);
  191. argv1[0] = tor_strdup("ewige");
  192. argv1[1] = tor_strdup("Blumenkraft");
  193. argv1[2] = NULL;
  194. argv2 = tor_malloc_zero(sizeof(char*)*4);
  195. argv2[0] = tor_strdup("und");
  196. argv2[1] = tor_strdup("ewige");
  197. argv2[2] = tor_strdup("Schlangenkraft");
  198. argv2[3] = NULL;
  199. mp1 = managed_proxy_create(t1, argv1, 1);
  200. mp2 = managed_proxy_create(t2, argv2, 1);
  201. r = parse_smethod_line("SMETHOD hagbard 127.0.0.1:5555", mp1);
  202. tt_int_op(r, ==, 0);
  203. r = parse_smethod_line("SMETHOD celine 127.0.0.1:1723 ARGS:card=no-enemy",
  204. mp2);
  205. tt_int_op(r, ==, 0);
  206. /* Force these proxies to look "completed" or they won't generate output. */
  207. mp1->conf_state = mp2->conf_state = PT_PROTO_COMPLETED;
  208. s = pt_get_extra_info_descriptor_string();
  209. tt_assert(s);
  210. tt_str_op(s, ==,
  211. "transport hagbard 127.0.0.1:5555\n"
  212. "transport celine 127.0.0.1:1723 card=no-enemy\n");
  213. done:
  214. /* XXXX clean up better */
  215. smartlist_free(t1);
  216. smartlist_free(t2);
  217. tor_free(s);
  218. }
  219. #define PT_LEGACY(name) \
  220. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  221. struct testcase_t pt_tests[] = {
  222. PT_LEGACY(parsing),
  223. PT_LEGACY(protocol),
  224. { "get_transport_options", test_pt_get_transport_options, TT_FORK,
  225. NULL, NULL },
  226. { "get_extrainfo_string", test_pt_get_extrainfo_string, TT_FORK,
  227. NULL, NULL },
  228. END_OF_TESTCASES
  229. };