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/base/mapped_host_resolver.h"
7 #include "net/base/address_list.h"
8 #include "net/base/mock_host_resolver.h"
9 #include "net/base/net_errors.h"
10 #include "net/base/net_log.h"
11 #include "net/base/net_util.h"
12 #include "net/base/test_completion_callback.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 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
));
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(HostResolver::RequestInfo(
44 HostPortPair("www.google.com", 80)),
45 &address_list
, callback
.callback(), NULL
,
47 EXPECT_EQ(ERR_IO_PENDING
, rv
);
48 rv
= callback
.WaitForResult();
49 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, rv
);
51 // Remap *.google.com to baz.com.
52 EXPECT_TRUE(resolver
->AddRuleFromString("map *.google.com baz.com"));
54 // Try resolving "www.google.com:80". Should be remapped to "baz.com:80".
55 rv
= resolver
->Resolve(HostResolver::RequestInfo(
56 HostPortPair("www.google.com", 80)),
57 &address_list
, callback
.callback(), NULL
,
59 EXPECT_EQ(ERR_IO_PENDING
, rv
);
60 rv
= callback
.WaitForResult();
62 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list
));
64 // Try resolving "foo.com:77". This will NOT be remapped, so result
66 rv
= resolver
->Resolve(HostResolver::RequestInfo(HostPortPair("foo.com", 77)),
67 &address_list
, callback
.callback(), NULL
,
69 EXPECT_EQ(ERR_IO_PENDING
, rv
);
70 rv
= callback
.WaitForResult();
72 EXPECT_EQ("192.168.1.8:77", FirstAddress(address_list
));
74 // Remap "*.org" to "proxy:99".
75 EXPECT_TRUE(resolver
->AddRuleFromString("Map *.org proxy:99"));
77 // Try resolving "chromium.org:61". Should be remapped to "proxy:99".
78 rv
= resolver
->Resolve(HostResolver::RequestInfo
79 (HostPortPair("chromium.org", 61)),
80 &address_list
, callback
.callback(), NULL
,
82 EXPECT_EQ(ERR_IO_PENDING
, rv
);
83 rv
= callback
.WaitForResult();
85 EXPECT_EQ("192.168.1.11:99", FirstAddress(address_list
));
88 // Tests that exclusions are respected.
89 TEST(MappedHostResolverTest
, Exclusion
) {
90 // Create a mock host resolver, with specific hostname to IP mappings.
91 MockHostResolver
* resolver_impl(new MockHostResolver());
92 resolver_impl
->rules()->AddRule("baz", "192.168.1.5");
93 resolver_impl
->rules()->AddRule("www.google.com", "192.168.1.3");
95 // Create a remapped resolver that uses |resolver_impl|.
96 scoped_ptr
<MappedHostResolver
> resolver(
97 new MappedHostResolver(resolver_impl
));
100 AddressList address_list
;
101 TestCompletionCallback callback
;
103 // Remap "*.com" to "baz".
104 EXPECT_TRUE(resolver
->AddRuleFromString("map *.com baz"));
106 // Add an exclusion for "*.google.com".
107 EXPECT_TRUE(resolver
->AddRuleFromString("EXCLUDE *.google.com"));
109 // Try resolving "www.google.com". Should not be remapped due to exclusion).
110 rv
= resolver
->Resolve(HostResolver::RequestInfo(
111 HostPortPair("www.google.com", 80)),
112 &address_list
, callback
.callback(), NULL
,
114 EXPECT_EQ(ERR_IO_PENDING
, rv
);
115 rv
= callback
.WaitForResult();
117 EXPECT_EQ("192.168.1.3:80", FirstAddress(address_list
));
119 // Try resolving "chrome.com:80". Should be remapped to "baz:80".
120 rv
= resolver
->Resolve(HostResolver::RequestInfo(
121 HostPortPair("chrome.com", 80)),
122 &address_list
, callback
.callback(), NULL
,
124 EXPECT_EQ(ERR_IO_PENDING
, rv
);
125 rv
= callback
.WaitForResult();
127 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list
));
130 TEST(MappedHostResolverTest
, SetRulesFromString
) {
131 // Create a mock host resolver, with specific hostname to IP mappings.
132 MockHostResolver
* resolver_impl(new MockHostResolver());
133 resolver_impl
->rules()->AddRule("baz", "192.168.1.7");
134 resolver_impl
->rules()->AddRule("bar", "192.168.1.9");
136 // Create a remapped resolver that uses |resolver_impl|.
137 scoped_ptr
<MappedHostResolver
> resolver(
138 new MappedHostResolver(resolver_impl
));
141 AddressList address_list
;
142 TestCompletionCallback callback
;
144 // Remap "*.com" to "baz", and *.net to "bar:60".
145 resolver
->SetRulesFromString("map *.com baz , map *.net bar:60");
147 // Try resolving "www.google.com". Should be remapped to "baz".
148 rv
= resolver
->Resolve(HostResolver::RequestInfo(
149 HostPortPair("www.google.com", 80)),
150 &address_list
, callback
.callback(), NULL
,
152 EXPECT_EQ(ERR_IO_PENDING
, rv
);
153 rv
= callback
.WaitForResult();
155 EXPECT_EQ("192.168.1.7:80", FirstAddress(address_list
));
157 // Try resolving "chrome.net:80". Should be remapped to "bar:60".
158 rv
= resolver
->Resolve(HostResolver::RequestInfo(
159 HostPortPair("chrome.net", 80)),
160 &address_list
, callback
.callback(), NULL
,
162 EXPECT_EQ(ERR_IO_PENDING
, rv
);
163 rv
= callback
.WaitForResult();
165 EXPECT_EQ("192.168.1.9:60", FirstAddress(address_list
));
168 // Parsing bad rules should silently discard the rule (and never crash).
169 TEST(MappedHostResolverTest
, ParseInvalidRules
) {
170 scoped_ptr
<MappedHostResolver
> resolver(new MappedHostResolver(NULL
));
172 EXPECT_FALSE(resolver
->AddRuleFromString("xyz"));
173 EXPECT_FALSE(resolver
->AddRuleFromString(""));
174 EXPECT_FALSE(resolver
->AddRuleFromString(" "));
175 EXPECT_FALSE(resolver
->AddRuleFromString("EXCLUDE"));
176 EXPECT_FALSE(resolver
->AddRuleFromString("EXCLUDE foo bar"));
177 EXPECT_FALSE(resolver
->AddRuleFromString("INCLUDE"));
178 EXPECT_FALSE(resolver
->AddRuleFromString("INCLUDE x"));
179 EXPECT_FALSE(resolver
->AddRuleFromString("INCLUDE x :10"));