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

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 unused ChannelIDService::CancelRequest; add DCHECK to ChannelIDService::Request::Post 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 {
335 ChannelIDService::RequestHandle request_handle; 332 ChannelIDService::Request 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);
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 }
342 339
343 // Wait for reply from ChannelIDServiceWorker to be posted back to the 340 // Wait for reply from ChannelIDServiceWorker to be posted back to the
344 // ChannelIDService. 341 // ChannelIDService.
345 base::MessageLoop::current()->RunUntilIdle(); 342 base::MessageLoop::current()->RunUntilIdle();
346 343
347 // Even though the original request was cancelled, the service will still 344 // Even though the original request was cancelled, the service will still
348 // store the result, it just doesn't call the callback. 345 // store the result, it just doesn't call the callback.
349 EXPECT_EQ(1, service_->channel_id_count()); 346 EXPECT_EQ(1, service_->channel_id_count());
350 } 347 }
351 348
352 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { 349 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) {
353 std::string host("encrypted.google.com"); 350 std::string host("encrypted.google.com");
354 scoped_ptr<crypto::ECPrivateKey> key; 351 scoped_ptr<crypto::ECPrivateKey> key;
355 int error; 352 int error;
356 ChannelIDService::RequestHandle request_handle; 353 ChannelIDService::Request request;
357 354
358 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 355 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
359 &request_handle); 356 &request);
360 EXPECT_EQ(ERR_IO_PENDING, error); 357 EXPECT_EQ(ERR_IO_PENDING, error);
361 EXPECT_TRUE(request_handle.is_active()); 358 EXPECT_TRUE(request.is_active());
362 359
363 // Cancel request and destroy the ChannelIDService. 360 // Cancel request and destroy the ChannelIDService.
364 request_handle.Cancel(); 361 request.Cancel();
365 service_.reset(); 362 service_.reset();
366 363
367 // ChannelIDServiceWorker should not post anything back to the 364 // ChannelIDServiceWorker should not post anything back to the
368 // non-existent ChannelIDService, but run the loop just to be sure it 365 // non-existent ChannelIDService, but run the loop just to be sure it
369 // doesn't. 366 // doesn't.
370 base::MessageLoop::current()->RunUntilIdle(); 367 base::MessageLoop::current()->RunUntilIdle();
371 368
372 // If we got here without crashing or a valgrind error, it worked. 369 // If we got here without crashing or a valgrind error, it worked.
373 } 370 }
374 371
372 TEST_F(ChannelIDServiceTest, DestructionOfChannelIDServiceRequest) {
mattm 2015/06/09 23:08:35 Is this test different than ChannelIDServiceTest.C
nharper 2015/06/09 23:45:03 It looks like these tests should do the same thing
mattm 2015/06/10 00:14:24 Oh, that's a good point. I wasn't thinking about t
373 std::string host("encrypted.google.com");
374 scoped_ptr<crypto::ECPrivateKey> key;
375 int error;
376 TestCompletionCallback callback;
377 scoped_ptr<ChannelIDService::Request> request(
378 new ChannelIDService::Request());
379
380 error = service_->GetOrCreateChannelID(host, &key, callback.callback(),
381 request.get());
382 EXPECT_EQ(ERR_IO_PENDING, error);
383 EXPECT_TRUE(request->is_active());
384
385 // Delete the Request object, which should also cancel the request.
386 request.reset();
387
388 // Run the loop. If deleting the Request object didn't cancel the request,
389 // then running the request until completion will cause the test to crash
390 // (because it will try to update the now deleted Request object).
391 base::MessageLoop::current()->RunUntilIdle();
392
393 // If we got here without crashing or a valgrind error, it worked.
394 }
395
375 // Tests that shutting down the sequenced worker pool and then making new 396 // Tests that shutting down the sequenced worker pool and then making new
376 // requests gracefully fails. 397 // requests gracefully fails.
377 // This is a regression test for http://crbug.com/236387 398 // This is a regression test for http://crbug.com/236387
378 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { 399 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) {
379 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner); 400 scoped_refptr<FailingTaskRunner> task_runner(new FailingTaskRunner);
380 service_.reset(new ChannelIDService( 401 service_.reset(new ChannelIDService(
381 new DefaultChannelIDStore(NULL), task_runner)); 402 new DefaultChannelIDStore(NULL), task_runner));
382 403
383 // Make a request that will force synchronous completion. 404 // Make a request that will force synchronous completion.
384 std::string host("encrypted.google.com"); 405 std::string host("encrypted.google.com");
385 scoped_ptr<crypto::ECPrivateKey> key; 406 scoped_ptr<crypto::ECPrivateKey> key;
386 int error; 407 int error;
387 ChannelIDService::RequestHandle request_handle; 408 ChannelIDService::Request request;
388 409
389 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 410 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
390 &request_handle); 411 &request);
391 // If we got here without crashing or a valgrind error, it worked. 412 // If we got here without crashing or a valgrind error, it worked.
392 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error); 413 ASSERT_EQ(ERR_INSUFFICIENT_RESOURCES, error);
393 EXPECT_FALSE(request_handle.is_active()); 414 EXPECT_FALSE(request.is_active());
394 } 415 }
395 416
396 // Tests that simultaneous creation of different certs works. 417 // Tests that simultaneous creation of different certs works.
397 TEST_F(ChannelIDServiceTest, SimultaneousCreation) { 418 TEST_F(ChannelIDServiceTest, SimultaneousCreation) {
398 int error; 419 int error;
399 420
400 std::string host1("encrypted.google.com"); 421 std::string host1("encrypted.google.com");
401 scoped_ptr<crypto::ECPrivateKey> key1; 422 scoped_ptr<crypto::ECPrivateKey> key1;
402 TestCompletionCallback callback1; 423 TestCompletionCallback callback1;
403 ChannelIDService::RequestHandle request_handle1; 424 ChannelIDService::Request request1;
404 425
405 std::string host2("foo.com"); 426 std::string host2("foo.com");
406 scoped_ptr<crypto::ECPrivateKey> key2; 427 scoped_ptr<crypto::ECPrivateKey> key2;
407 TestCompletionCallback callback2; 428 TestCompletionCallback callback2;
408 ChannelIDService::RequestHandle request_handle2; 429 ChannelIDService::Request request2;
409 430
410 std::string host3("bar.com"); 431 std::string host3("bar.com");
411 scoped_ptr<crypto::ECPrivateKey> key3; 432 scoped_ptr<crypto::ECPrivateKey> key3;
412 TestCompletionCallback callback3; 433 TestCompletionCallback callback3;
413 ChannelIDService::RequestHandle request_handle3; 434 ChannelIDService::Request request3;
414 435
415 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(), 436 error = service_->GetOrCreateChannelID(host1, &key1, callback1.callback(),
416 &request_handle1); 437 &request1);
417 EXPECT_EQ(ERR_IO_PENDING, error); 438 EXPECT_EQ(ERR_IO_PENDING, error);
418 EXPECT_TRUE(request_handle1.is_active()); 439 EXPECT_TRUE(request1.is_active());
419 440
420 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(), 441 error = service_->GetOrCreateChannelID(host2, &key2, callback2.callback(),
421 &request_handle2); 442 &request2);
422 EXPECT_EQ(ERR_IO_PENDING, error); 443 EXPECT_EQ(ERR_IO_PENDING, error);
423 EXPECT_TRUE(request_handle2.is_active()); 444 EXPECT_TRUE(request2.is_active());
424 445
425 error = service_->GetOrCreateChannelID(host3, &key3, callback3.callback(), 446 error = service_->GetOrCreateChannelID(host3, &key3, callback3.callback(),
426 &request_handle3); 447 &request3);
427 EXPECT_EQ(ERR_IO_PENDING, error); 448 EXPECT_EQ(ERR_IO_PENDING, error);
428 EXPECT_TRUE(request_handle3.is_active()); 449 EXPECT_TRUE(request3.is_active());
429 450
430 error = callback1.WaitForResult(); 451 error = callback1.WaitForResult();
431 EXPECT_EQ(OK, error); 452 EXPECT_EQ(OK, error);
432 EXPECT_TRUE(key1); 453 EXPECT_TRUE(key1);
433 454
434 error = callback2.WaitForResult(); 455 error = callback2.WaitForResult();
435 EXPECT_EQ(OK, error); 456 EXPECT_EQ(OK, error);
436 EXPECT_TRUE(key2); 457 EXPECT_TRUE(key2);
437 458
438 error = callback3.WaitForResult(); 459 error = callback3.WaitForResult();
(...skipping 10 matching lines...) Expand all
449 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) { 470 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateNoChannelIDsInStore) {
450 MockChannelIDStoreWithAsyncGet* mock_store = 471 MockChannelIDStoreWithAsyncGet* mock_store =
451 new MockChannelIDStoreWithAsyncGet(); 472 new MockChannelIDStoreWithAsyncGet();
452 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 473 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
453 mock_store, base::MessageLoopProxy::current())); 474 mock_store, base::MessageLoopProxy::current()));
454 475
455 std::string host("encrypted.google.com"); 476 std::string host("encrypted.google.com");
456 477
457 int error; 478 int error;
458 TestCompletionCallback callback; 479 TestCompletionCallback callback;
459 ChannelIDService::RequestHandle request_handle; 480 ChannelIDService::Request request;
460 481
461 // Asynchronous completion with no certs in the store. 482 // Asynchronous completion with no certs in the store.
462 scoped_ptr<crypto::ECPrivateKey> key; 483 scoped_ptr<crypto::ECPrivateKey> key;
463 EXPECT_EQ(0, service_->channel_id_count()); 484 EXPECT_EQ(0, service_->channel_id_count());
464 error = service_->GetOrCreateChannelID(host, &key, callback.callback(), 485 error =
465 &request_handle); 486 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request);
466 EXPECT_EQ(ERR_IO_PENDING, error); 487 EXPECT_EQ(ERR_IO_PENDING, error);
467 EXPECT_TRUE(request_handle.is_active()); 488 EXPECT_TRUE(request.is_active());
468 489
469 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 490 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
470 491
471 error = callback.WaitForResult(); 492 error = callback.WaitForResult();
472 EXPECT_EQ(OK, error); 493 EXPECT_EQ(OK, error);
473 EXPECT_EQ(1, service_->channel_id_count()); 494 EXPECT_EQ(1, service_->channel_id_count());
474 EXPECT_TRUE(key); 495 EXPECT_TRUE(key);
475 EXPECT_FALSE(request_handle.is_active()); 496 EXPECT_FALSE(request.is_active());
476 } 497 }
477 498
478 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) { 499 TEST_F(ChannelIDServiceTest, AsyncStoreGetNoChannelIDsInStore) {
479 MockChannelIDStoreWithAsyncGet* mock_store = 500 MockChannelIDStoreWithAsyncGet* mock_store =
480 new MockChannelIDStoreWithAsyncGet(); 501 new MockChannelIDStoreWithAsyncGet();
481 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 502 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
482 mock_store, base::MessageLoopProxy::current())); 503 mock_store, base::MessageLoopProxy::current()));
483 504
484 std::string host("encrypted.google.com"); 505 std::string host("encrypted.google.com");
485 506
486 int error; 507 int error;
487 TestCompletionCallback callback; 508 TestCompletionCallback callback;
488 ChannelIDService::RequestHandle request_handle; 509 ChannelIDService::Request request;
489 510
490 // Asynchronous completion with no certs in the store. 511 // Asynchronous completion with no certs in the store.
491 scoped_ptr<crypto::ECPrivateKey> key; 512 scoped_ptr<crypto::ECPrivateKey> key;
492 EXPECT_EQ(0, service_->channel_id_count()); 513 EXPECT_EQ(0, service_->channel_id_count());
493 error = 514 error = service_->GetChannelID(host, &key, callback.callback(), &request);
494 service_->GetChannelID(host, &key, callback.callback(), &request_handle);
495 EXPECT_EQ(ERR_IO_PENDING, error); 515 EXPECT_EQ(ERR_IO_PENDING, error);
496 EXPECT_TRUE(request_handle.is_active()); 516 EXPECT_TRUE(request.is_active());
497 517
498 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 518 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
499 519
500 error = callback.WaitForResult(); 520 error = callback.WaitForResult();
501 EXPECT_EQ(ERR_FILE_NOT_FOUND, error); 521 EXPECT_EQ(ERR_FILE_NOT_FOUND, error);
502 EXPECT_EQ(0, service_->channel_id_count()); 522 EXPECT_EQ(0, service_->channel_id_count());
503 EXPECT_EQ(0u, service_->workers_created()); 523 EXPECT_EQ(0u, service_->workers_created());
504 EXPECT_FALSE(key); 524 EXPECT_FALSE(key);
505 EXPECT_FALSE(request_handle.is_active()); 525 EXPECT_FALSE(request.is_active());
506 } 526 }
507 527
508 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) { 528 TEST_F(ChannelIDServiceTest, AsyncStoreGetOrCreateOneCertInStore) {
509 MockChannelIDStoreWithAsyncGet* mock_store = 529 MockChannelIDStoreWithAsyncGet* mock_store =
510 new MockChannelIDStoreWithAsyncGet(); 530 new MockChannelIDStoreWithAsyncGet();
511 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 531 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
512 mock_store, base::MessageLoopProxy::current())); 532 mock_store, base::MessageLoopProxy::current()));
513 533
514 std::string host("encrypted.google.com"); 534 std::string host("encrypted.google.com");
515 535
516 int error; 536 int error;
517 TestCompletionCallback callback; 537 TestCompletionCallback callback;
518 ChannelIDService::RequestHandle request_handle; 538 ChannelIDService::Request request;
519 539
520 // Asynchronous completion with a cert in the store. 540 // Asynchronous completion with a cert in the store.
521 scoped_ptr<crypto::ECPrivateKey> key; 541 scoped_ptr<crypto::ECPrivateKey> key;
522 EXPECT_EQ(0, service_->channel_id_count()); 542 EXPECT_EQ(0, service_->channel_id_count());
523 error = service_->GetOrCreateChannelID(host, &key, callback.callback(), 543 error =
524 &request_handle); 544 service_->GetOrCreateChannelID(host, &key, callback.callback(), &request);
525 EXPECT_EQ(ERR_IO_PENDING, error); 545 EXPECT_EQ(ERR_IO_PENDING, error);
526 EXPECT_TRUE(request_handle.is_active()); 546 EXPECT_TRUE(request.is_active());
527 547
528 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); 548 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create());
529 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); 549 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get());
530 550
531 error = callback.WaitForResult(); 551 error = callback.WaitForResult();
532 EXPECT_EQ(OK, error); 552 EXPECT_EQ(OK, error);
533 EXPECT_EQ(1, service_->channel_id_count()); 553 EXPECT_EQ(1, service_->channel_id_count());
534 EXPECT_EQ(1u, service_->requests()); 554 EXPECT_EQ(1u, service_->requests());
535 EXPECT_EQ(1u, service_->key_store_hits()); 555 EXPECT_EQ(1u, service_->key_store_hits());
536 // Because the cert was found in the store, no new workers should have been 556 // Because the cert was found in the store, no new workers should have been
537 // created. 557 // created.
538 EXPECT_EQ(0u, service_->workers_created()); 558 EXPECT_EQ(0u, service_->workers_created());
539 EXPECT_TRUE(key); 559 EXPECT_TRUE(key);
540 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); 560 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get()));
541 EXPECT_FALSE(request_handle.is_active()); 561 EXPECT_FALSE(request.is_active());
542 } 562 }
543 563
544 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) { 564 TEST_F(ChannelIDServiceTest, AsyncStoreGetOneCertInStore) {
545 MockChannelIDStoreWithAsyncGet* mock_store = 565 MockChannelIDStoreWithAsyncGet* mock_store =
546 new MockChannelIDStoreWithAsyncGet(); 566 new MockChannelIDStoreWithAsyncGet();
547 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 567 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
548 mock_store, base::MessageLoopProxy::current())); 568 mock_store, base::MessageLoopProxy::current()));
549 569
550 std::string host("encrypted.google.com"); 570 std::string host("encrypted.google.com");
551 571
552 int error; 572 int error;
553 TestCompletionCallback callback; 573 TestCompletionCallback callback;
554 ChannelIDService::RequestHandle request_handle; 574 ChannelIDService::Request request;
555 575
556 // Asynchronous completion with a cert in the store. 576 // Asynchronous completion with a cert in the store.
557 scoped_ptr<crypto::ECPrivateKey> key; 577 scoped_ptr<crypto::ECPrivateKey> key;
558 std::string private_key, spki; 578 std::string private_key, spki;
559 EXPECT_EQ(0, service_->channel_id_count()); 579 EXPECT_EQ(0, service_->channel_id_count());
560 error = 580 error = service_->GetChannelID(host, &key, callback.callback(), &request);
561 service_->GetChannelID(host, &key, callback.callback(), &request_handle);
562 EXPECT_EQ(ERR_IO_PENDING, error); 581 EXPECT_EQ(ERR_IO_PENDING, error);
563 EXPECT_TRUE(request_handle.is_active()); 582 EXPECT_TRUE(request.is_active());
564 583
565 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create()); 584 scoped_ptr<crypto::ECPrivateKey> expected_key(crypto::ECPrivateKey::Create());
566 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get()); 585 mock_store->CallGetChannelIDCallbackWithResult(OK, expected_key.get());
567 586
568 error = callback.WaitForResult(); 587 error = callback.WaitForResult();
569 EXPECT_EQ(OK, error); 588 EXPECT_EQ(OK, error);
570 EXPECT_EQ(1, service_->channel_id_count()); 589 EXPECT_EQ(1, service_->channel_id_count());
571 EXPECT_EQ(1u, service_->requests()); 590 EXPECT_EQ(1u, service_->requests());
572 EXPECT_EQ(1u, service_->key_store_hits()); 591 EXPECT_EQ(1u, service_->key_store_hits());
573 // Because the cert was found in the store, no new workers should have been 592 // Because the cert was found in the store, no new workers should have been
574 // created. 593 // created.
575 EXPECT_EQ(0u, service_->workers_created()); 594 EXPECT_EQ(0u, service_->workers_created());
576 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get())); 595 EXPECT_TRUE(KeysEqual(expected_key.get(), key.get()));
577 EXPECT_FALSE(request_handle.is_active()); 596 EXPECT_FALSE(request.is_active());
578 } 597 }
579 598
580 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) { 599 TEST_F(ChannelIDServiceTest, AsyncStoreGetThenCreateNoCertsInStore) {
581 MockChannelIDStoreWithAsyncGet* mock_store = 600 MockChannelIDStoreWithAsyncGet* mock_store =
582 new MockChannelIDStoreWithAsyncGet(); 601 new MockChannelIDStoreWithAsyncGet();
583 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService( 602 service_ = scoped_ptr<ChannelIDService>(new ChannelIDService(
584 mock_store, base::MessageLoopProxy::current())); 603 mock_store, base::MessageLoopProxy::current()));
585 604
586 std::string host("encrypted.google.com"); 605 std::string host("encrypted.google.com");
587 606
588 int error; 607 int error;
589 608
590 // Asynchronous get with no certs in the store. 609 // Asynchronous get with no certs in the store.
591 TestCompletionCallback callback1; 610 TestCompletionCallback callback1;
592 ChannelIDService::RequestHandle request_handle1; 611 ChannelIDService::Request request1;
593 scoped_ptr<crypto::ECPrivateKey> key1; 612 scoped_ptr<crypto::ECPrivateKey> key1;
594 EXPECT_EQ(0, service_->channel_id_count()); 613 EXPECT_EQ(0, service_->channel_id_count());
595 error = service_->GetChannelID(host, &key1, callback1.callback(), 614 error = service_->GetChannelID(host, &key1, callback1.callback(), &request1);
596 &request_handle1);
597 EXPECT_EQ(ERR_IO_PENDING, error); 615 EXPECT_EQ(ERR_IO_PENDING, error);
598 EXPECT_TRUE(request_handle1.is_active()); 616 EXPECT_TRUE(request1.is_active());
599 617
600 // Asynchronous get/create with no certs in the store. 618 // Asynchronous get/create with no certs in the store.
601 TestCompletionCallback callback2; 619 TestCompletionCallback callback2;
602 ChannelIDService::RequestHandle request_handle2; 620 ChannelIDService::Request request2;
603 scoped_ptr<crypto::ECPrivateKey> key2; 621 scoped_ptr<crypto::ECPrivateKey> key2;
604 EXPECT_EQ(0, service_->channel_id_count()); 622 EXPECT_EQ(0, service_->channel_id_count());
605 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(), 623 error = service_->GetOrCreateChannelID(host, &key2, callback2.callback(),
606 &request_handle2); 624 &request2);
607 EXPECT_EQ(ERR_IO_PENDING, error); 625 EXPECT_EQ(ERR_IO_PENDING, error);
608 EXPECT_TRUE(request_handle2.is_active()); 626 EXPECT_TRUE(request2.is_active());
609 627
610 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr); 628 mock_store->CallGetChannelIDCallbackWithResult(ERR_FILE_NOT_FOUND, nullptr);
611 629
612 // Even though the first request didn't ask to create a cert, it gets joined 630 // Even though the first request didn't ask to create a cert, it gets joined
613 // by the second, which does, so both succeed. 631 // by the second, which does, so both succeed.
614 error = callback1.WaitForResult(); 632 error = callback1.WaitForResult();
615 EXPECT_EQ(OK, error); 633 EXPECT_EQ(OK, error);
616 error = callback2.WaitForResult(); 634 error = callback2.WaitForResult();
617 EXPECT_EQ(OK, error); 635 EXPECT_EQ(OK, error);
618 636
619 // One cert is created, one request is joined. 637 // One cert is created, one request is joined.
620 EXPECT_EQ(2U, service_->requests()); 638 EXPECT_EQ(2U, service_->requests());
621 EXPECT_EQ(1, service_->channel_id_count()); 639 EXPECT_EQ(1, service_->channel_id_count());
622 EXPECT_EQ(1u, service_->workers_created()); 640 EXPECT_EQ(1u, service_->workers_created());
623 EXPECT_EQ(1u, service_->inflight_joins()); 641 EXPECT_EQ(1u, service_->inflight_joins());
624 EXPECT_TRUE(key1); 642 EXPECT_TRUE(key1);
625 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 643 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
626 EXPECT_FALSE(request_handle1.is_active()); 644 EXPECT_FALSE(request1.is_active());
627 EXPECT_FALSE(request_handle2.is_active()); 645 EXPECT_FALSE(request2.is_active());
628 } 646 }
629 647
630 } // namespace 648 } // namespace
631 649
632 } // namespace net 650 } // 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