Index: content/browser/service_worker/link_header_support_unittest.cc |
diff --git a/content/browser/service_worker/link_header_support_unittest.cc b/content/browser/service_worker/link_header_support_unittest.cc |
index 8bc5922664524d64d295d5488048def563a60a03..34dfda9e24e4444abea3096e512dac6a45eb9ad5 100644 |
--- a/content/browser/service_worker/link_header_support_unittest.cc |
+++ b/content/browser/service_worker/link_header_support_unittest.cc |
@@ -7,22 +7,26 @@ |
#include "base/command_line.h" |
#include "base/logging.h" |
#include "base/run_loop.h" |
+#include "content/browser/loader/resource_request_info_impl.h" |
#include "content/browser/service_worker/embedded_worker_test_helper.h" |
#include "content/browser/service_worker/service_worker_context_wrapper.h" |
#include "content/browser/service_worker/service_worker_registration.h" |
-#include "content/public/browser/resource_request_info.h" |
+#include "content/browser/service_worker/service_worker_request_handler.h" |
#include "content/public/common/content_switches.h" |
#include "content/public/test/mock_resource_context.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
#include "net/http/http_response_headers.h" |
#include "net/url_request/url_request_test_job.h" |
#include "net/url_request/url_request_test_util.h" |
+#include "storage/browser/blob/blob_storage_context.h" |
#include "testing/gtest/include/gtest/gtest.h" |
namespace content { |
namespace { |
+const int kMockProviderId = 1; |
+ |
void SaveFoundRegistrationsCallback( |
ServiceWorkerStatusCode expected_status, |
bool* called, |
@@ -56,27 +60,53 @@ class LinkHeaderServiceWorkerTest : public ::testing::Test { |
void SetUp() override { |
helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
+ |
+ // An empty host. |
+ std::unique_ptr<ServiceWorkerProviderHost> host( |
+ new ServiceWorkerProviderHost( |
+ helper_->mock_render_process_id(), MSG_ROUTING_NONE, |
+ kMockProviderId, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
+ ServiceWorkerProviderHost::FrameSecurityLevel::UNINITIALIZED, |
+ context()->AsWeakPtr(), nullptr)); |
+ provider_host_ = host->AsWeakPtr(); |
+ context()->AddProviderHost(std::move(host)); |
} |
void TearDown() override { helper_.reset(); } |
+ ServiceWorkerContextCore* context() { return helper_->context(); } |
ServiceWorkerContextWrapper* context_wrapper() { |
return helper_->context_wrapper(); |
} |
+ ServiceWorkerProviderHost* provider_host() { return provider_host_.get(); } |
- void ProcessLinkHeader(const GURL& request_url, |
- const std::string& link_header) { |
+ std::unique_ptr<net::URLRequest> CreateRequest(const GURL& request_url, |
+ ResourceType resource_type) { |
std::unique_ptr<net::URLRequest> request = request_context_.CreateRequest( |
request_url, net::DEFAULT_PRIORITY, &request_delegate_); |
ResourceRequestInfo::AllocateForTesting( |
- request.get(), RESOURCE_TYPE_SCRIPT, &resource_context_, |
+ request.get(), resource_type, &resource_context_, |
-1 /* render_process_id */, -1 /* render_view_id */, |
- -1 /* render_frame_id */, false /* is_main_frame */, |
+ -1 /* render_frame_id */, resource_type == RESOURCE_TYPE_MAIN_FRAME, |
false /* parent_is_main_frame */, true /* allow_download */, |
true /* is_async */, false /* is_using_lofi */); |
+ ResourceRequestInfoImpl::ForRequest(request.get()) |
+ ->set_initiated_in_secure_context_for_testing(true); |
+ |
+ ServiceWorkerRequestHandler::InitializeHandler( |
+ request.get(), context_wrapper(), &blob_storage_context_, |
+ helper_->mock_render_process_id(), kMockProviderId, |
+ false /* skip_service_worker */, FETCH_REQUEST_MODE_NO_CORS, |
+ FETCH_CREDENTIALS_MODE_OMIT, FetchRedirectMode::FOLLOW_MODE, |
+ resource_type, REQUEST_CONTEXT_TYPE_HYPERLINK, |
+ REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, nullptr); |
+ |
+ return request; |
+ } |
- ProcessLinkHeaderForRequest(request.get(), link_header, context_wrapper()); |
- base::RunLoop().RunUntilIdle(); |
+ std::unique_ptr<net::URLRequest> CreateSubresourceRequest( |
+ const GURL& request_url) { |
+ return CreateRequest(request_url, RESOURCE_TYPE_SCRIPT); |
} |
std::vector<ServiceWorkerRegistrationInfo> GetRegistrations() { |
@@ -95,11 +125,15 @@ class LinkHeaderServiceWorkerTest : public ::testing::Test { |
net::TestURLRequestContext request_context_; |
net::TestDelegate request_delegate_; |
MockResourceContext resource_context_; |
+ base::WeakPtr<ServiceWorkerProviderHost> provider_host_; |
+ storage::BlobStorageContext blob_storage_context_; |
}; |
TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_Basic) { |
- ProcessLinkHeader(GURL("https://example.com/foo/bar/"), |
- "<../foo.js>; rel=serviceworker"); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), |
+ "<../foo.js>; rel=serviceworker", context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(1u, registrations.size()); |
@@ -109,8 +143,10 @@ TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_Basic) { |
} |
TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) { |
- ProcessLinkHeader(GURL("https://example.com/foo/bar/"), |
- "<../bar.js>; rel=serviceworker; scope=\"scope#ref\""); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), |
+ "<../bar.js>; rel=serviceworker; scope=\"scope#ref\"", context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(1u, registrations.size()); |
@@ -121,9 +157,12 @@ TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeWithFragment) { |
} |
TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) { |
- ProcessLinkHeader(GURL("https://example.com/foo/bar/"), |
- "<bar.js>; rel=serviceworker; " |
- "scope=\"https://example.com:443/foo/bar/scope\""); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foo/bar/")).get(), |
+ "<bar.js>; rel=serviceworker; " |
+ "scope=\"https://example.com:443/foo/bar/scope\"", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(1u, registrations.size()); |
@@ -134,17 +173,21 @@ TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeAbsoluteUrl) { |
} |
TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeDifferentOrigin) { |
- ProcessLinkHeader( |
- GURL("https://example.com/foobar/"), |
- "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\""); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), |
+ "<bar.js>; rel=serviceworker; scope=\"https://google.com/scope\"", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(0u, registrations.size()); |
} |
TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeUrlEncodedSlash) { |
- ProcessLinkHeader(GURL("https://example.com/foobar/"), |
- "<bar.js>; rel=serviceworker; scope=\"./foo%2Fbar\""); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), |
+ "<bar.js>; rel=serviceworker; scope=\"./foo%2Fbar\"", context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(0u, registrations.size()); |
@@ -152,17 +195,21 @@ TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScopeUrlEncodedSlash) { |
TEST_F(LinkHeaderServiceWorkerTest, |
InstallServiceWorker_ScriptUrlEncodedSlash) { |
- ProcessLinkHeader(GURL("https://example.com/foobar/"), |
- "<foo%2Fbar.js>; rel=serviceworker"); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), |
+ "<foo%2Fbar.js>; rel=serviceworker", context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(0u, registrations.size()); |
} |
TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScriptAbsoluteUrl) { |
- ProcessLinkHeader( |
- GURL("https://example.com/foobar/"), |
- "<https://example.com/bar.js>; rel=serviceworker; scope=foo"); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), |
+ "<https://example.com/bar.js>; rel=serviceworker; scope=foo", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(1u, registrations.size()); |
@@ -173,18 +220,23 @@ TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_ScriptAbsoluteUrl) { |
TEST_F(LinkHeaderServiceWorkerTest, |
InstallServiceWorker_ScriptDifferentOrigin) { |
- ProcessLinkHeader( |
- GURL("https://example.com/foobar/"), |
- "<https://google.com/bar.js>; rel=serviceworker; scope=foo"); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), |
+ "<https://google.com/bar.js>; rel=serviceworker; scope=foo", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(0u, registrations.size()); |
} |
TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) { |
- ProcessLinkHeader(GURL("https://example.com/foobar/"), |
- "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; " |
- "rel=serviceworker; scope=scope"); |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), |
+ "<bar.js>; rel=serviceworker; scope=foo, <baz.js>; " |
+ "rel=serviceworker; scope=scope", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(2u, registrations.size()); |
@@ -198,10 +250,12 @@ TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_MultipleWorkers) { |
TEST_F(LinkHeaderServiceWorkerTest, |
InstallServiceWorker_ValidAndInvalidValues) { |
- ProcessLinkHeader( |
- GURL("https://example.com/foobar/"), |
+ ProcessLinkHeaderForRequest( |
+ CreateSubresourceRequest(GURL("https://example.com/foobar/")).get(), |
"<https://google.com/bar.js>; rel=serviceworker; scope=foo, <baz.js>; " |
- "rel=serviceworker; scope=scope"); |
+ "rel=serviceworker; scope=scope", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
ASSERT_EQ(1u, registrations.size()); |
@@ -210,6 +264,70 @@ TEST_F(LinkHeaderServiceWorkerTest, |
registrations[0].active_version.script_url); |
} |
+TEST_F(LinkHeaderServiceWorkerTest, InstallServiceWorker_InsecureContext) { |
+ std::unique_ptr<net::URLRequest> request = |
+ CreateSubresourceRequest(GURL("https://example.com/foo/bar/")); |
+ ResourceRequestInfoImpl::ForRequest(request.get()) |
+ ->set_initiated_in_secure_context_for_testing(false); |
+ ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
+ ASSERT_EQ(0u, registrations.size()); |
+} |
+ |
+TEST_F(LinkHeaderServiceWorkerTest, |
+ InstallServiceWorker_NavigationFromInsecureContextToSecureContext) { |
+ std::unique_ptr<net::URLRequest> request = CreateRequest( |
+ GURL("https://example.com/foo/bar/"), RESOURCE_TYPE_MAIN_FRAME); |
+ ResourceRequestInfoImpl::ForRequest(request.get()) |
+ ->set_initiated_in_secure_context_for_testing(false); |
+ |
+ provider_host()->SetDocumentUrl(GURL("https://example.com/foo/bar/")); |
+ provider_host()->set_parent_frame_secure(true); |
+ |
+ ProcessLinkHeaderForRequest(request.get(), "<../foo.js>; rel=serviceworker", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
+ ASSERT_EQ(1u, registrations.size()); |
+ EXPECT_EQ(GURL("https://example.com/foo/"), registrations[0].pattern); |
+ EXPECT_EQ(GURL("https://example.com/foo/foo.js"), |
+ registrations[0].active_version.script_url); |
+} |
+ |
+TEST_F(LinkHeaderServiceWorkerTest, |
+ InstallServiceWorker_NavigationToInsecureContext) { |
+ provider_host()->SetDocumentUrl(GURL("http://example.com/foo/bar/")); |
+ provider_host()->set_parent_frame_secure(true); |
+ ProcessLinkHeaderForRequest(CreateRequest(GURL("http://example.com/foo/bar/"), |
+ RESOURCE_TYPE_MAIN_FRAME) |
+ .get(), |
+ "<../foo.js>; rel=serviceworker", |
+ context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
+ ASSERT_EQ(0u, registrations.size()); |
+} |
+ |
+TEST_F(LinkHeaderServiceWorkerTest, |
+ InstallServiceWorker_NavigationToInsecureHttpsContext) { |
+ provider_host()->SetDocumentUrl(GURL("https://example.com/foo/bar/")); |
+ provider_host()->set_parent_frame_secure(false); |
+ ProcessLinkHeaderForRequest( |
+ CreateRequest(GURL("https://example.com/foo/bar/"), |
+ RESOURCE_TYPE_MAIN_FRAME) |
+ .get(), |
+ "<../foo.js>; rel=serviceworker", context_wrapper()); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ std::vector<ServiceWorkerRegistrationInfo> registrations = GetRegistrations(); |
+ ASSERT_EQ(0u, registrations.size()); |
+} |
+ |
} // namespace |
} // namespace content |