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