2011-07-13 19:06:14 +02:00
|
|
|
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2013-01-16 07:54:56 +01:00
|
|
|
* Copyright (c) 2007-2013, The Tor Project, Inc. */
|
2011-07-13 19:06:14 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
#define PT_PRIVATE
|
|
|
|
#include "or.h"
|
2011-07-18 02:33:31 +02:00
|
|
|
#include "transports.h"
|
2011-07-13 19:06:14 +02:00
|
|
|
#include "circuitbuild.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
reset_mp(managed_proxy_t *mp)
|
|
|
|
{
|
2011-10-07 15:44:58 +02:00
|
|
|
mp->conf_state = PT_PROTO_LAUNCHED;
|
2011-07-13 19:06:14 +02:00
|
|
|
SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
|
|
|
|
smartlist_clear(mp->transports);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_pt_parsing(void)
|
|
|
|
{
|
|
|
|
char line[200];
|
2013-07-16 13:07:03 +02:00
|
|
|
transport_t *transport = NULL;
|
|
|
|
tor_addr_t test_addr;
|
2011-07-13 19:06:14 +02:00
|
|
|
|
|
|
|
managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
|
|
|
|
mp->conf_state = PT_PROTO_INFANT;
|
2012-01-18 21:53:30 +01:00
|
|
|
mp->transports = smartlist_new();
|
2011-07-13 19:06:14 +02:00
|
|
|
|
|
|
|
/* incomplete cmethod */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"CMETHOD trebuchet",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_cmethod_line(line, mp) < 0);
|
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
|
|
|
/* wrong proxy type */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_cmethod_line(line, mp) < 0);
|
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
|
|
|
/* wrong addrport */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"CMETHOD trebuchet socks4 abcd",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_cmethod_line(line, mp) < 0);
|
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
|
|
|
/* correct line */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_cmethod_line(line, mp) == 0);
|
2013-07-16 13:07:03 +02:00
|
|
|
test_assert(smartlist_len(mp->transports) == 1);
|
|
|
|
transport = smartlist_get(mp->transports, 0);
|
|
|
|
/* test registered address of transport */
|
|
|
|
tor_addr_parse(&test_addr, "127.0.0.1");
|
|
|
|
test_assert(tor_addr_eq(&test_addr, &transport->addr));
|
|
|
|
/* test registered port of transport */
|
|
|
|
test_assert(transport->port == 1999);
|
|
|
|
/* test registered SOCKS version of transport */
|
|
|
|
test_assert(transport->socks_version == PROXY_SOCKS5);
|
|
|
|
/* test registered name of transport */
|
|
|
|
test_streq(transport->name, "trebuchet");
|
2011-07-13 19:06:14 +02:00
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
|
|
|
/* incomplete smethod */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"SMETHOD trebuchet",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_smethod_line(line, mp) < 0);
|
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
|
|
|
/* wrong addr type */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"SMETHOD trebuchet abcd",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_smethod_line(line, mp) < 0);
|
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
|
|
|
/* cowwect */
|
2013-07-16 13:07:03 +02:00
|
|
|
strlcpy(line,"SMETHOD trebuchy 127.0.0.2:2999",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_smethod_line(line, mp) == 0);
|
2013-07-16 13:07:03 +02:00
|
|
|
test_assert(smartlist_len(mp->transports) == 1);
|
|
|
|
transport = smartlist_get(mp->transports, 0);
|
|
|
|
/* test registered address of transport */
|
|
|
|
tor_addr_parse(&test_addr, "127.0.0.2");
|
|
|
|
test_assert(tor_addr_eq(&test_addr, &transport->addr));
|
|
|
|
/* test registered port of transport */
|
|
|
|
test_assert(transport->port == 2999);
|
|
|
|
/* test registered name of transport */
|
|
|
|
test_streq(transport->name, "trebuchy");
|
2011-07-13 19:06:14 +02:00
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
2013-07-16 17:53:25 +02:00
|
|
|
/* Include some arguments. Good ones. */
|
|
|
|
strlcpy(line,"SMETHOD trebuchet 127.0.0.1:9999 ARGS:counterweight=3,sling=snappy",
|
|
|
|
sizeof(line));
|
|
|
|
test_assert(parse_smethod_line(line, mp) == 0);
|
|
|
|
tt_int_op(1, ==, smartlist_len(mp->transports));
|
|
|
|
{
|
|
|
|
const transport_t *transport = smartlist_get(mp->transports, 0);
|
|
|
|
tt_assert(transport);
|
|
|
|
tt_str_op(transport->name, ==, "trebuchet");
|
|
|
|
tt_int_op(transport->port, ==, 9999);
|
|
|
|
tt_str_op(fmt_addr(&transport->addr), ==, "127.0.0.1");
|
|
|
|
tt_str_op(transport->extra_info_args, ==,
|
|
|
|
"counterweight=3,sling=snappy");
|
|
|
|
}
|
|
|
|
reset_mp(mp);
|
|
|
|
|
2011-07-13 19:06:14 +02:00
|
|
|
/* unsupported version */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"VERSION 666",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_version(line, mp) < 0);
|
|
|
|
|
|
|
|
/* incomplete VERSION */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"VERSION ",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_version(line, mp) < 0);
|
|
|
|
|
|
|
|
/* correct VERSION */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"VERSION 1",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
test_assert(parse_version(line, mp) == 0);
|
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(mp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_pt_protocol(void)
|
|
|
|
{
|
|
|
|
char line[200];
|
|
|
|
|
2011-11-25 22:54:06 +01:00
|
|
|
managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
|
2011-10-07 15:44:58 +02:00
|
|
|
mp->conf_state = PT_PROTO_LAUNCHED;
|
2012-01-18 21:53:30 +01:00
|
|
|
mp->transports = smartlist_new();
|
2012-05-08 15:37:43 +02:00
|
|
|
mp->argv = tor_malloc_zero(sizeof(char*)*2);
|
|
|
|
mp->argv[0] = tor_strdup("<testcase>");
|
2011-07-13 19:06:14 +02:00
|
|
|
|
|
|
|
/* various wrong protocol runs: */
|
|
|
|
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"VERSION 1",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
handle_proxy_line(line, mp);
|
|
|
|
test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
|
|
|
|
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"VERSION 1",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
handle_proxy_line(line, mp);
|
|
|
|
test_assert(mp->conf_state == PT_PROTO_BROKEN);
|
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
handle_proxy_line(line, mp);
|
|
|
|
test_assert(mp->conf_state == PT_PROTO_BROKEN);
|
|
|
|
|
|
|
|
reset_mp(mp);
|
|
|
|
|
|
|
|
/* correct protocol run: */
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"VERSION 1",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
handle_proxy_line(line, mp);
|
|
|
|
test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
|
|
|
|
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
handle_proxy_line(line, mp);
|
|
|
|
test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
|
|
|
|
|
2013-05-11 23:06:22 +02:00
|
|
|
strlcpy(line,"CMETHODS DONE",sizeof(line));
|
2011-07-13 19:06:14 +02:00
|
|
|
handle_proxy_line(line, mp);
|
|
|
|
test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
|
|
|
|
|
|
|
|
done:
|
|
|
|
tor_free(mp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PT_LEGACY(name) \
|
|
|
|
{ #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
|
|
|
|
|
|
|
|
struct testcase_t pt_tests[] = {
|
|
|
|
PT_LEGACY(parsing),
|
|
|
|
PT_LEGACY(protocol),
|
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|