OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/ssl/channel_id_service.h" | 5 #include "net/ssl/channel_id_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/message_loop/message_loop_proxy.h" | |
14 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
| 14 #include "base/thread_task_runner_handle.h" |
15 #include "crypto/ec_private_key.h" | 15 #include "crypto/ec_private_key.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
18 #include "net/cert/asn1_util.h" | 18 #include "net/cert/asn1_util.h" |
19 #include "net/cert/x509_certificate.h" | 19 #include "net/cert/x509_certificate.h" |
20 #include "net/ssl/default_channel_id_store.h" | 20 #include "net/ssl/default_channel_id_store.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace net { | 23 namespace net { |
24 | 24 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 err, | 112 err, |
113 server_identifier_, | 113 server_identifier_, |
114 expiration_time, | 114 expiration_time, |
115 private_key, | 115 private_key, |
116 cert)); | 116 cert)); |
117 } | 117 } |
118 | 118 |
119 class ChannelIDServiceTest : public testing::Test { | 119 class ChannelIDServiceTest : public testing::Test { |
120 public: | 120 public: |
121 ChannelIDServiceTest() | 121 ChannelIDServiceTest() |
122 : service_(new ChannelIDService( | 122 : service_(new ChannelIDService(new DefaultChannelIDStore(NULL), |
123 new DefaultChannelIDStore(NULL), | 123 base::ThreadTaskRunnerHandle::Get())) {} |
124 base::MessageLoopProxy::current())) { | |
125 } | |
126 | 124 |
127 protected: | 125 protected: |
128 scoped_ptr<ChannelIDService> service_; | 126 scoped_ptr<ChannelIDService> service_; |
129 }; | 127 }; |
130 | 128 |
131 TEST_F(ChannelIDServiceTest, GetDomainForHost) { | 129 TEST_F(ChannelIDServiceTest, GetDomainForHost) { |
132 EXPECT_EQ("google.com", | 130 EXPECT_EQ("google.com", |
133 ChannelIDService::GetDomainForHost("google.com")); | 131 ChannelIDService::GetDomainForHost("google.com")); |
134 EXPECT_EQ("google.com", | 132 EXPECT_EQ("google.com", |
135 ChannelIDService::GetDomainForHost("www.google.com")); | 133 ChannelIDService::GetDomainForHost("www.google.com")); |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 EXPECT_EQ(OK, error); | 577 EXPECT_EQ(OK, error); |
580 EXPECT_FALSE(request_handle.is_active()); | 578 EXPECT_FALSE(request_handle.is_active()); |
581 EXPECT_EQ(2, service_->cert_count()); | 579 EXPECT_EQ(2, service_->cert_count()); |
582 EXPECT_STREQ("c", private_key_info2.c_str()); | 580 EXPECT_STREQ("c", private_key_info2.c_str()); |
583 EXPECT_STREQ("d", der_cert2.c_str()); | 581 EXPECT_STREQ("d", der_cert2.c_str()); |
584 } | 582 } |
585 | 583 |
586 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { | 584 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { |
587 MockChannelIDStoreWithAsyncGet* mock_store = | 585 MockChannelIDStoreWithAsyncGet* mock_store = |
588 new MockChannelIDStoreWithAsyncGet(); | 586 new MockChannelIDStoreWithAsyncGet(); |
589 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 587 service_ = scoped_ptr<ChannelIDService>( |
590 mock_store, base::MessageLoopProxy::current())); | 588 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
591 | 589 |
592 std::string host("encrypted.google.com"); | 590 std::string host("encrypted.google.com"); |
593 | 591 |
594 int error; | 592 int error; |
595 TestCompletionCallback callback; | 593 TestCompletionCallback callback; |
596 ChannelIDService::RequestHandle request_handle; | 594 ChannelIDService::RequestHandle request_handle; |
597 | 595 |
598 // Asynchronous completion with no certs in the store. | 596 // Asynchronous completion with no certs in the store. |
599 std::string private_key_info, der_cert; | 597 std::string private_key_info, der_cert; |
600 EXPECT_EQ(0, service_->cert_count()); | 598 EXPECT_EQ(0, service_->cert_count()); |
601 error = service_->GetOrCreateChannelID( | 599 error = service_->GetOrCreateChannelID( |
602 host, &private_key_info, &der_cert, callback.callback(), &request_handle); | 600 host, &private_key_info, &der_cert, callback.callback(), &request_handle); |
603 EXPECT_EQ(ERR_IO_PENDING, error); | 601 EXPECT_EQ(ERR_IO_PENDING, error); |
604 EXPECT_TRUE(request_handle.is_active()); | 602 EXPECT_TRUE(request_handle.is_active()); |
605 | 603 |
606 mock_store->CallGetChannelIDCallbackWithResult( | 604 mock_store->CallGetChannelIDCallbackWithResult( |
607 ERR_FILE_NOT_FOUND, base::Time(), std::string(), std::string()); | 605 ERR_FILE_NOT_FOUND, base::Time(), std::string(), std::string()); |
608 | 606 |
609 error = callback.WaitForResult(); | 607 error = callback.WaitForResult(); |
610 EXPECT_EQ(OK, error); | 608 EXPECT_EQ(OK, error); |
611 EXPECT_EQ(1, service_->cert_count()); | 609 EXPECT_EQ(1, service_->cert_count()); |
612 EXPECT_FALSE(private_key_info.empty()); | 610 EXPECT_FALSE(private_key_info.empty()); |
613 EXPECT_FALSE(der_cert.empty()); | 611 EXPECT_FALSE(der_cert.empty()); |
614 EXPECT_FALSE(request_handle.is_active()); | 612 EXPECT_FALSE(request_handle.is_active()); |
615 } | 613 } |
616 | 614 |
617 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { | 615 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { |
618 MockChannelIDStoreWithAsyncGet* mock_store = | 616 MockChannelIDStoreWithAsyncGet* mock_store = |
619 new MockChannelIDStoreWithAsyncGet(); | 617 new MockChannelIDStoreWithAsyncGet(); |
620 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 618 service_ = scoped_ptr<ChannelIDService>( |
621 mock_store, base::MessageLoopProxy::current())); | 619 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
622 | 620 |
623 std::string host("encrypted.google.com"); | 621 std::string host("encrypted.google.com"); |
624 | 622 |
625 int error; | 623 int error; |
626 TestCompletionCallback callback; | 624 TestCompletionCallback callback; |
627 ChannelIDService::RequestHandle request_handle; | 625 ChannelIDService::RequestHandle request_handle; |
628 | 626 |
629 // Asynchronous completion with no certs in the store. | 627 // Asynchronous completion with no certs in the store. |
630 std::string private_key, der_cert; | 628 std::string private_key, der_cert; |
631 EXPECT_EQ(0, service_->cert_count()); | 629 EXPECT_EQ(0, service_->cert_count()); |
632 error = service_->GetChannelID( | 630 error = service_->GetChannelID( |
633 host, &private_key, &der_cert, callback.callback(), &request_handle); | 631 host, &private_key, &der_cert, callback.callback(), &request_handle); |
634 EXPECT_EQ(ERR_IO_PENDING, error); | 632 EXPECT_EQ(ERR_IO_PENDING, error); |
635 EXPECT_TRUE(request_handle.is_active()); | 633 EXPECT_TRUE(request_handle.is_active()); |
636 | 634 |
637 mock_store->CallGetChannelIDCallbackWithResult( | 635 mock_store->CallGetChannelIDCallbackWithResult( |
638 ERR_FILE_NOT_FOUND, base::Time(), std::string(), std::string()); | 636 ERR_FILE_NOT_FOUND, base::Time(), std::string(), std::string()); |
639 | 637 |
640 error = callback.WaitForResult(); | 638 error = callback.WaitForResult(); |
641 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); | 639 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); |
642 EXPECT_EQ(0, service_->cert_count()); | 640 EXPECT_EQ(0, service_->cert_count()); |
643 EXPECT_EQ(0u, service_->workers_created()); | 641 EXPECT_EQ(0u, service_->workers_created()); |
644 EXPECT_TRUE(der_cert.empty()); | 642 EXPECT_TRUE(der_cert.empty()); |
645 EXPECT_FALSE(request_handle.is_active()); | 643 EXPECT_FALSE(request_handle.is_active()); |
646 } | 644 } |
647 | 645 |
648 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { | 646 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { |
649 MockChannelIDStoreWithAsyncGet* mock_store = | 647 MockChannelIDStoreWithAsyncGet* mock_store = |
650 new MockChannelIDStoreWithAsyncGet(); | 648 new MockChannelIDStoreWithAsyncGet(); |
651 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 649 service_ = scoped_ptr<ChannelIDService>( |
652 mock_store, base::MessageLoopProxy::current())); | 650 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
653 | 651 |
654 std::string host("encrypted.google.com"); | 652 std::string host("encrypted.google.com"); |
655 | 653 |
656 int error; | 654 int error; |
657 TestCompletionCallback callback; | 655 TestCompletionCallback callback; |
658 ChannelIDService::RequestHandle request_handle; | 656 ChannelIDService::RequestHandle request_handle; |
659 | 657 |
660 // Asynchronous completion with a cert in the store. | 658 // Asynchronous completion with a cert in the store. |
661 std::string private_key_info, der_cert; | 659 std::string private_key_info, der_cert; |
662 EXPECT_EQ(0, service_->cert_count()); | 660 EXPECT_EQ(0, service_->cert_count()); |
(...skipping 14 matching lines...) Expand all Loading... |
677 // created. | 675 // created. |
678 EXPECT_EQ(0u, service_->workers_created()); | 676 EXPECT_EQ(0u, service_->workers_created()); |
679 EXPECT_STREQ("ab", private_key_info.c_str()); | 677 EXPECT_STREQ("ab", private_key_info.c_str()); |
680 EXPECT_STREQ("cd", der_cert.c_str()); | 678 EXPECT_STREQ("cd", der_cert.c_str()); |
681 EXPECT_FALSE(request_handle.is_active()); | 679 EXPECT_FALSE(request_handle.is_active()); |
682 } | 680 } |
683 | 681 |
684 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { | 682 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { |
685 MockChannelIDStoreWithAsyncGet* mock_store = | 683 MockChannelIDStoreWithAsyncGet* mock_store = |
686 new MockChannelIDStoreWithAsyncGet(); | 684 new MockChannelIDStoreWithAsyncGet(); |
687 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 685 service_ = scoped_ptr<ChannelIDService>( |
688 mock_store, base::MessageLoopProxy::current())); | 686 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
689 | 687 |
690 std::string host("encrypted.google.com"); | 688 std::string host("encrypted.google.com"); |
691 | 689 |
692 int error; | 690 int error; |
693 TestCompletionCallback callback; | 691 TestCompletionCallback callback; |
694 ChannelIDService::RequestHandle request_handle; | 692 ChannelIDService::RequestHandle request_handle; |
695 | 693 |
696 // Asynchronous completion with a cert in the store. | 694 // Asynchronous completion with a cert in the store. |
697 std::string private_key, der_cert; | 695 std::string private_key, der_cert; |
698 EXPECT_EQ(0, service_->cert_count()); | 696 EXPECT_EQ(0, service_->cert_count()); |
(...skipping 13 matching lines...) Expand all Loading... |
712 // Because the cert was found in the store, no new workers should have been | 710 // Because the cert was found in the store, no new workers should have been |
713 // created. | 711 // created. |
714 EXPECT_EQ(0u, service_->workers_created()); | 712 EXPECT_EQ(0u, service_->workers_created()); |
715 EXPECT_STREQ("cd", der_cert.c_str()); | 713 EXPECT_STREQ("cd", der_cert.c_str()); |
716 EXPECT_FALSE(request_handle.is_active()); | 714 EXPECT_FALSE(request_handle.is_active()); |
717 } | 715 } |
718 | 716 |
719 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { | 717 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { |
720 MockChannelIDStoreWithAsyncGet* mock_store = | 718 MockChannelIDStoreWithAsyncGet* mock_store = |
721 new MockChannelIDStoreWithAsyncGet(); | 719 new MockChannelIDStoreWithAsyncGet(); |
722 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 720 service_ = scoped_ptr<ChannelIDService>( |
723 mock_store, base::MessageLoopProxy::current())); | 721 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
724 | 722 |
725 std::string host("encrypted.google.com"); | 723 std::string host("encrypted.google.com"); |
726 | 724 |
727 int error; | 725 int error; |
728 | 726 |
729 // Asynchronous get with no certs in the store. | 727 // Asynchronous get with no certs in the store. |
730 TestCompletionCallback callback1; | 728 TestCompletionCallback callback1; |
731 ChannelIDService::RequestHandle request_handle1; | 729 ChannelIDService::RequestHandle request_handle1; |
732 std::string private_key1, der_cert1; | 730 std::string private_key1, der_cert1; |
733 EXPECT_EQ(0, service_->cert_count()); | 731 EXPECT_EQ(0, service_->cert_count()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 EXPECT_EQ(der_cert1, der_cert2); | 763 EXPECT_EQ(der_cert1, der_cert2); |
766 EXPECT_FALSE(private_key1.empty()); | 764 EXPECT_FALSE(private_key1.empty()); |
767 EXPECT_EQ(private_key1, private_key2); | 765 EXPECT_EQ(private_key1, private_key2); |
768 EXPECT_FALSE(request_handle1.is_active()); | 766 EXPECT_FALSE(request_handle1.is_active()); |
769 EXPECT_FALSE(request_handle2.is_active()); | 767 EXPECT_FALSE(request_handle2.is_active()); |
770 } | 768 } |
771 | 769 |
772 } // namespace | 770 } // namespace |
773 | 771 |
774 } // namespace net | 772 } // namespace net |
OLD | NEW |