test_pt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /* unsupported version */
  76. strlcpy(line,"VERSION 666",sizeof(line));
  77. test_assert(parse_version(line, mp) < 0);
  78. /* incomplete VERSION */
  79. strlcpy(line,"VERSION ",sizeof(line));
  80. test_assert(parse_version(line, mp) < 0);
  81. /* correct VERSION */
  82. strlcpy(line,"VERSION 1",sizeof(line));
  83. test_assert(parse_version(line, mp) == 0);
  84. done:
  85. tor_free(mp);
  86. }
  87. static void
  88. test_pt_protocol(void)
  89. {
  90. char line[200];
  91. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  92. mp->conf_state = PT_PROTO_LAUNCHED;
  93. mp->transports = smartlist_new();
  94. mp->argv = tor_malloc_zero(sizeof(char*)*2);
  95. mp->argv[0] = tor_strdup("<testcase>");
  96. /* various wrong protocol runs: */
  97. strlcpy(line,"VERSION 1",sizeof(line));
  98. handle_proxy_line(line, mp);
  99. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  100. strlcpy(line,"VERSION 1",sizeof(line));
  101. handle_proxy_line(line, mp);
  102. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  103. reset_mp(mp);
  104. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  105. handle_proxy_line(line, mp);
  106. test_assert(mp->conf_state == PT_PROTO_BROKEN);
  107. reset_mp(mp);
  108. /* correct protocol run: */
  109. strlcpy(line,"VERSION 1",sizeof(line));
  110. handle_proxy_line(line, mp);
  111. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  112. strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
  113. handle_proxy_line(line, mp);
  114. test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
  115. strlcpy(line,"CMETHODS DONE",sizeof(line));
  116. handle_proxy_line(line, mp);
  117. test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
  118. done:
  119. tor_free(mp);
  120. }
  121. #define PT_LEGACY(name) \
  122. { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
  123. struct testcase_t pt_tests[] = {
  124. PT_LEGACY(parsing),
  125. PT_LEGACY(protocol),
  126. END_OF_TESTCASES
  127. };