roll skia to 4276
[chromium-blink-merge.git] / net / base / mapped_host_resolver_unittest.cc
blob5ea95c0aca490b559c231dc416ab8565638685ef
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"
15 namespace net {
17 namespace {
19 std::string FirstAddress(const AddressList& address_list) {
20 if (address_list.empty())
21 return "";
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));
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 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));
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 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));
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(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"));
182 } // namespace
184 } // namespace net