| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/dbus/services/proxy_resolution_service_provider.h" | 5 #include "chromeos/dbus/services/proxy_resolution_service_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "chromeos/dbus/services/service_provider_test_helper.h" | 13 #include "chromeos/dbus/services/service_provider_test_helper.h" |
| 14 #include "dbus/message.h" | 14 #include "dbus/message.h" |
| 15 #include "net/base/net_errors.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 17 | 18 |
| 18 namespace chromeos { | 19 namespace chromeos { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus | 23 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus |
| 23 // signal, to the following signal interface and the signal name. | 24 // signal, to the following signal interface and the signal name. |
| 24 const char kReturnSignalInterface[] = "org.chromium.TestInterface"; | 25 const char kReturnSignalInterface[] = "org.chromium.TestInterface"; |
| 25 const char kReturnSignalName[] = "TestSignal"; | 26 const char kReturnSignalName[] = "TestSignal"; |
| 26 | 27 |
| 27 // Test ProxyResolverDelegate implementation. | 28 // Test ProxyResolverDelegate implementation. |
| 28 class TestProxyResolverDelegate : public ProxyResolverDelegate { | 29 class TestProxyResolverDelegate : public ProxyResolverDelegate { |
| 29 public: | 30 public: |
| 30 explicit TestProxyResolverDelegate( | 31 explicit TestProxyResolverDelegate( |
| 31 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) | 32 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) |
| 32 : request_context_getter_( | 33 : request_context_getter_( |
| 33 new net::TestURLRequestContextGetter(network_task_runner)) {} | 34 new net::TestURLRequestContextGetter(network_task_runner)) { |
| 34 | 35 // Use direct connections by default. |
| 36 proxy_info_.UseDirect(); |
| 37 } |
| 35 ~TestProxyResolverDelegate() override {} | 38 ~TestProxyResolverDelegate() override {} |
| 36 | 39 |
| 37 // ProxyResolverDelegate override. | 40 void set_async(bool async) { async_ = async; } |
| 41 void set_result(net::Error result) { result_ = result; } |
| 42 |
| 43 const net::ProxyInfo& proxy_info() const { return proxy_info_; } |
| 44 net::ProxyInfo* mutable_proxy_info() { return &proxy_info_; } |
| 45 |
| 46 // ProxyResolverDelegate: |
| 38 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override { | 47 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override { |
| 39 return request_context_getter_; | 48 return request_context_getter_; |
| 40 } | 49 } |
| 50 int ResolveProxy(net::ProxyService* proxy_service, |
| 51 const GURL& url, |
| 52 net::ProxyInfo* results, |
| 53 const net::CompletionCallback& callback) override { |
| 54 EXPECT_EQ(proxy_service, |
| 55 request_context_getter_->GetURLRequestContext()->proxy_service()); |
| 56 *results = proxy_info_; |
| 57 |
| 58 if (!async_) |
| 59 return result_; |
| 60 |
| 61 request_context_getter_->GetNetworkTaskRunner()->PostTask( |
| 62 FROM_HERE, base::Bind(callback, result_)); |
| 63 return net::ERR_IO_PENDING; |
| 64 } |
| 41 | 65 |
| 42 private: | 66 private: |
| 67 // Should ResolveProxy() run asynchronously (rather than synchronously)? |
| 68 bool async_ = false; |
| 69 |
| 70 // Final result for ResolveProxy() to return. |
| 71 net::Error result_ = net::OK; |
| 72 |
| 73 // Proxy info for ResolveProxy() to return. |
| 74 net::ProxyInfo proxy_info_; |
| 75 |
| 43 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 76 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 44 | 77 |
| 45 DISALLOW_COPY_AND_ASSIGN(TestProxyResolverDelegate); | 78 DISALLOW_COPY_AND_ASSIGN(TestProxyResolverDelegate); |
| 46 }; | 79 }; |
| 47 | 80 |
| 48 } // namespace | 81 } // namespace |
| 49 | 82 |
| 50 class ProxyResolutionServiceProviderTest : public testing::Test { | 83 class ProxyResolutionServiceProviderTest : public testing::Test { |
| 51 public: | 84 public: |
| 52 void SetUp() override { | 85 void SetUp() override { |
| 53 // Create the proxy resolution service with the mock bus and the mock | 86 // Create the proxy resolution service with the mock bus and the mock |
| 54 // resolver injected. | 87 // resolver injected. |
| 88 delegate_ = |
| 89 new TestProxyResolverDelegate(base::ThreadTaskRunnerHandle::Get()); |
| 55 service_provider_.reset(ProxyResolutionServiceProvider::Create( | 90 service_provider_.reset(ProxyResolutionServiceProvider::Create( |
| 56 base::MakeUnique<TestProxyResolverDelegate>( | 91 std::unique_ptr<TestProxyResolverDelegate>(delegate_))); |
| 57 base::ThreadTaskRunnerHandle::Get()))); | |
| 58 | 92 |
| 59 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get()); | 93 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get()); |
| 60 | 94 |
| 61 // Connect to the signal that will be sent to kReturnSignalInterface and | 95 // Connect to the signal that will be sent to kReturnSignalInterface and |
| 62 // kReturnSignalName. ResolveNetworkProxy() will send the result as a | 96 // kReturnSignalName. ResolveNetworkProxy() will send the result as a |
| 63 // signal. OnSignalReceived() will be called upon the delivery. | 97 // signal. OnSignalReceived() will be called upon the delivery. |
| 64 test_helper_.SetUpReturnSignal( | 98 test_helper_.SetUpReturnSignal( |
| 65 kReturnSignalInterface, | 99 kReturnSignalInterface, |
| 66 kReturnSignalName, | 100 kReturnSignalName, |
| 67 base::Bind(&ProxyResolutionServiceProviderTest::OnSignalReceived, | 101 base::Bind(&ProxyResolutionServiceProviderTest::OnSignalReceived, |
| 68 base::Unretained(this)), | 102 base::Unretained(this)), |
| 69 base::Bind(&ProxyResolutionServiceProviderTest::OnConnectedToSignal, | 103 base::Bind(&ProxyResolutionServiceProviderTest::OnConnectedToSignal, |
| 70 base::Unretained(this))); | 104 base::Unretained(this))); |
| 71 } | 105 } |
| 72 | 106 |
| 73 void TearDown() override { | 107 void TearDown() override { |
| 74 test_helper_.TearDown(); | 108 test_helper_.TearDown(); |
| 75 service_provider_.reset(); | 109 service_provider_.reset(); |
| 76 } | 110 } |
| 77 | 111 |
| 78 protected: | 112 protected: |
| 113 // Arguments extracted from a D-Bus signal. |
| 114 struct SignalInfo { |
| 115 std::string source_url; |
| 116 std::string proxy_info; |
| 117 std::string error_message; |
| 118 }; |
| 119 |
| 79 // Called when a signal is received. | 120 // Called when a signal is received. |
| 80 void OnSignalReceived(dbus::Signal* signal) { | 121 void OnSignalReceived(dbus::Signal* signal) { |
| 81 EXPECT_EQ(kReturnSignalInterface, signal->GetInterface()); | 122 EXPECT_EQ(kReturnSignalInterface, signal->GetInterface()); |
| 82 EXPECT_EQ(kReturnSignalName, signal->GetMember()); | 123 EXPECT_EQ(kReturnSignalName, signal->GetMember()); |
| 83 | 124 |
| 125 ASSERT_FALSE(signal_); |
| 126 signal_ = base::MakeUnique<SignalInfo>(); |
| 127 |
| 84 // The signal should contain three strings. | 128 // The signal should contain three strings. |
| 85 dbus::MessageReader reader(signal); | 129 dbus::MessageReader reader(signal); |
| 86 EXPECT_TRUE(reader.PopString(&source_url_)); | 130 EXPECT_TRUE(reader.PopString(&signal_->source_url)); |
| 87 EXPECT_TRUE(reader.PopString(&proxy_info_)); | 131 EXPECT_TRUE(reader.PopString(&signal_->proxy_info)); |
| 88 EXPECT_TRUE(reader.PopString(&error_message_)); | 132 EXPECT_TRUE(reader.PopString(&signal_->error_message)); |
| 89 } | 133 } |
| 90 | 134 |
| 91 // Called when connected to a signal. | 135 // Called when connected to a signal. |
| 92 void OnConnectedToSignal(const std::string& signal_interface, | 136 void OnConnectedToSignal(const std::string& signal_interface, |
| 93 const std::string& signal_name, | 137 const std::string& signal_name, |
| 94 bool success){ | 138 bool success){ |
| 95 EXPECT_EQ(kReturnSignalInterface, signal_interface); | 139 EXPECT_EQ(kReturnSignalInterface, signal_interface); |
| 96 EXPECT_EQ(kReturnSignalName, signal_name); | 140 EXPECT_EQ(kReturnSignalName, signal_name); |
| 97 | |
| 98 EXPECT_TRUE(success); | 141 EXPECT_TRUE(success); |
| 99 } | 142 } |
| 100 | 143 |
| 101 std::string source_url_; | 144 // Makes a D-Bus call to |service_provider_|'s ResolveNetworkProxy method. |
| 102 std::string proxy_info_; | 145 // |response_out| is updated to hold the response, and |signal_out| is updated |
| 103 std::string error_message_; | 146 // to hold information about the emitted signal, if any. |
| 147 void CallMethod(const std::string& source_url, |
| 148 std::unique_ptr<dbus::Response>* response_out, |
| 149 std::unique_ptr<SignalInfo>* signal_out) { |
| 150 dbus::MethodCall method_call(kLibCrosServiceInterface, |
| 151 kResolveNetworkProxy); |
| 152 dbus::MessageWriter writer(&method_call); |
| 153 writer.AppendString(source_url); |
| 154 writer.AppendString(kReturnSignalInterface); |
| 155 writer.AppendString(kReturnSignalName); |
| 156 |
| 157 *response_out = test_helper_.CallMethod(&method_call); |
| 158 base::RunLoop().RunUntilIdle(); |
| 159 *signal_out = std::move(signal_); |
| 160 } |
| 161 |
| 162 // Information about the last D-Bus signal received by OnSignalReceived(). |
| 163 std::unique_ptr<SignalInfo> signal_; |
| 164 |
| 104 ServiceProviderTestHelper test_helper_; | 165 ServiceProviderTestHelper test_helper_; |
| 105 std::unique_ptr<CrosDBusService::ServiceProviderInterface> service_provider_; | 166 std::unique_ptr<CrosDBusService::ServiceProviderInterface> service_provider_; |
| 167 TestProxyResolverDelegate* delegate_; // Owned by service_provider_. |
| 106 }; | 168 }; |
| 107 | 169 |
| 108 TEST_F(ProxyResolutionServiceProviderTest, ResolveProxy) { | 170 // Tests that synchronously-resolved proxy information is returned. |
| 171 TEST_F(ProxyResolutionServiceProviderTest, ResolveProxySync) { |
| 109 const char kSourceURL[] = "http://www.gmail.com/"; | 172 const char kSourceURL[] = "http://www.gmail.com/"; |
| 110 | 173 |
| 111 // Create a method call to resolve proxy config for kSourceURL. | 174 std::unique_ptr<dbus::Response> response; |
| 112 dbus::MethodCall method_call(kLibCrosServiceInterface, kResolveNetworkProxy); | 175 std::unique_ptr<SignalInfo> signal; |
| 113 dbus::MessageWriter writer(&method_call); | 176 CallMethod(kSourceURL, &response, &signal); |
| 114 writer.AppendString(kSourceURL); | |
| 115 writer.AppendString(kReturnSignalInterface); | |
| 116 writer.AppendString(kReturnSignalName); | |
| 117 | |
| 118 // Call the ResolveNetworkProxy method. | |
| 119 std::unique_ptr<dbus::Response> response( | |
| 120 test_helper_.CallMethod(&method_call)); | |
| 121 base::RunLoop().RunUntilIdle(); | |
| 122 | 177 |
| 123 // An empty response should be returned. | 178 // An empty response should be returned. |
| 124 ASSERT_TRUE(response.get()); | 179 ASSERT_TRUE(response); |
| 125 dbus::MessageReader reader(response.get()); | 180 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); |
| 126 EXPECT_FALSE(reader.HasMoreData()); | |
| 127 | 181 |
| 128 // Confirm that the signal is received successfully. | 182 // Confirm that the signal is received successfully. |
| 129 EXPECT_EQ(kSourceURL, source_url_); | 183 ASSERT_TRUE(signal); |
| 130 EXPECT_EQ("DIRECT", proxy_info_); | 184 EXPECT_EQ(kSourceURL, signal->source_url); |
| 131 EXPECT_EQ("", error_message_); | 185 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); |
| 186 EXPECT_EQ("", signal->error_message); |
| 187 } |
| 188 |
| 189 // Tests that asynchronously-resolved proxy information is returned. |
| 190 TEST_F(ProxyResolutionServiceProviderTest, ResolveProxyAsync) { |
| 191 const char kSourceURL[] = "http://www.gmail.com/"; |
| 192 delegate_->set_async(true); |
| 193 delegate_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080"); |
| 194 |
| 195 std::unique_ptr<dbus::Response> response; |
| 196 std::unique_ptr<SignalInfo> signal; |
| 197 CallMethod(kSourceURL, &response, &signal); |
| 198 |
| 199 // An empty response should be returned. |
| 200 ASSERT_TRUE(response); |
| 201 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); |
| 202 |
| 203 // Confirm that the signal is received successfully. |
| 204 ASSERT_TRUE(signal); |
| 205 EXPECT_EQ(kSourceURL, signal->source_url); |
| 206 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); |
| 207 EXPECT_EQ("", signal->error_message); |
| 208 } |
| 209 |
| 210 // Tests that an error received during proxy resolution is returned. |
| 211 TEST_F(ProxyResolutionServiceProviderTest, ResolveProxyError) { |
| 212 const char kSourceURL[] = "http://www.gmail.com/"; |
| 213 const net::Error kError = net::ERR_FAILED; |
| 214 delegate_->set_result(kError); |
| 215 |
| 216 std::unique_ptr<dbus::Response> response; |
| 217 std::unique_ptr<SignalInfo> signal; |
| 218 CallMethod(kSourceURL, &response, &signal); |
| 219 |
| 220 // An empty response should be returned. |
| 221 ASSERT_TRUE(response); |
| 222 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); |
| 223 |
| 224 // The signal should contain an error. |
| 225 ASSERT_TRUE(signal); |
| 226 EXPECT_EQ(kSourceURL, signal->source_url); |
| 227 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); |
| 228 EXPECT_EQ(net::ErrorToString(kError), signal->error_message); |
| 132 } | 229 } |
| 133 | 230 |
| 134 } // namespace chromeos | 231 } // namespace chromeos |
| OLD | NEW |