Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/http/disk_based_cert_cache.h" | 5 #include "net/http/disk_based_cert_cache.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "net/base/completion_callback.h" | 9 #include "net/base/completion_callback.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 // are irrelevant. Only one MockTransaction per certificate can be used | 44 // are irrelevant. Only one MockTransaction per certificate can be used |
| 45 // at a time. | 45 // at a time. |
| 46 MockTransaction CreateMockTransaction(const char* key, int test_mode) { | 46 MockTransaction CreateMockTransaction(const char* key, int test_mode) { |
| 47 MockTransaction transaction = {key, "", base::Time(), "", LOAD_NORMAL, | 47 MockTransaction transaction = {key, "", base::Time(), "", LOAD_NORMAL, |
| 48 "", "", base::Time(), "", test_mode, | 48 "", "", base::Time(), "", test_mode, |
| 49 NULL, 0, OK}; | 49 NULL, 0, OK}; |
| 50 | 50 |
| 51 return transaction; | 51 return transaction; |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Helper class, for use with DiskBasedCertCache::Get, that will ensure that | 54 // Helper class, for use with DiskBasedCertCache::GetCertificate, that will |
| 55 // the returned certificate handle is kept alive after the callback has been | 55 // store the returned certificate handle and allow users to WaitForResult of |
| 56 // executed and allow a user to WaitForResult of DiskBasedCertCache::Get. | 56 // DiskBasedCertCache::GetCertificate. |
| 57 class TestGetCallback { | 57 class TestGetCallback { |
| 58 public: | 58 public: |
| 59 TestGetCallback() : cert_handle_(NULL) {} | 59 TestGetCallback() : cert_handle_(NULL) {} |
| 60 ~TestGetCallback() { | 60 ~TestGetCallback() { |
| 61 if (cert_handle_) | 61 if (cert_handle_) |
| 62 X509Certificate::FreeOSCertHandle(cert_handle_); | 62 X509Certificate::FreeOSCertHandle(cert_handle_); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Blocks until the underlying Get() operation has succeeded. | 65 // Blocks until the underlying GetCertificate() operation has succeeded. |
| 66 void WaitForResult() { cb_.WaitForResult(); } | 66 void WaitForResult() { cb_.WaitForResult(); } |
| 67 | 67 |
| 68 // Returns a Callback suitable for use with DiskBasedCertCache::Get(). The | 68 // Returns a Callback suitable for use with |
| 69 // returned callback is only valid while the TestGetCallback object is still | 69 // DiskBasedCertCache::GetCertificate(). The returned callback is only valid |
| 70 // valid. | 70 // while the TestGetCallback object is still valid. |
| 71 DiskBasedCertCache::GetCallback callback() { | 71 DiskBasedCertCache::GetCallback callback() { |
| 72 return base::Bind(&TestGetCallback::OnGetComplete, base::Unretained(this)); | 72 return base::Bind(&TestGetCallback::OnGetComplete, base::Unretained(this)); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Returns the associated certificate handle. | 75 // Returns the associated certificate handle. |
| 76 const X509Certificate::OSCertHandle& cert_handle() const { | 76 const X509Certificate::OSCertHandle& cert_handle() const { |
| 77 return cert_handle_; | 77 return cert_handle_; |
| 78 } | 78 } |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 void OnGetComplete(const X509Certificate::OSCertHandle handle) { | 81 void OnGetComplete(const X509Certificate::OSCertHandle handle) { |
| 82 if (handle) | 82 if (handle) |
| 83 cert_handle_ = X509Certificate::DupOSCertHandle(handle); | 83 cert_handle_ = X509Certificate::DupOSCertHandle(handle); |
| 84 cb_.callback().Run(OK); | 84 cb_.callback().Run(OK); |
| 85 } | 85 } |
| 86 | 86 |
| 87 TestCompletionCallback cb_; | 87 TestCompletionCallback cb_; |
| 88 X509Certificate::OSCertHandle cert_handle_; | 88 X509Certificate::OSCertHandle cert_handle_; |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 // Helper class, for use with DiskBasedCertCache::Set, that will store the | 91 // Helper class, for use with DiskBasedCertCache::SetCertificate, that will |
| 92 // returned key and allow a user to WaitForResult of DiskBasedCertCache::Set. | 92 // store the returned key and allow a user to WaitForResult of |
| 93 // DiskBasedCertCache::SetCertificate. | |
| 93 class TestSetCallback { | 94 class TestSetCallback { |
| 94 public: | 95 public: |
| 95 TestSetCallback() {} | 96 TestSetCallback() {} |
| 96 ~TestSetCallback() {} | 97 ~TestSetCallback() {} |
| 97 | 98 |
| 98 // Blocks until the underlying Set() operation has succeeded. | 99 // Blocks until the underlying SetCertificate() operation has succeeded. |
| 99 void WaitForResult() { cb_.WaitForResult(); } | 100 void WaitForResult() { cb_.WaitForResult(); } |
| 100 | 101 |
| 101 // Returns a Callback suitable for use with DiskBasedCertCache::Set(). The | 102 // Returns a Callback suitable for use with |
| 102 // returned callback is only valid while the TestSetCallback object is still | 103 // DiskBasedCertCache::SetCertificate(). The returned callback is only valid |
| 103 // valid. | 104 // while the TestSetCallback object is still valid. |
| 104 DiskBasedCertCache::SetCallback callback() { | 105 DiskBasedCertCache::SetCallback callback() { |
| 105 return base::Bind(&TestSetCallback::OnSetComplete, base::Unretained(this)); | 106 return base::Bind(&TestSetCallback::OnSetComplete, base::Unretained(this)); |
| 106 } | 107 } |
| 107 | 108 |
| 108 // Returns the associated certificate handle. | 109 // Returns the associated certificate handle. |
| 109 const std::string& key() const { return key_; } | 110 const std::string& key() const { return key_; } |
| 110 | 111 |
| 111 private: | 112 private: |
| 112 void OnSetComplete(const std::string& key) { | 113 void OnSetComplete(const std::string& key) { |
| 113 key_ = key; | 114 key_ = key; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 TEST(DiskBasedCertCache, SetCert) { | 187 TEST(DiskBasedCertCache, SetCert) { |
| 187 ScopedMockTransaction trans1( | 188 ScopedMockTransaction trans1( |
| 188 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 189 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 189 MockDiskCache backend; | 190 MockDiskCache backend; |
| 190 DiskBasedCertCache cache(&backend); | 191 DiskBasedCertCache cache(&backend); |
| 191 scoped_refptr<X509Certificate> cert( | 192 scoped_refptr<X509Certificate> cert( |
| 192 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 193 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 193 ASSERT_TRUE(cert.get()); | 194 ASSERT_TRUE(cert.get()); |
| 194 TestSetCallback set_callback; | 195 TestSetCallback set_callback; |
| 195 | 196 |
| 196 cache.Set(cert->os_cert_handle(), set_callback.callback()); | 197 cache.SetCertificate(cert->os_cert_handle(), set_callback.callback()); |
| 197 set_callback.WaitForResult(); | 198 set_callback.WaitForResult(); |
| 198 EXPECT_EQ(kCert1.cache_key, set_callback.key()); | 199 EXPECT_EQ(kCert1.cache_key, set_callback.key()); |
| 199 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); | 200 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); |
| 200 } | 201 } |
| 201 | 202 |
| 202 // Tests that a certificate can be retrieved from the cache. | 203 // Tests that a certificate can be retrieved from the cache. |
| 203 TEST(DiskBasedCertCache, GetCert) { | 204 TEST(DiskBasedCertCache, GetCert) { |
| 204 ScopedMockTransaction trans1( | 205 ScopedMockTransaction trans1( |
| 205 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 206 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 206 MockDiskCache backend; | 207 MockDiskCache backend; |
| 207 ASSERT_NO_FATAL_FAILURE( | 208 ASSERT_NO_FATAL_FAILURE( |
| 208 ImportCert(&backend, kCert1, false /* not corrupted */)); | 209 ImportCert(&backend, kCert1, false /* not corrupted */)); |
| 209 DiskBasedCertCache cache(&backend); | 210 DiskBasedCertCache cache(&backend); |
| 210 TestGetCallback get_callback; | 211 TestGetCallback get_callback; |
| 211 | 212 |
| 212 cache.Get(kCert1.cache_key, get_callback.callback()); | 213 cache.GetCertificate(kCert1.cache_key, get_callback.callback()); |
| 213 get_callback.WaitForResult(); | 214 get_callback.WaitForResult(); |
| 214 | 215 |
| 215 scoped_refptr<X509Certificate> cert( | 216 scoped_refptr<X509Certificate> cert( |
| 216 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 217 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 217 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(), | 218 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(), |
| 218 cert->os_cert_handle())); | 219 cert->os_cert_handle())); |
| 219 } | 220 } |
| 220 | 221 |
| 221 // Tests that the DiskBasedCertCache successfully writes to the cache | 222 // Tests that the DiskBasedCertCache successfully writes to the cache |
| 222 // if the cache acts synchronously | 223 // if the cache acts synchronously |
| 223 TEST(DiskBasedCertCache, SyncSet) { | 224 TEST(DiskBasedCertCache, SyncSet) { |
| 224 ScopedMockTransaction trans1( | 225 ScopedMockTransaction trans1( |
| 225 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_ALL)); | 226 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_ALL)); |
| 226 MockDiskCache backend; | 227 MockDiskCache backend; |
| 227 DiskBasedCertCache cache(&backend); | 228 DiskBasedCertCache cache(&backend); |
| 228 scoped_refptr<X509Certificate> cert( | 229 scoped_refptr<X509Certificate> cert( |
| 229 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 230 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 230 ASSERT_TRUE(cert.get()); | 231 ASSERT_TRUE(cert.get()); |
| 231 | 232 |
| 232 TestSetCallback set_callback; | 233 TestSetCallback set_callback; |
| 233 cache.Set(cert->os_cert_handle(), set_callback.callback()); | 234 cache.SetCertificate(cert->os_cert_handle(), set_callback.callback()); |
| 234 set_callback.WaitForResult(); | 235 set_callback.WaitForResult(); |
| 235 EXPECT_EQ(kCert1.cache_key, set_callback.key()); | 236 EXPECT_EQ(kCert1.cache_key, set_callback.key()); |
| 236 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); | 237 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); |
| 237 } | 238 } |
| 238 | 239 |
| 239 // Tests that the DiskBasedCertCache successfully reads from the cache | 240 // Tests that the DiskBasedCertCache successfully reads from the cache |
| 240 // if the cache acts synchronously | 241 // if the cache acts synchronously |
| 241 TEST(DiskBasedCertCache, SyncGet) { | 242 TEST(DiskBasedCertCache, SyncGet) { |
| 242 ScopedMockTransaction trans1( | 243 ScopedMockTransaction trans1( |
| 243 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_ALL)); | 244 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_ALL)); |
| 244 MockDiskCache backend; | 245 MockDiskCache backend; |
| 245 ASSERT_NO_FATAL_FAILURE( | 246 ASSERT_NO_FATAL_FAILURE( |
| 246 (ImportCert(&backend, kCert1, false /* not corrupted */))); | 247 (ImportCert(&backend, kCert1, false /* not corrupted */))); |
| 247 DiskBasedCertCache cache(&backend); | 248 DiskBasedCertCache cache(&backend); |
| 248 scoped_refptr<X509Certificate> cert( | 249 scoped_refptr<X509Certificate> cert( |
| 249 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 250 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 250 ASSERT_TRUE(cert.get()); | 251 ASSERT_TRUE(cert.get()); |
| 251 | 252 |
| 252 TestGetCallback get_callback; | 253 TestGetCallback get_callback; |
| 253 cache.Get(kCert1.cache_key, get_callback.callback()); | 254 cache.GetCertificate(kCert1.cache_key, get_callback.callback()); |
| 254 get_callback.WaitForResult(); | 255 get_callback.WaitForResult(); |
| 255 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(), | 256 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(), |
| 256 cert->os_cert_handle())); | 257 cert->os_cert_handle())); |
| 257 } | 258 } |
| 258 | 259 |
| 259 // Tests that Get will fail on a corrupted certificate. | 260 // Tests that GetCertificate will fail on a corrupted certificate. |
| 260 TEST(DiskBasedCertCache, GetBrokenCert) { | 261 TEST(DiskBasedCertCache, GetBrokenCert) { |
| 261 ScopedMockTransaction trans1( | 262 ScopedMockTransaction trans1( |
| 262 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 263 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 263 MockDiskCache backend; | 264 MockDiskCache backend; |
| 264 ASSERT_NO_FATAL_FAILURE(ImportCert(&backend, kCert1, true /* corrupted */)); | 265 ASSERT_NO_FATAL_FAILURE(ImportCert(&backend, kCert1, true /* corrupted */)); |
| 265 DiskBasedCertCache cache(&backend); | 266 DiskBasedCertCache cache(&backend); |
| 266 TestGetCallback get_callback; | 267 TestGetCallback get_callback; |
| 267 | 268 |
| 268 cache.Get(kCert1.cache_key, get_callback.callback()); | 269 cache.GetCertificate(kCert1.cache_key, get_callback.callback()); |
| 269 get_callback.WaitForResult(); | 270 get_callback.WaitForResult(); |
| 270 | 271 |
| 271 EXPECT_FALSE(get_callback.cert_handle()); | 272 EXPECT_FALSE(get_callback.cert_handle()); |
| 272 } | 273 } |
| 273 | 274 |
| 274 // Tests that attempting to retrieve a cert that is not in the cache will | 275 // Tests that attempting to retrieve a cert that is not in the cache will |
| 275 // return NULL. | 276 // return NULL. |
| 276 TEST(DiskBasedCertCache, GetUncachedCert) { | 277 TEST(DiskBasedCertCache, GetUncachedCert) { |
| 277 ScopedMockTransaction trans1( | 278 ScopedMockTransaction trans1( |
| 278 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 279 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 279 MockDiskCache backend; | 280 MockDiskCache backend; |
| 280 DiskBasedCertCache cache(&backend); | 281 DiskBasedCertCache cache(&backend); |
| 281 TestGetCallback get_callback; | 282 TestGetCallback get_callback; |
| 282 | 283 |
| 283 cache.Get(kCert1.cache_key, get_callback.callback()); | 284 cache.GetCertificate(kCert1.cache_key, get_callback.callback()); |
| 284 get_callback.WaitForResult(); | 285 get_callback.WaitForResult(); |
| 285 EXPECT_EQ(NULL, get_callback.cert_handle()); | 286 EXPECT_EQ(NULL, get_callback.cert_handle()); |
| 286 } | 287 } |
| 287 | 288 |
| 288 // Issues two requests to store a certificate in the cache | 289 // Issues two requests to store a certificate in the cache |
| 289 // (simultaneously), and checks that the DiskBasedCertCache stores the | 290 // (simultaneously), and checks that the DiskBasedCertCache stores the |
| 290 // certificate to the cache (in one write rather than two). | 291 // certificate to the cache (in one write rather than two). |
| 291 TEST(DiskBasedCertCache, SetMultiple) { | 292 TEST(DiskBasedCertCache, SetMultiple) { |
| 292 ScopedMockTransaction trans1( | 293 ScopedMockTransaction trans1( |
| 293 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 294 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 294 MockDiskCache backend; | 295 MockDiskCache backend; |
| 295 DiskBasedCertCache cache(&backend); | 296 DiskBasedCertCache cache(&backend); |
| 296 scoped_refptr<X509Certificate> cert( | 297 scoped_refptr<X509Certificate> cert( |
| 297 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 298 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 298 ASSERT_TRUE(cert.get()); | 299 ASSERT_TRUE(cert.get()); |
| 299 TestSetCallback set_callback1, set_callback2; | 300 TestSetCallback set_callback1, set_callback2; |
| 300 | 301 |
| 301 // Behind the scenes, these two operations will be combined | 302 // Behind the scenes, these two operations will be combined |
| 302 // into one operation. IgnoreCallbacks guarantees that the | 303 // into one operation. IgnoreCallbacks guarantees that the |
| 303 // first Set operation is not yet complete when the second Set is | 304 // first SetCertificate operation is not yet complete when the second Set is |
|
wtc
2014/08/01 03:03:55
second Set => second SetCertificate
| |
| 304 // called, and then IgnoreCallbacks(false) continues the | 305 // called, and then IgnoreCallbacks(false) continues the |
| 305 // (combined) operation in the |cache|. | 306 // (combined) operation in the |cache|. |
| 306 MockDiskEntry::IgnoreCallbacks(true); | 307 MockDiskEntry::IgnoreCallbacks(true); |
| 307 cache.Set(cert->os_cert_handle(), set_callback1.callback()); | 308 cache.SetCertificate(cert->os_cert_handle(), set_callback1.callback()); |
| 308 cache.Set(cert->os_cert_handle(), set_callback2.callback()); | 309 cache.SetCertificate(cert->os_cert_handle(), set_callback2.callback()); |
| 309 MockDiskEntry::IgnoreCallbacks(false); | 310 MockDiskEntry::IgnoreCallbacks(false); |
| 310 | 311 |
| 311 set_callback1.WaitForResult(); | 312 set_callback1.WaitForResult(); |
| 312 set_callback2.WaitForResult(); | 313 set_callback2.WaitForResult(); |
| 313 EXPECT_EQ(set_callback1.key(), set_callback2.key()); | 314 EXPECT_EQ(set_callback1.key(), set_callback2.key()); |
| 314 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); | 315 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); |
| 315 } | 316 } |
| 316 | 317 |
| 317 // Issues two requests to store a certificate in the cache | 318 // Issues two requests to store a certificate in the cache |
| 318 // because the first transaction finishes before the second | 319 // because the first transaction finishes before the second |
| 319 // one is issued, the first cache write is overwritten. | 320 // one is issued, the first cache write is overwritten. |
| 320 TEST(DiskBasedCertCache, SetOverwrite) { | 321 TEST(DiskBasedCertCache, SetOverwrite) { |
| 321 ScopedMockTransaction trans1( | 322 ScopedMockTransaction trans1( |
| 322 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 323 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 323 MockDiskCache backend; | 324 MockDiskCache backend; |
| 324 backend.set_double_create_check(false); | 325 backend.set_double_create_check(false); |
| 325 DiskBasedCertCache cache(&backend); | 326 DiskBasedCertCache cache(&backend); |
| 326 scoped_refptr<X509Certificate> cert( | 327 scoped_refptr<X509Certificate> cert( |
| 327 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 328 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 328 ASSERT_TRUE(cert.get()); | 329 ASSERT_TRUE(cert.get()); |
| 329 TestSetCallback set_callback1, set_callback2; | 330 TestSetCallback set_callback1, set_callback2; |
| 330 | 331 |
| 331 cache.Set(cert->os_cert_handle(), set_callback1.callback()); | 332 cache.SetCertificate(cert->os_cert_handle(), set_callback1.callback()); |
| 332 set_callback1.WaitForResult(); | 333 set_callback1.WaitForResult(); |
| 333 cache.Set(cert->os_cert_handle(), set_callback2.callback()); | 334 cache.SetCertificate(cert->os_cert_handle(), set_callback2.callback()); |
| 334 set_callback2.WaitForResult(); | 335 set_callback2.WaitForResult(); |
| 335 | 336 |
| 336 EXPECT_EQ(set_callback1.key(), set_callback2.key()); | 337 EXPECT_EQ(set_callback1.key(), set_callback2.key()); |
| 337 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); | 338 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); |
| 338 } | 339 } |
| 339 | 340 |
| 340 // Stores a certificate in the DiskBasedCertCache, then retrieves it | 341 // Stores a certificate in the DiskBasedCertCache, then retrieves it |
| 341 // and makes sure it was retrieved successfully. | 342 // and makes sure it was retrieved successfully. |
| 342 TEST(DiskBasedCertCache, SimpleSetAndGet) { | 343 TEST(DiskBasedCertCache, SimpleSetAndGet) { |
| 343 ScopedMockTransaction trans1( | 344 ScopedMockTransaction trans1( |
| 344 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 345 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 345 MockDiskCache backend; | 346 MockDiskCache backend; |
| 346 DiskBasedCertCache cache(&backend); | 347 DiskBasedCertCache cache(&backend); |
| 347 scoped_refptr<X509Certificate> cert( | 348 scoped_refptr<X509Certificate> cert( |
| 348 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 349 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 349 ASSERT_TRUE(cert.get()); | 350 ASSERT_TRUE(cert.get()); |
| 350 TestSetCallback set_callback; | 351 TestSetCallback set_callback; |
| 351 TestGetCallback get_callback; | 352 TestGetCallback get_callback; |
| 352 | 353 |
| 353 cache.Set(cert->os_cert_handle(), set_callback.callback()); | 354 cache.SetCertificate(cert->os_cert_handle(), set_callback.callback()); |
| 354 set_callback.WaitForResult(); | 355 set_callback.WaitForResult(); |
| 355 cache.Get(set_callback.key(), get_callback.callback()); | 356 cache.GetCertificate(set_callback.key(), get_callback.callback()); |
| 356 get_callback.WaitForResult(); | 357 get_callback.WaitForResult(); |
| 357 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(), | 358 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(), |
| 358 cert->os_cert_handle())); | 359 cert->os_cert_handle())); |
| 359 } | 360 } |
| 360 | 361 |
| 361 // Tests some basic functionality of the DiskBasedCertCache, with multiple | 362 // Tests some basic functionality of the DiskBasedCertCache, with multiple |
| 362 // set and get operations. | 363 // set and get operations. |
| 363 TEST(DiskBasedCertCache, BasicUsage) { | 364 TEST(DiskBasedCertCache, BasicUsage) { |
| 364 ScopedMockTransaction trans1( | 365 ScopedMockTransaction trans1( |
| 365 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START)); | 366 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START)); |
| 366 ScopedMockTransaction trans2( | 367 ScopedMockTransaction trans2( |
| 367 CreateMockTransaction(kCert2.cache_key, TEST_MODE_NORMAL)); | 368 CreateMockTransaction(kCert2.cache_key, TEST_MODE_NORMAL)); |
| 368 MockDiskCache backend; | 369 MockDiskCache backend; |
| 369 DiskBasedCertCache cache(&backend); | 370 DiskBasedCertCache cache(&backend); |
| 370 scoped_refptr<X509Certificate> cert1( | 371 scoped_refptr<X509Certificate> cert1( |
| 371 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 372 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 372 scoped_refptr<X509Certificate> cert2( | 373 scoped_refptr<X509Certificate> cert2( |
| 373 ImportCertFromFile(GetTestCertsDirectory(), kCert2.file_name)); | 374 ImportCertFromFile(GetTestCertsDirectory(), kCert2.file_name)); |
| 374 ASSERT_TRUE(cert1.get()); | 375 ASSERT_TRUE(cert1.get()); |
| 375 ASSERT_TRUE(cert2.get()); | 376 ASSERT_TRUE(cert2.get()); |
| 376 ASSERT_FALSE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(), | 377 ASSERT_FALSE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(), |
| 377 cert2->os_cert_handle())); | 378 cert2->os_cert_handle())); |
| 378 TestSetCallback set_callback1, set_callback2; | 379 TestSetCallback set_callback1, set_callback2; |
| 379 | 380 |
| 380 // Callbacks are temporarily ignored here to guarantee the asynchronous | 381 // Callbacks are temporarily ignored here to guarantee the asynchronous |
| 381 // operations of the DiskBasedCertCache are always executed in the same | 382 // operations of the DiskBasedCertCache are always executed in the same |
| 382 // order. | 383 // order. |
| 383 MockDiskEntry::IgnoreCallbacks(true); | 384 MockDiskEntry::IgnoreCallbacks(true); |
| 384 cache.Set(cert1->os_cert_handle(), set_callback1.callback()); | 385 cache.SetCertificate(cert1->os_cert_handle(), set_callback1.callback()); |
| 385 cache.Set(cert2->os_cert_handle(), set_callback2.callback()); | 386 cache.SetCertificate(cert2->os_cert_handle(), set_callback2.callback()); |
| 386 MockDiskEntry::IgnoreCallbacks(false); | 387 MockDiskEntry::IgnoreCallbacks(false); |
| 387 set_callback1.WaitForResult(); | 388 set_callback1.WaitForResult(); |
| 388 set_callback2.WaitForResult(); | 389 set_callback2.WaitForResult(); |
| 389 | 390 |
| 390 TestGetCallback get_callback1, get_callback2; | 391 TestGetCallback get_callback1, get_callback2; |
| 391 | 392 |
| 392 MockDiskEntry::IgnoreCallbacks(true); | 393 MockDiskEntry::IgnoreCallbacks(true); |
| 393 cache.Get(set_callback1.key(), get_callback1.callback()); | 394 cache.GetCertificate(set_callback1.key(), get_callback1.callback()); |
| 394 cache.Get(set_callback2.key(), get_callback2.callback()); | 395 cache.GetCertificate(set_callback2.key(), get_callback2.callback()); |
| 395 MockDiskEntry::IgnoreCallbacks(false); | 396 MockDiskEntry::IgnoreCallbacks(false); |
| 396 get_callback1.WaitForResult(); | 397 get_callback1.WaitForResult(); |
| 397 get_callback2.WaitForResult(); | 398 get_callback2.WaitForResult(); |
| 398 | 399 |
| 399 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(), | 400 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(), |
| 400 get_callback1.cert_handle())); | 401 get_callback1.cert_handle())); |
| 401 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert2->os_cert_handle(), | 402 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert2->os_cert_handle(), |
| 402 get_callback2.cert_handle())); | 403 get_callback2.cert_handle())); |
| 403 } | 404 } |
| 404 | 405 |
| 405 // Test the result of simultaneous requests to store and retrieve a | 406 // Test the result of simultaneous requests to store and retrieve a |
| 406 // certificate from the cache, with the get operation attempting to | 407 // certificate from the cache, with the get operation attempting to |
| 407 // open the cache first and therefore failing to open the entry. | 408 // open the cache first and therefore failing to open the entry. |
| 408 TEST(DiskBasedCertCache, SimultaneousGetSet) { | 409 TEST(DiskBasedCertCache, SimultaneousGetSet) { |
| 409 ScopedMockTransaction trans1( | 410 ScopedMockTransaction trans1( |
| 410 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START)); | 411 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START)); |
| 411 MockDiskCache backend; | 412 MockDiskCache backend; |
| 412 DiskBasedCertCache cache(&backend); | 413 DiskBasedCertCache cache(&backend); |
| 413 scoped_refptr<X509Certificate> cert( | 414 scoped_refptr<X509Certificate> cert( |
| 414 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 415 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 415 ASSERT_TRUE(cert.get()); | 416 ASSERT_TRUE(cert.get()); |
| 416 | 417 |
| 417 TestGetCallback get_callback; | 418 TestGetCallback get_callback; |
| 418 TestSetCallback set_callback; | 419 TestSetCallback set_callback; |
| 419 | 420 |
| 420 MockDiskEntry::IgnoreCallbacks(true); | 421 MockDiskEntry::IgnoreCallbacks(true); |
| 421 cache.Get(kCert1.cache_key, get_callback.callback()); | 422 cache.GetCertificate(kCert1.cache_key, get_callback.callback()); |
| 422 cache.Set(cert->os_cert_handle(), set_callback.callback()); | 423 cache.SetCertificate(cert->os_cert_handle(), set_callback.callback()); |
| 423 MockDiskEntry::IgnoreCallbacks(false); | 424 MockDiskEntry::IgnoreCallbacks(false); |
| 424 get_callback.WaitForResult(); | 425 get_callback.WaitForResult(); |
| 425 set_callback.WaitForResult(); | 426 set_callback.WaitForResult(); |
| 426 | 427 |
| 427 EXPECT_EQ(NULL, get_callback.cert_handle()); | 428 EXPECT_EQ(NULL, get_callback.cert_handle()); |
| 428 EXPECT_EQ(kCert1.cache_key, set_callback.key()); | 429 EXPECT_EQ(kCert1.cache_key, set_callback.key()); |
| 429 } | 430 } |
| 430 | 431 |
| 431 // Test the result of simultaneous requests to store and retrieve a | 432 // Test the result of simultaneous requests to store and retrieve a |
| 432 // certificate from the cache, with the get operation opening the cache | 433 // certificate from the cache, with the get operation opening the cache |
| 433 // after the set operation, leading to a successful read. | 434 // after the set operation, leading to a successful read. |
| 434 TEST(DiskBasedCertCache, SimultaneousSetGet) { | 435 TEST(DiskBasedCertCache, SimultaneousSetGet) { |
| 435 ScopedMockTransaction trans1( | 436 ScopedMockTransaction trans1( |
| 436 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START)); | 437 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START)); |
| 437 MockDiskCache backend; | 438 MockDiskCache backend; |
| 438 DiskBasedCertCache cache(&backend); | 439 DiskBasedCertCache cache(&backend); |
| 439 scoped_refptr<X509Certificate> cert( | 440 scoped_refptr<X509Certificate> cert( |
| 440 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 441 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 441 ASSERT_TRUE(cert.get()); | 442 ASSERT_TRUE(cert.get()); |
| 442 | 443 |
| 443 TestSetCallback set_callback; | 444 TestSetCallback set_callback; |
| 444 TestGetCallback get_callback; | 445 TestGetCallback get_callback; |
| 445 | 446 |
| 446 MockDiskEntry::IgnoreCallbacks(true); | 447 MockDiskEntry::IgnoreCallbacks(true); |
| 447 cache.Set(cert->os_cert_handle(), set_callback.callback()); | 448 cache.SetCertificate(cert->os_cert_handle(), set_callback.callback()); |
| 448 cache.Get(kCert1.cache_key, get_callback.callback()); | 449 cache.GetCertificate(kCert1.cache_key, get_callback.callback()); |
| 449 MockDiskEntry::IgnoreCallbacks(false); | 450 MockDiskEntry::IgnoreCallbacks(false); |
| 450 set_callback.WaitForResult(); | 451 set_callback.WaitForResult(); |
| 451 get_callback.WaitForResult(); | 452 get_callback.WaitForResult(); |
| 452 | 453 |
| 453 EXPECT_EQ(kCert1.cache_key, set_callback.key()); | 454 EXPECT_EQ(kCert1.cache_key, set_callback.key()); |
| 454 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert->os_cert_handle(), | 455 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert->os_cert_handle(), |
| 455 get_callback.cert_handle())); | 456 get_callback.cert_handle())); |
| 456 } | 457 } |
| 457 | 458 |
| 458 // Tests that the DiskBasedCertCache can be deleted without issues when | 459 // Tests that the DiskBasedCertCache can be deleted without issues when |
| 459 // there are pending operations in the disk cache. | 460 // there are pending operations in the disk cache. |
| 460 TEST(DiskBasedCertCache, DeletedCertCache) { | 461 TEST(DiskBasedCertCache, DeletedCertCache) { |
| 461 ScopedMockTransaction trans1( | 462 ScopedMockTransaction trans1( |
| 462 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 463 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 463 MockDiskCache backend; | 464 MockDiskCache backend; |
| 464 scoped_ptr<DiskBasedCertCache> cache(new DiskBasedCertCache(&backend)); | 465 scoped_ptr<DiskBasedCertCache> cache(new DiskBasedCertCache(&backend)); |
| 465 scoped_refptr<X509Certificate> cert( | 466 scoped_refptr<X509Certificate> cert( |
| 466 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 467 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 467 ASSERT_TRUE(cert.get()); | 468 ASSERT_TRUE(cert.get()); |
| 468 TestSetCallback set_callback; | 469 TestSetCallback set_callback; |
| 469 | 470 |
| 470 cache->Set(cert->os_cert_handle(), set_callback.callback()); | 471 cache->SetCertificate(cert->os_cert_handle(), set_callback.callback()); |
| 471 cache.reset(); | 472 cache.reset(); |
| 472 set_callback.WaitForResult(); | 473 set_callback.WaitForResult(); |
| 473 EXPECT_EQ(std::string(), set_callback.key()); | 474 EXPECT_EQ(std::string(), set_callback.key()); |
| 474 } | 475 } |
| 475 | 476 |
| 476 // Issues two successive read requests for a certificate, and then | 477 // Issues two successive read requests for a certificate, and then |
| 477 // checks that the DiskBasedCertCache correctly read and recorded | 478 // checks that the DiskBasedCertCache correctly read and recorded |
| 478 // reading through the in-memory MRU cache. | 479 // reading through the in-memory MRU cache. |
| 479 TEST(DiskBasedCertCache, MemCacheGet) { | 480 TEST(DiskBasedCertCache, MemCacheGet) { |
| 480 ScopedMockTransaction trans1( | 481 ScopedMockTransaction trans1( |
| 481 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 482 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 482 MockDiskCache backend; | 483 MockDiskCache backend; |
| 483 ASSERT_NO_FATAL_FAILURE( | 484 ASSERT_NO_FATAL_FAILURE( |
| 484 ImportCert(&backend, kCert1, false /* not corrupted */)); | 485 ImportCert(&backend, kCert1, false /* not corrupted */)); |
| 485 DiskBasedCertCache cache(&backend); | 486 DiskBasedCertCache cache(&backend); |
| 486 | 487 |
| 487 TestGetCallback get_callback1, get_callback2; | 488 TestGetCallback get_callback1, get_callback2; |
| 488 cache.Get(kCert1.cache_key, get_callback1.callback()); | 489 cache.GetCertificate(kCert1.cache_key, get_callback1.callback()); |
| 489 get_callback1.WaitForResult(); | 490 get_callback1.WaitForResult(); |
| 490 EXPECT_EQ(0U, cache.mem_cache_hits_for_testing()); | 491 EXPECT_EQ(0U, cache.mem_cache_hits_for_testing()); |
| 491 cache.Get(kCert1.cache_key, get_callback2.callback()); | 492 cache.GetCertificate(kCert1.cache_key, get_callback2.callback()); |
| 492 get_callback2.WaitForResult(); | 493 get_callback2.WaitForResult(); |
| 493 EXPECT_EQ(1U, cache.mem_cache_hits_for_testing()); | 494 EXPECT_EQ(1U, cache.mem_cache_hits_for_testing()); |
| 494 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback1.cert_handle(), | 495 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback1.cert_handle(), |
| 495 get_callback2.cert_handle())); | 496 get_callback2.cert_handle())); |
| 496 } | 497 } |
| 497 | 498 |
| 498 // Reads a corrupted certificate from the disk cache, and then overwrites | 499 // Reads a corrupted certificate from the disk cache, and then overwrites |
| 499 // it and checks that the uncorrupted version was stored in the in-memory | 500 // it and checks that the uncorrupted version was stored in the in-memory |
| 500 // cache. | 501 // cache. |
| 501 TEST(DiskBasedCertCache, CorruptOverwrite) { | 502 TEST(DiskBasedCertCache, CorruptOverwrite) { |
| 502 ScopedMockTransaction trans1( | 503 ScopedMockTransaction trans1( |
| 503 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); | 504 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL)); |
| 504 MockDiskCache backend; | 505 MockDiskCache backend; |
| 505 backend.set_double_create_check(false); | 506 backend.set_double_create_check(false); |
| 506 ASSERT_NO_FATAL_FAILURE(ImportCert(&backend, kCert1, true /* corrupted */)); | 507 ASSERT_NO_FATAL_FAILURE(ImportCert(&backend, kCert1, true /* corrupted */)); |
| 507 DiskBasedCertCache cache(&backend); | 508 DiskBasedCertCache cache(&backend); |
| 508 TestGetCallback get_callback1, get_callback2; | 509 TestGetCallback get_callback1, get_callback2; |
| 509 | 510 |
| 510 cache.Get(kCert1.cache_key, get_callback1.callback()); | 511 cache.GetCertificate(kCert1.cache_key, get_callback1.callback()); |
| 511 get_callback1.WaitForResult(); | 512 get_callback1.WaitForResult(); |
| 512 EXPECT_FALSE(get_callback2.cert_handle()); | 513 EXPECT_FALSE(get_callback2.cert_handle()); |
| 513 | 514 |
| 514 scoped_refptr<X509Certificate> cert( | 515 scoped_refptr<X509Certificate> cert( |
| 515 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); | 516 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name)); |
| 516 TestSetCallback set_callback; | 517 TestSetCallback set_callback; |
| 517 | 518 |
| 518 cache.Set(cert->os_cert_handle(), set_callback.callback()); | 519 cache.SetCertificate(cert->os_cert_handle(), set_callback.callback()); |
| 519 set_callback.WaitForResult(); | 520 set_callback.WaitForResult(); |
| 520 EXPECT_EQ(kCert1.cache_key, set_callback.key()); | 521 EXPECT_EQ(kCert1.cache_key, set_callback.key()); |
| 521 EXPECT_EQ(0U, cache.mem_cache_hits_for_testing()); | 522 EXPECT_EQ(0U, cache.mem_cache_hits_for_testing()); |
| 522 | 523 |
| 523 cache.Get(kCert1.cache_key, get_callback2.callback()); | 524 cache.GetCertificate(kCert1.cache_key, get_callback2.callback()); |
| 524 get_callback2.WaitForResult(); | 525 get_callback2.WaitForResult(); |
| 525 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback2.cert_handle(), | 526 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback2.cert_handle(), |
| 526 cert->os_cert_handle())); | 527 cert->os_cert_handle())); |
| 527 EXPECT_EQ(1U, cache.mem_cache_hits_for_testing()); | 528 EXPECT_EQ(1U, cache.mem_cache_hits_for_testing()); |
| 528 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); | 529 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1)); |
| 529 } | 530 } |
| 530 | 531 |
| 531 } // namespace net | 532 } // namespace net |
| OLD | NEW |