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
fuchsia_web / webengine / browser / post_message_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/test/test_future.h"
#include "content/public/test/browser_test.h"
#include "fuchsia_web/common/test/fit_adapter.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";
// Defines a suite of tests that exercise Frame-level post message
// functionality.
class PostMessageTest : public FrameImplTestBase {
public:
PostMessageTest() = default;
~PostMessageTest() override = default;
PostMessageTest(const PostMessageTest&) = delete;
PostMessageTest& operator=(const PostMessageTest&) = delete;
};
IN_PROC_BROWSER_TEST_F(PostMessageTest, SendData) {
auto frame = FrameForTest::Create(context(), {});
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL post_message_url(
embedded_test_server()->GetURL("/window_post_message.html"));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
post_message_url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(post_message_url,
"postmessage");
fuchsia::web::WebMessage message;
message.set_data(base::MemBufferFromString(kPage1Path, "test"));
base::test::TestFuture<fuchsia::web::Frame_PostMessage_Result> post_result;
frame->PostMessage(post_message_url.DeprecatedGetOriginAsURL().spec(),
std::move(message),
CallbackToFitFunction(post_result.GetCallback()));
ASSERT_TRUE(post_result.Wait());
frame.navigation_listener().RunUntilUrlAndTitleEquals(
embedded_test_server()->GetURL(kPage1Path), kPage1Title);
EXPECT_TRUE(post_result.Get().is_response());
}
// Send a MessagePort to the content, then perform bidirectional messaging
// through the port.
IN_PROC_BROWSER_TEST_F(PostMessageTest, PassMessagePort) {
auto frame = FrameForTest::Create(context(), {});
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL post_message_url(embedded_test_server()->GetURL("/message_port.html"));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
post_message_url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(post_message_url,
"messageport");
fuchsia::web::MessagePortPtr message_port;
{
base::test::TestFuture<fuchsia::web::Frame_PostMessage_Result> post_result;
frame->PostMessage(
post_message_url.DeprecatedGetOriginAsURL().spec(),
CreateWebMessageWithMessagePortRequest(
message_port.NewRequest(), base::MemBufferFromString("hi", "test")),
CallbackToFitFunction(post_result.GetCallback()));
base::test::TestFuture<fuchsia::web::WebMessage> receiver;
message_port->ReceiveMessage(CallbackToFitFunction(receiver.GetCallback()));
ASSERT_TRUE(receiver.Wait());
ASSERT_TRUE(receiver.Get().has_data());
EXPECT_EQ("got_port", *base::StringFromMemBuffer(receiver.Get().data()));
}
{
fuchsia::web::WebMessage msg;
msg.set_data(base::MemBufferFromString("ping", "test"));
base::test::TestFuture<fuchsia::web::MessagePort_PostMessage_Result>
post_result;
message_port->PostMessage(std::move(msg),
CallbackToFitFunction(post_result.GetCallback()));
base::test::TestFuture<fuchsia::web::WebMessage> receiver;
message_port->ReceiveMessage(CallbackToFitFunction(receiver.GetCallback()));
ASSERT_TRUE(post_result.Wait());
ASSERT_TRUE(receiver.Wait());
ASSERT_TRUE(receiver.Get().has_data());
EXPECT_EQ("ack ping", *base::StringFromMemBuffer(receiver.Get().data()));
EXPECT_TRUE(post_result.Get().is_response());
}
}
// Send a MessagePort to the content, then perform bidirectional messaging
// over its channel.
IN_PROC_BROWSER_TEST_F(PostMessageTest, MessagePortDisconnected) {
auto frame = FrameForTest::Create(context(), {});
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL post_message_url(embedded_test_server()->GetURL("/message_port.html"));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
post_message_url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(post_message_url,
"messageport");
fuchsia::web::MessagePortPtr message_port;
{
base::test::TestFuture<fuchsia::web::Frame_PostMessage_Result> post_result;
frame->PostMessage(
post_message_url.DeprecatedGetOriginAsURL().spec(),
CreateWebMessageWithMessagePortRequest(
message_port.NewRequest(), base::MemBufferFromString("hi", "test")),
CallbackToFitFunction(post_result.GetCallback()));
base::test::TestFuture<fuchsia::web::WebMessage> receiver;
message_port->ReceiveMessage(CallbackToFitFunction(receiver.GetCallback()));
ASSERT_TRUE(post_result.Wait());
ASSERT_TRUE(receiver.Wait());
ASSERT_TRUE(receiver.IsReady());
EXPECT_EQ("got_port", *base::StringFromMemBuffer(receiver.Get().data()));
EXPECT_TRUE(post_result.Get().is_response());
}
// Navigating off-page should tear down the Mojo channel, thereby causing the
// MessagePortImpl to self-destruct and tear down its FIDL channel.
{
base::RunLoop run_loop;
message_port.set_error_handler(
[&run_loop](zx_status_t) { run_loop.Quit(); });
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url::kAboutBlankURL));
run_loop.Run();
}
}
// Send a MessagePort to the content, and through that channel, receive a
// different MessagePort that was created by the content. Verify the second
// channel's liveness by sending a ping to it.
IN_PROC_BROWSER_TEST_F(PostMessageTest, UseContentProvidedPort) {
auto frame = FrameForTest::Create(context(), {});
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL post_message_url(embedded_test_server()->GetURL("/message_port.html"));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
post_message_url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(post_message_url,
"messageport");
fuchsia::web::MessagePortPtr incoming_message_port;
{
fuchsia::web::MessagePortPtr message_port;
base::test::TestFuture<fuchsia::web::Frame_PostMessage_Result> post_result;
frame->PostMessage(
"*",
CreateWebMessageWithMessagePortRequest(
message_port.NewRequest(), base::MemBufferFromString("hi", "test")),
CallbackToFitFunction(post_result.GetCallback()));
base::test::TestFuture<fuchsia::web::WebMessage> receiver;
message_port->ReceiveMessage(CallbackToFitFunction(receiver.GetCallback()));
ASSERT_TRUE(receiver.Wait());
ASSERT_TRUE(receiver.Get().has_data());
EXPECT_EQ("got_port", *base::StringFromMemBuffer(receiver.Get().data()));
ASSERT_TRUE(receiver.Get().has_incoming_transfer());
ASSERT_EQ(receiver.Get().incoming_transfer().size(), 1u);
incoming_message_port = receiver.Take()
.mutable_incoming_transfer()
->at(0)
.message_port()
.Bind();
EXPECT_TRUE(post_result.Get().is_response());
}
// Get the content to send three 'ack ping' messages, which will accumulate in
// the MessagePortImpl buffer.
for (int i = 0; i < 3; ++i) {
base::test::TestFuture<fuchsia::web::MessagePort_PostMessage_Result>
post_result;
fuchsia::web::WebMessage msg;
msg.set_data(base::MemBufferFromString("ping", "test"));
incoming_message_port->PostMessage(
std::move(msg), CallbackToFitFunction(post_result.GetCallback()));
ASSERT_TRUE(post_result.Wait());
EXPECT_TRUE(post_result.Get().is_response());
}
// Receive another acknowledgement from content on a side channel to ensure
// that all the "ack pings" are ready to be consumed.
{
fuchsia::web::MessagePortPtr ack_message_port;
// Quit the runloop only after we've received a WebMessage AND a PostMessage
// result.
base::test::TestFuture<fuchsia::web::Frame_PostMessage_Result> post_result;
frame->PostMessage("*",
CreateWebMessageWithMessagePortRequest(
ack_message_port.NewRequest(),
base::MemBufferFromString("hi", "test")),
CallbackToFitFunction(post_result.GetCallback()));
base::test::TestFuture<fuchsia::web::WebMessage> receiver;
ack_message_port->ReceiveMessage(
CallbackToFitFunction(receiver.GetCallback()));
ASSERT_TRUE(receiver.Wait());
ASSERT_TRUE(receiver.Get().has_data());
EXPECT_EQ("got_port", *base::StringFromMemBuffer(receiver.Get().data()));
EXPECT_TRUE(post_result.Get().is_response());
}
// Pull the three 'ack ping's from the buffer.
for (int i = 0; i < 3; ++i) {
base::test::TestFuture<fuchsia::web::WebMessage> receiver;
incoming_message_port->ReceiveMessage(
CallbackToFitFunction(receiver.GetCallback()));
ASSERT_TRUE(receiver.Wait());
ASSERT_TRUE(receiver.Get().has_data());
EXPECT_EQ("ack ping", *base::StringFromMemBuffer(receiver.Get().data()));
}
}
IN_PROC_BROWSER_TEST_F(PostMessageTest, BadOriginDropped) {
auto frame = FrameForTest::Create(context(), {});
net::test_server::EmbeddedTestServerHandle test_server_handle;
ASSERT_TRUE(test_server_handle =
embedded_test_server()->StartAndReturnHandle());
GURL post_message_url(embedded_test_server()->GetURL("/message_port.html"));
EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
post_message_url.spec()));
frame.navigation_listener().RunUntilUrlAndTitleEquals(post_message_url,
"messageport");
// PostMessage() to invalid origins should be ignored. We pass in a
// MessagePort but nothing should happen to it.
fuchsia::web::MessagePortPtr bad_origin_incoming_message_port;
base::test::TestFuture<fuchsia::web::Frame_PostMessage_Result>
unused_post_result;
frame->PostMessage("https://example.com",
CreateWebMessageWithMessagePortRequest(
bad_origin_incoming_message_port.NewRequest(),
base::MemBufferFromString("bad origin, bad!", "test")),
CallbackToFitFunction(unused_post_result.GetCallback()));
base::test::TestFuture<fuchsia::web::WebMessage> unused_message_read;
bad_origin_incoming_message_port->ReceiveMessage(
CallbackToFitFunction(unused_message_read.GetCallback()));
// PostMessage() with a valid origin should succeed.
// Verify it by looking for an ack message on the MessagePort we passed in.
// Since message events are handled in order, observing the result of this
// operation will verify whether the previous PostMessage() was received but
// discarded.
fuchsia::web::MessagePortPtr incoming_message_port;
fuchsia::web::MessagePortPtr message_port;
base::test::TestFuture<fuchsia::web::Frame_PostMessage_Result> post_result;
frame->PostMessage("*",
CreateWebMessageWithMessagePortRequest(
message_port.NewRequest(),
base::MemBufferFromString("good origin", "test")),
CallbackToFitFunction(post_result.GetCallback()));
base::test::TestFuture<fuchsia::web::WebMessage> receiver;
message_port->ReceiveMessage(CallbackToFitFunction(receiver.GetCallback()));
ASSERT_TRUE(receiver.Wait());
ASSERT_TRUE(receiver.Get().has_data());
EXPECT_EQ("got_port", *base::StringFromMemBuffer(receiver.Get().data()));
ASSERT_TRUE(receiver.Get().has_incoming_transfer());
ASSERT_EQ(receiver.Get().incoming_transfer().size(), 1u);
incoming_message_port =
receiver.Take().mutable_incoming_transfer()->at(0).message_port().Bind();
EXPECT_TRUE(post_result.Get().is_response());
// Verify that the first PostMessage() call wasn't handled.
EXPECT_FALSE(unused_message_read.IsReady());
}
} // namespace