123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- /* Copyright (c) 2001-2004, Roger Dingledine.
- * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2013, The Tor Project, Inc. */
- /* See LICENSE for licensing information */
- #include "or.h"
- #include "buffers.h"
- #include "config.h"
- #include "test.h"
- typedef struct socks_test_data_t {
- socks_request_t *req;
- buf_t *buf;
- } socks_test_data_t;
- static void *
- socks_test_setup(const struct testcase_t *testcase)
- {
- socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
- (void)testcase;
- data->buf = buf_new_with_capacity(256);
- data->req = socks_request_new();
- config_register_addressmaps(get_options());
- return data;
- }
- static int
- socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
- {
- socks_test_data_t *data = ptr;
- (void)testcase;
- buf_free(data->buf);
- socks_request_free(data->req);
- tor_free(data);
- return 1;
- }
- const struct testcase_setup_t socks_setup = {
- socks_test_setup, socks_test_cleanup
- };
- #define SOCKS_TEST_INIT() \
- socks_test_data_t *testdata = ptr; \
- buf_t *buf = testdata->buf; \
- socks_request_t *socks = testdata->req;
- #define ADD_DATA(buf, s) \
- write_to_buf(s, sizeof(s)-1, buf)
- static void
- socks_request_clear(socks_request_t *socks)
- {
- tor_free(socks->username);
- tor_free(socks->password);
- memset(socks, 0, sizeof(socks_request_t));
- }
- /** Perform unsupported SOCKS 4 commands */
- static void
- test_socks_4_unsupported_commands(void *ptr)
- {
- SOCKS_TEST_INIT();
- /* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
- ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
- tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks) == -1);
- tt_int_op(4,==, socks->socks_version);
- tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
- done:
- ;
- }
- /** Perform supported SOCKS 4 commands */
- static void
- test_socks_4_supported_commands(void *ptr)
- {
- SOCKS_TEST_INIT();
- tt_int_op(0,==, buf_datalen(buf));
- /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
- ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
- tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks) == 1);
- tt_int_op(4,==, socks->socks_version);
- tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
- tt_int_op(SOCKS_COMMAND_CONNECT,==, socks->command);
- tt_str_op("2.2.2.3",==, socks->address);
- tt_int_op(4370,==, socks->port);
- tt_assert(socks->got_auth == 0);
- tt_assert(! socks->username);
- tt_int_op(0,==, buf_datalen(buf));
- socks_request_clear(socks);
- /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
- ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
- tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks) == 1);
- tt_int_op(4,==, socks->socks_version);
- tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
- tt_int_op(SOCKS_COMMAND_CONNECT,==, socks->command);
- tt_str_op("2.2.2.4",==, socks->address);
- tt_int_op(4370,==, socks->port);
- tt_assert(socks->got_auth == 1);
- tt_assert(socks->username);
- tt_int_op(2,==, socks->usernamelen);
- tt_mem_op("me",==, socks->username, 2);
- tt_int_op(0,==, buf_datalen(buf));
- socks_request_clear(socks);
- /* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
- ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
- tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks) == 1);
- tt_int_op(4,==, socks->socks_version);
- tt_int_op(0,==, socks->replylen); /* XXX: shouldn't tor reply? */
- tt_str_op("torproject.org",==, socks->address);
- tt_int_op(0,==, buf_datalen(buf));
- done:
- ;
- }
- /** Perform unsupported SOCKS 5 commands */
- static void
- test_socks_5_unsupported_commands(void *ptr)
- {
- SOCKS_TEST_INIT();
- /* SOCKS 5 Send unsupported BIND [02] command */
- ADD_DATA(buf, "\x05\x02\x00\x01");
- tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks),==, 0);
- tt_int_op(0,==, buf_datalen(buf));
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
- tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks),==, -1);
- tt_int_op(5,==,socks->socks_version);
- tt_int_op(10,==,socks->replylen);
- tt_int_op(5,==,socks->reply[0]);
- tt_int_op(SOCKS5_COMMAND_NOT_SUPPORTED,==,socks->reply[1]);
- tt_int_op(1,==,socks->reply[3]);
- buf_clear(buf);
- socks_request_clear(socks);
- /* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
- ADD_DATA(buf, "\x05\x02\x00\x01");
- tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks),==, 0);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
- tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks),==, -1);
- tt_int_op(5,==,socks->socks_version);
- tt_int_op(10,==,socks->replylen);
- tt_int_op(5,==,socks->reply[0]);
- tt_int_op(SOCKS5_COMMAND_NOT_SUPPORTED,==,socks->reply[1]);
- tt_int_op(1,==,socks->reply[3]);
- done:
- ;
- }
- /** Perform supported SOCKS 5 commands */
- static void
- test_socks_5_supported_commands(void *ptr)
- {
- SOCKS_TEST_INIT();
- /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
- ADD_DATA(buf, "\x05\x01\x00");
- tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks),==, 0);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
- tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks),==, 1);
- tt_str_op("2.2.2.2",==, socks->address);
- tt_int_op(4369,==, socks->port);
- tt_int_op(0,==, buf_datalen(buf));
- socks_request_clear(socks);
- /* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
- ADD_DATA(buf, "\x05\x01\x00");
- ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
- tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks),==, 1);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- tt_str_op("torproject.org",==, socks->address);
- tt_int_op(4369,==, socks->port);
- tt_int_op(0,==, buf_datalen(buf));
- socks_request_clear(socks);
- /* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
- ADD_DATA(buf, "\x05\x01\x00");
- ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
- tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks) == 1);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- tt_str_op("torproject.org",==, socks->address);
- tt_int_op(0,==, buf_datalen(buf));
- socks_request_clear(socks);
- /* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
- ADD_DATA(buf, "\x05\x01\x00");
- ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
- tt_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
- get_options()->SafeSocks) == 1);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- tt_str_op("2.2.2.5",==, socks->address);
- tt_int_op(0,==, buf_datalen(buf));
- done:
- ;
- }
- /** Perform SOCKS 5 authentication */
- static void
- test_socks_5_no_authenticate(void *ptr)
- {
- SOCKS_TEST_INIT();
- /*SOCKS 5 No Authentication */
- ADD_DATA(buf,"\x05\x01\x00");
- tt_assert(!fetch_from_buf_socks(buf, socks,
- get_options()->TestSocks,
- get_options()->SafeSocks));
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(SOCKS_NO_AUTH,==, socks->reply[1]);
- tt_int_op(0,==, buf_datalen(buf));
- /*SOCKS 5 Send username/password anyway - pretend to be broken */
- ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
- tt_assert(!fetch_from_buf_socks(buf, socks,
- get_options()->TestSocks,
- get_options()->SafeSocks));
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(1,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- tt_int_op(2,==, socks->usernamelen);
- tt_int_op(2,==, socks->passwordlen);
- tt_mem_op("\x01\x01",==, socks->username, 2);
- tt_mem_op("\x01\x01",==, socks->password, 2);
- done:
- ;
- }
- /** Perform SOCKS 5 authentication */
- static void
- test_socks_5_authenticate(void *ptr)
- {
- SOCKS_TEST_INIT();
- /* SOCKS 5 Negotiate username/password authentication */
- ADD_DATA(buf, "\x05\x01\x02");
- tt_assert(!fetch_from_buf_socks(buf, socks,
- get_options()->TestSocks,
- get_options()->SafeSocks));
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(SOCKS_USER_PASS,==, socks->reply[1]);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(0,==, buf_datalen(buf));
- /* SOCKS 5 Send username/password */
- ADD_DATA(buf, "\x01\x02me\x08mypasswd");
- tt_assert(!fetch_from_buf_socks(buf, socks,
- get_options()->TestSocks,
- get_options()->SafeSocks));
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(1,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- tt_int_op(2,==, socks->usernamelen);
- tt_int_op(8,==, socks->passwordlen);
- tt_mem_op("me",==, socks->username, 2);
- tt_mem_op("mypasswd",==, socks->password, 8);
- done:
- ;
- }
- /** Perform SOCKS 5 authentication and send data all in one go */
- static void
- test_socks_5_authenticate_with_data(void *ptr)
- {
- SOCKS_TEST_INIT();
- /* SOCKS 5 Negotiate username/password authentication */
- ADD_DATA(buf, "\x05\x01\x02");
- tt_assert(!fetch_from_buf_socks(buf, socks,
- get_options()->TestSocks,
- get_options()->SafeSocks));
- tt_int_op(2,==, socks->replylen);
- tt_int_op(5,==, socks->reply[0]);
- tt_int_op(SOCKS_USER_PASS,==, socks->reply[1]);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(0,==, buf_datalen(buf));
- /* SOCKS 5 Send username/password */
- /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
- ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
- tt_assert(fetch_from_buf_socks(buf, socks,
- get_options()->TestSocks,
- get_options()->SafeSocks) == 1);
- tt_int_op(5,==, socks->socks_version);
- tt_int_op(2,==, socks->replylen);
- tt_int_op(1,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- tt_str_op("2.2.2.2",==, socks->address);
- tt_int_op(4369,==, socks->port);
- tt_int_op(2,==, socks->usernamelen);
- tt_int_op(3,==, socks->passwordlen);
- tt_mem_op("me",==, socks->username, 2);
- tt_mem_op("you",==, socks->password, 3);
- done:
- ;
- }
- /** Perform SOCKS 5 authentication before method negotiated */
- static void
- test_socks_5_auth_before_negotiation(void *ptr)
- {
- SOCKS_TEST_INIT();
- /* SOCKS 5 Send username/password */
- ADD_DATA(buf, "\x01\x02me\x02me");
- tt_assert(fetch_from_buf_socks(buf, socks,
- get_options()->TestSocks,
- get_options()->SafeSocks) == -1);
- tt_int_op(0,==, socks->socks_version);
- tt_int_op(0,==, socks->replylen);
- tt_int_op(0,==, socks->reply[0]);
- tt_int_op(0,==, socks->reply[1]);
- done:
- ;
- }
- #define SOCKSENT(name) \
- { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
- struct testcase_t socks_tests[] = {
- SOCKSENT(4_unsupported_commands),
- SOCKSENT(4_supported_commands),
- SOCKSENT(5_unsupported_commands),
- SOCKSENT(5_supported_commands),
- SOCKSENT(5_no_authenticate),
- SOCKSENT(5_auth_before_negotiation),
- SOCKSENT(5_authenticate),
- SOCKSENT(5_authenticate_with_data),
- END_OF_TESTCASES
- };
|