test_socks.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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.3: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.4: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("socks4: parsing failed - invalid request.");
  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("parsing failed - invalid request.");
  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. /* trunnel parsing will fail with -1 */
  543. tt_int_op(SOCKS5_GENERAL_ERROR,OP_EQ,socks->reply[1]);
  544. tt_int_op(1,OP_EQ,socks->reply[3]);
  545. done:
  546. ;
  547. }
  548. static void
  549. test_socks_5_bad_arguments(void *ptr)
  550. {
  551. SOCKS_TEST_INIT();
  552. setup_capture_of_logs(LOG_DEBUG);
  553. /* Socks5, bogus hostname */
  554. ADD_DATA(buf, "\x05\x01\x00" "\x05\x01\x00\x03\x03" "---" "\x00\x50" );
  555. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0), OP_EQ, -1);
  556. buf_clear(buf);
  557. expect_log_msg_containing("Your application (using socks5 to port 80) "
  558. "gave Tor a malformed hostname: ");
  559. mock_clean_saved_logs();
  560. socks_request_clear(socks);
  561. done:
  562. teardown_capture_of_logs();
  563. }
  564. /** check for correct behavior when the socks command has not arrived. */
  565. static void
  566. test_socks_truncated(void *ptr)
  567. {
  568. const struct {
  569. enum { NONE, AUTH, ALL } setup;
  570. const char *body;
  571. size_t len;
  572. } commands[] = {
  573. /* SOCKS4 */
  574. /* Connect, to an IP. */
  575. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04\x00", 9},
  576. /* Connect, to an IP, with authentication. */
  577. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04hello\x00", 14},
  578. /* SOCKS4A */
  579. /* Connect, to a hostname */
  580. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01\x00www.example.com\x00", 25},
  581. /* Connect, to a hostname, with authentication */
  582. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01hi\x00www.example.com\x00", 27},
  583. /* SOCKS5 */
  584. /* initial handshake */
  585. { NONE, "\x05\x00", 2 },
  586. /* no-auth handshake */
  587. { NONE, "\x05\x03\x99\x21\x10", 5 },
  588. /* SOCSK5, username-password, all empty. */
  589. { AUTH, "\x01\x00\x00", 3 },
  590. /* SOCSK5, username-password, 1 char each. */
  591. { AUTH, "\x01\x01x\x01y", 5 },
  592. /* SOCSK5, username-password, max length. */
  593. { AUTH, "\x01\xff"
  594. "Ogni tempo ha il suo fascismo: se ne notano i segni premonitori "
  595. "dovunque la concentrazione di potere nega al cittadino la "
  596. "possibilit\xc3\xa0 e la capacit\xc3\xa0 di esprimere ed attuare la "
  597. "sua volont\xc3\xa0. A questo si arriva in molti modi, non "
  598. "necessariamente col terror"
  599. "\xff"
  600. "e dell'intimidazione poliziesca, ma anche negando o distorcendo "
  601. "l'informazione, inquinando la giustizia, paralizzando la scuola, "
  602. "diffondendo in molti modi sottili la nostalgia per un mondo in cui "
  603. "regnava sovrano l'ordine, ed in cui la sicurezza dei pochi "
  604. /* privilegiati riposava sul lavoro forzato e sul silenzio forzato dei
  605. molti. -- Primo Levi */ , 513 },
  606. /* Socks5, IPv4 address */
  607. { ALL, "\x05\x01\x00\x01\x01\x02\x03\x04\x20\x20", 10 },
  608. /* Socks5, IPv6 address */
  609. { ALL, "\x05\x01\x00\x04"
  610. "\x49\x20\x48\x41\x5a\x20\x45\x41\x53\x54\x45\x52\x20\x45\x47\x47"
  611. "\x20\x20", 22 },
  612. /* Socks5, hostname, empty. */
  613. { ALL, "\x05\x01\x00\x03" "\x00" "\x00\x50", 7 },
  614. /* Socks5, hostname, moderate. */
  615. { ALL, "\x05\x01\x00\x03" "\x11" "onion.example.com" "\x00\x50", 24 },
  616. /* Socks5, hostname, maximum. */
  617. { ALL, "\x05\x01\x00\x03" "\xff"
  618. "whatsoever.I.shall.see.or.hear.in.the.course.of.my.profession.as.well."
  619. "as.outside.my.profession.in.my.intercourse.with.men.if.it.be.what."
  620. "should.not.be.published.abroad.I.will.never.divulge.holding.such."
  621. "things.to.be.holy.secrets.x.hippocratic.oath.wikipedia"
  622. "\x00\x50", 262 },
  623. };
  624. unsigned i, j;
  625. SOCKS_TEST_INIT();
  626. for (i = 0; i < ARRAY_LENGTH(commands); ++i) {
  627. for (j = 0; j < commands[i].len; ++j) {
  628. switch (commands[i].setup) {
  629. default: /* Falls through */
  630. case NONE:
  631. /* This test calls for no setup on the socks state. */
  632. break;
  633. case AUTH:
  634. /* This test calls for the socks state to be waiting for
  635. * username/password authentication */
  636. ADD_DATA(buf, "\x05\x01\x02");
  637. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  638. tt_int_op(0, OP_EQ, buf_datalen(buf));
  639. break;
  640. case ALL:
  641. /* This test calls for the socks state to be waiting for
  642. * the connection request */
  643. ADD_DATA(buf, "\x05\x01\x00");
  644. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  645. tt_int_op(0, OP_EQ, buf_datalen(buf));
  646. }
  647. TT_BLATHER(("Checking command %u, length %u, omitting char %u", i, j,
  648. (unsigned)commands[i].body[j]));
  649. buf_add(buf, commands[i].body, j);
  650. /* This should return 0 meaning "not done yet" */
  651. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  652. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  653. buf_clear(buf);
  654. socks_request_free(testdata->req);
  655. socks = testdata->req = socks_request_new();
  656. }
  657. }
  658. done:
  659. ;
  660. }
  661. static void
  662. test_socks_wrong_protocol(void *ptr)
  663. {
  664. SOCKS_TEST_INIT();
  665. setup_capture_of_logs(LOG_DEBUG);
  666. /* HTTP request. */
  667. ADD_DATA(buf, "GET /index.html HTTP/1.0" );
  668. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0), OP_EQ, -1);
  669. buf_clear(buf);
  670. expect_log_msg_containing("Socks version 71 not recognized. "
  671. "(This port is not an HTTP proxy;");
  672. mock_clean_saved_logs();
  673. socks_request_clear(socks);
  674. done:
  675. teardown_capture_of_logs();
  676. }
  677. /* Check our client-side socks4 parsing (that is to say, our parsing of
  678. * server responses).
  679. */
  680. static void
  681. test_socks_client_v4(void *arg)
  682. {
  683. (void)arg;
  684. buf_t *buf = buf_new();
  685. char *reason = NULL;
  686. /* Legit socks4 response, success */
  687. ADD_DATA(buf, "\x04\x5a\x20\x25\x01\x02\x03\x04");
  688. tt_int_op(1, OP_EQ,
  689. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  690. &reason));
  691. tt_ptr_op(reason, OP_EQ, NULL);
  692. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  693. /* Legit socks4 response, failure. */
  694. ADD_DATA(buf, "\x04\x5b\x20\x25\x01\x02\x03\x04");
  695. tt_int_op(-1, OP_EQ,
  696. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  697. &reason));
  698. tt_ptr_op(reason, OP_NE, NULL);
  699. tt_str_op(reason, OP_EQ, "server rejected connection");
  700. done:
  701. buf_free(buf);
  702. tor_free(reason);
  703. }
  704. /* Check our client-side socks5 authentication-negotiation parsing (that is to
  705. * say, our parsing of server responses).
  706. */
  707. static void
  708. test_socks_client_v5_auth(void *arg)
  709. {
  710. (void)arg;
  711. buf_t *buf = buf_new();
  712. char *reason = NULL;
  713. /* Legit socks5 responses, got a method we like. */
  714. ADD_DATA(buf, "\x05\x00");
  715. tt_int_op(1, OP_EQ,
  716. fetch_from_buf_socks_client(buf,
  717. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  718. &reason));
  719. tt_ptr_op(reason, OP_EQ, NULL);
  720. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  721. /* Same, but we wanted something else. */
  722. ADD_DATA(buf, "\x05\x00");
  723. tt_int_op(1, OP_EQ,
  724. fetch_from_buf_socks_client(buf,
  725. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  726. &reason));
  727. tt_ptr_op(reason, OP_EQ, NULL);
  728. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  729. /* Same, and they offered a password. */
  730. ADD_DATA(buf, "\x05\x02");
  731. tt_int_op(2, OP_EQ,
  732. fetch_from_buf_socks_client(buf,
  733. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  734. &reason));
  735. tt_ptr_op(reason, OP_EQ, NULL);
  736. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  737. /* They rejected our method, or selected something we don't know. */
  738. ADD_DATA(buf, "\x05\xff");
  739. tt_int_op(-1, OP_EQ,
  740. fetch_from_buf_socks_client(buf,
  741. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  742. &reason));
  743. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  744. "authentication methods");
  745. buf_clear(buf);
  746. tor_free(reason);
  747. ADD_DATA(buf, "\x05\xff");
  748. tt_int_op(-1, OP_EQ,
  749. fetch_from_buf_socks_client(buf,
  750. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  751. &reason));
  752. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  753. "authentication methods");
  754. tor_free(reason);
  755. buf_clear(buf);
  756. /* Now check for authentication responses: check success and failure. */
  757. ADD_DATA(buf, "\x01\x00");
  758. tt_int_op(1, OP_EQ,
  759. fetch_from_buf_socks_client(buf,
  760. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  761. &reason));
  762. tt_ptr_op(reason, OP_EQ, NULL);
  763. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  764. ADD_DATA(buf, "\x01\xf0");
  765. tt_int_op(-1, OP_EQ,
  766. fetch_from_buf_socks_client(buf,
  767. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  768. &reason));
  769. tt_ptr_op(reason, OP_NE, NULL);
  770. tt_str_op(reason, OP_EQ, "authentication failed");
  771. done:
  772. buf_free(buf);
  773. tor_free(reason);
  774. }
  775. /* Check our client-side socks5 connect parsing (that is to say, our parsing
  776. * of server responses).
  777. */
  778. static void
  779. test_socks_client_v5_connect(void *arg)
  780. {
  781. (void)arg;
  782. buf_t *buf = buf_new();
  783. char *reason = NULL;
  784. /* Legit socks5 responses, success, ipv4. */
  785. ADD_DATA(buf, "\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05");
  786. tt_int_op(1, OP_EQ,
  787. fetch_from_buf_socks_client(buf,
  788. PROXY_SOCKS5_WANT_CONNECT_OK,
  789. &reason));
  790. tt_ptr_op(reason, OP_EQ, NULL);
  791. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  792. /* Legit socks5 responses, success, ipv6. */
  793. ADD_DATA(buf, "\x05\x00\x00\x04"
  794. "abcdefghijklmnop"
  795. "\x00\x05");
  796. tt_int_op(1, OP_EQ,
  797. fetch_from_buf_socks_client(buf,
  798. PROXY_SOCKS5_WANT_CONNECT_OK,
  799. &reason));
  800. tt_ptr_op(reason, OP_EQ, NULL);
  801. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  802. /* Legit socks5 responses, success, hostname. */
  803. ADD_DATA(buf, "\x05\x00\x00\x03\x12"
  804. "gopher.example.com"
  805. "\x00\x05");
  806. tt_int_op(1, OP_EQ,
  807. fetch_from_buf_socks_client(buf,
  808. PROXY_SOCKS5_WANT_CONNECT_OK,
  809. &reason));
  810. tt_ptr_op(reason, OP_EQ, NULL);
  811. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  812. /* Legit socks5 responses, failure, hostname. */
  813. ADD_DATA(buf, "\x05\x03\x00\x03\x12"
  814. "gopher.example.com"
  815. "\x00\x05");
  816. tt_int_op(-1, OP_EQ,
  817. fetch_from_buf_socks_client(buf,
  818. PROXY_SOCKS5_WANT_CONNECT_OK,
  819. &reason));
  820. tt_ptr_op(reason, OP_NE, NULL);
  821. tt_str_op(reason, OP_EQ, "Network unreachable");
  822. tor_free(reason);
  823. buf_clear(buf);
  824. /* Bogus socks5 responses: what is address type 0x17? */
  825. ADD_DATA(buf, "\x05\x03\x00\x17\x12 blah blah");
  826. tt_int_op(-1, OP_EQ,
  827. fetch_from_buf_socks_client(buf,
  828. PROXY_SOCKS5_WANT_CONNECT_OK,
  829. &reason));
  830. tt_ptr_op(reason, OP_NE, NULL);
  831. tt_str_op(reason, OP_EQ, "invalid response to connect request");
  832. buf_clear(buf);
  833. done:
  834. buf_free(buf);
  835. tor_free(reason);
  836. }
  837. static void
  838. test_socks_client_truncated(void *arg)
  839. {
  840. (void)arg;
  841. buf_t *buf = buf_new();
  842. char *reason = NULL;
  843. #define S(str) str, (sizeof(str)-1)
  844. const struct {
  845. int state;
  846. const char *body;
  847. size_t len;
  848. } replies[] = {
  849. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5a\x20\x25\x01\x02\x03\x04") },
  850. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5b\x20\x25\x01\x02\x03\x04") },
  851. { PROXY_SOCKS5_WANT_AUTH_METHOD_NONE, S("\x05\x00") },
  852. { PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929, S("\x05\x00") },
  853. { PROXY_SOCKS5_WANT_AUTH_RFC1929_OK, S("\x01\x00") },
  854. { PROXY_SOCKS5_WANT_CONNECT_OK,
  855. S("\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05") },
  856. { PROXY_SOCKS5_WANT_CONNECT_OK,
  857. S("\x05\x00\x00\x04" "abcdefghijklmnop" "\x00\x05") },
  858. { PROXY_SOCKS5_WANT_CONNECT_OK,
  859. S("\x05\x00\x00\x03\x12" "gopher.example.com" "\x00\x05") },
  860. { PROXY_SOCKS5_WANT_CONNECT_OK,
  861. S("\x05\x03\x00\x03\x12" "gopher.example.com""\x00\x05") },
  862. { PROXY_SOCKS5_WANT_CONNECT_OK,
  863. S("\x05\x03\x00\x17") },
  864. };
  865. unsigned i, j;
  866. for (i = 0; i < ARRAY_LENGTH(replies); ++i) {
  867. for (j = 0; j < replies[i].len; ++j) {
  868. TT_BLATHER(("Checking command %u, length %u", i, j));
  869. buf_add(buf, replies[i].body, j);
  870. /* This should return 0 meaning "not done yet" */
  871. tt_int_op(0, OP_EQ,
  872. fetch_from_buf_socks_client(buf, replies[i].state, &reason));
  873. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  874. buf_clear(buf);
  875. tt_ptr_op(reason, OP_EQ, NULL);
  876. }
  877. }
  878. done:
  879. tor_free(reason);
  880. buf_free(buf);
  881. }
  882. #define SOCKSENT(name) \
  883. { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
  884. struct testcase_t socks_tests[] = {
  885. SOCKSENT(4_unsupported_commands),
  886. SOCKSENT(4_supported_commands),
  887. SOCKSENT(4_bad_arguments),
  888. SOCKSENT(5_unsupported_commands),
  889. SOCKSENT(5_supported_commands),
  890. SOCKSENT(5_no_authenticate),
  891. SOCKSENT(5_auth_unsupported_type),
  892. SOCKSENT(5_auth_unsupported_version),
  893. SOCKSENT(5_auth_before_negotiation),
  894. SOCKSENT(5_authenticate),
  895. SOCKSENT(5_authenticate_with_data),
  896. SOCKSENT(5_malformed_commands),
  897. SOCKSENT(5_bad_arguments),
  898. SOCKSENT(truncated),
  899. SOCKSENT(wrong_protocol),
  900. { "client/v4", test_socks_client_v4, TT_FORK, NULL, NULL },
  901. { "client/v5_auth", test_socks_client_v5_auth, TT_FORK, NULL, NULL },
  902. { "client/v5_connect", test_socks_client_v5_connect, TT_FORK, NULL, NULL },
  903. { "client/truncated", test_socks_client_truncated, TT_FORK, NULL, NULL },
  904. END_OF_TESTCASES
  905. };