test_pt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "transports.h"
  9. #include "circuitbuild.h"
  10. #include "test.h"
  11. static void
  12. reset_mp(managed_proxy_t *mp)
  13. {
  14. mp->conf_state = PT_PROTO_LAUNCHED;
  15. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  16. smartlist_clear(mp->transports);
  17. }
  18. static void
  19. test_pt_parsing(void)
  20. {
  21. char line[200];
  22. transport_t *transport = NULL;
  23. tor_addr_t test_addr;
  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) == 1);
  43. transport = smartlist_get(mp->transports, 0);
  44. /* test registered address of transport */
  45. tor_addr_parse(&test_addr, "127.0.0.1");
  46. test_assert(tor_addr_eq(&test_addr, &transport->addr));
  47. /* test registered port of transport */
  48. test_assert(transport->port == 1999);
  49. /* test registered SOCKS version of transport */
  50. test_assert(transport->socks_version == PROXY_SOCKS5);
  51. /* test registered name of transport */
  52. test_streq(transport->name, "trebuchet");
  53. reset_mp(mp);
  54. /* incomplete smethod */
  55. strlcpy(line,"SMETHOD trebuchet",sizeof(line));
  56. test_assert(parse_smethod_line(line, mp) < 0);
  57. reset_mp(mp);
  58. /* wrong addr type */
  59. strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
  60. test_assert(parse_smethod_line(line, mp) < 0);
  61. reset_mp(mp);
  62. /* cowwect */
  63. strlcpy(line,"SMETHOD trebuchy 127.0.0.2:2999",sizeof(line));
  64. test_assert(parse_smethod_line(line, mp) == 0);
  65. test_assert(smartlist_len(mp->transports) == 1);
  66. transport = smartlist_get(mp->transports, 0);
  67. /* test registered address of transport */
  68. tor_addr_parse(&test_addr, "127.0.0.2");
  69. test_assert(tor_addr_eq(&test_addr, &transport->addr));
  70. /* test registered port of transport */
  71. test_assert(transport->port == 2999);
  72. /* test registered name of transport */
  73. test_streq(transport->name, "trebuchy");
  74. reset_mp(mp);
  75. /* Include some arguments. Good ones. */
  76. strlcpy(line,"SMETHOD trebuchet 127.0.0.1:9999 ARGS:counterweight=3,sling=snappy",
  77. sizeof(line));
  78. test_assert(parse_smethod_line(line, mp) == 0);
  79. tt_int_op(1, ==, smartlist_len(mp->transports));
  80. {
  81. const transport_t *transport = smartlist_get(mp->transports, 0);
  82. tt_assert(transport);
  83. tt_str_op(transport->name, ==, "trebuchet");
  84. tt_int_op(transport->port, ==, 9999);
  85. tt_str_op(fmt_addr(&transport->addr), ==, "127.0.0.1");
  86. tt_str_op(transport->extra_info_args, ==,
  87. "counterweight=3,sling=snappy");
  88. }
  89. reset_mp(mp);
  90. /* unsupported version */
  91. strlcpy(line,"VERSION 666",sizeof(line));
  92. test_assert(parse_version(line, mp) < 0);
  93. /* incomplete VERSION */
  94. strlcpy(line,"VERSION ",sizeof(line));
  95. test_assert(parse_version(line, mp) < 0);
  96. /* correct VERSION */
  97. strlcpy(line,"VERSION 1",sizeof(line));
  98. test_assert(parse_version(line, mp) == 0);
  99. done:
  100. tor_free(mp);
  101. }
  102. static void
  103. test_pt_protocol(void)
  104. {
  105. char line[200];
  106. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  107. mp->conf_state = PT_PROTO_LAUNCHED;
  108. mp->transports = smartlist_new();
  109. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  110. mp->argv[0] = tor_strdup("<testcase>");
  111. /* various wrong protocol runs: */
  112. strlcpy(line,"VERSION 1",sizeof(line));
  113. handle_proxy_line(line, mp);
  114. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  115. strlcpy(line,"VERSION 1",sizeof(line));
  116. handle_proxy_line(line, mp);
  117. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  118. reset_mp(mp);
  119. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  120. handle_proxy_line(line, mp);
  121. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  122. reset_mp(mp);
  123. /* correct protocol run: */
  124. strlcpy(line,"VERSION 1",sizeof(line));
  125. handle_proxy_line(line, mp);
  126. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  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_ACCEPTING_METHODS);
  130. strlcpy(line,"CMETHODS DONE",sizeof(line));
  131. handle_proxy_line(line, mp);
  132. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  133. done:
  134. tor_free(mp);
  135. }
  136. #define PT_LEGACY(name) \
  137. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  138. struct testcase_t pt_tests[] = {
  139. PT_LEGACY(parsing),
  140. PT_LEGACY(protocol),
  141. END_OF_TESTCASES
  142. };