test_pt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
  25. mp->conf_state = PT_PROTO_INFANT;
  26. mp->transports = smartlist_new();
  27. /* incomplete cmethod */
  28. strlcpy(line,"CMETHOD trebuchet",sizeof(line));
  29. test_assert(parse_cmethod_line(line, mp) < 0);
  30. reset_mp(mp);
  31. /* wrong proxy type */
  32. strlcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999",sizeof(line));
  33. test_assert(parse_cmethod_line(line, mp) < 0);
  34. reset_mp(mp);
  35. /* wrong addrport */
  36. strlcpy(line,"CMETHOD trebuchet socks4 abcd",sizeof(line));
  37. test_assert(parse_cmethod_line(line, mp) < 0);
  38. reset_mp(mp);
  39. /* correct line */
  40. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  41. test_assert(parse_cmethod_line(line, mp) == 0);
  42. test_assert(smartlist_len(mp->transports));
  43. reset_mp(mp);
  44. /* incomplete smethod */
  45. strlcpy(line,"SMETHOD trebuchet",sizeof(line));
  46. test_assert(parse_smethod_line(line, mp) < 0);
  47. reset_mp(mp);
  48. /* wrong addr type */
  49. strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
  50. test_assert(parse_smethod_line(line, mp) < 0);
  51. reset_mp(mp);
  52. /* cowwect */
  53. strlcpy(line,"SMETHOD trebuchy 127.0.0.1:1999",sizeof(line));
  54. test_assert(parse_smethod_line(line, mp) == 0);
  55. reset_mp(mp);
  56. /* unsupported version */
  57. strlcpy(line,"VERSION 666",sizeof(line));
  58. test_assert(parse_version(line, mp) < 0);
  59. /* incomplete VERSION */
  60. strlcpy(line,"VERSION ",sizeof(line));
  61. test_assert(parse_version(line, mp) < 0);
  62. /* correct VERSION */
  63. strlcpy(line,"VERSION 1",sizeof(line));
  64. test_assert(parse_version(line, mp) == 0);
  65. done:
  66. tor_free(mp);
  67. }
  68. static void
  69. test_pt_get_transport_options(void *arg)
  70. {
  71. char **execve_args;
  72. smartlist_t *transport_list = smartlist_new();
  73. managed_proxy_t *mp;
  74. or_options_t *options = get_options_mutable();
  75. char *opt_str = NULL;
  76. config_line_t *cl = NULL;
  77. (void)arg;
  78. execve_args = tor_malloc(sizeof(char*)*2);
  79. execve_args[0] = tor_strdup("cheeseshop");
  80. execve_args[1] = NULL;
  81. mp = managed_proxy_create(transport_list, execve_args, 1);
  82. tt_ptr_op(mp, !=, NULL);
  83. opt_str = get_transport_options_for_server_proxy(mp);
  84. tt_ptr_op(opt_str, ==, NULL);
  85. smartlist_add(mp->transports_to_launch, tor_strdup("gruyere"));
  86. smartlist_add(mp->transports_to_launch, tor_strdup("roquefort"));
  87. smartlist_add(mp->transports_to_launch, tor_strdup("stnectaire"));
  88. tt_assert(options);
  89. cl = tor_malloc_zero(sizeof(config_line_t));
  90. cl->value = tor_strdup("gruyere melty=10 hardness=se;ven");
  91. options->ServerTransportOptions = cl;
  92. cl = tor_malloc_zero(sizeof(config_line_t));
  93. cl->value = tor_strdup("stnectaire melty=4 hardness=three");
  94. cl->next = options->ServerTransportOptions;
  95. options->ServerTransportOptions = cl;
  96. cl = tor_malloc_zero(sizeof(config_line_t));
  97. cl->value = tor_strdup("pepperjack melty=12 hardness=five");
  98. cl->next = options->ServerTransportOptions;
  99. options->ServerTransportOptions = cl;
  100. opt_str = get_transport_options_for_server_proxy(mp);
  101. tt_str_op(opt_str, ==,
  102. "gruyere:melty=10;gruyere:hardness=se\\;ven;"
  103. "stnectaire:melty=4;stnectaire:hardness=three");
  104. done:
  105. tor_free(opt_str);
  106. config_free_lines(cl);
  107. managed_proxy_destroy(mp, 0);
  108. smartlist_free(transport_list);
  109. }
  110. static void
  111. test_pt_protocol(void)
  112. {
  113. char line[200];
  114. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  115. mp->conf_state = PT_PROTO_LAUNCHED;
  116. mp->transports = smartlist_new();
  117. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  118. mp->argv[0] = tor_strdup("<testcase>");
  119. /* various wrong protocol runs: */
  120. strlcpy(line,"VERSION 1",sizeof(line));
  121. handle_proxy_line(line, mp);
  122. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  123. strlcpy(line,"VERSION 1",sizeof(line));
  124. handle_proxy_line(line, mp);
  125. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  126. reset_mp(mp);
  127. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  128. handle_proxy_line(line, mp);
  129. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  130. reset_mp(mp);
  131. /* correct protocol run: */
  132. strlcpy(line,"VERSION 1",sizeof(line));
  133. handle_proxy_line(line, mp);
  134. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  135. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  136. handle_proxy_line(line, mp);
  137. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  138. strlcpy(line,"CMETHODS DONE",sizeof(line));
  139. handle_proxy_line(line, mp);
  140. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  141. done:
  142. tor_free(mp);
  143. }
  144. #define PT_LEGACY(name) \
  145. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  146. struct testcase_t pt_tests[] = {
  147. PT_LEGACY(parsing),
  148. PT_LEGACY(protocol),
  149. { "get_transport_options", test_pt_get_transport_options, TT_FORK,
  150. NULL, NULL },
  151. END_OF_TESTCASES
  152. };