Revert 208429 "Disable AutofillQueryXmlParserTest.ParseAutofillF..."
[chromium-blink-merge.git] / net / dns / mapped_host_resolver_unittest.cc
blobd8594663c9dd09b691f43483eaf6f2bfa1eec797
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"
15 namespace net {
17 namespace {
19 std::string FirstAddress(const AddressList& address_list) {
20 if (address_list.empty())
21 return std::string();
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>()));
37 int rv;
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,
46 BoundNetLog());
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,
58 BoundNetLog());
59 EXPECT_EQ(ERR_IO_PENDING, rv);
60 rv = callback.WaitForResult();
61 EXPECT_EQ(OK, rv);
62 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list));
64 // Try resolving "foo.com:77". This will NOT be remapped, so result
65 // is "foo.com:77".
66 rv = resolver->Resolve(HostResolver::RequestInfo(HostPortPair("foo.com", 77)),
67 &address_list, callback.callback(), NULL,
68 BoundNetLog());
69 EXPECT_EQ(ERR_IO_PENDING, rv);
70 rv = callback.WaitForResult();
71 EXPECT_EQ(OK, rv);
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,
81 BoundNetLog());
82 EXPECT_EQ(ERR_IO_PENDING, rv);
83 rv = callback.WaitForResult();
84 EXPECT_EQ(OK, rv);
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 scoped_ptr<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.PassAs<HostResolver>()));
99 int rv;
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,
113 BoundNetLog());
114 EXPECT_EQ(ERR_IO_PENDING, rv);
115 rv = callback.WaitForResult();
116 EXPECT_EQ(OK, rv);
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,
123 BoundNetLog());
124 EXPECT_EQ(ERR_IO_PENDING, rv);
125 rv = callback.WaitForResult();
126 EXPECT_EQ(OK, rv);
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 scoped_ptr<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.PassAs<HostResolver>()));
140 int rv;
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,
151 BoundNetLog());
152 EXPECT_EQ(ERR_IO_PENDING, rv);
153 rv = callback.WaitForResult();
154 EXPECT_EQ(OK, rv);
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,
161 BoundNetLog());
162 EXPECT_EQ(ERR_IO_PENDING, rv);
163 rv = callback.WaitForResult();
164 EXPECT_EQ(OK, rv);
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(
171 new MappedHostResolver(scoped_ptr<HostResolver>()));
173 EXPECT_FALSE(resolver->AddRuleFromString("xyz"));
174 EXPECT_FALSE(resolver->AddRuleFromString(std::string()));
175 EXPECT_FALSE(resolver->AddRuleFromString(" "));
176 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE"));
177 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE foo bar"));
178 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE"));
179 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x"));
180 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x :10"));
183 // Test mapping hostnames to resolving failures.
184 TEST(MappedHostResolverTest, MapToError) {
185 scoped_ptr<MockHostResolver> resolver_impl(new MockHostResolver());
186 resolver_impl->rules()->AddRule("*", "192.168.1.5");
188 scoped_ptr<MappedHostResolver> resolver(
189 new MappedHostResolver(resolver_impl.PassAs<HostResolver>()));
191 int rv;
192 AddressList address_list;
194 // Remap *.google.com to resolving failures.
195 EXPECT_TRUE(resolver->AddRuleFromString("MAP *.google.com ~NOTFOUND"));
197 // Try resolving www.google.com --> Should give an error.
198 TestCompletionCallback callback1;
199 rv = resolver->Resolve(HostResolver::RequestInfo(
200 HostPortPair("www.google.com", 80)),
201 &address_list, callback1.callback(), NULL,
202 BoundNetLog());
203 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv);
205 // Try resolving www.foo.com --> Should succeed.
206 TestCompletionCallback callback2;
207 rv = resolver->Resolve(HostResolver::RequestInfo(
208 HostPortPair("www.foo.com", 80)),
209 &address_list, callback2.callback(), NULL,
210 BoundNetLog());
211 EXPECT_EQ(ERR_IO_PENDING, rv);
212 rv = callback2.WaitForResult();
213 EXPECT_EQ(OK, rv);
214 EXPECT_EQ("192.168.1.5:80", FirstAddress(address_list));
217 } // namespace
219 } // namespace net