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

Side by Side Diff: content/browser/service_worker/service_worker_dispatcher_host_unittest.cc

Issue 2506263005: Prevent a service worker from keeping itself alive by self postMessage. (Closed)
Patch Set: rebase Created 4 years 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/browser/service_worker/service_worker_dispatcher_host.h" 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/test/simple_test_tick_clock.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "content/browser/browser_thread_impl.h" 17 #include "content/browser/browser_thread_impl.h"
17 #include "content/browser/message_port_service.h" 18 #include "content/browser/message_port_service.h"
18 #include "content/browser/service_worker/embedded_worker_instance.h" 19 #include "content/browser/service_worker/embedded_worker_instance.h"
19 #include "content/browser/service_worker/embedded_worker_registry.h" 20 #include "content/browser/service_worker/embedded_worker_registry.h"
20 #include "content/browser/service_worker/embedded_worker_status.h" 21 #include "content/browser/service_worker/embedded_worker_status.h"
21 #include "content/browser/service_worker/embedded_worker_test_helper.h" 22 #include "content/browser/service_worker/embedded_worker_test_helper.h"
22 #include "content/browser/service_worker/service_worker_context_core.h" 23 #include "content/browser/service_worker/service_worker_context_core.h"
23 #include "content/browser/service_worker/service_worker_context_wrapper.h" 24 #include "content/browser/service_worker/service_worker_context_wrapper.h"
24 #include "content/browser/service_worker/service_worker_handle.h" 25 #include "content/browser/service_worker/service_worker_handle.h"
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 // worker object information for the source attribute of the message event. 688 // worker object information for the source attribute of the message event.
688 provider_host_->running_hosted_version_ = version_; 689 provider_host_->running_hosted_version_ = version_;
689 690
690 // Set aside the initial refcount of the worker handle. 691 // Set aside the initial refcount of the worker handle.
691 provider_host_->GetOrCreateServiceWorkerHandle(version_.get()); 692 provider_host_->GetOrCreateServiceWorkerHandle(version_.get());
692 ServiceWorkerHandle* sender_worker_handle = 693 ServiceWorkerHandle* sender_worker_handle =
693 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(), 694 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(),
694 version_->version_id()); 695 version_->version_id());
695 const int ref_count = sender_worker_handle->ref_count(); 696 const int ref_count = sender_worker_handle->ref_count();
696 697
698 // Set mock clock on version_ to check timeout behavior.
699 base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock();
700 tick_clock->SetNowTicks(base::TimeTicks::Now());
701 version_->SetTickClockForTesting(base::WrapUnique(tick_clock));
702
703 // Make sure worker has a non-zero timeout.
704 bool called = false;
705 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
706 version_->StartWorker(ServiceWorkerMetrics::EventType::UNKNOWN,
707 base::Bind(&SaveStatusCallback, &called, &status));
708 base::RunLoop().RunUntilIdle();
709 EXPECT_TRUE(called);
710 EXPECT_EQ(SERVICE_WORKER_OK, status);
711 version_->StartRequestWithCustomTimeout(
712 ServiceWorkerMetrics::EventType::ACTIVATE,
713 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback),
714 base::TimeDelta::FromSeconds(10), ServiceWorkerVersion::KILL_ON_TIMEOUT);
715
716 // Advance clock by a couple seconds.
717 tick_clock->Advance(base::TimeDelta::FromSeconds(4));
718 base::TimeDelta remaining_time = version_->remaining_timeout();
719 EXPECT_EQ(base::TimeDelta::FromSeconds(6), remaining_time);
720
697 // Dispatch ExtendableMessageEvent. 721 // Dispatch ExtendableMessageEvent.
698 std::vector<int> ports; 722 std::vector<int> ports;
699 SetUpDummyMessagePort(&ports); 723 SetUpDummyMessagePort(&ports);
700 bool called = false; 724 called = false;
701 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 725 status = SERVICE_WORKER_ERROR_MAX_VALUE;
702 DispatchExtendableMessageEvent( 726 DispatchExtendableMessageEvent(
703 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), 727 version_, base::string16(), url::Origin(version_->scope().GetOrigin()),
704 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); 728 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status));
705 for (int port : ports) 729 for (int port : ports)
706 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); 730 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port));
707 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); 731 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count());
708 base::RunLoop().RunUntilIdle(); 732 base::RunLoop().RunUntilIdle();
709 EXPECT_TRUE(called); 733 EXPECT_TRUE(called);
710 EXPECT_EQ(SERVICE_WORKER_OK, status); 734 EXPECT_EQ(SERVICE_WORKER_OK, status);
711 735
712 // Messages should be held until ports are created at the destination. 736 // Messages should be held until ports are created at the destination.
713 for (int port : ports) 737 for (int port : ports)
714 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); 738 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port));
715 739
716 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count()); 740 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count());
741
742 // Timeout of message event should not have extended life of service worker.
743 EXPECT_EQ(remaining_time, version_->remaining_timeout());
717 } 744 }
718 745
719 TEST_P(ServiceWorkerDispatcherHostTestP, DispatchExtendableMessageEvent_Fail) { 746 TEST_P(ServiceWorkerDispatcherHostTestP, DispatchExtendableMessageEvent_Fail) {
720 GURL pattern = GURL("http://www.example.com/"); 747 GURL pattern = GURL("http://www.example.com/");
721 GURL script_url = GURL("http://www.example.com/service_worker.js"); 748 GURL script_url = GURL("http://www.example.com/service_worker.js");
722 749
723 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); 750 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper));
724 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); 751 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_WORKER, pattern);
725 SetUpRegistration(pattern, script_url); 752 SetUpRegistration(pattern, script_url);
726 753
727 // Set the running hosted version so that we can retrieve a valid service
728 // worker object information for the source attribute of the message event.
729 provider_host_->running_hosted_version_ = version_;
730
731 // Set aside the initial refcount of the worker handle.
732 provider_host_->GetOrCreateServiceWorkerHandle(version_.get());
733 ServiceWorkerHandle* sender_worker_handle =
734 dispatcher_host_->FindServiceWorkerHandle(provider_host_->provider_id(),
735 version_->version_id());
736 const int ref_count = sender_worker_handle->ref_count();
737
738 // Try to dispatch ExtendableMessageEvent. This should fail to start the 754 // Try to dispatch ExtendableMessageEvent. This should fail to start the
739 // worker and to dispatch the event. 755 // worker and to dispatch the event.
740 std::vector<int> ports; 756 std::vector<int> ports;
741 SetUpDummyMessagePort(&ports); 757 SetUpDummyMessagePort(&ports);
742 bool called = false; 758 bool called = false;
743 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE; 759 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
744 DispatchExtendableMessageEvent( 760 DispatchExtendableMessageEvent(
745 version_, base::string16(), url::Origin(version_->scope().GetOrigin()), 761 version_, base::string16(), url::Origin(version_->scope().GetOrigin()),
746 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status)); 762 ports, provider_host_, base::Bind(&SaveStatusCallback, &called, &status));
747 for (int port : ports) 763 for (int port : ports)
748 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port)); 764 EXPECT_TRUE(MessagePortService::GetInstance()->AreMessagesHeld(port));
749 EXPECT_EQ(ref_count + 1, sender_worker_handle->ref_count());
750 base::RunLoop().RunUntilIdle(); 765 base::RunLoop().RunUntilIdle();
751 EXPECT_TRUE(called); 766 EXPECT_TRUE(called);
752 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); 767 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
753 768
754 // The error callback should clean up the ports and handle. 769 // The error callback should clean up the ports and handle.
755 for (int port : ports) 770 for (int port : ports)
756 EXPECT_FALSE(MessagePortService::GetInstance()->AreMessagesHeld(port)); 771 EXPECT_FALSE(MessagePortService::GetInstance()->AreMessagesHeld(port));
757 EXPECT_EQ(ref_count, sender_worker_handle->ref_count());
758 } 772 }
759 773
760 TEST_P(ServiceWorkerDispatcherHostTestP, OnSetHostedVersionId) { 774 TEST_P(ServiceWorkerDispatcherHostTestP, OnSetHostedVersionId) {
761 GURL pattern = GURL("http://www.example.com/"); 775 GURL pattern = GURL("http://www.example.com/");
762 GURL script_url = GURL("http://www.example.com/service_worker.js"); 776 GURL script_url = GURL("http://www.example.com/service_worker.js");
763 777
764 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper)); 778 Initialize(base::WrapUnique(new FailToStartWorkerTestHelper));
765 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); 779 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern);
766 SetUpRegistration(pattern, script_url); 780 SetUpRegistration(pattern, script_url);
767 781
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 852
839 base::RunLoop().RunUntilIdle(); 853 base::RunLoop().RunUntilIdle();
840 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); 854 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_);
841 } 855 }
842 856
843 INSTANTIATE_TEST_CASE_P(ServiceWorkerDispatcherHostTest, 857 INSTANTIATE_TEST_CASE_P(ServiceWorkerDispatcherHostTest,
844 ServiceWorkerDispatcherHostTestP, 858 ServiceWorkerDispatcherHostTestP,
845 testing::Bool()); 859 testing::Bool());
846 860
847 } // namespace content 861 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698