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

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: Workers now delete themselves, and DBCC deletion cancels workers. 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/mock_http_cache.h"
15 #include "net/test/cert_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 namespace net {
18
19 namespace {
20
21 // MockTransactions are required to use the blocking Mock Disk Cache Backend.
22
23 // This transaction corresponds to "root_ca_cert.pem" in GetTestCertsDirectory()
24 const MockTransaction kCertTransaction1 = {
25 "cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4", "", base::Time(),
26 "", LOAD_NORMAL, "",
27 "", base::Time(), "",
28 TEST_MODE_NORMAL, NULL, 0};
29
30 // This transaction corresponds to "ok_cert.pem" in GetTestCertsDirectory()
31 const MockTransaction kCertTransaction2 = {
32 "cert:9174C7CB9E4919604E7B1BFC430E4929DA45F65F", "", base::Time(),
33 "", LOAD_NORMAL, "",
34 "", base::Time(), "",
35 TEST_MODE_NORMAL, NULL, 0};
36
37 // MockCertCache is used so that results from the DiskBasedCertCache can be
38 // achieved
39 // using CompletionCallback::WaitForResult.
40 class MockCertCache {
41 public:
42 MockCertCache()
43 : backend(new MockDiskCache()),
44 cert_cache_(new DiskBasedCertCache(backend.get())),
45 weak_factory_(this) {}
46
47 void Set(const X509Certificate::OSCertHandle cert_handle,
48 std::string& key,
49 CompletionCallback cb) {
50 cert_cache_->Set(
51 cert_handle,
52 base::Bind(
53 &MockCertCache::FinishSet, weak_factory_.GetWeakPtr(), &key, cb));
54 }
55
56 void Get(std::string key,
57 X509Certificate::OSCertHandle& cert_handle,
58 CompletionCallback cb) {
59 cert_cache_->Get(key,
60 base::Bind(&MockCertCache::FinishGet,
61 weak_factory_.GetWeakPtr(),
62 &cert_handle,
63 cb));
64 }
65
66 void FinishSet(std::string* key_return,
67 CompletionCallback cb,
68 const std::string& key_received) {
69 *key_return = key_received;
70 base::ResetAndReturn(&cb).Run(OK);
71 }
72
73 void FinishGet(X509Certificate::OSCertHandle* handle_return,
74 CompletionCallback cb,
75 const X509Certificate::OSCertHandle handle_retrieved) {
76 *handle_return = handle_retrieved;
77 base::ResetAndReturn(&cb).Run(OK);
78 }
79
80 private:
81 scoped_ptr<disk_cache::Backend> backend;
82 scoped_ptr<DiskBasedCertCache> cert_cache_;
83 base::WeakPtrFactory<MockCertCache> weak_factory_;
84 };
85
86 // ----------------------------------------------------------------------------
87
88 } // namespace
89
90 // Tests that a certificate can be stored in the cache.
91 TEST(DiskBasedCertCache, SetCert) {
92 AddMockTransaction(&kCertTransaction1);
93 MockCertCache user;
94
95 scoped_refptr<X509Certificate> cert(
96 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
97
98 EXPECT_TRUE(cert.get());
99
100 TestCompletionCallback set_callback;
101
102 std::string key;
103
104 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
105 set_callback.WaitForResult();
106
107 ASSERT_TRUE(!key.empty());
108 }
109
110 // Tests that attempting to retrieve a cert that is not in the cache will
111 // return NULL.
112 TEST(DiskBasedCertCache, GetUncachedCert) {
113 AddMockTransaction(&kCertTransaction1);
114 MockCertCache user;
115
116 TestCompletionCallback get_callback;
117
118 X509Certificate::OSCertHandle cert_handle = NULL;
119 user.Get("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4",
120 cert_handle,
121 get_callback.callback());
122 get_callback.WaitForResult();
123
124 ASSERT_EQ(NULL, cert_handle);
125 }
126
127 // Tests that the same certificate can be requested to be stored from multiple
128 // locations simultaneously.
129 TEST(DiskBasedCertCache, SetMultiple) {
130 AddMockTransaction(&kCertTransaction1);
131 MockCertCache user;
132
133 scoped_refptr<X509Certificate> cert(
134 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
135
136 TestCompletionCallback set_callback1;
137 TestCompletionCallback set_callback2;
138
139 std::string key;
140
141 user.Set(cert.get()->os_cert_handle(), key, set_callback1.callback());
142 user.Set(cert.get()->os_cert_handle(), key, set_callback2.callback());
143 set_callback1.WaitForResult();
144 set_callback2.WaitForResult();
145 }
146
147 // Stores a certificate in DiskBasedCertCache, then retrieves it
148 // and makes sure it was retrieved successfully.
149 TEST(DiskBasedCertCache, SimpleSetAndGet) {
150 AddMockTransaction(&kCertTransaction1);
151 MockCertCache user;
152
153 scoped_refptr<X509Certificate> cert(
154 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
155
156 EXPECT_TRUE(cert.get());
157
158 TestCompletionCallback set_callback;
159 TestCompletionCallback get_callback;
160
161 std::string key;
162 X509Certificate::OSCertHandle retrieved_cert_handle = NULL;
163
164 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
165 set_callback.WaitForResult();
166
167 user.Get(key, retrieved_cert_handle, get_callback.callback());
168 get_callback.WaitForResult();
169
170 ASSERT_TRUE(X509Certificate::IsSameOSCert(retrieved_cert_handle,
171 cert.get()->os_cert_handle()));
172 }
173
174 // Test some basic usage with multiple certificates being stored and retrieved
175 // at the same time.
176 TEST(DiskBasedCertCache, BasicUsage) {
177 AddMockTransaction(&kCertTransaction1);
178 AddMockTransaction(&kCertTransaction2);
179
180 MockCertCache user;
181
182 scoped_refptr<X509Certificate> cert1(
183 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
184
185 scoped_refptr<X509Certificate> cert2(
186 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"));
187
188 EXPECT_TRUE(cert1.get());
189 EXPECT_TRUE(cert2.get());
190 EXPECT_TRUE(!X509Certificate::IsSameOSCert(cert1->os_cert_handle(),
191 cert2->os_cert_handle()));
192
193 TestCompletionCallback set_callback1, set_callback2;
194 std::string key1, key2;
195
196 user.Set(cert1.get()->os_cert_handle(), key1, set_callback1.callback());
197 user.Set(cert2.get()->os_cert_handle(), key2, set_callback2.callback());
198 set_callback1.WaitForResult();
199 set_callback2.WaitForResult();
200
201 TestCompletionCallback get_callback1, get_callback2;
202 X509Certificate::OSCertHandle cert_handle1, cert_handle2;
203
204 user.Get(key2, cert_handle2, get_callback2.callback());
205 user.Get(key1, cert_handle1, get_callback1.callback());
206 get_callback2.WaitForResult();
207 get_callback1.WaitForResult();
208
209 ASSERT_TRUE(
210 X509Certificate::IsSameOSCert(cert1->os_cert_handle(), cert_handle1));
211 ASSERT_TRUE(
212 X509Certificate::IsSameOSCert(cert2->os_cert_handle(), cert_handle2));
213 }
214
215 // Tests result of if a certificate is simultaneously asked to be stored and
216 // retrieved
217 // from the cache.
218 // TODO(brandonsalmon): improve the functionality of this circumstance.
219 TEST(DiskBasedCertCache, SimultaneousSetGet) {
220 AddMockTransaction(&kCertTransaction1);
221
222 MockCertCache user;
223
224 scoped_refptr<X509Certificate> cert(
225 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
226
227 TestCompletionCallback set_callback, get_callback;
228 X509Certificate::OSCertHandle cert_handle;
229 std::string key("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4");
230
231 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback());
232 user.Get(key, cert_handle, get_callback.callback());
233 get_callback.WaitForResult();
234 set_callback.WaitForResult();
235 }
236
237 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698