test_socks.c 34 KB

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