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