OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/ssl/default_channel_id_store.h" | 5 #include "net/ssl/default_channel_id_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "crypto/ec_private_key.h" |
16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 void CallCounter(int* counter) { | 24 void CallCounter(int* counter) { |
24 (*counter)++; | 25 (*counter)++; |
25 } | 26 } |
26 | 27 |
27 void GetChannelIDCallbackNotCalled(int err, | 28 void GetChannelIDCallbackNotCalled( |
28 const std::string& server_identifier, | 29 int err, |
29 base::Time expiration_time, | 30 const std::string& server_identifier, |
30 const std::string& private_key_result, | 31 scoped_ptr<crypto::ECPrivateKey> key_result) { |
31 const std::string& cert_result) { | |
32 ADD_FAILURE() << "Unexpected callback execution."; | 32 ADD_FAILURE() << "Unexpected callback execution."; |
33 } | 33 } |
34 | 34 |
35 class AsyncGetChannelIDHelper { | 35 class AsyncGetChannelIDHelper { |
36 public: | 36 public: |
37 AsyncGetChannelIDHelper() : called_(false) {} | 37 AsyncGetChannelIDHelper() : called_(false) {} |
38 | 38 |
39 void Callback(int err, | 39 void Callback(int err, |
40 const std::string& server_identifier, | 40 const std::string& server_identifier, |
41 base::Time expiration_time, | 41 scoped_ptr<crypto::ECPrivateKey> key_result) { |
42 const std::string& private_key_result, | |
43 const std::string& cert_result) { | |
44 err_ = err; | 42 err_ = err; |
45 server_identifier_ = server_identifier; | 43 server_identifier_ = server_identifier; |
46 expiration_time_ = expiration_time; | 44 key_ = key_result.Pass(); |
47 private_key_ = private_key_result; | |
48 cert_ = cert_result; | |
49 called_ = true; | 45 called_ = true; |
50 } | 46 } |
51 | 47 |
52 int err_; | 48 int err_; |
53 std::string server_identifier_; | 49 std::string server_identifier_; |
54 base::Time expiration_time_; | 50 scoped_ptr<crypto::ECPrivateKey> key_; |
55 std::string private_key_; | |
56 std::string cert_; | |
57 bool called_; | 51 bool called_; |
58 }; | 52 }; |
59 | 53 |
60 void GetAllCallback( | 54 void GetAllCallback( |
61 ChannelIDStore::ChannelIDList* dest, | 55 ChannelIDStore::ChannelIDList* dest, |
62 const ChannelIDStore::ChannelIDList& result) { | 56 const ChannelIDStore::ChannelIDList& result) { |
63 *dest = result; | 57 *dest = result; |
64 } | 58 } |
65 | 59 |
66 class MockPersistentStore | 60 class MockPersistentStore |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 103 |
110 void MockPersistentStore::DeleteChannelID( | 104 void MockPersistentStore::DeleteChannelID( |
111 const DefaultChannelIDStore::ChannelID& channel_id) { | 105 const DefaultChannelIDStore::ChannelID& channel_id) { |
112 channel_ids_.erase(channel_id.server_identifier()); | 106 channel_ids_.erase(channel_id.server_identifier()); |
113 } | 107 } |
114 | 108 |
115 void MockPersistentStore::SetForceKeepSessionState() {} | 109 void MockPersistentStore::SetForceKeepSessionState() {} |
116 | 110 |
117 MockPersistentStore::~MockPersistentStore() {} | 111 MockPersistentStore::~MockPersistentStore() {} |
118 | 112 |
| 113 void ExpectKeysEqual(crypto::ECPrivateKey* key1, crypto::ECPrivateKey* key2) { |
| 114 ASSERT_TRUE(key1); |
| 115 ASSERT_TRUE(key2); |
| 116 std::string public_key1, public_key2; |
| 117 EXPECT_TRUE(key1->ExportRawPublicKey(&public_key1)); |
| 118 EXPECT_TRUE(key2->ExportRawPublicKey(&public_key2)); |
| 119 EXPECT_EQ(public_key1, public_key2); |
| 120 } |
| 121 |
119 } // namespace | 122 } // namespace |
120 | 123 |
121 TEST(DefaultChannelIDStoreTest, TestLoading) { | 124 TEST(DefaultChannelIDStoreTest, TestLoading) { |
122 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 125 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
123 | 126 |
124 persistent_store->AddChannelID( | 127 persistent_store->AddChannelID(DefaultChannelIDStore::ChannelID( |
125 DefaultChannelIDStore::ChannelID( | 128 "google.com", base::Time(), crypto::ECPrivateKey::Create())); |
126 "google.com", | 129 persistent_store->AddChannelID(DefaultChannelIDStore::ChannelID( |
127 base::Time(), | 130 "verisign.com", base::Time(), crypto::ECPrivateKey::Create())); |
128 base::Time(), | |
129 "a", "b")); | |
130 persistent_store->AddChannelID( | |
131 DefaultChannelIDStore::ChannelID( | |
132 "verisign.com", | |
133 base::Time(), | |
134 base::Time(), | |
135 "c", "d")); | |
136 | 131 |
137 // Make sure channel_ids load properly. | 132 // Make sure channel_ids load properly. |
138 DefaultChannelIDStore store(persistent_store.get()); | 133 DefaultChannelIDStore store(persistent_store.get()); |
139 // Load has not occurred yet. | 134 // Load has not occurred yet. |
140 EXPECT_EQ(0, store.GetChannelIDCount()); | 135 EXPECT_EQ(0, store.GetChannelIDCount()); |
141 store.SetChannelID( | 136 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
142 "verisign.com", | 137 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
143 base::Time(), | |
144 base::Time(), | |
145 "e", "f"); | |
146 // Wait for load & queued set task. | 138 // Wait for load & queued set task. |
147 base::MessageLoop::current()->RunUntilIdle(); | 139 base::MessageLoop::current()->RunUntilIdle(); |
148 EXPECT_EQ(2, store.GetChannelIDCount()); | 140 EXPECT_EQ(2, store.GetChannelIDCount()); |
149 store.SetChannelID( | 141 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
150 "twitter.com", | 142 "twitter.com", base::Time(), crypto::ECPrivateKey::Create()))); |
151 base::Time(), | |
152 base::Time(), | |
153 "g", "h"); | |
154 // Set should be synchronous now that load is done. | 143 // Set should be synchronous now that load is done. |
155 EXPECT_EQ(3, store.GetChannelIDCount()); | 144 EXPECT_EQ(3, store.GetChannelIDCount()); |
156 } | 145 } |
157 | 146 |
158 //TODO(mattm): add more tests of without a persistent store? | 147 //TODO(mattm): add more tests of without a persistent store? |
159 TEST(DefaultChannelIDStoreTest, TestSettingAndGetting) { | 148 TEST(DefaultChannelIDStoreTest, TestSettingAndGetting) { |
160 // No persistent store, all calls will be synchronous. | 149 // No persistent store, all calls will be synchronous. |
161 DefaultChannelIDStore store(NULL); | 150 DefaultChannelIDStore store(NULL); |
162 base::Time expiration_time; | 151 crypto::ECPrivateKey* expected_key = crypto::ECPrivateKey::Create(); |
163 std::string private_key, cert; | 152 |
| 153 scoped_ptr<crypto::ECPrivateKey> key; |
164 EXPECT_EQ(0, store.GetChannelIDCount()); | 154 EXPECT_EQ(0, store.GetChannelIDCount()); |
165 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 155 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
166 store.GetChannelID("verisign.com", | 156 store.GetChannelID("verisign.com", &key, |
167 &expiration_time, | |
168 &private_key, | |
169 &cert, | |
170 base::Bind(&GetChannelIDCallbackNotCalled))); | 157 base::Bind(&GetChannelIDCallbackNotCalled))); |
171 EXPECT_TRUE(private_key.empty()); | 158 EXPECT_FALSE(key); |
172 EXPECT_TRUE(cert.empty()); | 159 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
173 store.SetChannelID( | 160 "verisign.com", base::Time::FromInternalValue(123), expected_key))); |
174 "verisign.com", | 161 EXPECT_EQ(OK, store.GetChannelID("verisign.com", &key, |
175 base::Time::FromInternalValue(123), | 162 base::Bind(&GetChannelIDCallbackNotCalled))); |
176 base::Time::FromInternalValue(456), | 163 ExpectKeysEqual(expected_key, key.get()); |
177 "i", "j"); | |
178 EXPECT_EQ(OK, | |
179 store.GetChannelID("verisign.com", | |
180 &expiration_time, | |
181 &private_key, | |
182 &cert, | |
183 base::Bind(&GetChannelIDCallbackNotCalled))); | |
184 EXPECT_EQ(456, expiration_time.ToInternalValue()); | |
185 EXPECT_EQ("i", private_key); | |
186 EXPECT_EQ("j", cert); | |
187 } | 164 } |
188 | 165 |
189 TEST(DefaultChannelIDStoreTest, TestDuplicateChannelIds) { | 166 TEST(DefaultChannelIDStoreTest, TestDuplicateChannelIds) { |
190 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 167 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
191 DefaultChannelIDStore store(persistent_store.get()); | 168 DefaultChannelIDStore store(persistent_store.get()); |
| 169 crypto::ECPrivateKey* expected_key = crypto::ECPrivateKey::Create(); |
192 | 170 |
193 base::Time expiration_time; | 171 scoped_ptr<crypto::ECPrivateKey> key; |
194 std::string private_key, cert; | |
195 EXPECT_EQ(0, store.GetChannelIDCount()); | 172 EXPECT_EQ(0, store.GetChannelIDCount()); |
196 store.SetChannelID( | 173 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
197 "verisign.com", | 174 "verisign.com", base::Time::FromInternalValue(123), |
198 base::Time::FromInternalValue(123), | 175 crypto::ECPrivateKey::Create()))); |
199 base::Time::FromInternalValue(1234), | 176 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
200 "a", "b"); | 177 "verisign.com", base::Time::FromInternalValue(456), expected_key))); |
201 store.SetChannelID( | |
202 "verisign.com", | |
203 base::Time::FromInternalValue(456), | |
204 base::Time::FromInternalValue(4567), | |
205 "c", "d"); | |
206 | 178 |
207 // Wait for load & queued set tasks. | 179 // Wait for load & queued set tasks. |
208 base::MessageLoop::current()->RunUntilIdle(); | 180 base::MessageLoop::current()->RunUntilIdle(); |
209 EXPECT_EQ(1, store.GetChannelIDCount()); | 181 EXPECT_EQ(1, store.GetChannelIDCount()); |
210 EXPECT_EQ(OK, | 182 EXPECT_EQ(OK, store.GetChannelID("verisign.com", &key, |
211 store.GetChannelID("verisign.com", | 183 base::Bind(&GetChannelIDCallbackNotCalled))); |
212 &expiration_time, | 184 ExpectKeysEqual(expected_key, key.get()); |
213 &private_key, | |
214 &cert, | |
215 base::Bind(&GetChannelIDCallbackNotCalled))); | |
216 EXPECT_EQ(4567, expiration_time.ToInternalValue()); | |
217 EXPECT_EQ("c", private_key); | |
218 EXPECT_EQ("d", cert); | |
219 } | 185 } |
220 | 186 |
221 TEST(DefaultChannelIDStoreTest, TestAsyncGet) { | 187 TEST(DefaultChannelIDStoreTest, TestAsyncGet) { |
222 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 188 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 189 crypto::ECPrivateKey* expected_key = crypto::ECPrivateKey::Create(); |
223 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 190 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
224 "verisign.com", | 191 "verisign.com", base::Time::FromInternalValue(123), expected_key)); |
225 base::Time::FromInternalValue(123), | |
226 base::Time::FromInternalValue(1234), | |
227 "a", "b")); | |
228 | 192 |
229 DefaultChannelIDStore store(persistent_store.get()); | 193 DefaultChannelIDStore store(persistent_store.get()); |
230 AsyncGetChannelIDHelper helper; | 194 AsyncGetChannelIDHelper helper; |
231 base::Time expiration_time; | 195 scoped_ptr<crypto::ECPrivateKey> key; |
232 std::string private_key; | |
233 std::string cert = "not set"; | |
234 EXPECT_EQ(0, store.GetChannelIDCount()); | 196 EXPECT_EQ(0, store.GetChannelIDCount()); |
235 EXPECT_EQ(ERR_IO_PENDING, | 197 EXPECT_EQ(ERR_IO_PENDING, |
236 store.GetChannelID("verisign.com", | 198 store.GetChannelID("verisign.com", &key, |
237 &expiration_time, | |
238 &private_key, | |
239 &cert, | |
240 base::Bind(&AsyncGetChannelIDHelper::Callback, | 199 base::Bind(&AsyncGetChannelIDHelper::Callback, |
241 base::Unretained(&helper)))); | 200 base::Unretained(&helper)))); |
242 | 201 |
243 // Wait for load & queued get tasks. | 202 // Wait for load & queued get tasks. |
244 base::MessageLoop::current()->RunUntilIdle(); | 203 base::MessageLoop::current()->RunUntilIdle(); |
245 EXPECT_EQ(1, store.GetChannelIDCount()); | 204 EXPECT_EQ(1, store.GetChannelIDCount()); |
246 EXPECT_EQ("not set", cert); | 205 EXPECT_FALSE(key); |
247 EXPECT_TRUE(helper.called_); | 206 EXPECT_TRUE(helper.called_); |
248 EXPECT_EQ(OK, helper.err_); | 207 EXPECT_EQ(OK, helper.err_); |
249 EXPECT_EQ("verisign.com", helper.server_identifier_); | 208 EXPECT_EQ("verisign.com", helper.server_identifier_); |
250 EXPECT_EQ(1234, helper.expiration_time_.ToInternalValue()); | 209 ExpectKeysEqual(expected_key, helper.key_.get()); |
251 EXPECT_EQ("a", helper.private_key_); | |
252 EXPECT_EQ("b", helper.cert_); | |
253 } | 210 } |
254 | 211 |
255 TEST(DefaultChannelIDStoreTest, TestDeleteAll) { | 212 TEST(DefaultChannelIDStoreTest, TestDeleteAll) { |
256 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 213 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
257 DefaultChannelIDStore store(persistent_store.get()); | 214 DefaultChannelIDStore store(persistent_store.get()); |
258 | 215 |
259 store.SetChannelID( | 216 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
260 "verisign.com", | 217 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
261 base::Time(), | 218 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
262 base::Time(), | 219 "google.com", base::Time(), crypto::ECPrivateKey::Create()))); |
263 "a", "b"); | 220 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
264 store.SetChannelID( | 221 "harvard.com", base::Time(), crypto::ECPrivateKey::Create()))); |
265 "google.com", | |
266 base::Time(), | |
267 base::Time(), | |
268 "c", "d"); | |
269 store.SetChannelID( | |
270 "harvard.com", | |
271 base::Time(), | |
272 base::Time(), | |
273 "e", "f"); | |
274 // Wait for load & queued set tasks. | 222 // Wait for load & queued set tasks. |
275 base::MessageLoop::current()->RunUntilIdle(); | 223 base::MessageLoop::current()->RunUntilIdle(); |
276 | 224 |
277 EXPECT_EQ(3, store.GetChannelIDCount()); | 225 EXPECT_EQ(3, store.GetChannelIDCount()); |
278 int delete_finished = 0; | 226 int delete_finished = 0; |
279 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); | 227 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); |
280 ASSERT_EQ(1, delete_finished); | 228 ASSERT_EQ(1, delete_finished); |
281 EXPECT_EQ(0, store.GetChannelIDCount()); | 229 EXPECT_EQ(0, store.GetChannelIDCount()); |
282 } | 230 } |
283 | 231 |
284 TEST(DefaultChannelIDStoreTest, TestAsyncGetAndDeleteAll) { | 232 TEST(DefaultChannelIDStoreTest, TestAsyncGetAndDeleteAll) { |
285 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 233 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
286 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 234 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
287 "verisign.com", | 235 "verisign.com", base::Time(), crypto::ECPrivateKey::Create())); |
288 base::Time(), | |
289 base::Time(), | |
290 "a", "b")); | |
291 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 236 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
292 "google.com", | 237 "google.com", base::Time(), crypto::ECPrivateKey::Create())); |
293 base::Time(), | |
294 base::Time(), | |
295 "c", "d")); | |
296 | 238 |
297 ChannelIDStore::ChannelIDList pre_channel_ids; | 239 ChannelIDStore::ChannelIDList pre_channel_ids; |
298 ChannelIDStore::ChannelIDList post_channel_ids; | 240 ChannelIDStore::ChannelIDList post_channel_ids; |
299 int delete_finished = 0; | 241 int delete_finished = 0; |
300 DefaultChannelIDStore store(persistent_store.get()); | 242 DefaultChannelIDStore store(persistent_store.get()); |
301 | 243 |
302 store.GetAllChannelIDs(base::Bind(GetAllCallback, &pre_channel_ids)); | 244 store.GetAllChannelIDs(base::Bind(GetAllCallback, &pre_channel_ids)); |
303 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); | 245 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); |
304 store.GetAllChannelIDs(base::Bind(GetAllCallback, &post_channel_ids)); | 246 store.GetAllChannelIDs(base::Bind(GetAllCallback, &post_channel_ids)); |
305 // Tasks have not run yet. | 247 // Tasks have not run yet. |
306 EXPECT_EQ(0u, pre_channel_ids.size()); | 248 EXPECT_EQ(0u, pre_channel_ids.size()); |
307 // Wait for load & queued tasks. | 249 // Wait for load & queued tasks. |
308 base::MessageLoop::current()->RunUntilIdle(); | 250 base::MessageLoop::current()->RunUntilIdle(); |
309 EXPECT_EQ(0, store.GetChannelIDCount()); | 251 EXPECT_EQ(0, store.GetChannelIDCount()); |
310 EXPECT_EQ(2u, pre_channel_ids.size()); | 252 EXPECT_EQ(2u, pre_channel_ids.size()); |
311 EXPECT_EQ(0u, post_channel_ids.size()); | 253 EXPECT_EQ(0u, post_channel_ids.size()); |
312 } | 254 } |
313 | 255 |
314 TEST(DefaultChannelIDStoreTest, TestDelete) { | 256 TEST(DefaultChannelIDStoreTest, TestDelete) { |
315 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 257 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
316 DefaultChannelIDStore store(persistent_store.get()); | 258 DefaultChannelIDStore store(persistent_store.get()); |
317 | 259 |
318 base::Time expiration_time; | 260 scoped_ptr<crypto::ECPrivateKey> key; |
319 std::string private_key, cert; | |
320 EXPECT_EQ(0, store.GetChannelIDCount()); | 261 EXPECT_EQ(0, store.GetChannelIDCount()); |
321 store.SetChannelID( | 262 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
322 "verisign.com", | 263 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
323 base::Time(), | |
324 base::Time(), | |
325 "a", "b"); | |
326 // Wait for load & queued set task. | 264 // Wait for load & queued set task. |
327 base::MessageLoop::current()->RunUntilIdle(); | 265 base::MessageLoop::current()->RunUntilIdle(); |
328 | 266 |
329 store.SetChannelID( | 267 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
330 "google.com", | 268 "google.com", base::Time(), crypto::ECPrivateKey::Create()))); |
331 base::Time(), | |
332 base::Time(), | |
333 "c", "d"); | |
334 | 269 |
335 EXPECT_EQ(2, store.GetChannelIDCount()); | 270 EXPECT_EQ(2, store.GetChannelIDCount()); |
336 int delete_finished = 0; | 271 int delete_finished = 0; |
337 store.DeleteChannelID("verisign.com", | 272 store.DeleteChannelID("verisign.com", |
338 base::Bind(&CallCounter, &delete_finished)); | 273 base::Bind(&CallCounter, &delete_finished)); |
339 ASSERT_EQ(1, delete_finished); | 274 ASSERT_EQ(1, delete_finished); |
340 EXPECT_EQ(1, store.GetChannelIDCount()); | 275 EXPECT_EQ(1, store.GetChannelIDCount()); |
341 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 276 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
342 store.GetChannelID("verisign.com", | 277 store.GetChannelID("verisign.com", &key, |
343 &expiration_time, | |
344 &private_key, | |
345 &cert, | |
346 base::Bind(&GetChannelIDCallbackNotCalled))); | 278 base::Bind(&GetChannelIDCallbackNotCalled))); |
347 EXPECT_EQ(OK, | 279 EXPECT_EQ(OK, store.GetChannelID("google.com", &key, |
348 store.GetChannelID("google.com", | 280 base::Bind(&GetChannelIDCallbackNotCalled))); |
349 &expiration_time, | |
350 &private_key, | |
351 &cert, | |
352 base::Bind(&GetChannelIDCallbackNotCalled))); | |
353 int delete2_finished = 0; | 281 int delete2_finished = 0; |
354 store.DeleteChannelID("google.com", | 282 store.DeleteChannelID("google.com", |
355 base::Bind(&CallCounter, &delete2_finished)); | 283 base::Bind(&CallCounter, &delete2_finished)); |
356 ASSERT_EQ(1, delete2_finished); | 284 ASSERT_EQ(1, delete2_finished); |
357 EXPECT_EQ(0, store.GetChannelIDCount()); | 285 EXPECT_EQ(0, store.GetChannelIDCount()); |
358 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 286 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
359 store.GetChannelID("google.com", | 287 store.GetChannelID("google.com", &key, |
360 &expiration_time, | |
361 &private_key, | |
362 &cert, | |
363 base::Bind(&GetChannelIDCallbackNotCalled))); | 288 base::Bind(&GetChannelIDCallbackNotCalled))); |
364 } | 289 } |
365 | 290 |
366 TEST(DefaultChannelIDStoreTest, TestAsyncDelete) { | 291 TEST(DefaultChannelIDStoreTest, TestAsyncDelete) { |
367 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 292 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 293 crypto::ECPrivateKey* expected_key = crypto::ECPrivateKey::Create(); |
| 294 persistent_store->AddChannelID( |
| 295 ChannelIDStore::ChannelID("a.com", base::Time::FromInternalValue(1), |
| 296 crypto::ECPrivateKey::Create())); |
368 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 297 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
369 "a.com", | 298 "b.com", base::Time::FromInternalValue(3), expected_key)); |
370 base::Time::FromInternalValue(1), | |
371 base::Time::FromInternalValue(2), | |
372 "a", "b")); | |
373 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | |
374 "b.com", | |
375 base::Time::FromInternalValue(3), | |
376 base::Time::FromInternalValue(4), | |
377 "c", "d")); | |
378 DefaultChannelIDStore store(persistent_store.get()); | 299 DefaultChannelIDStore store(persistent_store.get()); |
379 int delete_finished = 0; | 300 int delete_finished = 0; |
380 store.DeleteChannelID("a.com", | 301 store.DeleteChannelID("a.com", |
381 base::Bind(&CallCounter, &delete_finished)); | 302 base::Bind(&CallCounter, &delete_finished)); |
382 | 303 |
383 AsyncGetChannelIDHelper a_helper; | 304 AsyncGetChannelIDHelper a_helper; |
384 AsyncGetChannelIDHelper b_helper; | 305 AsyncGetChannelIDHelper b_helper; |
385 base::Time expiration_time; | 306 scoped_ptr<crypto::ECPrivateKey> key; |
386 std::string private_key; | |
387 std::string cert = "not set"; | |
388 EXPECT_EQ(0, store.GetChannelIDCount()); | 307 EXPECT_EQ(0, store.GetChannelIDCount()); |
389 EXPECT_EQ(ERR_IO_PENDING, | 308 EXPECT_EQ(ERR_IO_PENDING, |
390 store.GetChannelID( | 309 store.GetChannelID("a.com", &key, |
391 "a.com", &expiration_time, &private_key, &cert, | 310 base::Bind(&AsyncGetChannelIDHelper::Callback, |
392 base::Bind(&AsyncGetChannelIDHelper::Callback, | 311 base::Unretained(&a_helper)))); |
393 base::Unretained(&a_helper)))); | |
394 EXPECT_EQ(ERR_IO_PENDING, | 312 EXPECT_EQ(ERR_IO_PENDING, |
395 store.GetChannelID( | 313 store.GetChannelID("b.com", &key, |
396 "b.com", &expiration_time, &private_key, &cert, | 314 base::Bind(&AsyncGetChannelIDHelper::Callback, |
397 base::Bind(&AsyncGetChannelIDHelper::Callback, | 315 base::Unretained(&b_helper)))); |
398 base::Unretained(&b_helper)))); | |
399 | 316 |
400 EXPECT_EQ(0, delete_finished); | 317 EXPECT_EQ(0, delete_finished); |
401 EXPECT_FALSE(a_helper.called_); | 318 EXPECT_FALSE(a_helper.called_); |
402 EXPECT_FALSE(b_helper.called_); | 319 EXPECT_FALSE(b_helper.called_); |
403 // Wait for load & queued tasks. | 320 // Wait for load & queued tasks. |
404 base::MessageLoop::current()->RunUntilIdle(); | 321 base::MessageLoop::current()->RunUntilIdle(); |
405 EXPECT_EQ(1, delete_finished); | 322 EXPECT_EQ(1, delete_finished); |
406 EXPECT_EQ(1, store.GetChannelIDCount()); | 323 EXPECT_EQ(1, store.GetChannelIDCount()); |
407 EXPECT_EQ("not set", cert); | 324 EXPECT_FALSE(key); |
408 EXPECT_TRUE(a_helper.called_); | 325 EXPECT_TRUE(a_helper.called_); |
409 EXPECT_EQ(ERR_FILE_NOT_FOUND, a_helper.err_); | 326 EXPECT_EQ(ERR_FILE_NOT_FOUND, a_helper.err_); |
410 EXPECT_EQ("a.com", a_helper.server_identifier_); | 327 EXPECT_EQ("a.com", a_helper.server_identifier_); |
411 EXPECT_EQ(0, a_helper.expiration_time_.ToInternalValue()); | 328 EXPECT_FALSE(a_helper.key_); |
412 EXPECT_EQ("", a_helper.private_key_); | |
413 EXPECT_EQ("", a_helper.cert_); | |
414 EXPECT_TRUE(b_helper.called_); | 329 EXPECT_TRUE(b_helper.called_); |
415 EXPECT_EQ(OK, b_helper.err_); | 330 EXPECT_EQ(OK, b_helper.err_); |
416 EXPECT_EQ("b.com", b_helper.server_identifier_); | 331 EXPECT_EQ("b.com", b_helper.server_identifier_); |
417 EXPECT_EQ(4, b_helper.expiration_time_.ToInternalValue()); | 332 ExpectKeysEqual(expected_key, b_helper.key_.get()); |
418 EXPECT_EQ("c", b_helper.private_key_); | |
419 EXPECT_EQ("d", b_helper.cert_); | |
420 } | 333 } |
421 | 334 |
422 TEST(DefaultChannelIDStoreTest, TestGetAll) { | 335 TEST(DefaultChannelIDStoreTest, TestGetAll) { |
423 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 336 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
424 DefaultChannelIDStore store(persistent_store.get()); | 337 DefaultChannelIDStore store(persistent_store.get()); |
425 | 338 |
426 EXPECT_EQ(0, store.GetChannelIDCount()); | 339 EXPECT_EQ(0, store.GetChannelIDCount()); |
427 store.SetChannelID( | 340 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
428 "verisign.com", | 341 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
429 base::Time(), | 342 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
430 base::Time(), | 343 "google.com", base::Time(), crypto::ECPrivateKey::Create()))); |
431 "a", "b"); | 344 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
432 store.SetChannelID( | 345 "harvard.com", base::Time(), crypto::ECPrivateKey::Create()))); |
433 "google.com", | 346 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
434 base::Time(), | 347 "mit.com", base::Time(), crypto::ECPrivateKey::Create()))); |
435 base::Time(), | |
436 "c", "d"); | |
437 store.SetChannelID( | |
438 "harvard.com", | |
439 base::Time(), | |
440 base::Time(), | |
441 "e", "f"); | |
442 store.SetChannelID( | |
443 "mit.com", | |
444 base::Time(), | |
445 base::Time(), | |
446 "g", "h"); | |
447 // Wait for load & queued set tasks. | 348 // Wait for load & queued set tasks. |
448 base::MessageLoop::current()->RunUntilIdle(); | 349 base::MessageLoop::current()->RunUntilIdle(); |
449 | 350 |
450 EXPECT_EQ(4, store.GetChannelIDCount()); | 351 EXPECT_EQ(4, store.GetChannelIDCount()); |
451 ChannelIDStore::ChannelIDList channel_ids; | 352 ChannelIDStore::ChannelIDList channel_ids; |
452 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); | 353 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); |
453 EXPECT_EQ(4u, channel_ids.size()); | 354 EXPECT_EQ(4u, channel_ids.size()); |
454 } | 355 } |
455 | 356 |
456 TEST(DefaultChannelIDStoreTest, TestInitializeFrom) { | 357 TEST(DefaultChannelIDStoreTest, TestInitializeFrom) { |
457 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 358 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
458 DefaultChannelIDStore store(persistent_store.get()); | 359 DefaultChannelIDStore store(persistent_store.get()); |
| 360 crypto::ECPrivateKey* preexisting_key = crypto::ECPrivateKey::Create(); |
| 361 crypto::ECPrivateKey* both_key = crypto::ECPrivateKey::Create(); |
| 362 crypto::ECPrivateKey* copied_key = crypto::ECPrivateKey::Create(); |
459 | 363 |
460 store.SetChannelID( | 364 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
461 "preexisting.com", | 365 "preexisting.com", base::Time(), preexisting_key))); |
462 base::Time(), | 366 store.SetChannelID(make_scoped_ptr(new ChannelIDStore::ChannelID( |
463 base::Time(), | 367 "both.com", base::Time(), crypto::ECPrivateKey::Create()))); |
464 "a", "b"); | |
465 store.SetChannelID( | |
466 "both.com", | |
467 base::Time(), | |
468 base::Time(), | |
469 "c", "d"); | |
470 // Wait for load & queued set tasks. | 368 // Wait for load & queued set tasks. |
471 base::MessageLoop::current()->RunUntilIdle(); | 369 base::MessageLoop::current()->RunUntilIdle(); |
472 EXPECT_EQ(2, store.GetChannelIDCount()); | 370 EXPECT_EQ(2, store.GetChannelIDCount()); |
473 | 371 |
474 ChannelIDStore::ChannelIDList source_channel_ids; | 372 ChannelIDStore::ChannelIDList source_channel_ids; |
475 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 373 source_channel_ids.push_back(ChannelIDStore::ChannelID( |
476 "both.com", | 374 "both.com", base::Time(), |
477 base::Time(), | |
478 base::Time(), | |
479 // Key differs from above to test that existing entries are overwritten. | 375 // Key differs from above to test that existing entries are overwritten. |
480 "e", "f")); | 376 both_key)); |
481 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 377 source_channel_ids.push_back( |
482 "copied.com", | 378 ChannelIDStore::ChannelID("copied.com", base::Time(), copied_key)); |
483 base::Time(), | |
484 base::Time(), | |
485 "g", "h")); | |
486 store.InitializeFrom(source_channel_ids); | 379 store.InitializeFrom(source_channel_ids); |
487 EXPECT_EQ(3, store.GetChannelIDCount()); | 380 EXPECT_EQ(3, store.GetChannelIDCount()); |
488 | 381 |
489 ChannelIDStore::ChannelIDList channel_ids; | 382 ChannelIDStore::ChannelIDList channel_ids; |
490 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); | 383 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); |
491 ASSERT_EQ(3u, channel_ids.size()); | 384 ASSERT_EQ(3u, channel_ids.size()); |
492 | 385 |
493 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); | 386 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); |
494 EXPECT_EQ("both.com", channel_id->server_identifier()); | 387 EXPECT_EQ("both.com", channel_id->server_identifier()); |
495 EXPECT_EQ("e", channel_id->private_key()); | 388 ExpectKeysEqual(both_key, channel_id->key()); |
496 | 389 |
497 ++channel_id; | 390 ++channel_id; |
498 EXPECT_EQ("copied.com", channel_id->server_identifier()); | 391 EXPECT_EQ("copied.com", channel_id->server_identifier()); |
499 EXPECT_EQ("g", channel_id->private_key()); | 392 ExpectKeysEqual(copied_key, channel_id->key()); |
500 | 393 |
501 ++channel_id; | 394 ++channel_id; |
502 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); | 395 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); |
503 EXPECT_EQ("a", channel_id->private_key()); | 396 ExpectKeysEqual(preexisting_key, channel_id->key()); |
504 } | 397 } |
505 | 398 |
506 TEST(DefaultChannelIDStoreTest, TestAsyncInitializeFrom) { | 399 TEST(DefaultChannelIDStoreTest, TestAsyncInitializeFrom) { |
507 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 400 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 401 crypto::ECPrivateKey* preexisting_key = crypto::ECPrivateKey::Create(); |
| 402 crypto::ECPrivateKey* both_key = crypto::ECPrivateKey::Create(); |
| 403 crypto::ECPrivateKey* copied_key = crypto::ECPrivateKey::Create(); |
| 404 |
508 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 405 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
509 "preexisting.com", | 406 "preexisting.com", base::Time(), preexisting_key)); |
510 base::Time(), | |
511 base::Time(), | |
512 "a", "b")); | |
513 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 407 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
514 "both.com", | 408 "both.com", base::Time(), crypto::ECPrivateKey::Create())); |
515 base::Time(), | |
516 base::Time(), | |
517 "c", "d")); | |
518 | 409 |
519 DefaultChannelIDStore store(persistent_store.get()); | 410 DefaultChannelIDStore store(persistent_store.get()); |
520 ChannelIDStore::ChannelIDList source_channel_ids; | 411 ChannelIDStore::ChannelIDList source_channel_ids; |
521 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 412 source_channel_ids.push_back(ChannelIDStore::ChannelID( |
522 "both.com", | 413 "both.com", base::Time(), |
523 base::Time(), | |
524 base::Time(), | |
525 // Key differs from above to test that existing entries are overwritten. | 414 // Key differs from above to test that existing entries are overwritten. |
526 "e", "f")); | 415 both_key)); |
527 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 416 source_channel_ids.push_back( |
528 "copied.com", | 417 ChannelIDStore::ChannelID("copied.com", base::Time(), copied_key)); |
529 base::Time(), | |
530 base::Time(), | |
531 "g", "h")); | |
532 store.InitializeFrom(source_channel_ids); | 418 store.InitializeFrom(source_channel_ids); |
533 EXPECT_EQ(0, store.GetChannelIDCount()); | 419 EXPECT_EQ(0, store.GetChannelIDCount()); |
534 // Wait for load & queued tasks. | 420 // Wait for load & queued tasks. |
535 base::MessageLoop::current()->RunUntilIdle(); | 421 base::MessageLoop::current()->RunUntilIdle(); |
536 EXPECT_EQ(3, store.GetChannelIDCount()); | 422 EXPECT_EQ(3, store.GetChannelIDCount()); |
537 | 423 |
538 ChannelIDStore::ChannelIDList channel_ids; | 424 ChannelIDStore::ChannelIDList channel_ids; |
539 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); | 425 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); |
540 ASSERT_EQ(3u, channel_ids.size()); | 426 ASSERT_EQ(3u, channel_ids.size()); |
541 | 427 |
542 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); | 428 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); |
543 EXPECT_EQ("both.com", channel_id->server_identifier()); | 429 EXPECT_EQ("both.com", channel_id->server_identifier()); |
544 EXPECT_EQ("e", channel_id->private_key()); | 430 ExpectKeysEqual(both_key, channel_id->key()); |
545 | 431 |
546 ++channel_id; | 432 ++channel_id; |
547 EXPECT_EQ("copied.com", channel_id->server_identifier()); | 433 EXPECT_EQ("copied.com", channel_id->server_identifier()); |
548 EXPECT_EQ("g", channel_id->private_key()); | 434 ExpectKeysEqual(copied_key, channel_id->key()); |
549 | 435 |
550 ++channel_id; | 436 ++channel_id; |
551 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); | 437 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); |
552 EXPECT_EQ("a", channel_id->private_key()); | 438 ExpectKeysEqual(preexisting_key, channel_id->key()); |
553 } | 439 } |
554 | 440 |
555 } // namespace net | 441 } // namespace net |
OLD | NEW |