2010-08-02 21:09:37 +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. */
|
2010-08-02 21:09:37 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
#include "or.h"
|
2012-10-28 21:13:58 +01:00
|
|
|
#include "addressmap.h"
|
2010-08-02 21:09:37 +02:00
|
|
|
#include "config.h"
|
2012-09-12 23:34:50 +02:00
|
|
|
#include "confparse.h"
|
2010-08-02 21:09:37 +02:00
|
|
|
#include "connection_edge.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
static void
|
2011-09-08 17:19:06 +02:00
|
|
|
test_config_addressmap(void *arg)
|
2010-08-02 21:09:37 +02:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char address[256];
|
|
|
|
time_t expires = TIME_MAX;
|
2011-09-08 17:19:06 +02:00
|
|
|
(void)arg;
|
|
|
|
|
2010-12-27 18:35:16 +01:00
|
|
|
strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
|
|
|
|
"MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
|
|
|
|
"MapAddress *.google.com *.torserver.exit\n"
|
2010-12-13 23:13:01 +01:00
|
|
|
"MapAddress *.yahoo.com *.google.com.torserver.exit\n"
|
2010-12-27 18:35:16 +01:00
|
|
|
"MapAddress *.cn.com www.cnn.com\n"
|
2010-12-13 23:13:01 +01:00
|
|
|
"MapAddress *.cnn.com www.cnn.com\n"
|
|
|
|
"MapAddress ex.com www.cnn.com\n"
|
|
|
|
"MapAddress ey.com *.cnn.com\n"
|
2010-08-02 21:09:37 +02:00
|
|
|
"MapAddress www.torproject.org 1.1.1.1\n"
|
|
|
|
"MapAddress other.torproject.org "
|
|
|
|
"this.torproject.org.otherserver.exit\n"
|
|
|
|
"MapAddress test.torproject.org 2.2.2.2\n"
|
|
|
|
"MapAddress www.google.com 3.3.3.3\n"
|
|
|
|
"MapAddress www.example.org 4.4.4.4\n"
|
2010-12-27 18:35:16 +01:00
|
|
|
"MapAddress 4.4.4.4 7.7.7.7\n"
|
2010-08-02 21:09:37 +02:00
|
|
|
"MapAddress 4.4.4.4 5.5.5.5\n"
|
|
|
|
"MapAddress www.infiniteloop.org 6.6.6.6\n"
|
|
|
|
"MapAddress 6.6.6.6 www.infiniteloop.org\n"
|
|
|
|
, sizeof(buf));
|
|
|
|
|
2011-11-30 20:10:22 +01:00
|
|
|
config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
|
2010-08-02 21:09:37 +02:00
|
|
|
config_register_addressmaps(get_options());
|
|
|
|
|
2012-11-25 19:45:36 +01:00
|
|
|
/* Use old interface for now, so we don't need to rewrite the unit tests */
|
|
|
|
#define addressmap_rewrite(a,s,eo,ao) \
|
|
|
|
addressmap_rewrite((a),(s),AMR_FLAG_USE_IPV4_DNS|AMR_FLAG_USE_IPV6_DNS, \
|
|
|
|
(eo),(ao))
|
|
|
|
|
2010-12-27 18:35:16 +01:00
|
|
|
/* MapAddress .invalidwildcard.com .torserver.exit - no match */
|
|
|
|
strlcpy(address, "www.invalidwildcard.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-27 18:35:16 +01:00
|
|
|
|
|
|
|
/* MapAddress *invalidasterisk.com .torserver.exit - no match */
|
|
|
|
strlcpy(address, "www.invalidasterisk.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-27 18:35:16 +01:00
|
|
|
|
2010-08-02 21:09:37 +02:00
|
|
|
/* Where no mapping for FQDN match on top-level domain */
|
2010-12-13 23:13:01 +01:00
|
|
|
/* MapAddress .google.com .torserver.exit */
|
2010-08-02 21:09:37 +02:00
|
|
|
strlcpy(address, "reader.google.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-13 23:13:01 +01:00
|
|
|
test_streq(address, "reader.torserver.exit");
|
|
|
|
|
|
|
|
/* MapAddress *.yahoo.com *.google.com.torserver.exit */
|
|
|
|
strlcpy(address, "reader.yahoo.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "reader.google.com.torserver.exit");
|
|
|
|
|
2010-12-13 23:13:01 +01:00
|
|
|
/*MapAddress *.cnn.com www.cnn.com */
|
|
|
|
strlcpy(address, "cnn.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-13 23:13:01 +01:00
|
|
|
test_streq(address, "www.cnn.com");
|
|
|
|
|
|
|
|
/* MapAddress .cn.com www.cnn.com */
|
|
|
|
strlcpy(address, "www.cn.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-13 23:13:01 +01:00
|
|
|
test_streq(address, "www.cnn.com");
|
|
|
|
|
|
|
|
/* MapAddress ex.com www.cnn.com - no match */
|
|
|
|
strlcpy(address, "www.ex.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-13 23:13:01 +01:00
|
|
|
|
|
|
|
/* MapAddress ey.com *.cnn.com - invalid expression */
|
|
|
|
strlcpy(address, "ey.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-13 23:13:01 +01:00
|
|
|
|
2010-08-02 21:09:37 +02:00
|
|
|
/* Where mapping for FQDN match on FQDN */
|
|
|
|
strlcpy(address, "www.google.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "3.3.3.3");
|
|
|
|
|
|
|
|
strlcpy(address, "www.torproject.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "1.1.1.1");
|
|
|
|
|
|
|
|
strlcpy(address, "other.torproject.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "this.torproject.org.otherserver.exit");
|
|
|
|
|
|
|
|
strlcpy(address, "test.torproject.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "2.2.2.2");
|
|
|
|
|
2010-12-27 18:35:16 +01:00
|
|
|
/* Test a chain of address mappings and the order in which they were added:
|
|
|
|
"MapAddress www.example.org 4.4.4.4"
|
|
|
|
"MapAddress 4.4.4.4 7.7.7.7"
|
|
|
|
"MapAddress 4.4.4.4 5.5.5.5"
|
|
|
|
*/
|
2010-08-02 21:09:37 +02:00
|
|
|
strlcpy(address, "www.example.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "5.5.5.5");
|
|
|
|
|
|
|
|
/* Test infinite address mapping results in no change */
|
|
|
|
strlcpy(address, "www.infiniteloop.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "www.infiniteloop.org");
|
|
|
|
|
|
|
|
/* Test we don't find false positives */
|
|
|
|
strlcpy(address, "www.example.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
|
|
|
|
/* Test top-level-domain matching a bit harder */
|
|
|
|
addressmap_clear_configured();
|
2010-12-27 18:35:16 +01:00
|
|
|
strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
|
|
|
|
"MapAddress *.torproject.org 1.1.1.1\n"
|
|
|
|
"MapAddress *.net 2.2.2.2\n"
|
2010-08-02 21:09:37 +02:00
|
|
|
, sizeof(buf));
|
2011-11-30 20:10:22 +01:00
|
|
|
config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
|
2010-08-02 21:09:37 +02:00
|
|
|
config_register_addressmaps(get_options());
|
|
|
|
|
|
|
|
strlcpy(address, "www.abc.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-13 23:13:01 +01:00
|
|
|
test_streq(address, "www.abc.torserver.exit");
|
2010-08-02 21:09:37 +02:00
|
|
|
|
|
|
|
strlcpy(address, "www.def.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-12-13 23:13:01 +01:00
|
|
|
test_streq(address, "www.def.torserver.exit");
|
2010-08-02 21:09:37 +02:00
|
|
|
|
|
|
|
strlcpy(address, "www.torproject.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "1.1.1.1");
|
|
|
|
|
|
|
|
strlcpy(address, "test.torproject.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "1.1.1.1");
|
|
|
|
|
|
|
|
strlcpy(address, "torproject.net", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
test_streq(address, "2.2.2.2");
|
|
|
|
|
2010-12-27 18:35:16 +01:00
|
|
|
/* We don't support '*' as a mapping directive */
|
2010-08-02 21:09:37 +02:00
|
|
|
addressmap_clear_configured();
|
2010-12-27 18:35:16 +01:00
|
|
|
strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
|
2011-11-30 20:10:22 +01:00
|
|
|
config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
|
2010-08-02 21:09:37 +02:00
|
|
|
config_register_addressmaps(get_options());
|
|
|
|
|
|
|
|
strlcpy(address, "www.abc.com", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
|
|
|
|
strlcpy(address, "www.def.net", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
|
|
|
|
strlcpy(address, "www.torproject.org", sizeof(address));
|
2012-05-11 23:00:41 +02:00
|
|
|
test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
|
2010-08-02 21:09:37 +02:00
|
|
|
|
2012-11-25 19:45:36 +01:00
|
|
|
#undef addressmap_rewrite
|
|
|
|
|
2011-12-02 06:16:47 +01:00
|
|
|
done:
|
2010-08-02 21:09:37 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CONFIG_TEST(name, flags) \
|
|
|
|
{ #name, test_config_ ## name, flags, NULL, NULL }
|
|
|
|
|
|
|
|
struct testcase_t config_tests[] = {
|
2011-09-08 17:19:06 +02:00
|
|
|
CONFIG_TEST(addressmap, 0),
|
2010-08-02 21:09:37 +02:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|