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

Side by Side 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, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/cert/cert_verifier_cache_persister.h"
6
7 #include <memory>
8
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/test_completion_callback.h"
13 #include "net/base/test_data_directory.h"
14 #include "net/cert/cert_trust_anchor_provider.h"
15 #include "net/cert/cert_verify_proc.h"
16 #include "net/cert/cert_verify_result.h"
17 #include "net/cert/multi_threaded_cert_verifier.h"
18 #include "net/cert/x509_certificate.h"
19 #include "net/log/net_log.h"
20 #include "net/test/cert_test_util.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace net {
25
26 namespace {
27
28 class MockCertVerifyProc : public CertVerifyProc {
29 public:
30 MockCertVerifyProc() {}
31
32 private:
33 ~MockCertVerifyProc() override {}
34
35 // CertVerifyProc implementation
36 bool SupportsAdditionalTrustAnchors() const override { return false; }
37 bool SupportsOCSPStapling() const override { return false; }
38
39 int VerifyInternal(X509Certificate* cert,
40 const std::string& hostname,
41 const std::string& ocsp_response,
42 int flags,
43 CRLSet* crl_set,
44 const CertificateList& additional_trust_anchors,
45 CertVerifyResult* verify_result) override {
46 verify_result->Reset();
47 verify_result->verified_cert = cert;
48 verify_result->cert_status = CERT_STATUS_COMMON_NAME_INVALID;
49 return ERR_CERT_COMMON_NAME_INVALID;
50 }
51 };
52
53 } // namespace
54
55 TEST(CertVerifierCachePersisterTest, PersistCache) {
56 MultiThreadedCertVerifier verifier(new MockCertVerifyProc());
57 CertVerifierCachePersister persister(&verifier);
58
59 base::FilePath certs_dir = GetTestCertsDirectory();
60 scoped_refptr<X509Certificate> test_cert(
61 ImportCertFromFile(certs_dir, "ok_cert.pem"));
62 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
63
64 std::string example_hostname("www.example.com");
65 std::string ocsp_response;
66 int flags = 0;
67 CRLSet* crl_set = NULL;
68 int error;
69 CertVerifyResult verify_result;
70 TestCompletionCallback callback;
71 std::unique_ptr<CertVerifier::Request> request;
72
73 error = verifier.Verify(test_cert.get(), example_hostname, ocsp_response,
74 flags, crl_set, &verify_result, callback.callback(),
75 &request, BoundNetLog());
76 ASSERT_EQ(ERR_IO_PENDING, error);
77 EXPECT_TRUE(request);
78 error = callback.WaitForResult();
79 ASSERT_TRUE(IsCertificateError(error));
80
81 CertVerifyResult verify_result_serialized;
82 error = verifier.Verify(test_cert.get(), example_hostname, ocsp_response,
83 flags, crl_set, &verify_result_serialized,
84 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.
85 // Synchronous completion.
86 ASSERT_NE(ERR_IO_PENDING, error);
87 ASSERT_TRUE(IsCertificateError(error));
88 ASSERT_FALSE(request);
89
90 std::string data;
91 persister.SerializeCache(&data);
92 EXPECT_FALSE(data.empty());
93
94 // Restoring empty data should fail.
95 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.
96
97 // Restoring corrupted data should fail.
98 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.
99
100 // Restore the cache data for an existing entry should fail.
101 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.
102
103 // 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.
104 MultiThreadedCertVerifier verifier2(new MockCertVerifyProc());
105 CertVerifierCachePersister persister2(&verifier2);
106 EXPECT_TRUE(persister2.LoadCache(data));
107
108 error = verifier2.Verify(test_cert.get(), example_hostname, ocsp_response,
109 flags, crl_set, &verify_result, callback.callback(),
110 &request, BoundNetLog());
111 // Synchronous completion and verify that there is a cache hit.
112 ASSERT_NE(ERR_IO_PENDING, error);
113 ASSERT_TRUE(IsCertificateError(error));
114 ASSERT_FALSE(request);
115 }
116
117 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698