OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/base/mapped_host_resolver.h" | 5 #include "net/base/mapped_host_resolver.h" |
6 | 6 |
7 #include "net/base/mock_host_resolver.h" | 7 #include "net/base/mock_host_resolver.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 9 #include "net/base/net_log.h" |
9 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace net { | 13 namespace net { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 TEST(MappedHostResolverTest, Inclusion) { | 17 TEST(MappedHostResolverTest, Inclusion) { |
17 // Create a mock host resolver, with specific hostname to IP mappings. | 18 // Create a mock host resolver, with specific hostname to IP mappings. |
18 scoped_refptr<MockHostResolver> resolver_impl = new MockHostResolver(); | 19 scoped_refptr<MockHostResolver> resolver_impl = new MockHostResolver(); |
19 resolver_impl->rules()->AddSimulatedFailure("*google.com"); | 20 resolver_impl->rules()->AddSimulatedFailure("*google.com"); |
20 resolver_impl->rules()->AddRule("baz.com", "192.168.1.5"); | 21 resolver_impl->rules()->AddRule("baz.com", "192.168.1.5"); |
21 resolver_impl->rules()->AddRule("foo.com", "192.168.1.8"); | 22 resolver_impl->rules()->AddRule("foo.com", "192.168.1.8"); |
22 resolver_impl->rules()->AddRule("proxy", "192.168.1.11"); | 23 resolver_impl->rules()->AddRule("proxy", "192.168.1.11"); |
23 | 24 |
24 // Create a remapped resolver that uses |resolver_impl|. | 25 // Create a remapped resolver that uses |resolver_impl|. |
25 scoped_refptr<MappedHostResolver> resolver = | 26 scoped_refptr<MappedHostResolver> resolver = |
26 new MappedHostResolver(resolver_impl); | 27 new MappedHostResolver(resolver_impl); |
27 | 28 |
28 int rv; | 29 int rv; |
29 AddressList address_list; | 30 AddressList address_list; |
30 | 31 |
31 // Try resolving "www.google.com:80". There are no mappings yet, so this | 32 // Try resolving "www.google.com:80". There are no mappings yet, so this |
32 // hits |resolver_impl| and fails. | 33 // hits |resolver_impl| and fails. |
33 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), | 34 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), |
34 &address_list, NULL, NULL, NULL); | 35 &address_list, NULL, NULL, BoundNetLog()); |
35 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 36 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
36 | 37 |
37 // Remap *.google.com to baz.com. | 38 // Remap *.google.com to baz.com. |
38 EXPECT_TRUE(resolver->AddRuleFromString("map *.google.com baz.com")); | 39 EXPECT_TRUE(resolver->AddRuleFromString("map *.google.com baz.com")); |
39 | 40 |
40 // Try resolving "www.google.com:80". Should be remapped to "baz.com:80". | 41 // Try resolving "www.google.com:80". Should be remapped to "baz.com:80". |
41 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), | 42 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), |
42 &address_list, NULL, NULL, NULL); | 43 &address_list, NULL, NULL, BoundNetLog()); |
43 EXPECT_EQ(OK, rv); | 44 EXPECT_EQ(OK, rv); |
44 EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head())); | 45 EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head())); |
45 EXPECT_EQ(80, address_list.GetPort()); | 46 EXPECT_EQ(80, address_list.GetPort()); |
46 | 47 |
47 // Try resolving "foo.com:77". This will NOT be remapped, so result | 48 // Try resolving "foo.com:77". This will NOT be remapped, so result |
48 // is "foo.com:77". | 49 // is "foo.com:77". |
49 rv = resolver->Resolve(HostResolver::RequestInfo("foo.com", 77), | 50 rv = resolver->Resolve(HostResolver::RequestInfo("foo.com", 77), |
50 &address_list, NULL, NULL, NULL); | 51 &address_list, NULL, NULL, BoundNetLog()); |
51 EXPECT_EQ(OK, rv); | 52 EXPECT_EQ(OK, rv); |
52 EXPECT_EQ("192.168.1.8", NetAddressToString(address_list.head())); | 53 EXPECT_EQ("192.168.1.8", NetAddressToString(address_list.head())); |
53 EXPECT_EQ(77, address_list.GetPort()); | 54 EXPECT_EQ(77, address_list.GetPort()); |
54 | 55 |
55 // Remap "*.org" to "proxy:99". | 56 // Remap "*.org" to "proxy:99". |
56 EXPECT_TRUE(resolver->AddRuleFromString("Map *.org proxy:99")); | 57 EXPECT_TRUE(resolver->AddRuleFromString("Map *.org proxy:99")); |
57 | 58 |
58 // Try resolving "chromium.org:61". Should be remapped to "proxy:99". | 59 // Try resolving "chromium.org:61". Should be remapped to "proxy:99". |
59 rv = resolver->Resolve(HostResolver::RequestInfo("chromium.org", 61), | 60 rv = resolver->Resolve(HostResolver::RequestInfo("chromium.org", 61), |
60 &address_list, NULL, NULL, NULL); | 61 &address_list, NULL, NULL, BoundNetLog()); |
61 EXPECT_EQ(OK, rv); | 62 EXPECT_EQ(OK, rv); |
62 EXPECT_EQ("192.168.1.11", NetAddressToString(address_list.head())); | 63 EXPECT_EQ("192.168.1.11", NetAddressToString(address_list.head())); |
63 EXPECT_EQ(99, address_list.GetPort()); | 64 EXPECT_EQ(99, address_list.GetPort()); |
64 } | 65 } |
65 | 66 |
66 // Tests that exclusions are respected. | 67 // Tests that exclusions are respected. |
67 TEST(MappedHostResolverTest, Exclusion) { | 68 TEST(MappedHostResolverTest, Exclusion) { |
68 // Create a mock host resolver, with specific hostname to IP mappings. | 69 // Create a mock host resolver, with specific hostname to IP mappings. |
69 scoped_refptr<MockHostResolver> resolver_impl = new MockHostResolver(); | 70 scoped_refptr<MockHostResolver> resolver_impl = new MockHostResolver(); |
70 resolver_impl->rules()->AddRule("baz", "192.168.1.5"); | 71 resolver_impl->rules()->AddRule("baz", "192.168.1.5"); |
71 resolver_impl->rules()->AddRule("www.google.com", "192.168.1.3"); | 72 resolver_impl->rules()->AddRule("www.google.com", "192.168.1.3"); |
72 | 73 |
73 // Create a remapped resolver that uses |resolver_impl|. | 74 // Create a remapped resolver that uses |resolver_impl|. |
74 scoped_refptr<MappedHostResolver> resolver = | 75 scoped_refptr<MappedHostResolver> resolver = |
75 new MappedHostResolver(resolver_impl); | 76 new MappedHostResolver(resolver_impl); |
76 | 77 |
77 int rv; | 78 int rv; |
78 AddressList address_list; | 79 AddressList address_list; |
79 | 80 |
80 // Remap "*.com" to "baz". | 81 // Remap "*.com" to "baz". |
81 EXPECT_TRUE(resolver->AddRuleFromString("map *.com baz")); | 82 EXPECT_TRUE(resolver->AddRuleFromString("map *.com baz")); |
82 | 83 |
83 // Add an exclusion for "*.google.com". | 84 // Add an exclusion for "*.google.com". |
84 EXPECT_TRUE(resolver->AddRuleFromString("EXCLUDE *.google.com")); | 85 EXPECT_TRUE(resolver->AddRuleFromString("EXCLUDE *.google.com")); |
85 | 86 |
86 // Try resolving "www.google.com". Should not be remapped due to exclusion). | 87 // Try resolving "www.google.com". Should not be remapped due to exclusion). |
87 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), | 88 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), |
88 &address_list, NULL, NULL, NULL); | 89 &address_list, NULL, NULL, BoundNetLog()); |
89 EXPECT_EQ(OK, rv); | 90 EXPECT_EQ(OK, rv); |
90 EXPECT_EQ("192.168.1.3", NetAddressToString(address_list.head())); | 91 EXPECT_EQ("192.168.1.3", NetAddressToString(address_list.head())); |
91 EXPECT_EQ(80, address_list.GetPort()); | 92 EXPECT_EQ(80, address_list.GetPort()); |
92 | 93 |
93 // Try resolving "chrome.com:80". Should be remapped to "baz:80". | 94 // Try resolving "chrome.com:80". Should be remapped to "baz:80". |
94 rv = resolver->Resolve(HostResolver::RequestInfo("chrome.com", 80), | 95 rv = resolver->Resolve(HostResolver::RequestInfo("chrome.com", 80), |
95 &address_list, NULL, NULL, NULL); | 96 &address_list, NULL, NULL, BoundNetLog()); |
96 EXPECT_EQ(OK, rv); | 97 EXPECT_EQ(OK, rv); |
97 EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head())); | 98 EXPECT_EQ("192.168.1.5", NetAddressToString(address_list.head())); |
98 EXPECT_EQ(80, address_list.GetPort()); | 99 EXPECT_EQ(80, address_list.GetPort()); |
99 } | 100 } |
100 | 101 |
101 TEST(MappedHostResolverTest, SetRulesFromString) { | 102 TEST(MappedHostResolverTest, SetRulesFromString) { |
102 // Create a mock host resolver, with specific hostname to IP mappings. | 103 // Create a mock host resolver, with specific hostname to IP mappings. |
103 scoped_refptr<MockHostResolver> resolver_impl = new MockHostResolver(); | 104 scoped_refptr<MockHostResolver> resolver_impl = new MockHostResolver(); |
104 resolver_impl->rules()->AddRule("baz", "192.168.1.7"); | 105 resolver_impl->rules()->AddRule("baz", "192.168.1.7"); |
105 resolver_impl->rules()->AddRule("bar", "192.168.1.9"); | 106 resolver_impl->rules()->AddRule("bar", "192.168.1.9"); |
106 | 107 |
107 // Create a remapped resolver that uses |resolver_impl|. | 108 // Create a remapped resolver that uses |resolver_impl|. |
108 scoped_refptr<MappedHostResolver> resolver = | 109 scoped_refptr<MappedHostResolver> resolver = |
109 new MappedHostResolver(resolver_impl); | 110 new MappedHostResolver(resolver_impl); |
110 | 111 |
111 int rv; | 112 int rv; |
112 AddressList address_list; | 113 AddressList address_list; |
113 | 114 |
114 // Remap "*.com" to "baz", and *.net to "bar:60". | 115 // Remap "*.com" to "baz", and *.net to "bar:60". |
115 resolver->SetRulesFromString("map *.com baz , map *.net bar:60"); | 116 resolver->SetRulesFromString("map *.com baz , map *.net bar:60"); |
116 | 117 |
117 // Try resolving "www.google.com". Should be remapped to "baz". | 118 // Try resolving "www.google.com". Should be remapped to "baz". |
118 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), | 119 rv = resolver->Resolve(HostResolver::RequestInfo("www.google.com", 80), |
119 &address_list, NULL, NULL, NULL); | 120 &address_list, NULL, NULL, BoundNetLog()); |
120 EXPECT_EQ(OK, rv); | 121 EXPECT_EQ(OK, rv); |
121 EXPECT_EQ("192.168.1.7", NetAddressToString(address_list.head())); | 122 EXPECT_EQ("192.168.1.7", NetAddressToString(address_list.head())); |
122 EXPECT_EQ(80, address_list.GetPort()); | 123 EXPECT_EQ(80, address_list.GetPort()); |
123 | 124 |
124 // Try resolving "chrome.net:80". Should be remapped to "bar:60". | 125 // Try resolving "chrome.net:80". Should be remapped to "bar:60". |
125 rv = resolver->Resolve(HostResolver::RequestInfo("chrome.net", 80), | 126 rv = resolver->Resolve(HostResolver::RequestInfo("chrome.net", 80), |
126 &address_list, NULL, NULL, NULL); | 127 &address_list, NULL, NULL, BoundNetLog()); |
127 EXPECT_EQ(OK, rv); | 128 EXPECT_EQ(OK, rv); |
128 EXPECT_EQ("192.168.1.9", NetAddressToString(address_list.head())); | 129 EXPECT_EQ("192.168.1.9", NetAddressToString(address_list.head())); |
129 EXPECT_EQ(60, address_list.GetPort()); | 130 EXPECT_EQ(60, address_list.GetPort()); |
130 } | 131 } |
131 | 132 |
132 // Parsing bad rules should silently discard the rule (and never crash). | 133 // Parsing bad rules should silently discard the rule (and never crash). |
133 TEST(MappedHostResolverTest, ParseInvalidRules) { | 134 TEST(MappedHostResolverTest, ParseInvalidRules) { |
134 scoped_refptr<MappedHostResolver> resolver = new MappedHostResolver(NULL); | 135 scoped_refptr<MappedHostResolver> resolver = new MappedHostResolver(NULL); |
135 | 136 |
136 EXPECT_FALSE(resolver->AddRuleFromString("xyz")); | 137 EXPECT_FALSE(resolver->AddRuleFromString("xyz")); |
137 EXPECT_FALSE(resolver->AddRuleFromString("")); | 138 EXPECT_FALSE(resolver->AddRuleFromString("")); |
138 EXPECT_FALSE(resolver->AddRuleFromString(" ")); | 139 EXPECT_FALSE(resolver->AddRuleFromString(" ")); |
139 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE")); | 140 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE")); |
140 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE foo bar")); | 141 EXPECT_FALSE(resolver->AddRuleFromString("EXCLUDE foo bar")); |
141 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE")); | 142 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE")); |
142 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x")); | 143 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x")); |
143 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x :10")); | 144 EXPECT_FALSE(resolver->AddRuleFromString("INCLUDE x :10")); |
144 } | 145 } |
145 | 146 |
146 } // namespace | 147 } // namespace |
147 | 148 |
148 } // namespace net | 149 } // namespace net |
OLD | NEW |