OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/dns/mapped_host_resolver.h" | 5 #include "net/dns/mapped_host_resolver.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
12 #include "net/dns/mock_host_resolver.h" | 12 #include "net/dns/mock_host_resolver.h" |
13 #include "net/log/net_log.h" | 13 #include "net/log/net_log.h" |
| 14 #include "net/test/gtest_util.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
| 18 using net::test::IsError; |
| 19 using net::test::IsOk; |
| 20 |
16 namespace net { | 21 namespace net { |
17 | 22 |
18 namespace { | 23 namespace { |
19 | 24 |
20 std::string FirstAddress(const AddressList& address_list) { | 25 std::string FirstAddress(const AddressList& address_list) { |
21 if (address_list.empty()) | 26 if (address_list.empty()) |
22 return std::string(); | 27 return std::string(); |
23 return address_list.front().ToString(); | 28 return address_list.front().ToString(); |
24 } | 29 } |
25 | 30 |
(...skipping 15 matching lines...) Expand all Loading... |
41 // Try resolving "www.google.com:80". There are no mappings yet, so this | 46 // Try resolving "www.google.com:80". There are no mappings yet, so this |
42 // hits |resolver_impl| and fails. | 47 // hits |resolver_impl| and fails. |
43 TestCompletionCallback callback; | 48 TestCompletionCallback callback; |
44 rv = resolver->Resolve( | 49 rv = resolver->Resolve( |
45 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), | 50 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), |
46 DEFAULT_PRIORITY, | 51 DEFAULT_PRIORITY, |
47 &address_list, | 52 &address_list, |
48 callback.callback(), | 53 callback.callback(), |
49 NULL, | 54 NULL, |
50 BoundNetLog()); | 55 BoundNetLog()); |
51 EXPECT_EQ(ERR_IO_PENDING, rv); | 56 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
52 rv = callback.WaitForResult(); | 57 rv = callback.WaitForResult(); |
53 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 58 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
54 | 59 |
55 // Remap *.google.com to baz.com. | 60 // Remap *.google.com to baz.com. |
56 EXPECT_TRUE(resolver->AddRuleFromString("map *.google.com baz.com")); | 61 EXPECT_TRUE(resolver->AddRuleFromString("map *.google.com baz.com")); |
57 | 62 |
58 // Try resolving "www.google.com:80". Should be remapped to "baz.com:80". | 63 // Try resolving "www.google.com:80". Should be remapped to "baz.com:80". |
59 rv = resolver->Resolve( | 64 rv = resolver->Resolve( |
60 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), | 65 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), |
61 DEFAULT_PRIORITY, | 66 DEFAULT_PRIORITY, |
62 &address_list, | 67 &address_list, |
63 callback.callback(), | 68 callback.callback(), |
64 NULL, | 69 NULL, |
65 BoundNetLog()); | 70 BoundNetLog()); |
66 EXPECT_EQ(ERR_IO_PENDING, rv); | 71 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
67 rv = callback.WaitForResult(); | 72 rv = callback.WaitForResult(); |
68 EXPECT_EQ(OK, rv); | 73 EXPECT_THAT(rv, IsOk()); |
69 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); | 74 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); |
70 | 75 |
71 // Try resolving "foo.com:77". This will NOT be remapped, so result | 76 // Try resolving "foo.com:77". This will NOT be remapped, so result |
72 // is "foo.com:77". | 77 // is "foo.com:77". |
73 rv = resolver->Resolve(HostResolver::RequestInfo(HostPortPair("foo.com", 77)), | 78 rv = resolver->Resolve(HostResolver::RequestInfo(HostPortPair("foo.com", 77)), |
74 DEFAULT_PRIORITY, | 79 DEFAULT_PRIORITY, |
75 &address_list, | 80 &address_list, |
76 callback.callback(), | 81 callback.callback(), |
77 NULL, | 82 NULL, |
78 BoundNetLog()); | 83 BoundNetLog()); |
79 EXPECT_EQ(ERR_IO_PENDING, rv); | 84 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
80 rv = callback.WaitForResult(); | 85 rv = callback.WaitForResult(); |
81 EXPECT_EQ(OK, rv); | 86 EXPECT_THAT(rv, IsOk()); |
82 EXPECT_EQ("192.168.1.8:77", FirstAddress(address_list)); | 87 EXPECT_EQ("192.168.1.8:77", FirstAddress(address_list)); |
83 | 88 |
84 // Remap "*.org" to "proxy:99". | 89 // Remap "*.org" to "proxy:99". |
85 EXPECT_TRUE(resolver->AddRuleFromString("Map *.org proxy:99")); | 90 EXPECT_TRUE(resolver->AddRuleFromString("Map *.org proxy:99")); |
86 | 91 |
87 // Try resolving "chromium.org:61". Should be remapped to "proxy:99". | 92 // Try resolving "chromium.org:61". Should be remapped to "proxy:99". |
88 rv = resolver->Resolve( | 93 rv = resolver->Resolve( |
89 HostResolver::RequestInfo(HostPortPair("chromium.org", 61)), | 94 HostResolver::RequestInfo(HostPortPair("chromium.org", 61)), |
90 DEFAULT_PRIORITY, | 95 DEFAULT_PRIORITY, |
91 &address_list, | 96 &address_list, |
92 callback.callback(), | 97 callback.callback(), |
93 NULL, | 98 NULL, |
94 BoundNetLog()); | 99 BoundNetLog()); |
95 EXPECT_EQ(ERR_IO_PENDING, rv); | 100 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
96 rv = callback.WaitForResult(); | 101 rv = callback.WaitForResult(); |
97 EXPECT_EQ(OK, rv); | 102 EXPECT_THAT(rv, IsOk()); |
98 EXPECT_EQ("192.168.1.11:99", FirstAddress(address_list)); | 103 EXPECT_EQ("192.168.1.11:99", FirstAddress(address_list)); |
99 } | 104 } |
100 | 105 |
101 // Tests that exclusions are respected. | 106 // Tests that exclusions are respected. |
102 TEST(MappedHostResolverTest, Exclusion) { | 107 TEST(MappedHostResolverTest, Exclusion) { |
103 // Create a mock host resolver, with specific hostname to IP mappings. | 108 // Create a mock host resolver, with specific hostname to IP mappings. |
104 std::unique_ptr<MockHostResolver> resolver_impl(new MockHostResolver()); | 109 std::unique_ptr<MockHostResolver> resolver_impl(new MockHostResolver()); |
105 resolver_impl->rules()->AddRule("baz", "192.168.1.5"); | 110 resolver_impl->rules()->AddRule("baz", "192.168.1.5"); |
106 resolver_impl->rules()->AddRule("www.google.com", "192.168.1.3"); | 111 resolver_impl->rules()->AddRule("www.google.com", "192.168.1.3"); |
107 | 112 |
(...skipping 12 matching lines...) Expand all Loading... |
120 EXPECT_TRUE(resolver->AddRuleFromString("EXCLUDE *.google.com")); | 125 EXPECT_TRUE(resolver->AddRuleFromString("EXCLUDE *.google.com")); |
121 | 126 |
122 // Try resolving "www.google.com". Should not be remapped due to exclusion). | 127 // Try resolving "www.google.com". Should not be remapped due to exclusion). |
123 rv = resolver->Resolve( | 128 rv = resolver->Resolve( |
124 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), | 129 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), |
125 DEFAULT_PRIORITY, | 130 DEFAULT_PRIORITY, |
126 &address_list, | 131 &address_list, |
127 callback.callback(), | 132 callback.callback(), |
128 NULL, | 133 NULL, |
129 BoundNetLog()); | 134 BoundNetLog()); |
130 EXPECT_EQ(ERR_IO_PENDING, rv); | 135 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
131 rv = callback.WaitForResult(); | 136 rv = callback.WaitForResult(); |
132 EXPECT_EQ(OK, rv); | 137 EXPECT_THAT(rv, IsOk()); |
133 EXPECT_EQ("192.168.1.3:80", FirstAddress(address_list)); | 138 EXPECT_EQ("192.168.1.3:80", FirstAddress(address_list)); |
134 | 139 |
135 // Try resolving "chrome.com:80". Should be remapped to "baz:80". | 140 // Try resolving "chrome.com:80". Should be remapped to "baz:80". |
136 rv = resolver->Resolve( | 141 rv = resolver->Resolve( |
137 HostResolver::RequestInfo(HostPortPair("chrome.com", 80)), | 142 HostResolver::RequestInfo(HostPortPair("chrome.com", 80)), |
138 DEFAULT_PRIORITY, | 143 DEFAULT_PRIORITY, |
139 &address_list, | 144 &address_list, |
140 callback.callback(), | 145 callback.callback(), |
141 NULL, | 146 NULL, |
142 BoundNetLog()); | 147 BoundNetLog()); |
143 EXPECT_EQ(ERR_IO_PENDING, rv); | 148 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
144 rv = callback.WaitForResult(); | 149 rv = callback.WaitForResult(); |
145 EXPECT_EQ(OK, rv); | 150 EXPECT_THAT(rv, IsOk()); |
146 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); | 151 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); |
147 } | 152 } |
148 | 153 |
149 TEST(MappedHostResolverTest, SetRulesFromString) { | 154 TEST(MappedHostResolverTest, SetRulesFromString) { |
150 // Create a mock host resolver, with specific hostname to IP mappings. | 155 // Create a mock host resolver, with specific hostname to IP mappings. |
151 std::unique_ptr<MockHostResolver> resolver_impl(new MockHostResolver()); | 156 std::unique_ptr<MockHostResolver> resolver_impl(new MockHostResolver()); |
152 resolver_impl->rules()->AddRule("baz", "192.168.1.7"); | 157 resolver_impl->rules()->AddRule("baz", "192.168.1.7"); |
153 resolver_impl->rules()->AddRule("bar", "192.168.1.9"); | 158 resolver_impl->rules()->AddRule("bar", "192.168.1.9"); |
154 | 159 |
155 // Create a remapped resolver that uses |resolver_impl|. | 160 // Create a remapped resolver that uses |resolver_impl|. |
156 std::unique_ptr<MappedHostResolver> resolver( | 161 std::unique_ptr<MappedHostResolver> resolver( |
157 new MappedHostResolver(std::move(resolver_impl))); | 162 new MappedHostResolver(std::move(resolver_impl))); |
158 | 163 |
159 int rv; | 164 int rv; |
160 AddressList address_list; | 165 AddressList address_list; |
161 TestCompletionCallback callback; | 166 TestCompletionCallback callback; |
162 | 167 |
163 // Remap "*.com" to "baz", and *.net to "bar:60". | 168 // Remap "*.com" to "baz", and *.net to "bar:60". |
164 resolver->SetRulesFromString("map *.com baz , map *.net bar:60"); | 169 resolver->SetRulesFromString("map *.com baz , map *.net bar:60"); |
165 | 170 |
166 // Try resolving "www.google.com". Should be remapped to "baz". | 171 // Try resolving "www.google.com". Should be remapped to "baz". |
167 rv = resolver->Resolve( | 172 rv = resolver->Resolve( |
168 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), | 173 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), |
169 DEFAULT_PRIORITY, | 174 DEFAULT_PRIORITY, |
170 &address_list, | 175 &address_list, |
171 callback.callback(), | 176 callback.callback(), |
172 NULL, | 177 NULL, |
173 BoundNetLog()); | 178 BoundNetLog()); |
174 EXPECT_EQ(ERR_IO_PENDING, rv); | 179 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
175 rv = callback.WaitForResult(); | 180 rv = callback.WaitForResult(); |
176 EXPECT_EQ(OK, rv); | 181 EXPECT_THAT(rv, IsOk()); |
177 EXPECT_EQ("192.168.1.7:80", FirstAddress(address_list)); | 182 EXPECT_EQ("192.168.1.7:80", FirstAddress(address_list)); |
178 | 183 |
179 // Try resolving "chrome.net:80". Should be remapped to "bar:60". | 184 // Try resolving "chrome.net:80". Should be remapped to "bar:60". |
180 rv = resolver->Resolve( | 185 rv = resolver->Resolve( |
181 HostResolver::RequestInfo(HostPortPair("chrome.net", 80)), | 186 HostResolver::RequestInfo(HostPortPair("chrome.net", 80)), |
182 DEFAULT_PRIORITY, | 187 DEFAULT_PRIORITY, |
183 &address_list, | 188 &address_list, |
184 callback.callback(), | 189 callback.callback(), |
185 NULL, | 190 NULL, |
186 BoundNetLog()); | 191 BoundNetLog()); |
187 EXPECT_EQ(ERR_IO_PENDING, rv); | 192 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
188 rv = callback.WaitForResult(); | 193 rv = callback.WaitForResult(); |
189 EXPECT_EQ(OK, rv); | 194 EXPECT_THAT(rv, IsOk()); |
190 EXPECT_EQ("192.168.1.9:60", FirstAddress(address_list)); | 195 EXPECT_EQ("192.168.1.9:60", FirstAddress(address_list)); |
191 } | 196 } |
192 | 197 |
193 // Parsing bad rules should silently discard the rule (and never crash). | 198 // Parsing bad rules should silently discard the rule (and never crash). |
194 TEST(MappedHostResolverTest, ParseInvalidRules) { | 199 TEST(MappedHostResolverTest, ParseInvalidRules) { |
195 std::unique_ptr<MappedHostResolver> resolver( | 200 std::unique_ptr<MappedHostResolver> resolver( |
196 new MappedHostResolver(std::unique_ptr<HostResolver>())); | 201 new MappedHostResolver(std::unique_ptr<HostResolver>())); |
197 | 202 |
198 EXPECT_FALSE(resolver->AddRuleFromString("xyz")); | 203 EXPECT_FALSE(resolver->AddRuleFromString("xyz")); |
199 EXPECT_FALSE(resolver->AddRuleFromString(std::string())); | 204 EXPECT_FALSE(resolver->AddRuleFromString(std::string())); |
(...skipping 21 matching lines...) Expand all Loading... |
221 | 226 |
222 // Try resolving www.google.com --> Should give an error. | 227 // Try resolving www.google.com --> Should give an error. |
223 TestCompletionCallback callback1; | 228 TestCompletionCallback callback1; |
224 rv = resolver->Resolve( | 229 rv = resolver->Resolve( |
225 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), | 230 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), |
226 DEFAULT_PRIORITY, | 231 DEFAULT_PRIORITY, |
227 &address_list, | 232 &address_list, |
228 callback1.callback(), | 233 callback1.callback(), |
229 NULL, | 234 NULL, |
230 BoundNetLog()); | 235 BoundNetLog()); |
231 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 236 EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
232 | 237 |
233 // Try resolving www.foo.com --> Should succeed. | 238 // Try resolving www.foo.com --> Should succeed. |
234 TestCompletionCallback callback2; | 239 TestCompletionCallback callback2; |
235 rv = resolver->Resolve( | 240 rv = resolver->Resolve( |
236 HostResolver::RequestInfo(HostPortPair("www.foo.com", 80)), | 241 HostResolver::RequestInfo(HostPortPair("www.foo.com", 80)), |
237 DEFAULT_PRIORITY, | 242 DEFAULT_PRIORITY, |
238 &address_list, | 243 &address_list, |
239 callback2.callback(), | 244 callback2.callback(), |
240 NULL, | 245 NULL, |
241 BoundNetLog()); | 246 BoundNetLog()); |
242 EXPECT_EQ(ERR_IO_PENDING, rv); | 247 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
243 rv = callback2.WaitForResult(); | 248 rv = callback2.WaitForResult(); |
244 EXPECT_EQ(OK, rv); | 249 EXPECT_THAT(rv, IsOk()); |
245 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); | 250 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list)); |
246 } | 251 } |
247 | 252 |
248 } // namespace | 253 } // namespace |
249 | 254 |
250 } // namespace net | 255 } // namespace net |
OLD | NEW |