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

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

Issue 1149083013: Combine ChannelIDService::RequestHandle and ChannelIDServiceRequest classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove new test case and combine it with existing one Created 5 years, 6 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
« no previous file with comments | « net/ssl/channel_id_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 ChannelIDService::GetDomainForHost("goto")); 130 ChannelIDService::GetDomainForHost("goto"));
131 EXPECT_EQ("127.0.0.1", 131 EXPECT_EQ("127.0.0.1",
132 ChannelIDService::GetDomainForHost("127.0.0.1")); 132 ChannelIDService::GetDomainForHost("127.0.0.1"));
133 } 133 }
134 134
135 TEST_F(ChannelIDServiceTest, GetCacheMiss) { 135 TEST_F(ChannelIDServiceTest, GetCacheMiss) {
136 std::string host("encrypted.google.com"); 136 std::string host("encrypted.google.com");
137 137
138 int error; 138 int error;
139 TestCompletionCallback callback; 139 TestCompletionCallback callback;
140 ChannelIDService::RequestHandle request_handle; 140 ChannelIDService::Request request;
141 141
142 // Synchronous completion, because the store is initialized. 142 // Synchronous completion, because the store is initialized.
143 scoped_ptr<crypto::ECPrivateKey> key; 143 scoped_ptr<crypto::ECPrivateKey> key;
144 EXPECT_EQ(0, service_->channel_id_count()); 144 EXPECT_EQ(0, service_->channel_id_count());
145 error = 145 error = service_->GetChannelID(host, &key, callback.callback(), &request);
146 service_->GetChannelID(host, &key, callback.callback(), &request_handle);
147 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); 146 EXPECT_EQ(ERR_FILE_NOT_FOUND, error);
148 EXPECT_FALSE(request_handle.is_active()); 147 EXPECT_FALSE(request.is_active());
149 EXPECT_EQ(0, service_->channel_id_count()); 148 EXPECT_EQ(0, service_->channel_id_count());
150 EXPECT_FALSE(key); 149 EXPECT_FALSE(key);
151 } 150 }
152 151
153 TEST_F(ChannelIDServiceTest, CacheHit) { 152 TEST_F(ChannelIDServiceTest, CacheHit) {
154 std::string host("encrypted.google.com"); 153 std::string host("encrypted.google.com");
155 154
156 int error; 155 int error;
157 TestCompletionCallback callback; 156 TestCompletionCallback callback;
158 ChannelIDService::RequestHandle request_handle; 157 ChannelIDService::Request request;
159 158
160 // Asynchronous completion. 159 // Asynchronous completion.
161 scoped_ptr<crypto::ECPrivateKey> key1; 160 scoped_ptr<crypto::ECPrivateKey> key1;
162 EXPECT_EQ(0, service_->channel_id_count()); 161 EXPECT_EQ(0, service_->channel_id_count());
163 error = service_->GetOrCreateChannelID(host, &key1, callback.callback(), 162 error = service_->GetOrCreateChannelID(host, &key1, callback.callback(),
164 &request_handle); 163 &request);
165 EXPECT_EQ(ERR_IO_PENDING, error); 164 EXPECT_EQ(ERR_IO_PENDING, error);
166 EXPECT_TRUE(request_handle.is_active()); 165 EXPECT_TRUE(request.is_active());
167 error = callback.WaitForResult(); 166 error = callback.WaitForResult();
168 EXPECT_EQ(OK, error); 167 EXPECT_EQ(OK, error);
169 EXPECT_EQ(1, service_->channel_id_count()); 168 EXPECT_EQ(1, service_->channel_id_count());
170 EXPECT_TRUE(key1); 169 EXPECT_TRUE(key1);
171 EXPECT_FALSE(request_handle.is_active()); 170 EXPECT_FALSE(request.is_active());
172 171
173 // Synchronous completion. 172 // Synchronous completion.
174 scoped_ptr<crypto::ECPrivateKey> key2; 173 scoped_ptr<crypto::ECPrivateKey> key2;
175 error = service_->GetOrCreateChannelID(host, &key2, callback.callback(), 174 error = service_->GetOrCreateChannelID(host, &key2, callback.callback(),
176 &request_handle); 175 &request);
177 EXPECT_FALSE(request_handle.is_active()); 176 EXPECT_FALSE(request.is_active());
178 EXPECT_EQ(OK, error); 177 EXPECT_EQ(OK, error);
179 EXPECT_EQ(1, service_->channel_id_count()); 178 EXPECT_EQ(1, service_->channel_id_count());
180 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 179 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
181 180
182 // Synchronous get. 181 // Synchronous get.
183 scoped_ptr<crypto::ECPrivateKey> key3; 182 scoped_ptr<crypto::ECPrivateKey> key3;
184 error = 183 error = service_->GetChannelID(host, &key3, callback.callback(), &request);
185 service_->GetChannelID(host, &key3, callback.callback(), &request_handle); 184 EXPECT_FALSE(request.is_active());
186 EXPECT_FALSE(request_handle.is_active());
187 EXPECT_EQ(OK, error); 185 EXPECT_EQ(OK, error);
188 EXPECT_EQ(1, service_->channel_id_count()); 186 EXPECT_EQ(1, service_->channel_id_count());
189 EXPECT_TRUE(KeysEqual(key1.get(), key3.get())); 187 EXPECT_TRUE(KeysEqual(key1.get(), key3.get()));
190 188
191 EXPECT_EQ(3u, service_->requests()); 189 EXPECT_EQ(3u, service_->requests());
192 EXPECT_EQ(2u, service_->key_store_hits()); 190 EXPECT_EQ(2u, service_->key_store_hits());
193 EXPECT_EQ(0u, service_->inflight_joins()); 191 EXPECT_EQ(0u, service_->inflight_joins());
194 } 192 }
195 193
196 TEST_F(ChannelIDServiceTest, StoreChannelIDs) { 194 TEST_F(ChannelIDServiceTest, StoreChannelIDs) {
197 int error; 195 int error;
198 TestCompletionCallback callback; 196 TestCompletionCallback callback;
199 ChannelIDService::RequestHandle request_handle; 197 ChannelIDService::Request request;
200 198
201 std::string host1("encrypted.google.com"); 199 std::string host1("encrypted.google.com");
202 scoped_ptr<crypto::ECPrivateKey> key1; 200 scoped_ptr<crypto::ECPrivateKey> key1;
203 EXPECT_EQ(0, service_->channel_id_count()); 201 EXPECT_EQ(0, service_->channel_id_count());
204 error = service_->GetOrCreateChannelID(host1, &key1, callback.callback(), 202 error = service_->GetOrCreateChannelID(host1, &key1, callback.callback(),
205 &request_handle); 203 &request);
206 EXPECT_EQ(ERR_IO_PENDING, error); 204 EXPECT_EQ(ERR_IO_PENDING, error);
207 EXPECT_TRUE(request_handle.is_active()); 205 EXPECT_TRUE(request.is_active());
208 error = callback.WaitForResult(); 206 error = callback.WaitForResult();
209 EXPECT_EQ(OK, error); 207 EXPECT_EQ(OK, error);
210 EXPECT_EQ(1, service_->channel_id_count()); 208 EXPECT_EQ(1, service_->channel_id_count());
211 209
212 std::string host2("www.verisign.com"); 210 std::string host2("www.verisign.com");
213 scoped_ptr<crypto::ECPrivateKey> key2; 211 scoped_ptr<crypto::ECPrivateKey> key2;
214 error = service_->GetOrCreateChannelID(host2, &key2, callback.callback(), 212 error = service_->GetOrCreateChannelID(host2, &key2, callback.callback(),
215 &request_handle); 213 &request);
216 EXPECT_EQ(ERR_IO_PENDING, error); 214 EXPECT_EQ(ERR_IO_PENDING, error);
217 EXPECT_TRUE(request_handle.is_active()); 215 EXPECT_TRUE(request.is_active());
218 error = callback.WaitForResult(); 216 error = callback.WaitForResult();
219 EXPECT_EQ(OK, error); 217 EXPECT_EQ(OK, error);
220 EXPECT_EQ(2, service_->channel_id_count()); 218 EXPECT_EQ(2, service_->channel_id_count());
221 219
222 std::string host3("www.twitter.com"); 220 std::string host3("www.twitter.com");
223 scoped_ptr<crypto::ECPrivateKey> key3; 221 scoped_ptr<crypto::ECPrivateKey> key3;
224 error = service_->GetOrCreateChannelID(host3, &key3, callback.callback(), 222 error = service_->GetOrCreateChannelID(host3, &key3, callback.callback(),
225 &request_handle); 223 &request);
226 EXPECT_EQ(ERR_IO_PENDING, error); 224 EXPECT_EQ(ERR_IO_PENDING, error);
227 EXPECT_TRUE(request_handle.is_active()); 225 EXPECT_TRUE(request.is_active());
228 error = callback.WaitForResult(); 226 error = callback.WaitForResult();
229 EXPECT_EQ(OK, error); 227 EXPECT_EQ(OK, error);
230 EXPECT_EQ(3, service_->channel_id_count()); 228 EXPECT_EQ(3, service_->channel_id_count());
231 229
232 EXPECT_FALSE(KeysEqual(key1.get(), key2.get())); 230 EXPECT_FALSE(KeysEqual(key1.get(), key2.get()));
233 EXPECT_FALSE(KeysEqual(key1.get(), key3.get())); 231 EXPECT_FALSE(KeysEqual(key1.get(), key3.get()));
234 EXPECT_FALSE(KeysEqual(key2.get(), key3.get())); 232 EXPECT_FALSE(KeysEqual(key2.get(), key3.get()));
235 } 233 }
236 234
237 // Tests an inflight join. 235 // Tests an inflight join.
238 TEST_F(ChannelIDServiceTest, InflightJoin) { 236 TEST_F(ChannelIDServiceTest, InflightJoin) {
239 std::string host("encrypted.google.com"); 237 std::string host("encrypted.google.com");
240 int error; 238 int error;
241 239
242 scoped_ptr<crypto::ECPrivateKey> key1; 240 scoped_ptr<crypto::ECPrivateKey> key1;
243 TestCompletionCallback callback1; 241 TestCompletionCallback callback1;
244 ChannelIDService::RequestHandle request_handle1; 242 ChannelIDService::Request request1;
245 243
246 scoped_ptr<crypto::ECPrivateKey> key2; 244 scoped_ptr<crypto::ECPrivateKey> key2;
247 TestCompletionCallback callback2; 245 TestCompletionCallback callback2;
248 ChannelIDService::RequestHandle request_handle2; 246 ChannelIDService::Request request2;
249 247
250 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), 248 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(),
251 &request_handle1); 249 &request1);
252 EXPECT_EQ(ERR_IO_PENDING, error); 250 EXPECT_EQ(ERR_IO_PENDING, error);
253 EXPECT_TRUE(request_handle1.is_active()); 251 EXPECT_TRUE(request1.is_active());
254 // Should join with the original request. 252 // Should join with the original request.
255 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), 253 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(),
256 &request_handle2); 254 &request2);
257 EXPECT_EQ(ERR_IO_PENDING, error); 255 EXPECT_EQ(ERR_IO_PENDING, error);
258 EXPECT_TRUE(request_handle2.is_active()); 256 EXPECT_TRUE(request2.is_active());
259 257
260 error = callback1.WaitForResult(); 258 error = callback1.WaitForResult();
261 EXPECT_EQ(OK, error); 259 EXPECT_EQ(OK, error);
262 error = callback2.WaitForResult(); 260 error = callback2.WaitForResult();
263 EXPECT_EQ(OK, error); 261 EXPECT_EQ(OK, error);
264 262
265 EXPECT_EQ(2u, service_->requests()); 263 EXPECT_EQ(2u, service_->requests());
266 EXPECT_EQ(0u, service_->key_store_hits()); 264 EXPECT_EQ(0u, service_->key_store_hits());
267 EXPECT_EQ(1u, service_->inflight_joins()); 265 EXPECT_EQ(1u, service_->inflight_joins());
268 EXPECT_EQ(1u, service_->workers_created()); 266 EXPECT_EQ(1u, service_->workers_created());
269 } 267 }
270 268
271 // Tests an inflight join of a Get request to a GetOrCreate request. 269 // Tests an inflight join of a Get request to a GetOrCreate request.
272 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) { 270 TEST_F(ChannelIDServiceTest, InflightJoinGetOrCreateAndGet) {
273 std::string host("encrypted.google.com"); 271 std::string host("encrypted.google.com");
274 int error; 272 int error;
275 273
276 scoped_ptr<crypto::ECPrivateKey> key1; 274 scoped_ptr<crypto::ECPrivateKey> key1;
277 TestCompletionCallback callback1; 275 TestCompletionCallback callback1;
278 ChannelIDService::RequestHandle request_handle1; 276 ChannelIDService::Request request1;
279 277
280 scoped_ptr<crypto::ECPrivateKey> key2; 278 scoped_ptr<crypto::ECPrivateKey> key2;
281 TestCompletionCallback callback2; 279 TestCompletionCallback callback2;
282 ChannelIDService::RequestHandle request_handle2; 280 ChannelIDService::Request request2;
283 281
284 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(), 282 error = service_->GetOrCreateChannelID(host, &key1, callback1.callback(),
285 &request_handle1); 283 &request1);
286 EXPECT_EQ(ERR_IO_PENDING, error); 284 EXPECT_EQ(ERR_IO_PENDING, error);
287 EXPECT_TRUE(request_handle1.is_active()); 285 EXPECT_TRUE(request1.is_active());
288 // Should join with the original request. 286 // Should join with the original request.
289 error = service_->GetChannelID(host, &key2, callback2.callback(), 287 error = service_->GetChannelID(host, &key2, callback2.callback(), &request2);
290 &request_handle2);
291 EXPECT_EQ(ERR_IO_PENDING, error); 288 EXPECT_EQ(ERR_IO_PENDING, error);
292 EXPECT_TRUE(request_handle2.is_active()); 289 EXPECT_TRUE(request2.is_active());
293 290
294 error = callback1.WaitForResult(); 291 error = callback1.WaitForResult();
295 EXPECT_EQ(OK, error); 292 EXPECT_EQ(OK, error);
296 error = callback2.WaitForResult(); 293 error = callback2.WaitForResult();
297 EXPECT_EQ(OK, error); 294 EXPECT_EQ(OK, error);
298 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 295 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
299 296
300 EXPECT_EQ(2u, service_->requests()); 297 EXPECT_EQ(2u, service_->requests());
301 EXPECT_EQ(0u, service_->key_store_hits()); 298 EXPECT_EQ(0u, service_->key_store_hits());
302 EXPECT_EQ(1u, service_->inflight_joins()); 299 EXPECT_EQ(1u, service_->inflight_joins());
303 EXPECT_EQ(1u, service_->workers_created()); 300 EXPECT_EQ(1u, service_->workers_created());
304 } 301 }
305 302
306 // Tests that the callback of a canceled request is never made. 303 // Tests that the callback of a canceled request is never made.
307 TEST_F(ChannelIDServiceTest, CancelRequest) { 304 TEST_F(ChannelIDServiceTest, CancelRequest) {
308 std::string host("encrypted.google.com"); 305 std::string host("encrypted.google.com");
309 scoped_ptr<crypto::ECPrivateKey> key; 306 scoped_ptr<crypto::ECPrivateKey> key;
310 int error; 307 int error;
311 ChannelIDService::RequestHandle request_handle; 308 ChannelIDService::Request request;
312 309
313 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 310 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
314 &request_handle); 311 &request);
315 EXPECT_EQ(ERR_IO_PENDING, error); 312 EXPECT_EQ(ERR_IO_PENDING, error);
316 EXPECT_TRUE(request_handle.is_active()); 313 EXPECT_TRUE(request.is_active());
317 request_handle.Cancel(); 314 request.Cancel();
318 EXPECT_FALSE(request_handle.is_active()); 315 EXPECT_FALSE(request.is_active());
319 316
320 // Wait for reply from ChannelIDServiceWorker to be posted back to the 317 // Wait for reply from ChannelIDServiceWorker to be posted back to the
321 // ChannelIDService. 318 // ChannelIDService.
322 base::MessageLoop::current()->RunUntilIdle(); 319 base::MessageLoop::current()->RunUntilIdle();
323 320
324 // Even though the original request was cancelled, the service will still 321 // Even though the original request was cancelled, the service will still
325 // store the result, it just doesn't call the callback. 322 // store the result, it just doesn't call the callback.
326 EXPECT_EQ(1, service_->channel_id_count()); 323 EXPECT_EQ(1, service_->channel_id_count());
327 } 324 }
328 325
329 // Tests that destructing the RequestHandle cancels the request. 326 // Tests that destructing the Request cancels the request.
330 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) { 327 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) {
331 std::string host("encrypted.google.com"); 328 std::string host("encrypted.google.com");
332 scoped_ptr<crypto::ECPrivateKey> key; 329 scoped_ptr<crypto::ECPrivateKey> key;
333 int error; 330 int error;
334 { 331 scoped_ptr<ChannelIDService::Request> request(
335 ChannelIDService::RequestHandle request_handle; 332 new ChannelIDService::Request());
336 333
337 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 334 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
338 &request_handle); 335 request.get());
339 EXPECT_EQ(ERR_IO_PENDING, error); 336 EXPECT_EQ(ERR_IO_PENDING, error);
340 EXPECT_TRUE(request_handle.is_active()); 337 EXPECT_TRUE(request->is_active());
341 } 338
339 // Delete the Request object.
340 request.reset();
342 341
343 // Wait for reply from ChannelIDServiceWorker to be posted back to the 342 // Wait for reply from ChannelIDServiceWorker to be posted back to the
344 // ChannelIDService. 343 // ChannelIDService.
345 base::MessageLoop::current()->RunUntilIdle(); 344 base::MessageLoop::current()->RunUntilIdle();
346 345
347 // Even though the original request was cancelled, the service will still 346 // Even though the original request was cancelled, the service will still
348 // store the result, it just doesn't call the callback. 347 // store the result, it just doesn't call the callback.
349 EXPECT_EQ(1, service_->channel_id_count()); 348 EXPECT_EQ(1, service_->channel_id_count());
350 } 349 }
351 350
352 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { 351 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) {
353 std::string host("encrypted.google.com"); 352 std::string host("encrypted.google.com");
354 scoped_ptr<crypto::ECPrivateKey> key; 353 scoped_ptr<crypto::ECPrivateKey> key;
355 int error; 354 int error;
356 ChannelIDService::RequestHandle request_handle; 355 ChannelIDService::Request request;
357 356
358 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 357 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
359 &request_handle); 358 &request);
360 EXPECT_EQ(ERR_IO_PENDING, error); 359 EXPECT_EQ(ERR_IO_PENDING, error);
361 EXPECT_TRUE(request_handle.is_active()); 360 EXPECT_TRUE(request.is_active());
362 361
363 // Cancel request and destroy the ChannelIDService. 362 // Cancel request and destroy the ChannelIDService.
364 request_handle.Cancel(); 363 request.Cancel();
365 service_.reset(); 364 service_.reset();
366 365
367 // ChannelIDServiceWorker should not post anything back to the 366 // ChannelIDServiceWorker should not post anything back to the
368 // non-existent ChannelIDService, but run the loop just to be sure it 367 // non-existent ChannelIDService, but run the loop just to be sure it
369 // doesn't. 368 // doesn't.
370 base::MessageLoop::current()->RunUntilIdle(); 369 base::MessageLoop::current()->RunUntilIdle();
371 370
372 // If we got here without crashing or a valgrind error, it worked. 371 // If we got here without crashing or a valgrind error, it worked.
373 } 372 }
374 373
375 // Tests that shutting down the sequenced worker pool and then making new 374 // Tests that shutting down the sequenced worker pool and then making new
376 // requests gracefully fails. 375 // requests gracefully fails.
377 // This is a regression test for http://crbug.com/236387 376 // This is a regression test for http://crbug.com/236387
378 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { 377 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) {
379 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner); 378 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner);
380 service_.reset(new ChannelIDService( 379 service_.reset(new ChannelIDService(
381 new DefaultChannelIDStore(NULL), task_runner)); 380 new DefaultChannelIDStore(NULL), task_runner));
382 381
383 // Make a request that will force synchronous completion. 382 // Make a request that will force synchronous completion.
384 std::string host("encrypted.google.com"); 383 std::string host("encrypted.google.com");
385 scoped_ptr<crypto::ECPrivateKey> key; 384 scoped_ptr<crypto::ECPrivateKey> key;
386 int error; 385 int error;
387 ChannelIDService::RequestHandle request_handle; 386 ChannelIDService::Request request;
388 387
389 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 388 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
390 &request_handle); 389 &request);
391 // If we got here without crashing or a valgrind error, it worked. 390 // If we got here without crashing or a valgrind error, it worked.
392 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error); 391 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error);
393 EXPECT_FALSE(request_handle.is_active()); 392 EXPECT_FALSE(request.is_active());
394 } 393 }
395 394
396 // Tests that simultaneous creation of different certs works. 395 // Tests that simultaneous creation of different certs works.
397 TEST_F(ChannelIDServiceTest, SimultaneousCreation) { 396 TEST_F(ChannelIDServiceTest, SimultaneousCreation) {
398 int error; 397 int error;
399 398
400 std::string host1("encrypted.google.com"); 399 std::string host1("encrypted.google.com");
401 scoped_ptr<crypto::ECPrivateKey> key1; 400 scoped_ptr<crypto::ECPrivateKey> key1;
402 TestCompletionCallback callback1; 401 TestCompletionCallback callback1;
403 ChannelIDService::RequestHandle request_handle1; 402 ChannelIDService::Request request1;
404 403
405 std::string host2("foo.com"); 404 std::string host2("foo.com");
406 scoped_ptr<crypto::ECPrivateKey> key2; 405 scoped_ptr<crypto::ECPrivateKey> key2;
407 TestCompletionCallback callback2; 406 TestCompletionCallback callback2;
408 ChannelIDService::RequestHandle request_handle2; 407 ChannelIDService::Request request2;
409 408
410 std::string host3("bar.com"); 409 std::string host3("bar.com");
411 scoped_ptr<crypto::ECPrivateKey> key3; 410 scoped_ptr<crypto::ECPrivateKey> key3;
412 TestCompletionCallback callback3; 411 TestCompletionCallback callback3;
413 ChannelIDService::RequestHandle request_handle3; 412 ChannelIDService::Request request3;
414 413
415 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(), 414 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(),
416 &request_handle1); 415 &request1);
417 EXPECT_EQ(ERR_IO_PENDING, error); 416 EXPECT_EQ(ERR_IO_PENDING, error);
418 EXPECT_TRUE(request_handle1.is_active()); 417 EXPECT_TRUE(request1.is_active());
419 418
420 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(), 419 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(),
421 &request_handle2); 420 &request2);
422 EXPECT_EQ(ERR_IO_PENDING, error); 421 EXPECT_EQ(ERR_IO_PENDING, error);
423 EXPECT_TRUE(request_handle2.is_active()); 422 EXPECT_TRUE(request2.is_active());
424 423
425 error = service_->GetOrCreateChannelID(host3, &key3, callback3.callback(), 424 error = service_->GetOrCreateChannelID(host3, &key3, callback3.callback(),
426 &request_handle3); 425 &request3);
427 EXPECT_EQ(ERR_IO_PENDING, error); 426 EXPECT_EQ(ERR_IO_PENDING, error);
428 EXPECT_TRUE(request_handle3.is_active()); 427 EXPECT_TRUE(request3.is_active());
429 428
430 error = callback1.WaitForResult(); 429 error = callback1.WaitForResult();
431 EXPECT_EQ(OK, error); 430 EXPECT_EQ(OK, error);
432 EXPECT_TRUE(key1); 431 EXPECT_TRUE(key1);
433 432
434 error = callback2.WaitForResult(); 433 error = callback2.WaitForResult();
435 EXPECT_EQ(OK, error); 434 EXPECT_EQ(OK, error);
436 EXPECT_TRUE(key2); 435 EXPECT_TRUE(key2);
437 436
438 error = callback3.WaitForResult(); 437 error = callback3.WaitForResult();
(...skipping 10 matching lines...) Expand all
449 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { 448 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) {
450 MockChannelIDStoreWithAsyncGet* mock_store = 449 MockChannelIDStoreWithAsyncGet* mock_store =
451 new MockChannelIDStoreWithAsyncGet(); 450 new MockChannelIDStoreWithAsyncGet();
452 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 451 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
453 mock_store, base::MessageLoopProxy::current())); 452 mock_store, base::MessageLoopProxy::current()));
454 453
455 std::string host("encrypted.google.com"); 454 std::string host("encrypted.google.com");
456 455
457 int error; 456 int error;
458 TestCompletionCallback callback; 457 TestCompletionCallback callback;
459 ChannelIDService::RequestHandle request_handle; 458 ChannelIDService::Request request;
460 459
461 // Asynchronous completion with no certs in the store. 460 // Asynchronous completion with no certs in the store.
462 scoped_ptr<crypto::ECPrivateKey> key; 461 scoped_ptr<crypto::ECPrivateKey> key;
463 EXPECT_EQ(0, service_->channel_id_count()); 462 EXPECT_EQ(0, service_->channel_id_count());
464 error = service_->GetOrCreateChannelID(host, &key, callback.callback(), 463 error =
465 &request_handle); 464 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request);
466 EXPECT_EQ(ERR_IO_PENDING, error); 465 EXPECT_EQ(ERR_IO_PENDING, error);
467 EXPECT_TRUE(request_handle.is_active()); 466 EXPECT_TRUE(request.is_active());
468 467
469 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 468 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
470 469
471 error = callback.WaitForResult(); 470 error = callback.WaitForResult();
472 EXPECT_EQ(OK, error); 471 EXPECT_EQ(OK, error);
473 EXPECT_EQ(1, service_->channel_id_count()); 472 EXPECT_EQ(1, service_->channel_id_count());
474 EXPECT_TRUE(key); 473 EXPECT_TRUE(key);
475 EXPECT_FALSE(request_handle.is_active()); 474 EXPECT_FALSE(request.is_active());
476 } 475 }
477 476
478 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { 477 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) {
479 MockChannelIDStoreWithAsyncGet* mock_store = 478 MockChannelIDStoreWithAsyncGet* mock_store =
480 new MockChannelIDStoreWithAsyncGet(); 479 new MockChannelIDStoreWithAsyncGet();
481 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 480 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
482 mock_store, base::MessageLoopProxy::current())); 481 mock_store, base::MessageLoopProxy::current()));
483 482
484 std::string host("encrypted.google.com"); 483 std::string host("encrypted.google.com");
485 484
486 int error; 485 int error;
487 TestCompletionCallback callback; 486 TestCompletionCallback callback;
488 ChannelIDService::RequestHandle request_handle; 487 ChannelIDService::Request request;
489 488
490 // Asynchronous completion with no certs in the store. 489 // Asynchronous completion with no certs in the store.
491 scoped_ptr<crypto::ECPrivateKey> key; 490 scoped_ptr<crypto::ECPrivateKey> key;
492 EXPECT_EQ(0, service_->channel_id_count()); 491 EXPECT_EQ(0, service_->channel_id_count());
493 error = 492 error = service_->GetChannelID(host, &key, callback.callback(), &request);
494 service_->GetChannelID(host, &key, callback.callback(), &request_handle);
495 EXPECT_EQ(ERR_IO_PENDING, error); 493 EXPECT_EQ(ERR_IO_PENDING, error);
496 EXPECT_TRUE(request_handle.is_active()); 494 EXPECT_TRUE(request.is_active());
497 495
498 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 496 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
499 497
500 error = callback.WaitForResult(); 498 error = callback.WaitForResult();
501 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); 499 EXPECT_EQ(ERR_FILE_NOT_FOUND, error);
502 EXPECT_EQ(0, service_->channel_id_count()); 500 EXPECT_EQ(0, service_->channel_id_count());
503 EXPECT_EQ(0u, service_->workers_created()); 501 EXPECT_EQ(0u, service_->workers_created());
504 EXPECT_FALSE(key); 502 EXPECT_FALSE(key);
505 EXPECT_FALSE(request_handle.is_active()); 503 EXPECT_FALSE(request.is_active());
506 } 504 }
507 505
508 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { 506 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) {
509 MockChannelIDStoreWithAsyncGet* mock_store = 507 MockChannelIDStoreWithAsyncGet* mock_store =
510 new MockChannelIDStoreWithAsyncGet(); 508 new MockChannelIDStoreWithAsyncGet();
511 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 509 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
512 mock_store, base::MessageLoopProxy::current())); 510 mock_store, base::MessageLoopProxy::current()));
513 511
514 std::string host("encrypted.google.com"); 512 std::string host("encrypted.google.com");
515 513
516 int error; 514 int error;
517 TestCompletionCallback callback; 515 TestCompletionCallback callback;
518 ChannelIDService::RequestHandle request_handle; 516 ChannelIDService::Request request;
519 517
520 // Asynchronous completion with a cert in the store. 518 // Asynchronous completion with a cert in the store.
521 scoped_ptr<crypto::ECPrivateKey> key; 519 scoped_ptr<crypto::ECPrivateKey> key;
522 EXPECT_EQ(0, service_->channel_id_count()); 520 EXPECT_EQ(0, service_->channel_id_count());
523 error = service_->GetOrCreateChannelID(host, &key, callback.callback(), 521 error =
524 &request_handle); 522 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request);
525 EXPECT_EQ(ERR_IO_PENDING, error); 523 EXPECT_EQ(ERR_IO_PENDING, error);
526 EXPECT_TRUE(request_handle.is_active()); 524 EXPECT_TRUE(request.is_active());
527 525
528 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); 526 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create());
529 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); 527 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get());
530 528
531 error = callback.WaitForResult(); 529 error = callback.WaitForResult();
532 EXPECT_EQ(OK, error); 530 EXPECT_EQ(OK, error);
533 EXPECT_EQ(1, service_->channel_id_count()); 531 EXPECT_EQ(1, service_->channel_id_count());
534 EXPECT_EQ(1u, service_->requests()); 532 EXPECT_EQ(1u, service_->requests());
535 EXPECT_EQ(1u, service_->key_store_hits()); 533 EXPECT_EQ(1u, service_->key_store_hits());
536 // Because the cert was found in the store, no new workers should have been 534 // Because the cert was found in the store, no new workers should have been
537 // created. 535 // created.
538 EXPECT_EQ(0u, service_->workers_created()); 536 EXPECT_EQ(0u, service_->workers_created());
539 EXPECT_TRUE(key); 537 EXPECT_TRUE(key);
540 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); 538 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get()));
541 EXPECT_FALSE(request_handle.is_active()); 539 EXPECT_FALSE(request.is_active());
542 } 540 }
543 541
544 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { 542 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) {
545 MockChannelIDStoreWithAsyncGet* mock_store = 543 MockChannelIDStoreWithAsyncGet* mock_store =
546 new MockChannelIDStoreWithAsyncGet(); 544 new MockChannelIDStoreWithAsyncGet();
547 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 545 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
548 mock_store, base::MessageLoopProxy::current())); 546 mock_store, base::MessageLoopProxy::current()));
549 547
550 std::string host("encrypted.google.com"); 548 std::string host("encrypted.google.com");
551 549
552 int error; 550 int error;
553 TestCompletionCallback callback; 551 TestCompletionCallback callback;
554 ChannelIDService::RequestHandle request_handle; 552 ChannelIDService::Request request;
555 553
556 // Asynchronous completion with a cert in the store. 554 // Asynchronous completion with a cert in the store.
557 scoped_ptr<crypto::ECPrivateKey> key; 555 scoped_ptr<crypto::ECPrivateKey> key;
558 std::string private_key, spki; 556 std::string private_key, spki;
559 EXPECT_EQ(0, service_->channel_id_count()); 557 EXPECT_EQ(0, service_->channel_id_count());
560 error = 558 error = service_->GetChannelID(host, &key, callback.callback(), &request);
561 service_->GetChannelID(host, &key, callback.callback(), &request_handle);
562 EXPECT_EQ(ERR_IO_PENDING, error); 559 EXPECT_EQ(ERR_IO_PENDING, error);
563 EXPECT_TRUE(request_handle.is_active()); 560 EXPECT_TRUE(request.is_active());
564 561
565 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); 562 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create());
566 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); 563 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get());
567 564
568 error = callback.WaitForResult(); 565 error = callback.WaitForResult();
569 EXPECT_EQ(OK, error); 566 EXPECT_EQ(OK, error);
570 EXPECT_EQ(1, service_->channel_id_count()); 567 EXPECT_EQ(1, service_->channel_id_count());
571 EXPECT_EQ(1u, service_->requests()); 568 EXPECT_EQ(1u, service_->requests());
572 EXPECT_EQ(1u, service_->key_store_hits()); 569 EXPECT_EQ(1u, service_->key_store_hits());
573 // Because the cert was found in the store, no new workers should have been 570 // Because the cert was found in the store, no new workers should have been
574 // created. 571 // created.
575 EXPECT_EQ(0u, service_->workers_created()); 572 EXPECT_EQ(0u, service_->workers_created());
576 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); 573 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get()));
577 EXPECT_FALSE(request_handle.is_active()); 574 EXPECT_FALSE(request.is_active());
578 } 575 }
579 576
580 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { 577 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) {
581 MockChannelIDStoreWithAsyncGet* mock_store = 578 MockChannelIDStoreWithAsyncGet* mock_store =
582 new MockChannelIDStoreWithAsyncGet(); 579 new MockChannelIDStoreWithAsyncGet();
583 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 580 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
584 mock_store, base::MessageLoopProxy::current())); 581 mock_store, base::MessageLoopProxy::current()));
585 582
586 std::string host("encrypted.google.com"); 583 std::string host("encrypted.google.com");
587 584
588 int error; 585 int error;
589 586
590 // Asynchronous get with no certs in the store. 587 // Asynchronous get with no certs in the store.
591 TestCompletionCallback callback1; 588 TestCompletionCallback callback1;
592 ChannelIDService::RequestHandle request_handle1; 589 ChannelIDService::Request request1;
593 scoped_ptr<crypto::ECPrivateKey> key1; 590 scoped_ptr<crypto::ECPrivateKey> key1;
594 EXPECT_EQ(0, service_->channel_id_count()); 591 EXPECT_EQ(0, service_->channel_id_count());
595 error = service_->GetChannelID(host, &key1, callback1.callback(), 592 error = service_->GetChannelID(host, &key1, callback1.callback(), &request1);
596 &request_handle1);
597 EXPECT_EQ(ERR_IO_PENDING, error); 593 EXPECT_EQ(ERR_IO_PENDING, error);
598 EXPECT_TRUE(request_handle1.is_active()); 594 EXPECT_TRUE(request1.is_active());
599 595
600 // Asynchronous get/create with no certs in the store. 596 // Asynchronous get/create with no certs in the store.
601 TestCompletionCallback callback2; 597 TestCompletionCallback callback2;
602 ChannelIDService::RequestHandle request_handle2; 598 ChannelIDService::Request request2;
603 scoped_ptr<crypto::ECPrivateKey> key2; 599 scoped_ptr<crypto::ECPrivateKey> key2;
604 EXPECT_EQ(0, service_->channel_id_count()); 600 EXPECT_EQ(0, service_->channel_id_count());
605 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), 601 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(),
606 &request_handle2); 602 &request2);
607 EXPECT_EQ(ERR_IO_PENDING, error); 603 EXPECT_EQ(ERR_IO_PENDING, error);
608 EXPECT_TRUE(request_handle2.is_active()); 604 EXPECT_TRUE(request2.is_active());
609 605
610 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 606 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
611 607
612 // Even though the first request didn't ask to create a cert, it gets joined 608 // Even though the first request didn't ask to create a cert, it gets joined
613 // by the second, which does, so both succeed. 609 // by the second, which does, so both succeed.
614 error = callback1.WaitForResult(); 610 error = callback1.WaitForResult();
615 EXPECT_EQ(OK, error); 611 EXPECT_EQ(OK, error);
616 error = callback2.WaitForResult(); 612 error = callback2.WaitForResult();
617 EXPECT_EQ(OK, error); 613 EXPECT_EQ(OK, error);
618 614
619 // One cert is created, one request is joined. 615 // One cert is created, one request is joined.
620 EXPECT_EQ(2U, service_->requests()); 616 EXPECT_EQ(2U, service_->requests());
621 EXPECT_EQ(1, service_->channel_id_count()); 617 EXPECT_EQ(1, service_->channel_id_count());
622 EXPECT_EQ(1u, service_->workers_created()); 618 EXPECT_EQ(1u, service_->workers_created());
623 EXPECT_EQ(1u, service_->inflight_joins()); 619 EXPECT_EQ(1u, service_->inflight_joins());
624 EXPECT_TRUE(key1); 620 EXPECT_TRUE(key1);
625 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 621 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
626 EXPECT_FALSE(request_handle1.is_active()); 622 EXPECT_FALSE(request1.is_active());
627 EXPECT_FALSE(request_handle2.is_active()); 623 EXPECT_FALSE(request2.is_active());
628 } 624 }
629 625
630 } // namespace 626 } // namespace
631 627
632 } // namespace net 628 } // namespace net
OLDNEW
« no previous file with comments | « net/ssl/channel_id_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698