Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: net/ssl/channel_id_service_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698