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

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

Issue 2839663002: Instantiate ScopedTaskEnvironment in net unittests. (Closed)
Patch Set: self-review Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/ssl/channel_id_service.h" 5 #include "net/ssl/channel_id_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 14 #include "base/run_loop.h"
16 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
18 #include "base/task_runner.h" 17 #include "base/task_runner.h"
19 #include "base/task_scheduler/task_scheduler.h" 18 #include "base/task_scheduler/task_scheduler.h"
20 #include "base/test/scoped_task_scheduler.h" 19 #include "base/test/null_task_runner.h"
21 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
22 #include "crypto/ec_private_key.h" 21 #include "crypto/ec_private_key.h"
23 #include "net/base/net_errors.h" 22 #include "net/base/net_errors.h"
24 #include "net/base/test_completion_callback.h" 23 #include "net/base/test_completion_callback.h"
25 #include "net/cert/asn1_util.h" 24 #include "net/cert/asn1_util.h"
26 #include "net/cert/x509_certificate.h" 25 #include "net/cert/x509_certificate.h"
27 #include "net/ssl/default_channel_id_store.h" 26 #include "net/ssl/default_channel_id_store.h"
28 #include "net/test/channel_id_test_util.h" 27 #include "net/test/channel_id_test_util.h"
29 #include "net/test/gtest_util.h" 28 #include "net/test/gtest_util.h"
30 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 if (err == OK) 84 if (err == OK)
86 channel_id_count_ = 1; 85 channel_id_count_ = 1;
87 base::ThreadTaskRunnerHandle::Get()->PostTask( 86 base::ThreadTaskRunnerHandle::Get()->PostTask(
88 FROM_HERE, base::Bind(callback_, err, server_identifier_, 87 FROM_HERE, base::Bind(callback_, err, server_identifier_,
89 base::Passed(key ? key->Copy() : nullptr))); 88 base::Passed(key ? key->Copy() : nullptr)));
90 } 89 }
91 90
92 class ChannelIDServiceTest : public testing::Test { 91 class ChannelIDServiceTest : public testing::Test {
93 public: 92 public:
94 ChannelIDServiceTest() 93 ChannelIDServiceTest()
95 : scoped_task_scheduler_(base::MessageLoop::current()), 94 : service_(new ChannelIDService(new DefaultChannelIDStore(NULL))) {}
96 service_(new ChannelIDService(new DefaultChannelIDStore(NULL))) {}
97 95
98 protected: 96 protected:
99 base::test::ScopedTaskScheduler scoped_task_scheduler_;
100 std::unique_ptr<ChannelIDService> service_; 97 std::unique_ptr<ChannelIDService> service_;
101 }; 98 };
102 99
103 TEST_F(ChannelIDServiceTest, GetDomainForHost) { 100 TEST_F(ChannelIDServiceTest, GetDomainForHost) {
104 EXPECT_EQ("google.com", 101 EXPECT_EQ("google.com",
105 ChannelIDService::GetDomainForHost("google.com")); 102 ChannelIDService::GetDomainForHost("google.com"));
106 EXPECT_EQ("google.com", 103 EXPECT_EQ("google.com",
107 ChannelIDService::GetDomainForHost("www.google.com")); 104 ChannelIDService::GetDomainForHost("www.google.com"));
108 EXPECT_EQ("foo.appspot.com", 105 EXPECT_EQ("foo.appspot.com",
109 ChannelIDService::GetDomainForHost("foo.appspot.com")); 106 ChannelIDService::GetDomainForHost("foo.appspot.com"));
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 293
297 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 294 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
298 &request); 295 &request);
299 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); 296 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
300 EXPECT_TRUE(request.is_active()); 297 EXPECT_TRUE(request.is_active());
301 request.Cancel(); 298 request.Cancel();
302 EXPECT_FALSE(request.is_active()); 299 EXPECT_FALSE(request.is_active());
303 300
304 // Wait for reply from ChannelIDServiceWorker to be posted back to the 301 // Wait for reply from ChannelIDServiceWorker to be posted back to the
305 // ChannelIDService. 302 // ChannelIDService.
303 base::TaskScheduler::GetInstance()->FlushForTesting();
306 base::RunLoop().RunUntilIdle(); 304 base::RunLoop().RunUntilIdle();
307 305
308 // Even though the original request was cancelled, the service will still 306 // Even though the original request was cancelled, the service will still
309 // store the result, it just doesn't call the callback. 307 // store the result, it just doesn't call the callback.
310 EXPECT_EQ(1, service_->channel_id_count()); 308 EXPECT_EQ(1, service_->channel_id_count());
311 } 309 }
312 310
313 // Tests that destructing the Request cancels the request. 311 // Tests that destructing the Request cancels the request.
314 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) { 312 TEST_F(ChannelIDServiceTest, CancelRequestByHandleDestruction) {
315 std::string host("encrypted.google.com"); 313 std::string host("encrypted.google.com");
316 std::unique_ptr<crypto::ECPrivateKey> key; 314 std::unique_ptr<crypto::ECPrivateKey> key;
317 int error; 315 int error;
318 std::unique_ptr<ChannelIDService::Request> request( 316 std::unique_ptr<ChannelIDService::Request> request(
319 new ChannelIDService::Request()); 317 new ChannelIDService::Request());
320 318
321 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 319 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
322 request.get()); 320 request.get());
323 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); 321 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
324 EXPECT_TRUE(request->is_active()); 322 EXPECT_TRUE(request->is_active());
325 323
326 // Delete the Request object. 324 // Delete the Request object.
327 request.reset(); 325 request.reset();
328 326
329 // Wait for reply from ChannelIDServiceWorker to be posted back to the 327 // Wait for reply from ChannelIDServiceWorker to be posted back to the
330 // ChannelIDService. 328 // ChannelIDService.
329 base::TaskScheduler::GetInstance()->FlushForTesting();
331 base::RunLoop().RunUntilIdle(); 330 base::RunLoop().RunUntilIdle();
mmenke 2017/05/09 15:40:44 This means something different than the old code,
fdoray 2017/05/09 21:43:47 Addressed by calling ScopedTaskEnvironment::RunUnt
332 331
333 // Even though the original request was cancelled, the service will still 332 // Even though the original request was cancelled, the service will still
334 // store the result, it just doesn't call the callback. 333 // store the result, it just doesn't call the callback.
335 EXPECT_EQ(1, service_->channel_id_count()); 334 EXPECT_EQ(1, service_->channel_id_count());
336 } 335 }
337 336
338 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { 337 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) {
339 std::string host("encrypted.google.com"); 338 std::string host("encrypted.google.com");
340 std::unique_ptr<crypto::ECPrivateKey> key; 339 std::unique_ptr<crypto::ECPrivateKey> key;
341 int error; 340 int error;
342 ChannelIDService::Request request; 341 ChannelIDService::Request request;
343 342
344 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 343 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
345 &request); 344 &request);
346 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); 345 EXPECT_THAT(error, IsError(ERR_IO_PENDING));
347 EXPECT_TRUE(request.is_active()); 346 EXPECT_TRUE(request.is_active());
348 347
349 // Cancel request and destroy the ChannelIDService. 348 // Cancel request and destroy the ChannelIDService.
350 request.Cancel(); 349 request.Cancel();
351 service_.reset(); 350 service_.reset();
352 351
353 // ChannelIDServiceWorker should not post anything back to the 352 // ChannelIDServiceWorker should not post anything back to the
354 // non-existent ChannelIDService, but run the loop just to be sure it 353 // non-existent ChannelIDService, but run the loop just to be sure it
355 // doesn't. 354 // doesn't.
356 base::RunLoop().RunUntilIdle(); 355 base::RunLoop().RunUntilIdle();
357 356
358 // If we got here without crashing or a valgrind error, it worked. 357 // If we got here without crashing or a valgrind error, it worked.
359 } 358 }
360 359
361 // Tests that shutting down the TaskScheduler and then making new requests 360 // Tests that making new requests when the ChannelIDService can no longer post
362 // gracefully fails. 361 // tasks gracefully fails. This is a regression test for http://crbug.com/236387
363 // This is a regression test for http://crbug.com/236387
364 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { 362 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) {
365 // Drop all tasks posted to TaskScheduler from now on. 363 service_->set_task_runner_for_testing(
366 base::TaskScheduler::GetInstance()->Shutdown(); 364 make_scoped_refptr(new base::NullTaskRunner()));
367 365
368 // Make a request that will force synchronous completion. 366 // Make a request that will force synchronous completion.
369 std::string host("encrypted.google.com"); 367 std::string host("encrypted.google.com");
370 std::unique_ptr<crypto::ECPrivateKey> key; 368 std::unique_ptr<crypto::ECPrivateKey> key;
371 int error; 369 int error;
372 ChannelIDService::Request request; 370 ChannelIDService::Request request;
373 371
374 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), 372 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest),
375 &request); 373 &request);
376 // If we got here without crashing or a valgrind error, it worked. 374 // If we got here without crashing or a valgrind error, it worked.
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 EXPECT_EQ(1u, service_->inflight_joins()); 605 EXPECT_EQ(1u, service_->inflight_joins());
608 EXPECT_TRUE(key1); 606 EXPECT_TRUE(key1);
609 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); 607 EXPECT_TRUE(KeysEqual(key1.get(), key2.get()));
610 EXPECT_FALSE(request1.is_active()); 608 EXPECT_FALSE(request1.is_active());
611 EXPECT_FALSE(request2.is_active()); 609 EXPECT_FALSE(request2.is_active());
612 } 610 }
613 611
614 } // namespace 612 } // namespace
615 613
616 } // namespace net 614 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698