diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-05-12 15:59:20 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-05-25 06:57:22 +0000 |
commit | f7eaed5286974984ba5f9e3189d8f49d03e99f81 (patch) | |
tree | caed19b2af2024f35449fb0b781d0a25e09d4f8f /chromium/net/third_party/quiche/src/common | |
parent | 9729c4479fe23554eae6e6dd1f30ff488f470c84 (diff) | |
download | qtwebengine-chromium-f7eaed5286974984ba5f9e3189d8f49d03e99f81.tar.gz |
BASELINE: Update Chromium to 100.0.4896.167
Change-Id: I98cbeb5d7543d966ffe04d8cefded0c493a11333
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/net/third_party/quiche/src/common')
14 files changed, 394 insertions, 9 deletions
diff --git a/chromium/net/third_party/quiche/src/common/platform/api/quiche_command_line_flags.h b/chromium/net/third_party/quiche/src/common/platform/api/quiche_command_line_flags.h new file mode 100644 index 00000000000..3ba48bbab5b --- /dev/null +++ b/chromium/net/third_party/quiche/src/common/platform/api/quiche_command_line_flags.h @@ -0,0 +1,38 @@ +// Copyright (c) 2017 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_COMMON_PLATFORM_API_QUICHE_COMMAND_LINE_FLAGS_H_ +#define QUICHE_COMMON_PLATFORM_API_QUICHE_COMMAND_LINE_FLAGS_H_ + +#include <string> +#include <vector> + +#include "net/quiche/common/platform/impl/quiche_command_line_flags_impl.h" + +// Define a command-line flag that can be automatically set via +// QuicheParseCommandLineFlags(). +#define DEFINE_QUICHE_COMMAND_LINE_FLAG(type, name, default_value, help) \ + DEFINE_QUICHE_COMMAND_LINE_FLAG_IMPL(type, name, default_value, help) + +namespace quiche { + +// Parses command-line flags, setting flag variables defined using +// DEFINE_QUICHE_COMMAND_LINE_FLAG if they appear in the command line, and +// returning a list of any non-flag arguments specified in the command line. If +// the command line specifies '-h' or '--help', prints a usage message with flag +// descriptions to stdout and exits with status 0. If a flag has an unparsable +// value, writes an error message to stderr and exits with status 1. +inline std::vector<std::string> QuicheParseCommandLineFlags( + const char* usage, int argc, const char* const* argv) { + return QuicheParseCommandLineFlagsImpl(usage, argc, argv); +} + +// Prints a usage message with flag descriptions to stdout. +inline void QuichePrintCommandLineFlagHelp(const char* usage) { + QuichePrintCommandLineFlagHelpImpl(usage); +} + +} // namespace quiche + +#endif // QUICHE_COMMON_PLATFORM_API_QUICHE_COMMAND_LINE_FLAGS_H_ diff --git a/chromium/net/third_party/quiche/src/common/platform/api/quiche_export.h b/chromium/net/third_party/quiche/src/common/platform/api/quiche_export.h index 343985f98ea..4f64819d366 100644 --- a/chromium/net/third_party/quiche/src/common/platform/api/quiche_export.h +++ b/chromium/net/third_party/quiche/src/common/platform/api/quiche_export.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef THIRD_PARTY_QUICHE_PLATFORM_API_QUICHE_EXPORT_H_ -#define THIRD_PARTY_QUICHE_PLATFORM_API_QUICHE_EXPORT_H_ +#ifndef QUICHE_COMMON_PLATFORM_API_QUICHE_EXPORT_H_ +#define QUICHE_COMMON_PLATFORM_API_QUICHE_EXPORT_H_ #include "quiche_platform_impl/quiche_export_impl.h" @@ -18,4 +18,4 @@ // defined in a header, or is built in Chromium as part of tests or tools. #define QUICHE_NO_EXPORT QUICHE_NO_EXPORT_IMPL -#endif // THIRD_PARTY_QUICHE_PLATFORM_API_QUICHE_EXPORT_H_ +#endif // QUICHE_COMMON_PLATFORM_API_QUICHE_EXPORT_H_ diff --git a/chromium/net/third_party/quiche/src/common/platform/api/quiche_flag_utils.h b/chromium/net/third_party/quiche/src/common/platform/api/quiche_flag_utils.h index 0534064e2e7..fcd66231e7d 100644 --- a/chromium/net/third_party/quiche/src/common/platform/api/quiche_flag_utils.h +++ b/chromium/net/third_party/quiche/src/common/platform/api/quiche_flag_utils.h @@ -5,7 +5,7 @@ #ifndef QUICHE_COMMON_PLATFORM_API_QUICHE_FLAG_UTILS_H_ #define QUICHE_COMMON_PLATFORM_API_QUICHE_FLAG_UTILS_H_ -#include "net/quiche/common/platform/impl/quiche_flag_utils_impl.h" +#include "quiche_platform_impl/quiche_flag_utils_impl.h" #define QUICHE_RELOADABLE_FLAG_COUNT QUICHE_RELOADABLE_FLAG_COUNT_IMPL #define QUICHE_RELOADABLE_FLAG_COUNT_N QUICHE_RELOADABLE_FLAG_COUNT_N_IMPL diff --git a/chromium/net/third_party/quiche/src/common/platform/api/quiche_flags.h b/chromium/net/third_party/quiche/src/common/platform/api/quiche_flags.h index 83db9d06948..8b26edb0d9a 100644 --- a/chromium/net/third_party/quiche/src/common/platform/api/quiche_flags.h +++ b/chromium/net/third_party/quiche/src/common/platform/api/quiche_flags.h @@ -5,7 +5,7 @@ #ifndef QUICHE_COMMON_PLATFORM_API_QUICHE_FLAGS_H_ #define QUICHE_COMMON_PLATFORM_API_QUICHE_FLAGS_H_ -#include "net/quiche/common/platform/impl/quiche_flags_impl.h" +#include "quiche_platform_impl/quiche_flags_impl.h" #define GetQuicheReloadableFlag(module, flag) \ GetQuicheReloadableFlagImpl(module, flag) diff --git a/chromium/net/third_party/quiche/src/common/platform/api/quiche_lower_case_string.h b/chromium/net/third_party/quiche/src/common/platform/api/quiche_lower_case_string.h new file mode 100644 index 00000000000..4492b3c7f51 --- /dev/null +++ b/chromium/net/third_party/quiche/src/common/platform/api/quiche_lower_case_string.h @@ -0,0 +1,16 @@ +// 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. + +#ifndef QUICHE_COMMON_PLATFORM_API_QUICHE_LOWER_CASE_STRING_H_ +#define QUICHE_COMMON_PLATFORM_API_QUICHE_LOWER_CASE_STRING_H_ + +#include "net/quiche/common/platform/impl/quiche_lower_case_string_impl.h" + +namespace quiche { + +using QuicheLowerCaseString = QuicheLowerCaseStringImpl; + +} // namespace quiche + +#endif // QUICHE_COMMON_PLATFORM_API_QUICHE_LOWER_CASE_STRING_H_ diff --git a/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_flags_impl.cc b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_flags_impl.cc new file mode 100644 index 00000000000..81b1d8f23ed --- /dev/null +++ b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_flags_impl.cc @@ -0,0 +1,29 @@ +// Copyright 2014 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 "quiche_platform_impl/quic_flags_impl.h" + +#define DEFINE_QUIC_PROTOCOL_FLAG_SINGLE_VALUE(type, flag, value, doc) \ + type FLAGS_##flag = value; + +#define DEFINE_QUIC_PROTOCOL_FLAG_TWO_VALUES(type, flag, internal_value, \ + external_value, doc) \ + type FLAGS_##flag = external_value; + +// Preprocessor macros can only have one definition. +// Select the right macro based on the number of arguments. +#define GET_6TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, ...) arg6 +#define QUIC_PROTOCOL_FLAG_MACRO_CHOOSER(...) \ + GET_6TH_ARG(__VA_ARGS__, DEFINE_QUIC_PROTOCOL_FLAG_TWO_VALUES, \ + DEFINE_QUIC_PROTOCOL_FLAG_SINGLE_VALUE) +#define QUIC_PROTOCOL_FLAG(...) \ + QUIC_PROTOCOL_FLAG_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) + +#include "quic/core/quic_protocol_flags_list.h" + +#undef QUIC_PROTOCOL_FLAG +#undef QUIC_PROTOCOL_FLAG_MACRO_CHOOSER +#undef GET_6TH_ARG +#undef DEFINE_QUIC_PROTOCOL_FLAG_TWO_VALUES +#undef DEFINE_QUIC_PROTOCOL_FLAG_SINGLE_VALUE diff --git a/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_flags_impl.h b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_flags_impl.h new file mode 100644 index 00000000000..b58d24d6c58 --- /dev/null +++ b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_flags_impl.h @@ -0,0 +1,18 @@ +// Copyright 2014 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_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUIC_FLAGS_IMPL_H_ +#define QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUIC_FLAGS_IMPL_H_ + +#include <string> + +#include "common/platform/api/quiche_export.h" +#include "common/platform/api/quiche_flags.h" + +#define QUIC_PROTOCOL_FLAG(type, flag, ...) \ + QUICHE_EXPORT_PRIVATE extern type FLAGS_##flag; +#include "quic/core/quic_protocol_flags_list.h" +#undef QUIC_PROTOCOL_FLAG + +#endif // QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUIC_FLAGS_IMPL_H_ diff --git a/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_testvalue_impl.h b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_testvalue_impl.h index f4678e7fbeb..bc0d95e58ef 100644 --- a/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_testvalue_impl.h +++ b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quic_testvalue_impl.h @@ -1,5 +1,5 @@ -#ifndef QUICHE_COMMON_PLATFORM_DEFAULT_QUIC_TESTVALUE_IMPL_H_ -#define QUICHE_COMMON_PLATFORM_DEFAULT_QUIC_TESTVALUE_IMPL_H_ +#ifndef QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUIC_TESTVALUE_IMPL_H_ +#define QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUIC_TESTVALUE_IMPL_H_ #include "absl/strings/string_view.h" @@ -10,4 +10,4 @@ void AdjustTestValueImpl(absl::string_view /*label*/, T* /*var*/) {} } // namespace quic -#endif // QUICHE_COMMON_PLATFORM_DEFAULT_QUIC_TESTVALUE_IMPL_H_ +#endif // QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUIC_TESTVALUE_IMPL_H_ diff --git a/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flag_utils_impl.h b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flag_utils_impl.h new file mode 100644 index 00000000000..c38f75c67da --- /dev/null +++ b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flag_utils_impl.h @@ -0,0 +1,29 @@ +// 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. + +#ifndef QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_FLAG_UTILS_IMPL_H_ +#define QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_FLAG_UTILS_IMPL_H_ + +#define QUICHE_RELOADABLE_FLAG_COUNT_IMPL(flag) \ + do { \ + } while (0) +#define QUICHE_RELOADABLE_FLAG_COUNT_N_IMPL(flag, instance, total) \ + do { \ + } while (0) + +#define QUICHE_RESTART_FLAG_COUNT_IMPL(flag) \ + do { \ + } while (0) +#define QUICHE_RESTART_FLAG_COUNT_N_IMPL(flag, instance, total) \ + do { \ + } while (0) + +#define QUICHE_CODE_COUNT_IMPL(name) \ + do { \ + } while (0) +#define QUICHE_CODE_COUNT_N_IMPL(name, instance, total) \ + do { \ + } while (0) + +#endif // QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_FLAG_UTILS_IMPL_H_ diff --git a/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flags_impl.cc b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flags_impl.cc new file mode 100644 index 00000000000..b68f93d4ded --- /dev/null +++ b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flags_impl.cc @@ -0,0 +1,9 @@ +// Copyright 2020 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 "quiche_platform_impl/quiche_flags_impl.h" + +#define QUIC_FLAG(flag, value) bool flag = value; +#include "quic/core/quic_flags_list.h" +#undef QUIC_FLAG diff --git a/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flags_impl.h b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flags_impl.h new file mode 100644 index 00000000000..8d655edc35a --- /dev/null +++ b/chromium/net/third_party/quiche/src/common/platform/default/quiche_platform_impl/quiche_flags_impl.h @@ -0,0 +1,42 @@ +// Copyright 2020 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_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_FLAGS_IMPL_H_ +#define QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_FLAGS_IMPL_H_ + +#include <cstdint> +#include <map> +#include <memory> +#include <string> +#include <vector> + +#include "common/platform/api/quiche_export.h" + +#define QUIC_FLAG(flag, value) QUICHE_EXPORT_PRIVATE extern bool flag; +#include "quic/core/quic_flags_list.h" +#undef QUIC_FLAG + +inline bool GetQuicheFlagImpl(bool flag) { return flag; } +inline int32_t GetQuicheFlagImpl(int32_t flag) { return flag; } +inline int64_t GetQuicheFlagImpl(int64_t flag) { return flag; } +inline uint64_t GetQuicheFlagImpl(uint64_t flag) { return flag; } +inline double GetQuicheFlagImpl(double flag) { return flag; } +inline std::string GetQuicheFlagImpl(const std::string& flag) { return flag; } +#define SetQuicheFlagImpl(flag, value) ((flag) = (value)) + +// ------------------------------------------------------------------------ +// QUIC feature flags implementation. +// ------------------------------------------------------------------------ +#define QUICHE_RELOADABLE_FLAG(flag) FLAGS_quic_reloadable_flag_##flag +#define QUICHE_RESTART_FLAG(flag) FLAGS_quic_restart_flag_##flag +#define GetQuicheReloadableFlagImpl(module, flag) \ + GetQuicheFlag(QUICHE_RELOADABLE_FLAG(flag)) +#define SetQuicheReloadableFlagImpl(module, flag, value) \ + SetQuicheFlag(QUICHE_RELOADABLE_FLAG(flag), value) +#define GetQuicheRestartFlagImpl(module, flag) \ + GetQuicheFlag(QUICHE_RESTART_FLAG(flag)) +#define SetQuicheRestartFlagImpl(module, flag, value) \ + SetQuicheFlag(QUICHE_RESTART_FLAG(flag), value) + +#endif // QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_FLAGS_IMPL_H_ diff --git a/chromium/net/third_party/quiche/src/common/quiche_data_reader.cc b/chromium/net/third_party/quiche/src/common/quiche_data_reader.cc index f20c752bcce..1f948f1db8a 100644 --- a/chromium/net/third_party/quiche/src/common/quiche_data_reader.cc +++ b/chromium/net/third_party/quiche/src/common/quiche_data_reader.cc @@ -9,6 +9,7 @@ #include "absl/strings/numbers.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "common/platform/api/quiche_bug_tracker.h" #include "common/platform/api/quiche_logging.h" #include "common/quiche_endian.h" @@ -40,6 +41,21 @@ bool QuicheDataReader::ReadUInt16(uint16_t* result) { return true; } +bool QuicheDataReader::ReadUInt24(uint32_t* result) { + if (endianness_ != quiche::NETWORK_BYTE_ORDER) { + // TODO(b/214573190): Implement and test HOST_BYTE_ORDER case. + QUICHE_BUG(QuicheDataReader_ReadUInt24_NotImplemented); + return false; + } + + *result = 0; + if (!ReadBytes(reinterpret_cast<char*>(result) + 1, 3u)) { + return false; + } + *result = quiche::QuicheEndian::NetToHost32(*result); + return true; +} + bool QuicheDataReader::ReadUInt32(uint32_t* result) { if (!ReadBytes(result, sizeof(*result))) { return false; diff --git a/chromium/net/third_party/quiche/src/common/quiche_data_reader.h b/chromium/net/third_party/quiche/src/common/quiche_data_reader.h index 833198db879..ae8bbcf56db 100644 --- a/chromium/net/third_party/quiche/src/common/quiche_data_reader.h +++ b/chromium/net/third_party/quiche/src/common/quiche_data_reader.h @@ -45,11 +45,12 @@ class QUICHE_EXPORT_PRIVATE QuicheDataReader { // Empty destructor. ~QuicheDataReader() {} - // Reads an 8/16/32/64-bit unsigned integer into the given output + // Reads an 8/16/24/32/64-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); bool ReadUInt16(uint16_t* result); + bool ReadUInt24(uint32_t* result); bool ReadUInt32(uint32_t* result); bool ReadUInt64(uint64_t* result); diff --git a/chromium/net/third_party/quiche/src/common/quiche_data_reader_test.cc b/chromium/net/third_party/quiche/src/common/quiche_data_reader_test.cc new file mode 100644 index 00000000000..52ed292c936 --- /dev/null +++ b/chromium/net/third_party/quiche/src/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 |