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