Remove SpdyFrameReader. Repurpose spdy_frame_reader_test.cc as quiche_data_reader_test.cc. PiperOrigin-RevId: 422859761
diff --git a/common/quiche_data_reader_test.cc b/common/quiche_data_reader_test.cc new file mode 100644 index 0000000..52ed292 --- /dev/null +++ b/common/quiche_data_reader_test.cc
@@ -0,0 +1,187 @@ +// Copyright 2022 The Chromium 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 "common/quiche_data_reader.h" + +#include <cstdint> + +#include "common/platform/api/quiche_test.h" +#include "common/quiche_endian.h" + +namespace quiche { + +// TODO(b/214573190): Test Endianness::HOST_BYTE_ORDER. +// TODO(b/214573190): Test ReadUInt8, ReadUInt24, ReadUInt64, ReadBytesToUInt64, +// ReadStringPiece8, ReadStringPiece, ReadTag, etc. + +TEST(QuicheDataReaderTest, ReadUInt16) { + // Data in network byte order. + const uint16_t kData[] = { + QuicheEndian::HostToNet16(1), + QuicheEndian::HostToNet16(1 << 15), + }; + + QuicheDataReader reader(reinterpret_cast<const char*>(kData), sizeof(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + uint16_t uint16_val; + EXPECT_TRUE(reader.ReadUInt16(&uint16_val)); + EXPECT_FALSE(reader.IsDoneReading()); + EXPECT_EQ(1, uint16_val); + + EXPECT_TRUE(reader.ReadUInt16(&uint16_val)); + EXPECT_TRUE(reader.IsDoneReading()); + EXPECT_EQ(1 << 15, uint16_val); +} + +TEST(QuicheDataReaderTest, ReadUInt32) { + // Data in network byte order. + const uint32_t kData[] = { + QuicheEndian::HostToNet32(1), + QuicheEndian::HostToNet32(0x80000000), + }; + + QuicheDataReader reader(reinterpret_cast<const char*>(kData), + ABSL_ARRAYSIZE(kData) * sizeof(uint32_t)); + EXPECT_FALSE(reader.IsDoneReading()); + + uint32_t uint32_val; + EXPECT_TRUE(reader.ReadUInt32(&uint32_val)); + EXPECT_FALSE(reader.IsDoneReading()); + EXPECT_EQ(1u, uint32_val); + + EXPECT_TRUE(reader.ReadUInt32(&uint32_val)); + EXPECT_TRUE(reader.IsDoneReading()); + EXPECT_EQ(1u << 31, uint32_val); +} + +TEST(QuicheDataReaderTest, ReadStringPiece16) { + // Data in network byte order. + const char kData[] = { + 0x00, 0x02, // uint16_t(2) + 0x48, 0x69, // "Hi" + 0x00, 0x10, // uint16_t(16) + 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, + 0x20, 0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3" + }; + + QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + absl::string_view stringpiece_val; + EXPECT_TRUE(reader.ReadStringPiece16(&stringpiece_val)); + EXPECT_FALSE(reader.IsDoneReading()); + EXPECT_EQ(0, stringpiece_val.compare("Hi")); + + EXPECT_TRUE(reader.ReadStringPiece16(&stringpiece_val)); + EXPECT_TRUE(reader.IsDoneReading()); + EXPECT_EQ(0, stringpiece_val.compare("Testing, 1, 2, 3")); +} + +TEST(QuicheDataReaderTest, ReadUInt16WithBufferTooSmall) { + // Data in network byte order. + const char kData[] = { + 0x00, // part of a uint16_t + }; + + QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + uint16_t uint16_val; + EXPECT_FALSE(reader.ReadUInt16(&uint16_val)); +} + +TEST(QuicheDataReaderTest, ReadUInt32WithBufferTooSmall) { + // Data in network byte order. + const char kData[] = { + 0x00, 0x00, 0x00, // part of a uint32_t + }; + + QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + uint32_t uint32_val; + EXPECT_FALSE(reader.ReadUInt32(&uint32_val)); + + // Also make sure that trying to read a uint16_t, which technically could + // work, fails immediately due to previously encountered failed read. + uint16_t uint16_val; + EXPECT_FALSE(reader.ReadUInt16(&uint16_val)); +} + +// Tests ReadStringPiece16() with a buffer too small to fit the entire string. +TEST(QuicheDataReaderTest, ReadStringPiece16WithBufferTooSmall) { + // Data in network byte order. + const char kData[] = { + 0x00, 0x03, // uint16_t(3) + 0x48, 0x69, // "Hi" + }; + + QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + absl::string_view stringpiece_val; + EXPECT_FALSE(reader.ReadStringPiece16(&stringpiece_val)); + + // Also make sure that trying to read a uint16_t, which technically could + // work, fails immediately due to previously encountered failed read. + uint16_t uint16_val; + EXPECT_FALSE(reader.ReadUInt16(&uint16_val)); +} + +// Tests ReadStringPiece16() with a buffer too small even to fit the length. +TEST(QuicheDataReaderTest, ReadStringPiece16WithBufferWayTooSmall) { + // Data in network byte order. + const char kData[] = { + 0x00, // part of a uint16_t + }; + + QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + absl::string_view stringpiece_val; + EXPECT_FALSE(reader.ReadStringPiece16(&stringpiece_val)); + + // Also make sure that trying to read a uint16_t, which technically could + // work, fails immediately due to previously encountered failed read. + uint16_t uint16_val; + EXPECT_FALSE(reader.ReadUInt16(&uint16_val)); +} + +TEST(QuicheDataReaderTest, ReadBytes) { + // Data in network byte order. + const char kData[] = { + 0x66, 0x6f, 0x6f, // "foo" + 0x48, 0x69, // "Hi" + }; + + QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + char dest1[3] = {}; + EXPECT_TRUE(reader.ReadBytes(&dest1, ABSL_ARRAYSIZE(dest1))); + EXPECT_FALSE(reader.IsDoneReading()); + EXPECT_EQ("foo", absl::string_view(dest1, ABSL_ARRAYSIZE(dest1))); + + char dest2[2] = {}; + EXPECT_TRUE(reader.ReadBytes(&dest2, ABSL_ARRAYSIZE(dest2))); + EXPECT_TRUE(reader.IsDoneReading()); + EXPECT_EQ("Hi", absl::string_view(dest2, ABSL_ARRAYSIZE(dest2))); +} + +TEST(QuicheDataReaderTest, ReadBytesWithBufferTooSmall) { + // Data in network byte order. + const char kData[] = { + 0x01, + }; + + QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData)); + EXPECT_FALSE(reader.IsDoneReading()); + + char dest[ABSL_ARRAYSIZE(kData) + 2] = {}; + EXPECT_FALSE(reader.ReadBytes(&dest, ABSL_ARRAYSIZE(kData) + 1)); + EXPECT_STREQ("", dest); +} + +} // namespace quiche
diff --git a/spdy/core/spdy_frame_reader.cc b/spdy/core/spdy_frame_reader.cc deleted file mode 100644 index bd2305f..0000000 --- a/spdy/core/spdy_frame_reader.cc +++ /dev/null
@@ -1,202 +0,0 @@ -// Copyright (c) 2012 The Chromium 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 "spdy/core/spdy_frame_reader.h" - -#include "common/quiche_endian.h" -#include "spdy/core/spdy_protocol.h" - -namespace spdy { - -SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len) - : data_(data), len_(len), ofs_(0) {} - -bool SpdyFrameReader::ReadUInt8(uint8_t* result) { - // Make sure that we have the whole uint8_t. - if (!CanRead(1)) { - OnFailure(); - return false; - } - - // Read into result. - *result = *reinterpret_cast<const uint8_t*>(data_ + ofs_); - - // Iterate. - ofs_ += 1; - - return true; -} - -bool SpdyFrameReader::ReadUInt16(uint16_t* result) { - // Make sure that we have the whole uint16_t. - if (!CanRead(2)) { - OnFailure(); - return false; - } - - // Read into result. - *result = quiche::QuicheEndian::NetToHost16( - *(reinterpret_cast<const uint16_t*>(data_ + ofs_))); - - // Iterate. - ofs_ += 2; - - return true; -} - -bool SpdyFrameReader::ReadUInt32(uint32_t* result) { - // Make sure that we have the whole uint32_t. - if (!CanRead(4)) { - OnFailure(); - return false; - } - - // Read into result. - *result = quiche::QuicheEndian::NetToHost32( - *(reinterpret_cast<const uint32_t*>(data_ + ofs_))); - - // Iterate. - ofs_ += 4; - - return true; -} - -bool SpdyFrameReader::ReadUInt64(uint64_t* result) { - // Make sure that we have the whole uint64_t. - if (!CanRead(8)) { - OnFailure(); - return false; - } - - // Read into result. Network byte order is big-endian. - uint64_t upper = quiche::QuicheEndian::NetToHost32( - *(reinterpret_cast<const uint32_t*>(data_ + ofs_))); - uint64_t lower = quiche::QuicheEndian::NetToHost32( - *(reinterpret_cast<const uint32_t*>(data_ + ofs_ + 4))); - *result = (upper << 32) + lower; - - // Iterate. - ofs_ += 8; - - return true; -} - -bool SpdyFrameReader::ReadUInt31(uint32_t* result) { - bool success = ReadUInt32(result); - - // Zero out highest-order bit. - if (success) { - *result &= 0x7fffffff; - } - - return success; -} - -bool SpdyFrameReader::ReadUInt24(uint32_t* result) { - // Make sure that we have the whole uint24_t. - if (!CanRead(3)) { - OnFailure(); - return false; - } - - // Read into result. - *result = 0; - memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3); - *result = quiche::QuicheEndian::NetToHost32(*result); - - // Iterate. - ofs_ += 3; - - return true; -} - -bool SpdyFrameReader::ReadStringPiece16(absl::string_view* result) { - // Read resultant length. - uint16_t result_len; - if (!ReadUInt16(&result_len)) { - // OnFailure() already called. - return false; - } - - // Make sure that we have the whole string. - if (!CanRead(result_len)) { - OnFailure(); - return false; - } - - // Set result. - *result = absl::string_view(data_ + ofs_, result_len); - - // Iterate. - ofs_ += result_len; - - return true; -} - -bool SpdyFrameReader::ReadStringPiece32(absl::string_view* result) { - // Read resultant length. - uint32_t result_len; - if (!ReadUInt32(&result_len)) { - // OnFailure() already called. - return false; - } - - // Make sure that we have the whole string. - if (!CanRead(result_len)) { - OnFailure(); - return false; - } - - // Set result. - *result = absl::string_view(data_ + ofs_, result_len); - - // Iterate. - ofs_ += result_len; - - return true; -} - -bool SpdyFrameReader::ReadBytes(void* result, size_t size) { - // Make sure that we have enough data to read. - if (!CanRead(size)) { - OnFailure(); - return false; - } - - // Read into result. - memcpy(result, data_ + ofs_, size); - - // Iterate. - ofs_ += size; - - return true; -} - -bool SpdyFrameReader::Seek(size_t size) { - if (!CanRead(size)) { - OnFailure(); - return false; - } - - // Iterate. - ofs_ += size; - - return true; -} - -bool SpdyFrameReader::IsDoneReading() const { - return len_ == ofs_; -} - -bool SpdyFrameReader::CanRead(size_t bytes) const { - return bytes <= (len_ - ofs_); -} - -void SpdyFrameReader::OnFailure() { - // Set our iterator to the end of the buffer so that further reads fail - // immediately. - ofs_ = len_; -} - -} // namespace spdy
diff --git a/spdy/core/spdy_frame_reader.h b/spdy/core/spdy_frame_reader.h deleted file mode 100644 index f4078c9..0000000 --- a/spdy/core/spdy_frame_reader.h +++ /dev/null
@@ -1,129 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef QUICHE_SPDY_CORE_SPDY_FRAME_READER_H_ -#define QUICHE_SPDY_CORE_SPDY_FRAME_READER_H_ - -#include <cstdint> - -#include "absl/strings/string_view.h" -#include "common/platform/api/quiche_export.h" - -namespace spdy { - -// Used for reading SPDY frames. Though there isn't really anything terribly -// SPDY-specific here, it's a helper class that's useful when doing SPDY -// framing. -// -// To use, simply construct a SpdyFramerReader using the underlying buffer that -// you'd like to read fields from, then call one of the Read*() methods to -// actually do some reading. -// -// This class keeps an internal iterator to keep track of what's already been -// read and each successive Read*() call automatically increments said iterator -// on success. On failure, internal state of the SpdyFrameReader should not be -// trusted and it is up to the caller to throw away the failed instance and -// handle the error as appropriate. None of the Read*() methods should ever be -// called after failure, as they will also fail immediately. -class QUICHE_EXPORT_PRIVATE SpdyFrameReader { - public: - // Caller must provide an underlying buffer to work on. - SpdyFrameReader(const char* data, const size_t len); - - // Empty destructor. - ~SpdyFrameReader() {} - - // Reads an 8-bit unsigned integer into the given output parameter. - // Forwards the internal iterator on success. - // Returns true on success, false otherwise. - bool ReadUInt8(uint8_t* result); - - // Reads a 16-bit unsigned integer into the given output parameter. - // Forwards the internal iterator on success. - // Returns true on success, false otherwise. - bool ReadUInt16(uint16_t* result); - - // Reads a 32-bit unsigned integer into the given output parameter. - // Forwards the internal iterator on success. - // Returns true on success, false otherwise. - bool ReadUInt32(uint32_t* result); - - // Reads a 64-bit unsigned integer into the given output parameter. - // Forwards the internal iterator on success. - // Returns true on success, false otherwise. - bool ReadUInt64(uint64_t* result); - - // Reads a 31-bit unsigned integer into the given output parameter. This is - // equivalent to ReadUInt32() above except that the highest-order bit is - // discarded. - // Forwards the internal iterator (by 4B) on success. - // Returns true on success, false otherwise. - bool ReadUInt31(uint32_t* result); - - // Reads a 24-bit unsigned integer into the given output parameter. - // Forwards the internal iterator (by 3B) on success. - // Returns true on success, false otherwise. - bool ReadUInt24(uint32_t* result); - - // Reads a string prefixed with 16-bit length into the given output parameter. - // - // NOTE: Does not copy but rather references strings in the underlying buffer. - // This should be kept in mind when handling memory management! - // - // Forwards the internal iterator on success. - // Returns true on success, false otherwise. - bool ReadStringPiece16(absl::string_view* result); - - // Reads a string prefixed with 32-bit length into the given output parameter. - // - // NOTE: Does not copy but rather references strings in the underlying buffer. - // This should be kept in mind when handling memory management! - // - // Forwards the internal iterator on success. - // Returns true on success, false otherwise. - bool ReadStringPiece32(absl::string_view* result); - - // Reads a given number of bytes into the given buffer. The buffer - // must be of adequate size. - // Forwards the internal iterator on success. - // Returns true on success, false otherwise. - bool ReadBytes(void* result, size_t size); - - // Seeks a given number of bytes into the buffer from the current offset. - // Equivelant to an empty read. - // Forwards the internal iterator. - // Returns true on success, false otherwise. - bool Seek(size_t size); - - // Rewinds this reader to the beginning of the frame. - void Rewind() { ofs_ = 0; } - - // Returns true if the entirety of the underlying buffer has been read via - // Read*() calls. - bool IsDoneReading() const; - - // Returns the number of bytes that have been consumed by the reader so far. - size_t GetBytesConsumed() const { return ofs_; } - - private: - // Returns true if the underlying buffer has enough room to read the given - // amount of bytes. - bool CanRead(size_t bytes) const; - - // To be called when a read fails for any reason. - void OnFailure(); - - // The data buffer that we're reading from. - const char* data_; - - // The length of the data buffer that we're reading from. - const size_t len_; - - // The location of the next read from our data buffer. - size_t ofs_; -}; - -} // namespace spdy - -#endif // QUICHE_SPDY_CORE_SPDY_FRAME_READER_H_
diff --git a/spdy/core/spdy_frame_reader_test.cc b/spdy/core/spdy_frame_reader_test.cc deleted file mode 100644 index 035cac9..0000000 --- a/spdy/core/spdy_frame_reader_test.cc +++ /dev/null
@@ -1,247 +0,0 @@ -// Copyright (c) 2012 The Chromium 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 "spdy/core/spdy_frame_reader.h" - -#include <cstdint> - -#include "absl/base/macros.h" -#include "common/platform/api/quiche_test.h" -#include "common/quiche_endian.h" - -namespace spdy { - -TEST(SpdyFrameReaderTest, ReadUInt16) { - // Frame data in network byte order. - const uint16_t kFrameData[] = { - quiche::QuicheEndian::HostToNet16(1), - quiche::QuicheEndian::HostToNet16(1 << 15), - }; - - SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), - sizeof(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - uint16_t uint16_val; - EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - EXPECT_EQ(1, uint16_val); - - EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); - EXPECT_TRUE(frame_reader.IsDoneReading()); - EXPECT_EQ(1 << 15, uint16_val); -} - -TEST(SpdyFrameReaderTest, ReadUInt32) { - // Frame data in network byte order. - const uint32_t kFrameData[] = { - quiche::QuicheEndian::HostToNet32(1), - quiche::QuicheEndian::HostToNet32(0x80000000), - }; - - SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), - ABSL_ARRAYSIZE(kFrameData) * sizeof(uint32_t)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - uint32_t uint32_val; - EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - EXPECT_EQ(1u, uint32_val); - - EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); - EXPECT_TRUE(frame_reader.IsDoneReading()); - EXPECT_EQ(1u << 31, uint32_val); -} - -TEST(SpdyFrameReaderTest, ReadStringPiece16) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, 0x02, // uint16_t(2) - 0x48, 0x69, // "Hi" - 0x00, 0x10, // uint16_t(16) - 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, - 0x20, 0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3" - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - absl::string_view stringpiece_val; - EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - EXPECT_EQ(0, stringpiece_val.compare("Hi")); - - EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); - EXPECT_TRUE(frame_reader.IsDoneReading()); - EXPECT_EQ(0, stringpiece_val.compare("Testing, 1, 2, 3")); -} - -TEST(SpdyFrameReaderTest, ReadStringPiece32) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, 0x00, 0x00, 0x03, // uint32_t(3) - 0x66, 0x6f, 0x6f, // "foo" - 0x00, 0x00, 0x00, 0x10, // uint32_t(16) - 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, - 0x20, 0x34, 0x2c, 0x20, 0x35, 0x2c, 0x20, 0x36, // "Testing, 4, 5, 6" - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - absl::string_view stringpiece_val; - EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - EXPECT_EQ(0, stringpiece_val.compare("foo")); - - EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); - EXPECT_TRUE(frame_reader.IsDoneReading()); - EXPECT_EQ(0, stringpiece_val.compare("Testing, 4, 5, 6")); -} - -TEST(SpdyFrameReaderTest, ReadUInt16WithBufferTooSmall) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, // part of a uint16_t - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - uint16_t uint16_val; - EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); -} - -TEST(SpdyFrameReaderTest, ReadUInt32WithBufferTooSmall) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, 0x00, 0x00, // part of a uint32_t - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - uint32_t uint32_val; - EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val)); - - // Also make sure that trying to read a uint16_t, which technically could - // work, fails immediately due to previously encountered failed read. - uint16_t uint16_val; - EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); -} - -// Tests ReadStringPiece16() with a buffer too small to fit the entire string. -TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, 0x03, // uint16_t(3) - 0x48, 0x69, // "Hi" - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - absl::string_view stringpiece_val; - EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); - - // Also make sure that trying to read a uint16_t, which technically could - // work, fails immediately due to previously encountered failed read. - uint16_t uint16_val; - EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); -} - -// Tests ReadStringPiece16() with a buffer too small even to fit the length. -TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, // part of a uint16_t - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - absl::string_view stringpiece_val; - EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); - - // Also make sure that trying to read a uint16_t, which technically could - // work, fails immediately due to previously encountered failed read. - uint16_t uint16_val; - EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); -} - -// Tests ReadStringPiece32() with a buffer too small to fit the entire string. -TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferTooSmall) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, 0x00, 0x00, 0x03, // uint32_t(3) - 0x48, 0x69, // "Hi" - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - absl::string_view stringpiece_val; - EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); - - // Also make sure that trying to read a uint16_t, which technically could - // work, fails immediately due to previously encountered failed read. - uint16_t uint16_val; - EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); -} - -// Tests ReadStringPiece32() with a buffer too small even to fit the length. -TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x00, 0x00, 0x00, // part of a uint32_t - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - absl::string_view stringpiece_val; - EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); - - // Also make sure that trying to read a uint16_t, which technically could - // work, fails immediately due to previously encountered failed read. - uint16_t uint16_val; - EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); -} - -TEST(SpdyFrameReaderTest, ReadBytes) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x66, 0x6f, 0x6f, // "foo" - 0x48, 0x69, // "Hi" - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - char dest1[3] = {}; - EXPECT_TRUE(frame_reader.ReadBytes(&dest1, ABSL_ARRAYSIZE(dest1))); - EXPECT_FALSE(frame_reader.IsDoneReading()); - EXPECT_EQ("foo", absl::string_view(dest1, ABSL_ARRAYSIZE(dest1))); - - char dest2[2] = {}; - EXPECT_TRUE(frame_reader.ReadBytes(&dest2, ABSL_ARRAYSIZE(dest2))); - EXPECT_TRUE(frame_reader.IsDoneReading()); - EXPECT_EQ("Hi", absl::string_view(dest2, ABSL_ARRAYSIZE(dest2))); -} - -TEST(SpdyFrameReaderTest, ReadBytesWithBufferTooSmall) { - // Frame data in network byte order. - const char kFrameData[] = { - 0x01, - }; - - SpdyFrameReader frame_reader(kFrameData, ABSL_ARRAYSIZE(kFrameData)); - EXPECT_FALSE(frame_reader.IsDoneReading()); - - char dest[ABSL_ARRAYSIZE(kFrameData) + 2] = {}; - EXPECT_FALSE(frame_reader.ReadBytes(&dest, ABSL_ARRAYSIZE(kFrameData) + 1)); - EXPECT_STREQ("", dest); -} - -} // namespace spdy
diff --git a/spdy/core/spdy_framer.cc b/spdy/core/spdy_framer.cc index 4d62858..e97d45c 100644 --- a/spdy/core/spdy_framer.cc +++ b/spdy/core/spdy_framer.cc
@@ -17,7 +17,6 @@ #include "common/platform/api/quiche_logging.h" #include "spdy/core/spdy_bitmasks.h" #include "spdy/core/spdy_frame_builder.h" -#include "spdy/core/spdy_frame_reader.h" namespace spdy {
diff --git a/spdy/core/spdy_framer_test.cc b/spdy/core/spdy_framer_test.cc index b138300..9f04576 100644 --- a/spdy/core/spdy_framer_test.cc +++ b/spdy/core/spdy_framer_test.cc
@@ -22,7 +22,6 @@ #include "spdy/core/recording_headers_handler.h" #include "spdy/core/spdy_bitmasks.h" #include "spdy/core/spdy_frame_builder.h" -#include "spdy/core/spdy_frame_reader.h" #include "spdy/core/spdy_protocol.h" #include "spdy/core/spdy_test_utils.h"