diff --git a/rtc_base/byte_buffer.h b/rtc_base/byte_buffer.h index 947a87b2dc..43851f2116 100644 --- a/rtc_base/byte_buffer.h +++ b/rtc_base/byte_buffer.h @@ -51,7 +51,9 @@ class ByteBufferWriterT { absl::string_view DataAsStringView() const { return absl::string_view(reinterpret_cast(Data()), Length()); } - char* DataAsCharPointer() const { return reinterpret_cast(Data()); } + const char* DataAsCharPointer() const { + return reinterpret_cast(Data()); + } // Write value to the buffer. Resizes the buffer when it is // neccessary. @@ -91,7 +93,7 @@ class ByteBufferWriterT { WriteBytes(&last_byte, 1); } void WriteString(absl::string_view val) { - WriteBytes(val.data(), val.size()); + WriteBytes(reinterpret_cast(val.data()), val.size()); } void WriteBytes(const value_type* val, size_t len) { buffer_.AppendData(val, len); diff --git a/rtc_base/byte_buffer_unittest.cc b/rtc_base/byte_buffer_unittest.cc index f65299e639..7d38d809f4 100644 --- a/rtc_base/byte_buffer_unittest.cc +++ b/rtc_base/byte_buffer_unittest.cc @@ -16,10 +16,26 @@ #include "rtc_base/arraysize.h" #include "rtc_base/byte_order.h" +#include "test/gmock.h" #include "test/gtest.h" namespace rtc { +using ::testing::ElementsAre; + +TEST(ByteBufferTest, WriterAccessors) { + // To be changed into ByteBufferWriter when base type is converted. + ByteBufferWriterT> buffer; + buffer.WriteString("abc"); + EXPECT_EQ(buffer.Length(), 3U); + EXPECT_THAT(buffer.DataView(), ElementsAre('a', 'b', 'c')); + EXPECT_EQ(absl::string_view("abc"), buffer.DataAsStringView()); + + buffer.WriteUInt8(0); + EXPECT_STREQ(buffer.DataAsCharPointer(), "abc"); + EXPECT_STREQ(reinterpret_cast(buffer.Data()), "abc"); +} + TEST(ByteBufferTest, TestByteOrder) { uint16_t n16 = 1; uint32_t n32 = 1;