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

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: Fixed memory leak by remembering to close entry and free OSCertHandle. Created 6 years, 5 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
« no previous file with comments | « net/http/disk_based_cert_cache.cc ('k') | net/net.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/io_buffer.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/disk_cache/memory/mem_backend_impl.h"
15 #include "net/http/mock_http_cache.h"
16 #include "net/test/cert_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace net {
20
21 namespace {
22
23 // Testing the DiskBasedCertCache requires constant use of the
24 // certificates in GetTestCertsDirectory(). The TestCertMetaData
25 // struct stores metadata relevant to the DiskBasedCertCache for
26 // each used test certificate.
27 struct TestCertMetaData {
28 const char* file_name;
29 const char* cache_key;
30 };
31
32 const TestCertMetaData kCert1 = {
33 "root_ca_cert.pem", "cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4"};
34
35 const TestCertMetaData kCert2 = {
36 "ok_cert.pem", "cert:9174C7CB9E4919604E7B1BFC430E4929DA45F65F"};
37
38 // MockTransactions are required to use the MockDiskCache backend.
39 // |key| is a cache key, and is equivalent to the key that will be
40 // used to store or retrieve certificates in the cache. |test_mode|
41 // is an integer that is used to indicate properties of the test
42 // transaction, mostly whether or not it is synchronous.
43 // For testing the DiskBasedCertCache, other data members of the struct
44 // are irrelevant. Only one MockTransaction per certificate can be used
45 // at a time.
46 MockTransaction CreateMockTransaction(const char* key, int test_mode) {
47 MockTransaction transaction = {key, "", base::Time(), "", LOAD_NORMAL,
48 "", "", base::Time(), "", test_mode,
49 NULL, 0, OK};
50
51 return transaction;
52 }
53
54 // Helper class, for use with DiskBasedCertCache::Get, that will ensure that
55 // the returned certificate handle is kept alive after the callback has been
56 // executed and allow a user to WaitForResult of DiskBasedCertCache::Get.
57 class TestGetCallback {
58 public:
59 TestGetCallback() : cert_handle_(NULL) {}
60 ~TestGetCallback() {
61 if (cert_handle_)
62 X509Certificate::FreeOSCertHandle(cert_handle_);
63 }
64
65 // Blocks until the underlying Get() operation has succeeded.
66 void WaitForResult() { cb_.WaitForResult(); }
67
68 // Returns a Callback suitable for use with DiskBasedCertCache::Get(). The
69 // returned callback is only valid while the TestGetCallback object is still
70 // valid.
71 DiskBasedCertCache::GetCallback callback() {
72 return base::Bind(&TestGetCallback::OnGetComplete, base::Unretained(this));
73 }
74
75 // Returns the associated certificate handle.
76 const X509Certificate::OSCertHandle& cert_handle() const {
77 return cert_handle_;
78 }
79
80 private:
81 void OnGetComplete(const X509Certificate::OSCertHandle handle) {
82 if (handle)
83 cert_handle_ = X509Certificate::DupOSCertHandle(handle);
84 cb_.callback().Run(OK);
85 }
86
87 TestCompletionCallback cb_;
88 X509Certificate::OSCertHandle cert_handle_;
89 };
90
91 // Helper class, for use with DiskBasedCertCache::Set, that will store the
92 // returned key and allow a user to WaitForResult of DiskBasedCertCache::Set.
93 class TestSetCallback {
94 public:
95 TestSetCallback() {}
96 ~TestSetCallback() {}
97
98 // Blocks until the underlying Set() operation has succeeded.
99 void WaitForResult() { cb_.WaitForResult(); }
100
101 // Returns a Callback suitable for use with DiskBasedCertCache::Set(). The
102 // returned callback is only valid while the TestSetCallback object is still
103 // valid.
104 DiskBasedCertCache::SetCallback callback() {
105 return base::Bind(&TestSetCallback::OnSetComplete, base::Unretained(this));
106 }
107
108 // Returns the associated certificate handle.
109 const std::string& key() const { return key_; }
110
111 private:
112 void OnSetComplete(const std::string& key) {
113 key_ = key;
114 cb_.callback().Run(OK);
115 }
116
117 TestCompletionCallback cb_;
118 std::string key_;
119 };
120
121 // Stores the certificate corresponding to |cert_data| in |backend|. If
122 // |corrupt_data| is true, the certificate will be imported with errors
123 // so as to mimic a corrupted file on disk.
124 void ImportCert(disk_cache::Backend* backend,
125 const TestCertMetaData& cert_data,
126 bool corrupt_data) {
127 disk_cache::Entry* entry;
128 TestCompletionCallback callback;
129 int rv =
130 backend->CreateEntry(cert_data.cache_key, &entry, callback.callback());
131 EXPECT_EQ(OK, callback.GetResult(rv));
132 scoped_refptr<X509Certificate> cert(
133 ImportCertFromFile(GetTestCertsDirectory(), cert_data.file_name));
134 std::string write_data;
135 bool encoded =
136 X509Certificate::GetDEREncoded(cert->os_cert_handle(), &write_data);
137 ASSERT_TRUE(encoded);
138 if (corrupt_data) {
139 for (size_t i = 0; i < write_data.size(); i += 20)
140 ++write_data[i];
141 }
142 scoped_refptr<IOBuffer> buffer(new IOBuffer(write_data.size()));
143 memcpy(buffer->data(), write_data.data(), write_data.size());
144 rv = entry->WriteData(0 /* index */,
145 0 /* offset */,
146 buffer,
147 write_data.size(),
148 callback.callback(),
149 true /* truncate */);
150 ASSERT_EQ(static_cast<int>(write_data.size()), callback.GetResult(rv));
151 entry->Close();
152 }
153
154 // Checks that the the certificate corresponding to |cert_data| is an existing,
155 // correctly cached entry in |backend|.
156 void CheckCertCached(disk_cache::Backend* backend,
157 const TestCertMetaData& cert_data) {
158 disk_cache::Entry* entry;
159 TestCompletionCallback callback;
160 int rv = backend->OpenEntry(cert_data.cache_key, &entry, callback.callback());
161 EXPECT_EQ(OK, callback.GetResult(rv));
162 scoped_refptr<X509Certificate> cert(
163 ImportCertFromFile(GetTestCertsDirectory(), cert_data.file_name));
164 std::string write_data;
165 bool encoded =
166 X509Certificate::GetDEREncoded(cert->os_cert_handle(), &write_data);
167 ASSERT_TRUE(encoded);
168 int entry_size = entry->GetDataSize(0 /* index */);
169 scoped_refptr<IOBuffer> buffer(new IOBuffer(entry_size));
170 rv = entry->ReadData(
171 0 /* index */, 0 /* offset */, buffer, entry_size, callback.callback());
172 EXPECT_EQ(entry_size, callback.GetResult(rv));
173 entry->Close();
wtc 2014/06/28 14:22:30 Nit: as defensive programming, it is a good idea t
rvargas (doing something else) 2014/06/30 18:21:24 On the other hand, if it crashes due to reuse (som
174 X509Certificate::OSCertHandle cached_cert_handle =
175 X509Certificate::CreateOSCertHandleFromBytes(buffer->data(), entry_size);
176 EXPECT_TRUE(X509Certificate::IsSameOSCert(cached_cert_handle,
177 cert->os_cert_handle()));
178 X509Certificate::FreeOSCertHandle(cached_cert_handle);
179 }
180
181 } // namespace
182
183 // ----------------------------------------------------------------------------
184
185 // Tests that a certificate can be stored in the cache.
186 TEST(DiskBasedCertCache, SetCert) {
187 ScopedMockTransaction trans1(
188 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
189 MockDiskCache backend;
190 DiskBasedCertCache cache(&backend);
191 scoped_refptr<X509Certificate> cert(
192 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
193 ASSERT_TRUE(cert.get());
194 TestSetCallback set_callback;
195
196 cache.Set(cert->os_cert_handle(), set_callback.callback());
197 set_callback.WaitForResult();
198 EXPECT_EQ(kCert1.cache_key, set_callback.key());
199 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1));
200 }
201
202 // Tests that a certificate can be retrieved from the cache.
203 TEST(DiskBasedCertCache, GetCert) {
204 ScopedMockTransaction trans1(
205 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
206 MockDiskCache backend;
207 ASSERT_NO_FATAL_FAILURE(
208 ImportCert(&backend, kCert1, false /* not corrupted */));
209 DiskBasedCertCache cache(&backend);
210 TestGetCallback get_callback;
211
212 cache.Get(kCert1.cache_key, get_callback.callback());
213 get_callback.WaitForResult();
214
215 scoped_refptr<X509Certificate> cert(
216 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
217 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(),
218 cert->os_cert_handle()));
219 }
220
221 // Tests that the DiskBasedCertCache successfully writes to the cache
222 // if the cache acts synchronously
223 TEST(DiskBasedCertCache, SyncSet) {
224 ScopedMockTransaction trans1(
225 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_ALL));
226 MockDiskCache backend;
227 DiskBasedCertCache cache(&backend);
228 scoped_refptr<X509Certificate> cert(
229 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
230 ASSERT_TRUE(cert.get());
231
232 TestSetCallback set_callback;
233 cache.Set(cert->os_cert_handle(), set_callback.callback());
234 set_callback.WaitForResult();
235 EXPECT_EQ(kCert1.cache_key, set_callback.key());
236 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1));
237 }
238
239 // Tests that the DiskBasedCertCache successfully reads from the cache
240 // if the cache acts synchronously
241 TEST(DiskBasedCertCache, SyncGet) {
242 ScopedMockTransaction trans1(
243 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_ALL));
244 MockDiskCache backend;
245 ASSERT_NO_FATAL_FAILURE(
246 (ImportCert(&backend, kCert1, false /* not corrupted */)));
247 DiskBasedCertCache cache(&backend);
248 scoped_refptr<X509Certificate> cert(
249 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
250 ASSERT_TRUE(cert.get());
251
252 TestGetCallback get_callback;
253 cache.Get(kCert1.cache_key, get_callback.callback());
254 get_callback.WaitForResult();
255 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(),
256 cert->os_cert_handle()));
257 }
258
259 // Tests that Get will fail on a corrupted certificate.
260 TEST(DiskBasedCertCache, GetBrokenCert) {
261 ScopedMockTransaction trans1(
262 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
263 MockDiskCache backend;
264 ASSERT_NO_FATAL_FAILURE(ImportCert(&backend, kCert1, true /* corrupted */));
265 DiskBasedCertCache cache(&backend);
266 TestGetCallback get_callback;
267
268 cache.Get(kCert1.cache_key, get_callback.callback());
269 get_callback.WaitForResult();
270
271 scoped_refptr<X509Certificate> cert(
272 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
273 EXPECT_FALSE(get_callback.cert_handle());
274 }
275
276 // Tests that attempting to retrieve a cert that is not in the cache will
277 // return NULL.
278 TEST(DiskBasedCertCache, GetUncachedCert) {
279 ScopedMockTransaction trans1(
280 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
281 MockDiskCache backend;
282 DiskBasedCertCache cache(&backend);
283 TestGetCallback get_callback;
284
285 cache.Get(kCert1.cache_key, get_callback.callback());
286 get_callback.WaitForResult();
287 EXPECT_EQ(NULL, get_callback.cert_handle());
288 }
289
290 // Issues two requests to store a certificate in the cache
291 // (simultaneously), and checks that the DiskBasedCertCache stores the
292 // certificate to the cache (in one write rather than two).
293 TEST(DiskBasedCertCache, SetMultiple) {
294 ScopedMockTransaction trans1(
295 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
296 MockDiskCache backend;
297 DiskBasedCertCache cache(&backend);
298 scoped_refptr<X509Certificate> cert(
299 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
300 ASSERT_TRUE(cert.get());
301 TestSetCallback set_callback1, set_callback2;
302
303 // Behind the scenes, these two operations will be combined
304 // into one operation. IgnoreCallbacks guarantees that the
305 // first Set operation is not yet complete when the second Set is
306 // called, and then IgnoreCallbacks(false) continues the
307 // (combined) operation in the |cache|.
308 MockDiskEntry::IgnoreCallbacks(true);
309 cache.Set(cert->os_cert_handle(), set_callback1.callback());
310 cache.Set(cert->os_cert_handle(), set_callback2.callback());
311 MockDiskEntry::IgnoreCallbacks(false);
312
313 set_callback1.WaitForResult();
314 set_callback2.WaitForResult();
315 EXPECT_EQ(set_callback1.key(), set_callback2.key());
316 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1));
317 }
318
319 // Issues two requests to store a certificate in the cache
320 // because the first transaction finishes before the second
321 // one is issued, the first cache write is overwritten.
322 TEST(DiskBasedCertCache, SetOverwrite) {
323 ScopedMockTransaction trans1(
324 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
325 MockDiskCache backend;
326 backend.set_double_create_check(false);
327 DiskBasedCertCache cache(&backend);
328 scoped_refptr<X509Certificate> cert(
329 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
330 ASSERT_TRUE(cert.get());
331 TestSetCallback set_callback1, set_callback2;
332
333 cache.Set(cert->os_cert_handle(), set_callback1.callback());
334 set_callback1.WaitForResult();
335 cache.Set(cert->os_cert_handle(), set_callback2.callback());
336 set_callback2.WaitForResult();
337
338 EXPECT_EQ(set_callback1.key(), set_callback2.key());
339 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1));
340 }
341
342 // Stores a certificate in the DiskBasedCertCache, then retrieves it
343 // and makes sure it was retrieved successfully.
344 TEST(DiskBasedCertCache, SimpleSetAndGet) {
345 ScopedMockTransaction trans1(
346 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
347 MockDiskCache backend;
348 DiskBasedCertCache cache(&backend);
349 scoped_refptr<X509Certificate> cert(
350 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
351 ASSERT_TRUE(cert.get());
352 TestSetCallback set_callback;
353 TestGetCallback get_callback;
354
355 cache.Set(cert->os_cert_handle(), set_callback.callback());
356 set_callback.WaitForResult();
357 cache.Get(set_callback.key(), get_callback.callback());
358 get_callback.WaitForResult();
359 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(),
360 cert->os_cert_handle()));
361 }
362
363 // Tests some basic functionality of the DiskBasedCertCache, with multiple
364 // set and get operations.
365 TEST(DiskBasedCertCache, BasicUsage) {
366 ScopedMockTransaction trans1(
367 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START));
368 ScopedMockTransaction trans2(
369 CreateMockTransaction(kCert2.cache_key, TEST_MODE_NORMAL));
370 MockDiskCache backend;
371 DiskBasedCertCache cache(&backend);
372 scoped_refptr<X509Certificate> cert1(
373 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
374 scoped_refptr<X509Certificate> cert2(
375 ImportCertFromFile(GetTestCertsDirectory(), kCert2.file_name));
376 ASSERT_TRUE(cert1.get());
377 ASSERT_TRUE(cert2.get());
378 ASSERT_FALSE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(),
379 cert2->os_cert_handle()));
380 TestSetCallback set_callback1, set_callback2;
381
382 // Callbacks are temporarily ignored here to guarantee the asynchronous
383 // operations of the DiskBasedCertCache are always executed in the same
384 // order.
385 MockDiskEntry::IgnoreCallbacks(true);
386 cache.Set(cert1->os_cert_handle(), set_callback1.callback());
387 cache.Set(cert2->os_cert_handle(), set_callback2.callback());
388 MockDiskEntry::IgnoreCallbacks(false);
389 set_callback1.WaitForResult();
390 set_callback2.WaitForResult();
391
392 TestGetCallback get_callback1, get_callback2;
393
394 MockDiskEntry::IgnoreCallbacks(true);
395 cache.Get(set_callback1.key(), get_callback1.callback());
396 cache.Get(set_callback2.key(), get_callback2.callback());
397 MockDiskEntry::IgnoreCallbacks(false);
398 get_callback1.WaitForResult();
399 get_callback2.WaitForResult();
400
401 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(),
402 get_callback1.cert_handle()));
403 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert2->os_cert_handle(),
404 get_callback2.cert_handle()));
405 }
406
407 // Test the result of simultaneous requests to store and retrieve a
408 // certificate from the cache, with the get operation attempting to
409 // open the cache first and therefore failing to open the entry.
410 TEST(DiskBasedCertCache, SimultaneousGetSet) {
411 ScopedMockTransaction trans1(
412 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START));
413 MockDiskCache backend;
414 DiskBasedCertCache cache(&backend);
415 scoped_refptr<X509Certificate> cert(
416 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
417 ASSERT_TRUE(cert.get());
418
419 TestGetCallback get_callback;
420 TestSetCallback set_callback;
421
422 MockDiskEntry::IgnoreCallbacks(true);
423 cache.Get(kCert1.cache_key, get_callback.callback());
424 cache.Set(cert->os_cert_handle(), set_callback.callback());
425 MockDiskEntry::IgnoreCallbacks(false);
426 get_callback.WaitForResult();
427 set_callback.WaitForResult();
428
429 EXPECT_EQ(NULL, get_callback.cert_handle());
430 EXPECT_EQ(kCert1.cache_key, set_callback.key());
431 }
432
433 // Test the result of simultaneous requests to store and retrieve a
434 // certificate from the cache, with the get operation opening the cache
435 // after the set operation, leading to a successful read.
436 TEST(DiskBasedCertCache, SimultaneousSetGet) {
437 ScopedMockTransaction trans1(
438 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START));
439 MockDiskCache backend;
440 DiskBasedCertCache cache(&backend);
441 scoped_refptr<X509Certificate> cert(
442 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
443 ASSERT_TRUE(cert.get());
444
445 TestSetCallback set_callback;
446 TestGetCallback get_callback;
447
448 MockDiskEntry::IgnoreCallbacks(true);
449 cache.Set(cert->os_cert_handle(), set_callback.callback());
450 cache.Get(kCert1.cache_key, get_callback.callback());
451 MockDiskEntry::IgnoreCallbacks(false);
452 set_callback.WaitForResult();
453 get_callback.WaitForResult();
454
455 EXPECT_EQ(kCert1.cache_key, set_callback.key());
456 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert->os_cert_handle(),
457 get_callback.cert_handle()));
458 }
459
460 // Tests that the DiskBasedCertCache can be deleted without issues when
461 // there are pending operations in the disk cache.
462 TEST(DiskBasedCertCache, DeletedCertCache) {
463 ScopedMockTransaction trans1(
464 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
465 MockDiskCache backend;
466 scoped_ptr<DiskBasedCertCache> cache(new DiskBasedCertCache(&backend));
467 scoped_refptr<X509Certificate> cert(
468 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
469 ASSERT_TRUE(cert.get());
470 TestSetCallback set_callback;
471
472 cache->Set(cert->os_cert_handle(), set_callback.callback());
473 cache.reset();
474 set_callback.WaitForResult();
475 EXPECT_EQ(std::string(), set_callback.key());
476 }
477
478 } // namespace net
OLDNEW
« no previous file with comments | « net/http/disk_based_cert_cache.cc ('k') | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698