| 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/channel_id_service.h" | 5 #include "net/ssl/channel_id_service.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
| 18 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 19 #include "crypto/ec_private_key.h" | 20 #include "crypto/ec_private_key.h" |
| 20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 21 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
| 22 #include "net/cert/asn1_util.h" | 23 #include "net/cert/asn1_util.h" |
| 23 #include "net/cert/x509_certificate.h" | 24 #include "net/cert/x509_certificate.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 DISALLOW_COPY_AND_ASSIGN(FailingTaskRunner); | 56 DISALLOW_COPY_AND_ASSIGN(FailingTaskRunner); |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 class MockChannelIDStoreWithAsyncGet | 59 class MockChannelIDStoreWithAsyncGet |
| 59 : public DefaultChannelIDStore { | 60 : public DefaultChannelIDStore { |
| 60 public: | 61 public: |
| 61 MockChannelIDStoreWithAsyncGet() | 62 MockChannelIDStoreWithAsyncGet() |
| 62 : DefaultChannelIDStore(NULL), channel_id_count_(0) {} | 63 : DefaultChannelIDStore(NULL), channel_id_count_(0) {} |
| 63 | 64 |
| 64 int GetChannelID(const std::string& server_identifier, | 65 int GetChannelID(const std::string& server_identifier, |
| 65 scoped_ptr<crypto::ECPrivateKey>* key_result, | 66 std::unique_ptr<crypto::ECPrivateKey>* key_result, |
| 66 const GetChannelIDCallback& callback) override; | 67 const GetChannelIDCallback& callback) override; |
| 67 | 68 |
| 68 void SetChannelID(scoped_ptr<ChannelID> channel_id) override { | 69 void SetChannelID(std::unique_ptr<ChannelID> channel_id) override { |
| 69 channel_id_count_ = 1; | 70 channel_id_count_ = 1; |
| 70 } | 71 } |
| 71 | 72 |
| 72 int GetChannelIDCount() override { return channel_id_count_; } | 73 int GetChannelIDCount() override { return channel_id_count_; } |
| 73 | 74 |
| 74 void CallGetChannelIDCallbackWithResult(int err, crypto::ECPrivateKey* key); | 75 void CallGetChannelIDCallbackWithResult(int err, crypto::ECPrivateKey* key); |
| 75 | 76 |
| 76 private: | 77 private: |
| 77 GetChannelIDCallback callback_; | 78 GetChannelIDCallback callback_; |
| 78 std::string server_identifier_; | 79 std::string server_identifier_; |
| 79 int channel_id_count_; | 80 int channel_id_count_; |
| 80 }; | 81 }; |
| 81 | 82 |
| 82 int MockChannelIDStoreWithAsyncGet::GetChannelID( | 83 int MockChannelIDStoreWithAsyncGet::GetChannelID( |
| 83 const std::string& server_identifier, | 84 const std::string& server_identifier, |
| 84 scoped_ptr<crypto::ECPrivateKey>* key_result, | 85 std::unique_ptr<crypto::ECPrivateKey>* key_result, |
| 85 const GetChannelIDCallback& callback) { | 86 const GetChannelIDCallback& callback) { |
| 86 server_identifier_ = server_identifier; | 87 server_identifier_ = server_identifier; |
| 87 callback_ = callback; | 88 callback_ = callback; |
| 88 // Reset the cert count, it'll get incremented in either SetChannelID or | 89 // Reset the cert count, it'll get incremented in either SetChannelID or |
| 89 // CallGetChannelIDCallbackWithResult. | 90 // CallGetChannelIDCallbackWithResult. |
| 90 channel_id_count_ = 0; | 91 channel_id_count_ = 0; |
| 91 // Do nothing else: the results to be provided will be specified through | 92 // Do nothing else: the results to be provided will be specified through |
| 92 // CallGetChannelIDCallbackWithResult. | 93 // CallGetChannelIDCallbackWithResult. |
| 93 return ERR_IO_PENDING; | 94 return ERR_IO_PENDING; |
| 94 } | 95 } |
| 95 | 96 |
| 96 void MockChannelIDStoreWithAsyncGet::CallGetChannelIDCallbackWithResult( | 97 void MockChannelIDStoreWithAsyncGet::CallGetChannelIDCallbackWithResult( |
| 97 int err, | 98 int err, |
| 98 crypto::ECPrivateKey* key) { | 99 crypto::ECPrivateKey* key) { |
| 99 if (err == OK) | 100 if (err == OK) |
| 100 channel_id_count_ = 1; | 101 channel_id_count_ = 1; |
| 101 base::ThreadTaskRunnerHandle::Get()->PostTask( | 102 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 102 FROM_HERE, | 103 FROM_HERE, |
| 103 base::Bind(callback_, err, server_identifier_, | 104 base::Bind(callback_, err, server_identifier_, |
| 104 base::Passed(make_scoped_ptr(key ? key->Copy() : nullptr)))); | 105 base::Passed(base::WrapUnique(key ? key->Copy() : nullptr)))); |
| 105 } | 106 } |
| 106 | 107 |
| 107 class ChannelIDServiceTest : public testing::Test { | 108 class ChannelIDServiceTest : public testing::Test { |
| 108 public: | 109 public: |
| 109 ChannelIDServiceTest() | 110 ChannelIDServiceTest() |
| 110 : service_(new ChannelIDService(new DefaultChannelIDStore(NULL), | 111 : service_(new ChannelIDService(new DefaultChannelIDStore(NULL), |
| 111 base::ThreadTaskRunnerHandle::Get())) {} | 112 base::ThreadTaskRunnerHandle::Get())) {} |
| 112 | 113 |
| 113 protected: | 114 protected: |
| 114 scoped_ptr<ChannelIDService> service_; | 115 std::unique_ptr<ChannelIDService> service_; |
| 115 }; | 116 }; |
| 116 | 117 |
| 117 TEST_F(ChannelIDServiceTest, GetDomainForHost) { | 118 TEST_F(ChannelIDServiceTest, GetDomainForHost) { |
| 118 EXPECT_EQ("google.com", | 119 EXPECT_EQ("google.com", |
| 119 ChannelIDService::GetDomainForHost("google.com")); | 120 ChannelIDService::GetDomainForHost("google.com")); |
| 120 EXPECT_EQ("google.com", | 121 EXPECT_EQ("google.com", |
| 121 ChannelIDService::GetDomainForHost("www.google.com")); | 122 ChannelIDService::GetDomainForHost("www.google.com")); |
| 122 EXPECT_EQ("foo.appspot.com", | 123 EXPECT_EQ("foo.appspot.com", |
| 123 ChannelIDService::GetDomainForHost("foo.appspot.com")); | 124 ChannelIDService::GetDomainForHost("foo.appspot.com")); |
| 124 EXPECT_EQ("bar.appspot.com", | 125 EXPECT_EQ("bar.appspot.com", |
| 125 ChannelIDService::GetDomainForHost("foo.bar.appspot.com")); | 126 ChannelIDService::GetDomainForHost("foo.bar.appspot.com")); |
| 126 EXPECT_EQ("appspot.com", | 127 EXPECT_EQ("appspot.com", |
| 127 ChannelIDService::GetDomainForHost("appspot.com")); | 128 ChannelIDService::GetDomainForHost("appspot.com")); |
| 128 EXPECT_EQ("google.com", | 129 EXPECT_EQ("google.com", |
| 129 ChannelIDService::GetDomainForHost("www.mail.google.com")); | 130 ChannelIDService::GetDomainForHost("www.mail.google.com")); |
| 130 EXPECT_EQ("goto", | 131 EXPECT_EQ("goto", |
| 131 ChannelIDService::GetDomainForHost("goto")); | 132 ChannelIDService::GetDomainForHost("goto")); |
| 132 EXPECT_EQ("127.0.0.1", | 133 EXPECT_EQ("127.0.0.1", |
| 133 ChannelIDService::GetDomainForHost("127.0.0.1")); | 134 ChannelIDService::GetDomainForHost("127.0.0.1")); |
| 134 } | 135 } |
| 135 | 136 |
| 136 TEST_F(ChannelIDServiceTest, GetCacheMiss) { | 137 TEST_F(ChannelIDServiceTest, GetCacheMiss) { |
| 137 std::string host("encrypted.google.com"); | 138 std::string host("encrypted.google.com"); |
| 138 | 139 |
| 139 int error; | 140 int error; |
| 140 TestCompletionCallback callback; | 141 TestCompletionCallback callback; |
| 141 ChannelIDService::Request request; | 142 ChannelIDService::Request request; |
| 142 | 143 |
| 143 // Synchronous completion, because the store is initialized. | 144 // Synchronous completion, because the store is initialized. |
| 144 scoped_ptr<crypto::ECPrivateKey> key; | 145 std::unique_ptr<crypto::ECPrivateKey> key; |
| 145 EXPECT_EQ(0, service_->channel_id_count()); | 146 EXPECT_EQ(0, service_->channel_id_count()); |
| 146 error = service_->GetChannelID(host, &key, callback.callback(), &request); | 147 error = service_->GetChannelID(host, &key, callback.callback(), &request); |
| 147 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); | 148 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); |
| 148 EXPECT_FALSE(request.is_active()); | 149 EXPECT_FALSE(request.is_active()); |
| 149 EXPECT_EQ(0, service_->channel_id_count()); | 150 EXPECT_EQ(0, service_->channel_id_count()); |
| 150 EXPECT_FALSE(key); | 151 EXPECT_FALSE(key); |
| 151 } | 152 } |
| 152 | 153 |
| 153 TEST_F(ChannelIDServiceTest, CacheHit) { | 154 TEST_F(ChannelIDServiceTest, CacheHit) { |
| 154 std::string host("encrypted.google.com"); | 155 std::string host("encrypted.google.com"); |
| 155 | 156 |
| 156 int error; | 157 int error; |
| 157 TestCompletionCallback callback; | 158 TestCompletionCallback callback; |
| 158 ChannelIDService::Request request; | 159 ChannelIDService::Request request; |
| 159 | 160 |
| 160 // Asynchronous completion. | 161 // Asynchronous completion. |
| 161 scoped_ptr<crypto::ECPrivateKey> key1; | 162 std::unique_ptr<crypto::ECPrivateKey> key1; |
| 162 EXPECT_EQ(0, service_->channel_id_count()); | 163 EXPECT_EQ(0, service_->channel_id_count()); |
| 163 error = service_->GetOrCreateChannelID(host, &key1, callback.callback(), | 164 error = service_->GetOrCreateChannelID(host, &key1, callback.callback(), |
| 164 &request); | 165 &request); |
| 165 EXPECT_EQ(ERR_IO_PENDING, error); | 166 EXPECT_EQ(ERR_IO_PENDING, error); |
| 166 EXPECT_TRUE(request.is_active()); | 167 EXPECT_TRUE(request.is_active()); |
| 167 error = callback.WaitForResult(); | 168 error = callback.WaitForResult(); |
| 168 EXPECT_EQ(OK, error); | 169 EXPECT_EQ(OK, error); |
| 169 EXPECT_EQ(1, service_->channel_id_count()); | 170 EXPECT_EQ(1, service_->channel_id_count()); |
| 170 EXPECT_TRUE(key1); | 171 EXPECT_TRUE(key1); |
| 171 EXPECT_FALSE(request.is_active()); | 172 EXPECT_FALSE(request.is_active()); |
| 172 | 173 |
| 173 // Synchronous completion. | 174 // Synchronous completion. |
| 174 scoped_ptr<crypto::ECPrivateKey> key2; | 175 std::unique_ptr<crypto::ECPrivateKey> key2; |
| 175 error = service_->GetOrCreateChannelID(host, &key2, callback.callback(), | 176 error = service_->GetOrCreateChannelID(host, &key2, callback.callback(), |
| 176 &request); | 177 &request); |
| 177 EXPECT_FALSE(request.is_active()); | 178 EXPECT_FALSE(request.is_active()); |
| 178 EXPECT_EQ(OK, error); | 179 EXPECT_EQ(OK, error); |
| 179 EXPECT_EQ(1, service_->channel_id_count()); | 180 EXPECT_EQ(1, service_->channel_id_count()); |
| 180 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); | 181 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); |
| 181 | 182 |
| 182 // Synchronous get. | 183 // Synchronous get. |
| 183 scoped_ptr<crypto::ECPrivateKey> key3; | 184 std::unique_ptr<crypto::ECPrivateKey> key3; |
| 184 error = service_->GetChannelID(host, &key3, callback.callback(), &request); | 185 error = service_->GetChannelID(host, &key3, callback.callback(), &request); |
| 185 EXPECT_FALSE(request.is_active()); | 186 EXPECT_FALSE(request.is_active()); |
| 186 EXPECT_EQ(OK, error); | 187 EXPECT_EQ(OK, error); |
| 187 EXPECT_EQ(1, service_->channel_id_count()); | 188 EXPECT_EQ(1, service_->channel_id_count()); |
| 188 EXPECT_TRUE(KeysEqual(key1.get(), key3.get())); | 189 EXPECT_TRUE(KeysEqual(key1.get(), key3.get())); |
| 189 | 190 |
| 190 EXPECT_EQ(3u, service_->requests()); | 191 EXPECT_EQ(3u, service_->requests()); |
| 191 EXPECT_EQ(2u, service_->key_store_hits()); | 192 EXPECT_EQ(2u, service_->key_store_hits()); |
| 192 EXPECT_EQ(0u, service_->inflight_joins()); | 193 EXPECT_EQ(0u, service_->inflight_joins()); |
| 193 } | 194 } |
| 194 | 195 |
| 195 TEST_F(ChannelIDServiceTest, StoreChannelIDs) { | 196 TEST_F(ChannelIDServiceTest, StoreChannelIDs) { |
| 196 int error; | 197 int error; |
| 197 TestCompletionCallback callback; | 198 TestCompletionCallback callback; |
| 198 ChannelIDService::Request request; | 199 ChannelIDService::Request request; |
| 199 | 200 |
| 200 std::string host1("encrypted.google.com"); | 201 std::string host1("encrypted.google.com"); |
| 201 scoped_ptr<crypto::ECPrivateKey> key1; | 202 std::unique_ptr<crypto::ECPrivateKey> key1; |
| 202 EXPECT_EQ(0, service_->channel_id_count()); | 203 EXPECT_EQ(0, service_->channel_id_count()); |
| 203 error = service_->GetOrCreateChannelID(host1, &key1, callback.callback(), | 204 error = service_->GetOrCreateChannelID(host1, &key1, callback.callback(), |
| 204 &request); | 205 &request); |
| 205 EXPECT_EQ(ERR_IO_PENDING, error); | 206 EXPECT_EQ(ERR_IO_PENDING, error); |
| 206 EXPECT_TRUE(request.is_active()); | 207 EXPECT_TRUE(request.is_active()); |
| 207 error = callback.WaitForResult(); | 208 error = callback.WaitForResult(); |
| 208 EXPECT_EQ(OK, error); | 209 EXPECT_EQ(OK, error); |
| 209 EXPECT_EQ(1, service_->channel_id_count()); | 210 EXPECT_EQ(1, service_->channel_id_count()); |
| 210 | 211 |
| 211 std::string host2("www.verisign.com"); | 212 std::string host2("www.verisign.com"); |
| 212 scoped_ptr<crypto::ECPrivateKey> key2; | 213 std::unique_ptr<crypto::ECPrivateKey> key2; |
| 213 error = service_->GetOrCreateChannelID(host2, &key2, callback.callback(), | 214 error = service_->GetOrCreateChannelID(host2, &key2, callback.callback(), |
| 214 &request); | 215 &request); |
| 215 EXPECT_EQ(ERR_IO_PENDING, error); | 216 EXPECT_EQ(ERR_IO_PENDING, error); |
| 216 EXPECT_TRUE(request.is_active()); | 217 EXPECT_TRUE(request.is_active()); |
| 217 error = callback.WaitForResult(); | 218 error = callback.WaitForResult(); |
| 218 EXPECT_EQ(OK, error); | 219 EXPECT_EQ(OK, error); |
| 219 EXPECT_EQ(2, service_->channel_id_count()); | 220 EXPECT_EQ(2, service_->channel_id_count()); |
| 220 | 221 |
| 221 std::string host3("www.twitter.com"); | 222 std::string host3("www.twitter.com"); |
| 222 scoped_ptr<crypto::ECPrivateKey> key3; | 223 std::unique_ptr<crypto::ECPrivateKey> key3; |
| 223 error = service_->GetOrCreateChannelID(host3, &key3, callback.callback(), | 224 error = service_->GetOrCreateChannelID(host3, &key3, callback.callback(), |
| 224 &request); | 225 &request); |
| 225 EXPECT_EQ(ERR_IO_PENDING, error); | 226 EXPECT_EQ(ERR_IO_PENDING, error); |
| 226 EXPECT_TRUE(request.is_active()); | 227 EXPECT_TRUE(request.is_active()); |
| 227 error = callback.WaitForResult(); | 228 error = callback.WaitForResult(); |
| 228 EXPECT_EQ(OK, error); | 229 EXPECT_EQ(OK, error); |
| 229 EXPECT_EQ(3, service_->channel_id_count()); | 230 EXPECT_EQ(3, service_->channel_id_count()); |
| 230 | 231 |
| 231 EXPECT_FALSE(KeysEqual(key1.get(), key2.get())); | 232 EXPECT_FALSE(KeysEqual(key1.get(), key2.get())); |
| 232 EXPECT_FALSE(KeysEqual(key1.get(), key3.get())); | 233 EXPECT_FALSE(KeysEqual(key1.get(), key3.get())); |
| 233 EXPECT_FALSE(KeysEqual(key2.get(), key3.get())); | 234 EXPECT_FALSE(KeysEqual(key2.get(), key3.get())); |
| 234 } | 235 } |
| 235 | 236 |
| 236 // Tests an inflight join. | 237 // Tests an inflight join. |
| 237 TEST_F(ChannelIDServiceTest, InflightJoin) { | 238 TEST_F(ChannelIDServiceTest, InflightJoin) { |
| 238 std::string host("encrypted.google.com"); | 239 std::string host("encrypted.google.com"); |
| 239 int error; | 240 int error; |
| 240 | 241 |
| 241 scoped_ptr<crypto::ECPrivateKey> key1; | 242 std::unique_ptr<crypto::ECPrivateKey> key1; |
| 242 TestCompletionCallback callback1; | 243 TestCompletionCallback callback1; |
| 243 ChannelIDService::Request request1; | 244 ChannelIDService::Request request1; |
| 244 | 245 |
| 245 scoped_ptr<crypto::ECPrivateKey> key2; | 246 std::unique_ptr<crypto::ECPrivateKey> key2; |
| 246 TestCompletionCallback callback2; | 247 TestCompletionCallback callback2; |
| 247 ChannelIDService::Request request2; | 248 ChannelIDService::Request request2; |
| 248 | 249 |
| 249 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), | 250 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), |
| 250 &request1); | 251 &request1); |
| 251 EXPECT_EQ(ERR_IO_PENDING, error); | 252 EXPECT_EQ(ERR_IO_PENDING, error); |
| 252 EXPECT_TRUE(request1.is_active()); | 253 EXPECT_TRUE(request1.is_active()); |
| 253 // Should join with the original request. | 254 // Should join with the original request. |
| 254 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), | 255 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), |
| 255 &request2); | 256 &request2); |
| 256 EXPECT_EQ(ERR_IO_PENDING, error); | 257 EXPECT_EQ(ERR_IO_PENDING, error); |
| 257 EXPECT_TRUE(request2.is_active()); | 258 EXPECT_TRUE(request2.is_active()); |
| 258 | 259 |
| 259 error = callback1.WaitForResult(); | 260 error = callback1.WaitForResult(); |
| 260 EXPECT_EQ(OK, error); | 261 EXPECT_EQ(OK, error); |
| 261 error = callback2.WaitForResult(); | 262 error = callback2.WaitForResult(); |
| 262 EXPECT_EQ(OK, error); | 263 EXPECT_EQ(OK, error); |
| 263 | 264 |
| 264 EXPECT_EQ(2u, service_->requests()); | 265 EXPECT_EQ(2u, service_->requests()); |
| 265 EXPECT_EQ(0u, service_->key_store_hits()); | 266 EXPECT_EQ(0u, service_->key_store_hits()); |
| 266 EXPECT_EQ(1u, service_->inflight_joins()); | 267 EXPECT_EQ(1u, service_->inflight_joins()); |
| 267 EXPECT_EQ(1u, service_->workers_created()); | 268 EXPECT_EQ(1u, service_->workers_created()); |
| 268 } | 269 } |
| 269 | 270 |
| 270 // Tests an inflight join of a Get request to a GetOrCreate request. | 271 // Tests an inflight join of a Get request to a GetOrCreate request. |
| 271 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) { | 272 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) { |
| 272 std::string host("encrypted.google.com"); | 273 std::string host("encrypted.google.com"); |
| 273 int error; | 274 int error; |
| 274 | 275 |
| 275 scoped_ptr<crypto::ECPrivateKey> key1; | 276 std::unique_ptr<crypto::ECPrivateKey> key1; |
| 276 TestCompletionCallback callback1; | 277 TestCompletionCallback callback1; |
| 277 ChannelIDService::Request request1; | 278 ChannelIDService::Request request1; |
| 278 | 279 |
| 279 scoped_ptr<crypto::ECPrivateKey> key2; | 280 std::unique_ptr<crypto::ECPrivateKey> key2; |
| 280 TestCompletionCallback callback2; | 281 TestCompletionCallback callback2; |
| 281 ChannelIDService::Request request2; | 282 ChannelIDService::Request request2; |
| 282 | 283 |
| 283 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), | 284 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), |
| 284 &request1); | 285 &request1); |
| 285 EXPECT_EQ(ERR_IO_PENDING, error); | 286 EXPECT_EQ(ERR_IO_PENDING, error); |
| 286 EXPECT_TRUE(request1.is_active()); | 287 EXPECT_TRUE(request1.is_active()); |
| 287 // Should join with the original request. | 288 // Should join with the original request. |
| 288 error = service_->GetChannelID(host, &key2, callback2.callback(), &request2); | 289 error = service_->GetChannelID(host, &key2, callback2.callback(), &request2); |
| 289 EXPECT_EQ(ERR_IO_PENDING, error); | 290 EXPECT_EQ(ERR_IO_PENDING, error); |
| 290 EXPECT_TRUE(request2.is_active()); | 291 EXPECT_TRUE(request2.is_active()); |
| 291 | 292 |
| 292 error = callback1.WaitForResult(); | 293 error = callback1.WaitForResult(); |
| 293 EXPECT_EQ(OK, error); | 294 EXPECT_EQ(OK, error); |
| 294 error = callback2.WaitForResult(); | 295 error = callback2.WaitForResult(); |
| 295 EXPECT_EQ(OK, error); | 296 EXPECT_EQ(OK, error); |
| 296 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); | 297 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); |
| 297 | 298 |
| 298 EXPECT_EQ(2u, service_->requests()); | 299 EXPECT_EQ(2u, service_->requests()); |
| 299 EXPECT_EQ(0u, service_->key_store_hits()); | 300 EXPECT_EQ(0u, service_->key_store_hits()); |
| 300 EXPECT_EQ(1u, service_->inflight_joins()); | 301 EXPECT_EQ(1u, service_->inflight_joins()); |
| 301 EXPECT_EQ(1u, service_->workers_created()); | 302 EXPECT_EQ(1u, service_->workers_created()); |
| 302 } | 303 } |
| 303 | 304 |
| 304 // Tests that the callback of a canceled request is never made. | 305 // Tests that the callback of a canceled request is never made. |
| 305 TEST_F(ChannelIDServiceTest, CancelRequest) { | 306 TEST_F(ChannelIDServiceTest, CancelRequest) { |
| 306 std::string host("encrypted.google.com"); | 307 std::string host("encrypted.google.com"); |
| 307 scoped_ptr<crypto::ECPrivateKey> key; | 308 std::unique_ptr<crypto::ECPrivateKey> key; |
| 308 int error; | 309 int error; |
| 309 ChannelIDService::Request request; | 310 ChannelIDService::Request request; |
| 310 | 311 |
| 311 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), | 312 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 312 &request); | 313 &request); |
| 313 EXPECT_EQ(ERR_IO_PENDING, error); | 314 EXPECT_EQ(ERR_IO_PENDING, error); |
| 314 EXPECT_TRUE(request.is_active()); | 315 EXPECT_TRUE(request.is_active()); |
| 315 request.Cancel(); | 316 request.Cancel(); |
| 316 EXPECT_FALSE(request.is_active()); | 317 EXPECT_FALSE(request.is_active()); |
| 317 | 318 |
| 318 // Wait for reply from ChannelIDServiceWorker to be posted back to the | 319 // Wait for reply from ChannelIDServiceWorker to be posted back to the |
| 319 // ChannelIDService. | 320 // ChannelIDService. |
| 320 base::MessageLoop::current()->RunUntilIdle(); | 321 base::MessageLoop::current()->RunUntilIdle(); |
| 321 | 322 |
| 322 // Even though the original request was cancelled, the service will still | 323 // Even though the original request was cancelled, the service will still |
| 323 // store the result, it just doesn't call the callback. | 324 // store the result, it just doesn't call the callback. |
| 324 EXPECT_EQ(1, service_->channel_id_count()); | 325 EXPECT_EQ(1, service_->channel_id_count()); |
| 325 } | 326 } |
| 326 | 327 |
| 327 // Tests that destructing the Request cancels the request. | 328 // Tests that destructing the Request cancels the request. |
| 328 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) { | 329 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) { |
| 329 std::string host("encrypted.google.com"); | 330 std::string host("encrypted.google.com"); |
| 330 scoped_ptr<crypto::ECPrivateKey> key; | 331 std::unique_ptr<crypto::ECPrivateKey> key; |
| 331 int error; | 332 int error; |
| 332 scoped_ptr<ChannelIDService::Request> request( | 333 std::unique_ptr<ChannelIDService::Request> request( |
| 333 new ChannelIDService::Request()); | 334 new ChannelIDService::Request()); |
| 334 | 335 |
| 335 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), | 336 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 336 request.get()); | 337 request.get()); |
| 337 EXPECT_EQ(ERR_IO_PENDING, error); | 338 EXPECT_EQ(ERR_IO_PENDING, error); |
| 338 EXPECT_TRUE(request->is_active()); | 339 EXPECT_TRUE(request->is_active()); |
| 339 | 340 |
| 340 // Delete the Request object. | 341 // Delete the Request object. |
| 341 request.reset(); | 342 request.reset(); |
| 342 | 343 |
| 343 // Wait for reply from ChannelIDServiceWorker to be posted back to the | 344 // Wait for reply from ChannelIDServiceWorker to be posted back to the |
| 344 // ChannelIDService. | 345 // ChannelIDService. |
| 345 base::MessageLoop::current()->RunUntilIdle(); | 346 base::MessageLoop::current()->RunUntilIdle(); |
| 346 | 347 |
| 347 // Even though the original request was cancelled, the service will still | 348 // Even though the original request was cancelled, the service will still |
| 348 // store the result, it just doesn't call the callback. | 349 // store the result, it just doesn't call the callback. |
| 349 EXPECT_EQ(1, service_->channel_id_count()); | 350 EXPECT_EQ(1, service_->channel_id_count()); |
| 350 } | 351 } |
| 351 | 352 |
| 352 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { | 353 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { |
| 353 std::string host("encrypted.google.com"); | 354 std::string host("encrypted.google.com"); |
| 354 scoped_ptr<crypto::ECPrivateKey> key; | 355 std::unique_ptr<crypto::ECPrivateKey> key; |
| 355 int error; | 356 int error; |
| 356 ChannelIDService::Request request; | 357 ChannelIDService::Request request; |
| 357 | 358 |
| 358 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), | 359 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 359 &request); | 360 &request); |
| 360 EXPECT_EQ(ERR_IO_PENDING, error); | 361 EXPECT_EQ(ERR_IO_PENDING, error); |
| 361 EXPECT_TRUE(request.is_active()); | 362 EXPECT_TRUE(request.is_active()); |
| 362 | 363 |
| 363 // Cancel request and destroy the ChannelIDService. | 364 // Cancel request and destroy the ChannelIDService. |
| 364 request.Cancel(); | 365 request.Cancel(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 375 // Tests that shutting down the sequenced worker pool and then making new | 376 // Tests that shutting down the sequenced worker pool and then making new |
| 376 // requests gracefully fails. | 377 // requests gracefully fails. |
| 377 // This is a regression test for http://crbug.com/236387 | 378 // This is a regression test for http://crbug.com/236387 |
| 378 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { | 379 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { |
| 379 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner); | 380 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner); |
| 380 service_.reset(new ChannelIDService( | 381 service_.reset(new ChannelIDService( |
| 381 new DefaultChannelIDStore(NULL), task_runner)); | 382 new DefaultChannelIDStore(NULL), task_runner)); |
| 382 | 383 |
| 383 // Make a request that will force synchronous completion. | 384 // Make a request that will force synchronous completion. |
| 384 std::string host("encrypted.google.com"); | 385 std::string host("encrypted.google.com"); |
| 385 scoped_ptr<crypto::ECPrivateKey> key; | 386 std::unique_ptr<crypto::ECPrivateKey> key; |
| 386 int error; | 387 int error; |
| 387 ChannelIDService::Request request; | 388 ChannelIDService::Request request; |
| 388 | 389 |
| 389 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), | 390 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 390 &request); | 391 &request); |
| 391 // If we got here without crashing or a valgrind error, it worked. | 392 // If we got here without crashing or a valgrind error, it worked. |
| 392 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error); | 393 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error); |
| 393 EXPECT_FALSE(request.is_active()); | 394 EXPECT_FALSE(request.is_active()); |
| 394 } | 395 } |
| 395 | 396 |
| 396 // Tests that simultaneous creation of different certs works. | 397 // Tests that simultaneous creation of different certs works. |
| 397 TEST_F(ChannelIDServiceTest, SimultaneousCreation) { | 398 TEST_F(ChannelIDServiceTest, SimultaneousCreation) { |
| 398 int error; | 399 int error; |
| 399 | 400 |
| 400 std::string host1("encrypted.google.com"); | 401 std::string host1("encrypted.google.com"); |
| 401 scoped_ptr<crypto::ECPrivateKey> key1; | 402 std::unique_ptr<crypto::ECPrivateKey> key1; |
| 402 TestCompletionCallback callback1; | 403 TestCompletionCallback callback1; |
| 403 ChannelIDService::Request request1; | 404 ChannelIDService::Request request1; |
| 404 | 405 |
| 405 std::string host2("foo.com"); | 406 std::string host2("foo.com"); |
| 406 scoped_ptr<crypto::ECPrivateKey> key2; | 407 std::unique_ptr<crypto::ECPrivateKey> key2; |
| 407 TestCompletionCallback callback2; | 408 TestCompletionCallback callback2; |
| 408 ChannelIDService::Request request2; | 409 ChannelIDService::Request request2; |
| 409 | 410 |
| 410 std::string host3("bar.com"); | 411 std::string host3("bar.com"); |
| 411 scoped_ptr<crypto::ECPrivateKey> key3; | 412 std::unique_ptr<crypto::ECPrivateKey> key3; |
| 412 TestCompletionCallback callback3; | 413 TestCompletionCallback callback3; |
| 413 ChannelIDService::Request request3; | 414 ChannelIDService::Request request3; |
| 414 | 415 |
| 415 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(), | 416 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(), |
| 416 &request1); | 417 &request1); |
| 417 EXPECT_EQ(ERR_IO_PENDING, error); | 418 EXPECT_EQ(ERR_IO_PENDING, error); |
| 418 EXPECT_TRUE(request1.is_active()); | 419 EXPECT_TRUE(request1.is_active()); |
| 419 | 420 |
| 420 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(), | 421 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(), |
| 421 &request2); | 422 &request2); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 442 EXPECT_FALSE(KeysEqual(key1.get(), key2.get())); | 443 EXPECT_FALSE(KeysEqual(key1.get(), key2.get())); |
| 443 EXPECT_FALSE(KeysEqual(key1.get(), key3.get())); | 444 EXPECT_FALSE(KeysEqual(key1.get(), key3.get())); |
| 444 EXPECT_FALSE(KeysEqual(key2.get(), key3.get())); | 445 EXPECT_FALSE(KeysEqual(key2.get(), key3.get())); |
| 445 | 446 |
| 446 EXPECT_EQ(3, service_->channel_id_count()); | 447 EXPECT_EQ(3, service_->channel_id_count()); |
| 447 } | 448 } |
| 448 | 449 |
| 449 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { | 450 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { |
| 450 MockChannelIDStoreWithAsyncGet* mock_store = | 451 MockChannelIDStoreWithAsyncGet* mock_store = |
| 451 new MockChannelIDStoreWithAsyncGet(); | 452 new MockChannelIDStoreWithAsyncGet(); |
| 452 service_ = scoped_ptr<ChannelIDService>( | 453 service_ = std::unique_ptr<ChannelIDService>( |
| 453 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); | 454 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
| 454 | 455 |
| 455 std::string host("encrypted.google.com"); | 456 std::string host("encrypted.google.com"); |
| 456 | 457 |
| 457 int error; | 458 int error; |
| 458 TestCompletionCallback callback; | 459 TestCompletionCallback callback; |
| 459 ChannelIDService::Request request; | 460 ChannelIDService::Request request; |
| 460 | 461 |
| 461 // Asynchronous completion with no certs in the store. | 462 // Asynchronous completion with no certs in the store. |
| 462 scoped_ptr<crypto::ECPrivateKey> key; | 463 std::unique_ptr<crypto::ECPrivateKey> key; |
| 463 EXPECT_EQ(0, service_->channel_id_count()); | 464 EXPECT_EQ(0, service_->channel_id_count()); |
| 464 error = | 465 error = |
| 465 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request); | 466 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request); |
| 466 EXPECT_EQ(ERR_IO_PENDING, error); | 467 EXPECT_EQ(ERR_IO_PENDING, error); |
| 467 EXPECT_TRUE(request.is_active()); | 468 EXPECT_TRUE(request.is_active()); |
| 468 | 469 |
| 469 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); | 470 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); |
| 470 | 471 |
| 471 error = callback.WaitForResult(); | 472 error = callback.WaitForResult(); |
| 472 EXPECT_EQ(OK, error); | 473 EXPECT_EQ(OK, error); |
| 473 EXPECT_EQ(1, service_->channel_id_count()); | 474 EXPECT_EQ(1, service_->channel_id_count()); |
| 474 EXPECT_TRUE(key); | 475 EXPECT_TRUE(key); |
| 475 EXPECT_FALSE(request.is_active()); | 476 EXPECT_FALSE(request.is_active()); |
| 476 } | 477 } |
| 477 | 478 |
| 478 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { | 479 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { |
| 479 MockChannelIDStoreWithAsyncGet* mock_store = | 480 MockChannelIDStoreWithAsyncGet* mock_store = |
| 480 new MockChannelIDStoreWithAsyncGet(); | 481 new MockChannelIDStoreWithAsyncGet(); |
| 481 service_ = scoped_ptr<ChannelIDService>( | 482 service_ = std::unique_ptr<ChannelIDService>( |
| 482 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); | 483 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
| 483 | 484 |
| 484 std::string host("encrypted.google.com"); | 485 std::string host("encrypted.google.com"); |
| 485 | 486 |
| 486 int error; | 487 int error; |
| 487 TestCompletionCallback callback; | 488 TestCompletionCallback callback; |
| 488 ChannelIDService::Request request; | 489 ChannelIDService::Request request; |
| 489 | 490 |
| 490 // Asynchronous completion with no certs in the store. | 491 // Asynchronous completion with no certs in the store. |
| 491 scoped_ptr<crypto::ECPrivateKey> key; | 492 std::unique_ptr<crypto::ECPrivateKey> key; |
| 492 EXPECT_EQ(0, service_->channel_id_count()); | 493 EXPECT_EQ(0, service_->channel_id_count()); |
| 493 error = service_->GetChannelID(host, &key, callback.callback(), &request); | 494 error = service_->GetChannelID(host, &key, callback.callback(), &request); |
| 494 EXPECT_EQ(ERR_IO_PENDING, error); | 495 EXPECT_EQ(ERR_IO_PENDING, error); |
| 495 EXPECT_TRUE(request.is_active()); | 496 EXPECT_TRUE(request.is_active()); |
| 496 | 497 |
| 497 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); | 498 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); |
| 498 | 499 |
| 499 error = callback.WaitForResult(); | 500 error = callback.WaitForResult(); |
| 500 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); | 501 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); |
| 501 EXPECT_EQ(0, service_->channel_id_count()); | 502 EXPECT_EQ(0, service_->channel_id_count()); |
| 502 EXPECT_EQ(0u, service_->workers_created()); | 503 EXPECT_EQ(0u, service_->workers_created()); |
| 503 EXPECT_FALSE(key); | 504 EXPECT_FALSE(key); |
| 504 EXPECT_FALSE(request.is_active()); | 505 EXPECT_FALSE(request.is_active()); |
| 505 } | 506 } |
| 506 | 507 |
| 507 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { | 508 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { |
| 508 MockChannelIDStoreWithAsyncGet* mock_store = | 509 MockChannelIDStoreWithAsyncGet* mock_store = |
| 509 new MockChannelIDStoreWithAsyncGet(); | 510 new MockChannelIDStoreWithAsyncGet(); |
| 510 service_ = scoped_ptr<ChannelIDService>( | 511 service_ = std::unique_ptr<ChannelIDService>( |
| 511 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); | 512 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
| 512 | 513 |
| 513 std::string host("encrypted.google.com"); | 514 std::string host("encrypted.google.com"); |
| 514 | 515 |
| 515 int error; | 516 int error; |
| 516 TestCompletionCallback callback; | 517 TestCompletionCallback callback; |
| 517 ChannelIDService::Request request; | 518 ChannelIDService::Request request; |
| 518 | 519 |
| 519 // Asynchronous completion with a cert in the store. | 520 // Asynchronous completion with a cert in the store. |
| 520 scoped_ptr<crypto::ECPrivateKey> key; | 521 std::unique_ptr<crypto::ECPrivateKey> key; |
| 521 EXPECT_EQ(0, service_->channel_id_count()); | 522 EXPECT_EQ(0, service_->channel_id_count()); |
| 522 error = | 523 error = |
| 523 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request); | 524 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request); |
| 524 EXPECT_EQ(ERR_IO_PENDING, error); | 525 EXPECT_EQ(ERR_IO_PENDING, error); |
| 525 EXPECT_TRUE(request.is_active()); | 526 EXPECT_TRUE(request.is_active()); |
| 526 | 527 |
| 527 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); | 528 std::unique_ptr<crypto::ECPrivateKey> expected_key( |
| 529 crypto::ECPrivateKey::Create()); |
| 528 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); | 530 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); |
| 529 | 531 |
| 530 error = callback.WaitForResult(); | 532 error = callback.WaitForResult(); |
| 531 EXPECT_EQ(OK, error); | 533 EXPECT_EQ(OK, error); |
| 532 EXPECT_EQ(1, service_->channel_id_count()); | 534 EXPECT_EQ(1, service_->channel_id_count()); |
| 533 EXPECT_EQ(1u, service_->requests()); | 535 EXPECT_EQ(1u, service_->requests()); |
| 534 EXPECT_EQ(1u, service_->key_store_hits()); | 536 EXPECT_EQ(1u, service_->key_store_hits()); |
| 535 // Because the cert was found in the store, no new workers should have been | 537 // Because the cert was found in the store, no new workers should have been |
| 536 // created. | 538 // created. |
| 537 EXPECT_EQ(0u, service_->workers_created()); | 539 EXPECT_EQ(0u, service_->workers_created()); |
| 538 EXPECT_TRUE(key); | 540 EXPECT_TRUE(key); |
| 539 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); | 541 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); |
| 540 EXPECT_FALSE(request.is_active()); | 542 EXPECT_FALSE(request.is_active()); |
| 541 } | 543 } |
| 542 | 544 |
| 543 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { | 545 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { |
| 544 MockChannelIDStoreWithAsyncGet* mock_store = | 546 MockChannelIDStoreWithAsyncGet* mock_store = |
| 545 new MockChannelIDStoreWithAsyncGet(); | 547 new MockChannelIDStoreWithAsyncGet(); |
| 546 service_ = scoped_ptr<ChannelIDService>( | 548 service_ = std::unique_ptr<ChannelIDService>( |
| 547 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); | 549 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
| 548 | 550 |
| 549 std::string host("encrypted.google.com"); | 551 std::string host("encrypted.google.com"); |
| 550 | 552 |
| 551 int error; | 553 int error; |
| 552 TestCompletionCallback callback; | 554 TestCompletionCallback callback; |
| 553 ChannelIDService::Request request; | 555 ChannelIDService::Request request; |
| 554 | 556 |
| 555 // Asynchronous completion with a cert in the store. | 557 // Asynchronous completion with a cert in the store. |
| 556 scoped_ptr<crypto::ECPrivateKey> key; | 558 std::unique_ptr<crypto::ECPrivateKey> key; |
| 557 std::string private_key, spki; | 559 std::string private_key, spki; |
| 558 EXPECT_EQ(0, service_->channel_id_count()); | 560 EXPECT_EQ(0, service_->channel_id_count()); |
| 559 error = service_->GetChannelID(host, &key, callback.callback(), &request); | 561 error = service_->GetChannelID(host, &key, callback.callback(), &request); |
| 560 EXPECT_EQ(ERR_IO_PENDING, error); | 562 EXPECT_EQ(ERR_IO_PENDING, error); |
| 561 EXPECT_TRUE(request.is_active()); | 563 EXPECT_TRUE(request.is_active()); |
| 562 | 564 |
| 563 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); | 565 std::unique_ptr<crypto::ECPrivateKey> expected_key( |
| 566 crypto::ECPrivateKey::Create()); |
| 564 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); | 567 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); |
| 565 | 568 |
| 566 error = callback.WaitForResult(); | 569 error = callback.WaitForResult(); |
| 567 EXPECT_EQ(OK, error); | 570 EXPECT_EQ(OK, error); |
| 568 EXPECT_EQ(1, service_->channel_id_count()); | 571 EXPECT_EQ(1, service_->channel_id_count()); |
| 569 EXPECT_EQ(1u, service_->requests()); | 572 EXPECT_EQ(1u, service_->requests()); |
| 570 EXPECT_EQ(1u, service_->key_store_hits()); | 573 EXPECT_EQ(1u, service_->key_store_hits()); |
| 571 // Because the cert was found in the store, no new workers should have been | 574 // Because the cert was found in the store, no new workers should have been |
| 572 // created. | 575 // created. |
| 573 EXPECT_EQ(0u, service_->workers_created()); | 576 EXPECT_EQ(0u, service_->workers_created()); |
| 574 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); | 577 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); |
| 575 EXPECT_FALSE(request.is_active()); | 578 EXPECT_FALSE(request.is_active()); |
| 576 } | 579 } |
| 577 | 580 |
| 578 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { | 581 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { |
| 579 MockChannelIDStoreWithAsyncGet* mock_store = | 582 MockChannelIDStoreWithAsyncGet* mock_store = |
| 580 new MockChannelIDStoreWithAsyncGet(); | 583 new MockChannelIDStoreWithAsyncGet(); |
| 581 service_ = scoped_ptr<ChannelIDService>( | 584 service_ = std::unique_ptr<ChannelIDService>( |
| 582 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); | 585 new ChannelIDService(mock_store, base::ThreadTaskRunnerHandle::Get())); |
| 583 | 586 |
| 584 std::string host("encrypted.google.com"); | 587 std::string host("encrypted.google.com"); |
| 585 | 588 |
| 586 int error; | 589 int error; |
| 587 | 590 |
| 588 // Asynchronous get with no certs in the store. | 591 // Asynchronous get with no certs in the store. |
| 589 TestCompletionCallback callback1; | 592 TestCompletionCallback callback1; |
| 590 ChannelIDService::Request request1; | 593 ChannelIDService::Request request1; |
| 591 scoped_ptr<crypto::ECPrivateKey> key1; | 594 std::unique_ptr<crypto::ECPrivateKey> key1; |
| 592 EXPECT_EQ(0, service_->channel_id_count()); | 595 EXPECT_EQ(0, service_->channel_id_count()); |
| 593 error = service_->GetChannelID(host, &key1, callback1.callback(), &request1); | 596 error = service_->GetChannelID(host, &key1, callback1.callback(), &request1); |
| 594 EXPECT_EQ(ERR_IO_PENDING, error); | 597 EXPECT_EQ(ERR_IO_PENDING, error); |
| 595 EXPECT_TRUE(request1.is_active()); | 598 EXPECT_TRUE(request1.is_active()); |
| 596 | 599 |
| 597 // Asynchronous get/create with no certs in the store. | 600 // Asynchronous get/create with no certs in the store. |
| 598 TestCompletionCallback callback2; | 601 TestCompletionCallback callback2; |
| 599 ChannelIDService::Request request2; | 602 ChannelIDService::Request request2; |
| 600 scoped_ptr<crypto::ECPrivateKey> key2; | 603 std::unique_ptr<crypto::ECPrivateKey> key2; |
| 601 EXPECT_EQ(0, service_->channel_id_count()); | 604 EXPECT_EQ(0, service_->channel_id_count()); |
| 602 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), | 605 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), |
| 603 &request2); | 606 &request2); |
| 604 EXPECT_EQ(ERR_IO_PENDING, error); | 607 EXPECT_EQ(ERR_IO_PENDING, error); |
| 605 EXPECT_TRUE(request2.is_active()); | 608 EXPECT_TRUE(request2.is_active()); |
| 606 | 609 |
| 607 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); | 610 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); |
| 608 | 611 |
| 609 // Even though the first request didn't ask to create a cert, it gets joined | 612 // Even though the first request didn't ask to create a cert, it gets joined |
| 610 // by the second, which does, so both succeed. | 613 // by the second, which does, so both succeed. |
| 611 error = callback1.WaitForResult(); | 614 error = callback1.WaitForResult(); |
| 612 EXPECT_EQ(OK, error); | 615 EXPECT_EQ(OK, error); |
| 613 error = callback2.WaitForResult(); | 616 error = callback2.WaitForResult(); |
| 614 EXPECT_EQ(OK, error); | 617 EXPECT_EQ(OK, error); |
| 615 | 618 |
| 616 // One cert is created, one request is joined. | 619 // One cert is created, one request is joined. |
| 617 EXPECT_EQ(2U, service_->requests()); | 620 EXPECT_EQ(2U, service_->requests()); |
| 618 EXPECT_EQ(1, service_->channel_id_count()); | 621 EXPECT_EQ(1, service_->channel_id_count()); |
| 619 EXPECT_EQ(1u, service_->workers_created()); | 622 EXPECT_EQ(1u, service_->workers_created()); |
| 620 EXPECT_EQ(1u, service_->inflight_joins()); | 623 EXPECT_EQ(1u, service_->inflight_joins()); |
| 621 EXPECT_TRUE(key1); | 624 EXPECT_TRUE(key1); |
| 622 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); | 625 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); |
| 623 EXPECT_FALSE(request1.is_active()); | 626 EXPECT_FALSE(request1.is_active()); |
| 624 EXPECT_FALSE(request2.is_active()); | 627 EXPECT_FALSE(request2.is_active()); |
| 625 } | 628 } |
| 626 | 629 |
| 627 } // namespace | 630 } // namespace |
| 628 | 631 |
| 629 } // namespace net | 632 } // namespace net |
| OLD | NEW |