From 487b8743cbc79fcda2e361e686d60088bf3652ee Mon Sep 17 00:00:00 2001 From: Hakkyu Kim Date: Fri, 23 Jul 2021 06:26:01 +0900 Subject: [PATCH] Removes unnecessary error message: "Invalid read in StandardCodecByteStreamReader" for cpp BasicMessageChannel (flutter/engine#26956) --- .../common/client_wrapper/standard_codec.cc | 3 +++ .../standard_message_codec_unittests.cc | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/engine/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc b/engine/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc index e37e4a18dc4..d7dd89d5857 100644 --- a/engine/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc +++ b/engine/src/flutter/shell/platform/common/client_wrapper/standard_codec.cc @@ -318,6 +318,9 @@ StandardMessageCodec::~StandardMessageCodec() = default; std::unique_ptr StandardMessageCodec::DecodeMessageInternal( const uint8_t* binary_message, size_t message_size) const { + if (!binary_message) { + return std::make_unique(); + } ByteBufferStreamReader stream(binary_message, message_size); return std::make_unique(serializer_->ReadValue(&stream)); } diff --git a/engine/src/flutter/shell/platform/common/client_wrapper/standard_message_codec_unittests.cc b/engine/src/flutter/shell/platform/common/client_wrapper/standard_message_codec_unittests.cc index c7a92427df6..9dea217adb3 100644 --- a/engine/src/flutter/shell/platform/common/client_wrapper/standard_message_codec_unittests.cc +++ b/engine/src/flutter/shell/platform/common/client_wrapper/standard_message_codec_unittests.cc @@ -8,10 +8,23 @@ #include #include "flutter/shell/platform/common/client_wrapper/testing/test_codec_extensions.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" namespace flutter { +namespace { + +class MockStandardCodecSerializer : public StandardCodecSerializer { + public: + MOCK_CONST_METHOD2(WriteValue, + void(const EncodableValue& value, + ByteStreamWriter* stream)); + MOCK_CONST_METHOD2(ReadValueOfType, + EncodableValue(uint8_t type, ByteStreamReader* stream)); +}; +} // namespace + // Validates round-trip encoding and decoding of |value|, and checks that the // encoded value matches |expected_encoding|. // @@ -68,6 +81,18 @@ TEST(StandardMessageCodec, CanEncodeAndDecodeNull) { CheckEncodeDecode(EncodableValue(), bytes); } +TEST(StandardMessageCodec, CanDecodeEmptyBytesAsNullWithoutCallingSerializer) { + std::vector bytes = {}; + const MockStandardCodecSerializer serializer; + const StandardMessageCodec& codec = + StandardMessageCodec::GetInstance(&serializer); + + auto decoded = codec.DecodeMessage(bytes); + + EXPECT_EQ(EncodableValue(), *decoded); + EXPECT_CALL(serializer, ReadValueOfType(::testing::_, ::testing::_)).Times(0); +} + TEST(StandardMessageCodec, CanEncodeAndDecodeTrue) { std::vector bytes = {0x01}; CheckEncodeDecode(EncodableValue(true), bytes);