Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1444)

Unified Diff: net/http/disk_based_cert_cache_unittest.cc

Issue 329733002: Disk Based Certificate Cache Implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improved unittests, error checking, and improved functionality in workers (although this isn't prop… Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3e44b6b6c3b0bb4d2f608e5d59a2583ff472e5a9
--- /dev/null
+++ b/net/http/disk_based_cert_cache_unittest.cc
@@ -0,0 +1,177 @@
+// 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 "base/callback_helpers.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/http/http_cache.h"
+#include "net/test/cert_test_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace {
+
+// This class is essentially a workaround for not being able to use
+// CompletionCallback with non-int parameters. Using this class
+// it is possible to use the DiskBasedCertCache and also
+// TestCompletionCallback::WaitForResult.
+
+class MockCertCacheUser {
+ public:
+ explicit MockCertCacheUser(DiskBasedCertCache* cert_cache)
+ : weak_factory_(this), cert_cache_(cert_cache) {}
+
+ void Set(const X509Certificate::OSCertHandle cert_handle,
+ std::string& key,
+ CompletionCallback cb) {
+ user_callback_ = cb;
+ cert_cache_->Set(
+ cert_handle,
+ base::Bind(
+ &MockCertCacheUser::FinishSet, weak_factory_.GetWeakPtr(), &key));
+ }
+
+ void Get(std::string key,
+ X509Certificate::OSCertHandle& cert_handle,
+ CompletionCallback cb) {
+ user_callback_ = cb;
+ cert_cache_->Get(key,
+ base::Bind(&MockCertCacheUser::FinishGet,
+ weak_factory_.GetWeakPtr(),
+ &cert_handle));
+ }
+
+ void FinishSet(std::string* key_return, const std::string& key_received) {
+ *key_return = key_received;
+ base::ResetAndReturn(&user_callback_).Run(OK);
+ }
+
+ void FinishGet(X509Certificate::OSCertHandle* handle_return,
+ const X509Certificate::OSCertHandle handle_retrieved) {
+ *handle_return = handle_retrieved;
+ base::ResetAndReturn(&user_callback_).Run(OK);
+ }
+
+ private:
+ base::WeakPtrFactory<MockCertCacheUser> weak_factory_;
+ DiskBasedCertCache* cert_cache_;
+ CompletionCallback user_callback_;
+};
+
+// ----------------------------------------------------------------------------
+
+} // namespace
+TEST(DiskBasedCertCache, SetNewCert) {
+ scoped_ptr<disk_cache::Backend> backend(
+ disk_cache::MemBackendImpl::CreateBackend(0, NULL));
+
+ EXPECT_TRUE(backend.get());
+
+ DiskBasedCertCache cert_cache(backend.get());
+
+ scoped_refptr<X509Certificate> cert(
+ ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
+
+ EXPECT_TRUE(cert.get());
+
+ MockCertCacheUser user(&cert_cache);
+
+ TestCompletionCallback set_callback;
+
+ std::string key;
+
+ user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
+ set_callback.WaitForResult();
+
+ ASSERT_TRUE(!key.empty());
+}
+
+// Tests that attempting to retrieve a cert that is not in the cache will
+// return NULL.
+TEST(DiskBasedCertCache, GetUncachedCert) {
+ scoped_ptr<disk_cache::Backend> backend(
+ disk_cache::MemBackendImpl::CreateBackend(0, NULL));
+ EXPECT_TRUE(backend.get());
+
+ DiskBasedCertCache cert_cache(backend.get());
+
+ MockCertCacheUser user(&cert_cache);
+
+ TestCompletionCallback get_callback;
+
+ X509Certificate::OSCertHandle cert_handle = NULL;
+ user.Get("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4",
+ cert_handle,
+ get_callback.callback());
+ get_callback.WaitForResult();
+
+ ASSERT_EQ(NULL, cert_handle);
+}
+
+// Tests that no errors will be thrown if a certificate is already in the cache,
+// and that the past cached data will be overwritten without any errors.
+TEST(DiskBasedCertCache, SetOverwrite) {
+ scoped_ptr<disk_cache::Backend> backend(
+ disk_cache::MemBackendImpl::CreateBackend(0, NULL));
+
+ DiskBasedCertCache cert_cache(backend.get());
+
+ scoped_refptr<X509Certificate> cert(
+ ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
+
+ MockCertCacheUser user(&cert_cache);
+
+ TestCompletionCallback set_callback;
+ TestCompletionCallback get_callback;
+
+ std::string key;
+
+ user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
+ set_callback.WaitForResult();
+
+ user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
+ set_callback.WaitForResult();
+}
+
+// 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));
+
+ EXPECT_TRUE(backend.get());
+
+ DiskBasedCertCache cert_cache(backend.get());
+
+ scoped_refptr<X509Certificate> cert(
+ ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
+
+ EXPECT_TRUE(cert.get());
+
+ MockCertCacheUser user(&cert_cache);
+
+ TestCompletionCallback set_callback;
+ TestCompletionCallback get_callback;
+
+ std::string key;
+ X509Certificate::OSCertHandle retrieved_cert_handle = NULL;
+
+ user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
+ set_callback.WaitForResult();
+
+ user.Get(key, retrieved_cert_handle, get_callback.callback());
+ get_callback.WaitForResult();
+
+ ASSERT_TRUE(X509Certificate::IsSameOSCert(retrieved_cert_handle,
+ cert.get()->os_cert_handle()));
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698