test_pt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, 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. managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
  23. mp->conf_state = PT_PROTO_INFANT;
  24. mp->transports = smartlist_new();
  25. /* incomplete cmethod */
  26. strcpy(line,"CMETHOD trebuchet");
  27. test_assert(parse_cmethod_line(line, mp) < 0);
  28. reset_mp(mp);
  29. /* wrong proxy type */
  30. strcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999");
  31. test_assert(parse_cmethod_line(line, mp) < 0);
  32. reset_mp(mp);
  33. /* wrong addrport */
  34. strcpy(line,"CMETHOD trebuchet socks4 abcd");
  35. test_assert(parse_cmethod_line(line, mp) < 0);
  36. reset_mp(mp);
  37. /* correct line */
  38. strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
  39. test_assert(parse_cmethod_line(line, mp) == 0);
  40. test_assert(smartlist_len(mp->transports));
  41. reset_mp(mp);
  42. /* incomplete smethod */
  43. strcpy(line,"SMETHOD trebuchet");
  44. test_assert(parse_smethod_line(line, mp) < 0);
  45. reset_mp(mp);
  46. /* wrong addr type */
  47. strcpy(line,"SMETHOD trebuchet abcd");
  48. test_assert(parse_smethod_line(line, mp) < 0);
  49. reset_mp(mp);
  50. /* cowwect */
  51. strcpy(line,"SMETHOD trebuchy 127.0.0.1:1999");
  52. test_assert(parse_smethod_line(line, mp) == 0);
  53. reset_mp(mp);
  54. /* unsupported version */
  55. strcpy(line,"VERSION 666");
  56. test_assert(parse_version(line, mp) < 0);
  57. /* incomplete VERSION */
  58. strcpy(line,"VERSION ");
  59. test_assert(parse_version(line, mp) < 0);
  60. /* correct VERSION */
  61. strcpy(line,"VERSION 1");
  62. test_assert(parse_version(line, mp) == 0);
  63. done:
  64. tor_free(mp);
  65. }
  66. static void
  67. test_pt_protocol(void)
  68. {
  69. char line[200];
  70. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  71. mp->conf_state = PT_PROTO_LAUNCHED;
  72. mp->transports = smartlist_new();
  73. /* various wrong protocol runs: */
  74. strcpy(line, "TEST TEST");
  75. handle_proxy_line(line, mp);
  76. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  77. reset_mp(mp);
  78. strcpy(line,"VERSION 1");
  79. handle_proxy_line(line, mp);
  80. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  81. strcpy(line,"VERSION 1");
  82. handle_proxy_line(line, mp);
  83. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  84. reset_mp(mp);
  85. strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
  86. handle_proxy_line(line, mp);
  87. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  88. reset_mp(mp);
  89. /* correct protocol run: */
  90. strcpy(line,"VERSION 1");
  91. handle_proxy_line(line, mp);
  92. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  93. strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
  94. handle_proxy_line(line, mp);
  95. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  96. strcpy(line,"CMETHODS DONE");
  97. handle_proxy_line(line, mp);
  98. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  99. done:
  100. tor_free(mp);
  101. }
  102. #define PT_LEGACY(name) \
  103. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  104. struct testcase_t pt_tests[] = {
  105. PT_LEGACY(parsing),
  106. PT_LEGACY(protocol),
  107. END_OF_TESTCASES
  108. };