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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/http/disk_based_cert_cache.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "net/base/completion_callback.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/base/test_data_directory.h"
13 #include "net/disk_cache/memory/mem_backend_impl.h"
14 #include "net/http/http_cache.h"
15 #include "net/test/cert_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace net {
19
20 namespace {
21
22 // This class is essentially a workaround for not being able to use
23 // CompletionCallback with non-int parameters. Using this class
24 // it is possible to use the DiskBasedCertCache and also
25 // TestCompletionCallback::WaitForResult.
26
27 class MockCertCacheUser {
28 public:
29 explicit MockCertCacheUser(DiskBasedCertCache* cert_cache)
30 : weak_factory_(this), cert_cache_(cert_cache) {}
31
32 void Set(const X509Certificate::OSCertHandle cert_handle,
33 std::string& key,
34 CompletionCallback cb) {
35 user_callback_ = cb;
36 cert_cache_->Set(
37 cert_handle,
38 base::Bind(
39 &MockCertCacheUser::FinishSet, weak_factory_.GetWeakPtr(), &key));
40 }
41
42 void Get(std::string key,
43 X509Certificate::OSCertHandle& cert_handle,
44 CompletionCallback cb) {
45 user_callback_ = cb;
46 cert_cache_->Get(key,
47 base::Bind(&MockCertCacheUser::FinishGet,
48 weak_factory_.GetWeakPtr(),
49 &cert_handle));
50 }
51
52 void FinishSet(std::string* key_return, const std::string& key_received) {
53 *key_return = key_received;
54 base::ResetAndReturn(&user_callback_).Run(OK);
55 }
56
57 void FinishGet(X509Certificate::OSCertHandle* handle_return,
58 const X509Certificate::OSCertHandle handle_retrieved) {
59 *handle_return = handle_retrieved;
60 base::ResetAndReturn(&user_callback_).Run(OK);
61 }
62
63 private:
64 base::WeakPtrFactory<MockCertCacheUser> weak_factory_;
65 DiskBasedCertCache* cert_cache_;
66 CompletionCallback user_callback_;
67 };
68
69 // ----------------------------------------------------------------------------
70
71 } // namespace
72 TEST(DiskBasedCertCache, SetNewCert) {
73 scoped_ptr<disk_cache::Backend> backend(
74 disk_cache::MemBackendImpl::CreateBackend(0, NULL));
75
76 EXPECT_TRUE(backend.get());
77
78 DiskBasedCertCache cert_cache(backend.get());
79
80 scoped_refptr<X509Certificate> cert(
81 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
82
83 EXPECT_TRUE(cert.get());
84
85 MockCertCacheUser user(&cert_cache);
86
87 TestCompletionCallback set_callback;
88
89 std::string key;
90
91 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
92 set_callback.WaitForResult();
93
94 ASSERT_TRUE(!key.empty());
95 }
96
97 // Tests that attempting to retrieve a cert that is not in the cache will
98 // return NULL.
99 TEST(DiskBasedCertCache, GetUncachedCert) {
100 scoped_ptr<disk_cache::Backend> backend(
101 disk_cache::MemBackendImpl::CreateBackend(0, NULL));
102 EXPECT_TRUE(backend.get());
103
104 DiskBasedCertCache cert_cache(backend.get());
105
106 MockCertCacheUser user(&cert_cache);
107
108 TestCompletionCallback get_callback;
109
110 X509Certificate::OSCertHandle cert_handle = NULL;
111 user.Get("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4",
112 cert_handle,
113 get_callback.callback());
114 get_callback.WaitForResult();
115
116 ASSERT_EQ(NULL, cert_handle);
117 }
118
119 // Tests that no errors will be thrown if a certificate is already in the cache,
120 // and that the past cached data will be overwritten without any errors.
121 TEST(DiskBasedCertCache, SetOverwrite) {
122 scoped_ptr<disk_cache::Backend> backend(
123 disk_cache::MemBackendImpl::CreateBackend(0, NULL));
124
125 DiskBasedCertCache cert_cache(backend.get());
126
127 scoped_refptr<X509Certificate> cert(
128 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
129
130 MockCertCacheUser user(&cert_cache);
131
132 TestCompletionCallback set_callback;
133 TestCompletionCallback get_callback;
134
135 std::string key;
136
137 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
138 set_callback.WaitForResult();
139
140 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
141 set_callback.WaitForResult();
142 }
143
144 // Stores a cert cache in DiskBasedCertCache, then retrieves it
145 // and makes sure it was retrieved successfully.
146 TEST(DiskBasedCertCache, SimpleSetAndGet) {
147 scoped_ptr<disk_cache::Backend> backend(
148 disk_cache::MemBackendImpl::CreateBackend(0, NULL));
149
150 EXPECT_TRUE(backend.get());
151
152 DiskBasedCertCache cert_cache(backend.get());
153
154 scoped_refptr<X509Certificate> cert(
155 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
156
157 EXPECT_TRUE(cert.get());
158
159 MockCertCacheUser user(&cert_cache);
160
161 TestCompletionCallback set_callback;
162 TestCompletionCallback get_callback;
163
164 std::string key;
165 X509Certificate::OSCertHandle retrieved_cert_handle = NULL;
166
167 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
168 set_callback.WaitForResult();
169
170 user.Get(key, retrieved_cert_handle, get_callback.callback());
171 get_callback.WaitForResult();
172
173 ASSERT_TRUE(X509Certificate::IsSameOSCert(retrieved_cert_handle,
174 cert.get()->os_cert_handle()));
175 }
176
177 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698