test_socks.c 34 KB

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