| 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 <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 bool DomainNotEquals(const std::string& domain1, const std::string& domain2) { | 125 bool DomainNotEquals(const std::string& domain1, const std::string& domain2) { |
| 126 return !DomainEquals(domain1, domain2); | 126 return !DomainEquals(domain1, domain2); |
| 127 } | 127 } |
| 128 | 128 |
| 129 } // namespace | 129 } // namespace |
| 130 | 130 |
| 131 TEST(DefaultChannelIDStoreTest, TestLoading) { | 131 TEST(DefaultChannelIDStoreTest, TestLoading) { |
| 132 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 132 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 133 | 133 |
| 134 persistent_store->AddChannelID(DefaultChannelIDStore::ChannelID( | 134 persistent_store->AddChannelID(DefaultChannelIDStore::ChannelID( |
| 135 "google.com", base::Time(), | 135 "google.com", base::Time(), crypto::ECPrivateKey::Create())); |
| 136 base::WrapUnique(crypto::ECPrivateKey::Create()))); | |
| 137 persistent_store->AddChannelID(DefaultChannelIDStore::ChannelID( | 136 persistent_store->AddChannelID(DefaultChannelIDStore::ChannelID( |
| 138 "verisign.com", base::Time(), | 137 "verisign.com", base::Time(), crypto::ECPrivateKey::Create())); |
| 139 base::WrapUnique(crypto::ECPrivateKey::Create()))); | |
| 140 | 138 |
| 141 // Make sure channel_ids load properly. | 139 // Make sure channel_ids load properly. |
| 142 DefaultChannelIDStore store(persistent_store.get()); | 140 DefaultChannelIDStore store(persistent_store.get()); |
| 143 // Load has not occurred yet. | 141 // Load has not occurred yet. |
| 144 EXPECT_EQ(0, store.GetChannelIDCount()); | 142 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 145 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 143 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 146 "verisign.com", base::Time(), | 144 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 147 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 148 // Wait for load & queued set task. | 145 // Wait for load & queued set task. |
| 149 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
| 150 EXPECT_EQ(2, store.GetChannelIDCount()); | 147 EXPECT_EQ(2, store.GetChannelIDCount()); |
| 151 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 148 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 152 "twitter.com", base::Time(), | 149 "twitter.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 153 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 154 // Set should be synchronous now that load is done. | 150 // Set should be synchronous now that load is done. |
| 155 EXPECT_EQ(3, store.GetChannelIDCount()); | 151 EXPECT_EQ(3, store.GetChannelIDCount()); |
| 156 } | 152 } |
| 157 | 153 |
| 158 //TODO(mattm): add more tests of without a persistent store? | 154 //TODO(mattm): add more tests of without a persistent store? |
| 159 TEST(DefaultChannelIDStoreTest, TestSettingAndGetting) { | 155 TEST(DefaultChannelIDStoreTest, TestSettingAndGetting) { |
| 160 // No persistent store, all calls will be synchronous. | 156 // No persistent store, all calls will be synchronous. |
| 161 DefaultChannelIDStore store(NULL); | 157 DefaultChannelIDStore store(NULL); |
| 162 std::unique_ptr<crypto::ECPrivateKey> expected_key( | 158 std::unique_ptr<crypto::ECPrivateKey> expected_key( |
| 163 crypto::ECPrivateKey::Create()); | 159 crypto::ECPrivateKey::Create()); |
| 164 | 160 |
| 165 std::unique_ptr<crypto::ECPrivateKey> key; | 161 std::unique_ptr<crypto::ECPrivateKey> key; |
| 166 EXPECT_EQ(0, store.GetChannelIDCount()); | 162 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 167 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 163 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 168 store.GetChannelID("verisign.com", &key, | 164 store.GetChannelID("verisign.com", &key, |
| 169 base::Bind(&GetChannelIDCallbackNotCalled))); | 165 base::Bind(&GetChannelIDCallbackNotCalled))); |
| 170 EXPECT_FALSE(key); | 166 EXPECT_FALSE(key); |
| 171 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 167 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 172 "verisign.com", base::Time::FromInternalValue(123), | 168 "verisign.com", base::Time::FromInternalValue(123), |
| 173 base::WrapUnique(expected_key->Copy())))); | 169 expected_key->Copy()))); |
| 174 EXPECT_EQ(OK, store.GetChannelID("verisign.com", &key, | 170 EXPECT_EQ(OK, store.GetChannelID("verisign.com", &key, |
| 175 base::Bind(&GetChannelIDCallbackNotCalled))); | 171 base::Bind(&GetChannelIDCallbackNotCalled))); |
| 176 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); | 172 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); |
| 177 } | 173 } |
| 178 | 174 |
| 179 TEST(DefaultChannelIDStoreTest, TestDuplicateChannelIds) { | 175 TEST(DefaultChannelIDStoreTest, TestDuplicateChannelIds) { |
| 180 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 176 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 181 DefaultChannelIDStore store(persistent_store.get()); | 177 DefaultChannelIDStore store(persistent_store.get()); |
| 182 std::unique_ptr<crypto::ECPrivateKey> expected_key( | 178 std::unique_ptr<crypto::ECPrivateKey> expected_key( |
| 183 crypto::ECPrivateKey::Create()); | 179 crypto::ECPrivateKey::Create()); |
| 184 | 180 |
| 185 std::unique_ptr<crypto::ECPrivateKey> key; | 181 std::unique_ptr<crypto::ECPrivateKey> key; |
| 186 EXPECT_EQ(0, store.GetChannelIDCount()); | 182 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 187 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 183 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 188 "verisign.com", base::Time::FromInternalValue(123), | 184 "verisign.com", base::Time::FromInternalValue(123), |
| 189 base::WrapUnique(crypto::ECPrivateKey::Create())))); | 185 crypto::ECPrivateKey::Create()))); |
| 190 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 186 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 191 "verisign.com", base::Time::FromInternalValue(456), | 187 "verisign.com", base::Time::FromInternalValue(456), |
| 192 base::WrapUnique(expected_key->Copy())))); | 188 expected_key->Copy()))); |
| 193 | 189 |
| 194 // Wait for load & queued set tasks. | 190 // Wait for load & queued set tasks. |
| 195 base::RunLoop().RunUntilIdle(); | 191 base::RunLoop().RunUntilIdle(); |
| 196 EXPECT_EQ(1, store.GetChannelIDCount()); | 192 EXPECT_EQ(1, store.GetChannelIDCount()); |
| 197 EXPECT_EQ(OK, store.GetChannelID("verisign.com", &key, | 193 EXPECT_EQ(OK, store.GetChannelID("verisign.com", &key, |
| 198 base::Bind(&GetChannelIDCallbackNotCalled))); | 194 base::Bind(&GetChannelIDCallbackNotCalled))); |
| 199 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); | 195 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); |
| 200 } | 196 } |
| 201 | 197 |
| 202 TEST(DefaultChannelIDStoreTest, TestAsyncGet) { | 198 TEST(DefaultChannelIDStoreTest, TestAsyncGet) { |
| 203 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 199 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 204 std::unique_ptr<crypto::ECPrivateKey> expected_key( | 200 std::unique_ptr<crypto::ECPrivateKey> expected_key( |
| 205 crypto::ECPrivateKey::Create()); | 201 crypto::ECPrivateKey::Create()); |
| 206 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 202 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
| 207 "verisign.com", base::Time::FromInternalValue(123), | 203 "verisign.com", base::Time::FromInternalValue(123), |
| 208 base::WrapUnique(expected_key->Copy()))); | 204 expected_key->Copy())); |
| 209 | 205 |
| 210 DefaultChannelIDStore store(persistent_store.get()); | 206 DefaultChannelIDStore store(persistent_store.get()); |
| 211 AsyncGetChannelIDHelper helper; | 207 AsyncGetChannelIDHelper helper; |
| 212 std::unique_ptr<crypto::ECPrivateKey> key; | 208 std::unique_ptr<crypto::ECPrivateKey> key; |
| 213 EXPECT_EQ(0, store.GetChannelIDCount()); | 209 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 214 EXPECT_EQ(ERR_IO_PENDING, | 210 EXPECT_EQ(ERR_IO_PENDING, |
| 215 store.GetChannelID("verisign.com", &key, | 211 store.GetChannelID("verisign.com", &key, |
| 216 base::Bind(&AsyncGetChannelIDHelper::Callback, | 212 base::Bind(&AsyncGetChannelIDHelper::Callback, |
| 217 base::Unretained(&helper)))); | 213 base::Unretained(&helper)))); |
| 218 | 214 |
| 219 // Wait for load & queued get tasks. | 215 // Wait for load & queued get tasks. |
| 220 base::RunLoop().RunUntilIdle(); | 216 base::RunLoop().RunUntilIdle(); |
| 221 EXPECT_EQ(1, store.GetChannelIDCount()); | 217 EXPECT_EQ(1, store.GetChannelIDCount()); |
| 222 EXPECT_FALSE(key); | 218 EXPECT_FALSE(key); |
| 223 EXPECT_TRUE(helper.called_); | 219 EXPECT_TRUE(helper.called_); |
| 224 EXPECT_EQ(OK, helper.err_); | 220 EXPECT_EQ(OK, helper.err_); |
| 225 EXPECT_EQ("verisign.com", helper.server_identifier_); | 221 EXPECT_EQ("verisign.com", helper.server_identifier_); |
| 226 EXPECT_TRUE(KeysEqual(expected_key.get(), helper.key_.get())); | 222 EXPECT_TRUE(KeysEqual(expected_key.get(), helper.key_.get())); |
| 227 } | 223 } |
| 228 | 224 |
| 229 TEST(DefaultChannelIDStoreTest, TestDeleteAll) { | 225 TEST(DefaultChannelIDStoreTest, TestDeleteAll) { |
| 230 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 226 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 231 DefaultChannelIDStore store(persistent_store.get()); | 227 DefaultChannelIDStore store(persistent_store.get()); |
| 232 | 228 |
| 233 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 229 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 234 "verisign.com", base::Time(), | 230 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 235 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 236 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 231 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 237 "google.com", base::Time(), | 232 "google.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 238 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 239 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 233 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 240 "harvard.com", base::Time(), | 234 "harvard.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 241 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 242 // Wait for load & queued set tasks. | 235 // Wait for load & queued set tasks. |
| 243 base::RunLoop().RunUntilIdle(); | 236 base::RunLoop().RunUntilIdle(); |
| 244 | 237 |
| 245 EXPECT_EQ(3, store.GetChannelIDCount()); | 238 EXPECT_EQ(3, store.GetChannelIDCount()); |
| 246 int delete_finished = 0; | 239 int delete_finished = 0; |
| 247 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); | 240 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); |
| 248 ASSERT_EQ(1, delete_finished); | 241 ASSERT_EQ(1, delete_finished); |
| 249 EXPECT_EQ(0, store.GetChannelIDCount()); | 242 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 250 } | 243 } |
| 251 | 244 |
| 252 TEST(DefaultChannelIDStoreTest, TestDeleteForDomains) { | 245 TEST(DefaultChannelIDStoreTest, TestDeleteForDomains) { |
| 253 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 246 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 254 DefaultChannelIDStore store(persistent_store.get()); | 247 DefaultChannelIDStore store(persistent_store.get()); |
| 255 | 248 |
| 256 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 249 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 257 "verisign.com", base::Time(), | 250 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 258 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 259 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 251 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 260 "google.com", base::Time(), | 252 "google.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 261 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 262 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 253 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 263 "harvard.com", base::Time(), | 254 "harvard.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 264 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 265 // Wait for load & queued set tasks. | 255 // Wait for load & queued set tasks. |
| 266 base::RunLoop().RunUntilIdle(); | 256 base::RunLoop().RunUntilIdle(); |
| 267 EXPECT_EQ(3, store.GetChannelIDCount()); | 257 EXPECT_EQ(3, store.GetChannelIDCount()); |
| 268 | 258 |
| 269 // Whitelist deletion. | 259 // Whitelist deletion. |
| 270 int deletions_finished = 0; | 260 int deletions_finished = 0; |
| 271 store.DeleteForDomainsCreatedBetween( | 261 store.DeleteForDomainsCreatedBetween( |
| 272 base::Bind(&DomainEquals, base::ConstRef(std::string("verisign.com"))), | 262 base::Bind(&DomainEquals, base::ConstRef(std::string("verisign.com"))), |
| 273 base::Time(), base::Time(), | 263 base::Time(), base::Time(), |
| 274 base::Bind(&CallCounter, &deletions_finished)); | 264 base::Bind(&CallCounter, &deletions_finished)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 286 base::Bind(&CallCounter, &deletions_finished)); | 276 base::Bind(&CallCounter, &deletions_finished)); |
| 287 ASSERT_EQ(2, deletions_finished); | 277 ASSERT_EQ(2, deletions_finished); |
| 288 EXPECT_EQ(1, store.GetChannelIDCount()); | 278 EXPECT_EQ(1, store.GetChannelIDCount()); |
| 289 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); | 279 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); |
| 290 EXPECT_EQ("google.com", channel_ids.begin()->server_identifier()); | 280 EXPECT_EQ("google.com", channel_ids.begin()->server_identifier()); |
| 291 } | 281 } |
| 292 | 282 |
| 293 TEST(DefaultChannelIDStoreTest, TestAsyncGetAndDeleteAll) { | 283 TEST(DefaultChannelIDStoreTest, TestAsyncGetAndDeleteAll) { |
| 294 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 284 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 295 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 285 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
| 296 "verisign.com", base::Time(), | 286 "verisign.com", base::Time(), crypto::ECPrivateKey::Create())); |
| 297 base::WrapUnique(crypto::ECPrivateKey::Create()))); | |
| 298 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 287 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
| 299 "google.com", base::Time(), | 288 "google.com", base::Time(), crypto::ECPrivateKey::Create())); |
| 300 base::WrapUnique(crypto::ECPrivateKey::Create()))); | |
| 301 | 289 |
| 302 ChannelIDStore::ChannelIDList pre_channel_ids; | 290 ChannelIDStore::ChannelIDList pre_channel_ids; |
| 303 ChannelIDStore::ChannelIDList post_channel_ids; | 291 ChannelIDStore::ChannelIDList post_channel_ids; |
| 304 int delete_finished = 0; | 292 int delete_finished = 0; |
| 305 DefaultChannelIDStore store(persistent_store.get()); | 293 DefaultChannelIDStore store(persistent_store.get()); |
| 306 | 294 |
| 307 store.GetAllChannelIDs(base::Bind(GetAllCallback, &pre_channel_ids)); | 295 store.GetAllChannelIDs(base::Bind(GetAllCallback, &pre_channel_ids)); |
| 308 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); | 296 store.DeleteAll(base::Bind(&CallCounter, &delete_finished)); |
| 309 store.GetAllChannelIDs(base::Bind(GetAllCallback, &post_channel_ids)); | 297 store.GetAllChannelIDs(base::Bind(GetAllCallback, &post_channel_ids)); |
| 310 // Tasks have not run yet. | 298 // Tasks have not run yet. |
| 311 EXPECT_EQ(0u, pre_channel_ids.size()); | 299 EXPECT_EQ(0u, pre_channel_ids.size()); |
| 312 // Wait for load & queued tasks. | 300 // Wait for load & queued tasks. |
| 313 base::RunLoop().RunUntilIdle(); | 301 base::RunLoop().RunUntilIdle(); |
| 314 EXPECT_EQ(0, store.GetChannelIDCount()); | 302 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 315 EXPECT_EQ(2u, pre_channel_ids.size()); | 303 EXPECT_EQ(2u, pre_channel_ids.size()); |
| 316 EXPECT_EQ(0u, post_channel_ids.size()); | 304 EXPECT_EQ(0u, post_channel_ids.size()); |
| 317 } | 305 } |
| 318 | 306 |
| 319 TEST(DefaultChannelIDStoreTest, TestDelete) { | 307 TEST(DefaultChannelIDStoreTest, TestDelete) { |
| 320 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 308 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 321 DefaultChannelIDStore store(persistent_store.get()); | 309 DefaultChannelIDStore store(persistent_store.get()); |
| 322 | 310 |
| 323 std::unique_ptr<crypto::ECPrivateKey> key; | 311 std::unique_ptr<crypto::ECPrivateKey> key; |
| 324 EXPECT_EQ(0, store.GetChannelIDCount()); | 312 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 325 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 313 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 326 "verisign.com", base::Time(), | 314 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 327 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 328 // Wait for load & queued set task. | 315 // Wait for load & queued set task. |
| 329 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
| 330 | 317 |
| 331 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 318 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 332 "google.com", base::Time(), | 319 "google.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 333 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 334 | 320 |
| 335 EXPECT_EQ(2, store.GetChannelIDCount()); | 321 EXPECT_EQ(2, store.GetChannelIDCount()); |
| 336 int delete_finished = 0; | 322 int delete_finished = 0; |
| 337 store.DeleteChannelID("verisign.com", | 323 store.DeleteChannelID("verisign.com", |
| 338 base::Bind(&CallCounter, &delete_finished)); | 324 base::Bind(&CallCounter, &delete_finished)); |
| 339 ASSERT_EQ(1, delete_finished); | 325 ASSERT_EQ(1, delete_finished); |
| 340 EXPECT_EQ(1, store.GetChannelIDCount()); | 326 EXPECT_EQ(1, store.GetChannelIDCount()); |
| 341 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 327 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 342 store.GetChannelID("verisign.com", &key, | 328 store.GetChannelID("verisign.com", &key, |
| 343 base::Bind(&GetChannelIDCallbackNotCalled))); | 329 base::Bind(&GetChannelIDCallbackNotCalled))); |
| 344 EXPECT_EQ(OK, store.GetChannelID("google.com", &key, | 330 EXPECT_EQ(OK, store.GetChannelID("google.com", &key, |
| 345 base::Bind(&GetChannelIDCallbackNotCalled))); | 331 base::Bind(&GetChannelIDCallbackNotCalled))); |
| 346 int delete2_finished = 0; | 332 int delete2_finished = 0; |
| 347 store.DeleteChannelID("google.com", | 333 store.DeleteChannelID("google.com", |
| 348 base::Bind(&CallCounter, &delete2_finished)); | 334 base::Bind(&CallCounter, &delete2_finished)); |
| 349 ASSERT_EQ(1, delete2_finished); | 335 ASSERT_EQ(1, delete2_finished); |
| 350 EXPECT_EQ(0, store.GetChannelIDCount()); | 336 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 351 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 337 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 352 store.GetChannelID("google.com", &key, | 338 store.GetChannelID("google.com", &key, |
| 353 base::Bind(&GetChannelIDCallbackNotCalled))); | 339 base::Bind(&GetChannelIDCallbackNotCalled))); |
| 354 } | 340 } |
| 355 | 341 |
| 356 TEST(DefaultChannelIDStoreTest, TestAsyncDelete) { | 342 TEST(DefaultChannelIDStoreTest, TestAsyncDelete) { |
| 357 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 343 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 358 std::unique_ptr<crypto::ECPrivateKey> expected_key( | 344 std::unique_ptr<crypto::ECPrivateKey> expected_key( |
| 359 crypto::ECPrivateKey::Create()); | 345 crypto::ECPrivateKey::Create()); |
| 346 persistent_store->AddChannelID( |
| 347 ChannelIDStore::ChannelID("a.com", base::Time::FromInternalValue(1), |
| 348 crypto::ECPrivateKey::Create())); |
| 360 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 349 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
| 361 "a.com", base::Time::FromInternalValue(1), | 350 "b.com", base::Time::FromInternalValue(3), expected_key->Copy())); |
| 362 base::WrapUnique(crypto::ECPrivateKey::Create()))); | |
| 363 persistent_store->AddChannelID( | |
| 364 ChannelIDStore::ChannelID("b.com", base::Time::FromInternalValue(3), | |
| 365 base::WrapUnique(expected_key->Copy()))); | |
| 366 DefaultChannelIDStore store(persistent_store.get()); | 351 DefaultChannelIDStore store(persistent_store.get()); |
| 367 int delete_finished = 0; | 352 int delete_finished = 0; |
| 368 store.DeleteChannelID("a.com", | 353 store.DeleteChannelID("a.com", |
| 369 base::Bind(&CallCounter, &delete_finished)); | 354 base::Bind(&CallCounter, &delete_finished)); |
| 370 | 355 |
| 371 AsyncGetChannelIDHelper a_helper; | 356 AsyncGetChannelIDHelper a_helper; |
| 372 AsyncGetChannelIDHelper b_helper; | 357 AsyncGetChannelIDHelper b_helper; |
| 373 std::unique_ptr<crypto::ECPrivateKey> key; | 358 std::unique_ptr<crypto::ECPrivateKey> key; |
| 374 EXPECT_EQ(0, store.GetChannelIDCount()); | 359 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 375 EXPECT_EQ(ERR_IO_PENDING, | 360 EXPECT_EQ(ERR_IO_PENDING, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 398 EXPECT_EQ("b.com", b_helper.server_identifier_); | 383 EXPECT_EQ("b.com", b_helper.server_identifier_); |
| 399 EXPECT_TRUE(KeysEqual(expected_key.get(), b_helper.key_.get())); | 384 EXPECT_TRUE(KeysEqual(expected_key.get(), b_helper.key_.get())); |
| 400 } | 385 } |
| 401 | 386 |
| 402 TEST(DefaultChannelIDStoreTest, TestGetAll) { | 387 TEST(DefaultChannelIDStoreTest, TestGetAll) { |
| 403 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 388 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 404 DefaultChannelIDStore store(persistent_store.get()); | 389 DefaultChannelIDStore store(persistent_store.get()); |
| 405 | 390 |
| 406 EXPECT_EQ(0, store.GetChannelIDCount()); | 391 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 407 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 392 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 408 "verisign.com", base::Time(), | 393 "verisign.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 409 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 410 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 394 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 411 "google.com", base::Time(), | 395 "google.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 412 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 413 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 396 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 414 "harvard.com", base::Time(), | 397 "harvard.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 415 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 416 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 398 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 417 "mit.com", base::Time(), | 399 "mit.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 418 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 419 // Wait for load & queued set tasks. | 400 // Wait for load & queued set tasks. |
| 420 base::RunLoop().RunUntilIdle(); | 401 base::RunLoop().RunUntilIdle(); |
| 421 | 402 |
| 422 EXPECT_EQ(4, store.GetChannelIDCount()); | 403 EXPECT_EQ(4, store.GetChannelIDCount()); |
| 423 ChannelIDStore::ChannelIDList channel_ids; | 404 ChannelIDStore::ChannelIDList channel_ids; |
| 424 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); | 405 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); |
| 425 EXPECT_EQ(4u, channel_ids.size()); | 406 EXPECT_EQ(4u, channel_ids.size()); |
| 426 } | 407 } |
| 427 | 408 |
| 428 TEST(DefaultChannelIDStoreTest, TestInitializeFrom) { | 409 TEST(DefaultChannelIDStoreTest, TestInitializeFrom) { |
| 429 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 410 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 430 DefaultChannelIDStore store(persistent_store.get()); | 411 DefaultChannelIDStore store(persistent_store.get()); |
| 431 std::unique_ptr<crypto::ECPrivateKey> preexisting_key( | 412 std::unique_ptr<crypto::ECPrivateKey> preexisting_key( |
| 432 crypto::ECPrivateKey::Create()); | 413 crypto::ECPrivateKey::Create()); |
| 433 std::unique_ptr<crypto::ECPrivateKey> both_key( | 414 std::unique_ptr<crypto::ECPrivateKey> both_key( |
| 434 crypto::ECPrivateKey::Create()); | 415 crypto::ECPrivateKey::Create()); |
| 435 std::unique_ptr<crypto::ECPrivateKey> copied_key( | 416 std::unique_ptr<crypto::ECPrivateKey> copied_key( |
| 436 crypto::ECPrivateKey::Create()); | 417 crypto::ECPrivateKey::Create()); |
| 437 | 418 |
| 438 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 419 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 439 "preexisting.com", base::Time(), | 420 "preexisting.com", base::Time(), preexisting_key->Copy()))); |
| 440 base::WrapUnique(preexisting_key->Copy())))); | |
| 441 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( | 421 store.SetChannelID(base::WrapUnique(new ChannelIDStore::ChannelID( |
| 442 "both.com", base::Time(), | 422 "both.com", base::Time(), crypto::ECPrivateKey::Create()))); |
| 443 base::WrapUnique(crypto::ECPrivateKey::Create())))); | |
| 444 // Wait for load & queued set tasks. | 423 // Wait for load & queued set tasks. |
| 445 base::RunLoop().RunUntilIdle(); | 424 base::RunLoop().RunUntilIdle(); |
| 446 EXPECT_EQ(2, store.GetChannelIDCount()); | 425 EXPECT_EQ(2, store.GetChannelIDCount()); |
| 447 | 426 |
| 448 ChannelIDStore::ChannelIDList source_channel_ids; | 427 ChannelIDStore::ChannelIDList source_channel_ids; |
| 449 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 428 source_channel_ids.push_back(ChannelIDStore::ChannelID( |
| 450 "both.com", base::Time(), | 429 "both.com", base::Time(), |
| 451 // Key differs from above to test that existing entries are overwritten. | 430 // Key differs from above to test that existing entries are overwritten. |
| 452 base::WrapUnique(both_key->Copy()))); | 431 both_key->Copy())); |
| 453 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 432 source_channel_ids.push_back(ChannelIDStore::ChannelID( |
| 454 "copied.com", base::Time(), base::WrapUnique(copied_key->Copy()))); | 433 "copied.com", base::Time(), copied_key->Copy())); |
| 455 store.InitializeFrom(source_channel_ids); | 434 store.InitializeFrom(source_channel_ids); |
| 456 EXPECT_EQ(3, store.GetChannelIDCount()); | 435 EXPECT_EQ(3, store.GetChannelIDCount()); |
| 457 | 436 |
| 458 ChannelIDStore::ChannelIDList channel_ids; | 437 ChannelIDStore::ChannelIDList channel_ids; |
| 459 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); | 438 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); |
| 460 ASSERT_EQ(3u, channel_ids.size()); | 439 ASSERT_EQ(3u, channel_ids.size()); |
| 461 | 440 |
| 462 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); | 441 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); |
| 463 EXPECT_EQ("both.com", channel_id->server_identifier()); | 442 EXPECT_EQ("both.com", channel_id->server_identifier()); |
| 464 EXPECT_TRUE(KeysEqual(both_key.get(), channel_id->key())); | 443 EXPECT_TRUE(KeysEqual(both_key.get(), channel_id->key())); |
| 465 | 444 |
| 466 ++channel_id; | 445 ++channel_id; |
| 467 EXPECT_EQ("copied.com", channel_id->server_identifier()); | 446 EXPECT_EQ("copied.com", channel_id->server_identifier()); |
| 468 EXPECT_TRUE(KeysEqual(copied_key.get(), channel_id->key())); | 447 EXPECT_TRUE(KeysEqual(copied_key.get(), channel_id->key())); |
| 469 | 448 |
| 470 ++channel_id; | 449 ++channel_id; |
| 471 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); | 450 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); |
| 472 EXPECT_TRUE(KeysEqual(preexisting_key.get(), channel_id->key())); | 451 EXPECT_TRUE(KeysEqual(preexisting_key.get(), channel_id->key())); |
| 473 } | 452 } |
| 474 | 453 |
| 475 TEST(DefaultChannelIDStoreTest, TestAsyncInitializeFrom) { | 454 TEST(DefaultChannelIDStoreTest, TestAsyncInitializeFrom) { |
| 476 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 455 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 477 std::unique_ptr<crypto::ECPrivateKey> preexisting_key( | 456 std::unique_ptr<crypto::ECPrivateKey> preexisting_key( |
| 478 crypto::ECPrivateKey::Create()); | 457 crypto::ECPrivateKey::Create()); |
| 479 std::unique_ptr<crypto::ECPrivateKey> both_key( | 458 std::unique_ptr<crypto::ECPrivateKey> both_key( |
| 480 crypto::ECPrivateKey::Create()); | 459 crypto::ECPrivateKey::Create()); |
| 481 std::unique_ptr<crypto::ECPrivateKey> copied_key( | 460 std::unique_ptr<crypto::ECPrivateKey> copied_key( |
| 482 crypto::ECPrivateKey::Create()); | 461 crypto::ECPrivateKey::Create()); |
| 483 | 462 |
| 484 persistent_store->AddChannelID( | |
| 485 ChannelIDStore::ChannelID("preexisting.com", base::Time(), | |
| 486 base::WrapUnique(preexisting_key->Copy()))); | |
| 487 persistent_store->AddChannelID(ChannelIDStore::ChannelID( | 463 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
| 488 "both.com", base::Time(), | 464 "preexisting.com", base::Time(), preexisting_key->Copy())); |
| 489 base::WrapUnique(crypto::ECPrivateKey::Create()))); | 465 persistent_store->AddChannelID(ChannelIDStore::ChannelID( |
| 466 "both.com", base::Time(), crypto::ECPrivateKey::Create())); |
| 490 | 467 |
| 491 DefaultChannelIDStore store(persistent_store.get()); | 468 DefaultChannelIDStore store(persistent_store.get()); |
| 492 ChannelIDStore::ChannelIDList source_channel_ids; | 469 ChannelIDStore::ChannelIDList source_channel_ids; |
| 493 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 470 source_channel_ids.push_back(ChannelIDStore::ChannelID( |
| 494 "both.com", base::Time(), | 471 "both.com", base::Time(), |
| 495 // Key differs from above to test that existing entries are overwritten. | 472 // Key differs from above to test that existing entries are overwritten. |
| 496 base::WrapUnique(both_key->Copy()))); | 473 both_key->Copy())); |
| 497 source_channel_ids.push_back(ChannelIDStore::ChannelID( | 474 source_channel_ids.push_back(ChannelIDStore::ChannelID( |
| 498 "copied.com", base::Time(), base::WrapUnique(copied_key->Copy()))); | 475 "copied.com", base::Time(), copied_key->Copy())); |
| 499 store.InitializeFrom(source_channel_ids); | 476 store.InitializeFrom(source_channel_ids); |
| 500 EXPECT_EQ(0, store.GetChannelIDCount()); | 477 EXPECT_EQ(0, store.GetChannelIDCount()); |
| 501 // Wait for load & queued tasks. | 478 // Wait for load & queued tasks. |
| 502 base::RunLoop().RunUntilIdle(); | 479 base::RunLoop().RunUntilIdle(); |
| 503 EXPECT_EQ(3, store.GetChannelIDCount()); | 480 EXPECT_EQ(3, store.GetChannelIDCount()); |
| 504 | 481 |
| 505 ChannelIDStore::ChannelIDList channel_ids; | 482 ChannelIDStore::ChannelIDList channel_ids; |
| 506 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); | 483 store.GetAllChannelIDs(base::Bind(GetAllCallback, &channel_ids)); |
| 507 ASSERT_EQ(3u, channel_ids.size()); | 484 ASSERT_EQ(3u, channel_ids.size()); |
| 508 | 485 |
| 509 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); | 486 ChannelIDStore::ChannelIDList::iterator channel_id = channel_ids.begin(); |
| 510 EXPECT_EQ("both.com", channel_id->server_identifier()); | 487 EXPECT_EQ("both.com", channel_id->server_identifier()); |
| 511 EXPECT_TRUE(KeysEqual(both_key.get(), channel_id->key())); | 488 EXPECT_TRUE(KeysEqual(both_key.get(), channel_id->key())); |
| 512 | 489 |
| 513 ++channel_id; | 490 ++channel_id; |
| 514 EXPECT_EQ("copied.com", channel_id->server_identifier()); | 491 EXPECT_EQ("copied.com", channel_id->server_identifier()); |
| 515 EXPECT_TRUE(KeysEqual(copied_key.get(), channel_id->key())); | 492 EXPECT_TRUE(KeysEqual(copied_key.get(), channel_id->key())); |
| 516 | 493 |
| 517 ++channel_id; | 494 ++channel_id; |
| 518 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); | 495 EXPECT_EQ("preexisting.com", channel_id->server_identifier()); |
| 519 EXPECT_TRUE(KeysEqual(preexisting_key.get(), channel_id->key())); | 496 EXPECT_TRUE(KeysEqual(preexisting_key.get(), channel_id->key())); |
| 520 } | 497 } |
| 521 | 498 |
| 522 } // namespace net | 499 } // namespace net |
| OLD | NEW |