1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
content / browser / origin_trials / origin_trials_browsertest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/strcat.h"
#include "base/test/bind.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "content/shell/common/shell_switches.h"
using content::URLLoaderInterceptor;
namespace {
constexpr char kBaseDataDir[] = "content/test/data/origin_trials/";
void NavigateViaRenderer(content::WebContents* web_contents, const GURL& url) {
EXPECT_TRUE(
content::ExecJs(web_contents->GetPrimaryMainFrame(),
base::StrCat({"location.href='", url.spec(), "';"})));
// Enqueue a no-op script execution, which will block until the navigation
// initiated above completes.
EXPECT_TRUE(content::ExecJs(web_contents->GetPrimaryMainFrame(), "true"));
EXPECT_TRUE(content::WaitForLoadStop(web_contents));
EXPECT_EQ(web_contents->GetLastCommittedURL(), url);
}
} // namespace
namespace content {
class OriginTrialsBrowserTest : public content::ContentBrowserTest {
public:
OriginTrialsBrowserTest() : ContentBrowserTest() {}
OriginTrialsBrowserTest(const OriginTrialsBrowserTest&) = delete;
OriginTrialsBrowserTest& operator=(const OriginTrialsBrowserTest&) = delete;
~OriginTrialsBrowserTest() override {}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kExposeInternalsForTesting);
}
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
// We use a URLLoaderInterceptor, rather than the EmbeddedTestServer, since
// the origin trial token in the response is associated with a fixed
// origin, whereas EmbeddedTestServer serves content on a random port.
url_loader_interceptor_ =
std::make_unique<URLLoaderInterceptor>(base::BindLambdaForTesting(
[&](URLLoaderInterceptor::RequestParams* params) -> bool {
URLLoaderInterceptor::WriteResponse(
base::StrCat(
{kBaseDataDir, params->url_request.url.path_piece()}),
params->client.get());
return true;
}));
}
void TearDownOnMainThread() override {
url_loader_interceptor_.reset();
ContentBrowserTest::TearDownOnMainThread();
}
RenderFrameHost* GetFrameByName(const std::string frame_name) {
return FrameMatchingPredicate(
shell()->web_contents()->GetPrimaryPage(),
base::BindRepeating(FrameMatchesName, frame_name));
}
RenderFrameHost* GetPrimaryMainFrame() {
return shell()->web_contents()->GetPrimaryMainFrame();
}
testing::AssertionResult HasTrialEnabled(RenderFrameHost* frame) {
// Test if we can invoke normalMethod(), which is only available when the
// Frobulate OT is enabled.
return content::ExecJs(frame,
"internals.originTrialsTest().normalMethod();");
}
testing::AssertionResult HasNavigationTrialEnabled(RenderFrameHost* frame) {
// Test if we can invoke navigationMethod(), which is only available when
// the FrobulateNavigation OT is enabled.
return content::ExecJs(frame,
"internals.originTrialsTest().navigationMethod();");
}
private:
std::unique_ptr<URLLoaderInterceptor> url_loader_interceptor_;
};
IN_PROC_BROWSER_TEST_F(OriginTrialsBrowserTest, Basic) {
EXPECT_TRUE(NavigateToURL(shell(), GURL("https://example.test/basic.html")));
EXPECT_TRUE(HasTrialEnabled(GetPrimaryMainFrame()));
}
IN_PROC_BROWSER_TEST_F(OriginTrialsBrowserTest,
NonNavigationTrialNotActivatedAcrossNavigations) {
EXPECT_TRUE(NavigateToURL(shell(), GURL("https://example.test/basic.html")));
EXPECT_TRUE(HasTrialEnabled(GetPrimaryMainFrame()));
NavigateViaRenderer(shell()->web_contents(),
GURL("https://other.test/notrial.html"));
EXPECT_FALSE(HasTrialEnabled(GetPrimaryMainFrame()));
}
IN_PROC_BROWSER_TEST_F(OriginTrialsBrowserTest, Navigation) {
EXPECT_TRUE(
NavigateToURL(shell(), GURL("https://example.test/navigation.html")));
EXPECT_TRUE(HasNavigationTrialEnabled(GetPrimaryMainFrame()));
}
IN_PROC_BROWSER_TEST_F(OriginTrialsBrowserTest,
NavigationTrialActivatedAcrossNavigations) {
EXPECT_TRUE(
NavigateToURL(shell(), GURL("https://example.test/navigation.html")));
EXPECT_TRUE(HasNavigationTrialEnabled(GetPrimaryMainFrame()));
NavigateViaRenderer(shell()->web_contents(),
GURL("https://other.test/notrial.html"));
// Navigation trial should be enabled after navigating from navigation.html,
// because it is a cross-navigation OT.
EXPECT_TRUE(HasNavigationTrialEnabled(GetPrimaryMainFrame()));
NavigateViaRenderer(shell()->web_contents(),
GURL("https://other.test/basic.html"));
// Navigation trial should not be enabled after a second navigation, because
// cross-navigation OTs should only be forwarded to immediate navigations from
// where the trial was activated.
EXPECT_FALSE(HasNavigationTrialEnabled(GetPrimaryMainFrame()));
}
const char kCallWorkerScript[] =
"(() => {"
" const worker = new Worker('/worker.js');"
" const waitResult = new Promise((resolve, reject) => {"
" worker.onmessage = function(e) {"
" (e.data?resolve:reject)(`return ${e.data} from worker`);"
" };"
" });"
" worker.postMessage('ping');"
" return waitResult;"
"})()";
class ForceEnabledOriginTrialsBrowserTest
: public OriginTrialsBrowserTest,
public testing::WithParamInterface<bool>,
public WebContentsObserver {
public:
ForceEnabledOriginTrialsBrowserTest() = default;
~ForceEnabledOriginTrialsBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
OriginTrialsBrowserTest::SetUpCommandLine(command_line);
if (disable_site_isolation_)
command_line->AppendSwitch(switches::kDisableSiteIsolation);
}
void ReadyToCommitNavigation(NavigationHandle* navigation_handle) override {
if (navigation_handle->GetURL() == url_to_enable_trial_) {
navigation_handle->ForceEnableOriginTrials(
std::vector<std::string>({"Frobulate"}));
}
}
void set_url_to_enable_trial(const GURL& url) { url_to_enable_trial_ = url; }
protected:
bool disable_site_isolation_ = GetParam();
const GURL main_url_ =
GURL("https://example.test/force_enabled_main_frame.html");
private:
GURL url_to_enable_trial_;
};
IN_PROC_BROWSER_TEST_P(ForceEnabledOriginTrialsBrowserTest,
ForceEnabledOriginTrials_MainPage) {
set_url_to_enable_trial(main_url_);
Observe(shell()->web_contents());
EXPECT_TRUE(NavigateToURL(shell(), main_url_));
// Trial should be enabled for main frame.
EXPECT_TRUE(HasTrialEnabled(GetPrimaryMainFrame()));
// OT are enabled per-frame. Subframes should not have OT.
EXPECT_FALSE(HasTrialEnabled(GetFrameByName("same-origin")));
EXPECT_FALSE(HasTrialEnabled(GetFrameByName("cross-origin")));
EXPECT_FALSE(ExecJs(GetFrameByName("same-origin"), kCallWorkerScript));
// With site isolation, the cross-site iframe on |main_url_| will get its own
// process. Otherwise, we'll only get one main frame process.
ASSERT_EQ(AreAllSitesIsolatedForTesting() ? 2 : 1,
RenderProcessHost::GetCurrentRenderProcessCountForTesting());
// OT does not persist when we navigated away.
NavigateViaRenderer(shell()->web_contents(),
GURL("https://other.test/notrial.html"));
EXPECT_FALSE(HasTrialEnabled(GetPrimaryMainFrame()));
}
IN_PROC_BROWSER_TEST_P(ForceEnabledOriginTrialsBrowserTest,
ForceEnabledOriginTrials_IframeInPage) {
set_url_to_enable_trial(main_url_.Resolve("/notrial.html"));
Observe(shell()->web_contents());
EXPECT_TRUE(NavigateToURL(shell(), main_url_));
// Main frame does not have trial.
EXPECT_FALSE(HasTrialEnabled(GetPrimaryMainFrame()));
// Same-origin frame (which loads notrial.html) has trial.
EXPECT_TRUE(HasTrialEnabled(GetFrameByName("same-origin")));
// Cross-origin frame has no trial.
EXPECT_FALSE(HasTrialEnabled(GetFrameByName("cross-origin")));
// Worker in same-origin frame (which loads notrial.html>worker.js) has trial.
EXPECT_TRUE(ExecJs(GetFrameByName("same-origin"), kCallWorkerScript));
// When Iframe navigates away, it loses origin trial.
const GURL url("https://other.test/notrial.html");
TestNavigationObserver navigation_observer(url);
navigation_observer.WatchExistingWebContents();
ASSERT_TRUE(ExecJs(GetFrameByName("same-origin"),
content::JsReplace("location.href=$1", url.spec())));
navigation_observer.WaitForNavigationFinished();
EXPECT_FALSE(HasTrialEnabled(GetFrameByName("same-origin")));
}
IN_PROC_BROWSER_TEST_P(ForceEnabledOriginTrialsBrowserTest,
ForceEnabledOriginTrials_InjectedIframe) {
const GURL frame_url("https://newly-loaded.test/notrial.html");
set_url_to_enable_trial(frame_url);
Observe(shell()->web_contents());
EXPECT_TRUE(NavigateToURL(shell(), main_url_));
// Main frame and all iframes don't have trial.
EXPECT_FALSE(HasTrialEnabled(GetPrimaryMainFrame()));
EXPECT_FALSE(HasTrialEnabled(GetFrameByName("same-origin")));
EXPECT_FALSE(HasTrialEnabled(GetFrameByName("cross-origin")));
EXPECT_FALSE(ExecJs(GetFrameByName("same-origin"), kCallWorkerScript));
// Create an iframe with origin trial and wait for it to load
TestNavigationObserver navigation_observer(frame_url);
navigation_observer.WatchExistingWebContents();
ASSERT_TRUE(ExecJs(
GetFrameByName("same-origin"),
content::JsReplace("{"
" const ifrm = document.createElement('iframe');"
" ifrm.name='new-frame';"
" ifrm.src=$1;"
" document.body.appendChild(ifrm);"
"}",
frame_url.spec())));
navigation_observer.WaitForNavigationFinished();
// The newly created iframe should have origin trial.
EXPECT_TRUE(HasTrialEnabled(GetFrameByName("new-frame")));
}
INSTANTIATE_TEST_SUITE_P(SiteIsolation,
ForceEnabledOriginTrialsBrowserTest,
/*disable_site_isolation=*/::testing::Bool());
} // namespace content