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/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
11 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.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/log/net_log.h" | 16 #include "net/log/net_log.h" |
15 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 17 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
16 #include "net/proxy/in_process_mojo_proxy_resolver_factory.h" | 18 #include "net/proxy/in_process_mojo_proxy_resolver_factory.h" |
17 #include "net/proxy/mock_proxy_script_fetcher.h" | 19 #include "net/proxy/mock_proxy_script_fetcher.h" |
18 #include "net/proxy/mojo_proxy_resolver_factory.h" | 20 #include "net/proxy/mojo_proxy_resolver_factory.h" |
19 #include "net/proxy/proxy_config_service_fixed.h" | 21 #include "net/proxy/proxy_config_service_fixed.h" |
20 #include "net/proxy/proxy_service.h" | 22 #include "net/proxy/proxy_service.h" |
| 23 #include "net/test/event_waiter.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 25 #include "url/gurl.h" |
23 | 26 |
24 namespace net { | 27 namespace net { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 const char kPacUrl[] = "http://example.com/proxy.pac"; | 31 const char kPacUrl[] = "http://example.com/proxy.pac"; |
29 const char kSimplePacScript[] = | 32 const char kSimplePacScript[] = |
30 "function FindProxyForURL(url, host) {\n" | 33 "function FindProxyForURL(url, host) {\n" |
31 " return 'PROXY foo:1234';\n" | 34 " return 'PROXY foo:1234';\n" |
32 "}"; | 35 "}"; |
33 const char kDnsResolvePacScript[] = | 36 const char kDnsResolvePacScript[] = |
34 "function FindProxyForURL(url, host) {\n" | 37 "function FindProxyForURL(url, host) {\n" |
35 " if (dnsResolveEx('example.com') != '1.2.3.4')\n" | 38 " if (dnsResolveEx('example.com') != '1.2.3.4')\n" |
36 " return 'DIRECT';\n" | 39 " return 'DIRECT';\n" |
37 " return 'QUIC bar:4321';\n" | 40 " return 'QUIC bar:4321';\n" |
38 "}"; | 41 "}"; |
| 42 const char kErrorPacScript[] = |
| 43 "function FindProxyForURL(url, host) {\n" |
| 44 " throw new Error('test error');\n" |
| 45 "}"; |
| 46 |
| 47 class TestNetworkDelegate : public NetworkDelegateImpl { |
| 48 public: |
| 49 enum Event { |
| 50 PAC_SCRIPT_ERROR, |
| 51 }; |
| 52 |
| 53 EventWaiter<Event>& event_waiter() { return event_waiter_; } |
| 54 |
| 55 void OnPACScriptError(int line_number, const base::string16& error) override; |
| 56 |
| 57 private: |
| 58 EventWaiter<Event> event_waiter_; |
| 59 }; |
| 60 |
| 61 void TestNetworkDelegate::OnPACScriptError(int line_number, |
| 62 const base::string16& error) { |
| 63 event_waiter_.NotifyEvent(PAC_SCRIPT_ERROR); |
| 64 EXPECT_EQ(2, line_number); |
| 65 EXPECT_TRUE(base::UTF16ToUTF8(error).find("test error") != std::string::npos); |
| 66 } |
39 | 67 |
40 } // namespace | 68 } // namespace |
41 | 69 |
42 class ProxyServiceMojoTest : public testing::Test, | 70 class ProxyServiceMojoTest : public testing::Test, |
43 public MojoProxyResolverFactory { | 71 public MojoProxyResolverFactory { |
44 protected: | 72 protected: |
45 void SetUp() override { | 73 void SetUp() override { |
46 mock_host_resolver_.rules()->AddRule("example.com", "1.2.3.4"); | 74 mock_host_resolver_.rules()->AddRule("example.com", "1.2.3.4"); |
47 | 75 |
48 fetcher_ = new MockProxyScriptFetcher; | 76 fetcher_ = new MockProxyScriptFetcher; |
49 proxy_service_.reset(CreateProxyServiceUsingMojoFactory( | 77 proxy_service_.reset(CreateProxyServiceUsingMojoFactory( |
50 this, new ProxyConfigServiceFixed( | 78 this, new ProxyConfigServiceFixed( |
51 ProxyConfig::CreateFromCustomPacURL(GURL(kPacUrl))), | 79 ProxyConfig::CreateFromCustomPacURL(GURL(kPacUrl))), |
52 fetcher_, new DoNothingDhcpProxyScriptFetcher(), &mock_host_resolver_, | 80 fetcher_, new DoNothingDhcpProxyScriptFetcher(), &mock_host_resolver_, |
53 nullptr /* NetLog* */, nullptr /* NetworkDelegate* */)); | 81 nullptr /* NetLog* */, &network_delegate_)); |
54 } | 82 } |
55 | 83 |
56 scoped_ptr<base::ScopedClosureRunner> CreateResolver( | 84 scoped_ptr<base::ScopedClosureRunner> CreateResolver( |
57 const mojo::String& pac_script, | 85 const mojo::String& pac_script, |
58 mojo::InterfaceRequest<interfaces::ProxyResolver> req, | 86 mojo::InterfaceRequest<interfaces::ProxyResolver> req, |
59 interfaces::HostResolverPtr host_resolver, | 87 interfaces::HostResolverPtr host_resolver, |
| 88 interfaces::ProxyResolverErrorObserverPtr error_observer, |
60 interfaces::ProxyResolverFactoryRequestClientPtr client) override { | 89 interfaces::ProxyResolverFactoryRequestClientPtr client) override { |
61 InProcessMojoProxyResolverFactory::GetInstance()->CreateResolver( | 90 InProcessMojoProxyResolverFactory::GetInstance()->CreateResolver( |
62 pac_script, req.Pass(), host_resolver.Pass(), client.Pass()); | 91 pac_script, req.Pass(), host_resolver.Pass(), error_observer.Pass(), |
| 92 client.Pass()); |
63 return make_scoped_ptr( | 93 return make_scoped_ptr( |
64 new base::ScopedClosureRunner(on_delete_closure_.closure())); | 94 new base::ScopedClosureRunner(on_delete_closure_.closure())); |
65 } | 95 } |
66 | 96 |
| 97 TestNetworkDelegate network_delegate_; |
67 MockHostResolver mock_host_resolver_; | 98 MockHostResolver mock_host_resolver_; |
68 MockProxyScriptFetcher* fetcher_; // Owned by |proxy_service_|. | 99 MockProxyScriptFetcher* fetcher_; // Owned by |proxy_service_|. |
69 scoped_ptr<ProxyService> proxy_service_; | 100 scoped_ptr<ProxyService> proxy_service_; |
70 TestClosure on_delete_closure_; | 101 TestClosure on_delete_closure_; |
71 }; | 102 }; |
72 | 103 |
73 TEST_F(ProxyServiceMojoTest, Basic) { | 104 TEST_F(ProxyServiceMojoTest, Basic) { |
74 ProxyInfo info; | 105 ProxyInfo info; |
75 TestCompletionCallback callback; | 106 TestCompletionCallback callback; |
76 EXPECT_EQ(ERR_IO_PENDING, | 107 EXPECT_EQ(ERR_IO_PENDING, |
(...skipping 28 matching lines...) Expand all Loading... |
105 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); | 136 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
106 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); | 137 fetcher_->NotifyFetchCompletion(OK, kDnsResolvePacScript); |
107 | 138 |
108 EXPECT_EQ(OK, callback.WaitForResult()); | 139 EXPECT_EQ(OK, callback.WaitForResult()); |
109 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); | 140 EXPECT_EQ("QUIC bar:4321", info.ToPacString()); |
110 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); | 141 EXPECT_EQ(1u, mock_host_resolver_.num_resolve()); |
111 proxy_service_.reset(); | 142 proxy_service_.reset(); |
112 on_delete_closure_.WaitForResult(); | 143 on_delete_closure_.WaitForResult(); |
113 } | 144 } |
114 | 145 |
| 146 TEST_F(ProxyServiceMojoTest, Error) { |
| 147 ProxyInfo info; |
| 148 TestCompletionCallback callback; |
| 149 EXPECT_EQ(ERR_IO_PENDING, |
| 150 proxy_service_->ResolveProxy(GURL("http://foo"), LOAD_NORMAL, &info, |
| 151 callback.callback(), nullptr, nullptr, |
| 152 BoundNetLog())); |
| 153 |
| 154 // Proxy script fetcher should have a fetch triggered by the first |
| 155 // |ResolveProxy()| request. |
| 156 EXPECT_TRUE(fetcher_->has_pending_request()); |
| 157 EXPECT_EQ(GURL(kPacUrl), fetcher_->pending_request_url()); |
| 158 fetcher_->NotifyFetchCompletion(OK, kErrorPacScript); |
| 159 |
| 160 network_delegate_.event_waiter().WaitForEvent( |
| 161 TestNetworkDelegate::PAC_SCRIPT_ERROR); |
| 162 } |
| 163 |
115 } // namespace net | 164 } // namespace net |
OLD | NEW |