Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/browsing_data/browsing_data_server_bound_cert_helper.h" |
| 6 | |
| 7 | #include "base/bind.h" |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 8 | #include "base/run_loop.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 9 | #include "chrome/test/base/testing_profile.h" |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 10 | #include "content/public/browser/browser_thread.h" |
| 11 | #include "content/public/test/test_browser_thread_bundle.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "net/ssl/server_bound_cert_service.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "net/url_request/url_request_context.h" |
| 14 | #include "net/url_request/url_request_context_getter.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | using content::BrowserThread; |
| 18 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 19 | class BrowsingDataServerBoundCertHelperTest |
| 20 | : public testing::Test, |
| 21 | public net::SSLConfigService::Observer { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | public: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 23 | BrowsingDataServerBoundCertHelperTest() : ssl_config_changed_count_(0) { |
| 24 | } |
| 25 | |
| 26 | virtual void SetUp() OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | testing_profile_.reset(new TestingProfile()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 28 | |
| 29 | testing_profile_->GetSSLConfigService()->AddObserver(this); |
| 30 | } |
| 31 | |
| 32 | virtual void TearDown() OVERRIDE { |
| 33 | testing_profile_->GetSSLConfigService()->RemoveObserver(this); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void CreateCertsForTest() { |
| 37 | net::URLRequestContext* context = |
| 38 | testing_profile_->GetRequestContext()->GetURLRequestContext(); |
| 39 | net::ServerBoundCertStore* cert_store = |
| 40 | context->server_bound_cert_service()->GetCertStore(); |
| 41 | cert_store->SetServerBoundCert("https://www.google.com:443", |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 42 | base::Time(), base::Time(), |
| 43 | "key", "cert"); |
| 44 | cert_store->SetServerBoundCert("https://www.youtube.com:443", |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 45 | base::Time(), base::Time(), |
| 46 | "key", "cert"); |
| 47 | } |
| 48 | |
| 49 | void FetchCallback( |
| 50 | const net::ServerBoundCertStore::ServerBoundCertList& certs) { |
| 51 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 52 | server_bound_cert_list_ = certs; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 55 | // net::SSLConfigService::Observer implementation: |
| 56 | virtual void OnSSLConfigChanged() OVERRIDE { |
| 57 | ssl_config_changed_count_++; |
| 58 | } |
| 59 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 60 | protected: |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 61 | content::TestBrowserThreadBundle thread_bundle_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 62 | scoped_ptr<TestingProfile> testing_profile_; |
| 63 | |
| 64 | net::ServerBoundCertStore::ServerBoundCertList server_bound_cert_list_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 65 | |
| 66 | int ssl_config_changed_count_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | TEST_F(BrowsingDataServerBoundCertHelperTest, FetchData) { |
| 70 | CreateCertsForTest(); |
| 71 | scoped_refptr<BrowsingDataServerBoundCertHelper> helper( |
| 72 | BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); |
| 73 | |
| 74 | helper->StartFetching( |
| 75 | base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, |
| 76 | base::Unretained(this))); |
| 77 | |
| 78 | // Blocks until BrowsingDataServerBoundCertHelperTest::FetchCallback is |
| 79 | // notified. |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 80 | base::RunLoop().RunUntilIdle(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 81 | |
| 82 | ASSERT_EQ(2UL, server_bound_cert_list_.size()); |
| 83 | net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = |
| 84 | server_bound_cert_list_.begin(); |
| 85 | |
| 86 | // Correct because fetching server_bound_cert_list_ will get them out in the |
| 87 | // same order CreateCertsForTest put them in. |
| 88 | ASSERT_TRUE(it != server_bound_cert_list_.end()); |
| 89 | EXPECT_EQ("https://www.google.com:443", it->server_identifier()); |
| 90 | |
| 91 | ASSERT_TRUE(++it != server_bound_cert_list_.end()); |
| 92 | EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); |
| 93 | |
| 94 | ASSERT_TRUE(++it == server_bound_cert_list_.end()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 95 | |
| 96 | EXPECT_EQ(0, ssl_config_changed_count_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(BrowsingDataServerBoundCertHelperTest, DeleteCert) { |
| 100 | CreateCertsForTest(); |
| 101 | scoped_refptr<BrowsingDataServerBoundCertHelper> helper( |
| 102 | BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); |
| 103 | |
| 104 | helper->DeleteServerBoundCert("https://www.google.com:443"); |
| 105 | |
| 106 | helper->StartFetching( |
| 107 | base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, |
| 108 | base::Unretained(this))); |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 109 | base::RunLoop().RunUntilIdle(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 110 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 111 | EXPECT_EQ(1, ssl_config_changed_count_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 112 | ASSERT_EQ(1UL, server_bound_cert_list_.size()); |
| 113 | net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = |
| 114 | server_bound_cert_list_.begin(); |
| 115 | |
| 116 | ASSERT_TRUE(it != server_bound_cert_list_.end()); |
| 117 | EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); |
| 118 | |
| 119 | ASSERT_TRUE(++it == server_bound_cert_list_.end()); |
| 120 | |
| 121 | helper->DeleteServerBoundCert("https://www.youtube.com:443"); |
| 122 | |
| 123 | helper->StartFetching( |
| 124 | base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, |
| 125 | base::Unretained(this))); |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 126 | base::RunLoop().RunUntilIdle(); |
| 127 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 128 | EXPECT_EQ(2, ssl_config_changed_count_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 129 | ASSERT_EQ(0UL, server_bound_cert_list_.size()); |
| 130 | } |
| 131 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 132 | TEST_F(BrowsingDataServerBoundCertHelperTest, CannedEmpty) { |
| 133 | std::string origin = "https://www.google.com"; |
| 134 | |
| 135 | scoped_refptr<CannedBrowsingDataServerBoundCertHelper> helper( |
| 136 | new CannedBrowsingDataServerBoundCertHelper()); |
| 137 | |
| 138 | ASSERT_TRUE(helper->empty()); |
| 139 | helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 140 | origin, base::Time(), base::Time(), "key", "cert")); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 141 | ASSERT_FALSE(helper->empty()); |
| 142 | helper->Reset(); |
| 143 | ASSERT_TRUE(helper->empty()); |
| 144 | } |