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

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 isses stated in review of patch 8. Created 6 years, 6 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/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/23 18:38:40 Nit: add a blank line after the header #include st
17 namespace net {
18
19 namespace {
20
21 // MockTransactions are required to use the MockDiskCache backend.
22
23 // This transaction corresponds to "root_ca_cert.pem" in
24 // GetTestCertsDirectory().
25 const MockTransaction kCertTransaction1 = {
26 "cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4",
27 "",
28 base::Time(),
29 "",
30 LOAD_NORMAL,
31 "",
32 "",
33 base::Time(),
34 "",
35 TEST_MODE_NORMAL,
36 NULL,
37 0
wtc 2014/06/23 18:38:40 I suggest adding an initializer for the final fiel
38 };
39
40 // This transaction corresponds to "ok_cert.pem" in GetTestCertsDirectory().
41 const MockTransaction kCertTransaction2 = {
42 "cert:9174C7CB9E4919604E7B1BFC430E4929DA45F65F",
43 "",
44 base::Time(),
45 "",
46 LOAD_NORMAL,
47 "",
48 "",
49 base::Time(),
50 "",
51 TEST_MODE_NORMAL,
52 NULL,
53 0
54 };
55
56 // MockCertCache is used so that results from the DiskBasedCertCache can be
57 // recieved using CompletionCallback::WaitForResult.
wtc 2014/06/23 18:38:40 Nit: recieved => received
58 class MockCertCache {
59 public:
60 MockCertCache()
61 : backend(new MockDiskCache()),
62 cert_cache_(new DiskBasedCertCache(backend.get())),
63 weak_factory_(this) {}
64
65 void Set(X509Certificate::OSCertHandle cert_handle,
66 std::string* key,
67 const CompletionCallback& callback) {
68 cert_cache_->Set(cert_handle,
69 base::Bind(&MockCertCache::FinishSet,
wtc 2014/06/23 18:38:40 Nit: our naming convention for this kind of functi
70 weak_factory_.GetWeakPtr(),
71 key,
72 callback));
73 }
74
75 void Get(const std::string& key,
76 X509Certificate::OSCertHandle* cert_handle,
77 const CompletionCallback& callback) {
78 cert_cache_->Get(key,
79 base::Bind(&MockCertCache::FinishGet,
80 weak_factory_.GetWeakPtr(),
81 cert_handle,
82 callback));
83 }
84
85 void FinishSet(std::string* key_return,
86 CompletionCallback callback,
87 const std::string& key_received) {
88 *key_return = key_received;
89 base::ResetAndReturn(&callback).Run(OK);
wtc 2014/06/23 18:38:40 It's not necessary to use ResetAndReturn. You can
90 }
91
92 void FinishGet(X509Certificate::OSCertHandle* handle_return,
93 CompletionCallback callback,
94 const X509Certificate::OSCertHandle handle_retrieved) {
wtc 2014/06/23 18:38:40 Be consistent in using "xxx_received" or "xxx_retr
95 *handle_return = handle_retrieved;
96 base::ResetAndReturn(&callback).Run(OK);
97 }
98
99 void DeleteCertCache() { cert_cache_.reset(); }
100
101 private:
102 scoped_ptr<disk_cache::Backend> backend;
103 scoped_ptr<DiskBasedCertCache> cert_cache_;
104 base::WeakPtrFactory<MockCertCache> weak_factory_;
105 };
106
107 } // namespace
108
109 // ----------------------------------------------------------------------------
110
111 // Tests that a certificate can be stored in the cache.
112 TEST(DiskBasedCertCache, SetCert) {
113 AddMockTransaction(&kCertTransaction1);
114 MockCertCache user;
115
116 scoped_refptr<X509Certificate> cert(
117 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
118
119 EXPECT_TRUE(cert.get());
wtc 2014/06/23 18:38:40 Use ASSERT_TRUE instead of EXPECT_TRUE here, becau
120
121 TestCompletionCallback set_callback;
122
123 std::string key;
124
125 user.Set(cert.get()->os_cert_handle(), &key, set_callback.callback());
126 set_callback.WaitForResult();
wtc 2014/06/23 18:38:40 You should check that this returns OK.
127
128 ASSERT_TRUE(!key.empty());
wtc 2014/06/23 18:38:40 You should check with EXPECT_EQ that |key| has the
129 }
130
131 // Tests that attempting to retrieve a cert that is not in the cache will
132 // return NULL.
133 TEST(DiskBasedCertCache, GetUncachedCert) {
134 AddMockTransaction(&kCertTransaction1);
135 MockCertCache user;
136
137 TestCompletionCallback get_callback;
138
139 X509Certificate::OSCertHandle cert_handle = NULL;
140 user.Get("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4",
141 &cert_handle,
142 get_callback.callback());
143 get_callback.WaitForResult();
wtc 2014/06/23 18:38:40 You should check that this returns OK.
144
145 ASSERT_EQ(NULL, cert_handle);
wtc 2014/06/23 18:38:40 Nit: you can use EXPECT_EQ here.
146 }
147
148 // Tests that the same certificate can be requested to be stored from multiple
149 // locations simultaneously.
150 TEST(DiskBasedCertCache, SetMultiple) {
151 AddMockTransaction(&kCertTransaction1);
152 MockCertCache user;
153
154 scoped_refptr<X509Certificate> cert(
155 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
156
157 TestCompletionCallback set_callback1;
158 TestCompletionCallback set_callback2;
159
160 std::string key;
wtc 2014/06/23 18:38:40 Use |key1| and |key2|, and pass &key1 and key2 to
161
162 user.Set(cert.get()->os_cert_handle(), &key, set_callback1.callback());
163 user.Set(cert.get()->os_cert_handle(), &key, set_callback2.callback());
164 set_callback1.WaitForResult();
165 set_callback2.WaitForResult();
166 }
167
168 // Stores a certificate in DiskBasedCertCache, then retrieves it
169 // and makes sure it was retrieved successfully.
170 TEST(DiskBasedCertCache, SimpleSetAndGet) {
171 AddMockTransaction(&kCertTransaction1);
172 MockCertCache user;
173
174 scoped_refptr<X509Certificate> cert(
175 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
176
177 EXPECT_TRUE(cert.get());
178
179 TestCompletionCallback set_callback;
180 TestCompletionCallback get_callback;
181
182 std::string key;
183 X509Certificate::OSCertHandle retrieved_cert_handle = NULL;
184
185 user.Set(cert.get()->os_cert_handle(), &key, set_callback.callback());
186 set_callback.WaitForResult();
187
188 user.Get(key, &retrieved_cert_handle, get_callback.callback());
189 get_callback.WaitForResult();
190
191 ASSERT_TRUE(X509Certificate::IsSameOSCert(retrieved_cert_handle,
192 cert.get()->os_cert_handle()));
193 }
194
195 // Test some basic usage with multiple certificates being stored and retrieved
196 // at the same time.
197 TEST(DiskBasedCertCache, BasicUsage) {
198 AddMockTransaction(&kCertTransaction1);
199 AddMockTransaction(&kCertTransaction2);
200
201 MockCertCache user;
202
203 scoped_refptr<X509Certificate> cert1(
204 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
205
206 scoped_refptr<X509Certificate> cert2(
207 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"));
208
209 EXPECT_TRUE(cert1.get());
210 EXPECT_TRUE(cert2.get());
211 EXPECT_TRUE(!X509Certificate::IsSameOSCert(cert1->os_cert_handle(),
wtc 2014/06/23 18:38:40 If there is EXPECT_FALSE, use it.
212 cert2->os_cert_handle()));
213
214 TestCompletionCallback set_callback1, set_callback2;
215 std::string key1, key2;
216
217 user.Set(cert1.get()->os_cert_handle(), &key1, set_callback1.callback());
218 user.Set(cert2.get()->os_cert_handle(), &key2, set_callback2.callback());
219 set_callback1.WaitForResult();
220 set_callback2.WaitForResult();
221
222 TestCompletionCallback get_callback1, get_callback2;
223 X509Certificate::OSCertHandle cert_handle1, cert_handle2;
224
225 user.Get(key2, &cert_handle2, get_callback2.callback());
226 user.Get(key1, &cert_handle1, get_callback1.callback());
227 get_callback2.WaitForResult();
228 get_callback1.WaitForResult();
229
230 ASSERT_TRUE(
231 X509Certificate::IsSameOSCert(cert1->os_cert_handle(), cert_handle1));
232 ASSERT_TRUE(
wtc 2014/06/23 18:38:40 Use EXPECT_TRUE.
233 X509Certificate::IsSameOSCert(cert2->os_cert_handle(), cert_handle2));
234 }
235
236 // Tests result of if a certificate is simultaneously asked to be stored and
wtc 2014/06/23 18:38:40 Nit: the first few words of this comment don't rea
237 // retrieved
238 // from the cache.
wtc 2014/06/23 18:38:40 Nit: move to the previous line.
239 // TODO(brandonsalmon): improve the functionality of this circumstance.
240 TEST(DiskBasedCertCache, SimultaneousSetGet) {
241 AddMockTransaction(&kCertTransaction1);
242
243 MockCertCache user;
244
245 scoped_refptr<X509Certificate> cert(
246 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
247
248 TestCompletionCallback set_callback, get_callback;
249 X509Certificate::OSCertHandle cert_handle;
250 std::string key("cert:4C005EF1CF45F80D4A5A2BCFB00D4F198121E8D4");
251
252 user.Set(cert.get()->os_cert_handle(), &key, set_callback.callback());
253 user.Get(key, &cert_handle, get_callback.callback());
254 get_callback.WaitForResult();
255 set_callback.WaitForResult();
wtc 2014/06/23 18:38:39 What is the expected result?
256 }
257
258 // Tests whether or not an operation will be correctly canceled in the
259 // circumstance of the DiskBasedCertCache being deleted early.
260 TEST(DiskBasedCertCache, DeletedCertCache) {
261 AddMockTransaction(&kCertTransaction1);
262
263 MockCertCache user;
264
265 std::string key;
266 scoped_refptr<X509Certificate> cert(
267 ImportCertFromFile(GetTestCertsDirectory(), "root_ca_cert.pem"));
268
269 TestCompletionCallback set_callback;
270 user.Set(cert.get()->os_cert_handle(), &key, set_callback.callback());
271
272 user.DeleteCertCache();
273 set_callback.WaitForResult();
274 ASSERT_EQ(key, std::string());
wtc 2014/06/23 18:38:40 1. We should verify that the expected result of |k
275 }
276
277 } // 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