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
|
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "include/v8-inspector.h"
#include "include/v8-local-handle.h"
#include "include/v8-primitive.h"
#include "src/inspector/string-util.h"
#include "src/inspector/v8-inspector-impl.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
using v8_inspector::String16;
using v8_inspector::StringBuffer;
using v8_inspector::StringView;
using v8_inspector::toString16;
using v8_inspector::toStringView;
using v8_inspector::V8ContextInfo;
using v8_inspector::V8Inspector;
using v8_inspector::V8InspectorSession;
namespace v8 {
namespace internal {
using InspectorTest = TestWithContext;
namespace {
class NoopChannel : public V8Inspector::Channel {
public:
~NoopChannel() override = default;
void sendResponse(int callId,
std::unique_ptr<StringBuffer> message) override {}
void sendNotification(std::unique_ptr<StringBuffer> message) override {}
void flushProtocolNotifications() override {}
};
void WrapOnInterrupt(v8::Isolate* isolate, void* data) {
const char* object_group = "";
StringView object_group_view(reinterpret_cast<const uint8_t*>(object_group),
strlen(object_group));
reinterpret_cast<V8InspectorSession*>(data)->wrapObject(
isolate->GetCurrentContext(), v8::Null(isolate), object_group_view,
false);
}
} // namespace
TEST_F(InspectorTest, WrapInsideWrapOnInterrupt) {
v8::Isolate* isolate = v8_isolate();
v8::HandleScope handle_scope(isolate);
v8_inspector::V8InspectorClient default_client;
std::unique_ptr<V8Inspector> inspector =
V8Inspector::create(isolate, &default_client);
const char* name = "";
StringView name_view(reinterpret_cast<const uint8_t*>(name), strlen(name));
V8ContextInfo context_info(v8_context(), 1, name_view);
inspector->contextCreated(context_info);
NoopChannel channel;
const char* state = "{}";
StringView state_view(reinterpret_cast<const uint8_t*>(state), strlen(state));
std::unique_ptr<V8InspectorSession> session = inspector->connect(
1, &channel, state_view, v8_inspector::V8Inspector::kFullyTrusted);
const char* object_group = "";
StringView object_group_view(reinterpret_cast<const uint8_t*>(object_group),
strlen(object_group));
isolate->RequestInterrupt(&WrapOnInterrupt, session.get());
session->wrapObject(v8_context(), v8::Null(isolate), object_group_view,
false);
}
TEST_F(InspectorTest, BinaryFromBase64) {
auto checkBinary = [](const v8_inspector::protocol::Binary& binary,
const std::vector<uint8_t>& values) {
std::vector<uint8_t> binary_vector(binary.data(),
binary.data() + binary.size());
CHECK_EQ(binary_vector, values);
};
{
bool success;
auto binary = v8_inspector::protocol::Binary::fromBase64("", &success);
CHECK(success);
checkBinary(binary, {});
}
{
bool success;
auto binary = v8_inspector::protocol::Binary::fromBase64("YQ==", &success);
CHECK(success);
checkBinary(binary, {'a'});
}
{
bool success;
auto binary = v8_inspector::protocol::Binary::fromBase64("YWI=", &success);
CHECK(success);
checkBinary(binary, {'a', 'b'});
}
{
bool success;
auto binary = v8_inspector::protocol::Binary::fromBase64("YWJj", &success);
CHECK(success);
checkBinary(binary, {'a', 'b', 'c'});
}
{
bool success;
// Wrong input length:
auto binary = v8_inspector::protocol::Binary::fromBase64("Y", &success);
CHECK(!success);
}
{
bool success;
// Invalid space:
auto binary = v8_inspector::protocol::Binary::fromBase64("=AAA", &success);
CHECK(!success);
}
{
bool success;
// Invalid space in a non-final block of four:
auto binary =
v8_inspector::protocol::Binary::fromBase64("AAA=AAAA", &success);
CHECK(!success);
}
{
bool success;
// Invalid invalid space in second to last position:
auto binary = v8_inspector::protocol::Binary::fromBase64("AA=A", &success);
CHECK(!success);
}
{
bool success;
// Invalid character:
auto binary = v8_inspector::protocol::Binary::fromBase64(" ", &success);
CHECK(!success);
}
}
TEST_F(InspectorTest, BinaryToBase64) {
uint8_t input[] = {'a', 'b', 'c'};
{
auto binary = v8_inspector::protocol::Binary::fromSpan(input, 0);
v8_inspector::protocol::String base64 = binary.toBase64();
CHECK_EQ(base64.utf8(), "");
}
{
auto binary = v8_inspector::protocol::Binary::fromSpan(input, 1);
v8_inspector::protocol::String base64 = binary.toBase64();
CHECK_EQ(base64.utf8(), "YQ==");
}
{
auto binary = v8_inspector::protocol::Binary::fromSpan(input, 2);
v8_inspector::protocol::String base64 = binary.toBase64();
CHECK_EQ(base64.utf8(), "YWI=");
}
{
auto binary = v8_inspector::protocol::Binary::fromSpan(input, 3);
v8_inspector::protocol::String base64 = binary.toBase64();
CHECK_EQ(base64.utf8(), "YWJj");
}
}
TEST_F(InspectorTest, BinaryBase64RoundTrip) {
std::array<uint8_t, 256> values;
for (uint16_t b = 0x0; b <= 0xFF; ++b) values[b] = b;
auto binary =
v8_inspector::protocol::Binary::fromSpan(values.data(), values.size());
v8_inspector::protocol::String base64 = binary.toBase64();
bool success = false;
auto roundtrip_binary =
v8_inspector::protocol::Binary::fromBase64(base64, &success);
CHECK(success);
CHECK_EQ(values.size(), roundtrip_binary.size());
for (size_t i = 0; i < values.size(); ++i) {
CHECK_EQ(values[i], roundtrip_binary.data()[i]);
}
}
TEST_F(InspectorTest, NoInterruptOnGetAssociatedData) {
v8::Isolate* isolate = v8_isolate();
v8::HandleScope handle_scope(isolate);
v8_inspector::V8InspectorClient default_client;
std::unique_ptr<v8_inspector::V8InspectorImpl> inspector(
new v8_inspector::V8InspectorImpl(isolate, &default_client));
v8::Local<v8::Value> error = v8::Exception::Error(NewString("custom error"));
v8::Local<v8::Name> key = NewString("key");
v8::Local<v8::Value> value = NewString("value");
inspector->associateExceptionData(v8_context(), error, key, value);
struct InterruptRecorder {
static void handler(v8::Isolate* isolate, void* data) {
reinterpret_cast<InterruptRecorder*>(data)->WasInvoked = true;
}
bool WasInvoked = false;
} recorder;
isolate->RequestInterrupt(&InterruptRecorder::handler, &recorder);
v8::Local<v8::Object> data =
inspector->getAssociatedExceptionData(error).ToLocalChecked();
CHECK(!recorder.WasInvoked);
CHECK_EQ(data->Get(v8_context(), key).ToLocalChecked(), value);
TryRunJS("0");
CHECK(recorder.WasInvoked);
}
TEST_F(InspectorTest, NoConsoleAPIForUntrustedClient) {
v8::Isolate* isolate = v8_isolate();
v8::HandleScope handle_scope(isolate);
v8_inspector::V8InspectorClient default_client;
std::unique_ptr<V8Inspector> inspector =
V8Inspector::create(isolate, &default_client);
V8ContextInfo context_info(v8_context(), 1, toStringView(""));
inspector->contextCreated(context_info);
class TestChannel : public V8Inspector::Channel {
public:
~TestChannel() override = default;
void sendResponse(int callId,
std::unique_ptr<StringBuffer> message) override {
CHECK_EQ(callId, 1);
CHECK_NE(toString16(message->string()).find(expected_response_matcher_),
String16::kNotFound);
}
void sendNotification(std::unique_ptr<StringBuffer> message) override {}
void flushProtocolNotifications() override {}
v8_inspector::String16 expected_response_matcher_;
};
TestChannel channel;
const char kCommand[] = R"({
"id": 1,
"method": "Runtime.evaluate",
"params": {
"expression": "$0 || 42",
"contextId": 1,
"includeCommandLineAPI": true
}
})";
std::unique_ptr<V8InspectorSession> trusted_session =
inspector->connect(1, &channel, toStringView("{}"),
v8_inspector::V8Inspector::kFullyTrusted);
channel.expected_response_matcher_ = R"("value":42)";
trusted_session->dispatchProtocolMessage(toStringView(kCommand));
std::unique_ptr<V8InspectorSession> untrusted_session = inspector->connect(
1, &channel, toStringView("{}"), v8_inspector::V8Inspector::kUntrusted);
channel.expected_response_matcher_ = R"("className":"ReferenceError")";
untrusted_session->dispatchProtocolMessage(toStringView(kCommand));
}
TEST_F(InspectorTest, ApiCreatedTasksAreCleanedUp) {
i::v8_flags.experimental_async_stack_tagging_api = true;
v8::Isolate* isolate = v8_isolate();
v8::HandleScope handle_scope(isolate);
v8_inspector::V8InspectorClient default_client;
std::unique_ptr<v8_inspector::V8InspectorImpl> inspector =
std::make_unique<v8_inspector::V8InspectorImpl>(isolate, &default_client);
V8ContextInfo context_info(v8_context(), 1, toStringView(""));
inspector->contextCreated(context_info);
// Trigger V8Console creation.
v8_inspector::V8Console* console = inspector->console();
CHECK(console);
{
v8::HandleScope handle_scope(isolate);
v8::MaybeLocal<v8::Value> result = TryRunJS(isolate, NewString(R"(
globalThis['task'] = console.createTask('Task');
)"));
CHECK(!result.IsEmpty());
// Run GC and check that the task is still here.
CollectAllGarbage();
CHECK_EQ(console->AllConsoleTasksForTest().size(), 1);
}
// Get rid of the task on the context, run GC and check we no longer have
// the TaskInfo in the inspector.
v8_context()->Global()->Delete(v8_context(), NewString("task")).Check();
CollectAllGarbage();
CHECK_EQ(console->AllConsoleTasksForTest().size(), 0);
}
} // namespace internal
} // namespace v8
|