test_socks.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "core/or/or.h"
  6. #include "lib/container/buffers.h"
  7. #include "app/config/config.h"
  8. #include "core/mainloop/connection.h"
  9. #include "core/proto/proto_socks.h"
  10. #include "test/test.h"
  11. #include "test/log_test_helpers.h"
  12. #include "core/or/socks_request_st.h"
  13. #include "lib/net/socks5_status.h"
  14. typedef struct socks_test_data_t {
  15. socks_request_t *req;
  16. buf_t *buf;
  17. } socks_test_data_t;
  18. static void *
  19. socks_test_setup(const struct testcase_t *testcase)
  20. {
  21. socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
  22. (void)testcase;
  23. data->buf = buf_new_with_capacity(256);
  24. data->req = socks_request_new();
  25. config_register_addressmaps(get_options());
  26. return data;
  27. }
  28. static int
  29. socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
  30. {
  31. socks_test_data_t *data = ptr;
  32. (void)testcase;
  33. buf_free(data->buf);
  34. socks_request_free(data->req);
  35. tor_free(data);
  36. return 1;
  37. }
  38. static const struct testcase_setup_t socks_setup = {
  39. socks_test_setup, socks_test_cleanup
  40. };
  41. #define SOCKS_TEST_INIT() \
  42. socks_test_data_t *testdata = ptr; \
  43. buf_t *buf = testdata->buf; \
  44. socks_request_t *socks = testdata->req;
  45. #define ADD_DATA(buf, s) \
  46. buf_add(buf, s, sizeof(s)-1)
  47. static void
  48. socks_request_clear(socks_request_t *socks)
  49. {
  50. tor_free(socks->username);
  51. tor_free(socks->password);
  52. memset(socks, 0, sizeof(socks_request_t));
  53. }
  54. /** Perform unsupported SOCKS 4 commands */
  55. static void
  56. test_socks_4_unsupported_commands(void *ptr)
  57. {
  58. SOCKS_TEST_INIT();
  59. /* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
  60. ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
  61. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  62. get_options()->SafeSocks),
  63. OP_EQ, -1);
  64. tt_int_op(4,OP_EQ, socks->socks_version);
  65. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  66. done:
  67. ;
  68. }
  69. /** Perform supported SOCKS 4 commands */
  70. static void
  71. test_socks_4_supported_commands(void *ptr)
  72. {
  73. SOCKS_TEST_INIT();
  74. tt_int_op(0,OP_EQ, buf_datalen(buf));
  75. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
  76. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
  77. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  78. get_options()->SafeSocks),
  79. OP_EQ, 1);
  80. tt_int_op(4,OP_EQ, socks->socks_version);
  81. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  82. tt_int_op(SOCKS_COMMAND_CONNECT,OP_EQ, socks->command);
  83. tt_str_op("2.2.2.3",OP_EQ, socks->address);
  84. tt_int_op(4370,OP_EQ, socks->port);
  85. tt_assert(socks->got_auth == 0);
  86. tt_assert(! socks->username);
  87. tt_int_op(0,OP_EQ, buf_datalen(buf));
  88. socks_request_clear(socks);
  89. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
  90. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
  91. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0),
  92. OP_EQ, 1);
  93. tt_int_op(4,OP_EQ, socks->socks_version);
  94. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  95. tt_int_op(SOCKS_COMMAND_CONNECT,OP_EQ, socks->command);
  96. tt_str_op("2.2.2.4",OP_EQ, socks->address);
  97. tt_int_op(4370,OP_EQ, socks->port);
  98. tt_assert(socks->got_auth == 1);
  99. tt_assert(socks->username);
  100. tt_int_op(2,OP_EQ, socks->usernamelen);
  101. tt_mem_op("me",OP_EQ, socks->username, 2);
  102. tt_int_op(0,OP_EQ, buf_datalen(buf));
  103. socks_request_clear(socks);
  104. /* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
  105. ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
  106. tt_int_op(fetch_from_buf_socks(buf, socks, 1,
  107. get_options()->SafeSocks),
  108. OP_EQ, 1);
  109. tt_int_op(4,OP_EQ, socks->socks_version);
  110. tt_int_op(0,OP_EQ, socks->replylen); /* XXX: shouldn't tor reply? */
  111. tt_str_op("torproject.org",OP_EQ, socks->address);
  112. tt_int_op(0,OP_EQ, buf_datalen(buf));
  113. done:
  114. ;
  115. }
  116. static void
  117. test_socks_4_bad_arguments(void *ptr)
  118. {
  119. SOCKS_TEST_INIT();
  120. setup_capture_of_logs(LOG_DEBUG);
  121. /* Try with 0 IPv4 address */
  122. ADD_DATA(buf, "\x04\x01\x00\x50\x00\x00\x00\x00\x00");
  123. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  124. get_options()->SafeSocks),
  125. OP_EQ, -1);
  126. buf_clear(buf);
  127. expect_log_msg_containing("Port or DestIP is zero.");
  128. mock_clean_saved_logs();
  129. /* Try with 0 port */
  130. ADD_DATA(buf, "\x04\x01\x00\x00\x01\x02\x03\x04\x00");
  131. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  132. get_options()->SafeSocks),
  133. OP_EQ, -1);
  134. buf_clear(buf);
  135. expect_log_msg_containing("Port or DestIP is zero.");
  136. mock_clean_saved_logs();
  137. /* Try with 2000-byte username (!) */
  138. ADD_DATA(buf, "\x04\x01\x00\x50\x01\x02\x03\x04");
  139. int i;
  140. for (i = 0; i < 200; ++i) {
  141. ADD_DATA(buf, "1234567890");
  142. }
  143. ADD_DATA(buf, "\x00");
  144. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0),
  145. OP_EQ, -1);
  146. buf_clear(buf);
  147. expect_log_msg_containing("user name too long; rejecting.");
  148. mock_clean_saved_logs();
  149. /* Try with 2000-byte hostname */
  150. ADD_DATA(buf, "\x04\x01\x00\x50\x00\x00\x00\x01\x00");
  151. for (i = 0; i < 200; ++i) {
  152. ADD_DATA(buf, "1234567890");
  153. }
  154. ADD_DATA(buf, "\x00");
  155. {
  156. const char *p;
  157. size_t s;
  158. buf_pullup(buf, 9999, &p, &s);
  159. }
  160. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0),
  161. OP_EQ, -1);
  162. buf_clear(buf);
  163. expect_log_msg_containing("Destaddr too long. Rejecting.");
  164. mock_clean_saved_logs();
  165. /* Try with 2000-byte hostname, not terminated. */
  166. ADD_DATA(buf, "\x04\x01\x00\x50\x00\x00\x00\x01\x00");
  167. for (i = 0; i < 200; ++i) {
  168. ADD_DATA(buf, "1234567890");
  169. }
  170. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0),
  171. OP_EQ, -1);
  172. buf_clear(buf);
  173. expect_log_msg_containing("Destaddr too long.");
  174. mock_clean_saved_logs();
  175. /* Socks4, bogus hostname */
  176. ADD_DATA(buf, "\x04\x01\x00\x50\x00\x00\x00\x01\x00" "---\x00" );
  177. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0), OP_EQ, -1);
  178. buf_clear(buf);
  179. expect_log_msg_containing("Your application (using socks4 to port 80) "
  180. "gave Tor a malformed hostname: ");
  181. mock_clean_saved_logs();
  182. done:
  183. teardown_capture_of_logs();
  184. }
  185. /** Perform unsupported SOCKS 5 commands */
  186. static void
  187. test_socks_5_unsupported_commands(void *ptr)
  188. {
  189. SOCKS_TEST_INIT();
  190. /* SOCKS 5 Send unsupported BIND [02] command */
  191. ADD_DATA(buf, "\x05\x02\x00\x01");
  192. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  193. get_options()->SafeSocks),OP_EQ, 0);
  194. tt_int_op(0,OP_EQ, buf_datalen(buf));
  195. tt_int_op(5,OP_EQ, socks->socks_version);
  196. tt_int_op(2,OP_EQ, socks->replylen);
  197. tt_int_op(5,OP_EQ, socks->reply[0]);
  198. tt_int_op(0,OP_EQ, socks->reply[1]);
  199. ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
  200. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  201. get_options()->SafeSocks),OP_EQ, -1);
  202. tt_int_op(5,OP_EQ,socks->socks_version);
  203. tt_int_op(10,OP_EQ,socks->replylen);
  204. tt_int_op(5,OP_EQ,socks->reply[0]);
  205. tt_int_op(SOCKS5_COMMAND_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  206. tt_int_op(1,OP_EQ,socks->reply[3]);
  207. buf_clear(buf);
  208. socks_request_clear(socks);
  209. /* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
  210. ADD_DATA(buf, "\x05\x02\x00\x01");
  211. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  212. get_options()->SafeSocks),OP_EQ, 0);
  213. tt_int_op(5,OP_EQ, socks->socks_version);
  214. tt_int_op(2,OP_EQ, socks->replylen);
  215. tt_int_op(5,OP_EQ, socks->reply[0]);
  216. tt_int_op(0,OP_EQ, socks->reply[1]);
  217. ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
  218. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  219. get_options()->SafeSocks),OP_EQ, -1);
  220. tt_int_op(5,OP_EQ,socks->socks_version);
  221. tt_int_op(10,OP_EQ,socks->replylen);
  222. tt_int_op(5,OP_EQ,socks->reply[0]);
  223. tt_int_op(SOCKS5_COMMAND_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  224. tt_int_op(1,OP_EQ,socks->reply[3]);
  225. done:
  226. ;
  227. }
  228. /** Perform supported SOCKS 5 commands */
  229. static void
  230. test_socks_5_supported_commands(void *ptr)
  231. {
  232. SOCKS_TEST_INIT();
  233. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  234. ADD_DATA(buf, "\x05\x01\x00");
  235. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  236. get_options()->SafeSocks),OP_EQ, 0);
  237. tt_int_op(5,OP_EQ, socks->socks_version);
  238. tt_int_op(2,OP_EQ, socks->replylen);
  239. tt_int_op(5,OP_EQ, socks->reply[0]);
  240. tt_int_op(0,OP_EQ, socks->reply[1]);
  241. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  242. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  243. get_options()->SafeSocks),OP_EQ, 1);
  244. tt_str_op("2.2.2.2",OP_EQ, socks->address);
  245. tt_int_op(4369,OP_EQ, socks->port);
  246. tt_int_op(0,OP_EQ, buf_datalen(buf));
  247. socks_request_clear(socks);
  248. /* SOCKS 5 Send CONNECT [01] to one of the ipv6 addresses for
  249. torproject.org:80 */
  250. ADD_DATA(buf, "\x05\x01\x00");
  251. ADD_DATA(buf, "\x05\x01\x00\x04"
  252. "\x20\x02\x41\xb8\x02\x02\x0d\xeb\x02\x13\x21\xff\xfe\x20\x14\x26"
  253. "\x00\x50");
  254. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  255. get_options()->SafeSocks),OP_EQ, 1);
  256. tt_int_op(5,OP_EQ, socks->socks_version);
  257. tt_int_op(2,OP_EQ, socks->replylen);
  258. tt_int_op(5,OP_EQ, socks->reply[0]);
  259. tt_int_op(0,OP_EQ, socks->reply[1]);
  260. tt_str_op("[2002:41b8:202:deb:213:21ff:fe20:1426]",OP_EQ, socks->address);
  261. tt_int_op(80,OP_EQ, socks->port);
  262. tt_int_op(0,OP_EQ, buf_datalen(buf));
  263. socks_request_clear(socks);
  264. /* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
  265. ADD_DATA(buf, "\x05\x01\x00");
  266. ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
  267. tt_int_op(fetch_from_buf_socks(buf, socks, 1,
  268. get_options()->SafeSocks),OP_EQ, 1);
  269. tt_int_op(5,OP_EQ, socks->socks_version);
  270. tt_int_op(2,OP_EQ, socks->replylen);
  271. tt_int_op(5,OP_EQ, socks->reply[0]);
  272. tt_int_op(0,OP_EQ, socks->reply[1]);
  273. tt_str_op("torproject.org",OP_EQ, socks->address);
  274. tt_int_op(4369,OP_EQ, socks->port);
  275. tt_int_op(0,OP_EQ, buf_datalen(buf));
  276. socks_request_clear(socks);
  277. /* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
  278. ADD_DATA(buf, "\x05\x01\x00");
  279. ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
  280. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  281. get_options()->SafeSocks),
  282. OP_EQ, 1);
  283. tt_int_op(5,OP_EQ, socks->socks_version);
  284. tt_int_op(2,OP_EQ, socks->replylen);
  285. tt_int_op(5,OP_EQ, socks->reply[0]);
  286. tt_int_op(0,OP_EQ, socks->reply[1]);
  287. tt_str_op("torproject.org",OP_EQ, socks->address);
  288. tt_int_op(0,OP_EQ, buf_datalen(buf));
  289. socks_request_clear(socks);
  290. /* SOCKS 5 Should NOT reject RESOLVE [F0] request for IPv4 address
  291. * string if SafeSocks is enabled. */
  292. ADD_DATA(buf, "\x05\x01\x00");
  293. ADD_DATA(buf, "\x05\xF0\x00\x03\x07");
  294. ADD_DATA(buf, "8.8.8.8");
  295. ADD_DATA(buf, "\x11\x11");
  296. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  297. OP_EQ, 1);
  298. tt_str_op("8.8.8.8", OP_EQ, socks->address);
  299. tt_int_op(4369, OP_EQ, socks->port);
  300. tt_int_op(0, OP_EQ, buf_datalen(buf));
  301. socks_request_clear(socks);
  302. /* SOCKS 5 should NOT reject RESOLVE [F0] request for IPv6 address
  303. * string if SafeSocks is enabled. */
  304. ADD_DATA(buf, "\x05\x01\x00");
  305. ADD_DATA(buf, "\x05\xF0\x00\x03\x29");
  306. ADD_DATA(buf, "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]");
  307. ADD_DATA(buf, "\x01\x02");
  308. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  309. OP_EQ, 1);
  310. tt_str_op("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", OP_EQ,
  311. socks->address);
  312. tt_int_op(258, OP_EQ, socks->port);
  313. tt_int_op(0, OP_EQ, buf_datalen(buf));
  314. socks_request_clear(socks);
  315. /* Also allow bracket-less form. */
  316. ADD_DATA(buf, "\x05\x01\x00");
  317. ADD_DATA(buf, "\x05\xF0\x00\x03\x27");
  318. ADD_DATA(buf, "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  319. ADD_DATA(buf, "\x01\x02");
  320. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  321. OP_EQ, 1);
  322. tt_str_op("2001:0db8:85a3:0000:0000:8a2e:0370:7334", OP_EQ,
  323. socks->address);
  324. tt_int_op(258, OP_EQ, socks->port);
  325. tt_int_op(0, OP_EQ, buf_datalen(buf));
  326. socks_request_clear(socks);
  327. /* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
  328. ADD_DATA(buf, "\x05\x01\x00");
  329. ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
  330. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  331. get_options()->SafeSocks),
  332. OP_EQ, 1);
  333. tt_int_op(5,OP_EQ, socks->socks_version);
  334. tt_int_op(2,OP_EQ, socks->replylen);
  335. tt_int_op(5,OP_EQ, socks->reply[0]);
  336. tt_int_op(0,OP_EQ, socks->reply[1]);
  337. tt_str_op("2.2.2.5",OP_EQ, socks->address);
  338. tt_int_op(0,OP_EQ, buf_datalen(buf));
  339. done:
  340. ;
  341. }
  342. /** Perform SOCKS 5 authentication */
  343. static void
  344. test_socks_5_no_authenticate(void *ptr)
  345. {
  346. SOCKS_TEST_INIT();
  347. /*SOCKS 5 No Authentication */
  348. ADD_DATA(buf,"\x05\x01\x00");
  349. tt_assert(!fetch_from_buf_socks(buf, socks,
  350. get_options()->TestSocks,
  351. get_options()->SafeSocks));
  352. tt_int_op(2,OP_EQ, socks->replylen);
  353. tt_int_op(5,OP_EQ, socks->reply[0]);
  354. tt_int_op(SOCKS_NO_AUTH,OP_EQ, socks->reply[1]);
  355. tt_int_op(0,OP_EQ, buf_datalen(buf));
  356. /*SOCKS 5 Send username/password anyway - pretend to be broken */
  357. ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
  358. tt_assert(!fetch_from_buf_socks(buf, socks,
  359. get_options()->TestSocks,
  360. get_options()->SafeSocks));
  361. tt_int_op(5,OP_EQ, socks->socks_version);
  362. tt_int_op(2,OP_EQ, socks->replylen);
  363. tt_int_op(1,OP_EQ, socks->reply[0]);
  364. tt_int_op(0,OP_EQ, socks->reply[1]);
  365. tt_int_op(2,OP_EQ, socks->usernamelen);
  366. tt_int_op(2,OP_EQ, socks->passwordlen);
  367. tt_mem_op("\x01\x01",OP_EQ, socks->username, 2);
  368. tt_mem_op("\x01\x01",OP_EQ, socks->password, 2);
  369. done:
  370. ;
  371. }
  372. /** Perform SOCKS 5 authentication */
  373. static void
  374. test_socks_5_authenticate(void *ptr)
  375. {
  376. SOCKS_TEST_INIT();
  377. /* SOCKS 5 Negotiate username/password authentication */
  378. ADD_DATA(buf, "\x05\x01\x02");
  379. tt_assert(!fetch_from_buf_socks(buf, socks,
  380. get_options()->TestSocks,
  381. get_options()->SafeSocks));
  382. tt_int_op(2,OP_EQ, socks->replylen);
  383. tt_int_op(5,OP_EQ, socks->reply[0]);
  384. tt_int_op(SOCKS_USER_PASS,OP_EQ, socks->reply[1]);
  385. tt_int_op(5,OP_EQ, socks->socks_version);
  386. tt_int_op(0,OP_EQ, buf_datalen(buf));
  387. /* SOCKS 5 Send username/password */
  388. ADD_DATA(buf, "\x01\x02me\x08mypasswd");
  389. tt_assert(!fetch_from_buf_socks(buf, socks,
  390. get_options()->TestSocks,
  391. get_options()->SafeSocks));
  392. tt_int_op(5,OP_EQ, socks->socks_version);
  393. tt_int_op(2,OP_EQ, socks->replylen);
  394. tt_int_op(1,OP_EQ, socks->reply[0]);
  395. tt_int_op(0,OP_EQ, socks->reply[1]);
  396. tt_int_op(2,OP_EQ, socks->usernamelen);
  397. tt_int_op(8,OP_EQ, socks->passwordlen);
  398. tt_mem_op("me",OP_EQ, socks->username, 2);
  399. tt_mem_op("mypasswd",OP_EQ, socks->password, 8);
  400. done:
  401. ;
  402. }
  403. /** Perform SOCKS 5 authentication and send data all in one go */
  404. static void
  405. test_socks_5_authenticate_with_data(void *ptr)
  406. {
  407. SOCKS_TEST_INIT();
  408. /* SOCKS 5 Negotiate username/password authentication */
  409. ADD_DATA(buf, "\x05\x01\x02");
  410. tt_assert(!fetch_from_buf_socks(buf, socks,
  411. get_options()->TestSocks,
  412. get_options()->SafeSocks));
  413. tt_int_op(2,OP_EQ, socks->replylen);
  414. tt_int_op(5,OP_EQ, socks->reply[0]);
  415. tt_int_op(SOCKS_USER_PASS,OP_EQ, socks->reply[1]);
  416. tt_int_op(5,OP_EQ, socks->socks_version);
  417. tt_int_op(0,OP_EQ, buf_datalen(buf));
  418. /* SOCKS 5 Send username/password */
  419. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  420. ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  421. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  422. get_options()->SafeSocks),
  423. OP_EQ, 1);
  424. tt_int_op(5,OP_EQ, socks->socks_version);
  425. tt_int_op(2,OP_EQ, socks->replylen);
  426. tt_int_op(1,OP_EQ, socks->reply[0]);
  427. tt_int_op(0,OP_EQ, socks->reply[1]);
  428. tt_str_op("2.2.2.2",OP_EQ, socks->address);
  429. tt_int_op(4369,OP_EQ, socks->port);
  430. tt_int_op(2,OP_EQ, socks->usernamelen);
  431. tt_int_op(3,OP_EQ, socks->passwordlen);
  432. tt_mem_op("me",OP_EQ, socks->username, 2);
  433. tt_mem_op("you",OP_EQ, socks->password, 3);
  434. done:
  435. ;
  436. }
  437. /** Try to negotiate an unsupported authentication type */
  438. static void
  439. test_socks_5_auth_unsupported_type(void *ptr)
  440. {
  441. SOCKS_TEST_INIT();
  442. /* None of these authentication types are recognized. */
  443. ADD_DATA(buf, "\x05\x03\x99\x21\x10");
  444. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  445. get_options()->SafeSocks),
  446. OP_EQ, -1);
  447. tt_int_op(0,OP_EQ, socks->socks_version);
  448. tt_int_op(2,OP_EQ, socks->replylen);
  449. tt_int_op(5,OP_EQ, socks->reply[0]);
  450. tt_int_op(0xff,OP_EQ, socks->reply[1]);
  451. done:
  452. ;
  453. }
  454. /** Try to negotiate an unsupported version of username/password auth. */
  455. static void
  456. test_socks_5_auth_unsupported_version(void *ptr)
  457. {
  458. SOCKS_TEST_INIT();
  459. /* Negotiate username/password */
  460. ADD_DATA(buf, "\x05\x01\x02");
  461. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  462. get_options()->SafeSocks),
  463. OP_EQ, 0);
  464. tt_int_op(0,OP_EQ, buf_datalen(buf)); /* buf should be drained */
  465. /* Now, suggest an unrecognized username/password version */
  466. ADD_DATA(buf, "\x02\x05" "hello" "\x05" "world");
  467. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  468. get_options()->SafeSocks),
  469. OP_EQ, -1);
  470. done:
  471. ;
  472. }
  473. /** Perform SOCKS 5 authentication before method negotiated */
  474. static void
  475. test_socks_5_auth_before_negotiation(void *ptr)
  476. {
  477. SOCKS_TEST_INIT();
  478. /* SOCKS 5 Send username/password */
  479. ADD_DATA(buf, "\x01\x02me\x02me");
  480. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  481. get_options()->SafeSocks),
  482. OP_EQ, -1);
  483. tt_int_op(0,OP_EQ, socks->socks_version);
  484. tt_int_op(0,OP_EQ, socks->replylen);
  485. tt_int_op(0,OP_EQ, socks->reply[0]);
  486. tt_int_op(0,OP_EQ, socks->reply[1]);
  487. done:
  488. ;
  489. }
  490. /** Perform malformed SOCKS 5 commands */
  491. static void
  492. test_socks_5_malformed_commands(void *ptr)
  493. {
  494. SOCKS_TEST_INIT();
  495. /* XXX: Stringified address length > MAX_SOCKS_ADDR_LEN will never happen */
  496. /** SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369, with SafeSocks set
  497. */
  498. ADD_DATA(buf, "\x05\x01\x00");
  499. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  500. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  501. OP_EQ, -1);
  502. tt_int_op(5,OP_EQ,socks->socks_version);
  503. tt_int_op(10,OP_EQ,socks->replylen);
  504. tt_int_op(5,OP_EQ,socks->reply[0]);
  505. tt_int_op(SOCKS5_NOT_ALLOWED,OP_EQ,socks->reply[1]);
  506. tt_int_op(1,OP_EQ,socks->reply[3]);
  507. buf_clear(buf);
  508. socks_request_clear(socks);
  509. /* SOCKS 5 Send RESOLVE_PTR [F1] for FQDN torproject.org */
  510. ADD_DATA(buf, "\x05\x01\x00");
  511. ADD_DATA(buf, "\x05\xF1\x00\x03\x0Etorproject.org\x11\x11");
  512. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  513. get_options()->SafeSocks),OP_EQ, -1);
  514. tt_int_op(5,OP_EQ,socks->socks_version);
  515. tt_int_op(10,OP_EQ,socks->replylen);
  516. tt_int_op(5,OP_EQ,socks->reply[0]);
  517. tt_int_op(SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  518. tt_int_op(1,OP_EQ,socks->reply[3]);
  519. buf_clear(buf);
  520. socks_request_clear(socks);
  521. /* XXX: len + 1 > MAX_SOCKS_ADDR_LEN (FQDN request) will never happen */
  522. /* SOCKS 5 Send CONNECT [01] to FQDN """"".com */
  523. ADD_DATA(buf, "\x05\x01\x00");
  524. ADD_DATA(buf, "\x05\x01\x00\x03\x09\"\"\"\"\".com\x11\x11");
  525. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  526. get_options()->SafeSocks),OP_EQ, -1);
  527. tt_int_op(5,OP_EQ,socks->socks_version);
  528. tt_int_op(10,OP_EQ,socks->replylen);
  529. tt_int_op(5,OP_EQ,socks->reply[0]);
  530. tt_int_op(SOCKS5_GENERAL_ERROR,OP_EQ,socks->reply[1]);
  531. tt_int_op(1,OP_EQ,socks->reply[3]);
  532. buf_clear(buf);
  533. socks_request_clear(socks);
  534. /* SOCKS 5 Send CONNECT [01] to address type 0x23 */
  535. ADD_DATA(buf, "\x05\x01\x00");
  536. ADD_DATA(buf, "\x05\x01\x00\x23\x02\x02\x02\x02\x11\x11");
  537. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  538. get_options()->SafeSocks),OP_EQ, -1);
  539. tt_int_op(5,OP_EQ,socks->socks_version);
  540. tt_int_op(10,OP_EQ,socks->replylen);
  541. tt_int_op(5,OP_EQ,socks->reply[0]);
  542. tt_int_op(SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  543. tt_int_op(1,OP_EQ,socks->reply[3]);
  544. done:
  545. ;
  546. }
  547. static void
  548. test_socks_5_bad_arguments(void *ptr)
  549. {
  550. SOCKS_TEST_INIT();
  551. setup_capture_of_logs(LOG_DEBUG);
  552. /* Socks5, bogus hostname */
  553. ADD_DATA(buf, "\x05\x01\x00" "\x05\x01\x00\x03\x03" "---" "\x00\x50" );
  554. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0), OP_EQ, -1);
  555. buf_clear(buf);
  556. expect_log_msg_containing("Your application (using socks5 to port 80) "
  557. "gave Tor a malformed hostname: ");
  558. mock_clean_saved_logs();
  559. socks_request_clear(socks);
  560. done:
  561. teardown_capture_of_logs();
  562. }
  563. /** check for correct behavior when the socks command has not arrived. */
  564. static void
  565. test_socks_truncated(void *ptr)
  566. {
  567. const struct {
  568. enum { NONE, AUTH, ALL } setup;
  569. const char *body;
  570. size_t len;
  571. } commands[] = {
  572. /* SOCKS4 */
  573. /* Connect, to an IP. */
  574. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04\x00", 9},
  575. /* Connect, to an IP, with authentication. */
  576. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04hello\x00", 14},
  577. /* SOCKS4A */
  578. /* Connect, to a hostname */
  579. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01\x00www.example.com\x00", 25},
  580. /* Connect, to a hostname, with authentication */
  581. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01hi\x00www.example.com\x00", 27},
  582. /* SOCKS5 */
  583. /* initial handshake */
  584. { NONE, "\x05\x00", 2 },
  585. /* no-auth handshake */
  586. { NONE, "\x05\x03\x99\x21\x10", 5 },
  587. /* SOCSK5, username-password, all empty. */
  588. { AUTH, "\x01\x00\x00", 3 },
  589. /* SOCSK5, username-password, 1 char each. */
  590. { AUTH, "\x01\x01x\x01y", 5 },
  591. /* SOCSK5, username-password, max length. */
  592. { AUTH, "\x01\xff"
  593. "Ogni tempo ha il suo fascismo: se ne notano i segni premonitori "
  594. "dovunque la concentrazione di potere nega al cittadino la "
  595. "possibilit\xc3\xa0 e la capacit\xc3\xa0 di esprimere ed attuare la "
  596. "sua volont\xc3\xa0. A questo si arriva in molti modi, non "
  597. "necessariamente col terror"
  598. "\xff"
  599. "e dell'intimidazione poliziesca, ma anche negando o distorcendo "
  600. "l'informazione, inquinando la giustizia, paralizzando la scuola, "
  601. "diffondendo in molti modi sottili la nostalgia per un mondo in cui "
  602. "regnava sovrano l'ordine, ed in cui la sicurezza dei pochi "
  603. /* privilegiati riposava sul lavoro forzato e sul silenzio forzato dei
  604. molti. -- Primo Levi */ , 513 },
  605. /* Socks5, IPv4 address */
  606. { ALL, "\x05\x01\x00\x01\x01\x02\x03\x04\x20\x20", 10 },
  607. /* Socks5, IPv6 address */
  608. { ALL, "\x05\x01\x00\x04"
  609. "\x49\x20\x48\x41\x5a\x20\x45\x41\x53\x54\x45\x52\x20\x45\x47\x47"
  610. "\x20\x20", 22 },
  611. /* Socks5, hostname, empty. */
  612. { ALL, "\x05\x01\x00\x03" "\x00" "\x00\x50", 7 },
  613. /* Socks5, hostname, moderate. */
  614. { ALL, "\x05\x01\x00\x03" "\x11" "onion.example.com" "\x00\x50", 24 },
  615. /* Socks5, hostname, maximum. */
  616. { ALL, "\x05\x01\x00\x03" "\xff"
  617. "whatsoever.I.shall.see.or.hear.in.the.course.of.my.profession.as.well."
  618. "as.outside.my.profession.in.my.intercourse.with.men.if.it.be.what."
  619. "should.not.be.published.abroad.I.will.never.divulge.holding.such."
  620. "things.to.be.holy.secrets.x.hippocratic.oath.wikipedia"
  621. "\x00\x50", 262 },
  622. };
  623. unsigned i, j;
  624. SOCKS_TEST_INIT();
  625. for (i = 0; i < ARRAY_LENGTH(commands); ++i) {
  626. for (j = 0; j < commands[i].len; ++j) {
  627. switch (commands[i].setup) {
  628. default: /* Falls through */
  629. case NONE:
  630. /* This test calls for no setup on the socks state. */
  631. break;
  632. case AUTH:
  633. /* This test calls for the socks state to be waiting for
  634. * username/password authentication */
  635. ADD_DATA(buf, "\x05\x01\x02");
  636. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  637. tt_int_op(0, OP_EQ, buf_datalen(buf));
  638. break;
  639. case ALL:
  640. /* This test calls for the socks state to be waiting for
  641. * the connection request */
  642. ADD_DATA(buf, "\x05\x01\x00");
  643. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  644. tt_int_op(0, OP_EQ, buf_datalen(buf));
  645. }
  646. TT_BLATHER(("Checking command %u, length %u, omitting char %u", i, j,
  647. (unsigned)commands[i].body[j]));
  648. buf_add(buf, commands[i].body, j);
  649. /* This should return 0 meaning "not done yet" */
  650. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  651. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  652. buf_clear(buf);
  653. socks_request_free(testdata->req);
  654. socks = testdata->req = socks_request_new();
  655. }
  656. }
  657. done:
  658. ;
  659. }
  660. static void
  661. test_socks_wrong_protocol(void *ptr)
  662. {
  663. SOCKS_TEST_INIT();
  664. setup_capture_of_logs(LOG_DEBUG);
  665. /* HTTP request. */
  666. ADD_DATA(buf, "GET /index.html HTTP/1.0" );
  667. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0), OP_EQ, -1);
  668. buf_clear(buf);
  669. expect_log_msg_containing("Socks version 71 not recognized. "
  670. "(This port is not an HTTP proxy;");
  671. mock_clean_saved_logs();
  672. socks_request_clear(socks);
  673. done:
  674. teardown_capture_of_logs();
  675. }
  676. /* Check our client-side socks4 parsing (that is to say, our parsing of
  677. * server responses).
  678. */
  679. static void
  680. test_socks_client_v4(void *arg)
  681. {
  682. (void)arg;
  683. buf_t *buf = buf_new();
  684. char *reason = NULL;
  685. /* Legit socks4 response, success */
  686. ADD_DATA(buf, "\x04\x5a\x20\x25\x01\x02\x03\x04");
  687. tt_int_op(1, OP_EQ,
  688. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  689. &reason));
  690. tt_ptr_op(reason, OP_EQ, NULL);
  691. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  692. /* Legit socks4 response, failure. */
  693. ADD_DATA(buf, "\x04\x5b\x20\x25\x01\x02\x03\x04");
  694. tt_int_op(-1, OP_EQ,
  695. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  696. &reason));
  697. tt_ptr_op(reason, OP_NE, NULL);
  698. tt_str_op(reason, OP_EQ, "server rejected connection");
  699. done:
  700. buf_free(buf);
  701. tor_free(reason);
  702. }
  703. /* Check our client-side socks5 authentication-negotiation parsing (that is to
  704. * say, our parsing of server responses).
  705. */
  706. static void
  707. test_socks_client_v5_auth(void *arg)
  708. {
  709. (void)arg;
  710. buf_t *buf = buf_new();
  711. char *reason = NULL;
  712. /* Legit socks5 responses, got a method we like. */
  713. ADD_DATA(buf, "\x05\x00");
  714. tt_int_op(1, OP_EQ,
  715. fetch_from_buf_socks_client(buf,
  716. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  717. &reason));
  718. tt_ptr_op(reason, OP_EQ, NULL);
  719. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  720. /* Same, but we wanted something else. */
  721. ADD_DATA(buf, "\x05\x00");
  722. tt_int_op(1, OP_EQ,
  723. fetch_from_buf_socks_client(buf,
  724. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  725. &reason));
  726. tt_ptr_op(reason, OP_EQ, NULL);
  727. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  728. /* Same, and they offered a password. */
  729. ADD_DATA(buf, "\x05\x02");
  730. tt_int_op(2, OP_EQ,
  731. fetch_from_buf_socks_client(buf,
  732. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  733. &reason));
  734. tt_ptr_op(reason, OP_EQ, NULL);
  735. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  736. /* They rejected our method, or selected something we don't know. */
  737. ADD_DATA(buf, "\x05\xff");
  738. tt_int_op(-1, OP_EQ,
  739. fetch_from_buf_socks_client(buf,
  740. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  741. &reason));
  742. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  743. "authentication methods");
  744. buf_clear(buf);
  745. tor_free(reason);
  746. ADD_DATA(buf, "\x05\xff");
  747. tt_int_op(-1, OP_EQ,
  748. fetch_from_buf_socks_client(buf,
  749. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  750. &reason));
  751. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  752. "authentication methods");
  753. tor_free(reason);
  754. buf_clear(buf);
  755. /* Now check for authentication responses: check success and failure. */
  756. ADD_DATA(buf, "\x01\x00");
  757. tt_int_op(1, OP_EQ,
  758. fetch_from_buf_socks_client(buf,
  759. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  760. &reason));
  761. tt_ptr_op(reason, OP_EQ, NULL);
  762. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  763. ADD_DATA(buf, "\x01\xf0");
  764. tt_int_op(-1, OP_EQ,
  765. fetch_from_buf_socks_client(buf,
  766. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  767. &reason));
  768. tt_ptr_op(reason, OP_NE, NULL);
  769. tt_str_op(reason, OP_EQ, "authentication failed");
  770. done:
  771. buf_free(buf);
  772. tor_free(reason);
  773. }
  774. /* Check our client-side socks5 connect parsing (that is to say, our parsing
  775. * of server responses).
  776. */
  777. static void
  778. test_socks_client_v5_connect(void *arg)
  779. {
  780. (void)arg;
  781. buf_t *buf = buf_new();
  782. char *reason = NULL;
  783. /* Legit socks5 responses, success, ipv4. */
  784. ADD_DATA(buf, "\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05");
  785. tt_int_op(1, OP_EQ,
  786. fetch_from_buf_socks_client(buf,
  787. PROXY_SOCKS5_WANT_CONNECT_OK,
  788. &reason));
  789. tt_ptr_op(reason, OP_EQ, NULL);
  790. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  791. /* Legit socks5 responses, success, ipv6. */
  792. ADD_DATA(buf, "\x05\x00\x00\x04"
  793. "abcdefghijklmnop"
  794. "\x00\x05");
  795. tt_int_op(1, OP_EQ,
  796. fetch_from_buf_socks_client(buf,
  797. PROXY_SOCKS5_WANT_CONNECT_OK,
  798. &reason));
  799. tt_ptr_op(reason, OP_EQ, NULL);
  800. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  801. /* Legit socks5 responses, success, hostname. */
  802. ADD_DATA(buf, "\x05\x00\x00\x03\x12"
  803. "gopher.example.com"
  804. "\x00\x05");
  805. tt_int_op(1, OP_EQ,
  806. fetch_from_buf_socks_client(buf,
  807. PROXY_SOCKS5_WANT_CONNECT_OK,
  808. &reason));
  809. tt_ptr_op(reason, OP_EQ, NULL);
  810. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  811. /* Legit socks5 responses, failure, hostname. */
  812. ADD_DATA(buf, "\x05\x03\x00\x03\x12"
  813. "gopher.example.com"
  814. "\x00\x05");
  815. tt_int_op(-1, OP_EQ,
  816. fetch_from_buf_socks_client(buf,
  817. PROXY_SOCKS5_WANT_CONNECT_OK,
  818. &reason));
  819. tt_ptr_op(reason, OP_NE, NULL);
  820. tt_str_op(reason, OP_EQ, "Network unreachable");
  821. tor_free(reason);
  822. buf_clear(buf);
  823. /* Bogus socks5 responses: what is address type 0x17? */
  824. ADD_DATA(buf, "\x05\x03\x00\x17\x12 blah blah");
  825. tt_int_op(-1, OP_EQ,
  826. fetch_from_buf_socks_client(buf,
  827. PROXY_SOCKS5_WANT_CONNECT_OK,
  828. &reason));
  829. tt_ptr_op(reason, OP_NE, NULL);
  830. tt_str_op(reason, OP_EQ, "invalid response to connect request");
  831. buf_clear(buf);
  832. done:
  833. buf_free(buf);
  834. tor_free(reason);
  835. }
  836. static void
  837. test_socks_client_truncated(void *arg)
  838. {
  839. (void)arg;
  840. buf_t *buf = buf_new();
  841. char *reason = NULL;
  842. #define S(str) str, (sizeof(str)-1)
  843. const struct {
  844. int state;
  845. const char *body;
  846. size_t len;
  847. } replies[] = {
  848. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5a\x20\x25\x01\x02\x03\x04") },
  849. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5b\x20\x25\x01\x02\x03\x04") },
  850. { PROXY_SOCKS5_WANT_AUTH_METHOD_NONE, S("\x05\x00") },
  851. { PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929, S("\x05\x00") },
  852. { PROXY_SOCKS5_WANT_AUTH_RFC1929_OK, S("\x01\x00") },
  853. { PROXY_SOCKS5_WANT_CONNECT_OK,
  854. S("\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05") },
  855. { PROXY_SOCKS5_WANT_CONNECT_OK,
  856. S("\x05\x00\x00\x04" "abcdefghijklmnop" "\x00\x05") },
  857. { PROXY_SOCKS5_WANT_CONNECT_OK,
  858. S("\x05\x00\x00\x03\x12" "gopher.example.com" "\x00\x05") },
  859. { PROXY_SOCKS5_WANT_CONNECT_OK,
  860. S("\x05\x03\x00\x03\x12" "gopher.example.com""\x00\x05") },
  861. { PROXY_SOCKS5_WANT_CONNECT_OK,
  862. S("\x05\x03\x00\x17") },
  863. };
  864. unsigned i, j;
  865. for (i = 0; i < ARRAY_LENGTH(replies); ++i) {
  866. for (j = 0; j < replies[i].len; ++j) {
  867. TT_BLATHER(("Checking command %u, length %u", i, j));
  868. buf_add(buf, replies[i].body, j);
  869. /* This should return 0 meaning "not done yet" */
  870. tt_int_op(0, OP_EQ,
  871. fetch_from_buf_socks_client(buf, replies[i].state, &reason));
  872. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  873. buf_clear(buf);
  874. tt_ptr_op(reason, OP_EQ, NULL);
  875. }
  876. }
  877. done:
  878. tor_free(reason);
  879. buf_free(buf);
  880. }
  881. #define SOCKSENT(name) \
  882. { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
  883. struct testcase_t socks_tests[] = {
  884. SOCKSENT(4_unsupported_commands),
  885. SOCKSENT(4_supported_commands),
  886. SOCKSENT(4_bad_arguments),
  887. SOCKSENT(5_unsupported_commands),
  888. SOCKSENT(5_supported_commands),
  889. SOCKSENT(5_no_authenticate),
  890. SOCKSENT(5_auth_unsupported_type),
  891. SOCKSENT(5_auth_unsupported_version),
  892. SOCKSENT(5_auth_before_negotiation),
  893. SOCKSENT(5_authenticate),
  894. SOCKSENT(5_authenticate_with_data),
  895. SOCKSENT(5_malformed_commands),
  896. SOCKSENT(5_bad_arguments),
  897. SOCKSENT(truncated),
  898. SOCKSENT(wrong_protocol),
  899. { "client/v4", test_socks_client_v4, TT_FORK, NULL, NULL },
  900. { "client/v5_auth", test_socks_client_v5_auth, TT_FORK, NULL, NULL },
  901. { "client/v5_connect", test_socks_client_v5_connect, TT_FORK, NULL, NULL },
  902. { "client/truncated", test_socks_client_truncated, TT_FORK, NULL, NULL },
  903. END_OF_TESTCASES
  904. };