Chromium Code Reviews| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
| 15 #include "crypto/ec_private_key.h" | 16 #include "crypto/ec_private_key.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
| 18 #include "net/cert/asn1_util.h" | 19 #include "net/cert/asn1_util.h" |
| 19 #include "net/cert/x509_certificate.h" | 20 #include "net/cert/x509_certificate.h" |
| 20 #include "net/ssl/default_channel_id_store.h" | 21 #include "net/ssl/default_channel_id_store.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 namespace net { | 24 namespace net { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 50 DISALLOW_COPY_AND_ASSIGN(FailingTaskRunner); | 51 DISALLOW_COPY_AND_ASSIGN(FailingTaskRunner); |
| 51 }; | 52 }; |
| 52 | 53 |
| 53 class MockChannelIDStoreWithAsyncGet | 54 class MockChannelIDStoreWithAsyncGet |
| 54 : public DefaultChannelIDStore { | 55 : public DefaultChannelIDStore { |
| 55 public: | 56 public: |
| 56 MockChannelIDStoreWithAsyncGet() | 57 MockChannelIDStoreWithAsyncGet() |
| 57 : DefaultChannelIDStore(NULL), channel_id_count_(0) {} | 58 : DefaultChannelIDStore(NULL), channel_id_count_(0) {} |
| 58 | 59 |
| 59 int GetChannelID(const std::string& server_identifier, | 60 int GetChannelID(const std::string& server_identifier, |
| 60 base::Time* expiration_time, | 61 scoped_ptr<crypto::ECPrivateKey>* key_result, |
| 61 std::string* private_key_result, | |
| 62 std::string* cert_result, | |
| 63 const GetChannelIDCallback& callback) override; | 62 const GetChannelIDCallback& callback) override; |
| 64 | 63 |
| 65 void SetChannelID(const std::string& server_identifier, | 64 void SetChannelID(scoped_ptr<ChannelID> channel_id) override { |
| 66 base::Time creation_time, | |
| 67 base::Time expiration_time, | |
| 68 const std::string& private_key, | |
| 69 const std::string& cert) override { | |
| 70 channel_id_count_ = 1; | 65 channel_id_count_ = 1; |
| 71 } | 66 } |
| 72 | 67 |
| 73 int GetChannelIDCount() override { return channel_id_count_; } | 68 int GetChannelIDCount() override { return channel_id_count_; } |
| 74 | 69 |
| 75 void CallGetChannelIDCallbackWithResult(int err, | 70 void CallGetChannelIDCallbackWithResult(int err, crypto::ECPrivateKey* key); |
| 76 base::Time expiration_time, | |
| 77 const std::string& private_key, | |
| 78 const std::string& cert); | |
| 79 | 71 |
| 80 private: | 72 private: |
| 81 GetChannelIDCallback callback_; | 73 GetChannelIDCallback callback_; |
| 82 std::string server_identifier_; | 74 std::string server_identifier_; |
| 83 int channel_id_count_; | 75 int channel_id_count_; |
| 84 }; | 76 }; |
| 85 | 77 |
| 86 int MockChannelIDStoreWithAsyncGet::GetChannelID( | 78 int MockChannelIDStoreWithAsyncGet::GetChannelID( |
| 87 const std::string& server_identifier, | 79 const std::string& server_identifier, |
| 88 base::Time* expiration_time, | 80 scoped_ptr<crypto::ECPrivateKey>* key_result, |
| 89 std::string* private_key_result, | |
| 90 std::string* cert_result, | |
| 91 const GetChannelIDCallback& callback) { | 81 const GetChannelIDCallback& callback) { |
| 92 server_identifier_ = server_identifier; | 82 server_identifier_ = server_identifier; |
| 93 callback_ = callback; | 83 callback_ = callback; |
| 94 // Reset the cert count, it'll get incremented in either SetChannelID or | 84 // Reset the cert count, it'll get incremented in either SetChannelID or |
| 95 // CallGetChannelIDCallbackWithResult. | 85 // CallGetChannelIDCallbackWithResult. |
| 96 channel_id_count_ = 0; | 86 channel_id_count_ = 0; |
| 97 // Do nothing else: the results to be provided will be specified through | 87 // Do nothing else: the results to be provided will be specified through |
| 98 // CallGetChannelIDCallbackWithResult. | 88 // CallGetChannelIDCallbackWithResult. |
| 99 return ERR_IO_PENDING; | 89 return ERR_IO_PENDING; |
| 100 } | 90 } |
| 101 | 91 |
| 102 void | 92 void MockChannelIDStoreWithAsyncGet::CallGetChannelIDCallbackWithResult( |
| 103 MockChannelIDStoreWithAsyncGet::CallGetChannelIDCallbackWithResult( | |
| 104 int err, | 93 int err, |
| 105 base::Time expiration_time, | 94 crypto::ECPrivateKey* key) { |
| 106 const std::string& private_key, | |
| 107 const std::string& cert) { | |
| 108 if (err == OK) | 95 if (err == OK) |
| 109 channel_id_count_ = 1; | 96 channel_id_count_ = 1; |
| 110 base::MessageLoop::current()->PostTask(FROM_HERE, | 97 base::MessageLoop::current()->PostTask( |
|
Ryan Sleevi
2015/05/08 23:43:14
Future cleanup (with low hanging fruit) base::Thre
| |
| 111 base::Bind(callback_, | 98 FROM_HERE, base::Bind(callback_, err, server_identifier_, |
| 112 err, | 99 base::Passed(scoped_ptr<crypto::ECPrivateKey>( |
| 113 server_identifier_, | 100 key ? key->Copy() : nullptr)))); |
| 114 expiration_time, | |
| 115 private_key, | |
| 116 cert)); | |
| 117 } | 101 } |
| 118 | 102 |
| 119 class ChannelIDServiceTest : public testing::Test { | 103 class ChannelIDServiceTest : public testing::Test { |
| 120 public: | 104 public: |
| 121 ChannelIDServiceTest() | 105 ChannelIDServiceTest() |
| 122 : service_(new ChannelIDService( | 106 : service_(new ChannelIDService( |
| 123 new DefaultChannelIDStore(NULL), | 107 new DefaultChannelIDStore(NULL), |
| 124 base::MessageLoopProxy::current())) { | 108 base::MessageLoopProxy::current())) { |
| 125 } | 109 } |
| 126 | 110 |
| 127 protected: | 111 protected: |
| 128 scoped_ptr<ChannelIDService> service_; | 112 scoped_ptr<ChannelIDService> service_; |
| 129 }; | 113 }; |
| 130 | 114 |
| 115 void ExpectKeysEqual(const scoped_ptr<crypto::ECPrivateKey>& key1, | |
| 116 const scoped_ptr<crypto::ECPrivateKey>& key2) { | |
| 117 ASSERT_TRUE(key1); | |
| 118 ASSERT_TRUE(key2); | |
| 119 std::string public_key1, public_key2; | |
| 120 EXPECT_TRUE(key1->ExportRawPublicKey(&public_key1)); | |
| 121 EXPECT_TRUE(key2->ExportRawPublicKey(&public_key2)); | |
| 122 EXPECT_EQ(public_key1, public_key2); | |
| 123 } | |
| 124 | |
| 125 void ExpectKeysNotEqual(scoped_ptr<crypto::ECPrivateKey>& key1, | |
| 126 scoped_ptr<crypto::ECPrivateKey>& key2) { | |
| 127 ASSERT_TRUE(key1); | |
| 128 ASSERT_TRUE(key2); | |
| 129 std::string public_key1, public_key2; | |
| 130 EXPECT_TRUE(key1->ExportRawPublicKey(&public_key1)); | |
| 131 EXPECT_TRUE(key2->ExportRawPublicKey(&public_key2)); | |
| 132 EXPECT_NE(public_key1, public_key2); | |
|
Ryan Sleevi
2015/05/08 23:43:14
same comments re expect/assert in subroutines
nharper
2015/05/11 21:26:44
Done.
| |
| 133 } | |
| 134 | |
| 131 TEST_F(ChannelIDServiceTest, GetDomainForHost) { | 135 TEST_F(ChannelIDServiceTest, GetDomainForHost) { |
| 132 EXPECT_EQ("google.com", | 136 EXPECT_EQ("google.com", |
| 133 ChannelIDService::GetDomainForHost("google.com")); | 137 ChannelIDService::GetDomainForHost("google.com")); |
| 134 EXPECT_EQ("google.com", | 138 EXPECT_EQ("google.com", |
| 135 ChannelIDService::GetDomainForHost("www.google.com")); | 139 ChannelIDService::GetDomainForHost("www.google.com")); |
| 136 EXPECT_EQ("foo.appspot.com", | 140 EXPECT_EQ("foo.appspot.com", |
| 137 ChannelIDService::GetDomainForHost("foo.appspot.com")); | 141 ChannelIDService::GetDomainForHost("foo.appspot.com")); |
| 138 EXPECT_EQ("bar.appspot.com", | 142 EXPECT_EQ("bar.appspot.com", |
| 139 ChannelIDService::GetDomainForHost("foo.bar.appspot.com")); | 143 ChannelIDService::GetDomainForHost("foo.bar.appspot.com")); |
| 140 EXPECT_EQ("appspot.com", | 144 EXPECT_EQ("appspot.com", |
| 141 ChannelIDService::GetDomainForHost("appspot.com")); | 145 ChannelIDService::GetDomainForHost("appspot.com")); |
| 142 EXPECT_EQ("google.com", | 146 EXPECT_EQ("google.com", |
| 143 ChannelIDService::GetDomainForHost("www.mail.google.com")); | 147 ChannelIDService::GetDomainForHost("www.mail.google.com")); |
| 144 EXPECT_EQ("goto", | 148 EXPECT_EQ("goto", |
| 145 ChannelIDService::GetDomainForHost("goto")); | 149 ChannelIDService::GetDomainForHost("goto")); |
| 146 EXPECT_EQ("127.0.0.1", | 150 EXPECT_EQ("127.0.0.1", |
| 147 ChannelIDService::GetDomainForHost("127.0.0.1")); | 151 ChannelIDService::GetDomainForHost("127.0.0.1")); |
| 148 } | 152 } |
| 149 | 153 |
| 150 TEST_F(ChannelIDServiceTest, GetCacheMiss) { | 154 TEST_F(ChannelIDServiceTest, GetCacheMiss) { |
| 151 std::string host("encrypted.google.com"); | 155 std::string host("encrypted.google.com"); |
| 152 | 156 |
| 153 int error; | 157 int error; |
| 154 TestCompletionCallback callback; | 158 TestCompletionCallback callback; |
| 155 ChannelIDService::RequestHandle request_handle; | 159 ChannelIDService::RequestHandle request_handle; |
| 156 | 160 |
| 157 // Synchronous completion, because the store is initialized. | 161 // Synchronous completion, because the store is initialized. |
| 158 std::string private_key, der_cert; | 162 scoped_ptr<crypto::ECPrivateKey> key; |
| 159 EXPECT_EQ(0, service_->cert_count()); | 163 EXPECT_EQ(0, service_->cert_count()); |
| 160 error = service_->GetChannelID( | 164 error = |
| 161 host, &private_key, &der_cert, callback.callback(), &request_handle); | 165 service_->GetChannelID(host, &key, callback.callback(), &request_handle); |
| 162 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); | 166 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); |
| 163 EXPECT_FALSE(request_handle.is_active()); | 167 EXPECT_FALSE(request_handle.is_active()); |
| 164 EXPECT_EQ(0, service_->cert_count()); | 168 EXPECT_EQ(0, service_->cert_count()); |
| 165 EXPECT_TRUE(der_cert.empty()); | 169 EXPECT_FALSE(key); |
| 166 } | 170 } |
| 167 | 171 |
| 168 TEST_F(ChannelIDServiceTest, CacheHit) { | 172 TEST_F(ChannelIDServiceTest, CacheHit) { |
| 169 std::string host("encrypted.google.com"); | 173 std::string host("encrypted.google.com"); |
| 170 | 174 |
| 171 int error; | 175 int error; |
| 172 TestCompletionCallback callback; | 176 TestCompletionCallback callback; |
| 173 ChannelIDService::RequestHandle request_handle; | 177 ChannelIDService::RequestHandle request_handle; |
| 174 | 178 |
| 175 // Asynchronous completion. | 179 // Asynchronous completion. |
| 176 std::string private_key_info1, der_cert1; | 180 scoped_ptr<crypto::ECPrivateKey> key1; |
| 177 EXPECT_EQ(0, service_->cert_count()); | 181 EXPECT_EQ(0, service_->cert_count()); |
| 178 error = service_->GetOrCreateChannelID( | 182 error = service_->GetOrCreateChannelID(host, &key1, callback.callback(), |
| 179 host, &private_key_info1, &der_cert1, | 183 &request_handle); |
| 180 callback.callback(), &request_handle); | |
| 181 EXPECT_EQ(ERR_IO_PENDING, error); | 184 EXPECT_EQ(ERR_IO_PENDING, error); |
| 182 EXPECT_TRUE(request_handle.is_active()); | 185 EXPECT_TRUE(request_handle.is_active()); |
| 183 error = callback.WaitForResult(); | 186 error = callback.WaitForResult(); |
| 184 EXPECT_EQ(OK, error); | 187 EXPECT_EQ(OK, error); |
| 185 EXPECT_EQ(1, service_->cert_count()); | 188 EXPECT_EQ(1, service_->cert_count()); |
| 186 EXPECT_FALSE(private_key_info1.empty()); | 189 EXPECT_TRUE(key1); |
| 187 EXPECT_FALSE(der_cert1.empty()); | |
| 188 EXPECT_FALSE(request_handle.is_active()); | 190 EXPECT_FALSE(request_handle.is_active()); |
| 189 | 191 |
| 190 // Synchronous completion. | 192 // Synchronous completion. |
| 191 std::string private_key_info2, der_cert2; | 193 scoped_ptr<crypto::ECPrivateKey> key2; |
| 192 error = service_->GetOrCreateChannelID( | 194 error = service_->GetOrCreateChannelID(host, &key2, callback.callback(), |
| 193 host, &private_key_info2, &der_cert2, | 195 &request_handle); |
| 194 callback.callback(), &request_handle); | |
| 195 EXPECT_FALSE(request_handle.is_active()); | 196 EXPECT_FALSE(request_handle.is_active()); |
| 196 EXPECT_EQ(OK, error); | 197 EXPECT_EQ(OK, error); |
| 197 EXPECT_EQ(1, service_->cert_count()); | 198 EXPECT_EQ(1, service_->cert_count()); |
| 198 EXPECT_EQ(private_key_info1, private_key_info2); | 199 ExpectKeysEqual(key1, key2); |
| 199 EXPECT_EQ(der_cert1, der_cert2); | |
| 200 | 200 |
| 201 // Synchronous get. | 201 // Synchronous get. |
| 202 std::string private_key_info3, der_cert3; | 202 scoped_ptr<crypto::ECPrivateKey> key3; |
| 203 error = service_->GetChannelID( | 203 error = |
| 204 host, &private_key_info3, &der_cert3, callback.callback(), | 204 service_->GetChannelID(host, &key3, callback.callback(), &request_handle); |
| 205 &request_handle); | |
| 206 EXPECT_FALSE(request_handle.is_active()); | 205 EXPECT_FALSE(request_handle.is_active()); |
| 207 EXPECT_EQ(OK, error); | 206 EXPECT_EQ(OK, error); |
| 208 EXPECT_EQ(1, service_->cert_count()); | 207 EXPECT_EQ(1, service_->cert_count()); |
| 209 EXPECT_EQ(der_cert1, der_cert3); | 208 ExpectKeysEqual(key1, key3); |
| 210 EXPECT_EQ(private_key_info1, private_key_info3); | |
| 211 | 209 |
| 212 EXPECT_EQ(3u, service_->requests()); | 210 EXPECT_EQ(3u, service_->requests()); |
| 213 EXPECT_EQ(2u, service_->cert_store_hits()); | 211 EXPECT_EQ(2u, service_->key_store_hits()); |
| 214 EXPECT_EQ(0u, service_->inflight_joins()); | 212 EXPECT_EQ(0u, service_->inflight_joins()); |
| 215 } | 213 } |
| 216 | 214 |
| 217 TEST_F(ChannelIDServiceTest, StoreChannelIDs) { | 215 TEST_F(ChannelIDServiceTest, StoreChannelIDs) { |
| 218 int error; | 216 int error; |
| 219 TestCompletionCallback callback; | 217 TestCompletionCallback callback; |
| 220 ChannelIDService::RequestHandle request_handle; | 218 ChannelIDService::RequestHandle request_handle; |
| 221 | 219 |
| 222 std::string host1("encrypted.google.com"); | 220 std::string host1("encrypted.google.com"); |
| 223 std::string private_key_info1, der_cert1; | 221 scoped_ptr<crypto::ECPrivateKey> key1; |
| 224 EXPECT_EQ(0, service_->cert_count()); | 222 EXPECT_EQ(0, service_->cert_count()); |
| 225 error = service_->GetOrCreateChannelID( | 223 error = service_->GetOrCreateChannelID(host1, &key1, callback.callback(), |
| 226 host1, &private_key_info1, &der_cert1, | 224 &request_handle); |
| 227 callback.callback(), &request_handle); | |
| 228 EXPECT_EQ(ERR_IO_PENDING, error); | 225 EXPECT_EQ(ERR_IO_PENDING, error); |
| 229 EXPECT_TRUE(request_handle.is_active()); | 226 EXPECT_TRUE(request_handle.is_active()); |
| 230 error = callback.WaitForResult(); | 227 error = callback.WaitForResult(); |
| 231 EXPECT_EQ(OK, error); | 228 EXPECT_EQ(OK, error); |
| 232 EXPECT_EQ(1, service_->cert_count()); | 229 EXPECT_EQ(1, service_->cert_count()); |
| 233 | 230 |
| 234 std::string host2("www.verisign.com"); | 231 std::string host2("www.verisign.com"); |
| 235 std::string private_key_info2, der_cert2; | 232 scoped_ptr<crypto::ECPrivateKey> key2; |
| 236 error = service_->GetOrCreateChannelID( | 233 error = service_->GetOrCreateChannelID(host2, &key2, callback.callback(), |
| 237 host2, &private_key_info2, &der_cert2, | 234 &request_handle); |
| 238 callback.callback(), &request_handle); | |
| 239 EXPECT_EQ(ERR_IO_PENDING, error); | 235 EXPECT_EQ(ERR_IO_PENDING, error); |
| 240 EXPECT_TRUE(request_handle.is_active()); | 236 EXPECT_TRUE(request_handle.is_active()); |
| 241 error = callback.WaitForResult(); | 237 error = callback.WaitForResult(); |
| 242 EXPECT_EQ(OK, error); | 238 EXPECT_EQ(OK, error); |
| 243 EXPECT_EQ(2, service_->cert_count()); | 239 EXPECT_EQ(2, service_->cert_count()); |
| 244 | 240 |
| 245 std::string host3("www.twitter.com"); | 241 std::string host3("www.twitter.com"); |
| 246 std::string private_key_info3, der_cert3; | 242 scoped_ptr<crypto::ECPrivateKey> key3; |
| 247 error = service_->GetOrCreateChannelID( | 243 error = service_->GetOrCreateChannelID(host3, &key3, callback.callback(), |
| 248 host3, &private_key_info3, &der_cert3, | 244 &request_handle); |
| 249 callback.callback(), &request_handle); | |
| 250 EXPECT_EQ(ERR_IO_PENDING, error); | 245 EXPECT_EQ(ERR_IO_PENDING, error); |
| 251 EXPECT_TRUE(request_handle.is_active()); | 246 EXPECT_TRUE(request_handle.is_active()); |
| 252 error = callback.WaitForResult(); | 247 error = callback.WaitForResult(); |
| 253 EXPECT_EQ(OK, error); | 248 EXPECT_EQ(OK, error); |
| 254 EXPECT_EQ(3, service_->cert_count()); | 249 EXPECT_EQ(3, service_->cert_count()); |
| 255 | 250 |
| 256 EXPECT_NE(private_key_info1, private_key_info2); | 251 ExpectKeysNotEqual(key1, key2); |
| 257 EXPECT_NE(der_cert1, der_cert2); | 252 ExpectKeysNotEqual(key1, key3); |
| 258 EXPECT_NE(private_key_info1, private_key_info3); | 253 ExpectKeysNotEqual(key2, key3); |
| 259 EXPECT_NE(der_cert1, der_cert3); | |
| 260 EXPECT_NE(private_key_info2, private_key_info3); | |
| 261 EXPECT_NE(der_cert2, der_cert3); | |
| 262 } | 254 } |
| 263 | 255 |
| 264 // Tests an inflight join. | 256 // Tests an inflight join. |
| 265 TEST_F(ChannelIDServiceTest, InflightJoin) { | 257 TEST_F(ChannelIDServiceTest, InflightJoin) { |
| 266 std::string host("encrypted.google.com"); | 258 std::string host("encrypted.google.com"); |
| 267 int error; | 259 int error; |
| 268 | 260 |
| 269 std::string private_key_info1, der_cert1; | 261 scoped_ptr<crypto::ECPrivateKey> key1; |
| 270 TestCompletionCallback callback1; | 262 TestCompletionCallback callback1; |
| 271 ChannelIDService::RequestHandle request_handle1; | 263 ChannelIDService::RequestHandle request_handle1; |
| 272 | 264 |
| 273 std::string private_key_info2, der_cert2; | 265 scoped_ptr<crypto::ECPrivateKey> key2; |
| 274 TestCompletionCallback callback2; | 266 TestCompletionCallback callback2; |
| 275 ChannelIDService::RequestHandle request_handle2; | 267 ChannelIDService::RequestHandle request_handle2; |
| 276 | 268 |
| 277 error = service_->GetOrCreateChannelID( | 269 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), |
| 278 host, &private_key_info1, &der_cert1, | 270 &request_handle1); |
| 279 callback1.callback(), &request_handle1); | |
| 280 EXPECT_EQ(ERR_IO_PENDING, error); | 271 EXPECT_EQ(ERR_IO_PENDING, error); |
| 281 EXPECT_TRUE(request_handle1.is_active()); | 272 EXPECT_TRUE(request_handle1.is_active()); |
| 282 // Should join with the original request. | 273 // Should join with the original request. |
| 283 error = service_->GetOrCreateChannelID( | 274 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), |
| 284 host, &private_key_info2, &der_cert2, | 275 &request_handle2); |
| 285 callback2.callback(), &request_handle2); | |
| 286 EXPECT_EQ(ERR_IO_PENDING, error); | 276 EXPECT_EQ(ERR_IO_PENDING, error); |
| 287 EXPECT_TRUE(request_handle2.is_active()); | 277 EXPECT_TRUE(request_handle2.is_active()); |
| 288 | 278 |
| 289 error = callback1.WaitForResult(); | 279 error = callback1.WaitForResult(); |
| 290 EXPECT_EQ(OK, error); | 280 EXPECT_EQ(OK, error); |
| 291 error = callback2.WaitForResult(); | 281 error = callback2.WaitForResult(); |
| 292 EXPECT_EQ(OK, error); | 282 EXPECT_EQ(OK, error); |
| 293 | 283 |
| 294 EXPECT_EQ(2u, service_->requests()); | 284 EXPECT_EQ(2u, service_->requests()); |
| 295 EXPECT_EQ(0u, service_->cert_store_hits()); | 285 EXPECT_EQ(0u, service_->key_store_hits()); |
| 296 EXPECT_EQ(1u, service_->inflight_joins()); | 286 EXPECT_EQ(1u, service_->inflight_joins()); |
| 297 EXPECT_EQ(1u, service_->workers_created()); | 287 EXPECT_EQ(1u, service_->workers_created()); |
| 298 } | 288 } |
| 299 | 289 |
| 300 // Tests an inflight join of a Get request to a GetOrCreate request. | 290 // Tests an inflight join of a Get request to a GetOrCreate request. |
| 301 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) { | 291 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) { |
| 302 std::string host("encrypted.google.com"); | 292 std::string host("encrypted.google.com"); |
| 303 int error; | 293 int error; |
| 304 | 294 |
| 305 std::string private_key_info1, der_cert1; | 295 scoped_ptr<crypto::ECPrivateKey> key1; |
| 306 TestCompletionCallback callback1; | 296 TestCompletionCallback callback1; |
| 307 ChannelIDService::RequestHandle request_handle1; | 297 ChannelIDService::RequestHandle request_handle1; |
| 308 | 298 |
| 309 std::string private_key_info2; | 299 scoped_ptr<crypto::ECPrivateKey> key2; |
| 310 std::string der_cert2; | |
| 311 TestCompletionCallback callback2; | 300 TestCompletionCallback callback2; |
| 312 ChannelIDService::RequestHandle request_handle2; | 301 ChannelIDService::RequestHandle request_handle2; |
| 313 | 302 |
| 314 error = service_->GetOrCreateChannelID( | 303 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), |
| 315 host, &private_key_info1, &der_cert1, | 304 &request_handle1); |
| 316 callback1.callback(), &request_handle1); | |
| 317 EXPECT_EQ(ERR_IO_PENDING, error); | 305 EXPECT_EQ(ERR_IO_PENDING, error); |
| 318 EXPECT_TRUE(request_handle1.is_active()); | 306 EXPECT_TRUE(request_handle1.is_active()); |
| 319 // Should join with the original request. | 307 // Should join with the original request. |
| 320 error = service_->GetChannelID( | 308 error = service_->GetChannelID(host, &key2, callback2.callback(), |
| 321 host, &private_key_info2, &der_cert2, callback2.callback(), | 309 &request_handle2); |
| 322 &request_handle2); | |
| 323 EXPECT_EQ(ERR_IO_PENDING, error); | 310 EXPECT_EQ(ERR_IO_PENDING, error); |
| 324 EXPECT_TRUE(request_handle2.is_active()); | 311 EXPECT_TRUE(request_handle2.is_active()); |
| 325 | 312 |
| 326 error = callback1.WaitForResult(); | 313 error = callback1.WaitForResult(); |
| 327 EXPECT_EQ(OK, error); | 314 EXPECT_EQ(OK, error); |
| 328 error = callback2.WaitForResult(); | 315 error = callback2.WaitForResult(); |
| 329 EXPECT_EQ(OK, error); | 316 EXPECT_EQ(OK, error); |
| 330 EXPECT_EQ(der_cert1, der_cert2); | 317 ExpectKeysEqual(key1, key2); |
| 331 | 318 |
| 332 EXPECT_EQ(2u, service_->requests()); | 319 EXPECT_EQ(2u, service_->requests()); |
| 333 EXPECT_EQ(0u, service_->cert_store_hits()); | 320 EXPECT_EQ(0u, service_->key_store_hits()); |
| 334 EXPECT_EQ(1u, service_->inflight_joins()); | 321 EXPECT_EQ(1u, service_->inflight_joins()); |
| 335 EXPECT_EQ(1u, service_->workers_created()); | 322 EXPECT_EQ(1u, service_->workers_created()); |
| 336 } | 323 } |
| 337 | 324 |
| 338 TEST_F(ChannelIDServiceTest, ExtractValuesFromBytesEC) { | |
| 339 std::string host("encrypted.google.com"); | |
| 340 std::string private_key_info, der_cert; | |
| 341 int error; | |
| 342 TestCompletionCallback callback; | |
| 343 ChannelIDService::RequestHandle request_handle; | |
| 344 | |
| 345 error = service_->GetOrCreateChannelID( | |
| 346 host, &private_key_info, &der_cert, callback.callback(), | |
| 347 &request_handle); | |
| 348 EXPECT_EQ(ERR_IO_PENDING, error); | |
| 349 EXPECT_TRUE(request_handle.is_active()); | |
| 350 error = callback.WaitForResult(); | |
| 351 EXPECT_EQ(OK, error); | |
| 352 | |
| 353 base::StringPiece spki_piece; | |
| 354 ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(der_cert, &spki_piece)); | |
| 355 std::vector<uint8> spki( | |
| 356 spki_piece.data(), | |
| 357 spki_piece.data() + spki_piece.size()); | |
| 358 | |
| 359 // Check that we can retrieve the key from the bytes. | |
| 360 std::vector<uint8> key_vec(private_key_info.begin(), private_key_info.end()); | |
| 361 scoped_ptr<crypto::ECPrivateKey> private_key( | |
| 362 crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | |
| 363 ChannelIDService::kEPKIPassword, key_vec, spki)); | |
| 364 EXPECT_TRUE(private_key != NULL); | |
| 365 | |
| 366 // Check that we can retrieve the cert from the bytes. | |
| 367 scoped_refptr<X509Certificate> x509cert( | |
| 368 X509Certificate::CreateFromBytes(der_cert.data(), der_cert.size())); | |
| 369 EXPECT_TRUE(x509cert.get() != NULL); | |
| 370 } | |
| 371 | |
| 372 // Tests that the callback of a canceled request is never made. | 325 // Tests that the callback of a canceled request is never made. |
| 373 TEST_F(ChannelIDServiceTest, CancelRequest) { | 326 TEST_F(ChannelIDServiceTest, CancelRequest) { |
| 374 std::string host("encrypted.google.com"); | 327 std::string host("encrypted.google.com"); |
| 375 std::string private_key_info, der_cert; | 328 scoped_ptr<crypto::ECPrivateKey> key; |
| 376 int error; | 329 int error; |
| 377 ChannelIDService::RequestHandle request_handle; | 330 ChannelIDService::RequestHandle request_handle; |
| 378 | 331 |
| 379 error = service_->GetOrCreateChannelID(host, | 332 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 380 &private_key_info, | |
| 381 &der_cert, | |
| 382 base::Bind(&FailTest), | |
| 383 &request_handle); | 333 &request_handle); |
| 384 EXPECT_EQ(ERR_IO_PENDING, error); | 334 EXPECT_EQ(ERR_IO_PENDING, error); |
| 385 EXPECT_TRUE(request_handle.is_active()); | 335 EXPECT_TRUE(request_handle.is_active()); |
| 386 request_handle.Cancel(); | 336 request_handle.Cancel(); |
| 387 EXPECT_FALSE(request_handle.is_active()); | 337 EXPECT_FALSE(request_handle.is_active()); |
| 388 | 338 |
| 389 // Wait for reply from ChannelIDServiceWorker to be posted back to the | 339 // Wait for reply from ChannelIDServiceWorker to be posted back to the |
| 390 // ChannelIDService. | 340 // ChannelIDService. |
| 391 base::MessageLoop::current()->RunUntilIdle(); | 341 base::MessageLoop::current()->RunUntilIdle(); |
| 392 | 342 |
| 393 // Even though the original request was cancelled, the service will still | 343 // Even though the original request was cancelled, the service will still |
| 394 // store the result, it just doesn't call the callback. | 344 // store the result, it just doesn't call the callback. |
| 395 EXPECT_EQ(1, service_->cert_count()); | 345 EXPECT_EQ(1, service_->cert_count()); |
| 396 } | 346 } |
| 397 | 347 |
| 398 // Tests that destructing the RequestHandle cancels the request. | 348 // Tests that destructing the RequestHandle cancels the request. |
| 399 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) { | 349 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) { |
| 400 std::string host("encrypted.google.com"); | 350 std::string host("encrypted.google.com"); |
| 401 std::string private_key_info, der_cert; | 351 scoped_ptr<crypto::ECPrivateKey> key; |
| 402 int error; | 352 int error; |
| 403 { | 353 { |
| 404 ChannelIDService::RequestHandle request_handle; | 354 ChannelIDService::RequestHandle request_handle; |
| 405 | 355 |
| 406 error = service_->GetOrCreateChannelID(host, | 356 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 407 &private_key_info, | |
| 408 &der_cert, | |
| 409 base::Bind(&FailTest), | |
| 410 &request_handle); | 357 &request_handle); |
| 411 EXPECT_EQ(ERR_IO_PENDING, error); | 358 EXPECT_EQ(ERR_IO_PENDING, error); |
| 412 EXPECT_TRUE(request_handle.is_active()); | 359 EXPECT_TRUE(request_handle.is_active()); |
| 413 } | 360 } |
| 414 | 361 |
| 415 // Wait for reply from ChannelIDServiceWorker to be posted back to the | 362 // Wait for reply from ChannelIDServiceWorker to be posted back to the |
| 416 // ChannelIDService. | 363 // ChannelIDService. |
| 417 base::MessageLoop::current()->RunUntilIdle(); | 364 base::MessageLoop::current()->RunUntilIdle(); |
| 418 | 365 |
| 419 // Even though the original request was cancelled, the service will still | 366 // Even though the original request was cancelled, the service will still |
| 420 // store the result, it just doesn't call the callback. | 367 // store the result, it just doesn't call the callback. |
| 421 EXPECT_EQ(1, service_->cert_count()); | 368 EXPECT_EQ(1, service_->cert_count()); |
| 422 } | 369 } |
| 423 | 370 |
| 424 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { | 371 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { |
| 425 std::string host("encrypted.google.com"); | 372 std::string host("encrypted.google.com"); |
| 426 std::string private_key_info, der_cert; | 373 scoped_ptr<crypto::ECPrivateKey> key; |
| 427 int error; | 374 int error; |
| 428 ChannelIDService::RequestHandle request_handle; | 375 ChannelIDService::RequestHandle request_handle; |
| 429 | 376 |
| 430 error = service_->GetOrCreateChannelID(host, | 377 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 431 &private_key_info, | |
| 432 &der_cert, | |
| 433 base::Bind(&FailTest), | |
| 434 &request_handle); | 378 &request_handle); |
| 435 EXPECT_EQ(ERR_IO_PENDING, error); | 379 EXPECT_EQ(ERR_IO_PENDING, error); |
| 436 EXPECT_TRUE(request_handle.is_active()); | 380 EXPECT_TRUE(request_handle.is_active()); |
| 437 | 381 |
| 438 // Cancel request and destroy the ChannelIDService. | 382 // Cancel request and destroy the ChannelIDService. |
| 439 request_handle.Cancel(); | 383 request_handle.Cancel(); |
| 440 service_.reset(); | 384 service_.reset(); |
| 441 | 385 |
| 442 // ChannelIDServiceWorker should not post anything back to the | 386 // ChannelIDServiceWorker should not post anything back to the |
| 443 // non-existent ChannelIDService, but run the loop just to be sure it | 387 // non-existent ChannelIDService, but run the loop just to be sure it |
| 444 // doesn't. | 388 // doesn't. |
| 445 base::MessageLoop::current()->RunUntilIdle(); | 389 base::MessageLoop::current()->RunUntilIdle(); |
| 446 | 390 |
| 447 // If we got here without crashing or a valgrind error, it worked. | 391 // If we got here without crashing or a valgrind error, it worked. |
| 448 } | 392 } |
| 449 | 393 |
| 450 // Tests that shutting down the sequenced worker pool and then making new | 394 // Tests that shutting down the sequenced worker pool and then making new |
| 451 // requests gracefully fails. | 395 // requests gracefully fails. |
| 452 // This is a regression test for http://crbug.com/236387 | 396 // This is a regression test for http://crbug.com/236387 |
| 453 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { | 397 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { |
| 454 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner); | 398 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner); |
| 455 service_.reset(new ChannelIDService( | 399 service_.reset(new ChannelIDService( |
| 456 new DefaultChannelIDStore(NULL), task_runner)); | 400 new DefaultChannelIDStore(NULL), task_runner)); |
| 457 | 401 |
| 458 // Make a request that will force synchronous completion. | 402 // Make a request that will force synchronous completion. |
| 459 std::string host("encrypted.google.com"); | 403 std::string host("encrypted.google.com"); |
| 460 std::string private_key_info, der_cert; | 404 scoped_ptr<crypto::ECPrivateKey> key; |
| 461 int error; | 405 int error; |
| 462 ChannelIDService::RequestHandle request_handle; | 406 ChannelIDService::RequestHandle request_handle; |
| 463 | 407 |
| 464 error = service_->GetOrCreateChannelID(host, | 408 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 465 &private_key_info, | |
| 466 &der_cert, | |
| 467 base::Bind(&FailTest), | |
| 468 &request_handle); | 409 &request_handle); |
| 469 // If we got here without crashing or a valgrind error, it worked. | 410 // If we got here without crashing or a valgrind error, it worked. |
| 470 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error); | 411 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error); |
| 471 EXPECT_FALSE(request_handle.is_active()); | 412 EXPECT_FALSE(request_handle.is_active()); |
| 472 } | 413 } |
| 473 | 414 |
| 474 // Tests that simultaneous creation of different certs works. | 415 // Tests that simultaneous creation of different certs works. |
| 475 TEST_F(ChannelIDServiceTest, SimultaneousCreation) { | 416 TEST_F(ChannelIDServiceTest, SimultaneousCreation) { |
| 476 int error; | 417 int error; |
| 477 | 418 |
| 478 std::string host1("encrypted.google.com"); | 419 std::string host1("encrypted.google.com"); |
| 479 std::string private_key_info1, der_cert1; | 420 scoped_ptr<crypto::ECPrivateKey> key1; |
| 480 TestCompletionCallback callback1; | 421 TestCompletionCallback callback1; |
| 481 ChannelIDService::RequestHandle request_handle1; | 422 ChannelIDService::RequestHandle request_handle1; |
| 482 | 423 |
| 483 std::string host2("foo.com"); | 424 std::string host2("foo.com"); |
| 484 std::string private_key_info2, der_cert2; | 425 scoped_ptr<crypto::ECPrivateKey> key2; |
| 485 TestCompletionCallback callback2; | 426 TestCompletionCallback callback2; |
| 486 ChannelIDService::RequestHandle request_handle2; | 427 ChannelIDService::RequestHandle request_handle2; |
| 487 | 428 |
| 488 std::string host3("bar.com"); | 429 std::string host3("bar.com"); |
| 489 std::string private_key_info3, der_cert3; | 430 scoped_ptr<crypto::ECPrivateKey> key3; |
| 490 TestCompletionCallback callback3; | 431 TestCompletionCallback callback3; |
| 491 ChannelIDService::RequestHandle request_handle3; | 432 ChannelIDService::RequestHandle request_handle3; |
| 492 | 433 |
| 493 error = service_->GetOrCreateChannelID(host1, | 434 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(), |
| 494 &private_key_info1, | |
| 495 &der_cert1, | |
| 496 callback1.callback(), | |
| 497 &request_handle1); | 435 &request_handle1); |
| 498 EXPECT_EQ(ERR_IO_PENDING, error); | 436 EXPECT_EQ(ERR_IO_PENDING, error); |
| 499 EXPECT_TRUE(request_handle1.is_active()); | 437 EXPECT_TRUE(request_handle1.is_active()); |
| 500 | 438 |
| 501 error = service_->GetOrCreateChannelID(host2, | 439 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(), |
| 502 &private_key_info2, | |
| 503 &der_cert2, | |
| 504 callback2.callback(), | |
| 505 &request_handle2); | 440 &request_handle2); |
| 506 EXPECT_EQ(ERR_IO_PENDING, error); | 441 EXPECT_EQ(ERR_IO_PENDING, error); |
| 507 EXPECT_TRUE(request_handle2.is_active()); | 442 EXPECT_TRUE(request_handle2.is_active()); |
| 508 | 443 |
| 509 error = service_->GetOrCreateChannelID(host3, | 444 error = service_->GetOrCreateChannelID(host3, &key3, callback3.callback(), |
| 510 &private_key_info3, | |
| 511 &der_cert3, | |
| 512 callback3.callback(), | |
| 513 &request_handle3); | 445 &request_handle3); |
| 514 EXPECT_EQ(ERR_IO_PENDING, error); | 446 EXPECT_EQ(ERR_IO_PENDING, error); |
| 515 EXPECT_TRUE(request_handle3.is_active()); | 447 EXPECT_TRUE(request_handle3.is_active()); |
| 516 | 448 |
| 517 error = callback1.WaitForResult(); | 449 error = callback1.WaitForResult(); |
| 518 EXPECT_EQ(OK, error); | 450 EXPECT_EQ(OK, error); |
| 519 EXPECT_FALSE(private_key_info1.empty()); | 451 EXPECT_TRUE(key1); |
| 520 EXPECT_FALSE(der_cert1.empty()); | |
| 521 | 452 |
| 522 error = callback2.WaitForResult(); | 453 error = callback2.WaitForResult(); |
| 523 EXPECT_EQ(OK, error); | 454 EXPECT_EQ(OK, error); |
| 524 EXPECT_FALSE(private_key_info2.empty()); | 455 EXPECT_TRUE(key2); |
| 525 EXPECT_FALSE(der_cert2.empty()); | |
| 526 | 456 |
| 527 error = callback3.WaitForResult(); | 457 error = callback3.WaitForResult(); |
| 528 EXPECT_EQ(OK, error); | 458 EXPECT_EQ(OK, error); |
| 529 EXPECT_FALSE(private_key_info3.empty()); | 459 EXPECT_TRUE(key3); |
| 530 EXPECT_FALSE(der_cert3.empty()); | |
| 531 | 460 |
| 532 EXPECT_NE(private_key_info1, private_key_info2); | 461 ExpectKeysNotEqual(key1, key2); |
| 533 EXPECT_NE(der_cert1, der_cert2); | 462 ExpectKeysNotEqual(key1, key3); |
| 534 | 463 ExpectKeysNotEqual(key2, key3); |
| 535 EXPECT_NE(private_key_info1, private_key_info3); | |
| 536 EXPECT_NE(der_cert1, der_cert3); | |
| 537 | |
| 538 EXPECT_NE(private_key_info2, private_key_info3); | |
| 539 EXPECT_NE(der_cert2, der_cert3); | |
| 540 | 464 |
| 541 EXPECT_EQ(3, service_->cert_count()); | 465 EXPECT_EQ(3, service_->cert_count()); |
| 542 } | 466 } |
| 543 | 467 |
| 544 TEST_F(ChannelIDServiceTest, Expiration) { | |
| 545 ChannelIDStore* store = service_->GetChannelIDStore(); | |
| 546 base::Time now = base::Time::Now(); | |
| 547 store->SetChannelID("good", | |
| 548 now, | |
| 549 now + base::TimeDelta::FromDays(1), | |
| 550 "a", | |
| 551 "b"); | |
| 552 store->SetChannelID("expired", | |
| 553 now - base::TimeDelta::FromDays(2), | |
| 554 now - base::TimeDelta::FromDays(1), | |
| 555 "c", | |
| 556 "d"); | |
| 557 EXPECT_EQ(2, service_->cert_count()); | |
| 558 | |
| 559 int error; | |
| 560 TestCompletionCallback callback; | |
| 561 ChannelIDService::RequestHandle request_handle; | |
| 562 | |
| 563 // Cert is valid - synchronous completion. | |
| 564 std::string private_key_info1, der_cert1; | |
| 565 error = service_->GetOrCreateChannelID( | |
| 566 "good", &private_key_info1, &der_cert1, | |
| 567 callback.callback(), &request_handle); | |
| 568 EXPECT_EQ(OK, error); | |
| 569 EXPECT_FALSE(request_handle.is_active()); | |
| 570 EXPECT_EQ(2, service_->cert_count()); | |
| 571 EXPECT_STREQ("a", private_key_info1.c_str()); | |
| 572 EXPECT_STREQ("b", der_cert1.c_str()); | |
| 573 | |
| 574 // Expired cert is valid as well - synchronous completion. | |
| 575 std::string private_key_info2, der_cert2; | |
| 576 error = service_->GetOrCreateChannelID( | |
| 577 "expired", &private_key_info2, &der_cert2, | |
| 578 callback.callback(), &request_handle); | |
| 579 EXPECT_EQ(OK, error); | |
| 580 EXPECT_FALSE(request_handle.is_active()); | |
| 581 EXPECT_EQ(2, service_->cert_count()); | |
| 582 EXPECT_STREQ("c", private_key_info2.c_str()); | |
| 583 EXPECT_STREQ("d", der_cert2.c_str()); | |
| 584 } | |
| 585 | |
| 586 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { | 468 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { |
| 587 MockChannelIDStoreWithAsyncGet* mock_store = | 469 MockChannelIDStoreWithAsyncGet* mock_store = |
| 588 new MockChannelIDStoreWithAsyncGet(); | 470 new MockChannelIDStoreWithAsyncGet(); |
| 589 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 471 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( |
| 590 mock_store, base::MessageLoopProxy::current())); | 472 mock_store, base::MessageLoopProxy::current())); |
| 591 | 473 |
| 592 std::string host("encrypted.google.com"); | 474 std::string host("encrypted.google.com"); |
| 593 | 475 |
| 594 int error; | 476 int error; |
| 595 TestCompletionCallback callback; | 477 TestCompletionCallback callback; |
| 596 ChannelIDService::RequestHandle request_handle; | 478 ChannelIDService::RequestHandle request_handle; |
| 597 | 479 |
| 598 // Asynchronous completion with no certs in the store. | 480 // Asynchronous completion with no certs in the store. |
| 599 std::string private_key_info, der_cert; | 481 scoped_ptr<crypto::ECPrivateKey> key; |
| 600 EXPECT_EQ(0, service_->cert_count()); | 482 EXPECT_EQ(0, service_->cert_count()); |
| 601 error = service_->GetOrCreateChannelID( | 483 error = service_->GetOrCreateChannelID(host, &key, callback.callback(), |
| 602 host, &private_key_info, &der_cert, callback.callback(), &request_handle); | 484 &request_handle); |
| 603 EXPECT_EQ(ERR_IO_PENDING, error); | 485 EXPECT_EQ(ERR_IO_PENDING, error); |
| 604 EXPECT_TRUE(request_handle.is_active()); | 486 EXPECT_TRUE(request_handle.is_active()); |
| 605 | 487 |
| 606 mock_store->CallGetChannelIDCallbackWithResult( | 488 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); |
| 607 ERR_FILE_NOT_FOUND, base::Time(), std::string(), std::string()); | |
| 608 | 489 |
| 609 error = callback.WaitForResult(); | 490 error = callback.WaitForResult(); |
| 610 EXPECT_EQ(OK, error); | 491 EXPECT_EQ(OK, error); |
| 611 EXPECT_EQ(1, service_->cert_count()); | 492 EXPECT_EQ(1, service_->cert_count()); |
| 612 EXPECT_FALSE(private_key_info.empty()); | 493 EXPECT_TRUE(key); |
| 613 EXPECT_FALSE(der_cert.empty()); | |
| 614 EXPECT_FALSE(request_handle.is_active()); | 494 EXPECT_FALSE(request_handle.is_active()); |
| 615 } | 495 } |
| 616 | 496 |
| 617 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { | 497 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { |
| 618 MockChannelIDStoreWithAsyncGet* mock_store = | 498 MockChannelIDStoreWithAsyncGet* mock_store = |
| 619 new MockChannelIDStoreWithAsyncGet(); | 499 new MockChannelIDStoreWithAsyncGet(); |
| 620 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 500 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( |
| 621 mock_store, base::MessageLoopProxy::current())); | 501 mock_store, base::MessageLoopProxy::current())); |
| 622 | 502 |
| 623 std::string host("encrypted.google.com"); | 503 std::string host("encrypted.google.com"); |
| 624 | 504 |
| 625 int error; | 505 int error; |
| 626 TestCompletionCallback callback; | 506 TestCompletionCallback callback; |
| 627 ChannelIDService::RequestHandle request_handle; | 507 ChannelIDService::RequestHandle request_handle; |
| 628 | 508 |
| 629 // Asynchronous completion with no certs in the store. | 509 // Asynchronous completion with no certs in the store. |
| 630 std::string private_key, der_cert; | 510 scoped_ptr<crypto::ECPrivateKey> key; |
| 631 EXPECT_EQ(0, service_->cert_count()); | 511 EXPECT_EQ(0, service_->cert_count()); |
| 632 error = service_->GetChannelID( | 512 error = |
| 633 host, &private_key, &der_cert, callback.callback(), &request_handle); | 513 service_->GetChannelID(host, &key, callback.callback(), &request_handle); |
| 634 EXPECT_EQ(ERR_IO_PENDING, error); | 514 EXPECT_EQ(ERR_IO_PENDING, error); |
| 635 EXPECT_TRUE(request_handle.is_active()); | 515 EXPECT_TRUE(request_handle.is_active()); |
| 636 | 516 |
| 637 mock_store->CallGetChannelIDCallbackWithResult( | 517 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); |
| 638 ERR_FILE_NOT_FOUND, base::Time(), std::string(), std::string()); | |
| 639 | 518 |
| 640 error = callback.WaitForResult(); | 519 error = callback.WaitForResult(); |
| 641 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); | 520 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); |
| 642 EXPECT_EQ(0, service_->cert_count()); | 521 EXPECT_EQ(0, service_->cert_count()); |
| 643 EXPECT_EQ(0u, service_->workers_created()); | 522 EXPECT_EQ(0u, service_->workers_created()); |
| 644 EXPECT_TRUE(der_cert.empty()); | 523 EXPECT_FALSE(key); |
| 645 EXPECT_FALSE(request_handle.is_active()); | 524 EXPECT_FALSE(request_handle.is_active()); |
| 646 } | 525 } |
| 647 | 526 |
| 648 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { | 527 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { |
| 649 MockChannelIDStoreWithAsyncGet* mock_store = | 528 MockChannelIDStoreWithAsyncGet* mock_store = |
| 650 new MockChannelIDStoreWithAsyncGet(); | 529 new MockChannelIDStoreWithAsyncGet(); |
| 651 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 530 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( |
| 652 mock_store, base::MessageLoopProxy::current())); | 531 mock_store, base::MessageLoopProxy::current())); |
| 653 | 532 |
| 654 std::string host("encrypted.google.com"); | 533 std::string host("encrypted.google.com"); |
| 655 | 534 |
| 656 int error; | 535 int error; |
| 657 TestCompletionCallback callback; | 536 TestCompletionCallback callback; |
| 658 ChannelIDService::RequestHandle request_handle; | 537 ChannelIDService::RequestHandle request_handle; |
| 659 | 538 |
| 660 // Asynchronous completion with a cert in the store. | 539 // Asynchronous completion with a cert in the store. |
| 661 std::string private_key_info, der_cert; | 540 scoped_ptr<crypto::ECPrivateKey> key; |
| 662 EXPECT_EQ(0, service_->cert_count()); | 541 EXPECT_EQ(0, service_->cert_count()); |
| 663 error = service_->GetOrCreateChannelID( | 542 error = service_->GetOrCreateChannelID(host, &key, callback.callback(), |
| 664 host, &private_key_info, &der_cert, callback.callback(), &request_handle); | 543 &request_handle); |
| 665 EXPECT_EQ(ERR_IO_PENDING, error); | 544 EXPECT_EQ(ERR_IO_PENDING, error); |
| 666 EXPECT_TRUE(request_handle.is_active()); | 545 EXPECT_TRUE(request_handle.is_active()); |
| 667 | 546 |
| 668 mock_store->CallGetChannelIDCallbackWithResult( | 547 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); |
| 669 OK, base::Time(), "ab", "cd"); | 548 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); |
| 670 | 549 |
| 671 error = callback.WaitForResult(); | 550 error = callback.WaitForResult(); |
| 672 EXPECT_EQ(OK, error); | 551 EXPECT_EQ(OK, error); |
| 673 EXPECT_EQ(1, service_->cert_count()); | 552 EXPECT_EQ(1, service_->cert_count()); |
| 674 EXPECT_EQ(1u, service_->requests()); | 553 EXPECT_EQ(1u, service_->requests()); |
| 675 EXPECT_EQ(1u, service_->cert_store_hits()); | 554 EXPECT_EQ(1u, service_->key_store_hits()); |
| 676 // Because the cert was found in the store, no new workers should have been | 555 // Because the cert was found in the store, no new workers should have been |
| 677 // created. | 556 // created. |
| 678 EXPECT_EQ(0u, service_->workers_created()); | 557 EXPECT_EQ(0u, service_->workers_created()); |
| 679 EXPECT_STREQ("ab", private_key_info.c_str()); | 558 EXPECT_TRUE(key); |
| 680 EXPECT_STREQ("cd", der_cert.c_str()); | 559 ExpectKeysEqual(expected_key, key); |
| 681 EXPECT_FALSE(request_handle.is_active()); | 560 EXPECT_FALSE(request_handle.is_active()); |
| 682 } | 561 } |
| 683 | 562 |
| 684 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { | 563 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { |
| 685 MockChannelIDStoreWithAsyncGet* mock_store = | 564 MockChannelIDStoreWithAsyncGet* mock_store = |
| 686 new MockChannelIDStoreWithAsyncGet(); | 565 new MockChannelIDStoreWithAsyncGet(); |
| 687 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 566 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( |
| 688 mock_store, base::MessageLoopProxy::current())); | 567 mock_store, base::MessageLoopProxy::current())); |
| 689 | 568 |
| 690 std::string host("encrypted.google.com"); | 569 std::string host("encrypted.google.com"); |
| 691 | 570 |
| 692 int error; | 571 int error; |
| 693 TestCompletionCallback callback; | 572 TestCompletionCallback callback; |
| 694 ChannelIDService::RequestHandle request_handle; | 573 ChannelIDService::RequestHandle request_handle; |
| 695 | 574 |
| 696 // Asynchronous completion with a cert in the store. | 575 // Asynchronous completion with a cert in the store. |
| 697 std::string private_key, der_cert; | 576 scoped_ptr<crypto::ECPrivateKey> key; |
| 577 std::string private_key, spki; | |
| 698 EXPECT_EQ(0, service_->cert_count()); | 578 EXPECT_EQ(0, service_->cert_count()); |
| 699 error = service_->GetChannelID( | 579 error = |
| 700 host, &private_key, &der_cert, callback.callback(), &request_handle); | 580 service_->GetChannelID(host, &key, callback.callback(), &request_handle); |
| 701 EXPECT_EQ(ERR_IO_PENDING, error); | 581 EXPECT_EQ(ERR_IO_PENDING, error); |
| 702 EXPECT_TRUE(request_handle.is_active()); | 582 EXPECT_TRUE(request_handle.is_active()); |
| 703 | 583 |
| 704 mock_store->CallGetChannelIDCallbackWithResult( | 584 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); |
| 705 OK, base::Time(), "ab", "cd"); | 585 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); |
| 706 | 586 |
| 707 error = callback.WaitForResult(); | 587 error = callback.WaitForResult(); |
| 708 EXPECT_EQ(OK, error); | 588 EXPECT_EQ(OK, error); |
| 709 EXPECT_EQ(1, service_->cert_count()); | 589 EXPECT_EQ(1, service_->cert_count()); |
| 710 EXPECT_EQ(1u, service_->requests()); | 590 EXPECT_EQ(1u, service_->requests()); |
| 711 EXPECT_EQ(1u, service_->cert_store_hits()); | 591 EXPECT_EQ(1u, service_->key_store_hits()); |
| 712 // Because the cert was found in the store, no new workers should have been | 592 // Because the cert was found in the store, no new workers should have been |
| 713 // created. | 593 // created. |
| 714 EXPECT_EQ(0u, service_->workers_created()); | 594 EXPECT_EQ(0u, service_->workers_created()); |
| 715 EXPECT_STREQ("cd", der_cert.c_str()); | 595 ExpectKeysEqual(expected_key, key); |
| 716 EXPECT_FALSE(request_handle.is_active()); | 596 EXPECT_FALSE(request_handle.is_active()); |
| 717 } | 597 } |
| 718 | 598 |
| 719 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { | 599 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { |
| 720 MockChannelIDStoreWithAsyncGet* mock_store = | 600 MockChannelIDStoreWithAsyncGet* mock_store = |
| 721 new MockChannelIDStoreWithAsyncGet(); | 601 new MockChannelIDStoreWithAsyncGet(); |
| 722 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( | 602 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( |
| 723 mock_store, base::MessageLoopProxy::current())); | 603 mock_store, base::MessageLoopProxy::current())); |
| 724 | 604 |
| 725 std::string host("encrypted.google.com"); | 605 std::string host("encrypted.google.com"); |
| 726 | 606 |
| 727 int error; | 607 int error; |
| 728 | 608 |
| 729 // Asynchronous get with no certs in the store. | 609 // Asynchronous get with no certs in the store. |
| 730 TestCompletionCallback callback1; | 610 TestCompletionCallback callback1; |
| 731 ChannelIDService::RequestHandle request_handle1; | 611 ChannelIDService::RequestHandle request_handle1; |
| 732 std::string private_key1, der_cert1; | 612 scoped_ptr<crypto::ECPrivateKey> key1; |
| 733 EXPECT_EQ(0, service_->cert_count()); | 613 EXPECT_EQ(0, service_->cert_count()); |
| 734 error = service_->GetChannelID( | 614 error = service_->GetChannelID(host, &key1, callback1.callback(), |
| 735 host, &private_key1, &der_cert1, callback1.callback(), &request_handle1); | 615 &request_handle1); |
| 736 EXPECT_EQ(ERR_IO_PENDING, error); | 616 EXPECT_EQ(ERR_IO_PENDING, error); |
| 737 EXPECT_TRUE(request_handle1.is_active()); | 617 EXPECT_TRUE(request_handle1.is_active()); |
| 738 | 618 |
| 739 // Asynchronous get/create with no certs in the store. | 619 // Asynchronous get/create with no certs in the store. |
| 740 TestCompletionCallback callback2; | 620 TestCompletionCallback callback2; |
| 741 ChannelIDService::RequestHandle request_handle2; | 621 ChannelIDService::RequestHandle request_handle2; |
| 742 std::string private_key2, der_cert2; | 622 scoped_ptr<crypto::ECPrivateKey> key2; |
| 743 EXPECT_EQ(0, service_->cert_count()); | 623 EXPECT_EQ(0, service_->cert_count()); |
| 744 error = service_->GetOrCreateChannelID( | 624 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), |
| 745 host, &private_key2, &der_cert2, callback2.callback(), &request_handle2); | 625 &request_handle2); |
| 746 EXPECT_EQ(ERR_IO_PENDING, error); | 626 EXPECT_EQ(ERR_IO_PENDING, error); |
| 747 EXPECT_TRUE(request_handle2.is_active()); | 627 EXPECT_TRUE(request_handle2.is_active()); |
| 748 | 628 |
| 749 mock_store->CallGetChannelIDCallbackWithResult( | 629 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); |
| 750 ERR_FILE_NOT_FOUND, base::Time(), std::string(), std::string()); | |
| 751 | 630 |
| 752 // Even though the first request didn't ask to create a cert, it gets joined | 631 // Even though the first request didn't ask to create a cert, it gets joined |
| 753 // by the second, which does, so both succeed. | 632 // by the second, which does, so both succeed. |
| 754 error = callback1.WaitForResult(); | 633 error = callback1.WaitForResult(); |
| 755 EXPECT_EQ(OK, error); | 634 EXPECT_EQ(OK, error); |
| 756 error = callback2.WaitForResult(); | 635 error = callback2.WaitForResult(); |
| 757 EXPECT_EQ(OK, error); | 636 EXPECT_EQ(OK, error); |
| 758 | 637 |
| 759 // One cert is created, one request is joined. | 638 // One cert is created, one request is joined. |
| 760 EXPECT_EQ(2U, service_->requests()); | 639 EXPECT_EQ(2U, service_->requests()); |
| 761 EXPECT_EQ(1, service_->cert_count()); | 640 EXPECT_EQ(1, service_->cert_count()); |
| 762 EXPECT_EQ(1u, service_->workers_created()); | 641 EXPECT_EQ(1u, service_->workers_created()); |
| 763 EXPECT_EQ(1u, service_->inflight_joins()); | 642 EXPECT_EQ(1u, service_->inflight_joins()); |
| 764 EXPECT_FALSE(der_cert1.empty()); | 643 EXPECT_TRUE(key1); |
| 765 EXPECT_EQ(der_cert1, der_cert2); | 644 ExpectKeysEqual(key1, key2); |
| 766 EXPECT_FALSE(private_key1.empty()); | |
| 767 EXPECT_EQ(private_key1, private_key2); | |
| 768 EXPECT_FALSE(request_handle1.is_active()); | 645 EXPECT_FALSE(request_handle1.is_active()); |
| 769 EXPECT_FALSE(request_handle2.is_active()); | 646 EXPECT_FALSE(request_handle2.is_active()); |
| 770 } | 647 } |
| 771 | 648 |
| 772 } // namespace | 649 } // namespace |
| 773 | 650 |
| 774 } // namespace net | 651 } // namespace net |
| OLD | NEW |