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

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 v2 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() { 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 EXPECT_EQ(kCert1.cache_key, set_callback.key());
230 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1));
231 }
232
233 // Tests that the DiskBasedCertCache successfully reads from the cache
234 // if the cache acts synchronously
235 TEST(DiskBasedCertCache, SyncGet) {
236 ScopedMockTransaction trans1(
237 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_ALL));
238 MockDiskCache backend;
239 ASSERT_NO_FATAL_FAILURE(
240 (ImportCert(&backend, kCert1, false /* not corrupted */)));
241 DiskBasedCertCache cache(&backend);
242 scoped_refptr<X509Certificate> cert(
243 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
244 ASSERT_TRUE(cert.get());
245
246 TestGetCallback get_callback;
247 cache.Get(kCert1.cache_key, get_callback.callback());
wtc 2014/06/27 00:57:01 Hmm... I think we should still call WaitForResult(
248 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(),
249 cert->os_cert_handle());
250 }
251
252 // Tests that Get will fail on a corrupted certificate.
253 TEST(DiskBasedCertCache, GetBrokenCert) {
254 ScopedMockTransaction trans1(
255 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
256 MockDiskCache backend;
257 ASSERT_NO_FATAL_FAILURE(ImportCert(&backend, kCert1, true /* corrupted */));
258 DiskBasedCertCache cache(&backend);
259 TestGetCallback get_callback;
260
261 cache.Get(kCert1.cache_key, get_callback.callback());
262 get_callback.WaitForResult();
263
264 scoped_refptr<X509Certificate> cert(
265 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
266 EXPECT_FALSE(get_callback.cert_handle());
267 }
268
269 // Tests that attempting to retrieve a cert that is not in the cache will
270 // return NULL.
271 TEST(DiskBasedCertCache, GetUncachedCert) {
272 ScopedMockTransaction trans1(
273 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
274 MockDiskCache backend;
275 DiskBasedCertCache cache(&backend);
276 TestGetCallback get_callback;
277
278 cache.Get(kCert1.cache_key, get_callback.callback());
279 get_callback.WaitForResult();
280 EXPECT_EQ(NULL, get_callback.cert_handle());
281 }
282
283 // Issues two requests to store a certificate in the cache
284 // (simultaneously), and checks that the DiskBasedCertCache stores the
285 // certificate to the cache (in one write rather than two).
286 TEST(DiskBasedCertCache, SetMultiple) {
287 ScopedMockTransaction trans1(
288 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
289 MockDiskCache backend;
290 DiskBasedCertCache cache(&backend);
291 scoped_refptr<X509Certificate> cert(
292 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
293 ASSERT_TRUE(cert.get());
294 TestSetCallback set_callback1, set_callback2;
295
296 // Behind the scenes, these two operations will be combined
297 // into one operation. IgnoreCallbacks guarantees that the
298 // first Set operation is not yet complete when the second Set is
299 // called, and then IgnoreCallbacks(false) continues the
300 // (combined) operation in the |cache|.
301 MockDiskEntry::IgnoreCallbacks(true);
302 cache.Set(cert->os_cert_handle(), set_callback1.callback());
303 cache.Set(cert->os_cert_handle(), set_callback2.callback());
304 MockDiskEntry::IgnoreCallbacks(false);
305
306 set_callback1.WaitForResult();
307 set_callback2.WaitForResult();
308 EXPECT_EQ(set_callback1.key(), set_callback2.key());
309 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1));
310 }
311
312 // Issues two requests to store a certificate in the cache
313 // because the first transaction finishes before the second
314 // one is issued, the first cache write is overwritten.
315 TEST(DiskBasedCertCache, SetOverwrite) {
316 ScopedMockTransaction trans1(
317 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
318 MockDiskCache backend;
319 backend.set_double_create_check(false);
320 DiskBasedCertCache cache(&backend);
321 scoped_refptr<X509Certificate> cert(
322 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
323 ASSERT_TRUE(cert.get());
324 TestSetCallback set_callback1, set_callback2;
325
326 cache.Set(cert->os_cert_handle(), set_callback1.callback());
327 set_callback1.WaitForResult();
328 cache.Set(cert->os_cert_handle(), set_callback2.callback());
329 set_callback2.WaitForResult();
330
331 EXPECT_EQ(set_callback1.key(), set_callback2.key());
332 ASSERT_NO_FATAL_FAILURE(CheckCertCached(&backend, kCert1));
333 }
334
335 // Stores a certificate in the DiskBasedCertCache, then retrieves it
336 // and makes sure it was retrieved successfully.
337 TEST(DiskBasedCertCache, SimpleSetAndGet) {
338 ScopedMockTransaction trans1(
339 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
340 MockDiskCache backend;
341 DiskBasedCertCache cache(&backend);
342 scoped_refptr<X509Certificate> cert(
343 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
344 ASSERT_TRUE(cert.get());
345 TestSetCallback set_callback;
346 TestGetCallback get_callback;
347
348 cache.Set(cert->os_cert_handle(), set_callback.callback());
349 set_callback.WaitForResult();
350 cache.Get(set_callback.key(), get_callback.callback());
351 get_callback.WaitForResult();
352 EXPECT_TRUE(X509Certificate::IsSameOSCert(get_callback.cert_handle(),
353 cert->os_cert_handle()));
354 }
355
356 // Tests some basic functionality of the DiskBasedCertCache, with multiple
357 // set and get operations.
358 TEST(DiskBasedCertCache, BasicUsage) {
359 ScopedMockTransaction trans1(
360 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START));
361 ScopedMockTransaction trans2(
362 CreateMockTransaction(kCert2.cache_key, TEST_MODE_NORMAL));
363 MockDiskCache backend;
364 DiskBasedCertCache cache(&backend);
365 scoped_refptr<X509Certificate> cert1(
366 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
367 scoped_refptr<X509Certificate> cert2(
368 ImportCertFromFile(GetTestCertsDirectory(), kCert2.file_name));
369 ASSERT_TRUE(cert1.get());
370 ASSERT_TRUE(cert2.get());
371 ASSERT_FALSE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(),
372 cert2->os_cert_handle()));
373 TestSetCallback set_callback1, set_callback2;
374
375 // Callbacks are temporarily ignored here to guarantee the asynchronous
376 // operations of the DiskBasedCertCache are always executed in the same
377 // order.
378 MockDiskEntry::IgnoreCallbacks(true);
379 cache.Set(cert1->os_cert_handle(), set_callback1.callback());
380 cache.Set(cert2->os_cert_handle(), set_callback2.callback());
381 MockDiskEntry::IgnoreCallbacks(false);
382 set_callback1.WaitForResult();
383 set_callback2.WaitForResult();
384
385 TestGetCallback get_callback1, get_callback2;
386
387 MockDiskEntry::IgnoreCallbacks(true);
388 cache.Get(set_callback1.key(), get_callback1.callback());
389 cache.Get(set_callback2.key(), get_callback2.callback());
390 MockDiskEntry::IgnoreCallbacks(false);
391 get_callback1.WaitForResult();
392 get_callback2.WaitForResult();
393
394 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert1->os_cert_handle(),
395 get_callback1.cert_handle()));
396 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert2->os_cert_handle(),
397 get_callback2.cert_handle()));
398 }
399
400 // Test the result of simultaneous requests to store and retrieve a
401 // certificate from the cache, with the get operation attempting to
402 // open the cache first and therefore failing to open the entry.
403 TEST(DiskBasedCertCache, SimultaneousGetSet) {
404 ScopedMockTransaction trans1(
405 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START));
406 MockDiskCache backend;
407 DiskBasedCertCache cache(&backend);
408 scoped_refptr<X509Certificate> cert(
409 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
410 ASSERT_TRUE(cert.get());
411
412 TestGetCallback get_callback;
413 TestSetCallback set_callback;
414
415 MockDiskEntry::IgnoreCallbacks(true);
416 cache.Get(kCert1.cache_key, get_callback.callback());
417 cache.Set(cert->os_cert_handle(), set_callback.callback());
418 MockDiskEntry::IgnoreCallbacks(false);
419 get_callback.WaitForResult();
420 set_callback.WaitForResult();
421
422 EXPECT_EQ(NULL, get_callback.cert_handle());
423 EXPECT_EQ(kCert1.cache_key, set_callback.key());
424 }
425
426 // Test the result of simultaneous requests to store and retrieve a
427 // certificate from the cache, with the get operation opening the cache
428 // after the set operation, leading to a successful read.
429 TEST(DiskBasedCertCache, SimultaneousSetGet) {
430 ScopedMockTransaction trans1(
431 CreateMockTransaction(kCert1.cache_key, TEST_MODE_SYNC_CACHE_START));
432 MockDiskCache backend;
433 DiskBasedCertCache cache(&backend);
434 scoped_refptr<X509Certificate> cert(
435 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
436 ASSERT_TRUE(cert.get());
437
438 TestSetCallback set_callback;
439 TestGetCallback get_callback;
440
441 MockDiskEntry::IgnoreCallbacks(true);
442 cache.Set(cert->os_cert_handle(), set_callback.callback());
443 cache.Get(kCert1.cache_key, get_callback.callback());
444 MockDiskEntry::IgnoreCallbacks(false);
445 set_callback.WaitForResult();
446 get_callback.WaitForResult();
447
448 EXPECT_EQ(kCert1.cache_key, set_callback.key());
449 EXPECT_TRUE(X509Certificate::IsSameOSCert(cert->os_cert_handle(),
450 get_callback.cert_handle()));
451 }
452
453 // Tests that the DiskBasedCertCache can be deleted without issues when
454 // there are pending operations in the disk cache.
455 TEST(DiskBasedCertCache, DeletedCertCache) {
456 ScopedMockTransaction trans1(
457 CreateMockTransaction(kCert1.cache_key, TEST_MODE_NORMAL));
458 MockDiskCache backend;
459 scoped_ptr<DiskBasedCertCache> cache(new DiskBasedCertCache(&backend));
460 scoped_refptr<X509Certificate> cert(
461 ImportCertFromFile(GetTestCertsDirectory(), kCert1.file_name));
462 ASSERT_TRUE(cert.get());
463 TestSetCallback set_callback;
464
465 cache->Set(cert->os_cert_handle(), set_callback.callback());
466 cache.reset();
467 set_callback.WaitForResult();
468 EXPECT_EQ(std::string(), set_callback.key());
469 }
470
471 } // 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