| 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" | 16 #include "net/base/load_flags.h" |
| 17 #include "net/base/network_delegate_impl.h" | 17 #include "net/base/network_delegate_impl.h" |
| 18 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
| 19 #include "net/dns/mock_host_resolver.h" | 19 #include "net/dns/mock_host_resolver.h" |
| 20 #include "net/log/net_log.h" | 20 #include "net/log/net_log.h" |
| 21 #include "net/log/test_net_log.h" | 21 #include "net/log/test_net_log.h" |
| 22 #include "net/log/test_net_log_entry.h" | 22 #include "net/log/test_net_log_entry.h" |
| 23 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 23 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 24 #include "net/proxy/in_process_mojo_proxy_resolver_factory.h" | 24 #include "net/proxy/in_process_mojo_proxy_resolver_factory.h" |
| 25 #include "net/proxy/mock_proxy_script_fetcher.h" | 25 #include "net/proxy/mock_proxy_script_fetcher.h" |
| 26 #include "net/proxy/mojo_proxy_resolver_factory.h" | 26 #include "net/proxy/mojo_proxy_resolver_factory.h" |
| 27 #include "net/proxy/proxy_config_service_fixed.h" | 27 #include "net/proxy/proxy_config_service_fixed.h" |
| 28 #include "net/proxy/proxy_service.h" | 28 #include "net/proxy/proxy_service.h" |
| 29 #include "net/test/event_waiter.h" | 29 #include "net/test/event_waiter.h" |
| 30 #include "net/test/gtest_util.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 33 | 34 |
| 35 using net::test::IsOk; |
| 36 |
| 34 namespace net { | 37 namespace net { |
| 35 | 38 |
| 36 namespace { | 39 namespace { |
| 37 | 40 |
| 38 const char kPacUrl[] = "http://example.com/proxy.pac"; | 41 const char kPacUrl[] = "http://example.com/proxy.pac"; |
| 39 const char kSimplePacScript[] = | 42 const char kSimplePacScript[] = |
| 40 "function FindProxyForURL(url, host) {\n" | 43 "function FindProxyForURL(url, host) {\n" |
| 41 " return 'PROXY foo:1234';\n" | 44 " return 'PROXY foo:1234';\n" |
| 42 "}"; | 45 "}"; |
| 43 const char kDnsResolvePacScript[] = | 46 const char kDnsResolvePacScript[] = |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 proxy_service_->ResolveProxy( | 163 proxy_service_->ResolveProxy( |
| 161 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, | 164 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, |
| 162 callback.callback(), nullptr, nullptr, BoundNetLog())); | 165 callback.callback(), nullptr, nullptr, BoundNetLog())); |
| 163 | 166 |
| 164 // Proxy script fetcher should have a fetch triggered by the first | 167 // Proxy script fetcher should have a fetch triggered by the first |
| 165 // |ResolveProxy()| request. | 168 // |ResolveProxy()| request. |
| 166 EXPECT_TRUE(fetcher_->has_pending_request()); | 169 EXPECT_TRUE(fetcher_->has_pending_request()); |
| 167 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 170 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
| 168 fetcher_->NotifyFetchCompletion(OK, kSimplePacScript); | 171 fetcher_->NotifyFetchCompletion(OK, kSimplePacScript); |
| 169 | 172 |
| 170 EXPECT_EQ(OK, callback.WaitForResult()); | 173 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 171 EXPECT_EQ("PROXY foo:1234", info.ToPacString()); | 174 EXPECT_EQ("PROXY foo:1234", info.ToPacString()); |
| 172 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); | 175 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); |
| 173 proxy_service_.reset(); | 176 proxy_service_.reset(); |
| 174 on_delete_closure_.WaitForResult(); | 177 on_delete_closure_.WaitForResult(); |
| 175 } | 178 } |
| 176 | 179 |
| 177 TEST_F(ProxyServiceMojoTest, DnsResolution) { | 180 TEST_F(ProxyServiceMojoTest, DnsResolution) { |
| 178 ProxyInfo info; | 181 ProxyInfo info; |
| 179 TestCompletionCallback callback; | 182 TestCompletionCallback callback; |
| 180 BoundTestNetLog bound_net_log; | 183 BoundTestNetLog bound_net_log; |
| 181 EXPECT_EQ(ERR_IO_PENDING, | 184 EXPECT_EQ(ERR_IO_PENDING, |
| 182 proxy_service_->ResolveProxy( | 185 proxy_service_->ResolveProxy( |
| 183 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, | 186 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, |
| 184 callback.callback(), nullptr, nullptr, bound_net_log.bound())); | 187 callback.callback(), nullptr, nullptr, bound_net_log.bound())); |
| 185 | 188 |
| 186 // Proxy script fetcher should have a fetch triggered by the first | 189 // Proxy script fetcher should have a fetch triggered by the first |
| 187 // |ResolveProxy()| request. | 190 // |ResolveProxy()| request. |
| 188 EXPECT_TRUE(fetcher_->has_pending_request()); | 191 EXPECT_TRUE(fetcher_->has_pending_request()); |
| 189 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 192 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
| 190 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); | 193 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); |
| 191 | 194 |
| 192 EXPECT_EQ(OK, callback.WaitForResult()); | 195 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 193 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); | 196 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); |
| 194 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); | 197 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); |
| 195 proxy_service_.reset(); | 198 proxy_service_.reset(); |
| 196 on_delete_closure_.WaitForResult(); | 199 on_delete_closure_.WaitForResult(); |
| 197 | 200 |
| 198 TestNetLogEntry::List entries; | 201 TestNetLogEntry::List entries; |
| 199 bound_net_log.GetEntries(&entries); | 202 bound_net_log.GetEntries(&entries); |
| 200 // There should be one entry with type TYPE_HOST_RESOLVER_IMPL_JOB. | 203 // There should be one entry with type TYPE_HOST_RESOLVER_IMPL_JOB. |
| 201 EXPECT_EQ(1, std::count_if(entries.begin(), entries.end(), | 204 EXPECT_EQ(1, std::count_if(entries.begin(), entries.end(), |
| 202 [](const TestNetLogEntry& entry) { | 205 [](const TestNetLogEntry& entry) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 216 | 219 |
| 217 // Proxy script fetcher should have a fetch triggered by the first | 220 // Proxy script fetcher should have a fetch triggered by the first |
| 218 // |ResolveProxy()| request. | 221 // |ResolveProxy()| request. |
| 219 EXPECT_TRUE(fetcher_->has_pending_request()); | 222 EXPECT_TRUE(fetcher_->has_pending_request()); |
| 220 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 223 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
| 221 fetcher_->NotifyFetchCompletion(OK, kThrowingPacScript); | 224 fetcher_->NotifyFetchCompletion(OK, kThrowingPacScript); |
| 222 | 225 |
| 223 network_delegate_.event_waiter().WaitForEvent( | 226 network_delegate_.event_waiter().WaitForEvent( |
| 224 TestNetworkDelegate::PAC_SCRIPT_ERROR); | 227 TestNetworkDelegate::PAC_SCRIPT_ERROR); |
| 225 | 228 |
| 226 EXPECT_EQ(OK, callback.WaitForResult()); | 229 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 227 EXPECT_EQ("DIRECT", info.ToPacString()); | 230 EXPECT_EQ("DIRECT", info.ToPacString()); |
| 228 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); | 231 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); |
| 229 | 232 |
| 230 TestNetLogEntry::List entries; | 233 TestNetLogEntry::List entries; |
| 231 bound_net_log.GetEntries(&entries); | 234 bound_net_log.GetEntries(&entries); |
| 232 CheckCapturedNetLogEntries(entries); | 235 CheckCapturedNetLogEntries(entries); |
| 233 entries.clear(); | 236 entries.clear(); |
| 234 net_log_.GetEntries(&entries); | 237 net_log_.GetEntries(&entries); |
| 235 CheckCapturedNetLogEntries(entries); | 238 CheckCapturedNetLogEntries(entries); |
| 236 } | 239 } |
| 237 | 240 |
| 238 TEST_F(ProxyServiceMojoTest, ErrorOnInitialization) { | 241 TEST_F(ProxyServiceMojoTest, ErrorOnInitialization) { |
| 239 ProxyInfo info; | 242 ProxyInfo info; |
| 240 TestCompletionCallback callback; | 243 TestCompletionCallback callback; |
| 241 EXPECT_EQ(ERR_IO_PENDING, | 244 EXPECT_EQ(ERR_IO_PENDING, |
| 242 proxy_service_->ResolveProxy( | 245 proxy_service_->ResolveProxy( |
| 243 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, | 246 GURL("http://foo"), std::string(), LOAD_NORMAL, &info, |
| 244 callback.callback(), nullptr, nullptr, BoundNetLog())); | 247 callback.callback(), nullptr, nullptr, BoundNetLog())); |
| 245 | 248 |
| 246 // Proxy script fetcher should have a fetch triggered by the first | 249 // Proxy script fetcher should have a fetch triggered by the first |
| 247 // |ResolveProxy()| request. | 250 // |ResolveProxy()| request. |
| 248 EXPECT_TRUE(fetcher_->has_pending_request()); | 251 EXPECT_TRUE(fetcher_->has_pending_request()); |
| 249 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 252 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
| 250 fetcher_->NotifyFetchCompletion(OK, kThrowingOnLoadPacScript); | 253 fetcher_->NotifyFetchCompletion(OK, kThrowingOnLoadPacScript); |
| 251 | 254 |
| 252 network_delegate_.event_waiter().WaitForEvent( | 255 network_delegate_.event_waiter().WaitForEvent( |
| 253 TestNetworkDelegate::PAC_SCRIPT_ERROR); | 256 TestNetworkDelegate::PAC_SCRIPT_ERROR); |
| 254 | 257 |
| 255 EXPECT_EQ(OK, callback.WaitForResult()); | 258 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 256 EXPECT_EQ("DIRECT", info.ToPacString()); | 259 EXPECT_EQ("DIRECT", info.ToPacString()); |
| 257 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); | 260 EXPECT_EQ(0u, mock_host_resolver_.num_resolve()); |
| 258 | 261 |
| 259 TestNetLogEntry::List entries; | 262 TestNetLogEntry::List entries; |
| 260 net_log_.GetEntries(&entries); | 263 net_log_.GetEntries(&entries); |
| 261 CheckCapturedNetLogEntries(entries); | 264 CheckCapturedNetLogEntries(entries); |
| 262 } | 265 } |
| 263 | 266 |
| 264 } // namespace net | 267 } // namespace net |
| OLD | NEW |