test_socks.c 34 KB

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