test_socks.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, 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 with empty username/password fields.
  404. * Technically this violates RfC 1929, but some client software will send
  405. * this kind of message to Tor.
  406. * */
  407. static void
  408. test_socks_5_authenticate_empty_user_pass(void *ptr)
  409. {
  410. SOCKS_TEST_INIT();
  411. /* SOCKS 5 Negotiate username/password authentication */
  412. ADD_DATA(buf, "\x05\x01\x02");
  413. tt_assert(!fetch_from_buf_socks(buf, socks,
  414. get_options()->TestSocks,
  415. get_options()->SafeSocks));
  416. tt_int_op(2,OP_EQ, socks->replylen);
  417. tt_int_op(5,OP_EQ, socks->reply[0]);
  418. tt_int_op(SOCKS_USER_PASS,OP_EQ, socks->reply[1]);
  419. tt_int_op(5,OP_EQ, socks->socks_version);
  420. tt_int_op(0,OP_EQ, buf_datalen(buf));
  421. /* SOCKS 5 Send username/password auth message with empty user/pass fields */
  422. ADD_DATA(buf, "\x01\x00\x00");
  423. tt_assert(!fetch_from_buf_socks(buf, socks,
  424. get_options()->TestSocks,
  425. get_options()->SafeSocks));
  426. tt_int_op(5,OP_EQ, socks->socks_version);
  427. tt_int_op(2,OP_EQ, socks->replylen);
  428. tt_int_op(1,OP_EQ, socks->reply[0]);
  429. tt_int_op(0,OP_EQ, socks->reply[1]);
  430. tt_int_op(0,OP_EQ, socks->usernamelen);
  431. tt_int_op(0,OP_EQ, socks->passwordlen);
  432. done:
  433. ;
  434. }
  435. /** Perform SOCKS 5 authentication and send data all in one go */
  436. static void
  437. test_socks_5_authenticate_with_data(void *ptr)
  438. {
  439. SOCKS_TEST_INIT();
  440. /* SOCKS 5 Negotiate username/password authentication */
  441. ADD_DATA(buf, "\x05\x01\x02");
  442. tt_assert(!fetch_from_buf_socks(buf, socks,
  443. get_options()->TestSocks,
  444. get_options()->SafeSocks));
  445. tt_int_op(2,OP_EQ, socks->replylen);
  446. tt_int_op(5,OP_EQ, socks->reply[0]);
  447. tt_int_op(SOCKS_USER_PASS,OP_EQ, socks->reply[1]);
  448. tt_int_op(5,OP_EQ, socks->socks_version);
  449. tt_int_op(0,OP_EQ, buf_datalen(buf));
  450. /* SOCKS 5 Send username/password */
  451. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  452. ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  453. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  454. get_options()->SafeSocks),
  455. OP_EQ, 1);
  456. tt_int_op(5,OP_EQ, socks->socks_version);
  457. tt_int_op(2,OP_EQ, socks->replylen);
  458. tt_int_op(1,OP_EQ, socks->reply[0]);
  459. tt_int_op(0,OP_EQ, socks->reply[1]);
  460. tt_str_op("2.2.2.2",OP_EQ, socks->address);
  461. tt_int_op(4369,OP_EQ, socks->port);
  462. tt_int_op(2,OP_EQ, socks->usernamelen);
  463. tt_int_op(3,OP_EQ, socks->passwordlen);
  464. tt_mem_op("me",OP_EQ, socks->username, 2);
  465. tt_mem_op("you",OP_EQ, socks->password, 3);
  466. done:
  467. ;
  468. }
  469. /** Try to negotiate an unsupported authentication type */
  470. static void
  471. test_socks_5_auth_unsupported_type(void *ptr)
  472. {
  473. SOCKS_TEST_INIT();
  474. /* None of these authentication types are recognized. */
  475. ADD_DATA(buf, "\x05\x03\x99\x21\x10");
  476. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  477. get_options()->SafeSocks),
  478. OP_EQ, -1);
  479. tt_int_op(0,OP_EQ, socks->socks_version);
  480. tt_int_op(2,OP_EQ, socks->replylen);
  481. tt_int_op(5,OP_EQ, socks->reply[0]);
  482. tt_int_op(0xff,OP_EQ, socks->reply[1]);
  483. done:
  484. ;
  485. }
  486. /** Try to negotiate an unsupported version of username/password auth. */
  487. static void
  488. test_socks_5_auth_unsupported_version(void *ptr)
  489. {
  490. SOCKS_TEST_INIT();
  491. /* Negotiate username/password */
  492. ADD_DATA(buf, "\x05\x01\x02");
  493. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  494. get_options()->SafeSocks),
  495. OP_EQ, 0);
  496. tt_int_op(0,OP_EQ, buf_datalen(buf)); /* buf should be drained */
  497. /* Now, suggest an unrecognized username/password version */
  498. ADD_DATA(buf, "\x02\x05" "hello" "\x05" "world");
  499. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  500. get_options()->SafeSocks),
  501. OP_EQ, -1);
  502. done:
  503. ;
  504. }
  505. /** Perform SOCKS 5 authentication before method negotiated */
  506. static void
  507. test_socks_5_auth_before_negotiation(void *ptr)
  508. {
  509. SOCKS_TEST_INIT();
  510. /* SOCKS 5 Send username/password */
  511. ADD_DATA(buf, "\x01\x02me\x02me");
  512. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  513. get_options()->SafeSocks),
  514. OP_EQ, -1);
  515. tt_int_op(0,OP_EQ, socks->socks_version);
  516. tt_int_op(0,OP_EQ, socks->replylen);
  517. tt_int_op(0,OP_EQ, socks->reply[0]);
  518. tt_int_op(0,OP_EQ, socks->reply[1]);
  519. done:
  520. ;
  521. }
  522. /** Perform malformed SOCKS 5 commands */
  523. static void
  524. test_socks_5_malformed_commands(void *ptr)
  525. {
  526. SOCKS_TEST_INIT();
  527. /* XXX: Stringified address length > MAX_SOCKS_ADDR_LEN will never happen */
  528. /** SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369, with SafeSocks set
  529. */
  530. ADD_DATA(buf, "\x05\x01\x00");
  531. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  532. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1),
  533. OP_EQ, -1);
  534. tt_int_op(5,OP_EQ,socks->socks_version);
  535. tt_int_op(10,OP_EQ,socks->replylen);
  536. tt_int_op(5,OP_EQ,socks->reply[0]);
  537. tt_int_op(SOCKS5_NOT_ALLOWED,OP_EQ,socks->reply[1]);
  538. tt_int_op(1,OP_EQ,socks->reply[3]);
  539. buf_clear(buf);
  540. socks_request_clear(socks);
  541. /* SOCKS 5 Send RESOLVE_PTR [F1] for FQDN torproject.org */
  542. ADD_DATA(buf, "\x05\x01\x00");
  543. ADD_DATA(buf, "\x05\xF1\x00\x03\x0Etorproject.org\x11\x11");
  544. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  545. get_options()->SafeSocks),OP_EQ, -1);
  546. tt_int_op(5,OP_EQ,socks->socks_version);
  547. tt_int_op(10,OP_EQ,socks->replylen);
  548. tt_int_op(5,OP_EQ,socks->reply[0]);
  549. tt_int_op(SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED,OP_EQ,socks->reply[1]);
  550. tt_int_op(1,OP_EQ,socks->reply[3]);
  551. buf_clear(buf);
  552. socks_request_clear(socks);
  553. /* XXX: len + 1 > MAX_SOCKS_ADDR_LEN (FQDN request) will never happen */
  554. /* SOCKS 5 Send CONNECT [01] to FQDN """"".com */
  555. ADD_DATA(buf, "\x05\x01\x00");
  556. ADD_DATA(buf, "\x05\x01\x00\x03\x09\"\"\"\"\".com\x11\x11");
  557. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  558. get_options()->SafeSocks),OP_EQ, -1);
  559. tt_int_op(5,OP_EQ,socks->socks_version);
  560. tt_int_op(10,OP_EQ,socks->replylen);
  561. tt_int_op(5,OP_EQ,socks->reply[0]);
  562. tt_int_op(SOCKS5_GENERAL_ERROR,OP_EQ,socks->reply[1]);
  563. tt_int_op(1,OP_EQ,socks->reply[3]);
  564. buf_clear(buf);
  565. socks_request_clear(socks);
  566. /* SOCKS 5 Send CONNECT [01] to address type 0x23 */
  567. ADD_DATA(buf, "\x05\x01\x00");
  568. ADD_DATA(buf, "\x05\x01\x00\x23\x02\x02\x02\x02\x11\x11");
  569. tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  570. get_options()->SafeSocks),OP_EQ, -1);
  571. tt_int_op(5,OP_EQ,socks->socks_version);
  572. tt_int_op(10,OP_EQ,socks->replylen);
  573. tt_int_op(5,OP_EQ,socks->reply[0]);
  574. /* trunnel parsing will fail with -1 */
  575. tt_int_op(SOCKS5_GENERAL_ERROR,OP_EQ,socks->reply[1]);
  576. tt_int_op(1,OP_EQ,socks->reply[3]);
  577. done:
  578. ;
  579. }
  580. static void
  581. test_socks_5_bad_arguments(void *ptr)
  582. {
  583. SOCKS_TEST_INIT();
  584. setup_capture_of_logs(LOG_DEBUG);
  585. /* Socks5, bogus hostname */
  586. ADD_DATA(buf, "\x05\x01\x00" "\x05\x01\x00\x03\x03" "---" "\x00\x50" );
  587. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0), OP_EQ, -1);
  588. buf_clear(buf);
  589. expect_log_msg_containing("Your application (using socks5 to port 80) "
  590. "gave Tor a malformed hostname: ");
  591. mock_clean_saved_logs();
  592. socks_request_clear(socks);
  593. done:
  594. teardown_capture_of_logs();
  595. }
  596. /** check for correct behavior when the socks command has not arrived. */
  597. static void
  598. test_socks_truncated(void *ptr)
  599. {
  600. const struct {
  601. enum { NONE, AUTH, ALL } setup;
  602. const char *body;
  603. size_t len;
  604. } commands[] = {
  605. /* SOCKS4 */
  606. /* Connect, to an IP. */
  607. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04\x00", 9},
  608. /* Connect, to an IP, with authentication. */
  609. { NONE, "\x04\x01\x05\x05\x01\x02\x03\x04hello\x00", 14},
  610. /* SOCKS4A */
  611. /* Connect, to a hostname */
  612. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01\x00www.example.com\x00", 25},
  613. /* Connect, to a hostname, with authentication */
  614. { NONE, "\x04\x01\x09\x09\x00\x00\x00\x01hi\x00www.example.com\x00", 27},
  615. /* SOCKS5 */
  616. /* initial handshake */
  617. { NONE, "\x05\x00", 2 },
  618. /* no-auth handshake */
  619. { NONE, "\x05\x03\x99\x21\x10", 5 },
  620. /* SOCSK5, username-password, all empty. */
  621. { AUTH, "\x01\x00\x00", 3 },
  622. /* SOCSK5, username-password, 1 char each. */
  623. { AUTH, "\x01\x01x\x01y", 5 },
  624. /* SOCSK5, username-password, max length. */
  625. { AUTH, "\x01\xff"
  626. "Ogni tempo ha il suo fascismo: se ne notano i segni premonitori "
  627. "dovunque la concentrazione di potere nega al cittadino la "
  628. "possibilit\xc3\xa0 e la capacit\xc3\xa0 di esprimere ed attuare la "
  629. "sua volont\xc3\xa0. A questo si arriva in molti modi, non "
  630. "necessariamente col terror"
  631. "\xff"
  632. "e dell'intimidazione poliziesca, ma anche negando o distorcendo "
  633. "l'informazione, inquinando la giustizia, paralizzando la scuola, "
  634. "diffondendo in molti modi sottili la nostalgia per un mondo in cui "
  635. "regnava sovrano l'ordine, ed in cui la sicurezza dei pochi "
  636. /* privilegiati riposava sul lavoro forzato e sul silenzio forzato dei
  637. molti. -- Primo Levi */ , 513 },
  638. /* Socks5, IPv4 address */
  639. { ALL, "\x05\x01\x00\x01\x01\x02\x03\x04\x20\x20", 10 },
  640. /* Socks5, IPv6 address */
  641. { ALL, "\x05\x01\x00\x04"
  642. "\x49\x20\x48\x41\x5a\x20\x45\x41\x53\x54\x45\x52\x20\x45\x47\x47"
  643. "\x20\x20", 22 },
  644. /* Socks5, hostname, empty. */
  645. { ALL, "\x05\x01\x00\x03" "\x00" "\x00\x50", 7 },
  646. /* Socks5, hostname, moderate. */
  647. { ALL, "\x05\x01\x00\x03" "\x11" "onion.example.com" "\x00\x50", 24 },
  648. /* Socks5, hostname, maximum. */
  649. { ALL, "\x05\x01\x00\x03" "\xff"
  650. "whatsoever.I.shall.see.or.hear.in.the.course.of.my.profession.as.well."
  651. "as.outside.my.profession.in.my.intercourse.with.men.if.it.be.what."
  652. "should.not.be.published.abroad.I.will.never.divulge.holding.such."
  653. "things.to.be.holy.secrets.x.hippocratic.oath.wikipedia"
  654. "\x00\x50", 262 },
  655. };
  656. unsigned i, j;
  657. SOCKS_TEST_INIT();
  658. for (i = 0; i < ARRAY_LENGTH(commands); ++i) {
  659. for (j = 0; j < commands[i].len; ++j) {
  660. switch (commands[i].setup) {
  661. default: /* Falls through */
  662. case NONE:
  663. /* This test calls for no setup on the socks state. */
  664. break;
  665. case AUTH:
  666. /* This test calls for the socks state to be waiting for
  667. * username/password authentication */
  668. ADD_DATA(buf, "\x05\x01\x02");
  669. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  670. tt_int_op(0, OP_EQ, buf_datalen(buf));
  671. break;
  672. case ALL:
  673. /* This test calls for the socks state to be waiting for
  674. * the connection request */
  675. ADD_DATA(buf, "\x05\x01\x00");
  676. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  677. tt_int_op(0, OP_EQ, buf_datalen(buf));
  678. }
  679. TT_BLATHER(("Checking command %u, length %u, omitting char %u", i, j,
  680. (unsigned)commands[i].body[j]));
  681. buf_add(buf, commands[i].body, j);
  682. /* This should return 0 meaning "not done yet" */
  683. tt_int_op(0, OP_EQ, fetch_from_buf_socks(buf, socks, 0, 0));
  684. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  685. buf_clear(buf);
  686. socks_request_free(testdata->req);
  687. socks = testdata->req = socks_request_new();
  688. }
  689. }
  690. done:
  691. ;
  692. }
  693. static void
  694. test_socks_wrong_protocol(void *ptr)
  695. {
  696. SOCKS_TEST_INIT();
  697. setup_capture_of_logs(LOG_DEBUG);
  698. /* HTTP request. */
  699. ADD_DATA(buf, "GET /index.html HTTP/1.0" );
  700. tt_int_op(fetch_from_buf_socks(buf, socks, 1, 0), OP_EQ, -1);
  701. buf_clear(buf);
  702. expect_log_msg_containing("Socks version 71 not recognized. "
  703. "(This port is not an HTTP proxy;");
  704. mock_clean_saved_logs();
  705. socks_request_clear(socks);
  706. done:
  707. teardown_capture_of_logs();
  708. }
  709. /* Check our client-side socks4 parsing (that is to say, our parsing of
  710. * server responses).
  711. */
  712. static void
  713. test_socks_client_v4(void *arg)
  714. {
  715. (void)arg;
  716. buf_t *buf = buf_new();
  717. char *reason = NULL;
  718. /* Legit socks4 response, success */
  719. ADD_DATA(buf, "\x04\x5a\x20\x25\x01\x02\x03\x04");
  720. tt_int_op(1, OP_EQ,
  721. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  722. &reason));
  723. tt_ptr_op(reason, OP_EQ, NULL);
  724. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  725. /* Legit socks4 response, failure. */
  726. ADD_DATA(buf, "\x04\x5b\x20\x25\x01\x02\x03\x04");
  727. tt_int_op(-1, OP_EQ,
  728. fetch_from_buf_socks_client(buf, PROXY_SOCKS4_WANT_CONNECT_OK,
  729. &reason));
  730. tt_ptr_op(reason, OP_NE, NULL);
  731. tt_str_op(reason, OP_EQ, "server rejected connection");
  732. done:
  733. buf_free(buf);
  734. tor_free(reason);
  735. }
  736. /* Check our client-side socks5 authentication-negotiation parsing (that is to
  737. * say, our parsing of server responses).
  738. */
  739. static void
  740. test_socks_client_v5_auth(void *arg)
  741. {
  742. (void)arg;
  743. buf_t *buf = buf_new();
  744. char *reason = NULL;
  745. /* Legit socks5 responses, got a method we like. */
  746. ADD_DATA(buf, "\x05\x00");
  747. tt_int_op(1, OP_EQ,
  748. fetch_from_buf_socks_client(buf,
  749. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  750. &reason));
  751. tt_ptr_op(reason, OP_EQ, NULL);
  752. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  753. /* Same, but we wanted something else. */
  754. ADD_DATA(buf, "\x05\x00");
  755. tt_int_op(1, OP_EQ,
  756. fetch_from_buf_socks_client(buf,
  757. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  758. &reason));
  759. tt_ptr_op(reason, OP_EQ, NULL);
  760. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  761. /* Same, and they offered a password. */
  762. ADD_DATA(buf, "\x05\x02");
  763. tt_int_op(2, OP_EQ,
  764. fetch_from_buf_socks_client(buf,
  765. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  766. &reason));
  767. tt_ptr_op(reason, OP_EQ, NULL);
  768. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  769. /* They rejected our method, or selected something we don't know. */
  770. ADD_DATA(buf, "\x05\xff");
  771. tt_int_op(-1, OP_EQ,
  772. fetch_from_buf_socks_client(buf,
  773. PROXY_SOCKS5_WANT_AUTH_METHOD_NONE,
  774. &reason));
  775. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  776. "authentication methods");
  777. buf_clear(buf);
  778. tor_free(reason);
  779. ADD_DATA(buf, "\x05\xff");
  780. tt_int_op(-1, OP_EQ,
  781. fetch_from_buf_socks_client(buf,
  782. PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929,
  783. &reason));
  784. tt_str_op(reason, OP_EQ, "server doesn't support any of our available "
  785. "authentication methods");
  786. tor_free(reason);
  787. buf_clear(buf);
  788. /* Now check for authentication responses: check success and failure. */
  789. ADD_DATA(buf, "\x01\x00");
  790. tt_int_op(1, OP_EQ,
  791. fetch_from_buf_socks_client(buf,
  792. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  793. &reason));
  794. tt_ptr_op(reason, OP_EQ, NULL);
  795. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  796. ADD_DATA(buf, "\x01\xf0");
  797. tt_int_op(-1, OP_EQ,
  798. fetch_from_buf_socks_client(buf,
  799. PROXY_SOCKS5_WANT_AUTH_RFC1929_OK,
  800. &reason));
  801. tt_ptr_op(reason, OP_NE, NULL);
  802. tt_str_op(reason, OP_EQ, "authentication failed");
  803. done:
  804. buf_free(buf);
  805. tor_free(reason);
  806. }
  807. /* Check our client-side socks5 connect parsing (that is to say, our parsing
  808. * of server responses).
  809. */
  810. static void
  811. test_socks_client_v5_connect(void *arg)
  812. {
  813. (void)arg;
  814. buf_t *buf = buf_new();
  815. char *reason = NULL;
  816. /* Legit socks5 responses, success, ipv4. */
  817. ADD_DATA(buf, "\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05");
  818. tt_int_op(1, OP_EQ,
  819. fetch_from_buf_socks_client(buf,
  820. PROXY_SOCKS5_WANT_CONNECT_OK,
  821. &reason));
  822. tt_ptr_op(reason, OP_EQ, NULL);
  823. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  824. /* Legit socks5 responses, success, ipv6. */
  825. ADD_DATA(buf, "\x05\x00\x00\x04"
  826. "abcdefghijklmnop"
  827. "\x00\x05");
  828. tt_int_op(1, OP_EQ,
  829. fetch_from_buf_socks_client(buf,
  830. PROXY_SOCKS5_WANT_CONNECT_OK,
  831. &reason));
  832. tt_ptr_op(reason, OP_EQ, NULL);
  833. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  834. /* Legit socks5 responses, success, hostname. */
  835. ADD_DATA(buf, "\x05\x00\x00\x03\x12"
  836. "gopher.example.com"
  837. "\x00\x05");
  838. tt_int_op(1, OP_EQ,
  839. fetch_from_buf_socks_client(buf,
  840. PROXY_SOCKS5_WANT_CONNECT_OK,
  841. &reason));
  842. tt_ptr_op(reason, OP_EQ, NULL);
  843. tt_int_op(buf_datalen(buf), OP_EQ, 0);
  844. /* Legit socks5 responses, failure, hostname. */
  845. ADD_DATA(buf, "\x05\x03\x00\x03\x12"
  846. "gopher.example.com"
  847. "\x00\x05");
  848. tt_int_op(-1, OP_EQ,
  849. fetch_from_buf_socks_client(buf,
  850. PROXY_SOCKS5_WANT_CONNECT_OK,
  851. &reason));
  852. tt_ptr_op(reason, OP_NE, NULL);
  853. tt_str_op(reason, OP_EQ, "Network unreachable");
  854. tor_free(reason);
  855. buf_clear(buf);
  856. /* Bogus socks5 responses: what is address type 0x17? */
  857. ADD_DATA(buf, "\x05\x03\x00\x17\x12 blah blah");
  858. tt_int_op(-1, OP_EQ,
  859. fetch_from_buf_socks_client(buf,
  860. PROXY_SOCKS5_WANT_CONNECT_OK,
  861. &reason));
  862. tt_ptr_op(reason, OP_NE, NULL);
  863. tt_str_op(reason, OP_EQ, "invalid response to connect request");
  864. buf_clear(buf);
  865. done:
  866. buf_free(buf);
  867. tor_free(reason);
  868. }
  869. static void
  870. test_socks_client_truncated(void *arg)
  871. {
  872. (void)arg;
  873. buf_t *buf = buf_new();
  874. char *reason = NULL;
  875. #define S(str) str, (sizeof(str)-1)
  876. const struct {
  877. int state;
  878. const char *body;
  879. size_t len;
  880. } replies[] = {
  881. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5a\x20\x25\x01\x02\x03\x04") },
  882. { PROXY_SOCKS4_WANT_CONNECT_OK, S("\x04\x5b\x20\x25\x01\x02\x03\x04") },
  883. { PROXY_SOCKS5_WANT_AUTH_METHOD_NONE, S("\x05\x00") },
  884. { PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929, S("\x05\x00") },
  885. { PROXY_SOCKS5_WANT_AUTH_RFC1929_OK, S("\x01\x00") },
  886. { PROXY_SOCKS5_WANT_CONNECT_OK,
  887. S("\x05\x00\x00\x01\x01\x02\x03\x04\x00\x05") },
  888. { PROXY_SOCKS5_WANT_CONNECT_OK,
  889. S("\x05\x00\x00\x04" "abcdefghijklmnop" "\x00\x05") },
  890. { PROXY_SOCKS5_WANT_CONNECT_OK,
  891. S("\x05\x00\x00\x03\x12" "gopher.example.com" "\x00\x05") },
  892. { PROXY_SOCKS5_WANT_CONNECT_OK,
  893. S("\x05\x03\x00\x03\x12" "gopher.example.com""\x00\x05") },
  894. { PROXY_SOCKS5_WANT_CONNECT_OK,
  895. S("\x05\x03\x00\x17") },
  896. };
  897. unsigned i, j;
  898. for (i = 0; i < ARRAY_LENGTH(replies); ++i) {
  899. for (j = 0; j < replies[i].len; ++j) {
  900. TT_BLATHER(("Checking command %u, length %u", i, j));
  901. buf_add(buf, replies[i].body, j);
  902. /* This should return 0 meaning "not done yet" */
  903. tt_int_op(0, OP_EQ,
  904. fetch_from_buf_socks_client(buf, replies[i].state, &reason));
  905. tt_uint_op(j, OP_EQ, buf_datalen(buf)); /* Nothing was drained */
  906. buf_clear(buf);
  907. tt_ptr_op(reason, OP_EQ, NULL);
  908. }
  909. }
  910. done:
  911. tor_free(reason);
  912. buf_free(buf);
  913. }
  914. #define SOCKSENT(name) \
  915. { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
  916. struct testcase_t socks_tests[] = {
  917. SOCKSENT(4_unsupported_commands),
  918. SOCKSENT(4_supported_commands),
  919. SOCKSENT(4_bad_arguments),
  920. SOCKSENT(5_unsupported_commands),
  921. SOCKSENT(5_supported_commands),
  922. SOCKSENT(5_no_authenticate),
  923. SOCKSENT(5_auth_unsupported_type),
  924. SOCKSENT(5_auth_unsupported_version),
  925. SOCKSENT(5_auth_before_negotiation),
  926. SOCKSENT(5_authenticate),
  927. SOCKSENT(5_authenticate_empty_user_pass),
  928. SOCKSENT(5_authenticate_with_data),
  929. SOCKSENT(5_malformed_commands),
  930. SOCKSENT(5_bad_arguments),
  931. SOCKSENT(truncated),
  932. SOCKSENT(wrong_protocol),
  933. { "client/v4", test_socks_client_v4, TT_FORK, NULL, NULL },
  934. { "client/v5_auth", test_socks_client_v5_auth, TT_FORK, NULL, NULL },
  935. { "client/v5_connect", test_socks_client_v5_connect, TT_FORK, NULL, NULL },
  936. { "client/truncated", test_socks_client_truncated, TT_FORK, NULL, NULL },
  937. END_OF_TESTCASES
  938. };