blob: bffad522a4f3067741933f6f6c3f652577e9ba57 [file] [log] [blame]
Hiroki Nakagawad68ad2e2019-04-05 05:17:441// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file tests that Web Workers (a Content feature) work in the Chromium
6// embedder.
7
8#include "base/bind.h"
9#include "base/callback.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/ui/browser.h"
12#include "chrome/test/base/in_process_browser_test.h"
13#include "chrome/test/base/ui_test_utils.h"
14#include "components/content_settings/core/common/pref_names.h"
15#include "components/prefs/pref_service.h"
16#include "content/public/browser/browser_context.h"
17#include "content/public/test/browser_test_utils.h"
18#include "net/test/embedded_test_server/embedded_test_server.h"
19#include "net/test/embedded_test_server/http_request.h"
20#include "net/test/embedded_test_server/http_response.h"
21
22enum class EnableThirdPartyCookieBlocking { kEnable, kDisable };
23
24// A simple fixture used for testing dedicated workers and shared workers. The
25// fixture stashes the HTTP request to the worker script for inspecting the
26// headers.
27//
28// This is in //chrome instead of //content since the tests exercise the
29// |kBlockThirdPartyCookies| preference which is not a //content concept.
30class ChromeWorkerBrowserTest : public InProcessBrowserTest {
31 public:
32 void SetUp() override {
33 embedded_test_server()->RegisterRequestHandler(
34 base::BindRepeating(&ChromeWorkerBrowserTest::CaptureHeaderHandler,
35 base::Unretained(this), "/capture"));
36 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
37 InProcessBrowserTest::SetUp();
38 }
39
40 void SetUpOnMainThread() override {
41 embedded_test_server()->StartAcceptingConnections();
42 }
43
44 protected:
45 // Tests worker script fetch (always same-origin) is not affected by the
46 // third-party cookie blocking configuration.
47 // This is the regression test for https://crbug.com/933287.
48 void TestWorkerScriptFetchWithThirdPartyCookieBlocking(
49 EnableThirdPartyCookieBlocking enable_third_party_cookie_blocking,
50 const std::string& test_url) {
51 const std::string kCookie = "foo=bar";
52
53 // Set up third-party cookie blocking.
54 browser()->profile()->GetPrefs()->SetBoolean(
55 prefs::kBlockThirdPartyCookies,
56 enable_third_party_cookie_blocking ==
57 EnableThirdPartyCookieBlocking::kEnable);
58
59 // Make sure cookies are not set.
60 ASSERT_TRUE(
61 GetCookies(browser()->profile(), embedded_test_server()->base_url())
62 .empty());
63
64 // Request for the worker script should not send cookies.
65 {
66 base::RunLoop run_loop;
67 quit_closure_ = run_loop.QuitClosure();
68 ui_test_utils::NavigateToURL(browser(),
69 embedded_test_server()->GetURL(test_url));
70 run_loop.Run();
71 EXPECT_FALSE(base::ContainsKey(header_map_, "Cookie"));
72 }
73
74 // Set a cookie.
75 ASSERT_TRUE(SetCookie(browser()->profile(),
76 embedded_test_server()->base_url(), kCookie));
77
78 // Request for the worker script should send the cookie regardless of the
79 // third-party cookie blocking configuration.
80 {
81 base::RunLoop run_loop;
82 quit_closure_ = run_loop.QuitClosure();
83 ui_test_utils::NavigateToURL(browser(),
84 embedded_test_server()->GetURL(test_url));
85 run_loop.Run();
86 EXPECT_TRUE(base::ContainsKey(header_map_, "Cookie"));
87 EXPECT_EQ(kCookie, header_map_["Cookie"]);
88 }
89 }
90
91 // TODO(nhiroki): Add tests for creating workers from third-party iframes
92 // while third-party cookie blocking is enabled. This expects that cookies are
93 // not blocked.
94
95 private:
96 std::unique_ptr<net::test_server::HttpResponse> CaptureHeaderHandler(
97 const std::string& path,
98 const net::test_server::HttpRequest& request) {
99 if (request.GetURL().path() != path)
100 return nullptr;
101 // Stash the HTTP request headers.
102 header_map_ = request.headers;
103 std::move(quit_closure_).Run();
104 return std::make_unique<net::test_server::BasicHttpResponse>();
105 }
106
107 net::test_server::HttpRequest::HeaderMap header_map_;
108 base::OnceClosure quit_closure_;
109};
110
111IN_PROC_BROWSER_TEST_F(ChromeWorkerBrowserTest,
112 DedicatedWorkerScriptFetchWithThirdPartyBlocking) {
113 TestWorkerScriptFetchWithThirdPartyCookieBlocking(
114 EnableThirdPartyCookieBlocking::kEnable,
115 "/workers/create_dedicated_worker.html?worker_url=/capture");
116}
117
118IN_PROC_BROWSER_TEST_F(ChromeWorkerBrowserTest,
119 DedicatedWorkerScriptFetchWithoutThirdPartyBlocking) {
120 TestWorkerScriptFetchWithThirdPartyCookieBlocking(
121 EnableThirdPartyCookieBlocking::kDisable,
122 "/workers/create_dedicated_worker.html?worker_url=/capture");
123}
124
125IN_PROC_BROWSER_TEST_F(ChromeWorkerBrowserTest,
126 SharedWorkerScriptFetchWithThirdPartyBlocking) {
127 TestWorkerScriptFetchWithThirdPartyCookieBlocking(
128 EnableThirdPartyCookieBlocking::kEnable,
129 "/workers/create_shared_worker.html?worker_url=/capture");
130}
131
132IN_PROC_BROWSER_TEST_F(ChromeWorkerBrowserTest,
133 SharedWorkerScriptFetchWithoutThirdPartyBlocking) {
134 TestWorkerScriptFetchWithThirdPartyCookieBlocking(
135 EnableThirdPartyCookieBlocking::kDisable,
136 "/workers/create_shared_worker.html?worker_url=/capture");
137}