Index: net/http/disk_based_cert_cache_unittest.cc |
diff --git a/net/http/disk_based_cert_cache_unittest.cc b/net/http/disk_based_cert_cache_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4f115a25fa17d595ed65e5c2d319bfe045556835 |
--- /dev/null |
+++ b/net/http/disk_based_cert_cache_unittest.cc |
@@ -0,0 +1,129 @@ |
+// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/http/disk_based_cert_cache.h" |
+ |
+#include "base/bind.h" |
+#include "net/base/completion_callback.h" |
+#include "net/base/net_errors.h" |
+#include "net/base/test_completion_callback.h" |
+#include "net/base/test_data_directory.h" |
+#include "net/disk_cache/memory/mem_backend_impl.h" |
+#include "net/test/cert_test_util.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
wtc
2014/06/12 03:13:17
You can put this file in the net namespace, so tha
|
+ |
+// This class is essentially a workaround for not being able to use |
+// net::CompletionCallback with non-int parameters. Using this class as a sort |
+// of wrapper, it is possible to use the DiskBasedCertCache and also |
+// net::TestCompletionCallback::WaitForResult. |
+ |
+class MockCertCacheUser { |
+ public: |
+ explicit MockCertCacheUser(net::DiskBasedCertCache* cert_cache) |
+ : weak_factory_(this), cert_cache_(cert_cache) {} |
+ |
+ void Set(const net::X509Certificate::OSCertHandle cert_handle, |
+ net::CompletionCallback cb) { |
+ user_callback_ = cb; |
+ cert_cache_->Set( |
+ cert_handle, |
+ base::Bind(&MockCertCacheUser::FinishSet, weak_factory_.GetWeakPtr())); |
+ } |
+ |
+ // todo: take in a key. This is slightly cumbersome ( ? since the string must |
wtc
2014/06/12 03:13:17
Use all caps TODO, and include your user name.
|
+ // stay alive for the cert cache and is passed by const ref) |
+ void Get(net::CompletionCallback cb) { |
+ user_callback_ = cb; |
+ cert_cache_->Get( |
+ last_key_, |
+ base::Bind(&MockCertCacheUser::FinishGet, weak_factory_.GetWeakPtr())); |
+ } |
+ |
+ void FinishSet(const std::string& key) { |
+ ASSERT_TRUE(!key.empty()); |
+ last_key_ = key; |
+ net::CompletionCallback callback = user_callback_; |
+ user_callback_.Reset(); |
+ callback.Run(net::OK); |
+ } |
+ |
+ void FinishGet(net::X509Certificate::OSCertHandle cert_handle) { |
+ ASSERT_TRUE(cert_handle); |
+ |
+ last_cert_handle_ = cert_handle; |
+ net::CompletionCallback callback = user_callback_; |
+ user_callback_.Reset(); |
+ callback.Run(net::OK); |
+ } |
+ |
+ net::X509Certificate::OSCertHandle GetLastCertHandle() { |
+ return last_cert_handle_; |
+ } |
+ |
+ private: |
+ base::WeakPtrFactory<MockCertCacheUser> weak_factory_; |
+ net::DiskBasedCertCache* cert_cache_; |
+ net::CompletionCallback user_callback_; |
+ std::string last_key_; |
+ net::X509Certificate::OSCertHandle last_cert_handle_; |
+}; |
+ |
+} // namespace |
+ |
+// Stores a cert cache in DiskBasedCertCache, then retrieves it |
+// and makes sure it was retrieved successfully. |
+TEST(DiskBasedCertCache, SimpleSetAndGet) { |
+ scoped_ptr<disk_cache::Backend> backend( |
+ disk_cache::MemBackendImpl::CreateBackend(0, NULL)); |
+ |
+ ASSERT_TRUE(backend.get()); |
+ |
+ net::DiskBasedCertCache cert_cache(backend.get()); |
+ |
+ scoped_refptr<net::X509Certificate> cert(net::ImportCertFromFile( |
+ net::GetTestCertsDirectory(), "root_ca_cert.pem")); |
+ |
+ ASSERT_TRUE(cert.get()); |
+ |
+ MockCertCacheUser user(&cert_cache); |
+ |
+ net::TestCompletionCallback callbackSet; |
+ net::TestCompletionCallback callbackGet; |
+ |
+ user.Set(cert.get()->os_cert_handle(), callbackSet.callback()); |
+ callbackSet.WaitForResult(); |
+ |
+ user.Get(callbackGet.callback()); |
+ callbackGet.WaitForResult(); |
+ |
+ net::X509Certificate::OSCertHandle retrieved_cert_handle = |
+ user.GetLastCertHandle(); |
+ |
+ ASSERT_TRUE(net::X509Certificate::IsSameOSCert(retrieved_cert_handle, |
+ cert.get()->os_cert_handle())); |
+} |
+ |
+// Test the behavior when a matching entry already exists when set is called. |
+TEST(DiskBasedCertCache, OverwriteSet) { |
+ scoped_ptr<disk_cache::Backend> backend( |
+ disk_cache::MemBackendImpl::CreateBackend(0, NULL)); |
+ |
+ net::DiskBasedCertCache cert_cache(backend.get()); |
+ |
+ scoped_refptr<net::X509Certificate> cert(net::ImportCertFromFile( |
+ net::GetTestCertsDirectory(), "root_ca_cert.pem")); |
+ |
+ MockCertCacheUser user(&cert_cache); |
+ |
+ net::TestCompletionCallback callbackSet; |
+ net::TestCompletionCallback callbackGet; |
+ |
+ user.Set(cert.get()->os_cert_handle(), callbackSet.callback()); |
+ callbackSet.WaitForResult(); |
+ |
+ user.Set(cert.get()->os_cert_handle(), callbackSet.callback()); |
+ callbackSet.WaitForResult(); |
+} |