Commit f6a76fad authored by Johannes Henkel's avatar Johannes Henkel Committed by Commit Bot

[DevTools] Roll inspector protocol (Cleanup) (V8)

New revision: 4c2a3acaea9f2e7958081dd361f81e20e9eff5e7

This cleanup cl does not change any behavior, it just
cleans up some headers and does a class rename
(StreamingParserHandler->ParserHandler). It was reviewed
upstream
https://chromium-review.googlesource.com/c/deps/inspector_protocol/+/1924792
https://chromium-review.googlesource.com/c/deps/inspector_protocol/+/1925679
and does not touch V8 code. Would like to get
this in to make it easier to review subsequent changes.

Thanks!

Change-Id: Ie9fe1434bafeb4f5090244f823d1e482ff805dd0
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1925721
Auto-Submit: Johannes Henkel <johannes@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Johannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65106}
parent 316036bc
......@@ -2,7 +2,7 @@ Name: inspector protocol
Short Name: inspector_protocol
URL: https://chromium.googlesource.com/deps/inspector_protocol/
Version: 0
Revision: d020a9e614d4a5116a7c71f288c0340e282e1a6e
Revision: 4c2a3acaea9f2e7958081dd361f81e20e9eff5e7
License: BSD
License File: LICENSE
Security Critical: no
......
......@@ -114,7 +114,7 @@ def read_config():
".lib.export_header": False,
".crdtp": False,
".crdtp.dir": os.path.join(inspector_protocol_dir, "crdtp"),
".crdtp.namespace": "inspector_protocol",
".crdtp.namespace": "crdtp",
}
for key_value in config_values:
parts = key_value.split("=")
......
......@@ -451,7 +451,7 @@ bool EnvelopeEncoder::EncodeStop(std::string* out) {
namespace {
template <typename C>
class CBOREncoder : public StreamingParserHandler {
class CBOREncoder : public ParserHandler {
public:
CBOREncoder(C* out, Status* status) : out_(out), status_(status) {
*status_ = Status();
......@@ -557,16 +557,14 @@ class CBOREncoder : public StreamingParserHandler {
};
} // namespace
std::unique_ptr<StreamingParserHandler> NewCBOREncoder(
std::vector<uint8_t>* out,
Status* status) {
return std::unique_ptr<StreamingParserHandler>(
std::unique_ptr<ParserHandler> NewCBOREncoder(std::vector<uint8_t>* out,
Status* status) {
return std::unique_ptr<ParserHandler>(
new CBOREncoder<std::vector<uint8_t>>(out, status));
}
std::unique_ptr<StreamingParserHandler> NewCBOREncoder(std::string* out,
Status* status) {
return std::unique_ptr<StreamingParserHandler>(
std::unique_ptr<ParserHandler> NewCBOREncoder(std::string* out,
Status* status) {
return std::unique_ptr<ParserHandler>(
new CBOREncoder<std::string>(out, status));
}
......@@ -870,21 +868,18 @@ static constexpr int kStackLimit = 300;
// to roundtrip JSON messages.
bool ParseMap(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out);
ParserHandler* out);
bool ParseArray(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out);
ParserHandler* out);
bool ParseValue(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out);
ParserHandler* out);
bool ParseEnvelope(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out);
ParserHandler* out);
void ParseUTF16String(CBORTokenizer* tokenizer, StreamingParserHandler* out) {
void ParseUTF16String(CBORTokenizer* tokenizer, ParserHandler* out) {
std::vector<uint16_t> value;
span<uint8_t> rep = tokenizer->GetString16WireRep();
for (size_t ii = 0; ii < rep.size(); ii += 2)
......@@ -893,7 +888,7 @@ void ParseUTF16String(CBORTokenizer* tokenizer, StreamingParserHandler* out) {
tokenizer->Next();
}
bool ParseUTF8String(CBORTokenizer* tokenizer, StreamingParserHandler* out) {
bool ParseUTF8String(CBORTokenizer* tokenizer, ParserHandler* out) {
assert(tokenizer->TokenTag() == CBORTokenTag::STRING8);
out->HandleString8(tokenizer->GetString8());
tokenizer->Next();
......@@ -902,7 +897,7 @@ bool ParseUTF8String(CBORTokenizer* tokenizer, StreamingParserHandler* out) {
bool ParseEnvelope(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out) {
ParserHandler* out) {
assert(tokenizer->TokenTag() == CBORTokenTag::ENVELOPE);
// Before we enter the envelope, we save the position that we
// expect to see after we're done parsing the envelope contents.
......@@ -948,7 +943,7 @@ bool ParseEnvelope(int32_t stack_depth,
bool ParseValue(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out) {
ParserHandler* out) {
if (stack_depth > kStackLimit) {
out->HandleError(
Status{Error::CBOR_STACK_LIMIT_EXCEEDED, tokenizer->Status().pos});
......@@ -1010,7 +1005,7 @@ bool ParseValue(int32_t stack_depth,
// detected.
bool ParseArray(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out) {
ParserHandler* out) {
assert(tokenizer->TokenTag() == CBORTokenTag::ARRAY_START);
tokenizer->Next();
out->HandleArrayBegin();
......@@ -1038,7 +1033,7 @@ bool ParseArray(int32_t stack_depth,
// detected.
bool ParseMap(int32_t stack_depth,
CBORTokenizer* tokenizer,
StreamingParserHandler* out) {
ParserHandler* out) {
assert(tokenizer->TokenTag() == CBORTokenTag::MAP_START);
out->HandleMapBegin();
tokenizer->Next();
......@@ -1073,7 +1068,7 @@ bool ParseMap(int32_t stack_depth,
}
} // namespace
void ParseCBOR(span<uint8_t> bytes, StreamingParserHandler* out) {
void ParseCBOR(span<uint8_t> bytes, ParserHandler* out) {
if (bytes.empty()) {
out->HandleError(Status{Error::CBOR_NO_INPUT, 0});
return;
......
......@@ -5,11 +5,8 @@
#ifndef V8_CRDTP_CBOR_H_
#define V8_CRDTP_CBOR_H_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
......@@ -140,11 +137,9 @@ class EnvelopeEncoder {
// that drives it. The handler will encode into |out|, and iff an error occurs
// it will set |status| to an error and clear |out|. Otherwise, |status.ok()|
// will be |true|.
std::unique_ptr<StreamingParserHandler> NewCBOREncoder(
std::vector<uint8_t>* out,
Status* status);
std::unique_ptr<StreamingParserHandler> NewCBOREncoder(std::string* out,
Status* status);
std::unique_ptr<ParserHandler> NewCBOREncoder(std::vector<uint8_t>* out,
Status* status);
std::unique_ptr<ParserHandler> NewCBOREncoder(std::string* out, Status* status);
// =============================================================================
// cbor::CBORTokenizer - for parsing individual CBOR items
......@@ -283,7 +278,7 @@ class CBORTokenizer {
// |out|. If an error occurs, sends |out->HandleError|, and parsing stops.
// The client is responsible for discarding the already received information in
// that case.
void ParseCBOR(span<uint8_t> bytes, StreamingParserHandler* out);
void ParseCBOR(span<uint8_t> bytes, ParserHandler* out);
// =============================================================================
// cbor::AppendString8EntryToMap - for limited in-place editing of messages
......
......@@ -13,8 +13,10 @@
#include <iostream>
#include <sstream>
#include <string>
#include "json.h"
#include "parser_handler.h"
#include "span.h"
#include "status.h"
#include "test_platform.h"
using testing::ElementsAreArray;
......@@ -57,7 +59,6 @@ const json::Platform& GetTestPlatform() {
} // namespace
namespace cbor {
// =============================================================================
// Detecting CBOR content
// =============================================================================
......@@ -657,17 +658,13 @@ TEST(EncodeDecodeEnvelopesTest, MessageWithNestingAndEnvelopeContentsAccess) {
// cbor::NewCBOREncoder - for encoding from a streaming parser
// =============================================================================
void EncodeUTF8ForTest(const std::string& key, std::vector<uint8_t>* out) {
EncodeString8(SpanFrom(key), out);
}
TEST(JSONToCBOREncoderTest, SevenBitStrings) {
// When a string can be represented as 7 bit ASCII, the encoder will use the
// STRING (major Type 3) type, so the actual characters end up as bytes on the
// wire.
std::vector<uint8_t> encoded;
Status status;
std::unique_ptr<StreamingParserHandler> encoder =
NewCBOREncoder(&encoded, &status);
std::unique_ptr<ParserHandler> encoder = NewCBOREncoder(&encoded, &status);
std::vector<uint16_t> utf16 = {'f', 'o', 'o'};
encoder->HandleString16(span<uint16_t>(utf16.data(), utf16.size()));
EXPECT_EQ(Error::OK, status.error);
......@@ -679,7 +676,7 @@ TEST(JSONToCBOREncoderTest, SevenBitStrings) {
}
TEST(JsonCborRoundtrip, EncodingDecoding) {
// Hits all the cases except binary and error in StreamingParserHandler, first
// Hits all the cases except binary and error in ParserHandler, first
// parsing a JSON message into CBOR, then parsing it back from CBOR into JSON.
std::string json =
"{"
......@@ -693,8 +690,7 @@ TEST(JsonCborRoundtrip, EncodingDecoding) {
"}";
std::vector<uint8_t> encoded;
Status status;
std::unique_ptr<StreamingParserHandler> encoder =
NewCBOREncoder(&encoded, &status);
std::unique_ptr<ParserHandler> encoder = NewCBOREncoder(&encoded, &status);
span<uint8_t> ascii_in = SpanFrom(json);
json::ParseJSON(GetTestPlatform(), ascii_in, encoder.get());
std::vector<uint8_t> expected = {
......@@ -739,8 +735,8 @@ TEST(JsonCborRoundtrip, EncodingDecoding) {
// And now we roundtrip, decoding the message we just encoded.
std::string decoded;
std::unique_ptr<StreamingParserHandler> json_encoder =
NewJSONEncoder(&GetTestPlatform(), &decoded, &status);
std::unique_ptr<ParserHandler> json_encoder =
json::NewJSONEncoder(&GetTestPlatform(), &decoded, &status);
ParseCBOR(span<uint8_t>(encoded.data(), encoded.size()), json_encoder.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ(json, decoded);
......@@ -755,13 +751,12 @@ TEST(JsonCborRoundtrip, MoreRoundtripExamples) {
SCOPED_TRACE(std::string("example: ") + json);
std::vector<uint8_t> encoded;
Status status;
std::unique_ptr<StreamingParserHandler> encoder =
NewCBOREncoder(&encoded, &status);
std::unique_ptr<ParserHandler> encoder = NewCBOREncoder(&encoded, &status);
span<uint8_t> ascii_in = SpanFrom(json);
ParseJSON(GetTestPlatform(), ascii_in, encoder.get());
json::ParseJSON(GetTestPlatform(), ascii_in, encoder.get());
std::string decoded;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &decoded, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &decoded, &status);
ParseCBOR(span<uint8_t>(encoded.data(), encoded.size()), json_writer.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ(json, decoded);
......@@ -769,7 +764,7 @@ TEST(JsonCborRoundtrip, MoreRoundtripExamples) {
}
TEST(JSONToCBOREncoderTest, HelloWorldBinary_WithTripToJson) {
// The StreamingParserHandler::HandleBinary is a special case: The JSON parser
// The ParserHandler::HandleBinary is a special case: The JSON parser
// will never call this method, because JSON does not natively support the
// binary type. So, we can't fully roundtrip. However, the other direction
// works: binary will be rendered in JSON, as a base64 string. So, we make
......@@ -778,8 +773,7 @@ TEST(JSONToCBOREncoderTest, HelloWorldBinary_WithTripToJson) {
// containing "Hello, world.".
std::vector<uint8_t> encoded;
Status status;
std::unique_ptr<StreamingParserHandler> encoder =
NewCBOREncoder(&encoded, &status);
std::unique_ptr<ParserHandler> encoder = NewCBOREncoder(&encoded, &status);
encoder->HandleMapBegin();
// Emit a key.
std::vector<uint16_t> key = {'f', 'o', 'o'};
......@@ -793,8 +787,8 @@ TEST(JSONToCBOREncoderTest, HelloWorldBinary_WithTripToJson) {
// Now drive the json writer via the CBOR decoder.
std::string decoded;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &decoded, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &decoded, &status);
ParseCBOR(SpanFrom(encoded), json_writer.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ(Status::npos(), status.pos);
......@@ -813,8 +807,8 @@ TEST(ParseCBORTest, ParseEmptyCBORMessage) {
std::vector<uint8_t> in = {0xd8, 0x5a, 0, 0, 0, 2, 0xbf, 0xff};
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(in.data(), in.size()), json_writer.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ("{}", out);
......@@ -837,8 +831,8 @@ TEST(ParseCBORTest, ParseCBORHelloWorld) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ("{\"msg\":\"Hello, \\ud83c\\udf0e.\"}", out);
......@@ -862,8 +856,8 @@ TEST(ParseCBORTest, UTF8IsSupportedInKeys) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ("{\"\\ud83c\\udf0e\":\"\\u263e\"}", out);
......@@ -873,8 +867,8 @@ TEST(ParseCBORTest, NoInputError) {
std::vector<uint8_t> in = {};
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(in.data(), in.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_NO_INPUT, status.error);
EXPECT_EQ("", out);
......@@ -887,8 +881,8 @@ TEST(ParseCBORTest, InvalidStartByteError) {
std::string json = "{\"msg\": \"Hello, world.\"}";
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(SpanFrom(json), json_writer.get());
EXPECT_EQ(Error::CBOR_INVALID_START_BYTE, status.error);
EXPECT_EQ("", out);
......@@ -903,8 +897,8 @@ TEST(ParseCBORTest, UnexpectedEofExpectedValueError) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_UNEXPECTED_EOF_EXPECTED_VALUE, status.error);
EXPECT_EQ(bytes.size(), status.pos);
......@@ -921,8 +915,8 @@ TEST(ParseCBORTest, UnexpectedEofInArrayError) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_UNEXPECTED_EOF_IN_ARRAY, status.error);
EXPECT_EQ(bytes.size(), status.pos);
......@@ -936,8 +930,8 @@ TEST(ParseCBORTest, UnexpectedEofInMapError) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_UNEXPECTED_EOF_IN_MAP, status.error);
EXPECT_EQ(7u, status.pos);
......@@ -950,8 +944,8 @@ TEST(ParseCBORTest, TopLevelCantBeEmptyEnvelope) {
std::vector<uint8_t> bytes = {0xd8, 0x5a, 0, 0, 0, 0}; // envelope
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_MAP_START_EXPECTED, status.error);
EXPECT_EQ(bytes.size(), status.pos);
......@@ -972,8 +966,8 @@ TEST(ParseCBORTest, MapStartExpectedAtTopLevel) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_MAP_START_EXPECTED, status.error);
EXPECT_EQ(6u, status.pos);
......@@ -1002,8 +996,8 @@ TEST(ParseCBORTest, OnlyMapsAndArraysSupportedInsideEnvelopes) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_MAP_OR_ARRAY_EXPECTED_IN_ENVELOPE, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1019,8 +1013,8 @@ TEST(ParseCBORTest, InvalidMapKeyError) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_INVALID_MAP_KEY, status.error);
EXPECT_EQ(7u, status.pos);
......@@ -1050,8 +1044,8 @@ TEST(ParseCBORTest, StackLimitExceededError) {
std::vector<uint8_t> bytes = MakeNestedCBOR(3);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ(Status::npos(), status.pos);
......@@ -1061,8 +1055,8 @@ TEST(ParseCBORTest, StackLimitExceededError) {
std::vector<uint8_t> bytes = MakeNestedCBOR(300);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ(Status::npos(), status.pos);
......@@ -1081,8 +1075,8 @@ TEST(ParseCBORTest, StackLimitExceededError) {
std::vector<uint8_t> bytes = MakeNestedCBOR(301);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_STACK_LIMIT_EXCEEDED, status.error);
EXPECT_EQ(opening_segment_size * 301, status.pos);
......@@ -1091,8 +1085,8 @@ TEST(ParseCBORTest, StackLimitExceededError) {
std::vector<uint8_t> bytes = MakeNestedCBOR(320);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_STACK_LIMIT_EXCEEDED, status.error);
EXPECT_EQ(opening_segment_size * 301, status.pos);
......@@ -1110,8 +1104,8 @@ TEST(ParseCBORTest, UnsupportedValueError) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_UNSUPPORTED_VALUE, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1133,8 +1127,8 @@ TEST(ParseCBORTest, InvalidString16Error) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_INVALID_STRING16, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1153,8 +1147,8 @@ TEST(ParseCBORTest, InvalidString8Error) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_INVALID_STRING8, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1175,8 +1169,8 @@ TEST(ParseCBORTest, InvalidBinaryError) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_INVALID_BINARY, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1196,8 +1190,8 @@ TEST(ParseCBORTest, InvalidDoubleError) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_INVALID_DOUBLE, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1217,8 +1211,8 @@ TEST(ParseCBORTest, InvalidSignedError) {
EXPECT_EQ(kPayloadLen, bytes.size() - 6);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_INVALID_INT32, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1240,8 +1234,8 @@ TEST(ParseCBORTest, TrailingJunk) {
std::numeric_limits<uint64_t>::max(), &bytes);
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_TRAILING_JUNK, status.error);
EXPECT_EQ(error_pos, status.pos);
......@@ -1263,8 +1257,8 @@ TEST(ParseCBORTest, EnvelopeContentsLengthMismatch) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(span<uint8_t>(bytes.data(), bytes.size()), json_writer.get());
EXPECT_EQ(Error::CBOR_ENVELOPE_CONTENTS_LENGTH_MISMATCH, status.error);
EXPECT_EQ(bytes.size(), status.pos);
......@@ -1298,8 +1292,8 @@ TYPED_TEST(AppendString8EntryToMapTest, AppendsEntrySuccessfully) {
EXPECT_EQ(Error::OK, status.error);
EXPECT_EQ(Status::npos(), status.pos);
std::string out;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(SpanFrom(msg), json_writer.get());
EXPECT_EQ("{\"key\":\"value\",\"foo\":\"bar\"}", out);
EXPECT_EQ(Error::OK, status.error);
......@@ -1321,8 +1315,8 @@ TYPED_TEST(AppendString8EntryToMapTest, AppendThreeEntries) {
TypeParam msg(encoded.begin(), encoded.end());
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
std::unique_ptr<ParserHandler> json_writer =
json::NewJSONEncoder(&GetTestPlatform(), &out, &status);
ParseCBOR(SpanFrom(msg), json_writer.get());
EXPECT_EQ("{\"key\":\"value\",\"key1\":\"value1\",\"key2\":\"value2\"}", out);
EXPECT_EQ(Error::OK, status.error);
......
......@@ -102,7 +102,7 @@ void Base64Encode(const span<uint8_t>& in, C* out) {
// Implements a handler for JSON parser events to emit a JSON string.
template <typename C>
class JSONEncoder : public StreamingParserHandler {
class JSONEncoder : public ParserHandler {
public:
JSONEncoder(const Platform* platform, C* out, Status* status)
: platform_(platform), out_(out), status_(status) {
......@@ -343,18 +343,17 @@ class JSONEncoder : public StreamingParserHandler {
};
} // namespace
std::unique_ptr<StreamingParserHandler> NewJSONEncoder(
const Platform* platform,
std::vector<uint8_t>* out,
Status* status) {
return std::unique_ptr<StreamingParserHandler>(
std::unique_ptr<ParserHandler> NewJSONEncoder(const Platform* platform,
std::vector<uint8_t>* out,
Status* status) {
return std::unique_ptr<ParserHandler>(
new JSONEncoder<std::vector<uint8_t>>(platform, out, status));
}
std::unique_ptr<StreamingParserHandler> NewJSONEncoder(const Platform* platform,
std::string* out,
Status* status) {
return std::unique_ptr<StreamingParserHandler>(
std::unique_ptr<ParserHandler> NewJSONEncoder(const Platform* platform,
std::string* out,
Status* status) {
return std::unique_ptr<ParserHandler>(
new JSONEncoder<std::string>(platform, out, status));
}
......@@ -388,7 +387,7 @@ const char* const kFalseString = "false";
template <typename Char>
class JsonParser {
public:
JsonParser(const Platform* platform, StreamingParserHandler* handler)
JsonParser(const Platform* platform, ParserHandler* handler)
: platform_(platform), handler_(handler) {}
void Parse(const Char* start, size_t length) {
......@@ -968,20 +967,20 @@ class JsonParser {
const Char* start_pos_ = nullptr;
bool error_ = false;
const Platform* platform_;
StreamingParserHandler* handler_;
ParserHandler* handler_;
};
} // namespace
void ParseJSON(const Platform& platform,
span<uint8_t> chars,
StreamingParserHandler* handler) {
ParserHandler* handler) {
JsonParser<uint8_t> parser(&platform, handler);
parser.Parse(chars.data(), chars.size());
}
void ParseJSON(const Platform& platform,
span<uint16_t> chars,
StreamingParserHandler* handler) {
ParserHandler* handler) {
JsonParser<uint16_t> parser(&platform, handler);
parser.Parse(chars.data(), chars.size());
}
......@@ -994,7 +993,7 @@ Status ConvertCBORToJSONTmpl(const Platform& platform,
span<uint8_t> cbor,
C* json) {
Status status;
std::unique_ptr<StreamingParserHandler> json_writer =
std::unique_ptr<ParserHandler> json_writer =
NewJSONEncoder(&platform, json, &status);
cbor::ParseCBOR(cbor, json_writer.get());
return status;
......@@ -1015,8 +1014,7 @@ Status ConvertCBORToJSON(const Platform& platform,
template <typename T, typename C>
Status ConvertJSONToCBORTmpl(const Platform& platform, span<T> json, C* cbor) {
Status status;
std::unique_ptr<StreamingParserHandler> encoder =
cbor::NewCBOREncoder(cbor, &status);
std::unique_ptr<ParserHandler> encoder = cbor::NewCBOREncoder(cbor, &status);
ParseJSON(platform, json, encoder.get());
return status;
}
......
......@@ -5,14 +5,7 @@
#ifndef V8_CRDTP_JSON_H_
#define V8_CRDTP_JSON_H_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "export.h"
#include "json_platform.h"
......@@ -30,13 +23,12 @@ namespace json {
// Except for calling the HandleError routine at any time, the client
// code must call the Handle* methods in an order in which they'd occur
// in valid JSON; otherwise we may crash (the code uses assert).
std::unique_ptr<StreamingParserHandler> NewJSONEncoder(
const Platform* platform,
std::vector<uint8_t>* out,
Status* status);
std::unique_ptr<StreamingParserHandler> NewJSONEncoder(const Platform* platform,
std::string* out,
Status* status);
std::unique_ptr<ParserHandler> NewJSONEncoder(const Platform* platform,
std::vector<uint8_t>* out,
Status* status);
std::unique_ptr<ParserHandler> NewJSONEncoder(const Platform* platform,
std::string* out,
Status* status);
// =============================================================================
// json::ParseJSON - for receiving streaming parser events for JSON
......@@ -44,10 +36,10 @@ std::unique_ptr<StreamingParserHandler> NewJSONEncoder(const Platform* platform,
void ParseJSON(const Platform& platform,
span<uint8_t> chars,
StreamingParserHandler* handler);
ParserHandler* handler);
void ParseJSON(const Platform& platform,
span<uint16_t> chars,
StreamingParserHandler* handler);
ParserHandler* handler);
// =============================================================================
// json::ConvertCBORToJSON, json::ConvertJSONToCBOR - for transcoding
......
......@@ -5,15 +5,7 @@
#ifndef V8_CRDTP_JSON_PLATFORM_H_
#define V8_CRDTP_JSON_PLATFORM_H_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "export.h"
namespace v8_crdtp {
......
......@@ -16,8 +16,6 @@
#include "test_platform.h"
using testing::ElementsAreArray;
namespace v8_crdtp {
namespace {
class TestPlatform : public json::Platform {
......@@ -61,14 +59,14 @@ namespace json {
// json::NewJSONEncoder - for encoding streaming parser events as JSON
// =============================================================================
void WriteUTF8AsUTF16(StreamingParserHandler* writer, const std::string& utf8) {
void WriteUTF8AsUTF16(ParserHandler* writer, const std::string& utf8) {
writer->HandleString16(SpanFrom(UTF8ToUTF16(SpanFrom(utf8))));
}
TEST(JsonEncoder, OverlongEncodings) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
// We encode 0x7f, which is the DEL ascii character, as a 4 byte UTF8
......@@ -87,7 +85,7 @@ TEST(JsonEncoder, OverlongEncodings) {
TEST(JsonEncoder, IncompleteUtf8Sequence) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleArrayBegin(); // This emits [, which starts an array.
......@@ -113,7 +111,7 @@ TEST(JsonEncoder, IncompleteUtf8Sequence) {
TEST(JsonStdStringWriterTest, HelloWorld) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleMapBegin();
WriteUTF8AsUTF16(writer.get(), "msg1");
......@@ -157,7 +155,7 @@ TEST(JsonStdStringWriterTest, RepresentingNonFiniteValuesAsNull) {
// So in practice it's mapped to null.
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleMapBegin();
writer->HandleString8(SpanFrom("Infinity"));
......@@ -172,13 +170,13 @@ TEST(JsonStdStringWriterTest, RepresentingNonFiniteValuesAsNull) {
}
TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) {
// The encoder emits binary submitted to StreamingParserHandler::HandleBinary
// The encoder emits binary submitted to ParserHandler::HandleBinary
// as base64. The following three examples are taken from
// https://en.wikipedia.org/wiki/Base64.
{
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleBinary(SpanFrom(std::vector<uint8_t>({'M', 'a', 'n'})));
EXPECT_TRUE(status.ok());
......@@ -187,7 +185,7 @@ TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) {
{
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleBinary(SpanFrom(std::vector<uint8_t>({'M', 'a'})));
EXPECT_TRUE(status.ok());
......@@ -196,7 +194,7 @@ TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) {
{
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleBinary(SpanFrom(std::vector<uint8_t>({'M'})));
EXPECT_TRUE(status.ok());
......@@ -205,7 +203,7 @@ TEST(JsonStdStringWriterTest, BinaryEncodedAsJsonString) {
{ // "Hello, world.", verified with base64decode.org.
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleBinary(SpanFrom(std::vector<uint8_t>(
{'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '.'})));
......@@ -219,7 +217,7 @@ TEST(JsonStdStringWriterTest, HandlesErrors) {
// status and clears the output.
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&GetTestPlatform(), &out, &status);
writer->HandleMapBegin();
WriteUTF8AsUTF16(writer.get(), "msg1");
......@@ -257,7 +255,7 @@ TEST(JsonStdStringWriterTest, DoubleToString) {
std::string out;
Status status;
std::unique_ptr<StreamingParserHandler> writer =
std::unique_ptr<ParserHandler> writer =
NewJSONEncoder(&platform, &out, &status);
writer->HandleArrayBegin();
writer->HandleDouble(.1);
......@@ -270,7 +268,7 @@ TEST(JsonStdStringWriterTest, DoubleToString) {
// json::ParseJSON - for receiving streaming parser events for JSON
// =============================================================================
class Log : public StreamingParserHandler {
class Log : public ParserHandler {
public:
void HandleMapBegin() override { log_ << "map begin\n"; }
......
......@@ -5,16 +5,7 @@
#ifndef V8_CRDTP_PARSER_HANDLER_H_
#define V8_CRDTP_PARSER_HANDLER_H_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "export.h"
#include "span.h"
#include "status.h"
......@@ -22,9 +13,9 @@ namespace v8_crdtp {
// Handler interface for parser events emitted by a streaming parser.
// See cbor::NewCBOREncoder, cbor::ParseCBOR, json::NewJSONEncoder,
// json::ParseJSON.
class StreamingParserHandler {
class ParserHandler {
public:
virtual ~StreamingParserHandler() = default;
virtual ~ParserHandler() = default;
virtual void HandleMapBegin() = 0;
virtual void HandleMapEnd() = 0;
virtual void HandleArrayBegin() = 0;
......
......@@ -6,16 +6,11 @@
#define V8_CRDTP_SPAN_H_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "export.h"
namespace v8_crdtp {
// =============================================================================
// span - sequence of bytes
......
......@@ -2,22 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "span.h"
#include <array>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "span.h"
#include "test_platform.h"
using testing::ElementsAreArray;
namespace v8_crdtp {
// =============================================================================
// span - sequence of bytes
......
......@@ -4,13 +4,6 @@
#include "status.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <limits>
#include <stack>
namespace v8_crdtp {
// =============================================================================
// Status and Error codes
......
......@@ -2,17 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_CRDTP_ENCODING_H_
#define V8_CRDTP_ENCODING_H_
#ifndef V8_CRDTP_STATUS_H_
#define V8_CRDTP_STATUS_H_
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "export.h"
......@@ -20,6 +15,7 @@ namespace v8_crdtp {
// =============================================================================
// Status and Error codes
// =============================================================================
enum class Error {
OK = 0,
// JSON parsing errors - json_parser.{h,cc}.
......@@ -89,4 +85,4 @@ struct Status {
};
} // namespace v8_crdtp
#endif // V8_CRDTP_ENCODING_H_
#endif // V8_CRDTP_STATUS_H_
......@@ -3,21 +3,8 @@
// found in the LICENSE file.
#include "status.h"
#include <array>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "test_platform.h"
using testing::ElementsAreArray;
namespace v8_crdtp {
// =============================================================================
// Status and Error codes
......
......@@ -317,13 +317,12 @@ std::unique_ptr<Serializable> InternalResponse::createErrorResponse(int callId,
void InternalResponse::AppendSerialized(std::vector<uint8_t>* out) const
{
using {{config.crdtp.namespace}}::cbor::NewCBOREncoder;
using {{config.crdtp.namespace}}::StreamingParserHandler;
using {{config.crdtp.namespace}}::ParserHandler;
using {{config.crdtp.namespace}}::Status;
using {{config.crdtp.namespace}}::SpanFrom;
Status status;
std::unique_ptr<StreamingParserHandler> encoder =
NewCBOREncoder(out, &status);
std::unique_ptr<ParserHandler> encoder = NewCBOREncoder(out, &status);
encoder->HandleMapBegin();
if (m_method) {
encoder->HandleString8(SpanFrom("method"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment