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