Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chromeos/dbus/services/proxy_resolution_service_provider_unittest.cc

Issue 2787253002: chromeos: Simplify D-Bus proxy provider (but not tests). (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/base/net_errors.h"
16 #include "net/proxy/mock_proxy_resolver.h"
17 #include "net/proxy/proxy_config_service_fixed.h"
18 #include "net/proxy/proxy_info.h"
19 #include "net/proxy/proxy_resolver.h"
20 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_test_util.h" 21 #include "net/url_request/url_request_test_util.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h" 22 #include "third_party/cros_system_api/dbus/service_constants.h"
18 23
19 namespace chromeos { 24 namespace chromeos {
20 25
21 namespace { 26 namespace {
22 27
23 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus 28 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus
24 // signal, to the following signal interface and the signal name. 29 // signal, to the following signal interface and the signal name.
25 const char kReturnSignalInterface[] = "org.chromium.TestInterface"; 30 const char kReturnSignalInterface[] = "org.chromium.TestInterface";
26 const char kReturnSignalName[] = "TestSignal"; 31 const char kReturnSignalName[] = "TestSignal";
27 32
33 // Trivial net::ProxyResolver implementation that returns canned data either
34 // synchronously or asynchronously.
35 class TestProxyResolver : public net::ProxyResolver {
36 public:
37 explicit TestProxyResolver(
38 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
39 : network_task_runner_(network_task_runner) {
40 proxy_info_.UseDirect();
41 }
42 ~TestProxyResolver() override = default;
43
44 const net::ProxyInfo& proxy_info() const { return proxy_info_; }
45 net::ProxyInfo* mutable_proxy_info() { return &proxy_info_; }
46
47 void set_error(net::Error error) { error_ = error; }
48 void set_async(bool async) { async_ = async; }
49
50 // net::ProxyResolver:
51 int GetProxyForURL(const GURL& url,
52 net::ProxyInfo* results,
53 const net::CompletionCallback& callback,
54 std::unique_ptr<Request>* request,
55 const net::NetLogWithSource& net_log) override {
56 CHECK(network_task_runner_->BelongsToCurrentThread());
57 results->Use(proxy_info_);
James Cook 2017/03/31 17:33:10 Should this only happen in the non-async case? I'm
Daniel Erat 2017/03/31 17:46:37 nope, this is how the results are returned for asy
58 if (!async_)
59 return error_;
60
61 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
62 base::Bind(callback, error_));
63 return net::ERR_IO_PENDING;
64 }
65
66 private:
67 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
68
69 // Final result for GetProxyForURL() to return.
70 net::Error error_ = net::OK;
71
72 // Proxy info for GetProxyForURL() to return.
73 net::ProxyInfo proxy_info_;
74
75 // If true, GetProxyForURL() replies asynchronously rather than synchronously.
76 bool async_ = false;
77
78 DISALLOW_COPY_AND_ASSIGN(TestProxyResolver);
79 };
80
81 // Trivial net::ProxyResolverFactory implementation that synchronously creates
82 // net::ForwardingProxyResolvers that forward to a single passed-in resolver.
83 class TestProxyResolverFactory : public net::ProxyResolverFactory {
84 public:
85 // Ownership of |resolver| remains with the caller. |resolver| must outlive
86 // the forwarding resolvers returned by CreateProxyResolver().
87 explicit TestProxyResolverFactory(net::ProxyResolver* resolver)
88 : net::ProxyResolverFactory(false /* expects_pac_bytes */),
89 resolver_(resolver) {}
90 ~TestProxyResolverFactory() override = default;
91
92 // net::ProxyResolverFactory:
93 int CreateProxyResolver(
94 const scoped_refptr<net::ProxyResolverScriptData>& pac_script,
95 std::unique_ptr<net::ProxyResolver>* resolver,
96 const net::CompletionCallback& callback,
97 std::unique_ptr<Request>* request) override {
98 *resolver = base::MakeUnique<net::ForwardingProxyResolver>(resolver_);
99 return net::OK;
100 }
101
102 private:
103 net::ProxyResolver* resolver_; // Not owned.
104
105 DISALLOW_COPY_AND_ASSIGN(TestProxyResolverFactory);
106 };
107
28 // Test ProxyResolutionServiceProvider::Delegate implementation. 108 // Test ProxyResolutionServiceProvider::Delegate implementation.
29 class TestDelegate : public ProxyResolutionServiceProvider::Delegate { 109 class TestDelegate : public ProxyResolutionServiceProvider::Delegate {
30 public: 110 public:
31 explicit TestDelegate( 111 explicit TestDelegate(
32 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) 112 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
33 : request_context_getter_( 113 net::ProxyResolver* proxy_resolver)
114 : proxy_resolver_(proxy_resolver),
115 context_getter_(
34 new net::TestURLRequestContextGetter(network_task_runner)) { 116 new net::TestURLRequestContextGetter(network_task_runner)) {
35 // Use direct connections by default. 117 // The config's autodetect property needs to be set in order for
36 proxy_info_.UseDirect(); 118 // net::ProxyService to send requests to our resolver.
119 net::ProxyConfig config = net::ProxyConfig::CreateAutoDetect();
120 proxy_service_ = base::MakeUnique<net::ProxyService>(
121 base::MakeUnique<net::ProxyConfigServiceFixed>(config),
122 base::MakeUnique<TestProxyResolverFactory>(proxy_resolver_),
123 nullptr /* net_log */);
124 context_getter_->GetURLRequestContext()->set_proxy_service(
125 proxy_service_.get());
37 } 126 }
38 ~TestDelegate() override = default; 127 ~TestDelegate() override = default;
39 128
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 // ProxyResolutionServiceProvider::Delegate: 129 // ProxyResolutionServiceProvider::Delegate:
47 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override { 130 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override {
48 return request_context_getter_; 131 return context_getter_;
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 } 132 }
65 133
66 private: 134 private:
67 // Should ResolveProxy() run asynchronously (rather than synchronously)? 135 net::ProxyResolver* proxy_resolver_; // Not owned.
68 bool async_ = false; 136 std::unique_ptr<net::ProxyService> proxy_service_;
69 137 scoped_refptr<net::TestURLRequestContextGetter> context_getter_;
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
76 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
77 138
78 DISALLOW_COPY_AND_ASSIGN(TestDelegate); 139 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
79 }; 140 };
80 141
81 } // namespace 142 } // namespace
82 143
83 class ProxyResolutionServiceProviderTest : public testing::Test { 144 class ProxyResolutionServiceProviderTest : public testing::Test {
84 public: 145 public:
146 ProxyResolutionServiceProviderTest() = default;
147 ~ProxyResolutionServiceProviderTest() override = default;
148
149 // testing::Test:
85 void SetUp() override { 150 void SetUp() override {
86 // Create the proxy resolution service with the mock bus and the mock 151 proxy_resolver_ = base::MakeUnique<TestProxyResolver>(
87 // resolver injected. 152 base::ThreadTaskRunnerHandle::Get());
88 delegate_ = new TestDelegate(base::ThreadTaskRunnerHandle::Get());
89 service_provider_ = base::MakeUnique<ProxyResolutionServiceProvider>( 153 service_provider_ = base::MakeUnique<ProxyResolutionServiceProvider>(
90 std::unique_ptr<TestDelegate>(delegate_)); 154 base::MakeUnique<TestDelegate>(base::ThreadTaskRunnerHandle::Get(),
91 155 proxy_resolver_.get()));
92 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get()); 156 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get());
93 } 157 }
94
95 void TearDown() override { 158 void TearDown() override {
96 test_helper_.TearDown(); 159 test_helper_.TearDown();
97 service_provider_.reset();
98 } 160 }
99 161
100 protected: 162 protected:
101 // Arguments extracted from a D-Bus signal. 163 // Arguments extracted from a D-Bus signal.
102 struct SignalInfo { 164 struct SignalInfo {
103 std::string source_url; 165 std::string source_url;
104 std::string proxy_info; 166 std::string proxy_info;
105 std::string error_message; 167 std::string error_message;
106 }; 168 };
107 169
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } 220 }
159 221
160 *response_out = test_helper_.CallMethod(&method_call); 222 *response_out = test_helper_.CallMethod(&method_call);
161 base::RunLoop().RunUntilIdle(); 223 base::RunLoop().RunUntilIdle();
162 *signal_out = std::move(signal_); 224 *signal_out = std::move(signal_);
163 } 225 }
164 226
165 // Information about the last D-Bus signal received by OnSignalReceived(). 227 // Information about the last D-Bus signal received by OnSignalReceived().
166 std::unique_ptr<SignalInfo> signal_; 228 std::unique_ptr<SignalInfo> signal_;
167 229
230 std::unique_ptr<TestProxyResolver> proxy_resolver_;
231 std::unique_ptr<ProxyResolutionServiceProvider> service_provider_;
168 ServiceProviderTestHelper test_helper_; 232 ServiceProviderTestHelper test_helper_;
169 std::unique_ptr<ProxyResolutionServiceProvider> service_provider_;
170 TestDelegate* delegate_; // Owned by |service_provider_|.
171 }; 233 };
172 234
173 // Tests that synchronously-resolved proxy information is returned via a signal. 235 // Tests that synchronously-resolved proxy information is returned via a signal.
174 TEST_F(ProxyResolutionServiceProviderTest, SignalSync) { 236 TEST_F(ProxyResolutionServiceProviderTest, SignalSync) {
175 const char kSourceURL[] = "http://www.gmail.com/"; 237 const char kSourceURL[] = "http://www.gmail.com/";
176 238
177 std::unique_ptr<dbus::Response> response; 239 std::unique_ptr<dbus::Response> response;
178 std::unique_ptr<SignalInfo> signal; 240 std::unique_ptr<SignalInfo> signal;
179 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); 241 CallMethod(kSourceURL, true /* request_signal */, &response, &signal);
180 242
181 // An empty response should be returned. 243 // An empty response should be returned.
182 ASSERT_TRUE(response); 244 ASSERT_TRUE(response);
183 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); 245 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData());
184 246
185 // Confirm that the signal is received successfully. 247 // Confirm that the signal is received successfully.
186 ASSERT_TRUE(signal); 248 ASSERT_TRUE(signal);
187 EXPECT_EQ(kSourceURL, signal->source_url); 249 EXPECT_EQ(kSourceURL, signal->source_url);
188 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); 250 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), signal->proxy_info);
189 EXPECT_EQ("", signal->error_message); 251 EXPECT_EQ("", signal->error_message);
190 } 252 }
191 253
192 // Tests that asynchronously-resolved proxy information is returned via a 254 // Tests that asynchronously-resolved proxy information is returned via a
193 // signal. 255 // signal.
194 TEST_F(ProxyResolutionServiceProviderTest, SignalAsync) { 256 TEST_F(ProxyResolutionServiceProviderTest, SignalAsync) {
195 const char kSourceURL[] = "http://www.gmail.com/"; 257 const char kSourceURL[] = "http://www.gmail.com/";
196 delegate_->set_async(true); 258 proxy_resolver_->set_async(true);
197 delegate_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080"); 259 proxy_resolver_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080");
198 260
199 std::unique_ptr<dbus::Response> response; 261 std::unique_ptr<dbus::Response> response;
200 std::unique_ptr<SignalInfo> signal; 262 std::unique_ptr<SignalInfo> signal;
201 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); 263 CallMethod(kSourceURL, true /* request_signal */, &response, &signal);
202 264
203 // An empty response should be returned. 265 // An empty response should be returned.
204 ASSERT_TRUE(response); 266 ASSERT_TRUE(response);
205 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); 267 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData());
206 268
207 // Confirm that the signal is received successfully. 269 // Confirm that the signal is received successfully.
208 ASSERT_TRUE(signal); 270 ASSERT_TRUE(signal);
209 EXPECT_EQ(kSourceURL, signal->source_url); 271 EXPECT_EQ(kSourceURL, signal->source_url);
210 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); 272 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), signal->proxy_info);
211 EXPECT_EQ("", signal->error_message); 273 EXPECT_EQ("", signal->error_message);
212 } 274 }
213 275
214 // Tests that an error received during proxy resolution is returned via a 276 // Tests that an error received during proxy resolution is returned via a
215 // signal. 277 // signal.
216 TEST_F(ProxyResolutionServiceProviderTest, SignalError) { 278 TEST_F(ProxyResolutionServiceProviderTest, SignalError) {
217 const char kSourceURL[] = "http://www.gmail.com/"; 279 const char kSourceURL[] = "http://www.gmail.com/";
218 const net::Error kError = net::ERR_FAILED; 280 const net::Error kError = net::ERR_FAILED;
219 delegate_->set_result(kError); 281 proxy_resolver_->set_error(kError);
220 282
221 std::unique_ptr<dbus::Response> response; 283 std::unique_ptr<dbus::Response> response;
222 std::unique_ptr<SignalInfo> signal; 284 std::unique_ptr<SignalInfo> signal;
223 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); 285 CallMethod(kSourceURL, true /* request_signal */, &response, &signal);
224 286
225 // An empty response should be returned. 287 // An empty response should be returned.
226 ASSERT_TRUE(response); 288 ASSERT_TRUE(response);
227 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); 289 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData());
228 290
229 // The signal should contain an error. 291 // The signal should contain an error.
230 ASSERT_TRUE(signal); 292 ASSERT_TRUE(signal);
231 EXPECT_EQ(kSourceURL, signal->source_url); 293 EXPECT_EQ(kSourceURL, signal->source_url);
232 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); 294 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), signal->proxy_info);
233 EXPECT_EQ(net::ErrorToString(kError), signal->error_message); 295 EXPECT_EQ(net::ErrorToString(kError), signal->error_message);
234 } 296 }
235 297
236 TEST_F(ProxyResolutionServiceProviderTest, ResponseSync) { 298 TEST_F(ProxyResolutionServiceProviderTest, ResponseSync) {
237 const char kSourceURL[] = "http://www.gmail.com/"; 299 const char kSourceURL[] = "http://www.gmail.com/";
238 std::unique_ptr<dbus::Response> response; 300 std::unique_ptr<dbus::Response> response;
239 std::unique_ptr<SignalInfo> signal; 301 std::unique_ptr<SignalInfo> signal;
240 CallMethod(kSourceURL, false /* request_signal */, &response, &signal); 302 CallMethod(kSourceURL, false /* request_signal */, &response, &signal);
241 303
242 // The response should contain the proxy info and an empty error. 304 // The response should contain the proxy info and an empty error.
305 // FIXME(derat): net::ProxyService::DidFinishResolvingProxy() falls back to
306 // direct connections and maps all resolution errors to net::OK unless
307 // |pac_mandatory| is set on the net::ProxyConfig, but I'm horribly confused
308 // about how to get a config with that setting into net::ProxyService().
Daniel Erat 2017/03/31 02:39:44 i'm not sure how to fix this, or whether i should
eroman 2017/04/04 21:06:10 If your question is how to mock it in the test, th
Daniel Erat 2017/04/05 02:34:13 oh, hmm. my question was how to mock it in this te
243 ASSERT_TRUE(response); 309 ASSERT_TRUE(response);
244 dbus::MessageReader reader(response.get()); 310 dbus::MessageReader reader(response.get());
245 std::string proxy_info, error; 311 std::string proxy_info, error;
246 EXPECT_TRUE(reader.PopString(&proxy_info)); 312 EXPECT_TRUE(reader.PopString(&proxy_info));
247 EXPECT_TRUE(reader.PopString(&error)); 313 EXPECT_TRUE(reader.PopString(&error));
248 EXPECT_EQ(delegate_->proxy_info().ToPacString(), proxy_info); 314 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), proxy_info);
249 EXPECT_EQ("", error); 315 EXPECT_EQ("", error);
250 316
251 // No signal should've been emitted. 317 // No signal should've been emitted.
252 EXPECT_FALSE(signal); 318 EXPECT_FALSE(signal);
253 } 319 }
254 320
255 TEST_F(ProxyResolutionServiceProviderTest, ResponseAsync) { 321 TEST_F(ProxyResolutionServiceProviderTest, ResponseAsync) {
256 const char kSourceURL[] = "http://www.gmail.com/"; 322 const char kSourceURL[] = "http://www.gmail.com/";
257 delegate_->set_async(true); 323 proxy_resolver_->set_async(true);
258 delegate_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080"); 324 proxy_resolver_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080");
259 std::unique_ptr<dbus::Response> response; 325 std::unique_ptr<dbus::Response> response;
260 std::unique_ptr<SignalInfo> signal; 326 std::unique_ptr<SignalInfo> signal;
261 CallMethod(kSourceURL, false /* request_signal */, &response, &signal); 327 CallMethod(kSourceURL, false /* request_signal */, &response, &signal);
262 328
263 // The response should contain the proxy info and an empty error. 329 // The response should contain the proxy info and an empty error.
264 ASSERT_TRUE(response); 330 ASSERT_TRUE(response);
265 dbus::MessageReader reader(response.get()); 331 dbus::MessageReader reader(response.get());
266 std::string proxy_info, error; 332 std::string proxy_info, error;
267 EXPECT_TRUE(reader.PopString(&proxy_info)); 333 EXPECT_TRUE(reader.PopString(&proxy_info));
268 EXPECT_TRUE(reader.PopString(&error)); 334 EXPECT_TRUE(reader.PopString(&error));
269 EXPECT_EQ(delegate_->proxy_info().ToPacString(), proxy_info); 335 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), proxy_info);
270 EXPECT_EQ("", error); 336 EXPECT_EQ("", error);
271 337
272 // No signal should've been emitted. 338 // No signal should've been emitted.
273 EXPECT_FALSE(signal); 339 EXPECT_FALSE(signal);
274 } 340 }
275 341
276 } // namespace chromeos 342 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698