v8/test/cctest/test-inspector.cc
Dan Elphick ec06bb6ce5 Reland "[include] Split out v8.h"
This is a reland of d1b27019d3

Fixes include:
Adding missing file to bazel build
Forward-declaring classing before friend-classing them to fix win/gcc
Add missing v8-isolate.h include for vtune builds

Original change's description:
> [include] Split out v8.h
>
> This moves every single class/function out of include/v8.h into a
> separate header in include/, which v8.h then includes so that
> externally nothing appears to have changed.
>
> Every include of v8.h from inside v8 has been changed to a more
> fine-grained include.
>
> Previously inline functions defined at the bottom of v8.h would call
> private non-inline functions in the V8 class. Since that class is now
> in v8-initialization.h and is rarely included (as that would create
> dependency cycles), this is not possible and so those methods have been
> moved out of the V8 class into the namespace v8::api_internal.
>
> None of the previous files in include/ now #include v8.h, which means
> if embedders were relying on this transitive dependency then it will
> give compile failures.
>
> v8-inspector.h does depend on v8-scripts.h for the time being to ensure
> that Chrome continue to compile but that change will be reverted once
> those transitive #includes in chrome are changed to include it directly.
>
> Full design:
> https://docs.google.com/document/d/1rTD--I8hCAr-Rho1WTumZzFKaDpEp0IJ8ejZtk4nJdA/edit?usp=sharing
>
> Bug: v8:11965
> Change-Id: I53b84b29581632710edc80eb11f819c2097a2877
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3097448
> Reviewed-by: Yang Guo <yangguo@chromium.org>
> Reviewed-by: Camillo Bruni <cbruni@chromium.org>
> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
> Reviewed-by: Leszek Swirski <leszeks@chromium.org>
> Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
> Commit-Queue: Dan Elphick <delphick@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#76424}

Cq-Include-Trybots: luci.v8.try:v8_linux_vtunejit
Bug: v8:11965
Change-Id: I99f5d3a73bf8fe25b650adfaf9567dc4e44a09e6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3113629
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Simon Zünd <szuend@chromium.org>
Commit-Queue: Dan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76460}
2021-08-24 13:08:55 +00:00

172 lines
5.4 KiB
C++

// 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/protocol/Runtime.h"
#include "src/inspector/string-util.h"
#include "test/cctest/cctest.h"
using v8_inspector::StringBuffer;
using v8_inspector::StringView;
using v8_inspector::V8ContextInfo;
using v8_inspector::V8Inspector;
using v8_inspector::V8InspectorSession;
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(WrapInsideWrapOnInterrupt) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
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(env.local(), 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);
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(env.local(), v8::Null(isolate), object_group_view, false);
}
TEST(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(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(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]);
}
}