OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/proxy/proxy_resolver_v8_tracing.h" | 5 #include "net/proxy/proxy_resolver_v8_tracing.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/path_service.h" | 10 #include "base/path_service.h" |
12 #include "base/stl_util.h" | 11 #include "base/run_loop.h" |
13 #include "base/strings/string_util.h" | |
14 #include "base/strings/stringprintf.h" | |
15 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
16 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
17 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
18 #include "base/values.h" | 15 #include "base/values.h" |
19 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
20 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
21 #include "net/dns/host_cache.h" | 18 #include "net/dns/host_cache.h" |
22 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
23 #include "net/log/net_log.h" | 20 #include "net/log/net_log.h" |
24 #include "net/log/test_net_log.h" | |
25 #include "net/log/test_net_log_entry.h" | |
26 #include "net/log/test_net_log_util.h" | |
27 #include "net/proxy/proxy_info.h" | 21 #include "net/proxy/proxy_info.h" |
28 #include "net/proxy/proxy_resolver_error_observer.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "url/gurl.h" | 23 #include "url/gurl.h" |
31 | 24 |
32 namespace net { | 25 namespace net { |
33 | 26 |
34 namespace { | 27 namespace { |
35 | 28 |
36 class ProxyResolverV8TracingTest : public testing::Test { | 29 class ProxyResolverV8TracingTest : public testing::Test { |
37 public: | 30 public: |
38 void TearDown() override { | 31 void TearDown() override { |
39 // Drain any pending messages, which may be left over from cancellation. | 32 // Drain any pending messages, which may be left over from cancellation. |
40 // This way they get reliably run as part of the current test, rather than | 33 // This way they get reliably run as part of the current test, rather than |
41 // spilling into the next test's execution. | 34 // spilling into the next test's execution. |
42 base::MessageLoop::current()->RunUntilIdle(); | 35 base::RunLoop().RunUntilIdle(); |
43 } | 36 } |
44 }; | 37 }; |
45 | 38 |
46 scoped_refptr<ProxyResolverScriptData> LoadScriptData(const char* filename) { | 39 scoped_refptr<ProxyResolverScriptData> LoadScriptData(const char* filename) { |
47 base::FilePath path; | 40 base::FilePath path; |
48 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 41 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
49 path = path.AppendASCII("net"); | 42 path = path.AppendASCII("net"); |
50 path = path.AppendASCII("data"); | 43 path = path.AppendASCII("data"); |
51 path = path.AppendASCII("proxy_resolver_v8_tracing_unittest"); | 44 path = path.AppendASCII("proxy_resolver_v8_tracing_unittest"); |
52 path = path.AppendASCII(filename); | 45 path = path.AppendASCII(filename); |
53 | 46 |
54 // Try to read the file from disk. | 47 // Try to read the file from disk. |
55 std::string file_contents; | 48 std::string file_contents; |
56 bool ok = base::ReadFileToString(path, &file_contents); | 49 bool ok = base::ReadFileToString(path, &file_contents); |
57 | 50 |
58 // If we can't load the file from disk, something is misconfigured. | 51 // If we can't load the file from disk, something is misconfigured. |
59 EXPECT_TRUE(ok) << "Failed to read file: " << path.value(); | 52 EXPECT_TRUE(ok) << "Failed to read file: " << path.value(); |
60 | 53 |
61 // Load the PAC script into the ProxyResolver. | 54 // Load the PAC script into the ProxyResolver. |
62 return ProxyResolverScriptData::FromUTF8(file_contents); | 55 return ProxyResolverScriptData::FromUTF8(file_contents); |
63 } | 56 } |
64 | 57 |
65 scoped_ptr<ProxyResolverErrorObserver> ReturnErrorObserver( | 58 class MockBindings { |
66 scoped_ptr<ProxyResolverErrorObserver> error_observer) { | 59 public: |
67 return error_observer; | 60 explicit MockBindings(HostResolver* host_resolver) |
68 } | 61 : host_resolver_(host_resolver), event_(true, false) {} |
69 | 62 |
70 scoped_ptr<ProxyResolver> CreateResolver( | 63 void Alert(const base::string16& message) { |
71 NetLog* net_log, | 64 base::AutoLock l(lock_); |
72 HostResolver* host_resolver, | 65 alerts_.push_back(base::UTF16ToASCII(message)); |
73 scoped_ptr<ProxyResolverErrorObserver> error_observer, | 66 } |
| 67 void OnError(int line_number, const base::string16& error) { |
| 68 base::AutoLock l(lock_); |
| 69 errors_.push_back(std::make_pair(line_number, base::UTF16ToASCII(error))); |
| 70 event_.Signal(); |
| 71 } |
| 72 |
| 73 HostResolver* host_resolver() { return host_resolver_; } |
| 74 |
| 75 std::vector<std::string> GetAlerts() { |
| 76 base::AutoLock l(lock_); |
| 77 return alerts_; |
| 78 } |
| 79 |
| 80 std::vector<std::pair<int, std::string>> GetErrors() { |
| 81 base::AutoLock l(lock_); |
| 82 return errors_; |
| 83 } |
| 84 |
| 85 void WaitForError() { event_.Wait(); } |
| 86 |
| 87 scoped_ptr<ProxyResolverV8Tracing::Bindings> CreateBindings() { |
| 88 return make_scoped_ptr(new ForwardingBindings(this)); |
| 89 } |
| 90 |
| 91 private: |
| 92 class ForwardingBindings : public ProxyResolverV8Tracing::Bindings { |
| 93 public: |
| 94 ForwardingBindings(MockBindings* bindings) : bindings_(bindings) {} |
| 95 |
| 96 // ProxyResolverV8Tracing::Bindings overrides. |
| 97 void Alert(const base::string16& message) override { |
| 98 bindings_->Alert(message); |
| 99 } |
| 100 |
| 101 void OnError(int line_number, const base::string16& error) override { |
| 102 bindings_->OnError(line_number, error); |
| 103 } |
| 104 |
| 105 BoundNetLog GetBoundNetLog() override { return BoundNetLog(); } |
| 106 |
| 107 HostResolver* GetHostResolver() override { |
| 108 return bindings_->host_resolver(); |
| 109 } |
| 110 |
| 111 private: |
| 112 MockBindings* bindings_; |
| 113 }; |
| 114 |
| 115 base::Lock lock_; |
| 116 std::vector<std::string> alerts_; |
| 117 std::vector<std::pair<int, std::string>> errors_; |
| 118 HostResolver* const host_resolver_; |
| 119 |
| 120 base::WaitableEvent event_; |
| 121 }; |
| 122 |
| 123 scoped_ptr<ProxyResolverV8Tracing> CreateResolver( |
| 124 scoped_ptr<ProxyResolverV8Tracing::Bindings> bindings, |
74 const char* filename) { | 125 const char* filename) { |
75 scoped_ptr<ProxyResolver> resolver; | 126 scoped_ptr<ProxyResolverV8Tracing> resolver; |
76 ProxyResolverFactoryV8Tracing factory( | 127 scoped_ptr<ProxyResolverV8TracingFactory> factory( |
77 host_resolver, net_log, ProxyResolver::LoadStateChangedCallback(), | 128 ProxyResolverV8TracingFactory::Create()); |
78 base::Bind(&ReturnErrorObserver, base::Passed(&error_observer))); | |
79 TestCompletionCallback callback; | 129 TestCompletionCallback callback; |
80 scoped_ptr<ProxyResolverFactory::Request> request; | 130 scoped_ptr<ProxyResolverFactory::Request> request; |
81 int rv = factory.CreateProxyResolver(LoadScriptData(filename), &resolver, | 131 factory->CreateProxyResolverV8Tracing(LoadScriptData(filename), |
82 callback.callback(), &request); | 132 bindings.Pass(), &resolver, |
83 EXPECT_EQ(ERR_IO_PENDING, rv); | 133 callback.callback(), &request); |
84 EXPECT_EQ(OK, callback.WaitForResult()); | 134 EXPECT_EQ(OK, callback.WaitForResult()); |
85 EXPECT_TRUE(resolver); | 135 EXPECT_TRUE(resolver); |
86 return resolver.Pass(); | 136 return resolver.Pass(); |
87 } | 137 } |
88 | 138 |
89 class MockErrorObserver : public ProxyResolverErrorObserver { | 139 TEST_F(ProxyResolverV8TracingTest, Simple) { |
90 public: | 140 MockCachingHostResolver host_resolver; |
91 MockErrorObserver() : event_(true, false) {} | 141 MockBindings mock_bindings(&host_resolver); |
92 | 142 |
93 void OnPACScriptError(int line_number, const base::string16& error) override { | 143 scoped_ptr<ProxyResolverV8Tracing> resolver = |
94 { | 144 CreateResolver(mock_bindings.CreateBindings(), "simple.js"); |
95 base::AutoLock l(lock_); | |
96 output += base::StringPrintf("Error: line %d: %s\n", line_number, | |
97 base::UTF16ToASCII(error).c_str()); | |
98 } | |
99 event_.Signal(); | |
100 } | |
101 | |
102 std::string GetOutput() { | |
103 base::AutoLock l(lock_); | |
104 return output; | |
105 } | |
106 | |
107 void WaitForOutput() { | |
108 event_.Wait(); | |
109 } | |
110 | |
111 private: | |
112 base::Lock lock_; | |
113 std::string output; | |
114 | |
115 base::WaitableEvent event_; | |
116 }; | |
117 | |
118 TEST_F(ProxyResolverV8TracingTest, Simple) { | |
119 TestNetLog log; | |
120 BoundTestNetLog request_log; | |
121 MockCachingHostResolver host_resolver; | |
122 MockErrorObserver* error_observer = new MockErrorObserver; | |
123 | |
124 scoped_ptr<ProxyResolver> resolver = CreateResolver( | |
125 &log, &host_resolver, make_scoped_ptr(error_observer), "simple.js"); | |
126 | 145 |
127 TestCompletionCallback callback; | 146 TestCompletionCallback callback; |
128 ProxyInfo proxy_info; | 147 ProxyInfo proxy_info; |
129 | 148 |
130 int rv = | 149 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
131 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 150 callback.callback(), NULL, |
132 callback.callback(), NULL, request_log.bound()); | 151 mock_bindings.CreateBindings()); |
133 | 152 |
134 EXPECT_EQ(ERR_IO_PENDING, rv); | |
135 EXPECT_EQ(OK, callback.WaitForResult()); | 153 EXPECT_EQ(OK, callback.WaitForResult()); |
136 | 154 |
137 EXPECT_EQ("foo:99", proxy_info.proxy_server().ToURI()); | 155 EXPECT_EQ("foo:99", proxy_info.proxy_server().ToURI()); |
138 | 156 |
139 EXPECT_EQ(0u, host_resolver.num_resolve()); | 157 EXPECT_EQ(0u, host_resolver.num_resolve()); |
140 | 158 |
141 // There were no errors. | 159 // There were no alerts or errors. |
142 EXPECT_EQ("", error_observer->GetOutput()); | 160 EXPECT_TRUE(mock_bindings.GetAlerts().empty()); |
143 | 161 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
144 // Check the NetLogs -- nothing was logged. | |
145 EXPECT_EQ(0u, log.GetSize()); | |
146 EXPECT_EQ(0u, request_log.GetSize()); | |
147 } | 162 } |
148 | 163 |
149 TEST_F(ProxyResolverV8TracingTest, JavascriptError) { | 164 TEST_F(ProxyResolverV8TracingTest, JavascriptError) { |
150 TestNetLog log; | |
151 BoundTestNetLog request_log; | |
152 MockCachingHostResolver host_resolver; | 165 MockCachingHostResolver host_resolver; |
153 MockErrorObserver* error_observer = new MockErrorObserver; | 166 MockBindings mock_bindings(&host_resolver); |
154 | 167 |
155 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 168 scoped_ptr<ProxyResolverV8Tracing> resolver = |
156 &log, &host_resolver, make_scoped_ptr(error_observer), "error.js"); | 169 CreateResolver(mock_bindings.CreateBindings(), "error.js"); |
157 | 170 |
158 TestCompletionCallback callback; | 171 TestCompletionCallback callback; |
159 ProxyInfo proxy_info; | 172 ProxyInfo proxy_info; |
160 | 173 |
161 int rv = | 174 resolver->GetProxyForURL(GURL("http://throw-an-error/"), &proxy_info, |
162 resolver->GetProxyForURL(GURL("http://throw-an-error/"), &proxy_info, | 175 callback.callback(), NULL, |
163 callback.callback(), NULL, request_log.bound()); | 176 mock_bindings.CreateBindings()); |
164 | 177 |
165 EXPECT_EQ(ERR_IO_PENDING, rv); | |
166 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult()); | 178 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult()); |
167 | 179 |
168 EXPECT_EQ(0u, host_resolver.num_resolve()); | 180 EXPECT_EQ(0u, host_resolver.num_resolve()); |
169 | 181 |
170 EXPECT_EQ("Error: line 5: Uncaught TypeError: Cannot read property 'split' " | 182 // Check the output -- there was 1 alert and 1 javascript error. |
171 "of null\n", error_observer->GetOutput()); | 183 ASSERT_EQ(1u, mock_bindings.GetAlerts().size()); |
172 | 184 EXPECT_EQ("Prepare to DIE!", mock_bindings.GetAlerts()[0]); |
173 // Check the NetLogs -- there was 1 alert and 1 javascript error, and they | 185 ASSERT_EQ(1u, mock_bindings.GetErrors().size()); |
174 // were output to both the global log, and per-request log. | 186 EXPECT_EQ(5, mock_bindings.GetErrors()[0].first); |
175 TestNetLogEntry::List entries_list[2]; | 187 EXPECT_EQ("Uncaught TypeError: Cannot read property 'split' of null", |
176 log.GetEntries(&entries_list[0]); | 188 mock_bindings.GetErrors()[0].second); |
177 request_log.GetEntries(&entries_list[1]); | |
178 | |
179 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) { | |
180 const TestNetLogEntry::List& entries = entries_list[list_i]; | |
181 EXPECT_EQ(2u, entries.size()); | |
182 EXPECT_TRUE( | |
183 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
184 NetLog::PHASE_NONE)); | |
185 EXPECT_TRUE( | |
186 LogContainsEvent(entries, 1, NetLog::TYPE_PAC_JAVASCRIPT_ERROR, | |
187 NetLog::PHASE_NONE)); | |
188 | |
189 EXPECT_EQ("{\"message\":\"Prepare to DIE!\"}", entries[0].GetParamsJson()); | |
190 EXPECT_EQ("{\"line_number\":5,\"message\":\"Uncaught TypeError: Cannot " | |
191 "read property 'split' of null\"}", entries[1].GetParamsJson()); | |
192 } | |
193 } | 189 } |
194 | 190 |
195 TEST_F(ProxyResolverV8TracingTest, TooManyAlerts) { | 191 TEST_F(ProxyResolverV8TracingTest, TooManyAlerts) { |
196 TestNetLog log; | |
197 BoundTestNetLog request_log; | |
198 MockCachingHostResolver host_resolver; | 192 MockCachingHostResolver host_resolver; |
199 MockErrorObserver* error_observer = new MockErrorObserver; | 193 MockBindings mock_bindings(&host_resolver); |
200 | 194 |
201 scoped_ptr<ProxyResolver> resolver = | 195 scoped_ptr<ProxyResolverV8Tracing> resolver = |
202 CreateResolver(&log, &host_resolver, make_scoped_ptr(error_observer), | 196 CreateResolver(mock_bindings.CreateBindings(), "too_many_alerts.js"); |
203 "too_many_alerts.js"); | |
204 | 197 |
205 TestCompletionCallback callback; | 198 TestCompletionCallback callback; |
206 ProxyInfo proxy_info; | 199 ProxyInfo proxy_info; |
207 | 200 |
208 int rv = | 201 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
209 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 202 callback.callback(), NULL, |
210 callback.callback(), NULL, request_log.bound()); | 203 mock_bindings.CreateBindings()); |
211 | 204 |
212 EXPECT_EQ(ERR_IO_PENDING, rv); | |
213 EXPECT_EQ(OK, callback.WaitForResult()); | 205 EXPECT_EQ(OK, callback.WaitForResult()); |
214 | 206 |
215 // Iteration1 does a DNS resolve | 207 // Iteration1 does a DNS resolve |
216 // Iteration2 exceeds the alert buffer | 208 // Iteration2 exceeds the alert buffer |
217 // Iteration3 runs in blocking mode and completes | 209 // Iteration3 runs in blocking mode and completes |
218 EXPECT_EQ("foo:3", proxy_info.proxy_server().ToURI()); | 210 EXPECT_EQ("foo:3", proxy_info.proxy_server().ToURI()); |
219 | 211 |
220 EXPECT_EQ(1u, host_resolver.num_resolve()); | 212 EXPECT_EQ(1u, host_resolver.num_resolve()); |
221 | 213 |
222 // No errors. | 214 // No errors. |
223 EXPECT_EQ("", error_observer->GetOutput()); | 215 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
224 | 216 |
225 // Check the NetLogs -- the script generated 50 alerts, which were mirrored | 217 // Check the alerts -- the script generated 50 alerts. |
226 // to both the global and per-request logs. | 218 std::vector<std::string> alerts = mock_bindings.GetAlerts(); |
227 TestNetLogEntry::List entries_list[2]; | 219 ASSERT_EQ(50u, alerts.size()); |
228 log.GetEntries(&entries_list[0]); | 220 for (size_t i = 0; i < alerts.size(); i++) { |
229 request_log.GetEntries(&entries_list[1]); | 221 EXPECT_EQ("Gee, all these alerts are silly!", alerts[i]); |
230 | |
231 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) { | |
232 const TestNetLogEntry::List& entries = entries_list[list_i]; | |
233 EXPECT_EQ(50u, entries.size()); | |
234 for (size_t i = 0; i < entries.size(); ++i) { | |
235 ASSERT_TRUE( | |
236 LogContainsEvent(entries, i, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
237 NetLog::PHASE_NONE)); | |
238 } | |
239 } | 222 } |
240 } | 223 } |
241 | 224 |
242 // Verify that buffered alerts cannot grow unboundedly, even when the message is | 225 // Verify that buffered alerts cannot grow unboundedly, even when the message is |
243 // empty string. | 226 // empty string. |
244 TEST_F(ProxyResolverV8TracingTest, TooManyEmptyAlerts) { | 227 TEST_F(ProxyResolverV8TracingTest, TooManyEmptyAlerts) { |
245 TestNetLog log; | |
246 BoundTestNetLog request_log; | |
247 MockCachingHostResolver host_resolver; | 228 MockCachingHostResolver host_resolver; |
248 MockErrorObserver* error_observer = new MockErrorObserver; | 229 MockBindings mock_bindings(&host_resolver); |
249 | 230 |
250 scoped_ptr<ProxyResolver> resolver = | 231 scoped_ptr<ProxyResolverV8Tracing> resolver = CreateResolver( |
251 CreateResolver(&log, &host_resolver, make_scoped_ptr(error_observer), | 232 mock_bindings.CreateBindings(), "too_many_empty_alerts.js"); |
252 "too_many_empty_alerts.js"); | |
253 | 233 |
254 TestCompletionCallback callback; | 234 TestCompletionCallback callback; |
255 ProxyInfo proxy_info; | 235 ProxyInfo proxy_info; |
256 | 236 |
257 int rv = | 237 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
258 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 238 callback.callback(), NULL, |
259 callback.callback(), NULL, request_log.bound()); | 239 mock_bindings.CreateBindings()); |
260 | 240 |
261 EXPECT_EQ(ERR_IO_PENDING, rv); | |
262 EXPECT_EQ(OK, callback.WaitForResult()); | 241 EXPECT_EQ(OK, callback.WaitForResult()); |
263 | 242 |
264 EXPECT_EQ("foo:3", proxy_info.proxy_server().ToURI()); | 243 EXPECT_EQ("foo:3", proxy_info.proxy_server().ToURI()); |
265 | 244 |
266 EXPECT_EQ(1u, host_resolver.num_resolve()); | 245 EXPECT_EQ(1u, host_resolver.num_resolve()); |
267 | 246 |
268 // No errors. | 247 // No errors. |
269 EXPECT_EQ("", error_observer->GetOutput()); | 248 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
270 | 249 |
271 // Check the NetLogs -- the script generated 50 alerts, which were mirrored | 250 // Check the alerts -- the script generated 1000 alerts. |
272 // to both the global and per-request logs. | 251 std::vector<std::string> alerts = mock_bindings.GetAlerts(); |
273 TestNetLogEntry::List entries_list[2]; | 252 ASSERT_EQ(1000u, alerts.size()); |
274 log.GetEntries(&entries_list[0]); | 253 for (size_t i = 0; i < alerts.size(); i++) { |
275 request_log.GetEntries(&entries_list[1]); | 254 EXPECT_EQ("", alerts[i]); |
276 | |
277 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) { | |
278 const TestNetLogEntry::List& entries = entries_list[list_i]; | |
279 EXPECT_EQ(1000u, entries.size()); | |
280 for (size_t i = 0; i < entries.size(); ++i) { | |
281 ASSERT_TRUE( | |
282 LogContainsEvent(entries, i, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
283 NetLog::PHASE_NONE)); | |
284 } | |
285 } | 255 } |
286 } | 256 } |
287 | 257 |
288 // This test runs a PAC script that issues a sequence of DNS resolves. The test | 258 // This test runs a PAC script that issues a sequence of DNS resolves. The test |
289 // verifies the final result, and that the underlying DNS resolver received | 259 // verifies the final result, and that the underlying DNS resolver received |
290 // the correct set of queries. | 260 // the correct set of queries. |
291 TEST_F(ProxyResolverV8TracingTest, Dns) { | 261 TEST_F(ProxyResolverV8TracingTest, Dns) { |
292 TestNetLog log; | |
293 BoundTestNetLog request_log; | |
294 MockCachingHostResolver host_resolver; | 262 MockCachingHostResolver host_resolver; |
295 MockErrorObserver* error_observer = new MockErrorObserver; | 263 MockBindings mock_bindings(&host_resolver); |
296 | 264 |
297 host_resolver.rules()->AddRuleForAddressFamily( | 265 host_resolver.rules()->AddRuleForAddressFamily( |
298 "host1", ADDRESS_FAMILY_IPV4, "166.155.144.44"); | 266 "host1", ADDRESS_FAMILY_IPV4, "166.155.144.44"); |
299 host_resolver.rules() | 267 host_resolver.rules() |
300 ->AddIPLiteralRule("host1", "::1,192.168.1.1", std::string()); | 268 ->AddIPLiteralRule("host1", "::1,192.168.1.1", std::string()); |
301 host_resolver.rules()->AddSimulatedFailure("host2"); | 269 host_resolver.rules()->AddSimulatedFailure("host2"); |
302 host_resolver.rules()->AddRule("host3", "166.155.144.33"); | 270 host_resolver.rules()->AddRule("host3", "166.155.144.33"); |
303 host_resolver.rules()->AddRule("host5", "166.155.144.55"); | 271 host_resolver.rules()->AddRule("host5", "166.155.144.55"); |
304 host_resolver.rules()->AddSimulatedFailure("host6"); | 272 host_resolver.rules()->AddSimulatedFailure("host6"); |
305 host_resolver.rules()->AddRuleForAddressFamily( | 273 host_resolver.rules()->AddRuleForAddressFamily( |
306 "*", ADDRESS_FAMILY_IPV4, "122.133.144.155"); | 274 "*", ADDRESS_FAMILY_IPV4, "122.133.144.155"); |
307 host_resolver.rules()->AddRule("*", "133.122.100.200"); | 275 host_resolver.rules()->AddRule("*", "133.122.100.200"); |
308 | 276 |
309 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 277 scoped_ptr<ProxyResolverV8Tracing> resolver = |
310 &log, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); | 278 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
311 | 279 |
312 TestCompletionCallback callback; | 280 TestCompletionCallback callback; |
313 ProxyInfo proxy_info; | 281 ProxyInfo proxy_info; |
314 | 282 |
315 int rv = | 283 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
316 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 284 callback.callback(), NULL, |
317 callback.callback(), NULL, request_log.bound()); | 285 mock_bindings.CreateBindings()); |
318 | 286 |
319 EXPECT_EQ(ERR_IO_PENDING, rv); | |
320 EXPECT_EQ(OK, callback.WaitForResult()); | 287 EXPECT_EQ(OK, callback.WaitForResult()); |
321 | 288 |
322 // The test does 13 DNS resolution, however only 7 of them are unique. | 289 // The test does 13 DNS resolution, however only 7 of them are unique. |
323 EXPECT_EQ(7u, host_resolver.num_resolve()); | 290 EXPECT_EQ(7u, host_resolver.num_resolve()); |
324 | 291 |
325 const char* kExpectedResult = | 292 const char* kExpectedResult = |
326 "122.133.144.155-" // myIpAddress() | 293 "122.133.144.155-" // myIpAddress() |
327 "null-" // dnsResolve('') | 294 "null-" // dnsResolve('') |
328 "__1_192.168.1.1-" // dnsResolveEx('host1') | 295 "__1_192.168.1.1-" // dnsResolveEx('host1') |
329 "null-" // dnsResolve('host2') | 296 "null-" // dnsResolve('host2') |
330 "166.155.144.33-" // dnsResolve('host3') | 297 "166.155.144.33-" // dnsResolve('host3') |
331 "122.133.144.155-" // myIpAddress() | 298 "122.133.144.155-" // myIpAddress() |
332 "166.155.144.33-" // dnsResolve('host3') | 299 "166.155.144.33-" // dnsResolve('host3') |
333 "__1_192.168.1.1-" // dnsResolveEx('host1') | 300 "__1_192.168.1.1-" // dnsResolveEx('host1') |
334 "122.133.144.155-" // myIpAddress() | 301 "122.133.144.155-" // myIpAddress() |
335 "null-" // dnsResolve('host2') | 302 "null-" // dnsResolve('host2') |
336 "-" // dnsResolveEx('host6') | 303 "-" // dnsResolveEx('host6') |
337 "133.122.100.200-" // myIpAddressEx() | 304 "133.122.100.200-" // myIpAddressEx() |
338 "166.155.144.44" // dnsResolve('host1') | 305 "166.155.144.44" // dnsResolve('host1') |
339 ":99"; | 306 ":99"; |
340 | 307 |
341 EXPECT_EQ(kExpectedResult, proxy_info.proxy_server().ToURI()); | 308 EXPECT_EQ(kExpectedResult, proxy_info.proxy_server().ToURI()); |
342 | 309 |
343 // No errors. | 310 // No errors. |
344 EXPECT_EQ("", error_observer->GetOutput()); | 311 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
345 | 312 |
346 // Check the NetLogs -- the script generated 1 alert, mirrored to both | 313 // The script generated 1 alert. |
347 // the per-request and global logs. | 314 ASSERT_EQ(1u, mock_bindings.GetAlerts().size()); |
348 TestNetLogEntry::List entries_list[2]; | 315 EXPECT_EQ("iteration: 7", mock_bindings.GetAlerts()[0]); |
349 log.GetEntries(&entries_list[0]); | |
350 request_log.GetEntries(&entries_list[1]); | |
351 | |
352 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) { | |
353 const TestNetLogEntry::List& entries = entries_list[list_i]; | |
354 EXPECT_EQ(1u, entries.size()); | |
355 EXPECT_TRUE( | |
356 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
357 NetLog::PHASE_NONE)); | |
358 EXPECT_EQ("{\"message\":\"iteration: 7\"}", entries[0].GetParamsJson()); | |
359 } | |
360 } | 316 } |
361 | 317 |
362 // This test runs a PAC script that does "myIpAddress()" followed by | 318 // This test runs a PAC script that does "myIpAddress()" followed by |
363 // "dnsResolve()". This requires 2 restarts. However once the HostResolver's | 319 // "dnsResolve()". This requires 2 restarts. However once the HostResolver's |
364 // cache is warmed, subsequent calls should take 0 restarts. | 320 // cache is warmed, subsequent calls should take 0 restarts. |
365 TEST_F(ProxyResolverV8TracingTest, DnsChecksCache) { | 321 TEST_F(ProxyResolverV8TracingTest, DnsChecksCache) { |
366 TestNetLog log; | |
367 BoundTestNetLog request_log; | |
368 MockCachingHostResolver host_resolver; | 322 MockCachingHostResolver host_resolver; |
369 MockErrorObserver* error_observer = new MockErrorObserver; | 323 MockBindings mock_bindings(&host_resolver); |
370 | 324 |
371 host_resolver.rules()->AddRule("foopy", "166.155.144.11"); | 325 host_resolver.rules()->AddRule("foopy", "166.155.144.11"); |
372 host_resolver.rules()->AddRule("*", "122.133.144.155"); | 326 host_resolver.rules()->AddRule("*", "122.133.144.155"); |
373 | 327 |
374 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 328 scoped_ptr<ProxyResolverV8Tracing> resolver = |
375 &log, &host_resolver, make_scoped_ptr(error_observer), "simple_dns.js"); | 329 CreateResolver(mock_bindings.CreateBindings(), "simple_dns.js"); |
376 | 330 |
377 TestCompletionCallback callback1; | 331 TestCompletionCallback callback1; |
378 TestCompletionCallback callback2; | 332 TestCompletionCallback callback2; |
379 ProxyInfo proxy_info; | 333 ProxyInfo proxy_info; |
380 | 334 |
381 int rv = | 335 resolver->GetProxyForURL(GURL("http://foopy/req1"), &proxy_info, |
382 resolver->GetProxyForURL(GURL("http://foopy/req1"), &proxy_info, | 336 callback1.callback(), NULL, |
383 callback1.callback(), NULL, request_log.bound()); | 337 mock_bindings.CreateBindings()); |
384 | 338 |
385 EXPECT_EQ(ERR_IO_PENDING, rv); | |
386 EXPECT_EQ(OK, callback1.WaitForResult()); | 339 EXPECT_EQ(OK, callback1.WaitForResult()); |
387 | 340 |
388 // The test does 2 DNS resolutions. | 341 // The test does 2 DNS resolutions. |
389 EXPECT_EQ(2u, host_resolver.num_resolve()); | 342 EXPECT_EQ(2u, host_resolver.num_resolve()); |
390 | 343 |
391 // The first request took 2 restarts, hence on g_iteration=3. | 344 // The first request took 2 restarts, hence on g_iteration=3. |
392 EXPECT_EQ("166.155.144.11:3", proxy_info.proxy_server().ToURI()); | 345 EXPECT_EQ("166.155.144.11:3", proxy_info.proxy_server().ToURI()); |
393 | 346 |
394 rv = | 347 resolver->GetProxyForURL(GURL("http://foopy/req2"), &proxy_info, |
395 resolver->GetProxyForURL(GURL("http://foopy/req2"), &proxy_info, | 348 callback2.callback(), NULL, |
396 callback2.callback(), NULL, request_log.bound()); | 349 mock_bindings.CreateBindings()); |
397 | 350 |
398 EXPECT_EQ(ERR_IO_PENDING, rv); | |
399 EXPECT_EQ(OK, callback2.WaitForResult()); | 351 EXPECT_EQ(OK, callback2.WaitForResult()); |
400 | 352 |
401 EXPECT_EQ(4u, host_resolver.num_resolve()); | 353 EXPECT_EQ(4u, host_resolver.num_resolve()); |
402 | 354 |
403 // This time no restarts were required, so g_iteration incremented by 1. | 355 // This time no restarts were required, so g_iteration incremented by 1. |
404 EXPECT_EQ("166.155.144.11:4", proxy_info.proxy_server().ToURI()); | 356 EXPECT_EQ("166.155.144.11:4", proxy_info.proxy_server().ToURI()); |
405 | 357 |
406 // No errors. | 358 // There were no alerts or errors. |
407 EXPECT_EQ("", error_observer->GetOutput()); | 359 EXPECT_TRUE(mock_bindings.GetAlerts().empty()); |
408 | 360 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
409 EXPECT_EQ(0u, log.GetSize()); | |
410 EXPECT_EQ(0u, request_log.GetSize()); | |
411 } | 361 } |
412 | 362 |
413 // This test runs a weird PAC script that was designed to defeat the DNS tracing | 363 // This test runs a weird PAC script that was designed to defeat the DNS tracing |
414 // optimization. The proxy resolver should detect the inconsistency and | 364 // optimization. The proxy resolver should detect the inconsistency and |
415 // fall-back to synchronous mode execution. | 365 // fall-back to synchronous mode execution. |
416 TEST_F(ProxyResolverV8TracingTest, FallBackToSynchronous1) { | 366 TEST_F(ProxyResolverV8TracingTest, FallBackToSynchronous1) { |
417 TestNetLog log; | |
418 BoundTestNetLog request_log; | |
419 MockCachingHostResolver host_resolver; | 367 MockCachingHostResolver host_resolver; |
420 MockErrorObserver* error_observer = new MockErrorObserver; | 368 MockBindings mock_bindings(&host_resolver); |
421 | 369 |
422 host_resolver.rules()->AddRule("host1", "166.155.144.11"); | 370 host_resolver.rules()->AddRule("host1", "166.155.144.11"); |
423 host_resolver.rules()->AddRule("crazy4", "133.199.111.4"); | 371 host_resolver.rules()->AddRule("crazy4", "133.199.111.4"); |
424 host_resolver.rules()->AddRule("*", "122.133.144.155"); | 372 host_resolver.rules()->AddRule("*", "122.133.144.155"); |
425 | 373 |
426 scoped_ptr<ProxyResolver> resolver = | 374 scoped_ptr<ProxyResolverV8Tracing> resolver = |
427 CreateResolver(&log, &host_resolver, make_scoped_ptr(error_observer), | 375 CreateResolver(mock_bindings.CreateBindings(), "global_sideffects1.js"); |
428 "global_sideffects1.js"); | |
429 | 376 |
430 TestCompletionCallback callback; | 377 TestCompletionCallback callback; |
431 ProxyInfo proxy_info; | 378 ProxyInfo proxy_info; |
432 | 379 |
433 int rv = | 380 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
434 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 381 callback.callback(), NULL, |
435 callback.callback(), NULL, request_log.bound()); | 382 mock_bindings.CreateBindings()); |
436 EXPECT_EQ(ERR_IO_PENDING, rv); | |
437 EXPECT_EQ(OK, callback.WaitForResult()); | 383 EXPECT_EQ(OK, callback.WaitForResult()); |
438 | 384 |
439 // The script itself only does 2 DNS resolves per execution, however it | 385 // The script itself only does 2 DNS resolves per execution, however it |
440 // constructs the hostname using a global counter which changes on each | 386 // constructs the hostname using a global counter which changes on each |
441 // invocation. | 387 // invocation. |
442 EXPECT_EQ(3u, host_resolver.num_resolve()); | 388 EXPECT_EQ(3u, host_resolver.num_resolve()); |
443 | 389 |
444 EXPECT_EQ("166.155.144.11-133.199.111.4:100", | 390 EXPECT_EQ("166.155.144.11-133.199.111.4:100", |
445 proxy_info.proxy_server().ToURI()); | 391 proxy_info.proxy_server().ToURI()); |
446 | 392 |
447 // No errors. | 393 // No errors. |
448 EXPECT_EQ("", error_observer->GetOutput()); | 394 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
449 | 395 |
450 // Check the NetLogs -- the script generated 1 alert, mirrored to both | 396 ASSERT_EQ(1u, mock_bindings.GetAlerts().size()); |
451 // the per-request and global logs. | 397 EXPECT_EQ("iteration: 4", mock_bindings.GetAlerts()[0]); |
452 TestNetLogEntry::List entries_list[2]; | |
453 log.GetEntries(&entries_list[0]); | |
454 request_log.GetEntries(&entries_list[1]); | |
455 | |
456 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) { | |
457 const TestNetLogEntry::List& entries = entries_list[list_i]; | |
458 EXPECT_EQ(1u, entries.size()); | |
459 EXPECT_TRUE( | |
460 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
461 NetLog::PHASE_NONE)); | |
462 EXPECT_EQ("{\"message\":\"iteration: 4\"}", entries[0].GetParamsJson()); | |
463 } | |
464 } | 398 } |
465 | 399 |
466 // This test runs a weird PAC script that was designed to defeat the DNS tracing | 400 // This test runs a weird PAC script that was designed to defeat the DNS tracing |
467 // optimization. The proxy resolver should detect the inconsistency and | 401 // optimization. The proxy resolver should detect the inconsistency and |
468 // fall-back to synchronous mode execution. | 402 // fall-back to synchronous mode execution. |
469 TEST_F(ProxyResolverV8TracingTest, FallBackToSynchronous2) { | 403 TEST_F(ProxyResolverV8TracingTest, FallBackToSynchronous2) { |
470 TestNetLog log; | |
471 BoundTestNetLog request_log; | |
472 MockCachingHostResolver host_resolver; | 404 MockCachingHostResolver host_resolver; |
473 MockErrorObserver* error_observer = new MockErrorObserver; | 405 MockBindings mock_bindings(&host_resolver); |
474 | 406 |
475 host_resolver.rules()->AddRule("host1", "166.155.144.11"); | 407 host_resolver.rules()->AddRule("host1", "166.155.144.11"); |
476 host_resolver.rules()->AddRule("host2", "166.155.144.22"); | 408 host_resolver.rules()->AddRule("host2", "166.155.144.22"); |
477 host_resolver.rules()->AddRule("host3", "166.155.144.33"); | 409 host_resolver.rules()->AddRule("host3", "166.155.144.33"); |
478 host_resolver.rules()->AddRule("host4", "166.155.144.44"); | 410 host_resolver.rules()->AddRule("host4", "166.155.144.44"); |
479 host_resolver.rules()->AddRule("*", "122.133.144.155"); | 411 host_resolver.rules()->AddRule("*", "122.133.144.155"); |
480 | 412 |
481 scoped_ptr<ProxyResolver> resolver = | 413 scoped_ptr<ProxyResolverV8Tracing> resolver = |
482 CreateResolver(&log, &host_resolver, make_scoped_ptr(error_observer), | 414 CreateResolver(mock_bindings.CreateBindings(), "global_sideffects2.js"); |
483 "global_sideffects2.js"); | |
484 | 415 |
485 TestCompletionCallback callback; | 416 TestCompletionCallback callback; |
486 ProxyInfo proxy_info; | 417 ProxyInfo proxy_info; |
487 | 418 |
488 int rv = | 419 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
489 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 420 callback.callback(), NULL, |
490 callback.callback(), NULL, request_log.bound()); | 421 mock_bindings.CreateBindings()); |
491 EXPECT_EQ(ERR_IO_PENDING, rv); | |
492 EXPECT_EQ(OK, callback.WaitForResult()); | 422 EXPECT_EQ(OK, callback.WaitForResult()); |
493 | 423 |
494 EXPECT_EQ(3u, host_resolver.num_resolve()); | 424 EXPECT_EQ(3u, host_resolver.num_resolve()); |
495 | 425 |
496 EXPECT_EQ("166.155.144.44:100", proxy_info.proxy_server().ToURI()); | 426 EXPECT_EQ("166.155.144.44:100", proxy_info.proxy_server().ToURI()); |
497 | 427 |
498 // No errors. | 428 // There were no alerts or errors. |
499 EXPECT_EQ("", error_observer->GetOutput()); | 429 EXPECT_TRUE(mock_bindings.GetAlerts().empty()); |
500 | 430 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
501 // Check the NetLogs -- nothing was logged. | |
502 EXPECT_EQ(0u, log.GetSize()); | |
503 EXPECT_EQ(0u, request_log.GetSize()); | |
504 } | 431 } |
505 | 432 |
506 // This test runs a weird PAC script that yields a never ending sequence | 433 // This test runs a weird PAC script that yields a never ending sequence |
507 // of DNS resolves when restarting. Running it will hit the maximum | 434 // of DNS resolves when restarting. Running it will hit the maximum |
508 // DNS resolves per request limit (20) after which every DNS resolve will | 435 // DNS resolves per request limit (20) after which every DNS resolve will |
509 // fail. | 436 // fail. |
510 TEST_F(ProxyResolverV8TracingTest, InfiniteDNSSequence) { | 437 TEST_F(ProxyResolverV8TracingTest, InfiniteDNSSequence) { |
511 TestNetLog log; | |
512 BoundTestNetLog request_log; | |
513 MockCachingHostResolver host_resolver; | 438 MockCachingHostResolver host_resolver; |
514 MockErrorObserver* error_observer = new MockErrorObserver; | 439 MockBindings mock_bindings(&host_resolver); |
515 | 440 |
516 host_resolver.rules()->AddRule("host*", "166.155.144.11"); | 441 host_resolver.rules()->AddRule("host*", "166.155.144.11"); |
517 host_resolver.rules()->AddRule("*", "122.133.144.155"); | 442 host_resolver.rules()->AddRule("*", "122.133.144.155"); |
518 | 443 |
519 scoped_ptr<ProxyResolver> resolver = | 444 scoped_ptr<ProxyResolverV8Tracing> resolver = |
520 CreateResolver(&log, &host_resolver, make_scoped_ptr(error_observer), | 445 CreateResolver(mock_bindings.CreateBindings(), "global_sideffects3.js"); |
521 "global_sideffects3.js"); | |
522 | 446 |
523 TestCompletionCallback callback; | 447 TestCompletionCallback callback; |
524 ProxyInfo proxy_info; | 448 ProxyInfo proxy_info; |
525 | 449 |
526 int rv = | 450 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
527 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 451 callback.callback(), NULL, |
528 callback.callback(), NULL, request_log.bound()); | 452 mock_bindings.CreateBindings()); |
529 EXPECT_EQ(ERR_IO_PENDING, rv); | |
530 EXPECT_EQ(OK, callback.WaitForResult()); | 453 EXPECT_EQ(OK, callback.WaitForResult()); |
531 | 454 |
532 EXPECT_EQ(20u, host_resolver.num_resolve()); | 455 EXPECT_EQ(20u, host_resolver.num_resolve()); |
533 | 456 |
534 EXPECT_EQ( | 457 EXPECT_EQ( |
535 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" | 458 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" |
536 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" | 459 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" |
537 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" | 460 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" |
538 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" | 461 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" |
539 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" | 462 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-" |
540 "null:21", proxy_info.proxy_server().ToURI()); | 463 "null:21", proxy_info.proxy_server().ToURI()); |
541 | 464 |
542 // No errors. | 465 // No errors. |
543 EXPECT_EQ("", error_observer->GetOutput()); | 466 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
544 | 467 |
545 // Check the NetLogs -- 1 alert was logged. | 468 // 1 alert. |
546 EXPECT_EQ(1u, log.GetSize()); | 469 EXPECT_EQ(1u, mock_bindings.GetAlerts().size()); |
547 EXPECT_EQ(1u, request_log.GetSize()); | 470 EXPECT_EQ("iteration: 21", mock_bindings.GetAlerts()[0]); |
548 } | 471 } |
549 | 472 |
550 // This test runs a weird PAC script that yields a never ending sequence | 473 // This test runs a weird PAC script that yields a never ending sequence |
551 // of DNS resolves when restarting. Running it will hit the maximum | 474 // of DNS resolves when restarting. Running it will hit the maximum |
552 // DNS resolves per request limit (20) after which every DNS resolve will | 475 // DNS resolves per request limit (20) after which every DNS resolve will |
553 // fail. | 476 // fail. |
554 TEST_F(ProxyResolverV8TracingTest, InfiniteDNSSequence2) { | 477 TEST_F(ProxyResolverV8TracingTest, InfiniteDNSSequence2) { |
555 TestNetLog log; | |
556 BoundTestNetLog request_log; | |
557 MockCachingHostResolver host_resolver; | 478 MockCachingHostResolver host_resolver; |
558 MockErrorObserver* error_observer = new MockErrorObserver; | 479 MockBindings mock_bindings(&host_resolver); |
559 | 480 |
560 host_resolver.rules()->AddRule("host*", "166.155.144.11"); | 481 host_resolver.rules()->AddRule("host*", "166.155.144.11"); |
561 host_resolver.rules()->AddRule("*", "122.133.144.155"); | 482 host_resolver.rules()->AddRule("*", "122.133.144.155"); |
562 | 483 |
563 scoped_ptr<ProxyResolver> resolver = | 484 scoped_ptr<ProxyResolverV8Tracing> resolver = |
564 CreateResolver(&log, &host_resolver, make_scoped_ptr(error_observer), | 485 CreateResolver(mock_bindings.CreateBindings(), "global_sideffects4.js"); |
565 "global_sideffects4.js"); | |
566 | 486 |
567 TestCompletionCallback callback; | 487 TestCompletionCallback callback; |
568 ProxyInfo proxy_info; | 488 ProxyInfo proxy_info; |
569 | 489 |
570 int rv = | 490 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
571 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 491 callback.callback(), NULL, |
572 callback.callback(), NULL, request_log.bound()); | 492 mock_bindings.CreateBindings()); |
573 EXPECT_EQ(ERR_IO_PENDING, rv); | |
574 EXPECT_EQ(OK, callback.WaitForResult()); | 493 EXPECT_EQ(OK, callback.WaitForResult()); |
575 | 494 |
576 EXPECT_EQ(20u, host_resolver.num_resolve()); | 495 EXPECT_EQ(20u, host_resolver.num_resolve()); |
577 | 496 |
578 EXPECT_EQ("null21:34", proxy_info.proxy_server().ToURI()); | 497 EXPECT_EQ("null21:34", proxy_info.proxy_server().ToURI()); |
579 | 498 |
580 // No errors. | 499 // No errors. |
581 EXPECT_EQ("", error_observer->GetOutput()); | 500 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
582 | 501 |
583 // Check the NetLogs -- 1 alert was logged. | 502 // 1 alert. |
584 EXPECT_EQ(1u, log.GetSize()); | 503 EXPECT_EQ(1u, mock_bindings.GetAlerts().size()); |
585 EXPECT_EQ(1u, request_log.GetSize()); | 504 EXPECT_EQ("iteration: 21", mock_bindings.GetAlerts()[0]); |
586 } | 505 } |
587 | 506 |
588 void DnsDuringInitHelper(bool synchronous_host_resolver) { | 507 void DnsDuringInitHelper(bool synchronous_host_resolver) { |
589 TestNetLog log; | |
590 BoundTestNetLog request_log; | |
591 MockCachingHostResolver host_resolver; | 508 MockCachingHostResolver host_resolver; |
| 509 MockBindings mock_bindings(&host_resolver); |
592 host_resolver.set_synchronous_mode(synchronous_host_resolver); | 510 host_resolver.set_synchronous_mode(synchronous_host_resolver); |
593 MockErrorObserver* error_observer = new MockErrorObserver; | |
594 | 511 |
595 host_resolver.rules()->AddRule("host1", "91.13.12.1"); | 512 host_resolver.rules()->AddRule("host1", "91.13.12.1"); |
596 host_resolver.rules()->AddRule("host2", "91.13.12.2"); | 513 host_resolver.rules()->AddRule("host2", "91.13.12.2"); |
597 | 514 |
598 scoped_ptr<ProxyResolver> resolver = | 515 scoped_ptr<ProxyResolverV8Tracing> resolver = |
599 CreateResolver(&log, &host_resolver, make_scoped_ptr(error_observer), | 516 CreateResolver(mock_bindings.CreateBindings(), "dns_during_init.js"); |
600 "dns_during_init.js"); | |
601 | 517 |
602 // Initialization did 2 dnsResolves. | 518 // Initialization did 2 dnsResolves. |
603 EXPECT_EQ(2u, host_resolver.num_resolve()); | 519 EXPECT_EQ(2u, host_resolver.num_resolve()); |
604 | 520 |
605 host_resolver.rules()->ClearRules(); | 521 host_resolver.rules()->ClearRules(); |
606 host_resolver.GetHostCache()->clear(); | 522 host_resolver.GetHostCache()->clear(); |
607 | 523 |
608 host_resolver.rules()->AddRule("host1", "145.88.13.3"); | 524 host_resolver.rules()->AddRule("host1", "145.88.13.3"); |
609 host_resolver.rules()->AddRule("host2", "137.89.8.45"); | 525 host_resolver.rules()->AddRule("host2", "137.89.8.45"); |
610 | 526 |
611 TestCompletionCallback callback; | 527 TestCompletionCallback callback; |
612 ProxyInfo proxy_info; | 528 ProxyInfo proxy_info; |
613 | 529 |
614 int rv = | 530 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
615 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 531 callback.callback(), NULL, |
616 callback.callback(), NULL, request_log.bound()); | 532 mock_bindings.CreateBindings()); |
617 EXPECT_EQ(ERR_IO_PENDING, rv); | |
618 EXPECT_EQ(OK, callback.WaitForResult()); | 533 EXPECT_EQ(OK, callback.WaitForResult()); |
619 | 534 |
620 // Fetched host1 and host2 again, since the ones done during initialization | 535 // Fetched host1 and host2 again, since the ones done during initialization |
621 // should not have been cached. | 536 // should not have been cached. |
622 EXPECT_EQ(4u, host_resolver.num_resolve()); | 537 EXPECT_EQ(4u, host_resolver.num_resolve()); |
623 | 538 |
624 EXPECT_EQ("91.13.12.1-91.13.12.2-145.88.13.3-137.89.8.45:99", | 539 EXPECT_EQ("91.13.12.1-91.13.12.2-145.88.13.3-137.89.8.45:99", |
625 proxy_info.proxy_server().ToURI()); | 540 proxy_info.proxy_server().ToURI()); |
626 | 541 |
627 // Check the NetLogs -- the script generated 2 alerts during initialization. | 542 // 2 alerts. |
628 EXPECT_EQ(0u, request_log.GetSize()); | 543 ASSERT_EQ(2u, mock_bindings.GetAlerts().size()); |
629 TestNetLogEntry::List entries; | 544 EXPECT_EQ("Watsup", mock_bindings.GetAlerts()[0]); |
630 log.GetEntries(&entries); | 545 EXPECT_EQ("Watsup2", mock_bindings.GetAlerts()[1]); |
631 | |
632 ASSERT_EQ(2u, entries.size()); | |
633 EXPECT_TRUE( | |
634 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
635 NetLog::PHASE_NONE)); | |
636 EXPECT_TRUE( | |
637 LogContainsEvent(entries, 1, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, | |
638 NetLog::PHASE_NONE)); | |
639 | |
640 EXPECT_EQ("{\"message\":\"Watsup\"}", entries[0].GetParamsJson()); | |
641 EXPECT_EQ("{\"message\":\"Watsup2\"}", entries[1].GetParamsJson()); | |
642 } | 546 } |
643 | 547 |
644 // Tests a PAC script which does DNS resolves during initialization. | 548 // Tests a PAC script which does DNS resolves during initialization. |
645 TEST_F(ProxyResolverV8TracingTest, DnsDuringInit) { | 549 TEST_F(ProxyResolverV8TracingTest, DnsDuringInit) { |
646 // Test with both both a host resolver that always completes asynchronously, | 550 // Test with both both a host resolver that always completes asynchronously, |
647 // and then again with one that completes synchronously. | 551 // and then again with one that completes synchronously. |
648 DnsDuringInitHelper(false); | 552 DnsDuringInitHelper(false); |
649 DnsDuringInitHelper(true); | 553 DnsDuringInitHelper(true); |
650 } | 554 } |
651 | 555 |
652 void CrashCallback(int) { | 556 void CrashCallback(int) { |
653 // Be extra sure that if the callback ever gets invoked, the test will fail. | 557 // Be extra sure that if the callback ever gets invoked, the test will fail. |
654 CHECK(false); | 558 CHECK(false); |
655 } | 559 } |
656 | 560 |
657 // Start some requests, cancel them all, and then destroy the resolver. | 561 // Start some requests, cancel them all, and then destroy the resolver. |
658 // Note the execution order for this test can vary. Since multiple | 562 // Note the execution order for this test can vary. Since multiple |
659 // threads are involved, the cancellation may be received a different | 563 // threads are involved, the cancellation may be received a different |
660 // times. | 564 // times. |
661 TEST_F(ProxyResolverV8TracingTest, CancelAll) { | 565 TEST_F(ProxyResolverV8TracingTest, CancelAll) { |
662 MockCachingHostResolver host_resolver; | 566 MockCachingHostResolver host_resolver; |
663 MockErrorObserver* error_observer = new MockErrorObserver; | 567 MockBindings mock_bindings(&host_resolver); |
664 | 568 |
665 host_resolver.rules()->AddSimulatedFailure("*"); | 569 host_resolver.rules()->AddSimulatedFailure("*"); |
666 | 570 |
667 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 571 scoped_ptr<ProxyResolverV8Tracing> resolver = |
668 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); | 572 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
669 | 573 |
670 const size_t kNumRequests = 5; | 574 const size_t kNumRequests = 5; |
671 ProxyInfo proxy_info[kNumRequests]; | 575 ProxyInfo proxy_info[kNumRequests]; |
672 ProxyResolver::RequestHandle request[kNumRequests]; | 576 ProxyResolver::RequestHandle request[kNumRequests]; |
673 | 577 |
674 for (size_t i = 0; i < kNumRequests; ++i) { | 578 for (size_t i = 0; i < kNumRequests; ++i) { |
675 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i], | 579 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info[i], |
676 base::Bind(&CrashCallback), &request[i], | 580 base::Bind(&CrashCallback), &request[i], |
677 BoundNetLog()); | 581 mock_bindings.CreateBindings()); |
678 EXPECT_EQ(ERR_IO_PENDING, rv); | |
679 } | 582 } |
680 | 583 |
681 for (size_t i = 0; i < kNumRequests; ++i) { | 584 for (size_t i = 0; i < kNumRequests; ++i) { |
682 resolver->CancelRequest(request[i]); | 585 resolver->CancelRequest(request[i]); |
683 } | 586 } |
684 } | 587 } |
685 | 588 |
686 // Note the execution order for this test can vary. Since multiple | 589 // Note the execution order for this test can vary. Since multiple |
687 // threads are involved, the cancellation may be received a different | 590 // threads are involved, the cancellation may be received a different |
688 // times. | 591 // times. |
689 TEST_F(ProxyResolverV8TracingTest, CancelSome) { | 592 TEST_F(ProxyResolverV8TracingTest, CancelSome) { |
690 MockCachingHostResolver host_resolver; | 593 MockCachingHostResolver host_resolver; |
691 MockErrorObserver* error_observer = new MockErrorObserver; | 594 MockBindings mock_bindings(&host_resolver); |
692 | 595 |
693 host_resolver.rules()->AddSimulatedFailure("*"); | 596 host_resolver.rules()->AddSimulatedFailure("*"); |
694 | 597 |
695 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 598 scoped_ptr<ProxyResolverV8Tracing> resolver = |
696 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); | 599 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
697 | 600 |
698 ProxyInfo proxy_info1; | 601 ProxyInfo proxy_info1; |
699 ProxyInfo proxy_info2; | 602 ProxyInfo proxy_info2; |
700 ProxyResolver::RequestHandle request1; | 603 ProxyResolver::RequestHandle request1; |
701 ProxyResolver::RequestHandle request2; | 604 ProxyResolver::RequestHandle request2; |
702 TestCompletionCallback callback; | 605 TestCompletionCallback callback; |
703 | 606 |
704 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, | 607 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, |
705 base::Bind(&CrashCallback), &request1, | 608 base::Bind(&CrashCallback), &request1, |
706 BoundNetLog()); | 609 mock_bindings.CreateBindings()); |
707 EXPECT_EQ(ERR_IO_PENDING, rv); | 610 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2, |
708 | 611 callback.callback(), &request2, |
709 rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info2, | 612 mock_bindings.CreateBindings()); |
710 callback.callback(), &request2, BoundNetLog()); | |
711 EXPECT_EQ(ERR_IO_PENDING, rv); | |
712 | 613 |
713 resolver->CancelRequest(request1); | 614 resolver->CancelRequest(request1); |
714 | 615 |
715 EXPECT_EQ(OK, callback.WaitForResult()); | 616 EXPECT_EQ(OK, callback.WaitForResult()); |
716 } | 617 } |
717 | 618 |
718 // Cancel a request after it has finished running on the worker thread, and has | 619 // Cancel a request after it has finished running on the worker thread, and has |
719 // posted a task the completion task back to origin thread. | 620 // posted a task the completion task back to origin thread. |
720 TEST_F(ProxyResolverV8TracingTest, CancelWhilePendingCompletionTask) { | 621 TEST_F(ProxyResolverV8TracingTest, CancelWhilePendingCompletionTask) { |
721 MockCachingHostResolver host_resolver; | 622 MockCachingHostResolver host_resolver; |
722 MockErrorObserver* error_observer = new MockErrorObserver; | 623 MockBindings mock_bindings(&host_resolver); |
723 | 624 |
724 host_resolver.rules()->AddSimulatedFailure("*"); | 625 host_resolver.rules()->AddSimulatedFailure("*"); |
725 | 626 |
726 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 627 scoped_ptr<ProxyResolverV8Tracing> resolver = |
727 nullptr, &host_resolver, make_scoped_ptr(error_observer), "error.js"); | 628 CreateResolver(mock_bindings.CreateBindings(), "error.js"); |
728 | 629 |
729 ProxyInfo proxy_info1; | 630 ProxyInfo proxy_info1; |
730 ProxyInfo proxy_info2; | 631 ProxyInfo proxy_info2; |
731 ProxyInfo proxy_info3; | 632 ProxyInfo proxy_info3; |
732 ProxyResolver::RequestHandle request1; | 633 ProxyResolver::RequestHandle request1; |
733 ProxyResolver::RequestHandle request2; | 634 ProxyResolver::RequestHandle request2; |
734 ProxyResolver::RequestHandle request3; | 635 ProxyResolver::RequestHandle request3; |
735 TestCompletionCallback callback; | 636 TestCompletionCallback callback; |
736 | 637 |
737 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, | 638 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info1, |
738 base::Bind(&CrashCallback), &request1, | 639 base::Bind(&CrashCallback), &request1, |
739 BoundNetLog()); | 640 mock_bindings.CreateBindings()); |
740 EXPECT_EQ(ERR_IO_PENDING, rv); | |
741 | 641 |
742 rv = resolver->GetProxyForURL(GURL("http://throw-an-error/"), &proxy_info2, | 642 resolver->GetProxyForURL(GURL("http://throw-an-error"), &proxy_info2, |
743 callback.callback(), &request2, BoundNetLog()); | 643 callback.callback(), &request2, |
744 EXPECT_EQ(ERR_IO_PENDING, rv); | 644 mock_bindings.CreateBindings()); |
745 | 645 |
746 // Wait until the first request has finished running on the worker thread. | 646 // Wait until the first request has finished running on the worker thread. |
747 // (The second request will output an error). | 647 // (The second request will output an error). |
748 error_observer->WaitForOutput(); | 648 mock_bindings.WaitForError(); |
749 | 649 |
750 // Cancel the first request, while it has a pending completion task on | 650 // Cancel the first request, while it has a pending completion task on |
751 // the origin thread. | 651 // the origin thread. |
752 resolver->CancelRequest(request1); | 652 resolver->CancelRequest(request1); |
753 | 653 |
754 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult()); | 654 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult()); |
755 | 655 |
756 // Start another request, to make sure it is able to complete. | 656 // Start another request, to make sure it is able to complete. |
757 rv = resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"), | 657 resolver->GetProxyForURL(GURL("http://i-have-no-idea-what-im-doing/"), |
758 &proxy_info3, callback.callback(), &request3, | 658 &proxy_info3, callback.callback(), &request3, |
759 BoundNetLog()); | 659 mock_bindings.CreateBindings()); |
760 EXPECT_EQ(ERR_IO_PENDING, rv); | |
761 | 660 |
762 EXPECT_EQ(OK, callback.WaitForResult()); | 661 EXPECT_EQ(OK, callback.WaitForResult()); |
763 | 662 |
764 EXPECT_EQ("i-approve-this-message:42", | 663 EXPECT_EQ("i-approve-this-message:42", |
765 proxy_info3.proxy_server().ToURI()); | 664 proxy_info3.proxy_server().ToURI()); |
766 } | 665 } |
767 | 666 |
768 // This implementation of HostResolver allows blocking until a resolve request | 667 // This implementation of HostResolver allows blocking until a resolve request |
769 // has been received. The resolve requests it receives will never be completed. | 668 // has been received. The resolve requests it receives will never be completed. |
770 class BlockableHostResolver : public HostResolver { | 669 class BlockableHostResolver : public HostResolver { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 private: | 728 private: |
830 int num_cancelled_requests_; | 729 int num_cancelled_requests_; |
831 bool waiting_for_resolve_; | 730 bool waiting_for_resolve_; |
832 base::Callback<void(void)> action_; | 731 base::Callback<void(void)> action_; |
833 }; | 732 }; |
834 | 733 |
835 // This cancellation test exercises a more predictable cancellation codepath -- | 734 // This cancellation test exercises a more predictable cancellation codepath -- |
836 // when the request has an outstanding DNS request in flight. | 735 // when the request has an outstanding DNS request in flight. |
837 TEST_F(ProxyResolverV8TracingTest, CancelWhileOutstandingNonBlockingDns) { | 736 TEST_F(ProxyResolverV8TracingTest, CancelWhileOutstandingNonBlockingDns) { |
838 BlockableHostResolver host_resolver; | 737 BlockableHostResolver host_resolver; |
839 MockErrorObserver* error_observer = new MockErrorObserver; | 738 MockBindings mock_bindings(&host_resolver); |
840 | 739 |
841 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 740 scoped_ptr<ProxyResolverV8Tracing> resolver = |
842 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); | 741 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
843 | 742 |
844 ProxyInfo proxy_info1; | 743 ProxyInfo proxy_info1; |
845 ProxyInfo proxy_info2; | 744 ProxyInfo proxy_info2; |
846 ProxyResolver::RequestHandle request1; | 745 ProxyResolver::RequestHandle request1; |
847 ProxyResolver::RequestHandle request2; | 746 ProxyResolver::RequestHandle request2; |
848 | 747 |
849 int rv = resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1, | 748 resolver->GetProxyForURL(GURL("http://foo/req1"), &proxy_info1, |
850 base::Bind(&CrashCallback), &request1, | 749 base::Bind(&CrashCallback), &request1, |
851 BoundNetLog()); | 750 mock_bindings.CreateBindings()); |
852 | |
853 EXPECT_EQ(ERR_IO_PENDING, rv); | |
854 | 751 |
855 host_resolver.WaitUntilRequestIsReceived(); | 752 host_resolver.WaitUntilRequestIsReceived(); |
856 | 753 |
857 rv = resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2, | 754 resolver->GetProxyForURL(GURL("http://foo/req2"), &proxy_info2, |
858 base::Bind(&CrashCallback), &request2, | 755 base::Bind(&CrashCallback), &request2, |
859 BoundNetLog()); | 756 mock_bindings.CreateBindings()); |
860 | |
861 EXPECT_EQ(ERR_IO_PENDING, rv); | |
862 | 757 |
863 host_resolver.WaitUntilRequestIsReceived(); | 758 host_resolver.WaitUntilRequestIsReceived(); |
864 | 759 |
865 resolver->CancelRequest(request1); | 760 resolver->CancelRequest(request1); |
866 resolver->CancelRequest(request2); | 761 resolver->CancelRequest(request2); |
867 | 762 |
868 EXPECT_EQ(2, host_resolver.num_cancelled_requests()); | 763 EXPECT_EQ(2, host_resolver.num_cancelled_requests()); |
869 | 764 |
870 // After leaving this scope, the ProxyResolver is destroyed. | 765 // After leaving this scope, the ProxyResolver is destroyed. |
871 // This should not cause any problems, as the outstanding work | 766 // This should not cause any problems, as the outstanding work |
872 // should have been cancelled. | 767 // should have been cancelled. |
873 } | 768 } |
874 | 769 |
875 void CancelRequestAndPause(ProxyResolver* resolver, | 770 void CancelRequestAndPause(ProxyResolverV8Tracing* resolver, |
876 ProxyResolver::RequestHandle request) { | 771 ProxyResolver::RequestHandle request) { |
877 resolver->CancelRequest(request); | 772 resolver->CancelRequest(request); |
878 | 773 |
879 // Sleep for a little bit. This makes it more likely for the worker | 774 // Sleep for a little bit. This makes it more likely for the worker |
880 // thread to have returned from its call, and serves as a regression | 775 // thread to have returned from its call, and serves as a regression |
881 // test for http://crbug.com/173373. | 776 // test for http://crbug.com/173373. |
882 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); | 777 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
883 } | 778 } |
884 | 779 |
885 // In non-blocking mode, the worker thread actually does block for | 780 // In non-blocking mode, the worker thread actually does block for |
886 // a short time to see if the result is in the DNS cache. Test | 781 // a short time to see if the result is in the DNS cache. Test |
887 // cancellation while the worker thread is waiting on this event. | 782 // cancellation while the worker thread is waiting on this event. |
888 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns) { | 783 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns) { |
889 BlockableHostResolver host_resolver; | 784 BlockableHostResolver host_resolver; |
890 MockErrorObserver* error_observer = new MockErrorObserver; | 785 MockBindings mock_bindings(&host_resolver); |
891 | 786 |
892 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 787 scoped_ptr<ProxyResolverV8Tracing> resolver = |
893 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); | 788 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
894 | 789 |
895 ProxyInfo proxy_info; | 790 ProxyInfo proxy_info; |
896 ProxyResolver::RequestHandle request; | 791 ProxyResolver::RequestHandle request; |
897 | 792 |
898 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 793 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
899 base::Bind(&CrashCallback), &request, | 794 base::Bind(&CrashCallback), &request, |
900 BoundNetLog()); | 795 mock_bindings.CreateBindings()); |
901 | |
902 EXPECT_EQ(ERR_IO_PENDING, rv); | |
903 | 796 |
904 host_resolver.SetAction( | 797 host_resolver.SetAction( |
905 base::Bind(CancelRequestAndPause, resolver.get(), request)); | 798 base::Bind(CancelRequestAndPause, resolver.get(), request)); |
906 | 799 |
907 host_resolver.WaitUntilRequestIsReceived(); | 800 host_resolver.WaitUntilRequestIsReceived(); |
908 | 801 |
909 // At this point the host resolver ran Resolve(), and should have cancelled | 802 // At this point the host resolver ran Resolve(), and should have cancelled |
910 // the request. | 803 // the request. |
911 | 804 |
912 EXPECT_EQ(1, host_resolver.num_cancelled_requests()); | 805 EXPECT_EQ(1, host_resolver.num_cancelled_requests()); |
913 } | 806 } |
914 | 807 |
915 // Cancel the request while there is a pending DNS request, however before | 808 // Cancel the request while there is a pending DNS request, however before |
916 // the request is sent to the host resolver. | 809 // the request is sent to the host resolver. |
917 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns2) { | 810 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns2) { |
918 MockCachingHostResolver host_resolver; | 811 MockCachingHostResolver host_resolver; |
919 MockErrorObserver* error_observer = new MockErrorObserver; | 812 MockBindings mock_bindings(&host_resolver); |
920 | 813 |
921 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 814 scoped_ptr<ProxyResolverV8Tracing> resolver = |
922 nullptr, &host_resolver, make_scoped_ptr(error_observer), "dns.js"); | 815 CreateResolver(mock_bindings.CreateBindings(), "dns.js"); |
923 | 816 |
924 ProxyInfo proxy_info; | 817 ProxyInfo proxy_info; |
925 ProxyResolver::RequestHandle request; | 818 ProxyResolver::RequestHandle request; |
926 | 819 |
927 int rv = resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, | 820 resolver->GetProxyForURL(GURL("http://foo/"), &proxy_info, |
928 base::Bind(&CrashCallback), &request, | 821 base::Bind(&CrashCallback), &request, |
929 BoundNetLog()); | 822 mock_bindings.CreateBindings()); |
930 | |
931 EXPECT_EQ(ERR_IO_PENDING, rv); | |
932 | 823 |
933 // Wait a bit, so the DNS task has hopefully been posted. The test will | 824 // Wait a bit, so the DNS task has hopefully been posted. The test will |
934 // work whatever the delay is here, but it is most useful if the delay | 825 // work whatever the delay is here, but it is most useful if the delay |
935 // is large enough to allow a task to be posted back. | 826 // is large enough to allow a task to be posted back. |
936 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 827 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
937 resolver->CancelRequest(request); | 828 resolver->CancelRequest(request); |
938 | 829 |
939 EXPECT_EQ(0u, host_resolver.num_resolve()); | 830 EXPECT_EQ(0u, host_resolver.num_resolve()); |
940 } | 831 } |
941 | 832 |
942 TEST_F(ProxyResolverV8TracingTest, | 833 TEST_F(ProxyResolverV8TracingTest, |
943 CancelCreateResolverWhileOutstandingBlockingDns) { | 834 CancelCreateResolverWhileOutstandingBlockingDns) { |
944 BlockableHostResolver host_resolver; | 835 BlockableHostResolver host_resolver; |
945 MockErrorObserver* error_observer = new MockErrorObserver; | 836 MockBindings mock_bindings(&host_resolver); |
946 | 837 |
947 ProxyResolverFactoryV8Tracing factory( | 838 scoped_ptr<ProxyResolverV8TracingFactory> factory( |
948 &host_resolver, nullptr, ProxyResolver::LoadStateChangedCallback(), | 839 ProxyResolverV8TracingFactory::Create()); |
949 base::Bind(&ReturnErrorObserver, | 840 scoped_ptr<ProxyResolverV8Tracing> resolver; |
950 base::Passed(make_scoped_ptr(error_observer)))); | |
951 | |
952 scoped_ptr<ProxyResolver> resolver; | |
953 scoped_ptr<ProxyResolverFactory::Request> request; | 841 scoped_ptr<ProxyResolverFactory::Request> request; |
954 int rv = factory.CreateProxyResolver(LoadScriptData("dns_during_init.js"), | 842 factory->CreateProxyResolverV8Tracing( |
955 &resolver, base::Bind(&CrashCallback), | 843 LoadScriptData("dns_during_init.js"), mock_bindings.CreateBindings(), |
956 &request); | 844 &resolver, base::Bind(&CrashCallback), &request); |
957 EXPECT_EQ(ERR_IO_PENDING, rv); | |
958 | 845 |
959 host_resolver.WaitUntilRequestIsReceived(); | 846 host_resolver.WaitUntilRequestIsReceived(); |
960 | 847 |
961 request.reset(); | 848 request.reset(); |
962 EXPECT_EQ(1, host_resolver.num_cancelled_requests()); | 849 EXPECT_EQ(1, host_resolver.num_cancelled_requests()); |
963 } | 850 } |
964 | 851 |
965 TEST_F(ProxyResolverV8TracingTest, DeleteFactoryWhileOutstandingBlockingDns) { | 852 TEST_F(ProxyResolverV8TracingTest, DeleteFactoryWhileOutstandingBlockingDns) { |
966 BlockableHostResolver host_resolver; | 853 BlockableHostResolver host_resolver; |
967 MockErrorObserver* error_observer = new MockErrorObserver; | 854 MockBindings mock_bindings(&host_resolver); |
968 | 855 |
969 scoped_ptr<ProxyResolver> resolver; | 856 scoped_ptr<ProxyResolverV8Tracing> resolver; |
970 scoped_ptr<ProxyResolverFactory::Request> request; | 857 scoped_ptr<ProxyResolverFactory::Request> request; |
971 { | 858 { |
972 ProxyResolverFactoryV8Tracing factory( | 859 scoped_ptr<ProxyResolverV8TracingFactory> factory( |
973 &host_resolver, nullptr, ProxyResolver::LoadStateChangedCallback(), | 860 ProxyResolverV8TracingFactory::Create()); |
974 base::Bind(&ReturnErrorObserver, | |
975 base::Passed(make_scoped_ptr(error_observer)))); | |
976 | 861 |
977 int rv = factory.CreateProxyResolver(LoadScriptData("dns_during_init.js"), | 862 factory->CreateProxyResolverV8Tracing( |
978 &resolver, base::Bind(&CrashCallback), | 863 LoadScriptData("dns_during_init.js"), mock_bindings.CreateBindings(), |
979 &request); | 864 &resolver, base::Bind(&CrashCallback), &request); |
980 EXPECT_EQ(ERR_IO_PENDING, rv); | |
981 host_resolver.WaitUntilRequestIsReceived(); | 865 host_resolver.WaitUntilRequestIsReceived(); |
982 } | 866 } |
983 EXPECT_EQ(1, host_resolver.num_cancelled_requests()); | 867 EXPECT_EQ(1, host_resolver.num_cancelled_requests()); |
984 } | 868 } |
985 | 869 |
986 TEST_F(ProxyResolverV8TracingTest, ErrorLoadingScript) { | 870 TEST_F(ProxyResolverV8TracingTest, ErrorLoadingScript) { |
987 BlockableHostResolver host_resolver; | 871 BlockableHostResolver host_resolver; |
988 MockErrorObserver* error_observer = new MockErrorObserver; | 872 MockBindings mock_bindings(&host_resolver); |
989 | 873 |
990 ProxyResolverFactoryV8Tracing factory( | 874 scoped_ptr<ProxyResolverV8TracingFactory> factory( |
991 &host_resolver, nullptr, ProxyResolver::LoadStateChangedCallback(), | 875 ProxyResolverV8TracingFactory::Create()); |
992 base::Bind(&ReturnErrorObserver, | 876 scoped_ptr<ProxyResolverV8Tracing> resolver; |
993 base::Passed(make_scoped_ptr(error_observer)))); | |
994 | |
995 scoped_ptr<ProxyResolver> resolver; | |
996 scoped_ptr<ProxyResolverFactory::Request> request; | 877 scoped_ptr<ProxyResolverFactory::Request> request; |
997 TestCompletionCallback callback; | 878 TestCompletionCallback callback; |
998 int rv = | 879 factory->CreateProxyResolverV8Tracing( |
999 factory.CreateProxyResolver(LoadScriptData("error_on_load.js"), &resolver, | 880 LoadScriptData("error_on_load.js"), mock_bindings.CreateBindings(), |
1000 callback.callback(), &request); | 881 &resolver, callback.callback(), &request); |
1001 EXPECT_EQ(ERR_IO_PENDING, rv); | 882 |
1002 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult()); | 883 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult()); |
1003 EXPECT_FALSE(resolver); | 884 EXPECT_FALSE(resolver); |
1004 } | 885 } |
1005 | 886 |
1006 // This tests that the execution of a PAC script is terminated when the DNS | 887 // This tests that the execution of a PAC script is terminated when the DNS |
1007 // dependencies are missing. If the test fails, then it will hang. | 888 // dependencies are missing. If the test fails, then it will hang. |
1008 TEST_F(ProxyResolverV8TracingTest, Terminate) { | 889 TEST_F(ProxyResolverV8TracingTest, Terminate) { |
1009 TestNetLog log; | |
1010 BoundTestNetLog request_log; | |
1011 MockCachingHostResolver host_resolver; | 890 MockCachingHostResolver host_resolver; |
1012 MockErrorObserver* error_observer = new MockErrorObserver; | 891 MockBindings mock_bindings(&host_resolver); |
1013 | 892 |
1014 host_resolver.rules()->AddRule("host1", "182.111.0.222"); | 893 host_resolver.rules()->AddRule("host1", "182.111.0.222"); |
1015 host_resolver.rules()->AddRule("host2", "111.33.44.55"); | 894 host_resolver.rules()->AddRule("host2", "111.33.44.55"); |
1016 | 895 |
1017 scoped_ptr<ProxyResolver> resolver = CreateResolver( | 896 scoped_ptr<ProxyResolverV8Tracing> resolver = |
1018 &log, &host_resolver, make_scoped_ptr(error_observer), "terminate.js"); | 897 CreateResolver(mock_bindings.CreateBindings(), "terminate.js"); |
1019 | 898 |
1020 TestCompletionCallback callback; | 899 TestCompletionCallback callback; |
1021 ProxyInfo proxy_info; | 900 ProxyInfo proxy_info; |
1022 | 901 |
1023 int rv = | 902 resolver->GetProxyForURL(GURL("http://foopy/req1"), &proxy_info, |
1024 resolver->GetProxyForURL(GURL("http://foopy/req1"), &proxy_info, | 903 callback.callback(), NULL, |
1025 callback.callback(), NULL, request_log.bound()); | 904 mock_bindings.CreateBindings()); |
1026 | |
1027 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1028 EXPECT_EQ(OK, callback.WaitForResult()); | 905 EXPECT_EQ(OK, callback.WaitForResult()); |
1029 | 906 |
1030 // The test does 2 DNS resolutions. | 907 // The test does 2 DNS resolutions. |
1031 EXPECT_EQ(2u, host_resolver.num_resolve()); | 908 EXPECT_EQ(2u, host_resolver.num_resolve()); |
1032 | 909 |
1033 EXPECT_EQ("foopy:3", proxy_info.proxy_server().ToURI()); | 910 EXPECT_EQ("foopy:3", proxy_info.proxy_server().ToURI()); |
1034 | 911 |
1035 // No errors. | 912 // No errors or alerts. |
1036 EXPECT_EQ("", error_observer->GetOutput()); | 913 EXPECT_TRUE(mock_bindings.GetErrors().empty()); |
1037 | 914 EXPECT_TRUE(mock_bindings.GetAlerts().empty()); |
1038 EXPECT_EQ(0u, log.GetSize()); | |
1039 EXPECT_EQ(0u, request_log.GetSize()); | |
1040 } | 915 } |
1041 | 916 |
1042 // Tests that multiple instances of ProxyResolverV8Tracing can coexist and run | 917 // Tests that multiple instances of ProxyResolverV8Tracing can coexist and run |
1043 // correctly at the same time. This is relevant because at the moment (time | 918 // correctly at the same time. This is relevant because at the moment (time |
1044 // this test was written) each ProxyResolverV8Tracing creates its own thread to | 919 // this test was written) each ProxyResolverV8Tracing creates its own thread to |
1045 // run V8 on, however each thread is operating on the same v8::Isolate. | 920 // run V8 on, however each thread is operating on the same v8::Isolate. |
1046 TEST_F(ProxyResolverV8TracingTest, MultipleResolvers) { | 921 TEST_F(ProxyResolverV8TracingTest, MultipleResolvers) { |
1047 // ------------------------ | 922 // ------------------------ |
1048 // Setup resolver0 | 923 // Setup resolver0 |
1049 // ------------------------ | 924 // ------------------------ |
1050 MockHostResolver host_resolver0; | 925 MockHostResolver host_resolver0; |
| 926 MockBindings mock_bindings0(&host_resolver0); |
1051 host_resolver0.rules()->AddRuleForAddressFamily( | 927 host_resolver0.rules()->AddRuleForAddressFamily( |
1052 "host1", ADDRESS_FAMILY_IPV4, "166.155.144.44"); | 928 "host1", ADDRESS_FAMILY_IPV4, "166.155.144.44"); |
1053 host_resolver0.rules() | 929 host_resolver0.rules() |
1054 ->AddIPLiteralRule("host1", "::1,192.168.1.1", std::string()); | 930 ->AddIPLiteralRule("host1", "::1,192.168.1.1", std::string()); |
1055 host_resolver0.rules()->AddSimulatedFailure("host2"); | 931 host_resolver0.rules()->AddSimulatedFailure("host2"); |
1056 host_resolver0.rules()->AddRule("host3", "166.155.144.33"); | 932 host_resolver0.rules()->AddRule("host3", "166.155.144.33"); |
1057 host_resolver0.rules()->AddRule("host5", "166.155.144.55"); | 933 host_resolver0.rules()->AddRule("host5", "166.155.144.55"); |
1058 host_resolver0.rules()->AddSimulatedFailure("host6"); | 934 host_resolver0.rules()->AddSimulatedFailure("host6"); |
1059 host_resolver0.rules()->AddRuleForAddressFamily( | 935 host_resolver0.rules()->AddRuleForAddressFamily( |
1060 "*", ADDRESS_FAMILY_IPV4, "122.133.144.155"); | 936 "*", ADDRESS_FAMILY_IPV4, "122.133.144.155"); |
1061 host_resolver0.rules()->AddRule("*", "133.122.100.200"); | 937 host_resolver0.rules()->AddRule("*", "133.122.100.200"); |
1062 scoped_ptr<ProxyResolver> resolver0 = | 938 scoped_ptr<ProxyResolverV8Tracing> resolver0 = |
1063 CreateResolver(nullptr, &host_resolver0, | 939 CreateResolver(mock_bindings0.CreateBindings(), "dns.js"); |
1064 make_scoped_ptr(new MockErrorObserver), "dns.js"); | |
1065 | 940 |
1066 // ------------------------ | 941 // ------------------------ |
1067 // Setup resolver1 | 942 // Setup resolver1 |
1068 // ------------------------ | 943 // ------------------------ |
1069 scoped_ptr<ProxyResolver> resolver1 = | 944 scoped_ptr<ProxyResolverV8Tracing> resolver1 = |
1070 CreateResolver(nullptr, &host_resolver0, | 945 CreateResolver(mock_bindings0.CreateBindings(), "dns.js"); |
1071 make_scoped_ptr(new MockErrorObserver), "dns.js"); | |
1072 | 946 |
1073 // ------------------------ | 947 // ------------------------ |
1074 // Setup resolver2 | 948 // Setup resolver2 |
1075 // ------------------------ | 949 // ------------------------ |
1076 scoped_ptr<ProxyResolver> resolver2 = | 950 scoped_ptr<ProxyResolverV8Tracing> resolver2 = |
1077 CreateResolver(nullptr, &host_resolver0, | 951 CreateResolver(mock_bindings0.CreateBindings(), "simple.js"); |
1078 make_scoped_ptr(new MockErrorObserver), "simple.js"); | |
1079 | 952 |
1080 // ------------------------ | 953 // ------------------------ |
1081 // Setup resolver3 | 954 // Setup resolver3 |
1082 // ------------------------ | 955 // ------------------------ |
1083 MockHostResolver host_resolver3; | 956 MockHostResolver host_resolver3; |
| 957 MockBindings mock_bindings3(&host_resolver3); |
1084 host_resolver3.rules()->AddRule("foo", "166.155.144.33"); | 958 host_resolver3.rules()->AddRule("foo", "166.155.144.33"); |
1085 scoped_ptr<ProxyResolver> resolver3 = | 959 scoped_ptr<ProxyResolverV8Tracing> resolver3 = |
1086 CreateResolver(nullptr, &host_resolver3, | 960 CreateResolver(mock_bindings3.CreateBindings(), "simple_dns.js"); |
1087 make_scoped_ptr(new MockErrorObserver), "simple_dns.js"); | |
1088 | 961 |
1089 // ------------------------ | 962 // ------------------------ |
1090 // Queue up work for each resolver (which will be running in parallel). | 963 // Queue up work for each resolver (which will be running in parallel). |
1091 // ------------------------ | 964 // ------------------------ |
1092 | 965 |
1093 ProxyResolver* resolver[] = { | 966 ProxyResolverV8Tracing* resolver[] = { |
1094 resolver0.get(), resolver1.get(), resolver2.get(), resolver3.get(), | 967 resolver0.get(), resolver1.get(), resolver2.get(), resolver3.get(), |
1095 }; | 968 }; |
1096 | 969 |
1097 const size_t kNumResolvers = arraysize(resolver); | 970 const size_t kNumResolvers = arraysize(resolver); |
1098 const size_t kNumIterations = 20; | 971 const size_t kNumIterations = 20; |
1099 const size_t kNumResults = kNumResolvers * kNumIterations; | 972 const size_t kNumResults = kNumResolvers * kNumIterations; |
1100 TestCompletionCallback callback[kNumResults]; | 973 TestCompletionCallback callback[kNumResults]; |
1101 ProxyInfo proxy_info[kNumResults]; | 974 ProxyInfo proxy_info[kNumResults]; |
1102 | 975 |
1103 for (size_t i = 0; i < kNumResults; ++i) { | 976 for (size_t i = 0; i < kNumResults; ++i) { |
1104 size_t resolver_i = i % kNumResolvers; | 977 size_t resolver_i = i % kNumResolvers; |
1105 int rv = resolver[resolver_i]->GetProxyForURL( | 978 resolver[resolver_i]->GetProxyForURL( |
1106 GURL("http://foo/"), &proxy_info[i], callback[i].callback(), NULL, | 979 GURL("http://foo/"), &proxy_info[i], callback[i].callback(), NULL, |
1107 BoundNetLog()); | 980 resolver_i == 3 ? mock_bindings3.CreateBindings() |
1108 EXPECT_EQ(ERR_IO_PENDING, rv); | 981 : mock_bindings0.CreateBindings()); |
1109 } | 982 } |
1110 | 983 |
1111 // ------------------------ | 984 // ------------------------ |
1112 // Verify all of the results. | 985 // Verify all of the results. |
1113 // ------------------------ | 986 // ------------------------ |
1114 | 987 |
1115 const char* kExpectedForDnsJs = | 988 const char* kExpectedForDnsJs = |
1116 "122.133.144.155-" // myIpAddress() | 989 "122.133.144.155-" // myIpAddress() |
1117 "null-" // dnsResolve('') | 990 "null-" // dnsResolve('') |
1118 "__1_192.168.1.1-" // dnsResolveEx('host1') | 991 "__1_192.168.1.1-" // dnsResolveEx('host1') |
(...skipping 24 matching lines...) Expand all Loading... |
1143 proxy_uri.substr(0, proxy_uri.find(':') + 1)); | 1016 proxy_uri.substr(0, proxy_uri.find(':') + 1)); |
1144 } else { | 1017 } else { |
1145 NOTREACHED(); | 1018 NOTREACHED(); |
1146 } | 1019 } |
1147 } | 1020 } |
1148 } | 1021 } |
1149 | 1022 |
1150 } // namespace | 1023 } // namespace |
1151 | 1024 |
1152 } // namespace net | 1025 } // namespace net |
OLD | NEW |