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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
fuchsia_web / webengine / browser / javascript_browsertest.cc [blame]
// Copyright 2021 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/fuchsia/mem_buffer_util.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "content/public/test/browser_test.h"
#include "fuchsia_web/common/test/frame_for_test.h"
#include "fuchsia_web/common/test/frame_test_util.h"
#include "fuchsia_web/common/test/test_navigation_listener.h"
#include "fuchsia_web/webengine/browser/frame_impl_browser_test_base.h"
namespace {
constexpr char kPage1Path[] = "/title1.html";
constexpr char kPage1Title[] = "title 1";
constexpr char kDynamicTitlePath[] = "/dynamic_title.html";
constexpr int64_t kOnLoadScriptId = 0;
using JavaScriptTest = FrameImplTestBase;
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScript) {
constexpr int64_t kBindingsId = 1234;
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kBindingsId, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = 'hello';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlEquals(url);
}
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScriptUpdated) {
constexpr int64_t kBindingsId = 1234;
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kBindingsId, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = 'hello';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
// Verify that this script clobbers the previous script, as opposed to being
// injected alongside it. (The latter would result in the title being
// "helloclobber").
frame->AddBeforeLoadJavaScript(
kBindingsId, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = document.title + 'clobber';",
"test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, "clobber");
}
// Verifies that bindings are injected in order by producing a cumulative,
// non-commutative result.
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScriptOrdered) {
constexpr int64_t kBindingsId1 = 1234;
constexpr int64_t kBindingsId2 = 5678;
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kBindingsId1, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = 'hello';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
frame->AddBeforeLoadJavaScript(
kBindingsId2, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title += ' there';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, "hello there");
}
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScriptRemoved) {
constexpr int64_t kBindingsId1 = 1234;
constexpr int64_t kBindingsId2 = 5678;
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kBindingsId1, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = 'foo';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
// Add a script which clobbers "foo".
frame->AddBeforeLoadJavaScript(
kBindingsId2, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = 'bar';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
// Deletes the clobbering script.
frame->RemoveBeforeLoadJavaScript(kBindingsId2);
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, "foo");
}
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScriptRemoveInvalidId) {
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kPage1Path));
auto frame = FrameForTest::Create(context(), {});
frame->RemoveBeforeLoadJavaScript(kOnLoadScriptId);
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, kPage1Title);
}
// Test JS injection using ExecuteJavaScriptNoResult() to set a value, and
// ExecuteJavaScript() to retrieve that value.
IN_PROC_BROWSER_TEST_F(JavaScriptTest, ExecuteJavaScript) {
constexpr char kJsonStringLiteral[] = "\"I am a literal, literally\"";
auto frame = FrameForTest::Create(context(), {});
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
const GURL kUrl(embedded_test_server()->GetURL(kPage1Path));
// Navigate to a page and wait for it to finish loading.
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
kUrl.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, kPage1Title);
// Execute with no result to set the variable.
frame->ExecuteJavaScriptNoResult(
{kUrl.DeprecatedGetOriginAsURL().spec()},
base::MemBufferFromString(
base::StringPrintf("my_variable = %s;", kJsonStringLiteral), "test"),
[](fuchsia::web::Frame_ExecuteJavaScriptNoResult_Result result) {
EXPECT_TRUE(result.is_response());
});
// Execute a script snippet to return the variable's value.
base::RunLoop loop;
frame->ExecuteJavaScript(
{kUrl.DeprecatedGetOriginAsURL().spec()},
base::MemBufferFromString("my_variable;", "test"),
[&](fuchsia::web::Frame_ExecuteJavaScript_Result result) {
ASSERT_TRUE(result.is_response());
std::string result_json =
*base::StringFromMemBuffer(result.response().result);
EXPECT_EQ(result_json, kJsonStringLiteral);
loop.Quit();
});
loop.Run();
}
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScriptVmoDestroyed) {
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kOnLoadScriptId, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = 'hello';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, "hello");
}
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScriptWrongOrigin) {
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kOnLoadScriptId, {"http://example.com"},
base::MemBufferFromString("stashed_title = 'hello';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
// Expect that the original HTML title is used, because we didn't inject a
// script with a replacement title.
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(
url, "Welcome to Stan the Offline Dino's Homepage");
}
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BeforeLoadScriptWildcardOrigin) {
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kOnLoadScriptId, {"*"},
base::MemBufferFromString("stashed_title = 'hello';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
// Test script injection for the origin 127.0.0.1.
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, "hello");
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url::kAboutBlankURL));
frame.navigation_listener().RunUntilUrlEquals(GURL(url::kAboutBlankURL));
// Test script injection using a different origin ("localhost"), which should
// still be picked up by the wildcard.
GURL alt_url = embedded_test_server()->GetURL("localhost", kDynamicTitlePath);
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
alt_url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(alt_url, "hello");
}
// Test that we can inject scripts before and after RenderFrame creation.
IN_PROC_BROWSER_TEST_F(JavaScriptTest,
BeforeLoadScriptEarlyAndLateRegistrations) {
constexpr int64_t kOnLoadScriptId2 = kOnLoadScriptId + 1;
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kDynamicTitlePath));
auto frame = FrameForTest::Create(context(), {});
frame->AddBeforeLoadJavaScript(
kOnLoadScriptId, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title = 'hello';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, "hello");
frame->AddBeforeLoadJavaScript(
kOnLoadScriptId2, {embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("stashed_title += ' there';", "test"),
[](fuchsia::web::Frame_AddBeforeLoadJavaScript_Result result) {
EXPECT_TRUE(result.is_response());
});
// Navigate away to clean the slate.
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url::kAboutBlankURL));
frame.navigation_listener().RunUntilUrlEquals(GURL(url::kAboutBlankURL));
// Navigate back and see if both scripts are working.
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, "hello there");
}
IN_PROC_BROWSER_TEST_F(JavaScriptTest, BadEncoding) {
auto frame = FrameForTest::Create(context(), {});
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL url(embedded_test_server()->GetURL(kPage1Path));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(url, kPage1Title);
base::RunLoop run_loop;
// 0xFE is an illegal UTF-8 byte; it should cause UTF-8 conversion to fail.
frame->ExecuteJavaScriptNoResult(
{embedded_test_server()->GetOrigin().Serialize()},
base::MemBufferFromString("true;\xfe", "test"),
[&run_loop](fuchsia::web::Frame_ExecuteJavaScriptNoResult_Result result) {
EXPECT_TRUE(result.is_err());
EXPECT_EQ(result.err(), fuchsia::web::FrameError::BUFFER_NOT_UTF8);
run_loop.Quit();
});
run_loop.Run();
}
} // namespace