test_socks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "or.h"
  6. #include "buffers.h"
  7. #include "config.h"
  8. #include "test.h"
  9. typedef struct socks_test_data_t {
  10. socks_request_t *req;
  11. buf_t *buf;
  12. } socks_test_data_t;
  13. static void *
  14. socks_test_setup(const struct testcase_t *testcase)
  15. {
  16. socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
  17. (void)testcase;
  18. data->buf = buf_new_with_capacity(256);
  19. data->req = socks_request_new();
  20. config_register_addressmaps(get_options());
  21. return data;
  22. }
  23. static int
  24. socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
  25. {
  26. socks_test_data_t *data = ptr;
  27. (void)testcase;
  28. buf_free(data->buf);
  29. socks_request_free(data->req);
  30. tor_free(data);
  31. return 1;
  32. }
  33. const struct testcase_setup_t socks_setup = {
  34. socks_test_setup, socks_test_cleanup
  35. };
  36. #define SOCKS_TEST_INIT() \
  37. socks_test_data_t *testdata = ptr; \
  38. buf_t *buf = testdata->buf; \
  39. socks_request_t *socks = testdata->req;
  40. #define ADD_DATA(buf, s) \
  41. write_to_buf(s, sizeof(s)-1, buf)
  42. static void
  43. socks_request_clear(socks_request_t *socks)
  44. {
  45. tor_free(socks->username);
  46. tor_free(socks->password);
  47. memset(socks, 0, sizeof(socks_request_t));
  48. }
  49. /** Perform unsupported SOCKS 4 commands */
  50. static void
  51. test_socks_4_unsupported_commands(void *ptr)
  52. {
  53. SOCKS_TEST_INIT();
  54. /* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
  55. ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
  56. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  57. get_options()->SafeSocks) == -1);
  58. test_eq(4, socks->socks_version);
  59. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  60. done:
  61. ;
  62. }
  63. /** Perform supported SOCKS 4 commands */
  64. static void
  65. test_socks_4_supported_commands(void *ptr)
  66. {
  67. SOCKS_TEST_INIT();
  68. test_eq(0, buf_datalen(buf));
  69. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
  70. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
  71. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  72. get_options()->SafeSocks) == 1);
  73. test_eq(4, socks->socks_version);
  74. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  75. test_eq(SOCKS_COMMAND_CONNECT, socks->command);
  76. test_streq("2.2.2.3", socks->address);
  77. test_eq(4370, socks->port);
  78. test_assert(socks->got_auth == 0);
  79. test_assert(! socks->username);
  80. test_eq(0, buf_datalen(buf));
  81. socks_request_clear(socks);
  82. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
  83. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
  84. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  85. get_options()->SafeSocks) == 1);
  86. test_eq(4, socks->socks_version);
  87. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  88. test_eq(SOCKS_COMMAND_CONNECT, socks->command);
  89. test_streq("2.2.2.4", socks->address);
  90. test_eq(4370, socks->port);
  91. test_assert(socks->got_auth == 1);
  92. test_assert(socks->username);
  93. test_eq(2, socks->usernamelen);
  94. test_memeq("me", socks->username, 2);
  95. test_eq(0, buf_datalen(buf));
  96. socks_request_clear(socks);
  97. /* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
  98. ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
  99. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  100. get_options()->SafeSocks) == 1);
  101. test_eq(4, socks->socks_version);
  102. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  103. test_streq("torproject.org", socks->address);
  104. test_eq(0, buf_datalen(buf));
  105. done:
  106. ;
  107. }
  108. /** Perform unsupported SOCKS 5 commands */
  109. static void
  110. test_socks_5_unsupported_commands(void *ptr)
  111. {
  112. SOCKS_TEST_INIT();
  113. /* SOCKS 5 Send unsupported BIND [02] command */
  114. ADD_DATA(buf, "\x05\x02\x00\x01");
  115. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  116. get_options()->SafeSocks), 0);
  117. test_eq(0, buf_datalen(buf));
  118. test_eq(5, socks->socks_version);
  119. test_eq(2, socks->replylen);
  120. test_eq(5, socks->reply[0]);
  121. test_eq(0, socks->reply[1]);
  122. ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
  123. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  124. get_options()->SafeSocks), -1);
  125. /* XXX: shouldn't tor reply 'command not supported' [07]? */
  126. buf_clear(buf);
  127. socks_request_clear(socks);
  128. /* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
  129. ADD_DATA(buf, "\x05\x03\x00\x01\x02");
  130. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  131. get_options()->SafeSocks), 0);
  132. test_eq(5, socks->socks_version);
  133. test_eq(2, socks->replylen);
  134. test_eq(5, socks->reply[0]);
  135. test_eq(2, socks->reply[1]);
  136. ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
  137. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  138. get_options()->SafeSocks), -1);
  139. /* XXX: shouldn't tor reply 'command not supported' [07]? */
  140. done:
  141. ;
  142. }
  143. /** Perform supported SOCKS 5 commands */
  144. static void
  145. test_socks_5_supported_commands(void *ptr)
  146. {
  147. SOCKS_TEST_INIT();
  148. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  149. ADD_DATA(buf, "\x05\x01\x00");
  150. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  151. get_options()->SafeSocks), 0);
  152. test_eq(5, socks->socks_version);
  153. test_eq(2, socks->replylen);
  154. test_eq(5, socks->reply[0]);
  155. test_eq(0, socks->reply[1]);
  156. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  157. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  158. get_options()->SafeSocks), 1);
  159. test_streq("2.2.2.2", socks->address);
  160. test_eq(4369, socks->port);
  161. test_eq(0, buf_datalen(buf));
  162. socks_request_clear(socks);
  163. /* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
  164. ADD_DATA(buf, "\x05\x01\x00");
  165. ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
  166. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  167. get_options()->SafeSocks), 1);
  168. test_eq(5, socks->socks_version);
  169. test_eq(2, socks->replylen);
  170. test_eq(5, socks->reply[0]);
  171. test_eq(0, socks->reply[1]);
  172. test_streq("torproject.org", socks->address);
  173. test_eq(4369, socks->port);
  174. test_eq(0, buf_datalen(buf));
  175. socks_request_clear(socks);
  176. /* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
  177. ADD_DATA(buf, "\x05\x01\x00");
  178. ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
  179. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  180. get_options()->SafeSocks) == 1);
  181. test_eq(5, socks->socks_version);
  182. test_eq(2, socks->replylen);
  183. test_eq(5, socks->reply[0]);
  184. test_eq(0, socks->reply[1]);
  185. test_streq("torproject.org", socks->address);
  186. test_eq(0, buf_datalen(buf));
  187. socks_request_clear(socks);
  188. /* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
  189. ADD_DATA(buf, "\x05\x01\x00");
  190. ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
  191. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  192. get_options()->SafeSocks) == 1);
  193. test_eq(5, socks->socks_version);
  194. test_eq(2, socks->replylen);
  195. test_eq(5, socks->reply[0]);
  196. test_eq(0, socks->reply[1]);
  197. test_streq("2.2.2.5", socks->address);
  198. test_eq(0, buf_datalen(buf));
  199. done:
  200. ;
  201. }
  202. /** Perform SOCKS 5 authentication */
  203. static void
  204. test_socks_5_no_authenticate(void *ptr)
  205. {
  206. SOCKS_TEST_INIT();
  207. /*SOCKS 5 No Authentication */
  208. ADD_DATA(buf,"\x05\x01\x00");
  209. test_assert(!fetch_from_buf_socks(buf, socks,
  210. get_options()->TestSocks,
  211. get_options()->SafeSocks));
  212. test_eq(2, socks->replylen);
  213. test_eq(5, socks->reply[0]);
  214. test_eq(SOCKS_NO_AUTH, socks->reply[1]);
  215. test_eq(0, buf_datalen(buf));
  216. /*SOCKS 5 Send username/password anyway - pretend to be broken */
  217. ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
  218. test_assert(!fetch_from_buf_socks(buf, socks,
  219. get_options()->TestSocks,
  220. get_options()->SafeSocks));
  221. test_eq(5, socks->socks_version);
  222. test_eq(2, socks->replylen);
  223. test_eq(1, socks->reply[0]);
  224. test_eq(0, socks->reply[1]);
  225. test_eq(2, socks->usernamelen);
  226. test_eq(2, socks->passwordlen);
  227. test_memeq("\x01\x01", socks->username, 2);
  228. test_memeq("\x01\x01", socks->password, 2);
  229. done:
  230. ;
  231. }
  232. /** Perform SOCKS 5 authentication */
  233. static void
  234. test_socks_5_authenticate(void *ptr)
  235. {
  236. SOCKS_TEST_INIT();
  237. /* SOCKS 5 Negotiate username/password authentication */
  238. ADD_DATA(buf, "\x05\x01\x02");
  239. test_assert(!fetch_from_buf_socks(buf, socks,
  240. get_options()->TestSocks,
  241. get_options()->SafeSocks));
  242. test_eq(2, socks->replylen);
  243. test_eq(5, socks->reply[0]);
  244. test_eq(SOCKS_USER_PASS, socks->reply[1]);
  245. test_eq(5, socks->socks_version);
  246. test_eq(0, buf_datalen(buf));
  247. /* SOCKS 5 Send username/password */
  248. ADD_DATA(buf, "\x01\x02me\x08mypasswd");
  249. test_assert(!fetch_from_buf_socks(buf, socks,
  250. get_options()->TestSocks,
  251. get_options()->SafeSocks));
  252. test_eq(5, socks->socks_version);
  253. test_eq(2, socks->replylen);
  254. test_eq(1, socks->reply[0]);
  255. test_eq(0, socks->reply[1]);
  256. test_eq(2, socks->usernamelen);
  257. test_eq(8, socks->passwordlen);
  258. test_memeq("me", socks->username, 2);
  259. test_memeq("mypasswd", socks->password, 8);
  260. done:
  261. ;
  262. }
  263. /** Perform SOCKS 5 authentication and send data all in one go */
  264. static void
  265. test_socks_5_authenticate_with_data(void *ptr)
  266. {
  267. SOCKS_TEST_INIT();
  268. /* SOCKS 5 Negotiate username/password authentication */
  269. ADD_DATA(buf, "\x05\x01\x02");
  270. test_assert(!fetch_from_buf_socks(buf, socks,
  271. get_options()->TestSocks,
  272. get_options()->SafeSocks));
  273. test_eq(2, socks->replylen);
  274. test_eq(5, socks->reply[0]);
  275. test_eq(SOCKS_USER_PASS, socks->reply[1]);
  276. test_eq(5, socks->socks_version);
  277. test_eq(0, buf_datalen(buf));
  278. /* SOCKS 5 Send username/password */
  279. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  280. ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  281. test_assert(fetch_from_buf_socks(buf, socks,
  282. get_options()->TestSocks,
  283. get_options()->SafeSocks) == 1);
  284. test_eq(5, socks->socks_version);
  285. test_eq(2, socks->replylen);
  286. test_eq(1, socks->reply[0]);
  287. test_eq(0, socks->reply[1]);
  288. test_streq("2.2.2.2", socks->address);
  289. test_eq(4369, socks->port);
  290. test_eq(2, socks->usernamelen);
  291. test_eq(3, socks->passwordlen);
  292. test_memeq("me", socks->username, 2);
  293. test_memeq("you", socks->password, 3);
  294. done:
  295. ;
  296. }
  297. /** Perform SOCKS 5 authentication before method negotiated */
  298. static void
  299. test_socks_5_auth_before_negotiation(void *ptr)
  300. {
  301. SOCKS_TEST_INIT();
  302. /* SOCKS 5 Send username/password */
  303. ADD_DATA(buf, "\x01\x02me\x02me");
  304. test_assert(fetch_from_buf_socks(buf, socks,
  305. get_options()->TestSocks,
  306. get_options()->SafeSocks) == -1);
  307. test_eq(0, socks->socks_version);
  308. test_eq(0, socks->replylen);
  309. test_eq(0, socks->reply[0]);
  310. test_eq(0, socks->reply[1]);
  311. done:
  312. ;
  313. }
  314. #define SOCKSENT(name) \
  315. { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
  316. struct testcase_t socks_tests[] = {
  317. SOCKSENT(4_unsupported_commands),
  318. SOCKSENT(4_supported_commands),
  319. SOCKSENT(5_unsupported_commands),
  320. SOCKSENT(5_supported_commands),
  321. SOCKSENT(5_no_authenticate),
  322. SOCKSENT(5_auth_before_negotiation),
  323. SOCKSENT(5_authenticate),
  324. SOCKSENT(5_authenticate_with_data),
  325. END_OF_TESTCASES
  326. };