OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_service_mojo.h" | 5 #include "net/proxy/proxy_service_mojo.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "net/base/load_flags.h" | |
17 #include "net/base/network_delegate_impl.h" | 16 #include "net/base/network_delegate_impl.h" |
18 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
19 #include "net/dns/mock_host_resolver.h" | 18 #include "net/dns/mock_host_resolver.h" |
20 #include "net/log/net_log.h" | 19 #include "net/log/net_log.h" |
21 #include "net/log/test_net_log.h" | 20 #include "net/log/test_net_log.h" |
22 #include "net/log/test_net_log_entry.h" | 21 #include "net/log/test_net_log_entry.h" |
23 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 22 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
24 #include "net/proxy/in_process_mojo_proxy_resolver_factory.h" | 23 #include "net/proxy/in_process_mojo_proxy_resolver_factory.h" |
25 #include "net/proxy/mock_proxy_script_fetcher.h" | 24 #include "net/proxy/mock_proxy_script_fetcher.h" |
26 #include "net/proxy/mojo_proxy_resolver_factory.h" | 25 #include "net/proxy/mojo_proxy_resolver_factory.h" |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 MockProxyScriptFetcher* fetcher_; // Owned by |proxy_service_|. | 152 MockProxyScriptFetcher* fetcher_; // Owned by |proxy_service_|. |
154 TestNetLog net_log_; | 153 TestNetLog net_log_; |
155 TestClosure on_delete_closure_; | 154 TestClosure on_delete_closure_; |
156 std::unique_ptr<ProxyService> proxy_service_; | 155 std::unique_ptr<ProxyService> proxy_service_; |
157 }; | 156 }; |
158 | 157 |
159 TEST_F(ProxyServiceMojoTest, Basic) { | 158 TEST_F(ProxyServiceMojoTest, Basic) { |
160 ProxyInfo info; | 159 ProxyInfo info; |
161 TestCompletionCallback callback; | 160 TestCompletionCallback callback; |
162 EXPECT_EQ(ERR_IO_PENDING, | 161 EXPECT_EQ(ERR_IO_PENDING, |
163 proxy_service_->ResolveProxy( | 162 proxy_service_->ResolveProxy(GURL("http://foo"), std::string(), |
164 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, | 163 &info, callback.callback(), nullptr, |
165 callback.callback(), nullptr, nullptr, BoundNetLog())); | 164 nullptr, BoundNetLog())); |
166 | 165 |
167 // Proxy script fetcher should have a fetch triggered by the first | 166 // Proxy script fetcher should have a fetch triggered by the first |
168 // |ResolveProxy()| request. | 167 // |ResolveProxy()| request. |
169 EXPECT_TRUE(fetcher_->has_pending_request()); | 168 EXPECT_TRUE(fetcher_->has_pending_request()); |
170 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 169 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
171 fetcher_->NotifyFetchCompletion(OK, kSimplePacScript); | 170 fetcher_->NotifyFetchCompletion(OK, kSimplePacScript); |
172 | 171 |
173 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 172 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
174 EXPECT_EQ("PROXY foo:1234", info.ToPacString()); | 173 EXPECT_EQ("PROXY foo:1234", info.ToPacString()); |
175 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); | 174 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); |
176 proxy_service_.reset(); | 175 proxy_service_.reset(); |
177 on_delete_closure_.WaitForResult(); | 176 on_delete_closure_.WaitForResult(); |
178 } | 177 } |
179 | 178 |
180 TEST_F(ProxyServiceMojoTest, DnsResolution) { | 179 TEST_F(ProxyServiceMojoTest, DnsResolution) { |
181 ProxyInfo info; | 180 ProxyInfo info; |
182 TestCompletionCallback callback; | 181 TestCompletionCallback callback; |
183 BoundTestNetLog bound_net_log; | 182 BoundTestNetLog bound_net_log; |
184 EXPECT_EQ(ERR_IO_PENDING, | 183 EXPECT_EQ(ERR_IO_PENDING, |
185 proxy_service_->ResolveProxy( | 184 proxy_service_->ResolveProxy(GURL("http://foo"), std::string(), |
186 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, | 185 &info, callback.callback(), nullptr, |
187 callback.callback(), nullptr, nullptr, bound_net_log.bound())); | 186 nullptr, bound_net_log.bound())); |
188 | 187 |
189 // Proxy script fetcher should have a fetch triggered by the first | 188 // Proxy script fetcher should have a fetch triggered by the first |
190 // |ResolveProxy()| request. | 189 // |ResolveProxy()| request. |
191 EXPECT_TRUE(fetcher_->has_pending_request()); | 190 EXPECT_TRUE(fetcher_->has_pending_request()); |
192 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 191 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
193 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); | 192 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); |
194 | 193 |
195 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 194 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
196 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); | 195 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); |
197 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); | 196 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); |
198 proxy_service_.reset(); | 197 proxy_service_.reset(); |
199 on_delete_closure_.WaitForResult(); | 198 on_delete_closure_.WaitForResult(); |
200 | 199 |
201 TestNetLogEntry::List entries; | 200 TestNetLogEntry::List entries; |
202 bound_net_log.GetEntries(&entries); | 201 bound_net_log.GetEntries(&entries); |
203 // There should be one entry with type TYPE_HOST_RESOLVER_IMPL_JOB. | 202 // There should be one entry with type TYPE_HOST_RESOLVER_IMPL_JOB. |
204 EXPECT_EQ(1, std::count_if(entries.begin(), entries.end(), | 203 EXPECT_EQ(1, std::count_if(entries.begin(), entries.end(), |
205 [](const TestNetLogEntry& entry) { | 204 [](const TestNetLogEntry& entry) { |
206 return entry.type == | 205 return entry.type == |
207 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB; | 206 NetLog::TYPE_HOST_RESOLVER_IMPL_JOB; |
208 })); | 207 })); |
209 } | 208 } |
210 | 209 |
211 TEST_F(ProxyServiceMojoTest, Error) { | 210 TEST_F(ProxyServiceMojoTest, Error) { |
212 ProxyInfo info; | 211 ProxyInfo info; |
213 TestCompletionCallback callback; | 212 TestCompletionCallback callback; |
214 BoundTestNetLog bound_net_log; | 213 BoundTestNetLog bound_net_log; |
215 EXPECT_EQ(ERR_IO_PENDING, | 214 EXPECT_EQ(ERR_IO_PENDING, |
216 proxy_service_->ResolveProxy( | 215 proxy_service_->ResolveProxy(GURL("http://foo"), std::string(), |
217 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, | 216 &info, callback.callback(), nullptr, |
218 callback.callback(), nullptr, nullptr, bound_net_log.bound())); | 217 nullptr, bound_net_log.bound())); |
219 | 218 |
220 // Proxy script fetcher should have a fetch triggered by the first | 219 // Proxy script fetcher should have a fetch triggered by the first |
221 // |ResolveProxy()| request. | 220 // |ResolveProxy()| request. |
222 EXPECT_TRUE(fetcher_->has_pending_request()); | 221 EXPECT_TRUE(fetcher_->has_pending_request()); |
223 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 222 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
224 fetcher_->NotifyFetchCompletion(OK, kThrowingPacScript); | 223 fetcher_->NotifyFetchCompletion(OK, kThrowingPacScript); |
225 | 224 |
226 network_delegate_.event_waiter().WaitForEvent( | 225 network_delegate_.event_waiter().WaitForEvent( |
227 TestNetworkDelegate::PAC_SCRIPT_ERROR); | 226 TestNetworkDelegate::PAC_SCRIPT_ERROR); |
228 | 227 |
229 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 228 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
230 EXPECT_EQ("DIRECT", info.ToPacString()); | 229 EXPECT_EQ("DIRECT", info.ToPacString()); |
231 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); | 230 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); |
232 | 231 |
233 TestNetLogEntry::List entries; | 232 TestNetLogEntry::List entries; |
234 bound_net_log.GetEntries(&entries); | 233 bound_net_log.GetEntries(&entries); |
235 CheckCapturedNetLogEntries(entries); | 234 CheckCapturedNetLogEntries(entries); |
236 entries.clear(); | 235 entries.clear(); |
237 net_log_.GetEntries(&entries); | 236 net_log_.GetEntries(&entries); |
238 CheckCapturedNetLogEntries(entries); | 237 CheckCapturedNetLogEntries(entries); |
239 } | 238 } |
240 | 239 |
241 TEST_F(ProxyServiceMojoTest, ErrorOnInitialization) { | 240 TEST_F(ProxyServiceMojoTest, ErrorOnInitialization) { |
242 ProxyInfo info; | 241 ProxyInfo info; |
243 TestCompletionCallback callback; | 242 TestCompletionCallback callback; |
244 EXPECT_EQ(ERR_IO_PENDING, | 243 EXPECT_EQ(ERR_IO_PENDING, |
245 proxy_service_->ResolveProxy( | 244 proxy_service_->ResolveProxy(GURL("http://foo"), std::string(), |
246 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, | 245 &info, callback.callback(), nullptr, |
247 callback.callback(), nullptr, nullptr, BoundNetLog())); | 246 nullptr, BoundNetLog())); |
248 | 247 |
249 // Proxy script fetcher should have a fetch triggered by the first | 248 // Proxy script fetcher should have a fetch triggered by the first |
250 // |ResolveProxy()| request. | 249 // |ResolveProxy()| request. |
251 EXPECT_TRUE(fetcher_->has_pending_request()); | 250 EXPECT_TRUE(fetcher_->has_pending_request()); |
252 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 251 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
253 fetcher_->NotifyFetchCompletion(OK, kThrowingOnLoadPacScript); | 252 fetcher_->NotifyFetchCompletion(OK, kThrowingOnLoadPacScript); |
254 | 253 |
255 network_delegate_.event_waiter().WaitForEvent( | 254 network_delegate_.event_waiter().WaitForEvent( |
256 TestNetworkDelegate::PAC_SCRIPT_ERROR); | 255 TestNetworkDelegate::PAC_SCRIPT_ERROR); |
257 | 256 |
258 EXPECT_THAT(callback.WaitForResult(), IsOk()); | 257 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
259 EXPECT_EQ("DIRECT", info.ToPacString()); | 258 EXPECT_EQ("DIRECT", info.ToPacString()); |
260 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); | 259 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); |
261 | 260 |
262 TestNetLogEntry::List entries; | 261 TestNetLogEntry::List entries; |
263 net_log_.GetEntries(&entries); | 262 net_log_.GetEntries(&entries); |
264 CheckCapturedNetLogEntries(entries); | 263 CheckCapturedNetLogEntries(entries); |
265 } | 264 } |
266 | 265 |
267 } // namespace net | 266 } // namespace net |
OLD | NEW |