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
3 // found in the LICENSE file.
5 #include "net/dns/mapped_host_resolver.h"
7 #include "net/base/address_list.h"
8 #include "net/base/net_errors.h"
9 #include "net/base/net_log.h"
10 #include "net/base/net_util.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 std::string
FirstAddress(const AddressList
& address_list
) {
20 if (address_list
.empty())
22 return address_list
.front().ToString();
25 TEST(MappedHostResolverTest
, Inclusion
) {
26 // Create a mock host resolver, with specific hostname to IP mappings.
27 scoped_ptr
<MockHostResolver
> resolver_impl(new MockHostResolver());
28 resolver_impl
->rules()->AddSimulatedFailure("*google.com");
29 resolver_impl
->rules()->AddRule("baz.com", "192.168.1.5");
30 resolver_impl
->rules()->AddRule("foo.com", "192.168.1.8");
31 resolver_impl
->rules()->AddRule("proxy", "192.168.1.11");
33 // Create a remapped resolver that uses |resolver_impl|.
34 scoped_ptr
<MappedHostResolver
> resolver(
35 new MappedHostResolver(resolver_impl
.PassAs
<HostResolver
>()));
38 AddressList address_list
;
40 // Try resolving "www.google.com:80". There are no mappings yet, so this
41 // hits |resolver_impl| and fails.
42 TestCompletionCallback callback
;
43 rv
= resolver
->Resolve(
44 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
50 EXPECT_EQ(ERR_IO_PENDING
, rv
);
51 rv
= callback
.WaitForResult();
52 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, rv
);
54 // Remap *.google.com to baz.com.
55 EXPECT_TRUE(resolver
->AddRuleFromString("map *.google.com baz.com"));
57 // Try resolving "www.google.com:80". Should be remapped to "baz.com:80".
58 rv
= resolver
->Resolve(
59 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
65 EXPECT_EQ(ERR_IO_PENDING
, rv
);
66 rv
= callback
.WaitForResult();
68 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list
));
70 // Try resolving "foo.com:77". This will NOT be remapped, so result
72 rv
= resolver
->Resolve(HostResolver::RequestInfo(HostPortPair("foo.com", 77)),
78 EXPECT_EQ(ERR_IO_PENDING
, rv
);
79 rv
= callback
.WaitForResult();
81 EXPECT_EQ("192.168.1.8:77", FirstAddress(address_list
));
83 // Remap "*.org" to "proxy:99".
84 EXPECT_TRUE(resolver
->AddRuleFromString("Map *.org proxy:99"));
86 // Try resolving "chromium.org:61". Should be remapped to "proxy:99".
87 rv
= resolver
->Resolve(
88 HostResolver::RequestInfo(HostPortPair("chromium.org", 61)),
94 EXPECT_EQ(ERR_IO_PENDING
, rv
);
95 rv
= callback
.WaitForResult();
97 EXPECT_EQ("192.168.1.11:99", FirstAddress(address_list
));
100 // Tests that exclusions are respected.
101 TEST(MappedHostResolverTest
, Exclusion
) {
102 // Create a mock host resolver, with specific hostname to IP mappings.
103 scoped_ptr
<MockHostResolver
> resolver_impl(new MockHostResolver());
104 resolver_impl
->rules()->AddRule("baz", "192.168.1.5");
105 resolver_impl
->rules()->AddRule("www.google.com", "192.168.1.3");
107 // Create a remapped resolver that uses |resolver_impl|.
108 scoped_ptr
<MappedHostResolver
> resolver(
109 new MappedHostResolver(resolver_impl
.PassAs
<HostResolver
>()));
112 AddressList address_list
;
113 TestCompletionCallback callback
;
115 // Remap "*.com" to "baz".
116 EXPECT_TRUE(resolver
->AddRuleFromString("map *.com baz"));
118 // Add an exclusion for "*.google.com".
119 EXPECT_TRUE(resolver
->AddRuleFromString("EXCLUDE *.google.com"));
121 // Try resolving "www.google.com". Should not be remapped due to exclusion).
122 rv
= resolver
->Resolve(
123 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
129 EXPECT_EQ(ERR_IO_PENDING
, rv
);
130 rv
= callback
.WaitForResult();
132 EXPECT_EQ("192.168.1.3:80", FirstAddress(address_list
));
134 // Try resolving "chrome.com:80". Should be remapped to "baz:80".
135 rv
= resolver
->Resolve(
136 HostResolver::RequestInfo(HostPortPair("chrome.com", 80)),
142 EXPECT_EQ(ERR_IO_PENDING
, rv
);
143 rv
= callback
.WaitForResult();
145 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list
));
148 TEST(MappedHostResolverTest
, SetRulesFromString
) {
149 // Create a mock host resolver, with specific hostname to IP mappings.
150 scoped_ptr
<MockHostResolver
> resolver_impl(new MockHostResolver());
151 resolver_impl
->rules()->AddRule("baz", "192.168.1.7");
152 resolver_impl
->rules()->AddRule("bar", "192.168.1.9");
154 // Create a remapped resolver that uses |resolver_impl|.
155 scoped_ptr
<MappedHostResolver
> resolver(
156 new MappedHostResolver(resolver_impl
.PassAs
<HostResolver
>()));
159 AddressList address_list
;
160 TestCompletionCallback callback
;
162 // Remap "*.com" to "baz", and *.net to "bar:60".
163 resolver
->SetRulesFromString("map *.com baz , map *.net bar:60");
165 // Try resolving "www.google.com". Should be remapped to "baz".
166 rv
= resolver
->Resolve(
167 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
173 EXPECT_EQ(ERR_IO_PENDING
, rv
);
174 rv
= callback
.WaitForResult();
176 EXPECT_EQ("192.168.1.7:80", FirstAddress(address_list
));
178 // Try resolving "chrome.net:80". Should be remapped to "bar:60".
179 rv
= resolver
->Resolve(
180 HostResolver::RequestInfo(HostPortPair("chrome.net", 80)),
186 EXPECT_EQ(ERR_IO_PENDING
, rv
);
187 rv
= callback
.WaitForResult();
189 EXPECT_EQ("192.168.1.9:60", FirstAddress(address_list
));
192 // Parsing bad rules should silently discard the rule (and never crash).
193 TEST(MappedHostResolverTest
, ParseInvalidRules
) {
194 scoped_ptr
<MappedHostResolver
> resolver(
195 new MappedHostResolver(scoped_ptr
<HostResolver
>()));
197 EXPECT_FALSE(resolver
->AddRuleFromString("xyz"));
198 EXPECT_FALSE(resolver
->AddRuleFromString(std::string()));
199 EXPECT_FALSE(resolver
->AddRuleFromString(" "));
200 EXPECT_FALSE(resolver
->AddRuleFromString("EXCLUDE"));
201 EXPECT_FALSE(resolver
->AddRuleFromString("EXCLUDE foo bar"));
202 EXPECT_FALSE(resolver
->AddRuleFromString("INCLUDE"));
203 EXPECT_FALSE(resolver
->AddRuleFromString("INCLUDE x"));
204 EXPECT_FALSE(resolver
->AddRuleFromString("INCLUDE x :10"));
207 // Test mapping hostnames to resolving failures.
208 TEST(MappedHostResolverTest
, MapToError
) {
209 scoped_ptr
<MockHostResolver
> resolver_impl(new MockHostResolver());
210 resolver_impl
->rules()->AddRule("*", "192.168.1.5");
212 scoped_ptr
<MappedHostResolver
> resolver(
213 new MappedHostResolver(resolver_impl
.PassAs
<HostResolver
>()));
216 AddressList address_list
;
218 // Remap *.google.com to resolving failures.
219 EXPECT_TRUE(resolver
->AddRuleFromString("MAP *.google.com ~NOTFOUND"));
221 // Try resolving www.google.com --> Should give an error.
222 TestCompletionCallback callback1
;
223 rv
= resolver
->Resolve(
224 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)),
227 callback1
.callback(),
230 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, rv
);
232 // Try resolving www.foo.com --> Should succeed.
233 TestCompletionCallback callback2
;
234 rv
= resolver
->Resolve(
235 HostResolver::RequestInfo(HostPortPair("www.foo.com", 80)),
238 callback2
.callback(),
241 EXPECT_EQ(ERR_IO_PENDING
, rv
);
242 rv
= callback2
.WaitForResult();
244 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list
));