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 "content/browser/service_worker/service_worker_provider_host.h" | 5 #include "content/browser/service_worker/service_worker_provider_host.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
11 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 11 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
13 #include "content/browser/service_worker/service_worker_register_job.h" | 13 #include "content/browser/service_worker/service_worker_register_job.h" |
14 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
15 #include "content/browser/service_worker/service_worker_test_utils.h" | 15 #include "content/browser/service_worker/service_worker_test_utils.h" |
16 #include "content/browser/service_worker/service_worker_version.h" | 16 #include "content/browser/service_worker/service_worker_version.h" |
| 17 #include "content/common/url_schemes.h" |
17 #include "content/public/common/origin_util.h" | 18 #include "content/public/common/origin_util.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "content/test/test_content_browser_client.h" | 20 #include "content/test/test_content_browser_client.h" |
20 #include "content/test/test_content_client.h" | 21 #include "content/test/test_content_client.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 namespace content { | 24 namespace content { |
24 | 25 |
25 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; | 26 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; |
26 | 27 |
27 class ServiceWorkerTestContentClient : public TestContentClient { | 28 class ServiceWorkerTestContentClient : public TestContentClient { |
28 public: | 29 public: |
29 void AddServiceWorkerSchemes(std::set<std::string>* schemes) override { | 30 void AddAdditionalSchemes(Schemes* schemes) override { |
30 schemes->insert(kServiceWorkerScheme); | 31 schemes->service_worker_schemes.push_back(kServiceWorkerScheme); |
31 } | 32 } |
32 }; | 33 }; |
33 | 34 |
34 class ServiceWorkerProviderHostTest : public testing::Test { | 35 class ServiceWorkerProviderHostTest : public testing::Test { |
35 protected: | 36 protected: |
36 ServiceWorkerProviderHostTest() | 37 ServiceWorkerProviderHostTest() |
37 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 38 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
38 SetContentClient(&test_content_client_); | 39 SetContentClient(&test_content_client_); |
39 } | 40 } |
40 ~ServiceWorkerProviderHostTest() override {} | 41 ~ServiceWorkerProviderHostTest() override {} |
41 | 42 |
42 void SetUp() override { | 43 void SetUp() override { |
43 old_content_browser_client_ = | 44 old_content_browser_client_ = |
44 SetBrowserClientForTesting(&test_content_browser_client_); | 45 SetBrowserClientForTesting(&test_content_browser_client_); |
| 46 RefreshSecuritySchemesForTesting(); |
45 | 47 |
46 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 48 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
47 context_ = helper_->context(); | 49 context_ = helper_->context(); |
48 script_url_ = GURL("https://www.example.com/service_worker.js"); | 50 script_url_ = GURL("https://www.example.com/service_worker.js"); |
49 registration1_ = new ServiceWorkerRegistration( | 51 registration1_ = new ServiceWorkerRegistration( |
50 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); | 52 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr()); |
51 registration2_ = new ServiceWorkerRegistration( | 53 registration2_ = new ServiceWorkerRegistration( |
52 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); | 54 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr()); |
53 registration3_ = new ServiceWorkerRegistration( | 55 registration3_ = new ServiceWorkerRegistration( |
54 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); | 56 GURL("https://other.example.com/"), 3L, context_->AsWeakPtr()); |
(...skipping 17 matching lines...) Expand all Loading... |
72 provider_host2_ = host2->AsWeakPtr(); | 74 provider_host2_ = host2->AsWeakPtr(); |
73 context_->AddProviderHost(base::WrapUnique(host1.release())); | 75 context_->AddProviderHost(base::WrapUnique(host1.release())); |
74 context_->AddProviderHost(base::WrapUnique(host2.release())); | 76 context_->AddProviderHost(base::WrapUnique(host2.release())); |
75 } | 77 } |
76 | 78 |
77 void TearDown() override { | 79 void TearDown() override { |
78 registration1_ = 0; | 80 registration1_ = 0; |
79 registration2_ = 0; | 81 registration2_ = 0; |
80 helper_.reset(); | 82 helper_.reset(); |
81 SetBrowserClientForTesting(old_content_browser_client_); | 83 SetBrowserClientForTesting(old_content_browser_client_); |
| 84 // Reset cached security schemes so we don't affect other tests. |
| 85 RefreshSecuritySchemesForTesting(); |
82 } | 86 } |
83 | 87 |
84 bool PatternHasProcessToRun(const GURL& pattern) const { | 88 bool PatternHasProcessToRun(const GURL& pattern) const { |
85 return context_->process_manager()->PatternHasProcessToRun(pattern); | 89 return context_->process_manager()->PatternHasProcessToRun(pattern); |
86 } | 90 } |
87 | 91 |
88 content::TestBrowserThreadBundle thread_bundle_; | 92 TestBrowserThreadBundle thread_bundle_; |
89 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 93 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
90 ServiceWorkerContextCore* context_; | 94 ServiceWorkerContextCore* context_; |
91 scoped_refptr<ServiceWorkerRegistration> registration1_; | 95 scoped_refptr<ServiceWorkerRegistration> registration1_; |
92 scoped_refptr<ServiceWorkerRegistration> registration2_; | 96 scoped_refptr<ServiceWorkerRegistration> registration2_; |
93 scoped_refptr<ServiceWorkerRegistration> registration3_; | 97 scoped_refptr<ServiceWorkerRegistration> registration3_; |
94 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 98 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
95 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 99 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
96 GURL script_url_; | 100 GURL script_url_; |
97 ServiceWorkerTestContentClient test_content_client_; | 101 ServiceWorkerTestContentClient test_content_client_; |
98 TestContentBrowserClient test_content_browser_client_; | 102 TestContentBrowserClient test_content_browser_client_; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 166 |
163 // SetDocumentUrl with another origin also updates matching registrations | 167 // SetDocumentUrl with another origin also updates matching registrations |
164 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); | 168 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); |
165 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); | 169 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); |
166 provider_host1_->RemoveMatchingRegistration(registration3_.get()); | 170 provider_host1_->RemoveMatchingRegistration(registration3_.get()); |
167 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); | 171 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); |
168 } | 172 } |
169 | 173 |
170 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { | 174 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { |
171 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; | 175 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; |
172 content::ResetSchemesAndOriginsWhitelistForTesting(); | |
173 | 176 |
174 // Insecure document URL. | 177 // Insecure document URL. |
175 provider_host1_->SetDocumentUrl(GURL("http://host")); | 178 provider_host1_->SetDocumentUrl(GURL("http://host")); |
176 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; | 179 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; |
177 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 180 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
178 | 181 |
179 // Insecure parent frame. | 182 // Insecure parent frame. |
180 provider_host1_->SetDocumentUrl(GURL("https://host")); | 183 provider_host1_->SetDocumentUrl(GURL("https://host")); |
181 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 184 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; |
182 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 185 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
(...skipping 11 matching lines...) Expand all Loading... |
194 EXPECT_FALSE(IsOriginSecure(url)); | 197 EXPECT_FALSE(IsOriginSecure(url)); |
195 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); | 198 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); |
196 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); | 199 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); |
197 | 200 |
198 // Exceptional service worker scheme with insecure parent frame. | 201 // Exceptional service worker scheme with insecure parent frame. |
199 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; | 202 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; |
200 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); | 203 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); |
201 } | 204 } |
202 | 205 |
203 } // namespace content | 206 } // namespace content |
OLD | NEW |