test_socks.c 34 KB

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