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

Side by Side Diff: content/browser/loader/resource_loader_unittest.cc

Issue 859213006: Cancel client auth requests when not promptable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@client-auth-cancel-1
Patch Set: mmenke comments Created 5 years, 10 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "content/browser/loader/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include "base/files/file.h" 7 #include "base/files/file.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/macros.h"
9 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h" 11 #include "base/run_loop.h"
11 #include "content/browser/browser_thread_impl.h" 12 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/loader/redirect_to_file_resource_handler.h" 13 #include "content/browser/loader/redirect_to_file_resource_handler.h"
13 #include "content/browser/loader/resource_loader_delegate.h" 14 #include "content/browser/loader/resource_loader_delegate.h"
15 #include "content/public/browser/client_certificate_delegate.h"
14 #include "content/public/browser/resource_request_info.h" 16 #include "content/public/browser/resource_request_info.h"
15 #include "content/public/common/resource_response.h" 17 #include "content/public/common/resource_response.h"
16 #include "content/public/test/mock_resource_context.h" 18 #include "content/public/test/mock_resource_context.h"
19 #include "content/public/test/test_browser_context.h"
17 #include "content/public/test/test_browser_thread_bundle.h" 20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/public/test/test_renderer_host.h"
18 #include "content/test/test_content_browser_client.h" 22 #include "content/test/test_content_browser_client.h"
23 #include "content/test/test_web_contents.h"
19 #include "ipc/ipc_message.h" 24 #include "ipc/ipc_message.h"
20 #include "net/base/io_buffer.h" 25 #include "net/base/io_buffer.h"
21 #include "net/base/mock_file_stream.h" 26 #include "net/base/mock_file_stream.h"
27 #include "net/base/net_errors.h"
22 #include "net/base/request_priority.h" 28 #include "net/base/request_priority.h"
23 #include "net/cert/x509_certificate.h" 29 #include "net/cert/x509_certificate.h"
24 #include "net/ssl/client_cert_store.h" 30 #include "net/ssl/client_cert_store.h"
25 #include "net/ssl/ssl_cert_request_info.h" 31 #include "net/ssl/ssl_cert_request_info.h"
26 #include "net/url_request/url_request.h" 32 #include "net/url_request/url_request.h"
33 #include "net/url_request/url_request_job_factory.h"
27 #include "net/url_request/url_request_job_factory_impl.h" 34 #include "net/url_request/url_request_job_factory_impl.h"
28 #include "net/url_request/url_request_test_job.h" 35 #include "net/url_request/url_request_test_job.h"
29 #include "net/url_request/url_request_test_util.h" 36 #include "net/url_request/url_request_test_util.h"
30 #include "storage/browser/blob/shareable_file_reference.h" 37 #include "storage/browser/blob/shareable_file_reference.h"
31 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
32 39
33 using storage::ShareableFileReference; 40 using storage::ShareableFileReference;
34 41
35 namespace content { 42 namespace content {
36 namespace { 43 namespace {
37 44
38 // Stub client certificate store that returns a preset list of certificates for 45 // Stub client certificate store that returns a preset list of certificates for
39 // each request and records the arguments of the most recent request for later 46 // each request and records the arguments of the most recent request for later
40 // inspection. 47 // inspection.
41 class ClientCertStoreStub : public net::ClientCertStore { 48 class ClientCertStoreStub : public net::ClientCertStore {
42 public: 49 public:
43 // Creates a new ClientCertStoreStub that returns |response| on query. It 50 // Creates a new ClientCertStoreStub that returns |response| on query. It
44 // saves the number of requests and most recently certificate authorities list 51 // saves the number of requests and most recently certificate authorities list
45 // in |requested_authorities| and |request_count|, respectively. The caller is 52 // in |requested_authorities| and |request_count|, respectively. The caller is
46 // responsible for ensuring those pointers outlive the ClientCertStoreStub. 53 // responsible for ensuring those pointers outlive the ClientCertStoreStub.
47 // 54 //
48 // TODO(ppi): Make the stub independent from the internal representation of 55 // TODO(ppi): Make the stub independent from the internal representation of
49 // SSLCertRequestInfo. For now it seems that we can neither save the 56 // SSLCertRequestInfo. For now it seems that we can neither save the
50 // scoped_refptr<> (since it is never passed to us) nor copy the entire 57 // scoped_refptr<> (since it is never passed to us) nor copy the entire
51 // CertificateRequestInfo (since there is no copy constructor). 58 // CertificateRequestInfo (since there is no copy constructor).
52 ClientCertStoreStub(const net::CertificateList& response, 59 ClientCertStoreStub(const net::CertificateList& response,
53 int* request_count, 60 int* request_count,
54 std::vector<std::string>* requested_authorities) 61 std::vector<std::string>* requested_authorities)
55 : response_(response), 62 : response_(response),
56 async_(false),
57 requested_authorities_(requested_authorities), 63 requested_authorities_(requested_authorities),
58 request_count_(request_count) { 64 request_count_(request_count) {
59 requested_authorities_->clear(); 65 requested_authorities_->clear();
60 *request_count_ = 0; 66 *request_count_ = 0;
61 } 67 }
62 68
63 ~ClientCertStoreStub() override {} 69 ~ClientCertStoreStub() override {}
64 70
65 // Configures whether the certificates are returned asynchronously or not.
66 void set_async(bool async) { async_ = async; }
67
68 // net::ClientCertStore: 71 // net::ClientCertStore:
69 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, 72 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info,
70 net::CertificateList* selected_certs, 73 net::CertificateList* selected_certs,
71 const base::Closure& callback) override { 74 const base::Closure& callback) override {
72 *requested_authorities_ = cert_request_info.cert_authorities; 75 *requested_authorities_ = cert_request_info.cert_authorities;
73 ++(*request_count_); 76 ++(*request_count_);
74 77
75 *selected_certs = response_; 78 *selected_certs = response_;
76 if (async_) { 79 callback.Run();
77 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
78 } else {
79 callback.Run();
80 }
81 } 80 }
82 81
83 private: 82 private:
84 const net::CertificateList response_; 83 const net::CertificateList response_;
85 bool async_;
86 std::vector<std::string>* requested_authorities_; 84 std::vector<std::string>* requested_authorities_;
87 int* request_count_; 85 int* request_count_;
88 }; 86 };
89 87
88 // Client certificate store which destroys its resource loader before the
89 // asynchronous GetClientCerts callback is called.
90 class LoaderDestroyingCertStore : public net::ClientCertStore {
91 public:
92 // Creates a client certificate store which, when looked up, posts a task to
93 // reset |loader| and then call the callback. The caller is responsible for
94 // ensuring the pointers remain valid until the process is complete.
95 explicit LoaderDestroyingCertStore(scoped_ptr<ResourceLoader>* loader)
96 : loader_(loader) {}
97
98 // net::ClientCertStore:
99 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info,
100 net::CertificateList* selected_certs,
101 const base::Closure& callback) override {
102 // Don't destroy |loader_| while it's on the stack.
103 base::MessageLoop::current()->PostTask(
104 FROM_HERE, base::Bind(&LoaderDestroyingCertStore::DoCallback,
105 base::Unretained(loader_), callback));
106 }
107
108 private:
109 static void DoCallback(scoped_ptr<ResourceLoader>* loader,
110 const base::Closure& callback) {
111 loader->reset();
112 callback.Run();
113 }
114
115 scoped_ptr<ResourceLoader>* loader_;
116 };
117
118 // A mock URLRequestJob which simulates an SSL client auth request.
119 class MockClientCertURLRequestJob : public net::URLRequestTestJob {
120 public:
121 MockClientCertURLRequestJob(net::URLRequest* request,
122 net::NetworkDelegate* network_delegate)
123 : net::URLRequestTestJob(request, network_delegate) {}
124
125 static std::vector<std::string> test_authorities() {
126 return std::vector<std::string>(1, "dummy");
127 }
128
129 // net::URLRequestTestJob:
130 void Start() override {
131 scoped_refptr<net::SSLCertRequestInfo> cert_request_info(
132 new net::SSLCertRequestInfo);
133 cert_request_info->cert_authorities = test_authorities();
134 base::MessageLoop::current()->PostTask(
135 FROM_HERE,
136 base::Bind(&MockClientCertURLRequestJob::NotifyCertificateRequested,
137 this, cert_request_info));
138 }
139
140 void ContinueWithCertificate(net::X509Certificate* cert) override {
141 net::URLRequestTestJob::Start();
142 }
143
144 private:
145 ~MockClientCertURLRequestJob() override {}
146
147 DISALLOW_COPY_AND_ASSIGN(MockClientCertURLRequestJob);
148 };
149
150 class MockClientCertJobProtocolHandler
151 : public net::URLRequestJobFactory::ProtocolHandler {
152 public:
153 // URLRequestJobFactory::ProtocolHandler implementation:
154 net::URLRequestJob* MaybeCreateJob(
155 net::URLRequest* request,
156 net::NetworkDelegate* network_delegate) const override {
157 return new MockClientCertURLRequestJob(request, network_delegate);
158 }
159 };
160
90 // Arbitrary read buffer size. 161 // Arbitrary read buffer size.
91 const int kReadBufSize = 1024; 162 const int kReadBufSize = 1024;
92 163
93 // Dummy implementation of ResourceHandler, instance of which is needed to 164 // Dummy implementation of ResourceHandler, instance of which is needed to
94 // initialize ResourceLoader. 165 // initialize ResourceLoader.
95 class ResourceHandlerStub : public ResourceHandler { 166 class ResourceHandlerStub : public ResourceHandler {
96 public: 167 public:
97 explicit ResourceHandlerStub(net::URLRequest* request) 168 explicit ResourceHandlerStub(net::URLRequest* request)
98 : ResourceHandler(request), 169 : ResourceHandler(request),
99 read_buffer_(new net::IOBuffer(kReadBufSize)), 170 read_buffer_(new net::IOBuffer(kReadBufSize)),
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 int total_bytes_downloaded_; 303 int total_bytes_downloaded_;
233 }; 304 };
234 305
235 // Test browser client that captures calls to SelectClientCertificates and 306 // Test browser client that captures calls to SelectClientCertificates and
236 // records the arguments of the most recent call for later inspection. 307 // records the arguments of the most recent call for later inspection.
237 class SelectCertificateBrowserClient : public TestContentBrowserClient { 308 class SelectCertificateBrowserClient : public TestContentBrowserClient {
238 public: 309 public:
239 SelectCertificateBrowserClient() : call_count_(0) {} 310 SelectCertificateBrowserClient() : call_count_(0) {}
240 311
241 void SelectClientCertificate( 312 void SelectClientCertificate(
242 int render_process_id, 313 WebContents* web_contents,
243 int render_view_id,
244 net::SSLCertRequestInfo* cert_request_info, 314 net::SSLCertRequestInfo* cert_request_info,
245 const base::Callback<void(net::X509Certificate*)>& callback) override { 315 scoped_ptr<ClientCertificateDelegate> delegate) override {
246 ++call_count_; 316 ++call_count_;
247 passed_certs_ = cert_request_info->client_certs; 317 passed_certs_ = cert_request_info->client_certs;
318 delegate_ = delegate.Pass();
248 } 319 }
249 320
250 int call_count() { 321 int call_count() { return call_count_; }
251 return call_count_; 322 net::CertificateList passed_certs() { return passed_certs_; }
252 } 323 ClientCertificateDelegate* delegate() { return delegate_.get(); }
253
254 net::CertificateList passed_certs() {
255 return passed_certs_;
256 }
257 324
258 private: 325 private:
259 net::CertificateList passed_certs_; 326 net::CertificateList passed_certs_;
260 int call_count_; 327 int call_count_;
328 scoped_ptr<ClientCertificateDelegate> delegate_;
261 }; 329 };
262 330
263 class ResourceContextStub : public MockResourceContext { 331 class ResourceContextStub : public MockResourceContext {
264 public: 332 public:
265 explicit ResourceContextStub(net::URLRequestContext* test_request_context) 333 explicit ResourceContextStub(net::URLRequestContext* test_request_context)
266 : MockResourceContext(test_request_context) {} 334 : MockResourceContext(test_request_context) {}
267 335
268 scoped_ptr<net::ClientCertStore> CreateClientCertStore() override { 336 scoped_ptr<net::ClientCertStore> CreateClientCertStore() override {
269 return dummy_cert_store_.Pass(); 337 return dummy_cert_store_.Pass();
270 } 338 }
(...skipping 19 matching lines...) Expand all
290 } // namespace 358 } // namespace
291 359
292 class ResourceLoaderTest : public testing::Test, 360 class ResourceLoaderTest : public testing::Test,
293 public ResourceLoaderDelegate { 361 public ResourceLoaderDelegate {
294 protected: 362 protected:
295 ResourceLoaderTest() 363 ResourceLoaderTest()
296 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 364 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
297 resource_context_(&test_url_request_context_), 365 resource_context_(&test_url_request_context_),
298 raw_ptr_resource_handler_(NULL), 366 raw_ptr_resource_handler_(NULL),
299 raw_ptr_to_request_(NULL) { 367 raw_ptr_to_request_(NULL) {
300 job_factory_.SetProtocolHandler(
301 "test", net::URLRequestTestJob::CreateProtocolHandler());
302 test_url_request_context_.set_job_factory(&job_factory_); 368 test_url_request_context_.set_job_factory(&job_factory_);
303 } 369 }
304 370
305 GURL test_url() const { 371 GURL test_url() const { return net::URLRequestTestJob::test_url_1(); }
306 return net::URLRequestTestJob::test_url_1();
307 }
308 372
309 std::string test_data() const { 373 std::string test_data() const {
310 return net::URLRequestTestJob::test_data_1(); 374 return net::URLRequestTestJob::test_data_1();
311 } 375 }
312 376
377 virtual net::URLRequestJobFactory::ProtocolHandler* CreateProtocolHandler() {
378 return net::URLRequestTestJob::CreateProtocolHandler();
379 }
380
313 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( 381 virtual scoped_ptr<ResourceHandler> WrapResourceHandler(
314 scoped_ptr<ResourceHandlerStub> leaf_handler, 382 scoped_ptr<ResourceHandlerStub> leaf_handler,
315 net::URLRequest* request) { 383 net::URLRequest* request) {
316 return leaf_handler.Pass(); 384 return leaf_handler.Pass();
317 } 385 }
318 386
319 void SetUp() override { 387 void SetUp() override {
320 const int kRenderProcessId = 1; 388 job_factory_.SetProtocolHandler("test", CreateProtocolHandler());
321 const int kRenderViewId = 2; 389
390 browser_context_.reset(new TestBrowserContext());
391 scoped_refptr<SiteInstance> site_instance =
392 SiteInstance::Create(browser_context_.get());
393 web_contents_.reset(
394 TestWebContents::Create(browser_context_.get(), site_instance.get()));
395 RenderFrameHost* rfh = web_contents_->GetMainFrame();
322 396
323 scoped_ptr<net::URLRequest> request( 397 scoped_ptr<net::URLRequest> request(
324 resource_context_.GetRequestContext()->CreateRequest( 398 resource_context_.GetRequestContext()->CreateRequest(
325 test_url(), 399 test_url(),
326 net::DEFAULT_PRIORITY, 400 net::DEFAULT_PRIORITY,
327 NULL /* delegate */, 401 NULL /* delegate */,
328 NULL /* cookie_store */)); 402 NULL /* cookie_store */));
329 raw_ptr_to_request_ = request.get(); 403 raw_ptr_to_request_ = request.get();
330 ResourceRequestInfo::AllocateForTesting(request.get(), 404 ResourceRequestInfo::AllocateForTesting(
331 RESOURCE_TYPE_MAIN_FRAME, 405 request.get(), RESOURCE_TYPE_MAIN_FRAME, &resource_context_,
332 &resource_context_, 406 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(),
333 kRenderProcessId, 407 rfh->GetRoutingID(), true /* is_main_frame */,
334 kRenderViewId, 408 false /* parent_is_main_frame */, true /* allow_download */,
335 MSG_ROUTING_NONE, 409 false /* is_async */);
336 true, // is_main_frame
337 false, // parent_is_main_frame
338 true, // allow_download
339 false); // is_async
340 scoped_ptr<ResourceHandlerStub> resource_handler( 410 scoped_ptr<ResourceHandlerStub> resource_handler(
341 new ResourceHandlerStub(request.get())); 411 new ResourceHandlerStub(request.get()));
342 raw_ptr_resource_handler_ = resource_handler.get(); 412 raw_ptr_resource_handler_ = resource_handler.get();
343 loader_.reset(new ResourceLoader( 413 loader_.reset(new ResourceLoader(
344 request.Pass(), 414 request.Pass(),
345 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_), 415 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_),
346 this)); 416 this));
347 } 417 }
348 418
419 void TearDown() override {
420 // Destroy the WebContents and pump the event loop before destroying
421 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup
422 // tasks complete.
423 web_contents_.reset();
424 base::RunLoop().RunUntilIdle();
425 }
426
349 // ResourceLoaderDelegate: 427 // ResourceLoaderDelegate:
350 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( 428 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
351 ResourceLoader* loader, 429 ResourceLoader* loader,
352 net::AuthChallengeInfo* auth_info) override { 430 net::AuthChallengeInfo* auth_info) override {
353 return NULL; 431 return NULL;
354 } 432 }
355 bool HandleExternalProtocol(ResourceLoader* loader, 433 bool HandleExternalProtocol(ResourceLoader* loader,
356 const GURL& url) override { 434 const GURL& url) override {
357 return false; 435 return false;
358 } 436 }
359 void DidStartRequest(ResourceLoader* loader) override {} 437 void DidStartRequest(ResourceLoader* loader) override {}
360 void DidReceiveRedirect(ResourceLoader* loader, 438 void DidReceiveRedirect(ResourceLoader* loader,
361 const GURL& new_url) override {} 439 const GURL& new_url) override {}
362 void DidReceiveResponse(ResourceLoader* loader) override {} 440 void DidReceiveResponse(ResourceLoader* loader) override {}
363 void DidFinishLoading(ResourceLoader* loader) override {} 441 void DidFinishLoading(ResourceLoader* loader) override {}
364 442
365 content::TestBrowserThreadBundle thread_bundle_; 443 TestBrowserThreadBundle thread_bundle_;
444 RenderViewHostTestEnabler rvh_test_enabler_;
366 445
367 net::URLRequestJobFactoryImpl job_factory_; 446 net::URLRequestJobFactoryImpl job_factory_;
368 net::TestURLRequestContext test_url_request_context_; 447 net::TestURLRequestContext test_url_request_context_;
369 ResourceContextStub resource_context_; 448 ResourceContextStub resource_context_;
449 scoped_ptr<TestBrowserContext> browser_context_;
450 scoped_ptr<TestWebContents> web_contents_;
370 451
371 // The ResourceLoader owns the URLRequest and the ResourceHandler. 452 // The ResourceLoader owns the URLRequest and the ResourceHandler.
372 ResourceHandlerStub* raw_ptr_resource_handler_; 453 ResourceHandlerStub* raw_ptr_resource_handler_;
373 net::URLRequest* raw_ptr_to_request_; 454 net::URLRequest* raw_ptr_to_request_;
374 scoped_ptr<ResourceLoader> loader_; 455 scoped_ptr<ResourceLoader> loader_;
375 }; 456 };
376 457
377 // Verifies if a call to net::UrlRequest::Delegate::OnCertificateRequested() 458 class ClientCertResourceLoaderTest : public ResourceLoaderTest {
378 // causes client cert store to be queried for certificates and if the returned 459 protected:
379 // certificates are correctly passed to the content browser client for 460 net::URLRequestJobFactory::ProtocolHandler* CreateProtocolHandler() override {
380 // selection. 461 return new MockClientCertJobProtocolHandler;
381 TEST_F(ResourceLoaderTest, ClientCertStoreLookup) { 462 }
463 };
464
465 // Tests that client certificates are requested with ClientCertStore lookup.
466 TEST_F(ClientCertResourceLoaderTest, WithStoreLookup) {
382 // Set up the test client cert store. 467 // Set up the test client cert store.
383 int store_request_count; 468 int store_request_count;
384 std::vector<std::string> store_requested_authorities; 469 std::vector<std::string> store_requested_authorities;
385 net::CertificateList dummy_certs(1, scoped_refptr<net::X509Certificate>( 470 net::CertificateList dummy_certs(1, scoped_refptr<net::X509Certificate>(
386 new net::X509Certificate("test", "test", base::Time(), base::Time()))); 471 new net::X509Certificate("test", "test", base::Time(), base::Time())));
387 scoped_ptr<ClientCertStoreStub> test_store(new ClientCertStoreStub( 472 scoped_ptr<ClientCertStoreStub> test_store(new ClientCertStoreStub(
388 dummy_certs, &store_request_count, &store_requested_authorities)); 473 dummy_certs, &store_request_count, &store_requested_authorities));
389 resource_context_.SetClientCertStore(test_store.Pass()); 474 resource_context_.SetClientCertStore(test_store.Pass());
390 475
391 // Prepare a dummy certificate request.
392 scoped_refptr<net::SSLCertRequestInfo> cert_request_info(
393 new net::SSLCertRequestInfo());
394 std::vector<std::string> dummy_authority(1, "dummy");
395 cert_request_info->cert_authorities = dummy_authority;
396
397 // Plug in test content browser client. 476 // Plug in test content browser client.
398 SelectCertificateBrowserClient test_client; 477 SelectCertificateBrowserClient test_client;
399 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 478 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
400 479
401 // Everything is set up. Trigger the resource loader certificate request event 480 // Start the request and wait for it to pause.
402 // and run the message loop. 481 loader_->StartRequest();
403 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get());
404 base::RunLoop().RunUntilIdle(); 482 base::RunLoop().RunUntilIdle();
405 483
406 // Restore the original content browser client. 484 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed());
407 SetBrowserClientForTesting(old_client);
408 485
409 // Check if the test store was queried against correct |cert_authorities|. 486 // Check if the test store was queried against correct |cert_authorities|.
410 EXPECT_EQ(1, store_request_count); 487 EXPECT_EQ(1, store_request_count);
411 EXPECT_EQ(dummy_authority, store_requested_authorities); 488 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(),
489 store_requested_authorities);
412 490
413 // Check if the retrieved certificates were passed to the content browser 491 // Check if the retrieved certificates were passed to the content browser
414 // client. 492 // client.
415 EXPECT_EQ(1, test_client.call_count()); 493 EXPECT_EQ(1, test_client.call_count());
416 EXPECT_EQ(dummy_certs, test_client.passed_certs()); 494 EXPECT_EQ(dummy_certs, test_client.passed_certs());
495
496 // Continue the request.
497 test_client.delegate()->ContinueWithCertificate(dummy_certs[0].get());
498 base::RunLoop().RunUntilIdle();
499 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed());
500 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error());
501
502 // Restore the original content browser client.
503 SetBrowserClientForTesting(old_client);
417 } 504 }
418 505
419 // Verifies if a call to net::URLRequest::Delegate::OnCertificateRequested() 506 // Tests that client certificates are requested on a platform with NULL
420 // on a platform with a NULL client cert store still calls the content browser 507 // ClientCertStore.
421 // client for selection. 508 TEST_F(ClientCertResourceLoaderTest, WithNullStore) {
422 TEST_F(ResourceLoaderTest, ClientCertStoreNull) { 509 // Plug in test content browser client.
423 // Prepare a dummy certificate request. 510 SelectCertificateBrowserClient test_client;
424 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( 511 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
425 new net::SSLCertRequestInfo()); 512
426 std::vector<std::string> dummy_authority(1, "dummy"); 513 // Start the request and wait for it to pause.
427 cert_request_info->cert_authorities = dummy_authority; 514 loader_->StartRequest();
515 base::RunLoop().RunUntilIdle();
516
517 // Check if the SelectClientCertificate was called on the content browser
518 // client.
519 EXPECT_EQ(1, test_client.call_count());
520 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
521
522 // Continue the request.
523 scoped_refptr<net::X509Certificate> cert(
524 new net::X509Certificate("test", "test", base::Time(), base::Time()));
525 test_client.delegate()->ContinueWithCertificate(cert.get());
526 base::RunLoop().RunUntilIdle();
527 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed());
528 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error());
529
530 // Restore the original content browser client.
531 SetBrowserClientForTesting(old_client);
532 }
533
534 // Tests that the ContentBrowserClient may cancel a certificate request.
535 TEST_F(ClientCertResourceLoaderTest, CancelSelection) {
536 // Plug in test content browser client.
537 SelectCertificateBrowserClient test_client;
538 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
539
540 // Start the request and wait for it to pause.
541 loader_->StartRequest();
542 base::RunLoop().RunUntilIdle();
543
544 // Check if the SelectClientCertificate was called on the content browser
545 // client.
546 EXPECT_EQ(1, test_client.call_count());
547 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
548
549 // Cancel the request.
550 test_client.delegate()->CancelCertificateSelection();
551 base::RunLoop().RunUntilIdle();
552 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed());
553 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
554 raw_ptr_resource_handler_->status().error());
555
556 // Restore the original content browser client.
557 SetBrowserClientForTesting(old_client);
558 }
559
560 // Verifies that requests without WebContents attached abort.
561 TEST_F(ClientCertResourceLoaderTest, NoWebContents) {
562 // Destroy the WebContents before starting the request.
563 web_contents_.reset();
428 564
429 // Plug in test content browser client. 565 // Plug in test content browser client.
430 SelectCertificateBrowserClient test_client; 566 SelectCertificateBrowserClient test_client;
431 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 567 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
432 568
433 // Everything is set up. Trigger the resource loader certificate request event 569 // Start the request and wait for it to pause.
434 // and run the message loop. 570 loader_->StartRequest();
435 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get());
436 base::RunLoop().RunUntilIdle(); 571 base::RunLoop().RunUntilIdle();
437 572
573 // Check that SelectClientCertificate wasn't called and the request aborted.
574 EXPECT_EQ(0, test_client.call_count());
575 EXPECT_TRUE(raw_ptr_resource_handler_->received_response_completed());
576 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
577 raw_ptr_resource_handler_->status().error());
578
438 // Restore the original content browser client. 579 // Restore the original content browser client.
439 SetBrowserClientForTesting(old_client); 580 SetBrowserClientForTesting(old_client);
440
441 // Check if the SelectClientCertificate was called on the content browser
442 // client.
443 EXPECT_EQ(1, test_client.call_count());
444 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
445 } 581 }
446 582
447 TEST_F(ResourceLoaderTest, ClientCertStoreAsyncCancel) { 583 // Verifies that ClientCertStore's callback doesn't crash if called after the
448 // Set up the test client cert store. 584 // loader is destroyed.
449 int store_request_count; 585 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) {
450 std::vector<std::string> store_requested_authorities; 586 scoped_ptr<LoaderDestroyingCertStore> test_store(
451 scoped_ptr<ClientCertStoreStub> test_store( 587 new LoaderDestroyingCertStore(&loader_));
452 new ClientCertStoreStub(net::CertificateList(), &store_request_count,
453 &store_requested_authorities));
454 test_store->set_async(true);
455 EXPECT_EQ(0, store_request_count);
456 resource_context_.SetClientCertStore(test_store.Pass()); 588 resource_context_.SetClientCertStore(test_store.Pass());
457 589
458 // Prepare a dummy certificate request. 590 loader_->StartRequest();
459 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( 591 base::RunLoop().RunUntilIdle();
460 new net::SSLCertRequestInfo()); 592 EXPECT_FALSE(loader_);
461 std::vector<std::string> dummy_authority(1, "dummy");
462 cert_request_info->cert_authorities = dummy_authority;
463 593
464 // Everything is set up. Trigger the resource loader certificate request 594 // Pump the event loop to ensure nothing asynchronous crashes either.
465 // event.
466 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get());
467
468 // Check if the test store was queried against correct |cert_authorities|.
469 EXPECT_EQ(1, store_request_count);
470 EXPECT_EQ(dummy_authority, store_requested_authorities);
471
472 // Cancel the request before the store calls the callback.
473 loader_.reset();
474
475 // Pump the event loop. There shouldn't be a crash when the callback is run.
476 base::RunLoop().RunUntilIdle(); 595 base::RunLoop().RunUntilIdle();
477 } 596 }
478 597
479 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { 598 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) {
480 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); 599 raw_ptr_resource_handler_->set_defer_request_on_will_start(true);
481 600
482 loader_->StartRequest(); 601 loader_->StartRequest();
483 loader_->CancelRequest(true); 602 loader_->CancelRequest(true);
484 static_cast<ResourceController*>(loader_.get())->Resume(); 603 static_cast<ResourceController*>(loader_.get())->Resume();
485 } 604 }
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 879 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
761 EXPECT_EQ(test_data(), contents); 880 EXPECT_EQ(test_data(), contents);
762 881
763 // Release the loader. The file should be gone now. 882 // Release the loader. The file should be gone now.
764 ReleaseLoader(); 883 ReleaseLoader();
765 base::RunLoop().RunUntilIdle(); 884 base::RunLoop().RunUntilIdle();
766 EXPECT_FALSE(base::PathExists(temp_path())); 885 EXPECT_FALSE(base::PathExists(temp_path()));
767 } 886 }
768 887
769 } // namespace content 888 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698