| 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 "net/ssl/default_server_bound_cert_store.h" | 5 #include "net/ssl/default_server_bound_cert_store.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 } | 50 } |
| 51 | 51 |
| 52 int err_; | 52 int err_; |
| 53 std::string server_identifier_; | 53 std::string server_identifier_; |
| 54 base::Time expiration_time_; | 54 base::Time expiration_time_; |
| 55 std::string private_key_; | 55 std::string private_key_; |
| 56 std::string cert_; | 56 std::string cert_; |
| 57 bool called_; | 57 bool called_; |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 void GetAllCallback( | 60 void GetAllCallback(ServerBoundCertStore::ServerBoundCertList* dest, |
| 61 ServerBoundCertStore::ServerBoundCertList* dest, | 61 const ServerBoundCertStore::ServerBoundCertList& result) { |
| 62 const ServerBoundCertStore::ServerBoundCertList& result) { | |
| 63 *dest = result; | 62 *dest = result; |
| 64 } | 63 } |
| 65 | 64 |
| 66 class MockPersistentStore | 65 class MockPersistentStore |
| 67 : public DefaultServerBoundCertStore::PersistentStore { | 66 : public DefaultServerBoundCertStore::PersistentStore { |
| 68 public: | 67 public: |
| 69 MockPersistentStore(); | 68 MockPersistentStore(); |
| 70 | 69 |
| 71 // DefaultServerBoundCertStore::PersistentStore implementation. | 70 // DefaultServerBoundCertStore::PersistentStore implementation. |
| 72 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; | 71 virtual void Load(const LoadedCallback& loaded_callback) OVERRIDE; |
| 73 virtual void AddServerBoundCert( | 72 virtual void AddServerBoundCert( |
| 74 const DefaultServerBoundCertStore::ServerBoundCert& cert) OVERRIDE; | 73 const DefaultServerBoundCertStore::ServerBoundCert& cert) OVERRIDE; |
| 75 virtual void DeleteServerBoundCert( | 74 virtual void DeleteServerBoundCert( |
| 76 const DefaultServerBoundCertStore::ServerBoundCert& cert) OVERRIDE; | 75 const DefaultServerBoundCertStore::ServerBoundCert& cert) OVERRIDE; |
| 77 virtual void SetForceKeepSessionState() OVERRIDE; | 76 virtual void SetForceKeepSessionState() OVERRIDE; |
| 78 | 77 |
| 79 protected: | 78 protected: |
| 80 virtual ~MockPersistentStore(); | 79 virtual ~MockPersistentStore(); |
| 81 | 80 |
| 82 private: | 81 private: |
| 83 typedef std::map<std::string, DefaultServerBoundCertStore::ServerBoundCert> | 82 typedef std::map<std::string, DefaultServerBoundCertStore::ServerBoundCert> |
| 84 ServerBoundCertMap; | 83 ServerBoundCertMap; |
| 85 | 84 |
| 86 ServerBoundCertMap origin_certs_; | 85 ServerBoundCertMap origin_certs_; |
| 87 }; | 86 }; |
| 88 | 87 |
| 89 MockPersistentStore::MockPersistentStore() {} | 88 MockPersistentStore::MockPersistentStore() { |
| 89 } |
| 90 | 90 |
| 91 void MockPersistentStore::Load(const LoadedCallback& loaded_callback) { | 91 void MockPersistentStore::Load(const LoadedCallback& loaded_callback) { |
| 92 scoped_ptr<ScopedVector<DefaultServerBoundCertStore::ServerBoundCert> > | 92 scoped_ptr<ScopedVector<DefaultServerBoundCertStore::ServerBoundCert> > certs( |
| 93 certs(new ScopedVector<DefaultServerBoundCertStore::ServerBoundCert>()); | 93 new ScopedVector<DefaultServerBoundCertStore::ServerBoundCert>()); |
| 94 ServerBoundCertMap::iterator it; | 94 ServerBoundCertMap::iterator it; |
| 95 | 95 |
| 96 for (it = origin_certs_.begin(); it != origin_certs_.end(); ++it) { | 96 for (it = origin_certs_.begin(); it != origin_certs_.end(); ++it) { |
| 97 certs->push_back( | 97 certs->push_back( |
| 98 new DefaultServerBoundCertStore::ServerBoundCert(it->second)); | 98 new DefaultServerBoundCertStore::ServerBoundCert(it->second)); |
| 99 } | 99 } |
| 100 | 100 |
| 101 base::MessageLoop::current()->PostTask( | 101 base::MessageLoop::current()->PostTask( |
| 102 FROM_HERE, base::Bind(loaded_callback, base::Passed(&certs))); | 102 FROM_HERE, base::Bind(loaded_callback, base::Passed(&certs))); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void MockPersistentStore::AddServerBoundCert( | 105 void MockPersistentStore::AddServerBoundCert( |
| 106 const DefaultServerBoundCertStore::ServerBoundCert& cert) { | 106 const DefaultServerBoundCertStore::ServerBoundCert& cert) { |
| 107 origin_certs_[cert.server_identifier()] = cert; | 107 origin_certs_[cert.server_identifier()] = cert; |
| 108 } | 108 } |
| 109 | 109 |
| 110 void MockPersistentStore::DeleteServerBoundCert( | 110 void MockPersistentStore::DeleteServerBoundCert( |
| 111 const DefaultServerBoundCertStore::ServerBoundCert& cert) { | 111 const DefaultServerBoundCertStore::ServerBoundCert& cert) { |
| 112 origin_certs_.erase(cert.server_identifier()); | 112 origin_certs_.erase(cert.server_identifier()); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void MockPersistentStore::SetForceKeepSessionState() {} | 115 void MockPersistentStore::SetForceKeepSessionState() { |
| 116 } |
| 116 | 117 |
| 117 MockPersistentStore::~MockPersistentStore() {} | 118 MockPersistentStore::~MockPersistentStore() { |
| 119 } |
| 118 | 120 |
| 119 } // namespace | 121 } // namespace |
| 120 | 122 |
| 121 TEST(DefaultServerBoundCertStoreTest, TestLoading) { | 123 TEST(DefaultServerBoundCertStoreTest, TestLoading) { |
| 122 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 124 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 123 | 125 |
| 124 persistent_store->AddServerBoundCert( | 126 persistent_store->AddServerBoundCert( |
| 125 DefaultServerBoundCertStore::ServerBoundCert( | 127 DefaultServerBoundCertStore::ServerBoundCert( |
| 126 "google.com", | 128 "google.com", base::Time(), base::Time(), "a", "b")); |
| 127 base::Time(), | |
| 128 base::Time(), | |
| 129 "a", "b")); | |
| 130 persistent_store->AddServerBoundCert( | 129 persistent_store->AddServerBoundCert( |
| 131 DefaultServerBoundCertStore::ServerBoundCert( | 130 DefaultServerBoundCertStore::ServerBoundCert( |
| 132 "verisign.com", | 131 "verisign.com", base::Time(), base::Time(), "c", "d")); |
| 133 base::Time(), | |
| 134 base::Time(), | |
| 135 "c", "d")); | |
| 136 | 132 |
| 137 // Make sure certs load properly. | 133 // Make sure certs load properly. |
| 138 DefaultServerBoundCertStore store(persistent_store.get()); | 134 DefaultServerBoundCertStore store(persistent_store.get()); |
| 139 // Load has not occurred yet. | 135 // Load has not occurred yet. |
| 140 EXPECT_EQ(0, store.GetCertCount()); | 136 EXPECT_EQ(0, store.GetCertCount()); |
| 141 store.SetServerBoundCert( | 137 store.SetServerBoundCert( |
| 142 "verisign.com", | 138 "verisign.com", base::Time(), base::Time(), "e", "f"); |
| 143 base::Time(), | |
| 144 base::Time(), | |
| 145 "e", "f"); | |
| 146 // Wait for load & queued set task. | 139 // Wait for load & queued set task. |
| 147 base::MessageLoop::current()->RunUntilIdle(); | 140 base::MessageLoop::current()->RunUntilIdle(); |
| 148 EXPECT_EQ(2, store.GetCertCount()); | 141 EXPECT_EQ(2, store.GetCertCount()); |
| 149 store.SetServerBoundCert( | 142 store.SetServerBoundCert("twitter.com", base::Time(), base::Time(), "g", "h"); |
| 150 "twitter.com", | |
| 151 base::Time(), | |
| 152 base::Time(), | |
| 153 "g", "h"); | |
| 154 // Set should be synchronous now that load is done. | 143 // Set should be synchronous now that load is done. |
| 155 EXPECT_EQ(3, store.GetCertCount()); | 144 EXPECT_EQ(3, store.GetCertCount()); |
| 156 } | 145 } |
| 157 | 146 |
| 158 //TODO(mattm): add more tests of without a persistent store? | 147 // TODO(mattm): add more tests of without a persistent store? |
| 159 TEST(DefaultServerBoundCertStoreTest, TestSettingAndGetting) { | 148 TEST(DefaultServerBoundCertStoreTest, TestSettingAndGetting) { |
| 160 // No persistent store, all calls will be synchronous. | 149 // No persistent store, all calls will be synchronous. |
| 161 DefaultServerBoundCertStore store(NULL); | 150 DefaultServerBoundCertStore store(NULL); |
| 162 base::Time expiration_time; | 151 base::Time expiration_time; |
| 163 std::string private_key, cert; | 152 std::string private_key, cert; |
| 164 EXPECT_EQ(0, store.GetCertCount()); | 153 EXPECT_EQ(0, store.GetCertCount()); |
| 165 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 154 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 166 store.GetServerBoundCert("verisign.com", | 155 store.GetServerBoundCert("verisign.com", |
| 167 &expiration_time, | 156 &expiration_time, |
| 168 &private_key, | 157 &private_key, |
| 169 &cert, | 158 &cert, |
| 170 base::Bind(&GetCertCallbackNotCalled))); | 159 base::Bind(&GetCertCallbackNotCalled))); |
| 171 EXPECT_TRUE(private_key.empty()); | 160 EXPECT_TRUE(private_key.empty()); |
| 172 EXPECT_TRUE(cert.empty()); | 161 EXPECT_TRUE(cert.empty()); |
| 173 store.SetServerBoundCert( | 162 store.SetServerBoundCert("verisign.com", |
| 174 "verisign.com", | 163 base::Time::FromInternalValue(123), |
| 175 base::Time::FromInternalValue(123), | 164 base::Time::FromInternalValue(456), |
| 176 base::Time::FromInternalValue(456), | 165 "i", |
| 177 "i", "j"); | 166 "j"); |
| 178 EXPECT_EQ(OK, | 167 EXPECT_EQ(OK, |
| 179 store.GetServerBoundCert("verisign.com", | 168 store.GetServerBoundCert("verisign.com", |
| 180 &expiration_time, | 169 &expiration_time, |
| 181 &private_key, | 170 &private_key, |
| 182 &cert, | 171 &cert, |
| 183 base::Bind(&GetCertCallbackNotCalled))); | 172 base::Bind(&GetCertCallbackNotCalled))); |
| 184 EXPECT_EQ(456, expiration_time.ToInternalValue()); | 173 EXPECT_EQ(456, expiration_time.ToInternalValue()); |
| 185 EXPECT_EQ("i", private_key); | 174 EXPECT_EQ("i", private_key); |
| 186 EXPECT_EQ("j", cert); | 175 EXPECT_EQ("j", cert); |
| 187 } | 176 } |
| 188 | 177 |
| 189 TEST(DefaultServerBoundCertStoreTest, TestDuplicateCerts) { | 178 TEST(DefaultServerBoundCertStoreTest, TestDuplicateCerts) { |
| 190 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 179 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 191 DefaultServerBoundCertStore store(persistent_store.get()); | 180 DefaultServerBoundCertStore store(persistent_store.get()); |
| 192 | 181 |
| 193 base::Time expiration_time; | 182 base::Time expiration_time; |
| 194 std::string private_key, cert; | 183 std::string private_key, cert; |
| 195 EXPECT_EQ(0, store.GetCertCount()); | 184 EXPECT_EQ(0, store.GetCertCount()); |
| 196 store.SetServerBoundCert( | 185 store.SetServerBoundCert("verisign.com", |
| 197 "verisign.com", | 186 base::Time::FromInternalValue(123), |
| 198 base::Time::FromInternalValue(123), | 187 base::Time::FromInternalValue(1234), |
| 199 base::Time::FromInternalValue(1234), | 188 "a", |
| 200 "a", "b"); | 189 "b"); |
| 201 store.SetServerBoundCert( | 190 store.SetServerBoundCert("verisign.com", |
| 202 "verisign.com", | 191 base::Time::FromInternalValue(456), |
| 203 base::Time::FromInternalValue(456), | 192 base::Time::FromInternalValue(4567), |
| 204 base::Time::FromInternalValue(4567), | 193 "c", |
| 205 "c", "d"); | 194 "d"); |
| 206 | 195 |
| 207 // Wait for load & queued set tasks. | 196 // Wait for load & queued set tasks. |
| 208 base::MessageLoop::current()->RunUntilIdle(); | 197 base::MessageLoop::current()->RunUntilIdle(); |
| 209 EXPECT_EQ(1, store.GetCertCount()); | 198 EXPECT_EQ(1, store.GetCertCount()); |
| 210 EXPECT_EQ(OK, | 199 EXPECT_EQ(OK, |
| 211 store.GetServerBoundCert("verisign.com", | 200 store.GetServerBoundCert("verisign.com", |
| 212 &expiration_time, | 201 &expiration_time, |
| 213 &private_key, | 202 &private_key, |
| 214 &cert, | 203 &cert, |
| 215 base::Bind(&GetCertCallbackNotCalled))); | 204 base::Bind(&GetCertCallbackNotCalled))); |
| 216 EXPECT_EQ(4567, expiration_time.ToInternalValue()); | 205 EXPECT_EQ(4567, expiration_time.ToInternalValue()); |
| 217 EXPECT_EQ("c", private_key); | 206 EXPECT_EQ("c", private_key); |
| 218 EXPECT_EQ("d", cert); | 207 EXPECT_EQ("d", cert); |
| 219 } | 208 } |
| 220 | 209 |
| 221 TEST(DefaultServerBoundCertStoreTest, TestAsyncGet) { | 210 TEST(DefaultServerBoundCertStoreTest, TestAsyncGet) { |
| 222 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 211 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 223 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( | 212 persistent_store->AddServerBoundCert( |
| 224 "verisign.com", | 213 ServerBoundCertStore::ServerBoundCert("verisign.com", |
| 225 base::Time::FromInternalValue(123), | 214 base::Time::FromInternalValue(123), |
| 226 base::Time::FromInternalValue(1234), | 215 base::Time::FromInternalValue(1234), |
| 227 "a", "b")); | 216 "a", |
| 217 "b")); |
| 228 | 218 |
| 229 DefaultServerBoundCertStore store(persistent_store.get()); | 219 DefaultServerBoundCertStore store(persistent_store.get()); |
| 230 AsyncGetCertHelper helper; | 220 AsyncGetCertHelper helper; |
| 231 base::Time expiration_time; | 221 base::Time expiration_time; |
| 232 std::string private_key; | 222 std::string private_key; |
| 233 std::string cert = "not set"; | 223 std::string cert = "not set"; |
| 234 EXPECT_EQ(0, store.GetCertCount()); | 224 EXPECT_EQ(0, store.GetCertCount()); |
| 235 EXPECT_EQ(ERR_IO_PENDING, | 225 EXPECT_EQ(ERR_IO_PENDING, |
| 236 store.GetServerBoundCert("verisign.com", | 226 store.GetServerBoundCert("verisign.com", |
| 237 &expiration_time, | 227 &expiration_time, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 250 EXPECT_EQ(1234, helper.expiration_time_.ToInternalValue()); | 240 EXPECT_EQ(1234, helper.expiration_time_.ToInternalValue()); |
| 251 EXPECT_EQ("a", helper.private_key_); | 241 EXPECT_EQ("a", helper.private_key_); |
| 252 EXPECT_EQ("b", helper.cert_); | 242 EXPECT_EQ("b", helper.cert_); |
| 253 } | 243 } |
| 254 | 244 |
| 255 TEST(DefaultServerBoundCertStoreTest, TestDeleteAll) { | 245 TEST(DefaultServerBoundCertStoreTest, TestDeleteAll) { |
| 256 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 246 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 257 DefaultServerBoundCertStore store(persistent_store.get()); | 247 DefaultServerBoundCertStore store(persistent_store.get()); |
| 258 | 248 |
| 259 store.SetServerBoundCert( | 249 store.SetServerBoundCert( |
| 260 "verisign.com", | 250 "verisign.com", base::Time(), base::Time(), "a", "b"); |
| 261 base::Time(), | 251 store.SetServerBoundCert("google.com", base::Time(), base::Time(), "c", "d"); |
| 262 base::Time(), | 252 store.SetServerBoundCert("harvard.com", base::Time(), base::Time(), "e", "f"); |
| 263 "a", "b"); | |
| 264 store.SetServerBoundCert( | |
| 265 "google.com", | |
| 266 base::Time(), | |
| 267 base::Time(), | |
| 268 "c", "d"); | |
| 269 store.SetServerBoundCert( | |
| 270 "harvard.com", | |
| 271 base::Time(), | |
| 272 base::Time(), | |
| 273 "e", "f"); | |
| 274 // Wait for load & queued set tasks. | 253 // Wait for load & queued set tasks. |
| 275 base::MessageLoop::current()->RunUntilIdle(); | 254 base::MessageLoop::current()->RunUntilIdle(); |
| 276 | 255 |
| 277 EXPECT_EQ(3, store.GetCertCount()); | 256 EXPECT_EQ(3, store.GetCertCount()); |
| 278 int delete_finished = 0; | 257 int delete_finished = 0; |
| 279 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); | 258 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); |
| 280 ASSERT_EQ(1, delete_finished); | 259 ASSERT_EQ(1, delete_finished); |
| 281 EXPECT_EQ(0, store.GetCertCount()); | 260 EXPECT_EQ(0, store.GetCertCount()); |
| 282 } | 261 } |
| 283 | 262 |
| 284 TEST(DefaultServerBoundCertStoreTest, TestAsyncGetAndDeleteAll) { | 263 TEST(DefaultServerBoundCertStoreTest, TestAsyncGetAndDeleteAll) { |
| 285 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 264 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 286 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( | 265 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( |
| 287 "verisign.com", | 266 "verisign.com", base::Time(), base::Time(), "a", "b")); |
| 288 base::Time(), | |
| 289 base::Time(), | |
| 290 "a", "b")); | |
| 291 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( | 267 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( |
| 292 "google.com", | 268 "google.com", base::Time(), base::Time(), "c", "d")); |
| 293 base::Time(), | |
| 294 base::Time(), | |
| 295 "c", "d")); | |
| 296 | 269 |
| 297 ServerBoundCertStore::ServerBoundCertList pre_certs; | 270 ServerBoundCertStore::ServerBoundCertList pre_certs; |
| 298 ServerBoundCertStore::ServerBoundCertList post_certs; | 271 ServerBoundCertStore::ServerBoundCertList post_certs; |
| 299 int delete_finished = 0; | 272 int delete_finished = 0; |
| 300 DefaultServerBoundCertStore store(persistent_store.get()); | 273 DefaultServerBoundCertStore store(persistent_store.get()); |
| 301 | 274 |
| 302 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &pre_certs)); | 275 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &pre_certs)); |
| 303 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); | 276 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); |
| 304 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &post_certs)); | 277 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &post_certs)); |
| 305 // Tasks have not run yet. | 278 // Tasks have not run yet. |
| 306 EXPECT_EQ(0u, pre_certs.size()); | 279 EXPECT_EQ(0u, pre_certs.size()); |
| 307 // Wait for load & queued tasks. | 280 // Wait for load & queued tasks. |
| 308 base::MessageLoop::current()->RunUntilIdle(); | 281 base::MessageLoop::current()->RunUntilIdle(); |
| 309 EXPECT_EQ(0, store.GetCertCount()); | 282 EXPECT_EQ(0, store.GetCertCount()); |
| 310 EXPECT_EQ(2u, pre_certs.size()); | 283 EXPECT_EQ(2u, pre_certs.size()); |
| 311 EXPECT_EQ(0u, post_certs.size()); | 284 EXPECT_EQ(0u, post_certs.size()); |
| 312 } | 285 } |
| 313 | 286 |
| 314 TEST(DefaultServerBoundCertStoreTest, TestDelete) { | 287 TEST(DefaultServerBoundCertStoreTest, TestDelete) { |
| 315 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 288 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 316 DefaultServerBoundCertStore store(persistent_store.get()); | 289 DefaultServerBoundCertStore store(persistent_store.get()); |
| 317 | 290 |
| 318 base::Time expiration_time; | 291 base::Time expiration_time; |
| 319 std::string private_key, cert; | 292 std::string private_key, cert; |
| 320 EXPECT_EQ(0, store.GetCertCount()); | 293 EXPECT_EQ(0, store.GetCertCount()); |
| 321 store.SetServerBoundCert( | 294 store.SetServerBoundCert( |
| 322 "verisign.com", | 295 "verisign.com", base::Time(), base::Time(), "a", "b"); |
| 323 base::Time(), | |
| 324 base::Time(), | |
| 325 "a", "b"); | |
| 326 // Wait for load & queued set task. | 296 // Wait for load & queued set task. |
| 327 base::MessageLoop::current()->RunUntilIdle(); | 297 base::MessageLoop::current()->RunUntilIdle(); |
| 328 | 298 |
| 329 store.SetServerBoundCert( | 299 store.SetServerBoundCert("google.com", base::Time(), base::Time(), "c", "d"); |
| 330 "google.com", | |
| 331 base::Time(), | |
| 332 base::Time(), | |
| 333 "c", "d"); | |
| 334 | 300 |
| 335 EXPECT_EQ(2, store.GetCertCount()); | 301 EXPECT_EQ(2, store.GetCertCount()); |
| 336 int delete_finished = 0; | 302 int delete_finished = 0; |
| 337 store.DeleteServerBoundCert("verisign.com", | 303 store.DeleteServerBoundCert("verisign.com", |
| 338 base::Bind(&CallCounter, &delete_finished)); | 304 base::Bind(&CallCounter, &delete_finished)); |
| 339 ASSERT_EQ(1, delete_finished); | 305 ASSERT_EQ(1, delete_finished); |
| 340 EXPECT_EQ(1, store.GetCertCount()); | 306 EXPECT_EQ(1, store.GetCertCount()); |
| 341 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 307 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 342 store.GetServerBoundCert("verisign.com", | 308 store.GetServerBoundCert("verisign.com", |
| 343 &expiration_time, | 309 &expiration_time, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 358 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 324 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 359 store.GetServerBoundCert("google.com", | 325 store.GetServerBoundCert("google.com", |
| 360 &expiration_time, | 326 &expiration_time, |
| 361 &private_key, | 327 &private_key, |
| 362 &cert, | 328 &cert, |
| 363 base::Bind(&GetCertCallbackNotCalled))); | 329 base::Bind(&GetCertCallbackNotCalled))); |
| 364 } | 330 } |
| 365 | 331 |
| 366 TEST(DefaultServerBoundCertStoreTest, TestAsyncDelete) { | 332 TEST(DefaultServerBoundCertStoreTest, TestAsyncDelete) { |
| 367 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 333 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 368 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( | 334 persistent_store->AddServerBoundCert( |
| 369 "a.com", | 335 ServerBoundCertStore::ServerBoundCert("a.com", |
| 370 base::Time::FromInternalValue(1), | 336 base::Time::FromInternalValue(1), |
| 371 base::Time::FromInternalValue(2), | 337 base::Time::FromInternalValue(2), |
| 372 "a", "b")); | 338 "a", |
| 373 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( | 339 "b")); |
| 374 "b.com", | 340 persistent_store->AddServerBoundCert( |
| 375 base::Time::FromInternalValue(3), | 341 ServerBoundCertStore::ServerBoundCert("b.com", |
| 376 base::Time::FromInternalValue(4), | 342 base::Time::FromInternalValue(3), |
| 377 "c", "d")); | 343 base::Time::FromInternalValue(4), |
| 344 "c", |
| 345 "d")); |
| 378 DefaultServerBoundCertStore store(persistent_store.get()); | 346 DefaultServerBoundCertStore store(persistent_store.get()); |
| 379 int delete_finished = 0; | 347 int delete_finished = 0; |
| 380 store.DeleteServerBoundCert("a.com", | 348 store.DeleteServerBoundCert("a.com", |
| 381 base::Bind(&CallCounter, &delete_finished)); | 349 base::Bind(&CallCounter, &delete_finished)); |
| 382 | 350 |
| 383 AsyncGetCertHelper a_helper; | 351 AsyncGetCertHelper a_helper; |
| 384 AsyncGetCertHelper b_helper; | 352 AsyncGetCertHelper b_helper; |
| 385 base::Time expiration_time; | 353 base::Time expiration_time; |
| 386 std::string private_key; | 354 std::string private_key; |
| 387 std::string cert = "not set"; | 355 std::string cert = "not set"; |
| 388 EXPECT_EQ(0, store.GetCertCount()); | 356 EXPECT_EQ(0, store.GetCertCount()); |
| 389 EXPECT_EQ(ERR_IO_PENDING, | 357 EXPECT_EQ(ERR_IO_PENDING, |
| 390 store.GetServerBoundCert( | 358 store.GetServerBoundCert("a.com", |
| 391 "a.com", &expiration_time, &private_key, &cert, | 359 &expiration_time, |
| 392 base::Bind(&AsyncGetCertHelper::Callback, | 360 &private_key, |
| 393 base::Unretained(&a_helper)))); | 361 &cert, |
| 362 base::Bind(&AsyncGetCertHelper::Callback, |
| 363 base::Unretained(&a_helper)))); |
| 394 EXPECT_EQ(ERR_IO_PENDING, | 364 EXPECT_EQ(ERR_IO_PENDING, |
| 395 store.GetServerBoundCert( | 365 store.GetServerBoundCert("b.com", |
| 396 "b.com", &expiration_time, &private_key, &cert, | 366 &expiration_time, |
| 397 base::Bind(&AsyncGetCertHelper::Callback, | 367 &private_key, |
| 398 base::Unretained(&b_helper)))); | 368 &cert, |
| 369 base::Bind(&AsyncGetCertHelper::Callback, |
| 370 base::Unretained(&b_helper)))); |
| 399 | 371 |
| 400 EXPECT_EQ(0, delete_finished); | 372 EXPECT_EQ(0, delete_finished); |
| 401 EXPECT_FALSE(a_helper.called_); | 373 EXPECT_FALSE(a_helper.called_); |
| 402 EXPECT_FALSE(b_helper.called_); | 374 EXPECT_FALSE(b_helper.called_); |
| 403 // Wait for load & queued tasks. | 375 // Wait for load & queued tasks. |
| 404 base::MessageLoop::current()->RunUntilIdle(); | 376 base::MessageLoop::current()->RunUntilIdle(); |
| 405 EXPECT_EQ(1, delete_finished); | 377 EXPECT_EQ(1, delete_finished); |
| 406 EXPECT_EQ(1, store.GetCertCount()); | 378 EXPECT_EQ(1, store.GetCertCount()); |
| 407 EXPECT_EQ("not set", cert); | 379 EXPECT_EQ("not set", cert); |
| 408 EXPECT_TRUE(a_helper.called_); | 380 EXPECT_TRUE(a_helper.called_); |
| 409 EXPECT_EQ(ERR_FILE_NOT_FOUND, a_helper.err_); | 381 EXPECT_EQ(ERR_FILE_NOT_FOUND, a_helper.err_); |
| 410 EXPECT_EQ("a.com", a_helper.server_identifier_); | 382 EXPECT_EQ("a.com", a_helper.server_identifier_); |
| 411 EXPECT_EQ(0, a_helper.expiration_time_.ToInternalValue()); | 383 EXPECT_EQ(0, a_helper.expiration_time_.ToInternalValue()); |
| 412 EXPECT_EQ("", a_helper.private_key_); | 384 EXPECT_EQ("", a_helper.private_key_); |
| 413 EXPECT_EQ("", a_helper.cert_); | 385 EXPECT_EQ("", a_helper.cert_); |
| 414 EXPECT_TRUE(b_helper.called_); | 386 EXPECT_TRUE(b_helper.called_); |
| 415 EXPECT_EQ(OK, b_helper.err_); | 387 EXPECT_EQ(OK, b_helper.err_); |
| 416 EXPECT_EQ("b.com", b_helper.server_identifier_); | 388 EXPECT_EQ("b.com", b_helper.server_identifier_); |
| 417 EXPECT_EQ(4, b_helper.expiration_time_.ToInternalValue()); | 389 EXPECT_EQ(4, b_helper.expiration_time_.ToInternalValue()); |
| 418 EXPECT_EQ("c", b_helper.private_key_); | 390 EXPECT_EQ("c", b_helper.private_key_); |
| 419 EXPECT_EQ("d", b_helper.cert_); | 391 EXPECT_EQ("d", b_helper.cert_); |
| 420 } | 392 } |
| 421 | 393 |
| 422 TEST(DefaultServerBoundCertStoreTest, TestGetAll) { | 394 TEST(DefaultServerBoundCertStoreTest, TestGetAll) { |
| 423 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 395 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 424 DefaultServerBoundCertStore store(persistent_store.get()); | 396 DefaultServerBoundCertStore store(persistent_store.get()); |
| 425 | 397 |
| 426 EXPECT_EQ(0, store.GetCertCount()); | 398 EXPECT_EQ(0, store.GetCertCount()); |
| 427 store.SetServerBoundCert( | 399 store.SetServerBoundCert( |
| 428 "verisign.com", | 400 "verisign.com", base::Time(), base::Time(), "a", "b"); |
| 429 base::Time(), | 401 store.SetServerBoundCert("google.com", base::Time(), base::Time(), "c", "d"); |
| 430 base::Time(), | 402 store.SetServerBoundCert("harvard.com", base::Time(), base::Time(), "e", "f"); |
| 431 "a", "b"); | 403 store.SetServerBoundCert("mit.com", base::Time(), base::Time(), "g", "h"); |
| 432 store.SetServerBoundCert( | |
| 433 "google.com", | |
| 434 base::Time(), | |
| 435 base::Time(), | |
| 436 "c", "d"); | |
| 437 store.SetServerBoundCert( | |
| 438 "harvard.com", | |
| 439 base::Time(), | |
| 440 base::Time(), | |
| 441 "e", "f"); | |
| 442 store.SetServerBoundCert( | |
| 443 "mit.com", | |
| 444 base::Time(), | |
| 445 base::Time(), | |
| 446 "g", "h"); | |
| 447 // Wait for load & queued set tasks. | 404 // Wait for load & queued set tasks. |
| 448 base::MessageLoop::current()->RunUntilIdle(); | 405 base::MessageLoop::current()->RunUntilIdle(); |
| 449 | 406 |
| 450 EXPECT_EQ(4, store.GetCertCount()); | 407 EXPECT_EQ(4, store.GetCertCount()); |
| 451 ServerBoundCertStore::ServerBoundCertList certs; | 408 ServerBoundCertStore::ServerBoundCertList certs; |
| 452 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &certs)); | 409 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &certs)); |
| 453 EXPECT_EQ(4u, certs.size()); | 410 EXPECT_EQ(4u, certs.size()); |
| 454 } | 411 } |
| 455 | 412 |
| 456 TEST(DefaultServerBoundCertStoreTest, TestInitializeFrom) { | 413 TEST(DefaultServerBoundCertStoreTest, TestInitializeFrom) { |
| 457 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 414 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 458 DefaultServerBoundCertStore store(persistent_store.get()); | 415 DefaultServerBoundCertStore store(persistent_store.get()); |
| 459 | 416 |
| 460 store.SetServerBoundCert( | 417 store.SetServerBoundCert( |
| 461 "preexisting.com", | 418 "preexisting.com", base::Time(), base::Time(), "a", "b"); |
| 462 base::Time(), | 419 store.SetServerBoundCert("both.com", base::Time(), base::Time(), "c", "d"); |
| 463 base::Time(), | |
| 464 "a", "b"); | |
| 465 store.SetServerBoundCert( | |
| 466 "both.com", | |
| 467 base::Time(), | |
| 468 base::Time(), | |
| 469 "c", "d"); | |
| 470 // Wait for load & queued set tasks. | 420 // Wait for load & queued set tasks. |
| 471 base::MessageLoop::current()->RunUntilIdle(); | 421 base::MessageLoop::current()->RunUntilIdle(); |
| 472 EXPECT_EQ(2, store.GetCertCount()); | 422 EXPECT_EQ(2, store.GetCertCount()); |
| 473 | 423 |
| 474 ServerBoundCertStore::ServerBoundCertList source_certs; | 424 ServerBoundCertStore::ServerBoundCertList source_certs; |
| 475 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( | 425 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( |
| 476 "both.com", | 426 "both.com", |
| 477 base::Time(), | 427 base::Time(), |
| 478 base::Time(), | 428 base::Time(), |
| 479 // Key differs from above to test that existing entries are overwritten. | 429 // Key differs from above to test that existing entries are overwritten. |
| 480 "e", "f")); | 430 "e", |
| 431 "f")); |
| 481 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( | 432 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( |
| 482 "copied.com", | 433 "copied.com", base::Time(), base::Time(), "g", "h")); |
| 483 base::Time(), | |
| 484 base::Time(), | |
| 485 "g", "h")); | |
| 486 store.InitializeFrom(source_certs); | 434 store.InitializeFrom(source_certs); |
| 487 EXPECT_EQ(3, store.GetCertCount()); | 435 EXPECT_EQ(3, store.GetCertCount()); |
| 488 | 436 |
| 489 ServerBoundCertStore::ServerBoundCertList certs; | 437 ServerBoundCertStore::ServerBoundCertList certs; |
| 490 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &certs)); | 438 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &certs)); |
| 491 ASSERT_EQ(3u, certs.size()); | 439 ASSERT_EQ(3u, certs.size()); |
| 492 | 440 |
| 493 ServerBoundCertStore::ServerBoundCertList::iterator cert = certs.begin(); | 441 ServerBoundCertStore::ServerBoundCertList::iterator cert = certs.begin(); |
| 494 EXPECT_EQ("both.com", cert->server_identifier()); | 442 EXPECT_EQ("both.com", cert->server_identifier()); |
| 495 EXPECT_EQ("e", cert->private_key()); | 443 EXPECT_EQ("e", cert->private_key()); |
| 496 | 444 |
| 497 ++cert; | 445 ++cert; |
| 498 EXPECT_EQ("copied.com", cert->server_identifier()); | 446 EXPECT_EQ("copied.com", cert->server_identifier()); |
| 499 EXPECT_EQ("g", cert->private_key()); | 447 EXPECT_EQ("g", cert->private_key()); |
| 500 | 448 |
| 501 ++cert; | 449 ++cert; |
| 502 EXPECT_EQ("preexisting.com", cert->server_identifier()); | 450 EXPECT_EQ("preexisting.com", cert->server_identifier()); |
| 503 EXPECT_EQ("a", cert->private_key()); | 451 EXPECT_EQ("a", cert->private_key()); |
| 504 } | 452 } |
| 505 | 453 |
| 506 TEST(DefaultServerBoundCertStoreTest, TestAsyncInitializeFrom) { | 454 TEST(DefaultServerBoundCertStoreTest, TestAsyncInitializeFrom) { |
| 507 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 455 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 508 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( | 456 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( |
| 509 "preexisting.com", | 457 "preexisting.com", base::Time(), base::Time(), "a", "b")); |
| 510 base::Time(), | |
| 511 base::Time(), | |
| 512 "a", "b")); | |
| 513 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( | 458 persistent_store->AddServerBoundCert(ServerBoundCertStore::ServerBoundCert( |
| 514 "both.com", | 459 "both.com", base::Time(), base::Time(), "c", "d")); |
| 515 base::Time(), | |
| 516 base::Time(), | |
| 517 "c", "d")); | |
| 518 | 460 |
| 519 DefaultServerBoundCertStore store(persistent_store.get()); | 461 DefaultServerBoundCertStore store(persistent_store.get()); |
| 520 ServerBoundCertStore::ServerBoundCertList source_certs; | 462 ServerBoundCertStore::ServerBoundCertList source_certs; |
| 521 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( | 463 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( |
| 522 "both.com", | 464 "both.com", |
| 523 base::Time(), | 465 base::Time(), |
| 524 base::Time(), | 466 base::Time(), |
| 525 // Key differs from above to test that existing entries are overwritten. | 467 // Key differs from above to test that existing entries are overwritten. |
| 526 "e", "f")); | 468 "e", |
| 469 "f")); |
| 527 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( | 470 source_certs.push_back(ServerBoundCertStore::ServerBoundCert( |
| 528 "copied.com", | 471 "copied.com", base::Time(), base::Time(), "g", "h")); |
| 529 base::Time(), | |
| 530 base::Time(), | |
| 531 "g", "h")); | |
| 532 store.InitializeFrom(source_certs); | 472 store.InitializeFrom(source_certs); |
| 533 EXPECT_EQ(0, store.GetCertCount()); | 473 EXPECT_EQ(0, store.GetCertCount()); |
| 534 // Wait for load & queued tasks. | 474 // Wait for load & queued tasks. |
| 535 base::MessageLoop::current()->RunUntilIdle(); | 475 base::MessageLoop::current()->RunUntilIdle(); |
| 536 EXPECT_EQ(3, store.GetCertCount()); | 476 EXPECT_EQ(3, store.GetCertCount()); |
| 537 | 477 |
| 538 ServerBoundCertStore::ServerBoundCertList certs; | 478 ServerBoundCertStore::ServerBoundCertList certs; |
| 539 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &certs)); | 479 store.GetAllServerBoundCerts(base::Bind(GetAllCallback, &certs)); |
| 540 ASSERT_EQ(3u, certs.size()); | 480 ASSERT_EQ(3u, certs.size()); |
| 541 | 481 |
| 542 ServerBoundCertStore::ServerBoundCertList::iterator cert = certs.begin(); | 482 ServerBoundCertStore::ServerBoundCertList::iterator cert = certs.begin(); |
| 543 EXPECT_EQ("both.com", cert->server_identifier()); | 483 EXPECT_EQ("both.com", cert->server_identifier()); |
| 544 EXPECT_EQ("e", cert->private_key()); | 484 EXPECT_EQ("e", cert->private_key()); |
| 545 | 485 |
| 546 ++cert; | 486 ++cert; |
| 547 EXPECT_EQ("copied.com", cert->server_identifier()); | 487 EXPECT_EQ("copied.com", cert->server_identifier()); |
| 548 EXPECT_EQ("g", cert->private_key()); | 488 EXPECT_EQ("g", cert->private_key()); |
| 549 | 489 |
| 550 ++cert; | 490 ++cert; |
| 551 EXPECT_EQ("preexisting.com", cert->server_identifier()); | 491 EXPECT_EQ("preexisting.com", cert->server_identifier()); |
| 552 EXPECT_EQ("a", cert->private_key()); | 492 EXPECT_EQ("a", cert->private_key()); |
| 553 } | 493 } |
| 554 | 494 |
| 555 } // namespace net | 495 } // namespace net |
| OLD | NEW |