Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 | 34 |
| 35 namespace content { | 35 namespace content { |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // Stub client certificate store that returns a preset list of certificates for | 38 // 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 | 39 // each request and records the arguments of the most recent request for later |
| 40 // inspection. | 40 // inspection. |
| 41 class ClientCertStoreStub : public net::ClientCertStore { | 41 class ClientCertStoreStub : public net::ClientCertStore { |
| 42 public: | 42 public: |
| 43 ClientCertStoreStub(const net::CertificateList& certs) | 43 ClientCertStoreStub(const net::CertificateList& certs) |
| 44 : response_(certs), | 44 : response_(certs), async_(false), request_count_(0) {} |
| 45 request_count_(0) {} | |
| 46 | 45 |
| 47 ~ClientCertStoreStub() override {} | 46 ~ClientCertStoreStub() override {} |
| 48 | 47 |
| 48 // Configures whether the certificates are returned asynchronously or not. | |
| 49 void set_async(bool async) { async_ = async; } | |
| 50 | |
| 49 // Returns |cert_authorities| field of the certificate request passed in the | 51 // Returns |cert_authorities| field of the certificate request passed in the |
| 50 // most recent call to GetClientCerts(). | 52 // most recent call to GetClientCerts(). |
| 51 // TODO(ppi): Make the stub independent from the internal representation of | 53 // TODO(ppi): Make the stub independent from the internal representation of |
| 52 // SSLCertRequestInfo. For now it seems that we cannot neither save the | 54 // SSLCertRequestInfo. For now it seems that we cannot neither save the |
| 53 // scoped_refptr<> (since it is never passed to us) nor copy the entire | 55 // scoped_refptr<> (since it is never passed to us) nor copy the entire |
| 54 // CertificateRequestInfo (since there is no copy constructor). | 56 // CertificateRequestInfo (since there is no copy constructor). |
| 55 std::vector<std::string> requested_authorities() { | 57 std::vector<std::string> requested_authorities() { |
| 56 return requested_authorities_; | 58 return requested_authorities_; |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Returns the number of calls to GetClientCerts(). | 61 // Returns the number of calls to GetClientCerts(). |
| 60 int request_count() { | 62 int request_count() { |
| 61 return request_count_; | 63 return request_count_; |
| 62 } | 64 } |
| 63 | 65 |
| 64 // net::ClientCertStore: | 66 // net::ClientCertStore: |
| 65 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, | 67 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, |
| 66 net::CertificateList* selected_certs, | 68 net::CertificateList* selected_certs, |
| 67 const base::Closure& callback) override { | 69 const base::Closure& callback) override { |
| 68 ++request_count_; | 70 ++request_count_; |
| 69 requested_authorities_ = cert_request_info.cert_authorities; | 71 requested_authorities_ = cert_request_info.cert_authorities; |
| 70 *selected_certs = response_; | 72 *selected_certs = response_; |
| 71 callback.Run(); | 73 if (async_) { |
| 74 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | |
| 75 } else { | |
| 76 callback.Run(); | |
| 77 } | |
| 72 } | 78 } |
| 73 | 79 |
| 74 private: | 80 private: |
| 75 const net::CertificateList response_; | 81 const net::CertificateList response_; |
| 82 bool async_; | |
| 76 int request_count_; | 83 int request_count_; |
| 77 std::vector<std::string> requested_authorities_; | 84 std::vector<std::string> requested_authorities_; |
| 78 }; | 85 }; |
| 79 | 86 |
| 80 // Arbitrary read buffer size. | 87 // Arbitrary read buffer size. |
| 81 const int kReadBufSize = 1024; | 88 const int kReadBufSize = 1024; |
| 82 | 89 |
| 83 // Dummy implementation of ResourceHandler, instance of which is needed to | 90 // Dummy implementation of ResourceHandler, instance of which is needed to |
| 84 // initialize ResourceLoader. | 91 // initialize ResourceLoader. |
| 85 class ResourceHandlerStub : public ResourceHandler { | 92 class ResourceHandlerStub : public ResourceHandler { |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 430 | 437 |
| 431 // Restore the original content browser client. | 438 // Restore the original content browser client. |
| 432 SetBrowserClientForTesting(old_client); | 439 SetBrowserClientForTesting(old_client); |
| 433 | 440 |
| 434 // Check if the SelectClientCertificate was called on the content browser | 441 // Check if the SelectClientCertificate was called on the content browser |
| 435 // client. | 442 // client. |
| 436 EXPECT_EQ(1, test_client.call_count()); | 443 EXPECT_EQ(1, test_client.call_count()); |
| 437 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); | 444 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); |
| 438 } | 445 } |
| 439 | 446 |
| 447 TEST_F(ResourceLoaderTest, ClientCertStoreAsyncCancel) { | |
| 448 // Set up the test client cert store. | |
| 449 scoped_ptr<ClientCertStoreStub> test_store( | |
| 450 new ClientCertStoreStub(net::CertificateList())); | |
| 451 test_store->set_async(true); | |
| 452 EXPECT_EQ(0, test_store->request_count()); | |
| 453 | |
| 454 // Ownership of the |test_store| is about to be turned over to ResourceLoader. | |
|
mmenke
2014/12/11 21:02:20
Could you add some docs to ClientCertStore? The n
davidben
2014/12/11 22:08:10
https://codereview.chromium.org/802563002
| |
| 455 // We need to keep raw pointer copies to access these objects later. | |
| 456 ClientCertStoreStub* raw_ptr_to_store = test_store.get(); | |
| 457 resource_context_.SetClientCertStore(test_store.Pass()); | |
| 458 | |
| 459 // Prepare a dummy certificate request. | |
| 460 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( | |
| 461 new net::SSLCertRequestInfo()); | |
| 462 std::vector<std::string> dummy_authority(1, "dummy"); | |
| 463 cert_request_info->cert_authorities = dummy_authority; | |
| 464 | |
| 465 // Everything is set up. Trigger the resource loader certificate request | |
| 466 // event. | |
| 467 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get()); | |
| 468 | |
| 469 // Check if the test store was queried against correct |cert_authorities|. | |
| 470 EXPECT_EQ(1, raw_ptr_to_store->request_count()); | |
| 471 EXPECT_EQ(dummy_authority, raw_ptr_to_store->requested_authorities()); | |
| 472 | |
| 473 // Cancel the request before the store calls the callback. | |
| 474 loader_.reset(); | |
| 475 | |
| 476 // Pump the event loop. There shouldn't be a crash when the callback is run. | |
| 477 base::RunLoop().RunUntilIdle(); | |
| 478 } | |
| 479 | |
| 440 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { | 480 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { |
| 441 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); | 481 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); |
| 442 | 482 |
| 443 loader_->StartRequest(); | 483 loader_->StartRequest(); |
| 444 loader_->CancelRequest(true); | 484 loader_->CancelRequest(true); |
| 445 static_cast<ResourceController*>(loader_.get())->Resume(); | 485 static_cast<ResourceController*>(loader_.get())->Resume(); |
| 446 } | 486 } |
| 447 | 487 |
| 448 // Tests that no invariants are broken if a ResourceHandler cancels during | 488 // Tests that no invariants are broken if a ResourceHandler cancels during |
| 449 // OnReadCompleted. | 489 // OnReadCompleted. |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 721 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); | 761 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); |
| 722 EXPECT_EQ(test_data(), contents); | 762 EXPECT_EQ(test_data(), contents); |
| 723 | 763 |
| 724 // Release the loader. The file should be gone now. | 764 // Release the loader. The file should be gone now. |
| 725 ReleaseLoader(); | 765 ReleaseLoader(); |
| 726 base::RunLoop().RunUntilIdle(); | 766 base::RunLoop().RunUntilIdle(); |
| 727 EXPECT_FALSE(base::PathExists(temp_path())); | 767 EXPECT_FALSE(base::PathExists(temp_path())); |
| 728 } | 768 } |
| 729 | 769 |
| 730 } // namespace content | 770 } // namespace content |
| OLD | NEW |