1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2012, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
9 #include "connection_edge.h"
13 test_config_addressmap(void *arg
)
17 time_t expires
= TIME_MAX
;
20 strlcpy(buf
, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
21 "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
22 "MapAddress *.google.com *.torserver.exit\n"
23 "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
24 "MapAddress *.cn.com www.cnn.com\n"
25 "MapAddress *.cnn.com www.cnn.com\n"
26 "MapAddress ex.com www.cnn.com\n"
27 "MapAddress ey.com *.cnn.com\n"
28 "MapAddress www.torproject.org 1.1.1.1\n"
29 "MapAddress other.torproject.org "
30 "this.torproject.org.otherserver.exit\n"
31 "MapAddress test.torproject.org 2.2.2.2\n"
32 "MapAddress www.google.com 3.3.3.3\n"
33 "MapAddress www.example.org 4.4.4.4\n"
34 "MapAddress 4.4.4.4 7.7.7.7\n"
35 "MapAddress 4.4.4.4 5.5.5.5\n"
36 "MapAddress www.infiniteloop.org 6.6.6.6\n"
37 "MapAddress 6.6.6.6 www.infiniteloop.org\n"
40 config_get_lines(buf
, &(get_options_mutable()->AddressMap
), 0);
41 config_register_addressmaps(get_options());
43 /* MapAddress .invalidwildcard.com .torserver.exit - no match */
44 strlcpy(address
, "www.invalidwildcard.com", sizeof(address
));
45 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
47 /* MapAddress *invalidasterisk.com .torserver.exit - no match */
48 strlcpy(address
, "www.invalidasterisk.com", sizeof(address
));
49 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
51 /* Where no mapping for FQDN match on top-level domain */
52 /* MapAddress .google.com .torserver.exit */
53 strlcpy(address
, "reader.google.com", sizeof(address
));
54 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
55 test_streq(address
, "reader.torserver.exit");
57 /* MapAddress *.yahoo.com *.google.com.torserver.exit */
58 strlcpy(address
, "reader.yahoo.com", sizeof(address
));
59 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
60 test_streq(address
, "reader.google.com.torserver.exit");
62 /*MapAddress *.cnn.com www.cnn.com */
63 strlcpy(address
, "cnn.com", sizeof(address
));
64 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
65 test_streq(address
, "www.cnn.com");
67 /* MapAddress .cn.com www.cnn.com */
68 strlcpy(address
, "www.cn.com", sizeof(address
));
69 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
70 test_streq(address
, "www.cnn.com");
72 /* MapAddress ex.com www.cnn.com - no match */
73 strlcpy(address
, "www.ex.com", sizeof(address
));
74 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
76 /* MapAddress ey.com *.cnn.com - invalid expression */
77 strlcpy(address
, "ey.com", sizeof(address
));
78 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
80 /* Where mapping for FQDN match on FQDN */
81 strlcpy(address
, "www.google.com", sizeof(address
));
82 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
83 test_streq(address
, "3.3.3.3");
85 strlcpy(address
, "www.torproject.org", sizeof(address
));
86 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
87 test_streq(address
, "1.1.1.1");
89 strlcpy(address
, "other.torproject.org", sizeof(address
));
90 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
91 test_streq(address
, "this.torproject.org.otherserver.exit");
93 strlcpy(address
, "test.torproject.org", sizeof(address
));
94 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
95 test_streq(address
, "2.2.2.2");
97 /* Test a chain of address mappings and the order in which they were added:
98 "MapAddress www.example.org 4.4.4.4"
99 "MapAddress 4.4.4.4 7.7.7.7"
100 "MapAddress 4.4.4.4 5.5.5.5"
102 strlcpy(address
, "www.example.org", sizeof(address
));
103 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
104 test_streq(address
, "5.5.5.5");
106 /* Test infinite address mapping results in no change */
107 strlcpy(address
, "www.infiniteloop.org", sizeof(address
));
108 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
109 test_streq(address
, "www.infiniteloop.org");
111 /* Test we don't find false positives */
112 strlcpy(address
, "www.example.com", sizeof(address
));
113 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
115 /* Test top-level-domain matching a bit harder */
116 addressmap_clear_configured();
117 strlcpy(buf
, "MapAddress *.com *.torserver.exit\n"
118 "MapAddress *.torproject.org 1.1.1.1\n"
119 "MapAddress *.net 2.2.2.2\n"
121 config_get_lines(buf
, &(get_options_mutable()->AddressMap
), 0);
122 config_register_addressmaps(get_options());
124 strlcpy(address
, "www.abc.com", sizeof(address
));
125 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
126 test_streq(address
, "www.abc.torserver.exit");
128 strlcpy(address
, "www.def.com", sizeof(address
));
129 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
130 test_streq(address
, "www.def.torserver.exit");
132 strlcpy(address
, "www.torproject.org", sizeof(address
));
133 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
134 test_streq(address
, "1.1.1.1");
136 strlcpy(address
, "test.torproject.org", sizeof(address
));
137 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
138 test_streq(address
, "1.1.1.1");
140 strlcpy(address
, "torproject.net", sizeof(address
));
141 test_assert(addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
142 test_streq(address
, "2.2.2.2");
144 /* We don't support '*' as a mapping directive */
145 addressmap_clear_configured();
146 strlcpy(buf
, "MapAddress * *.torserver.exit\n", sizeof(buf
));
147 config_get_lines(buf
, &(get_options_mutable()->AddressMap
), 0);
148 config_register_addressmaps(get_options());
150 strlcpy(address
, "www.abc.com", sizeof(address
));
151 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
153 strlcpy(address
, "www.def.net", sizeof(address
));
154 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
156 strlcpy(address
, "www.torproject.org", sizeof(address
));
157 test_assert(!addressmap_rewrite(address
, sizeof(address
), &expires
, NULL
));
163 #define CONFIG_TEST(name, flags) \
164 { #name, test_config_ ## name, flags, NULL, NULL }
166 struct testcase_t config_tests
[] = {
167 CONFIG_TEST(addressmap
, 0),