Chromium Code Reviews| 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/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" | |
|
wtc
2014/06/21 00:43:13
Add a blank line after the last #include statement
| |
| 17 namespace net { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // MockTransactions are required to use the blocking Mock Disk Cache Backend. | |
|
wtc
2014/06/21 00:43:13
Nit: Mock Disk Cache Backend => MockDiskCache Back
| |
| 22 | |
| 23 // This transaction corresponds to "root_ca_cert.pem" in GetTestCertsDirectory() | |
|
wtc
2014/06/21 00:43:13
Nit: add a period at the end of the sentence. Comm
| |
| 24 const MockTransaction kCertTransaction1 = { | |
| 25 "cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4", "", base::Time(), | |
| 26 "", LOAD_NORMAL, "", | |
| 27 "", base::Time(), "", | |
| 28 TEST_MODE_NORMAL, NULL, 0}; | |
|
wtc
2014/06/21 00:43:13
Nit: our Style Guide does not require aligning the
brandonsalmon
2014/06/21 02:21:32
In this case I also think this is ugly, however ac
wtc
2014/06/24 00:21:04
If git cl format formatted the code this way, then
| |
| 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 | |
|
wtc
2014/06/21 00:43:13
Nit: "achieved" seems wrong. Did you mean "receive
| |
| 39 // using CompletionCallback::WaitForResult. | |
|
wtc
2014/06/21 00:43:13
Nit: this fits on the previous line.
| |
| 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, | |
|
wtc
2014/06/21 00:43:13
Nit: this 'const' isn't necessary. (X509Certificat
| |
| 48 std::string& key, | |
|
wtc
2014/06/21 00:43:13
IMPORTANT: our Style Guide requires using pointers
| |
| 49 CompletionCallback cb) { | |
|
wtc
2014/06/21 00:43:13
Nit: our Style Guide recommends against using unco
| |
| 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, | |
|
wtc
2014/06/21 00:43:13
This should be
const std::string& key,
Our Sty
| |
| 57 X509Certificate::OSCertHandle& cert_handle, | |
| 58 CompletionCallback cb) { | |
|
wtc
2014/06/21 00:43:13
This probably should also be a const reference.
| |
| 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 void DeleteCertCache() { cert_cache_.reset(); } | |
| 81 | |
| 82 private: | |
| 83 scoped_ptr<disk_cache::Backend> backend; | |
| 84 scoped_ptr<DiskBasedCertCache> cert_cache_; | |
| 85 base::WeakPtrFactory<MockCertCache> weak_factory_; | |
| 86 }; | |
| 87 | |
| 88 // ---------------------------------------------------------------------------- | |
|
wtc
2014/06/21 00:43:13
Move this horizontal separator line after the "} /
| |
| 89 | |
| 90 } // namespace | |
| 91 | |
| 92 // Tests that a certificate can be stored in the cache. | |
| 93 TEST(DiskBasedCertCache, SetCert) { | |
| 94 AddMockTransaction(&kCertTransaction1); | |
| 95 MockCertCache user; | |
| 96 | |
| 97 scoped_refptr<X509Certificate> cert( | |
| 98 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem")); | |
| 99 | |
| 100 EXPECT_TRUE(cert.get()); | |
| 101 | |
| 102 TestCompletionCallback set_callback; | |
| 103 | |
| 104 std::string key; | |
| 105 | |
| 106 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback()); | |
| 107 set_callback.WaitForResult(); | |
| 108 | |
| 109 ASSERT_TRUE(!key.empty()); | |
| 110 } | |
| 111 | |
| 112 // Tests that attempting to retrieve a cert that is not in the cache will | |
| 113 // return NULL. | |
| 114 TEST(DiskBasedCertCache, GetUncachedCert) { | |
| 115 AddMockTransaction(&kCertTransaction1); | |
| 116 MockCertCache user; | |
| 117 | |
| 118 TestCompletionCallback get_callback; | |
| 119 | |
| 120 X509Certificate::OSCertHandle cert_handle = NULL; | |
| 121 user.Get("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4", | |
| 122 cert_handle, | |
| 123 get_callback.callback()); | |
| 124 get_callback.WaitForResult(); | |
| 125 | |
| 126 ASSERT_EQ(NULL, cert_handle); | |
| 127 } | |
| 128 | |
| 129 // Tests that the same certificate can be requested to be stored from multiple | |
| 130 // locations simultaneously. | |
| 131 TEST(DiskBasedCertCache, SetMultiple) { | |
| 132 AddMockTransaction(&kCertTransaction1); | |
| 133 MockCertCache user; | |
| 134 | |
| 135 scoped_refptr<X509Certificate> cert( | |
| 136 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem")); | |
| 137 | |
| 138 TestCompletionCallback set_callback1; | |
| 139 TestCompletionCallback set_callback2; | |
| 140 | |
| 141 std::string key; | |
| 142 | |
| 143 user.Set(cert.get()->os_cert_handle(), key, set_callback1.callback()); | |
| 144 user.Set(cert.get()->os_cert_handle(), key, set_callback2.callback()); | |
| 145 set_callback1.WaitForResult(); | |
| 146 set_callback2.WaitForResult(); | |
| 147 } | |
| 148 | |
| 149 // Stores a certificate in DiskBasedCertCache, then retrieves it | |
| 150 // and makes sure it was retrieved successfully. | |
| 151 TEST(DiskBasedCertCache, SimpleSetAndGet) { | |
| 152 AddMockTransaction(&kCertTransaction1); | |
| 153 MockCertCache user; | |
| 154 | |
| 155 scoped_refptr<X509Certificate> cert( | |
| 156 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem")); | |
| 157 | |
| 158 EXPECT_TRUE(cert.get()); | |
| 159 | |
| 160 TestCompletionCallback set_callback; | |
| 161 TestCompletionCallback get_callback; | |
| 162 | |
| 163 std::string key; | |
| 164 X509Certificate::OSCertHandle retrieved_cert_handle = NULL; | |
| 165 | |
| 166 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback()); | |
| 167 set_callback.WaitForResult(); | |
| 168 | |
| 169 user.Get(key, retrieved_cert_handle, get_callback.callback()); | |
| 170 get_callback.WaitForResult(); | |
| 171 | |
| 172 ASSERT_TRUE(X509Certificate::IsSameOSCert(retrieved_cert_handle, | |
| 173 cert.get()->os_cert_handle())); | |
| 174 } | |
| 175 | |
| 176 // Test some basic usage with multiple certificates being stored and retrieved | |
| 177 // at the same time. | |
| 178 TEST(DiskBasedCertCache, BasicUsage) { | |
| 179 AddMockTransaction(&kCertTransaction1); | |
| 180 AddMockTransaction(&kCertTransaction2); | |
| 181 | |
| 182 MockCertCache user; | |
| 183 | |
| 184 scoped_refptr<X509Certificate> cert1( | |
| 185 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem")); | |
| 186 | |
| 187 scoped_refptr<X509Certificate> cert2( | |
| 188 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem")); | |
| 189 | |
| 190 EXPECT_TRUE(cert1.get()); | |
| 191 EXPECT_TRUE(cert2.get()); | |
| 192 EXPECT_TRUE(!X509Certificate::IsSameOSCert(cert1->os_cert_handle(), | |
| 193 cert2->os_cert_handle())); | |
| 194 | |
| 195 TestCompletionCallback set_callback1, set_callback2; | |
| 196 std::string key1, key2; | |
| 197 | |
| 198 user.Set(cert1.get()->os_cert_handle(), key1, set_callback1.callback()); | |
| 199 user.Set(cert2.get()->os_cert_handle(), key2, set_callback2.callback()); | |
| 200 set_callback1.WaitForResult(); | |
| 201 set_callback2.WaitForResult(); | |
| 202 | |
| 203 TestCompletionCallback get_callback1, get_callback2; | |
| 204 X509Certificate::OSCertHandle cert_handle1, cert_handle2; | |
| 205 | |
| 206 user.Get(key2, cert_handle2, get_callback2.callback()); | |
| 207 user.Get(key1, cert_handle1, get_callback1.callback()); | |
| 208 get_callback2.WaitForResult(); | |
| 209 get_callback1.WaitForResult(); | |
| 210 | |
| 211 ASSERT_TRUE( | |
| 212 X509Certificate::IsSameOSCert(cert1->os_cert_handle(), cert_handle1)); | |
| 213 ASSERT_TRUE( | |
| 214 X509Certificate::IsSameOSCert(cert2->os_cert_handle(), cert_handle2)); | |
| 215 } | |
| 216 | |
| 217 // Tests result of if a certificate is simultaneously asked to be stored and | |
| 218 // retrieved | |
| 219 // from the cache. | |
| 220 // TODO(brandonsalmon): improve the functionality of this circumstance. | |
| 221 TEST(DiskBasedCertCache, SimultaneousSetGet) { | |
| 222 AddMockTransaction(&kCertTransaction1); | |
| 223 | |
| 224 MockCertCache user; | |
| 225 | |
| 226 scoped_refptr<X509Certificate> cert( | |
| 227 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem")); | |
| 228 | |
| 229 TestCompletionCallback set_callback, get_callback; | |
| 230 X509Certificate::OSCertHandle cert_handle; | |
| 231 std::string key("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4"); | |
| 232 | |
| 233 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback()); | |
| 234 user.Get(key, cert_handle, get_callback.callback()); | |
| 235 get_callback.WaitForResult(); | |
| 236 set_callback.WaitForResult(); | |
| 237 } | |
| 238 | |
| 239 // Tests whether or not an operation will be correctly canceled in the | |
| 240 // circumstance of the DiskBasedCertCache being deleted early. | |
| 241 TEST(DiskBasedCertCache, DeletedCertCache) { | |
| 242 AddMockTransaction(&kCertTransaction1); | |
| 243 | |
| 244 MockCertCache user; | |
| 245 | |
| 246 std::string key; | |
| 247 scoped_refptr<X509Certificate> cert( | |
| 248 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem")); | |
| 249 | |
| 250 TestCompletionCallback set_callback; | |
| 251 user.Set(cert.get()->os_cert_handle(), key, set_callback.callback()); | |
| 252 | |
| 253 user.DeleteCertCache(); | |
| 254 set_callback.WaitForResult(); | |
| 255 ASSERT_EQ(key, std::string()); | |
| 256 } | |
| 257 | |
| 258 } // namespace net | |
| OLD | NEW |