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

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

Powered by Google App Engine
This is Rietveld 408576698