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

Unified Diff: net/cert/cert_verifier_cache_persister_unittest.cc

Issue 1892033002: Cert - protobufs to serialize and deserialize CertVerifierCache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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/cert/cert_verifier_cache_persister_unittest.cc
diff --git a/net/cert/cert_verifier_cache_persister_unittest.cc b/net/cert/cert_verifier_cache_persister_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..843b0d1c02053b08aa6248cde257d942809104a6
--- /dev/null
+++ b/net/cert/cert_verifier_cache_persister_unittest.cc
@@ -0,0 +1,117 @@
+// Copyright (c) 2016 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/cert/cert_verifier_cache_persister.h"
+
+#include <memory>
+
+#include "base/files/file_path.h"
+#include "base/memory/ref_counted.h"
+#include "net/base/net_errors.h"
+#include "net/base/test_completion_callback.h"
+#include "net/base/test_data_directory.h"
+#include "net/cert/cert_trust_anchor_provider.h"
+#include "net/cert/cert_verify_proc.h"
+#include "net/cert/cert_verify_result.h"
+#include "net/cert/multi_threaded_cert_verifier.h"
+#include "net/cert/x509_certificate.h"
+#include "net/log/net_log.h"
+#include "net/test/cert_test_util.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace {
+
+class MockCertVerifyProc : public CertVerifyProc {
+ public:
+ MockCertVerifyProc() {}
+
+ private:
+ ~MockCertVerifyProc() override {}
+
+ // CertVerifyProc implementation
+ bool SupportsAdditionalTrustAnchors() const override { return false; }
+ bool SupportsOCSPStapling() const override { return false; }
+
+ int VerifyInternal(X509Certificate* cert,
+ const std::string& hostname,
+ const std::string& ocsp_response,
+ int flags,
+ CRLSet* crl_set,
+ const CertificateList& additional_trust_anchors,
+ CertVerifyResult* verify_result) override {
+ verify_result->Reset();
+ verify_result->verified_cert = cert;
+ verify_result->cert_status = CERT_STATUS_COMMON_NAME_INVALID;
+ return ERR_CERT_COMMON_NAME_INVALID;
+ }
+};
+
+} // namespace
+
+TEST(CertVerifierCachePersisterTest, PersistCache) {
+ MultiThreadedCertVerifier verifier(new MockCertVerifyProc());
+ CertVerifierCachePersister persister(&verifier);
+
+ base::FilePath certs_dir = GetTestCertsDirectory();
+ scoped_refptr<X509Certificate> test_cert(
+ ImportCertFromFile(certs_dir, "ok_cert.pem"));
+ ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
+
+ std::string example_hostname("www.example.com");
+ std::string ocsp_response;
+ int flags = 0;
+ CRLSet* crl_set = NULL;
+ int error;
+ CertVerifyResult verify_result;
+ TestCompletionCallback callback;
+ std::unique_ptr<CertVerifier::Request> request;
+
+ error = verifier.Verify(test_cert.get(), example_hostname, ocsp_response,
+ flags, crl_set, &verify_result, callback.callback(),
+ &request, BoundNetLog());
+ ASSERT_EQ(ERR_IO_PENDING, error);
+ EXPECT_TRUE(request);
+ error = callback.WaitForResult();
+ ASSERT_TRUE(IsCertificateError(error));
+
+ CertVerifyResult verify_result_serialized;
+ error = verifier.Verify(test_cert.get(), example_hostname, ocsp_response,
+ flags, crl_set, &verify_result_serialized,
+ callback.callback(), &request, BoundNetLog());
Ryan Sleevi 2016/05/13 20:18:12 What are you testing here? That MTCV caches? Its
ramant (doing other things) 2016/05/27 19:57:14 +1. It is unnecessary. Done.
+ // Synchronous completion.
+ ASSERT_NE(ERR_IO_PENDING, error);
+ ASSERT_TRUE(IsCertificateError(error));
+ ASSERT_FALSE(request);
+
+ std::string data;
+ persister.SerializeCache(&data);
+ EXPECT_FALSE(data.empty());
+
+ // Restoring empty data should fail.
+ EXPECT_FALSE(persister.LoadCache(std::string()));
Ryan Sleevi 2016/05/13 20:18:12 Separate test
ramant (doing other things) 2016/05/27 19:57:14 Done.
+
+ // Restoring corrupted data should fail.
+ EXPECT_FALSE(persister.LoadCache(std::string("junk")));
Ryan Sleevi 2016/05/13 20:18:12 Separate test
ramant (doing other things) 2016/05/27 19:57:14 Done.
+
+ // Restore the cache data for an existing entry should fail.
+ EXPECT_FALSE(persister.LoadCache(data));
Ryan Sleevi 2016/05/13 20:18:12 Separate test
ramant (doing other things) 2016/05/27 19:57:14 Done.
+
+ // Create a new Verifier and restoring the data into it, should succeed.
Ryan Sleevi 2016/05/13 20:18:12 Separate test
ramant (doing other things) 2016/05/27 19:57:14 Done.
+ MultiThreadedCertVerifier verifier2(new MockCertVerifyProc());
+ CertVerifierCachePersister persister2(&verifier2);
+ EXPECT_TRUE(persister2.LoadCache(data));
+
+ error = verifier2.Verify(test_cert.get(), example_hostname, ocsp_response,
+ flags, crl_set, &verify_result, callback.callback(),
+ &request, BoundNetLog());
+ // Synchronous completion and verify that there is a cache hit.
+ ASSERT_NE(ERR_IO_PENDING, error);
+ ASSERT_TRUE(IsCertificateError(error));
+ ASSERT_FALSE(request);
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698