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