OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/browsing_data/browsing_data_server_bound_cert_helper.h" | 5 #include "chrome/browser/browsing_data/browsing_data_server_bound_cert_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/synchronization/waitable_event.h" | |
10 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
11 #include "content/public/test/test_browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
12 #include "net/ssl/server_bound_cert_service.h" | 12 #include "net/ssl/server_bound_cert_service.h" |
13 #include "net/url_request/url_request_context.h" | 13 #include "net/url_request/url_request_context.h" |
14 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using content::BrowserThread; | 17 using content::BrowserThread; |
18 | 18 |
19 class BrowsingDataServerBoundCertHelperTest | 19 class BrowsingDataServerBoundCertHelperTest |
20 : public testing::Test, | 20 : public testing::Test, |
21 public net::SSLConfigService::Observer { | 21 public net::SSLConfigService::Observer { |
22 public: | 22 public: |
23 BrowsingDataServerBoundCertHelperTest() : ssl_config_changed_count_(0) { | 23 BrowsingDataServerBoundCertHelperTest() : ssl_config_changed_count_(0) { |
24 } | 24 } |
25 | 25 |
26 virtual void SetUp() OVERRIDE { | 26 virtual void SetUp() OVERRIDE { |
27 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | |
28 &message_loop_)); | |
29 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO, | |
30 &message_loop_)); | |
31 testing_profile_.reset(new TestingProfile()); | 27 testing_profile_.reset(new TestingProfile()); |
32 testing_profile_->CreateRequestContext(); | |
33 | 28 |
34 testing_profile_->GetSSLConfigService()->AddObserver(this); | 29 testing_profile_->GetSSLConfigService()->AddObserver(this); |
35 } | 30 } |
36 | 31 |
37 virtual void TearDown() OVERRIDE { | 32 virtual void TearDown() OVERRIDE { |
38 testing_profile_->GetSSLConfigService()->RemoveObserver(this); | 33 testing_profile_->GetSSLConfigService()->RemoveObserver(this); |
39 } | 34 } |
40 | 35 |
41 void CreateCertsForTest() { | 36 void CreateCertsForTest() { |
42 net::URLRequestContext* context = | 37 net::URLRequestContext* context = |
43 testing_profile_->GetRequestContext()->GetURLRequestContext(); | 38 testing_profile_->GetRequestContext()->GetURLRequestContext(); |
44 net::ServerBoundCertStore* cert_store = | 39 net::ServerBoundCertStore* cert_store = |
45 context->server_bound_cert_service()->GetCertStore(); | 40 context->server_bound_cert_service()->GetCertStore(); |
46 cert_store->SetServerBoundCert("https://www.google.com:443", | 41 cert_store->SetServerBoundCert("https://www.google.com:443", |
47 net::CLIENT_CERT_RSA_SIGN, | 42 net::CLIENT_CERT_RSA_SIGN, |
48 base::Time(), base::Time(), | 43 base::Time(), base::Time(), |
49 "key", "cert"); | 44 "key", "cert"); |
50 cert_store->SetServerBoundCert("https://www.youtube.com:443", | 45 cert_store->SetServerBoundCert("https://www.youtube.com:443", |
51 net::CLIENT_CERT_RSA_SIGN, | 46 net::CLIENT_CERT_RSA_SIGN, |
52 base::Time(), base::Time(), | 47 base::Time(), base::Time(), |
53 "key", "cert"); | 48 "key", "cert"); |
54 } | 49 } |
55 | 50 |
56 void FetchCallback( | 51 void FetchCallback( |
57 const net::ServerBoundCertStore::ServerBoundCertList& certs) { | 52 const net::ServerBoundCertStore::ServerBoundCertList& certs) { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
59 server_bound_cert_list_ = certs; | 54 server_bound_cert_list_ = certs; |
60 base::MessageLoop::current()->Quit(); | |
61 } | 55 } |
62 | 56 |
63 // net::SSLConfigService::Observer implementation: | 57 // net::SSLConfigService::Observer implementation: |
64 virtual void OnSSLConfigChanged() OVERRIDE { | 58 virtual void OnSSLConfigChanged() OVERRIDE { |
65 ssl_config_changed_count_++; | 59 ssl_config_changed_count_++; |
66 } | 60 } |
67 | 61 |
68 protected: | 62 protected: |
69 base::MessageLoop message_loop_; | 63 content::TestBrowserThreadBundle thread_bundle_; |
70 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
71 scoped_ptr<content::TestBrowserThread> io_thread_; | |
72 scoped_ptr<TestingProfile> testing_profile_; | 64 scoped_ptr<TestingProfile> testing_profile_; |
73 | 65 |
74 net::ServerBoundCertStore::ServerBoundCertList server_bound_cert_list_; | 66 net::ServerBoundCertStore::ServerBoundCertList server_bound_cert_list_; |
75 | 67 |
76 int ssl_config_changed_count_; | 68 int ssl_config_changed_count_; |
77 }; | 69 }; |
78 | 70 |
79 TEST_F(BrowsingDataServerBoundCertHelperTest, FetchData) { | 71 TEST_F(BrowsingDataServerBoundCertHelperTest, FetchData) { |
80 CreateCertsForTest(); | 72 CreateCertsForTest(); |
81 scoped_refptr<BrowsingDataServerBoundCertHelper> helper( | 73 scoped_refptr<BrowsingDataServerBoundCertHelper> helper( |
82 BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); | 74 BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); |
83 | 75 |
84 helper->StartFetching( | 76 helper->StartFetching( |
85 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | 77 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, |
86 base::Unretained(this))); | 78 base::Unretained(this))); |
87 | 79 |
88 // Blocks until BrowsingDataServerBoundCertHelperTest::FetchCallback is | 80 // Blocks until BrowsingDataServerBoundCertHelperTest::FetchCallback is |
89 // notified. | 81 // notified. |
90 base::MessageLoop::current()->Run(); | 82 base::RunLoop().RunUntilIdle(); |
91 | 83 |
92 ASSERT_EQ(2UL, server_bound_cert_list_.size()); | 84 ASSERT_EQ(2UL, server_bound_cert_list_.size()); |
93 net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = | 85 net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = |
94 server_bound_cert_list_.begin(); | 86 server_bound_cert_list_.begin(); |
95 | 87 |
96 // Correct because fetching server_bound_cert_list_ will get them out in the | 88 // Correct because fetching server_bound_cert_list_ will get them out in the |
97 // same order CreateCertsForTest put them in. | 89 // same order CreateCertsForTest put them in. |
98 ASSERT_TRUE(it != server_bound_cert_list_.end()); | 90 ASSERT_TRUE(it != server_bound_cert_list_.end()); |
99 EXPECT_EQ("https://www.google.com:443", it->server_identifier()); | 91 EXPECT_EQ("https://www.google.com:443", it->server_identifier()); |
100 | 92 |
101 ASSERT_TRUE(++it != server_bound_cert_list_.end()); | 93 ASSERT_TRUE(++it != server_bound_cert_list_.end()); |
102 EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); | 94 EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); |
103 | 95 |
104 ASSERT_TRUE(++it == server_bound_cert_list_.end()); | 96 ASSERT_TRUE(++it == server_bound_cert_list_.end()); |
105 | 97 |
106 EXPECT_EQ(0, ssl_config_changed_count_); | 98 EXPECT_EQ(0, ssl_config_changed_count_); |
107 } | 99 } |
108 | 100 |
109 TEST_F(BrowsingDataServerBoundCertHelperTest, DeleteCert) { | 101 TEST_F(BrowsingDataServerBoundCertHelperTest, DeleteCert) { |
110 CreateCertsForTest(); | 102 CreateCertsForTest(); |
111 scoped_refptr<BrowsingDataServerBoundCertHelper> helper( | 103 scoped_refptr<BrowsingDataServerBoundCertHelper> helper( |
112 BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); | 104 BrowsingDataServerBoundCertHelper::Create(testing_profile_.get())); |
113 | 105 |
114 helper->DeleteServerBoundCert("https://www.google.com:443"); | 106 helper->DeleteServerBoundCert("https://www.google.com:443"); |
115 | 107 |
116 helper->StartFetching( | 108 helper->StartFetching( |
117 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | 109 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, |
118 base::Unretained(this))); | 110 base::Unretained(this))); |
119 base::MessageLoop::current()->Run(); | 111 base::RunLoop().RunUntilIdle(); |
120 | 112 |
121 EXPECT_EQ(1, ssl_config_changed_count_); | 113 EXPECT_EQ(1, ssl_config_changed_count_); |
122 ASSERT_EQ(1UL, server_bound_cert_list_.size()); | 114 ASSERT_EQ(1UL, server_bound_cert_list_.size()); |
123 net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = | 115 net::ServerBoundCertStore::ServerBoundCertList::const_iterator it = |
124 server_bound_cert_list_.begin(); | 116 server_bound_cert_list_.begin(); |
125 | 117 |
126 ASSERT_TRUE(it != server_bound_cert_list_.end()); | 118 ASSERT_TRUE(it != server_bound_cert_list_.end()); |
127 EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); | 119 EXPECT_EQ("https://www.youtube.com:443", it->server_identifier()); |
128 | 120 |
129 ASSERT_TRUE(++it == server_bound_cert_list_.end()); | 121 ASSERT_TRUE(++it == server_bound_cert_list_.end()); |
130 | 122 |
131 helper->DeleteServerBoundCert("https://www.youtube.com:443"); | 123 helper->DeleteServerBoundCert("https://www.youtube.com:443"); |
132 | 124 |
133 helper->StartFetching( | 125 helper->StartFetching( |
134 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | 126 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, |
135 base::Unretained(this))); | 127 base::Unretained(this))); |
136 base::MessageLoop::current()->Run(); | 128 base::RunLoop().RunUntilIdle(); |
| 129 |
137 EXPECT_EQ(2, ssl_config_changed_count_); | 130 EXPECT_EQ(2, ssl_config_changed_count_); |
138 ASSERT_EQ(0UL, server_bound_cert_list_.size()); | 131 ASSERT_EQ(0UL, server_bound_cert_list_.size()); |
139 } | 132 } |
140 | 133 |
141 TEST_F(BrowsingDataServerBoundCertHelperTest, CannedUnique) { | 134 TEST_F(BrowsingDataServerBoundCertHelperTest, CannedUnique) { |
142 std::string origin = "https://www.google.com:443"; | 135 std::string origin = "https://www.google.com:443"; |
143 | 136 |
144 scoped_refptr<CannedBrowsingDataServerBoundCertHelper> helper( | 137 scoped_refptr<CannedBrowsingDataServerBoundCertHelper> helper( |
145 new CannedBrowsingDataServerBoundCertHelper()); | 138 new CannedBrowsingDataServerBoundCertHelper()); |
146 | 139 |
147 ASSERT_TRUE(helper->empty()); | 140 ASSERT_TRUE(helper->empty()); |
148 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( | 141 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( |
149 origin, net::CLIENT_CERT_RSA_SIGN, base::Time(), base::Time(), "key", | 142 origin, net::CLIENT_CERT_RSA_SIGN, base::Time(), base::Time(), "key", |
150 "cert")); | 143 "cert")); |
151 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( | 144 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( |
152 origin, net::CLIENT_CERT_ECDSA_SIGN, base::Time(), base::Time(), "key", | 145 origin, net::CLIENT_CERT_ECDSA_SIGN, base::Time(), base::Time(), "key", |
153 "cert")); | 146 "cert")); |
154 | 147 |
155 helper->StartFetching( | 148 helper->StartFetching( |
156 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, | 149 base::Bind(&BrowsingDataServerBoundCertHelperTest::FetchCallback, |
157 base::Unretained(this))); | 150 base::Unretained(this))); |
158 base::MessageLoop::current()->Run(); | 151 base::RunLoop().RunUntilIdle(); |
159 | 152 |
160 ASSERT_EQ(1UL, server_bound_cert_list_.size()); | 153 ASSERT_EQ(1UL, server_bound_cert_list_.size()); |
161 net::ServerBoundCertStore::ServerBoundCert& cert = | 154 net::ServerBoundCertStore::ServerBoundCert& cert = |
162 server_bound_cert_list_.front(); | 155 server_bound_cert_list_.front(); |
163 | 156 |
164 EXPECT_EQ("https://www.google.com:443", cert.server_identifier()); | 157 EXPECT_EQ("https://www.google.com:443", cert.server_identifier()); |
165 EXPECT_EQ(net::CLIENT_CERT_ECDSA_SIGN, cert.type()); | 158 EXPECT_EQ(net::CLIENT_CERT_ECDSA_SIGN, cert.type()); |
166 EXPECT_EQ(0, ssl_config_changed_count_); | 159 EXPECT_EQ(0, ssl_config_changed_count_); |
167 } | 160 } |
168 | 161 |
169 TEST_F(BrowsingDataServerBoundCertHelperTest, CannedEmpty) { | 162 TEST_F(BrowsingDataServerBoundCertHelperTest, CannedEmpty) { |
170 std::string origin = "https://www.google.com"; | 163 std::string origin = "https://www.google.com"; |
171 | 164 |
172 scoped_refptr<CannedBrowsingDataServerBoundCertHelper> helper( | 165 scoped_refptr<CannedBrowsingDataServerBoundCertHelper> helper( |
173 new CannedBrowsingDataServerBoundCertHelper()); | 166 new CannedBrowsingDataServerBoundCertHelper()); |
174 | 167 |
175 ASSERT_TRUE(helper->empty()); | 168 ASSERT_TRUE(helper->empty()); |
176 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( | 169 helper->AddServerBoundCert(net::ServerBoundCertStore::ServerBoundCert( |
177 origin, net::CLIENT_CERT_RSA_SIGN, base::Time(), base::Time(), "key", | 170 origin, net::CLIENT_CERT_RSA_SIGN, base::Time(), base::Time(), "key", |
178 "cert")); | 171 "cert")); |
179 ASSERT_FALSE(helper->empty()); | 172 ASSERT_FALSE(helper->empty()); |
180 helper->Reset(); | 173 helper->Reset(); |
181 ASSERT_TRUE(helper->empty()); | 174 ASSERT_TRUE(helper->empty()); |
182 } | 175 } |
OLD | NEW |