// Copyright (c) 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/common/quiche_data_writer.h" #include <cstdint> #include <cstring> #include <string> #include <vector> #include "absl/base/macros.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "quiche/common/platform/api/quiche_test.h" #include "quiche/common/quiche_data_reader.h" #include "quiche/common/quiche_endian.h" #include "quiche/common/test_tools/quiche_test_utils.h" namespace quiche { namespace test { namespace { char* AsChars(unsigned char* data) { … } struct TestParams { … }; // Used by ::testing::PrintToStringParamName(). std::string PrintToString(const TestParams& p) { … } std::vector<TestParams> GetTestParams() { … } class QuicheDataWriterTest : public QuicheTestWithParam<TestParams> { … }; INSTANTIATE_TEST_SUITE_P(…); TEST_P(QuicheDataWriterTest, Write16BitUnsignedIntegers) { … } TEST_P(QuicheDataWriterTest, Write24BitUnsignedIntegers) { … } TEST_P(QuicheDataWriterTest, Write32BitUnsignedIntegers) { … } TEST_P(QuicheDataWriterTest, Write40BitUnsignedIntegers) { … } TEST_P(QuicheDataWriterTest, Write48BitUnsignedIntegers) { … } TEST_P(QuicheDataWriterTest, Write56BitUnsignedIntegers) { … } TEST_P(QuicheDataWriterTest, Write64BitUnsignedIntegers) { … } TEST_P(QuicheDataWriterTest, WriteIntegers) { … } TEST_P(QuicheDataWriterTest, WriteBytes) { … } const int kVarIntBufferLength = …; // Encodes and then decodes a specified value, checks that the // value that was encoded is the same as the decoded value, the length // is correct, and that after decoding, all data in the buffer has // been consumed.. // Returns true if everything works, false if not. bool EncodeDecodeValue(uint64_t value_in, char* buffer, size_t size_of_buffer) { … } // Test that 8-byte-encoded Variable Length Integers are properly laid // out in the buffer. TEST_P(QuicheDataWriterTest, VarInt8Layout) { … } // Test that 4-byte-encoded Variable Length Integers are properly laid // out in the buffer. TEST_P(QuicheDataWriterTest, VarInt4Layout) { … } // Test that 2-byte-encoded Variable Length Integers are properly laid // out in the buffer. TEST_P(QuicheDataWriterTest, VarInt2Layout) { … } // Test that 1-byte-encoded Variable Length Integers are properly laid // out in the buffer. TEST_P(QuicheDataWriterTest, VarInt1Layout) { … } // Test certain, targeted, values that are expected to succeed: // 0, 1, // 0x3e, 0x3f, 0x40, 0x41 (around the 1-2 byte transitions) // 0x3ffe, 0x3fff, 0x4000, 0x4001 (the 2-4 byte transition) // 0x3ffffffe, 0x3fffffff, 0x40000000, 0x40000001 (the 4-8 byte // transition) // 0x3ffffffffffffffe, 0x3fffffffffffffff, (the highest valid values) // 0xfe, 0xff, 0x100, 0x101, // 0xfffe, 0xffff, 0x10000, 0x10001, // 0xfffffe, 0xffffff, 0x1000000, 0x1000001, // 0xfffffffe, 0xffffffff, 0x100000000, 0x100000001, // 0xfffffffffe, 0xffffffffff, 0x10000000000, 0x10000000001, // 0xfffffffffffe, 0xffffffffffff, 0x1000000000000, 0x1000000000001, // 0xfffffffffffffe, 0xffffffffffffff, 0x100000000000000, 0x100000000000001, TEST_P(QuicheDataWriterTest, VarIntGoodTargetedValues) { … } // // Test certain, targeted, values where failure is expected (the // values are invalid w.r.t. IETF VarInt encoding): // 0x4000000000000000, 0x4000000000000001, ( Just above max allowed value) // 0xfffffffffffffffe, 0xffffffffffffffff, (should fail) TEST_P(QuicheDataWriterTest, VarIntBadTargetedValues) { … } // Test writing varints with a forced length. TEST_P(QuicheDataWriterTest, WriteVarInt62WithForcedLength) { … } // Following tests all try to fill the buffer with multiple values, // go one value more than the buffer can accommodate, then read // the successfully encoded values, and try to read the unsuccessfully // encoded value. The following is the number of values to encode. const int kMultiVarCount = …; // Test writing & reading multiple 8-byte-encoded varints TEST_P(QuicheDataWriterTest, MultiVarInt8) { … } // Test writing & reading multiple 4-byte-encoded varints TEST_P(QuicheDataWriterTest, MultiVarInt4) { … } // Test writing & reading multiple 2-byte-encoded varints TEST_P(QuicheDataWriterTest, MultiVarInt2) { … } // Test writing & reading multiple 1-byte-encoded varints TEST_P(QuicheDataWriterTest, MultiVarInt1) { … } TEST_P(QuicheDataWriterTest, Seek) { … } TEST_P(QuicheDataWriterTest, SeekTooFarFails) { … } TEST_P(QuicheDataWriterTest, PayloadReads) { … } } // namespace } // namespace test } // namespace quiche