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 22 matching lines...) Expand all Loading... | |
33 using storage::ShareableFileReference; | 33 using storage::ShareableFileReference; |
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 // Creates a new ClientCertStoreStub that returns |response| on query. It |
44 : response_(certs), async_(false), request_count_(0) {} | 44 // saves the number of requests and most recently certificate authorities list |
45 // in |requested_authorities| and |request_count|, respectively. The caller is | |
46 // responsible for ensuring those pointers outlive the ClientCertStoreStub. | |
47 // | |
48 // TODO(ppi): Make the stub independent from the internal representation of | |
49 // SSLCertRequestInfo. For now it seems that we cannot neither save the | |
mmenke
2015/01/23 21:43:27
While you're here...."cannot neither" -> "can neit
davidben
2015/01/23 21:50:56
Done.
| |
50 // scoped_refptr<> (since it is never passed to us) nor copy the entire | |
51 // CertificateRequestInfo (since there is no copy constructor). | |
52 ClientCertStoreStub(const net::CertificateList& response, | |
53 int* request_count, | |
54 std::vector<std::string>* requested_authorities) | |
55 : response_(response), | |
56 async_(false), | |
57 requested_authorities_(requested_authorities), | |
58 request_count_(request_count) { | |
59 requested_authorities_->clear(); | |
60 *request_count_ = 0; | |
61 } | |
45 | 62 |
46 ~ClientCertStoreStub() override {} | 63 ~ClientCertStoreStub() override {} |
47 | 64 |
48 // Configures whether the certificates are returned asynchronously or not. | 65 // Configures whether the certificates are returned asynchronously or not. |
49 void set_async(bool async) { async_ = async; } | 66 void set_async(bool async) { async_ = async; } |
50 | 67 |
51 // Returns |cert_authorities| field of the certificate request passed in the | |
52 // most recent call to GetClientCerts(). | |
53 // TODO(ppi): Make the stub independent from the internal representation of | |
54 // SSLCertRequestInfo. For now it seems that we cannot neither save the | |
55 // scoped_refptr<> (since it is never passed to us) nor copy the entire | |
56 // CertificateRequestInfo (since there is no copy constructor). | |
57 std::vector<std::string> requested_authorities() { | |
58 return requested_authorities_; | |
59 } | |
60 | |
61 // Returns the number of calls to GetClientCerts(). | |
62 int request_count() { | |
63 return request_count_; | |
64 } | |
65 | |
66 // net::ClientCertStore: | 68 // net::ClientCertStore: |
67 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, | 69 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, |
68 net::CertificateList* selected_certs, | 70 net::CertificateList* selected_certs, |
69 const base::Closure& callback) override { | 71 const base::Closure& callback) override { |
70 ++request_count_; | 72 *requested_authorities_ = cert_request_info.cert_authorities; |
71 requested_authorities_ = cert_request_info.cert_authorities; | 73 ++(*request_count_); |
74 | |
72 *selected_certs = response_; | 75 *selected_certs = response_; |
73 if (async_) { | 76 if (async_) { |
74 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 77 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
75 } else { | 78 } else { |
76 callback.Run(); | 79 callback.Run(); |
77 } | 80 } |
78 } | 81 } |
79 | 82 |
80 private: | 83 private: |
81 const net::CertificateList response_; | 84 const net::CertificateList response_; |
82 bool async_; | 85 bool async_; |
83 int request_count_; | 86 std::vector<std::string>* requested_authorities_; |
84 std::vector<std::string> requested_authorities_; | 87 int* request_count_; |
85 }; | 88 }; |
86 | 89 |
87 // Arbitrary read buffer size. | 90 // Arbitrary read buffer size. |
88 const int kReadBufSize = 1024; | 91 const int kReadBufSize = 1024; |
89 | 92 |
90 // Dummy implementation of ResourceHandler, instance of which is needed to | 93 // Dummy implementation of ResourceHandler, instance of which is needed to |
91 // initialize ResourceLoader. | 94 // initialize ResourceLoader. |
92 class ResourceHandlerStub : public ResourceHandler { | 95 class ResourceHandlerStub : public ResourceHandler { |
93 public: | 96 public: |
94 explicit ResourceHandlerStub(net::URLRequest* request) | 97 explicit ResourceHandlerStub(net::URLRequest* request) |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 net::URLRequest* raw_ptr_to_request_; | 373 net::URLRequest* raw_ptr_to_request_; |
371 scoped_ptr<ResourceLoader> loader_; | 374 scoped_ptr<ResourceLoader> loader_; |
372 }; | 375 }; |
373 | 376 |
374 // Verifies if a call to net::UrlRequest::Delegate::OnCertificateRequested() | 377 // Verifies if a call to net::UrlRequest::Delegate::OnCertificateRequested() |
375 // causes client cert store to be queried for certificates and if the returned | 378 // causes client cert store to be queried for certificates and if the returned |
376 // certificates are correctly passed to the content browser client for | 379 // certificates are correctly passed to the content browser client for |
377 // selection. | 380 // selection. |
378 TEST_F(ResourceLoaderTest, ClientCertStoreLookup) { | 381 TEST_F(ResourceLoaderTest, ClientCertStoreLookup) { |
379 // Set up the test client cert store. | 382 // Set up the test client cert store. |
383 int store_request_count; | |
384 std::vector<std::string> store_requested_authorities; | |
380 net::CertificateList dummy_certs(1, scoped_refptr<net::X509Certificate>( | 385 net::CertificateList dummy_certs(1, scoped_refptr<net::X509Certificate>( |
381 new net::X509Certificate("test", "test", base::Time(), base::Time()))); | 386 new net::X509Certificate("test", "test", base::Time(), base::Time()))); |
382 scoped_ptr<ClientCertStoreStub> test_store( | 387 scoped_ptr<ClientCertStoreStub> test_store(new ClientCertStoreStub( |
383 new ClientCertStoreStub(dummy_certs)); | 388 dummy_certs, &store_request_count, &store_requested_authorities)); |
384 EXPECT_EQ(0, test_store->request_count()); | |
385 | |
386 // Ownership of the |test_store| is about to be turned over to ResourceLoader. | |
387 // We need to keep raw pointer copies to access these objects later. | |
388 ClientCertStoreStub* raw_ptr_to_store = test_store.get(); | |
389 resource_context_.SetClientCertStore(test_store.Pass()); | 389 resource_context_.SetClientCertStore(test_store.Pass()); |
390 | 390 |
391 // Prepare a dummy certificate request. | 391 // Prepare a dummy certificate request. |
392 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( | 392 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( |
393 new net::SSLCertRequestInfo()); | 393 new net::SSLCertRequestInfo()); |
394 std::vector<std::string> dummy_authority(1, "dummy"); | 394 std::vector<std::string> dummy_authority(1, "dummy"); |
395 cert_request_info->cert_authorities = dummy_authority; | 395 cert_request_info->cert_authorities = dummy_authority; |
396 | 396 |
397 // Plug in test content browser client. | 397 // Plug in test content browser client. |
398 SelectCertificateBrowserClient test_client; | 398 SelectCertificateBrowserClient test_client; |
399 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); | 399 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); |
400 | 400 |
401 // Everything is set up. Trigger the resource loader certificate request event | 401 // Everything is set up. Trigger the resource loader certificate request event |
402 // and run the message loop. | 402 // and run the message loop. |
403 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get()); | 403 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get()); |
404 base::RunLoop().RunUntilIdle(); | 404 base::RunLoop().RunUntilIdle(); |
405 | 405 |
406 // Restore the original content browser client. | 406 // Restore the original content browser client. |
407 SetBrowserClientForTesting(old_client); | 407 SetBrowserClientForTesting(old_client); |
408 | 408 |
409 // Check if the test store was queried against correct |cert_authorities|. | 409 // Check if the test store was queried against correct |cert_authorities|. |
410 EXPECT_EQ(1, raw_ptr_to_store->request_count()); | 410 EXPECT_EQ(1, store_request_count); |
411 EXPECT_EQ(dummy_authority, raw_ptr_to_store->requested_authorities()); | 411 EXPECT_EQ(dummy_authority, store_requested_authorities); |
412 | 412 |
413 // Check if the retrieved certificates were passed to the content browser | 413 // Check if the retrieved certificates were passed to the content browser |
414 // client. | 414 // client. |
415 EXPECT_EQ(1, test_client.call_count()); | 415 EXPECT_EQ(1, test_client.call_count()); |
416 EXPECT_EQ(dummy_certs, test_client.passed_certs()); | 416 EXPECT_EQ(dummy_certs, test_client.passed_certs()); |
417 } | 417 } |
418 | 418 |
419 // Verifies if a call to net::URLRequest::Delegate::OnCertificateRequested() | 419 // Verifies if a call to net::URLRequest::Delegate::OnCertificateRequested() |
420 // on a platform with a NULL client cert store still calls the content browser | 420 // on a platform with a NULL client cert store still calls the content browser |
421 // client for selection. | 421 // client for selection. |
(...skipping 17 matching lines...) Expand all Loading... | |
439 SetBrowserClientForTesting(old_client); | 439 SetBrowserClientForTesting(old_client); |
440 | 440 |
441 // Check if the SelectClientCertificate was called on the content browser | 441 // Check if the SelectClientCertificate was called on the content browser |
442 // client. | 442 // client. |
443 EXPECT_EQ(1, test_client.call_count()); | 443 EXPECT_EQ(1, test_client.call_count()); |
444 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); | 444 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); |
445 } | 445 } |
446 | 446 |
447 TEST_F(ResourceLoaderTest, ClientCertStoreAsyncCancel) { | 447 TEST_F(ResourceLoaderTest, ClientCertStoreAsyncCancel) { |
448 // Set up the test client cert store. | 448 // Set up the test client cert store. |
449 int store_request_count; | |
450 std::vector<std::string> store_requested_authorities; | |
449 scoped_ptr<ClientCertStoreStub> test_store( | 451 scoped_ptr<ClientCertStoreStub> test_store( |
450 new ClientCertStoreStub(net::CertificateList())); | 452 new ClientCertStoreStub(net::CertificateList(), &store_request_count, |
453 &store_requested_authorities)); | |
451 test_store->set_async(true); | 454 test_store->set_async(true); |
452 EXPECT_EQ(0, test_store->request_count()); | 455 EXPECT_EQ(0, store_request_count); |
453 | |
454 // Ownership of the |test_store| is about to be turned over to ResourceLoader. | |
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()); | 456 resource_context_.SetClientCertStore(test_store.Pass()); |
458 | 457 |
459 // Prepare a dummy certificate request. | 458 // Prepare a dummy certificate request. |
460 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( | 459 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( |
461 new net::SSLCertRequestInfo()); | 460 new net::SSLCertRequestInfo()); |
462 std::vector<std::string> dummy_authority(1, "dummy"); | 461 std::vector<std::string> dummy_authority(1, "dummy"); |
463 cert_request_info->cert_authorities = dummy_authority; | 462 cert_request_info->cert_authorities = dummy_authority; |
464 | 463 |
465 // Everything is set up. Trigger the resource loader certificate request | 464 // Everything is set up. Trigger the resource loader certificate request |
466 // event. | 465 // event. |
467 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get()); | 466 loader_->OnCertificateRequested(raw_ptr_to_request_, cert_request_info.get()); |
468 | 467 |
469 // Check if the test store was queried against correct |cert_authorities|. | 468 // Check if the test store was queried against correct |cert_authorities|. |
470 EXPECT_EQ(1, raw_ptr_to_store->request_count()); | 469 EXPECT_EQ(1, store_request_count); |
471 EXPECT_EQ(dummy_authority, raw_ptr_to_store->requested_authorities()); | 470 EXPECT_EQ(dummy_authority, store_requested_authorities); |
472 | 471 |
473 // Cancel the request before the store calls the callback. | 472 // Cancel the request before the store calls the callback. |
474 loader_.reset(); | 473 loader_.reset(); |
475 | 474 |
476 // Pump the event loop. There shouldn't be a crash when the callback is run. | 475 // Pump the event loop. There shouldn't be a crash when the callback is run. |
477 base::RunLoop().RunUntilIdle(); | 476 base::RunLoop().RunUntilIdle(); |
478 } | 477 } |
479 | 478 |
480 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { | 479 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { |
481 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); | 480 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
761 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); | 760 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); |
762 EXPECT_EQ(test_data(), contents); | 761 EXPECT_EQ(test_data(), contents); |
763 | 762 |
764 // Release the loader. The file should be gone now. | 763 // Release the loader. The file should be gone now. |
765 ReleaseLoader(); | 764 ReleaseLoader(); |
766 base::RunLoop().RunUntilIdle(); | 765 base::RunLoop().RunUntilIdle(); |
767 EXPECT_FALSE(base::PathExists(temp_path())); | 766 EXPECT_FALSE(base::PathExists(temp_path())); |
768 } | 767 } |
769 | 768 |
770 } // namespace content | 769 } // namespace content |
OLD | NEW |