test_pt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, 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. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  74. mp->argv[0] = tor_strdup("<testcase>");
  75. /* various wrong protocol runs: */
  76. strcpy(line,"VERSION 1");
  77. handle_proxy_line(line, mp);
  78. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  79. strcpy(line,"VERSION 1");
  80. handle_proxy_line(line, mp);
  81. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  82. reset_mp(mp);
  83. strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
  84. handle_proxy_line(line, mp);
  85. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  86. reset_mp(mp);
  87. /* correct protocol run: */
  88. strcpy(line,"VERSION 1");
  89. handle_proxy_line(line, mp);
  90. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  91. strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
  92. handle_proxy_line(line, mp);
  93. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  94. strcpy(line,"CMETHODS DONE");
  95. handle_proxy_line(line, mp);
  96. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  97. done:
  98. tor_free(mp);
  99. }
  100. #define PT_LEGACY(name) \
  101. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  102. struct testcase_t pt_tests[] = {
  103. PT_LEGACY(parsing),
  104. PT_LEGACY(protocol),
  105. END_OF_TESTCASES
  106. };