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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/broadcastchannel/broadcast_channel.h"
#include <iterator>
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/dom/events/native_event_listener.h"
#include "third_party/blink/renderer/core/event_type_names.h"
#include "third_party/blink/renderer/core/events/message_event.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/messaging/blink_cloneable_message.h"
#include "third_party/blink/renderer/core/testing/dummy_page_holder.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_remote.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "v8/include/v8.h"
namespace blink {
class BroadcastChannelTester : public GarbageCollected<BroadcastChannelTester>,
public mojom::blink::BroadcastChannelClient {
public:
explicit BroadcastChannelTester(ExecutionContext* execution_context)
: receiver_(this, execution_context), remote_(execution_context) {
// Ideally, these would share a pipe. This is more convenient.
mojo::PendingAssociatedReceiver<mojom::blink::BroadcastChannelClient>
receiver0;
mojo::PendingAssociatedRemote<mojom::blink::BroadcastChannelClient>
remote0 = receiver0.InitWithNewEndpointAndPassRemote();
receiver0.EnableUnassociatedUsage();
mojo::PendingAssociatedReceiver<mojom::blink::BroadcastChannelClient>
receiver1;
mojo::PendingAssociatedRemote<mojom::blink::BroadcastChannelClient>
remote1 = receiver1.InitWithNewEndpointAndPassRemote();
receiver1.EnableUnassociatedUsage();
scoped_refptr<base::SequencedTaskRunner> task_runner =
execution_context->GetTaskRunner(TaskType::kInternalTest);
receiver_.Bind(std::move(receiver0), task_runner);
remote_.Bind(std::move(remote1), task_runner);
channel_ = MakeGarbageCollected<BroadcastChannel>(
base::PassKey<BroadcastChannelTester>(), execution_context,
"BroadcastChannelTester", std::move(receiver1), std::move(remote0));
auto* listener = MakeGarbageCollected<EventListener>(this);
channel_->addEventListener(event_type_names::kMessage, listener);
channel_->addEventListener(event_type_names::kMessageerror, listener);
}
BroadcastChannel* channel() const { return channel_.Get(); }
const HeapVector<Member<MessageEvent>>& received_events() const {
return received_events_;
}
const Vector<BlinkCloneableMessage>& sent_messages() const {
return sent_messages_;
}
void AwaitNextUpdate(base::OnceClosure closure) {
on_next_update_.push_back(std::move(closure));
}
void PostMessage(BlinkCloneableMessage message) {
remote_->OnMessage(std::move(message));
}
void Trace(Visitor* visitor) const {
visitor->Trace(channel_);
visitor->Trace(received_events_);
visitor->Trace(receiver_);
visitor->Trace(remote_);
}
// BroadcastChannelClient
void OnMessage(BlinkCloneableMessage message) override {
sent_messages_.push_back(std::move(message));
ReportUpdate();
}
private:
class EventListener : public NativeEventListener {
public:
explicit EventListener(BroadcastChannelTester* tester) : tester_(tester) {}
void Trace(Visitor* visitor) const override {
NativeEventListener::Trace(visitor);
visitor->Trace(tester_);
}
void Invoke(ExecutionContext*, Event* event) override {
tester_->received_events_.push_back(static_cast<MessageEvent*>(event));
tester_->ReportUpdate();
}
private:
Member<BroadcastChannelTester> tester_;
};
void ReportUpdate() {
Vector<base::OnceClosure> closures = std::move(on_next_update_);
for (auto& closure : closures)
std::move(closure).Run();
}
Member<BroadcastChannel> channel_;
HeapVector<Member<MessageEvent>> received_events_;
Vector<BlinkCloneableMessage> sent_messages_;
Vector<base::OnceClosure> on_next_update_;
HeapMojoAssociatedReceiver<mojom::blink::BroadcastChannelClient,
BroadcastChannelTester>
receiver_;
HeapMojoAssociatedRemote<mojom::blink::BroadcastChannelClient> remote_;
};
namespace {
BlinkCloneableMessage MakeNullMessage() {
BlinkCloneableMessage message;
message.message = SerializedScriptValue::NullValue();
message.sender_agent_cluster_id = base::UnguessableToken::Create();
return message;
}
TEST(BroadcastChannelTest, DispatchMessageEvent) {
test::TaskEnvironment task_environment;
DummyPageHolder holder;
ExecutionContext* execution_context = holder.GetFrame().DomWindow();
auto* tester =
MakeGarbageCollected<BroadcastChannelTester>(execution_context);
base::RunLoop run_loop;
tester->AwaitNextUpdate(run_loop.QuitClosure());
tester->PostMessage(MakeNullMessage());
run_loop.Run();
ASSERT_EQ(tester->received_events().size(), 1u);
EXPECT_EQ(tester->received_events()[0]->type(), event_type_names::kMessage);
}
TEST(BroadcastChannelTest, AgentClusterLockedMatch) {
test::TaskEnvironment task_environment;
DummyPageHolder holder;
ExecutionContext* execution_context = holder.GetFrame().DomWindow();
auto* tester =
MakeGarbageCollected<BroadcastChannelTester>(execution_context);
base::RunLoop run_loop;
tester->AwaitNextUpdate(run_loop.QuitClosure());
BlinkCloneableMessage message = MakeNullMessage();
message.sender_agent_cluster_id = execution_context->GetAgentClusterID();
message.locked_to_sender_agent_cluster = true;
tester->PostMessage(std::move(message));
run_loop.Run();
ASSERT_EQ(tester->received_events().size(), 1u);
EXPECT_EQ(tester->received_events()[0]->type(), event_type_names::kMessage);
}
TEST(BroadcastChannelTest, AgentClusterLockedMismatch) {
test::TaskEnvironment task_environment;
DummyPageHolder holder;
ExecutionContext* execution_context = holder.GetFrame().DomWindow();
auto* tester =
MakeGarbageCollected<BroadcastChannelTester>(execution_context);
base::RunLoop run_loop;
tester->AwaitNextUpdate(run_loop.QuitClosure());
BlinkCloneableMessage message = MakeNullMessage();
message.locked_to_sender_agent_cluster = true;
tester->PostMessage(std::move(message));
run_loop.Run();
ASSERT_EQ(tester->received_events().size(), 1u);
EXPECT_EQ(tester->received_events()[0]->type(),
event_type_names::kMessageerror);
}
TEST(BroadcastChannelTest, MessageCannotDeserialize) {
test::TaskEnvironment task_environment;
DummyPageHolder holder;
LocalDOMWindow* window = holder.GetFrame().DomWindow();
auto* tester = MakeGarbageCollected<BroadcastChannelTester>(window);
SerializedScriptValue::ScopedOverrideCanDeserializeInForTesting
override_can_deserialize_in(base::BindLambdaForTesting(
[&](const SerializedScriptValue& value,
ExecutionContext* execution_context, bool can_deserialize) {
EXPECT_EQ(execution_context, window);
EXPECT_TRUE(can_deserialize);
return false;
}));
base::RunLoop run_loop;
tester->AwaitNextUpdate(run_loop.QuitClosure());
tester->PostMessage(MakeNullMessage());
run_loop.Run();
ASSERT_EQ(tester->received_events().size(), 1u);
EXPECT_EQ(tester->received_events()[0]->type(),
event_type_names::kMessageerror);
}
TEST(BroadcastChannelTest, OutgoingMessagesMarkedWithAgentClusterId) {
test::TaskEnvironment task_environment;
DummyPageHolder holder;
ExecutionContext* execution_context = holder.GetFrame().DomWindow();
ScriptState* script_state = ToScriptStateForMainWorld(&holder.GetFrame());
auto* tester =
MakeGarbageCollected<BroadcastChannelTester>(execution_context);
base::RunLoop run_loop;
tester->AwaitNextUpdate(run_loop.QuitClosure());
{
ScriptState::Scope scope(script_state);
tester->channel()->postMessage(
ScriptValue::CreateNull(script_state->GetIsolate()),
ASSERT_NO_EXCEPTION);
}
run_loop.Run();
ASSERT_EQ(tester->sent_messages().size(), 1u);
EXPECT_EQ(tester->sent_messages()[0].sender_agent_cluster_id,
execution_context->GetAgentClusterID());
EXPECT_FALSE(tester->sent_messages()[0].locked_to_sender_agent_cluster);
}
// TODO(crbug.com/1413818): iOS doesn't support WebAssembly yet.
#if BUILDFLAG(IS_IOS)
#define MAYBE_OutgoingAgentClusterLockedMessage \
DISABLED_OutgoingAgentClusterLockedMessage
#else
#define MAYBE_OutgoingAgentClusterLockedMessage \
OutgoingAgentClusterLockedMessage
#endif
TEST(BroadcastChannelTest, MAYBE_OutgoingAgentClusterLockedMessage) {
test::TaskEnvironment task_environment;
DummyPageHolder holder;
ExecutionContext* execution_context = holder.GetFrame().DomWindow();
ScriptState* script_state = ToScriptStateForMainWorld(&holder.GetFrame());
v8::Isolate* isolate = script_state->GetIsolate();
auto* tester =
MakeGarbageCollected<BroadcastChannelTester>(execution_context);
base::RunLoop run_loop;
tester->AwaitNextUpdate(run_loop.QuitClosure());
{
// WebAssembly modules are always agent cluster locked. This is a trivial
// one with no functionality, just the minimal magic and version.
static constexpr uint8_t kTrivialModuleBytes[] = {0x00, 0x61, 0x73, 0x6d,
0x01, 0x00, 0x00, 0x00};
ScriptState::Scope scope(script_state);
v8::Local<v8::WasmModuleObject> trivial_module =
v8::WasmModuleObject::Compile(isolate, {std::data(kTrivialModuleBytes),
std::size(kTrivialModuleBytes)})
.ToLocalChecked();
tester->channel()->postMessage(ScriptValue(isolate, trivial_module),
ASSERT_NO_EXCEPTION);
}
run_loop.Run();
ASSERT_EQ(tester->sent_messages().size(), 1u);
EXPECT_EQ(tester->sent_messages()[0].sender_agent_cluster_id,
execution_context->GetAgentClusterID());
EXPECT_TRUE(tester->sent_messages()[0].locked_to_sender_agent_cluster);
}
} // namespace
} // namespace blink
|