| OLD | NEW |
| 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/test/null_task_runner.h" |
| 20 #include "base/test/scoped_task_scheduler.h" | 19 #include "base/test/scoped_task_environment.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" |
| 29 #include "net/test/net_test_suite.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 32 |
| 33 using net::test::IsError; | 33 using net::test::IsError; |
| 34 using net::test::IsOk; | 34 using net::test::IsOk; |
| 35 | 35 |
| 36 namespace net { | 36 namespace net { |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 if (err == OK) | 85 if (err == OK) |
| 86 channel_id_count_ = 1; | 86 channel_id_count_ = 1; |
| 87 base::ThreadTaskRunnerHandle::Get()->PostTask( | 87 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 88 FROM_HERE, base::Bind(callback_, err, server_identifier_, | 88 FROM_HERE, base::Bind(callback_, err, server_identifier_, |
| 89 base::Passed(key ? key->Copy() : nullptr))); | 89 base::Passed(key ? key->Copy() : nullptr))); |
| 90 } | 90 } |
| 91 | 91 |
| 92 class ChannelIDServiceTest : public testing::Test { | 92 class ChannelIDServiceTest : public testing::Test { |
| 93 public: | 93 public: |
| 94 ChannelIDServiceTest() | 94 ChannelIDServiceTest() |
| 95 : scoped_task_scheduler_(base::MessageLoop::current()), | 95 : service_(new ChannelIDService(new DefaultChannelIDStore(NULL))) {} |
| 96 service_(new ChannelIDService(new DefaultChannelIDStore(NULL))) {} | |
| 97 | 96 |
| 98 protected: | 97 protected: |
| 99 base::test::ScopedTaskScheduler scoped_task_scheduler_; | |
| 100 std::unique_ptr<ChannelIDService> service_; | 98 std::unique_ptr<ChannelIDService> service_; |
| 101 }; | 99 }; |
| 102 | 100 |
| 103 TEST_F(ChannelIDServiceTest, GetDomainForHost) { | 101 TEST_F(ChannelIDServiceTest, GetDomainForHost) { |
| 104 EXPECT_EQ("google.com", | 102 EXPECT_EQ("google.com", |
| 105 ChannelIDService::GetDomainForHost("google.com")); | 103 ChannelIDService::GetDomainForHost("google.com")); |
| 106 EXPECT_EQ("google.com", | 104 EXPECT_EQ("google.com", |
| 107 ChannelIDService::GetDomainForHost("www.google.com")); | 105 ChannelIDService::GetDomainForHost("www.google.com")); |
| 108 EXPECT_EQ("foo.appspot.com", | 106 EXPECT_EQ("foo.appspot.com", |
| 109 ChannelIDService::GetDomainForHost("foo.appspot.com")); | 107 ChannelIDService::GetDomainForHost("foo.appspot.com")); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 294 |
| 297 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), | 295 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 298 &request); | 296 &request); |
| 299 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); | 297 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); |
| 300 EXPECT_TRUE(request.is_active()); | 298 EXPECT_TRUE(request.is_active()); |
| 301 request.Cancel(); | 299 request.Cancel(); |
| 302 EXPECT_FALSE(request.is_active()); | 300 EXPECT_FALSE(request.is_active()); |
| 303 | 301 |
| 304 // Wait for reply from ChannelIDServiceWorker to be posted back to the | 302 // Wait for reply from ChannelIDServiceWorker to be posted back to the |
| 305 // ChannelIDService. | 303 // ChannelIDService. |
| 306 base::RunLoop().RunUntilIdle(); | 304 NetTestSuite::GetScopedTaskEnvironment()->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. |
| 331 base::RunLoop().RunUntilIdle(); | 329 NetTestSuite::GetScopedTaskEnvironment()->RunUntilIdle(); |
| 332 | 330 |
| 333 // Even though the original request was cancelled, the service will still | 331 // Even though the original request was cancelled, the service will still |
| 334 // store the result, it just doesn't call the callback. | 332 // store the result, it just doesn't call the callback. |
| 335 EXPECT_EQ(1, service_->channel_id_count()); | 333 EXPECT_EQ(1, service_->channel_id_count()); |
| 336 } | 334 } |
| 337 | 335 |
| 338 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { | 336 TEST_F(ChannelIDServiceTest, DestructionWithPendingRequest) { |
| 339 std::string host("encrypted.google.com"); | 337 std::string host("encrypted.google.com"); |
| 340 std::unique_ptr<crypto::ECPrivateKey> key; | 338 std::unique_ptr<crypto::ECPrivateKey> key; |
| 341 int error; | 339 int error; |
| 342 ChannelIDService::Request request; | 340 ChannelIDService::Request request; |
| 343 | 341 |
| 344 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), | 342 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 345 &request); | 343 &request); |
| 346 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); | 344 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); |
| 347 EXPECT_TRUE(request.is_active()); | 345 EXPECT_TRUE(request.is_active()); |
| 348 | 346 |
| 349 // Cancel request and destroy the ChannelIDService. | 347 // Cancel request and destroy the ChannelIDService. |
| 350 request.Cancel(); | 348 request.Cancel(); |
| 351 service_.reset(); | 349 service_.reset(); |
| 352 | 350 |
| 353 // ChannelIDServiceWorker should not post anything back to the | 351 // ChannelIDServiceWorker should not post anything back to the |
| 354 // non-existent ChannelIDService, but run the loop just to be sure it | 352 // non-existent ChannelIDService, but run the loop just to be sure it |
| 355 // doesn't. | 353 // doesn't. |
| 356 base::RunLoop().RunUntilIdle(); | 354 base::RunLoop().RunUntilIdle(); |
| 357 | 355 |
| 358 // If we got here without crashing or a valgrind error, it worked. | 356 // If we got here without crashing or a valgrind error, it worked. |
| 359 } | 357 } |
| 360 | 358 |
| 361 // Tests that shutting down the TaskScheduler and then making new requests | 359 // Tests that making new requests when the ChannelIDService can no longer post |
| 362 // gracefully fails. | 360 // 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) { | 361 TEST_F(ChannelIDServiceTest, RequestAfterPoolShutdown) { |
| 365 // Drop all tasks posted to TaskScheduler from now on. | 362 service_->set_task_runner_for_testing( |
| 366 base::TaskScheduler::GetInstance()->Shutdown(); | 363 make_scoped_refptr(new base::NullTaskRunner())); |
| 367 | 364 |
| 368 // Make a request that will force synchronous completion. | 365 // Make a request that will force synchronous completion. |
| 369 std::string host("encrypted.google.com"); | 366 std::string host("encrypted.google.com"); |
| 370 std::unique_ptr<crypto::ECPrivateKey> key; | 367 std::unique_ptr<crypto::ECPrivateKey> key; |
| 371 int error; | 368 int error; |
| 372 ChannelIDService::Request request; | 369 ChannelIDService::Request request; |
| 373 | 370 |
| 374 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), | 371 error = service_->GetOrCreateChannelID(host, &key, base::Bind(&FailTest), |
| 375 &request); | 372 &request); |
| 376 // If we got here without crashing or a valgrind error, it worked. | 373 // If we got here without crashing or a valgrind error, it worked. |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 EXPECT_EQ(1u, service_->inflight_joins()); | 604 EXPECT_EQ(1u, service_->inflight_joins()); |
| 608 EXPECT_TRUE(key1); | 605 EXPECT_TRUE(key1); |
| 609 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); | 606 EXPECT_TRUE(KeysEqual(key1.get(), key2.get())); |
| 610 EXPECT_FALSE(request1.is_active()); | 607 EXPECT_FALSE(request1.is_active()); |
| 611 EXPECT_FALSE(request2.is_active()); | 608 EXPECT_FALSE(request2.is_active()); |
| 612 } | 609 } |
| 613 | 610 |
| 614 } // namespace | 611 } // namespace |
| 615 | 612 |
| 616 } // namespace net | 613 } // namespace net |
| OLD | NEW |