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