| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "net/base/load_flags.h" | 11 #include "net/base/load_flags.h" |
| 11 #include "net/base/net_log.h" | 12 #include "net/base/net_log.h" |
| 13 #include "net/base/network_delegate_impl.h" |
| 12 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
| 13 #include "net/dns/mock_host_resolver.h" | 15 #include "net/dns/mock_host_resolver.h" |
| 14 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 16 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 15 #include "net/proxy/mock_proxy_script_fetcher.h" | 17 #include "net/proxy/mock_proxy_script_fetcher.h" |
| 16 #include "net/proxy/proxy_config_service_fixed.h" | 18 #include "net/proxy/proxy_config_service_fixed.h" |
| 17 #include "net/proxy/proxy_service.h" | 19 #include "net/proxy/proxy_service.h" |
| 20 #include "net/test/event_waiter.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 20 | 23 |
| 21 namespace net { | 24 namespace net { |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 const char kPacUrl[] = "http://example.com/proxy.pac"; | 28 const char kPacUrl[] = "http://example.com/proxy.pac"; |
| 26 const char kSimplePacScript[] = | 29 const char kSimplePacScript[] = |
| 27 "function FindProxyForURL(url, host) {\n" | 30 "function FindProxyForURL(url, host) {\n" |
| 28 " return 'PROXY foo:1234';\n" | 31 " return 'PROXY foo:1234';\n" |
| 29 "}"; | 32 "}"; |
| 30 const char kDnsResolvePacScript[] = | 33 const char kDnsResolvePacScript[] = |
| 31 "function FindProxyForURL(url, host) {\n" | 34 "function FindProxyForURL(url, host) {\n" |
| 32 " if (dnsResolveEx('example.com') != '1.2.3.4')\n" | 35 " if (dnsResolveEx('example.com') != '1.2.3.4')\n" |
| 33 " return 'DIRECT';\n" | 36 " return 'DIRECT';\n" |
| 34 " return 'QUIC bar:4321';\n" | 37 " return 'QUIC bar:4321';\n" |
| 35 "}"; | 38 "}"; |
| 39 const char kErrorPacScript[] = |
| 40 "function FindProxyForURL(url, host) {\n" |
| 41 " throw new Error('test error');\n" |
| 42 "}"; |
| 43 |
| 44 class TestNetworkDelegate : public NetworkDelegateImpl { |
| 45 public: |
| 46 enum Event { |
| 47 PAC_SCRIPT_ERROR, |
| 48 }; |
| 49 |
| 50 EventWaiter<Event>& event_waiter() { return event_waiter_; } |
| 51 |
| 52 void OnPACScriptError(int line_number, const base::string16& error) override; |
| 53 |
| 54 private: |
| 55 EventWaiter<Event> event_waiter_; |
| 56 }; |
| 57 |
| 58 void TestNetworkDelegate::OnPACScriptError(int line_number, |
| 59 const base::string16& error) { |
| 60 event_waiter_.NotifyEvent(PAC_SCRIPT_ERROR); |
| 61 EXPECT_EQ(2, line_number); |
| 62 EXPECT_TRUE(base::UTF16ToUTF8(error).find("test error") != std::string::npos); |
| 63 } |
| 36 | 64 |
| 37 } // namespace | 65 } // namespace |
| 38 | 66 |
| 39 class ProxyServiceMojoTest : public testing::Test { | 67 class ProxyServiceMojoTest : public testing::Test { |
| 40 protected: | 68 protected: |
| 41 void SetUp() override { | 69 void SetUp() override { |
| 42 mock_host_resolver_.rules()->AddRule("example.com", "1.2.3.4"); | 70 mock_host_resolver_.rules()->AddRule("example.com", "1.2.3.4"); |
| 43 | 71 |
| 44 fetcher_ = new MockProxyScriptFetcher; | 72 fetcher_ = new MockProxyScriptFetcher; |
| 45 proxy_service_.reset(CreateProxyServiceUsingMojoInProcess( | 73 proxy_service_.reset(CreateProxyServiceUsingMojoInProcess( |
| 46 new ProxyConfigServiceFixed( | 74 new ProxyConfigServiceFixed( |
| 47 ProxyConfig::CreateFromCustomPacURL(GURL(kPacUrl))), | 75 ProxyConfig::CreateFromCustomPacURL(GURL(kPacUrl))), |
| 48 fetcher_, new DoNothingDhcpProxyScriptFetcher(), &mock_host_resolver_, | 76 fetcher_, new DoNothingDhcpProxyScriptFetcher(), &mock_host_resolver_, |
| 49 nullptr /* NetLog* */, nullptr /* NetworkDelegate* */)); | 77 nullptr /* NetLog* */, &network_delegate_)); |
| 50 } | 78 } |
| 51 | 79 |
| 80 TestNetworkDelegate network_delegate_; |
| 52 MockHostResolver mock_host_resolver_; | 81 MockHostResolver mock_host_resolver_; |
| 53 MockProxyScriptFetcher* fetcher_; // Owned by |proxy_service_|. | 82 MockProxyScriptFetcher* fetcher_; // Owned by |proxy_service_|. |
| 54 scoped_ptr<ProxyService> proxy_service_; | 83 scoped_ptr<ProxyService> proxy_service_; |
| 55 }; | 84 }; |
| 56 | 85 |
| 57 TEST_F(ProxyServiceMojoTest, Basic) { | 86 TEST_F(ProxyServiceMojoTest, Basic) { |
| 58 ProxyInfo info; | 87 ProxyInfo info; |
| 59 TestCompletionCallback callback; | 88 TestCompletionCallback callback; |
| 60 EXPECT_EQ(ERR_IO_PENDING, | 89 EXPECT_EQ(ERR_IO_PENDING, |
| 61 proxy_service_->ResolveProxy(GURL("http://foo"), LOAD_NORMAL, &info, | 90 proxy_service_->ResolveProxy(GURL("http://foo"), LOAD_NORMAL, &info, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 85 // |ResolveProxy()| request. | 114 // |ResolveProxy()| request. |
| 86 EXPECT_TRUE(fetcher_->has_pending_request()); | 115 EXPECT_TRUE(fetcher_->has_pending_request()); |
| 87 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 116 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
| 88 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); | 117 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); |
| 89 | 118 |
| 90 EXPECT_EQ(OK, callback.WaitForResult()); | 119 EXPECT_EQ(OK, callback.WaitForResult()); |
| 91 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); | 120 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); |
| 92 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); | 121 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); |
| 93 } | 122 } |
| 94 | 123 |
| 124 TEST_F(ProxyServiceMojoTest, Error) { |
| 125 ProxyInfo info; |
| 126 TestCompletionCallback callback; |
| 127 EXPECT_EQ(ERR_IO_PENDING, |
| 128 proxy_service_->ResolveProxy(GURL("http://foo"), LOAD_NORMAL, &info, |
| 129 callback.callback(), nullptr, nullptr, |
| 130 BoundNetLog())); |
| 131 |
| 132 // Proxy script fetcher should have a fetch triggered by the first |
| 133 // |ResolveProxy()| request. |
| 134 EXPECT_TRUE(fetcher_->has_pending_request()); |
| 135 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
| 136 fetcher_->NotifyFetchCompletion(OK, kErrorPacScript); |
| 137 |
| 138 network_delegate_.event_waiter().WaitForEvent( |
| 139 TestNetworkDelegate::PAC_SCRIPT_ERROR); |
| 140 } |
| 141 |
| 95 } // namespace net | 142 } // namespace net |
| OLD | NEW |