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