diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-08-24 12:15:48 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-08-28 13:30:04 +0000 |
commit | b014812705fc80bff0a5c120dfcef88f349816dc (patch) | |
tree | 25a2e2d9fa285f1add86aa333389a839f81a39ae /chromium/components/cbor | |
parent | 9f4560b1027ae06fdb497023cdcaf91b8511fa74 (diff) | |
download | qtwebengine-chromium-b014812705fc80bff0a5c120dfcef88f349816dc.tar.gz |
BASELINE: Update Chromium to 68.0.3440.125
Change-Id: I23f19369e01f688e496f5bf179abb521ad73874f
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/components/cbor')
-rw-r--r-- | chromium/components/cbor/cbor_reader.cc | 20 | ||||
-rw-r--r-- | chromium/components/cbor/cbor_reader.h | 46 | ||||
-rw-r--r-- | chromium/components/cbor/cbor_reader_unittest.cc | 100 |
3 files changed, 30 insertions, 136 deletions
diff --git a/chromium/components/cbor/cbor_reader.cc b/chromium/components/cbor/cbor_reader.cc index c08bbd6a5b8..5abab730783 100644 --- a/chromium/components/cbor/cbor_reader.cc +++ b/chromium/components/cbor/cbor_reader.cc @@ -102,26 +102,6 @@ base::Optional<CBORValue> CBORReader::Read(base::span<uint8_t const> data, return decoded_cbor; } -// static -base::Optional<CBORReader::DataItemHeader> CBORReader::ReadDataItemHeader( - base::span<const uint8_t> data, - size_t* num_bytes_consumed, - DecoderError* error_code_out) { - CBORReader reader(data.cbegin(), data.cend()); - base::Optional<DataItemHeader> decoded_header = reader.DecodeDataItemHeader(); - - if (error_code_out) - *error_code_out = reader.GetErrorCode(); - - if (reader.GetErrorCode() != DecoderError::CBOR_NO_ERROR) { - *num_bytes_consumed = 0; - return base::nullopt; - } - - *num_bytes_consumed = reader.num_bytes_consumed(); - return decoded_header; -} - base::Optional<CBORValue> CBORReader::DecodeCompleteDataItem( int max_nesting_level) { if (max_nesting_level < 0 || max_nesting_level > kCBORMaxDepth) { diff --git a/chromium/components/cbor/cbor_reader.h b/chromium/components/cbor/cbor_reader.h index 65b93d5bb68..9565e1e5f41 100644 --- a/chromium/components/cbor/cbor_reader.h +++ b/chromium/components/cbor/cbor_reader.h @@ -69,25 +69,6 @@ class CBOR_EXPORT CBORReader { UNKNOWN_ERROR, }; - // Encapsulates information extracted from the header of a CBOR data item, - // which consists of the initial byte, and a variable-length-encoded integer - // (if any). - // - // TODO(crbug.com/811717): This is an CBORReader internal detail which should - // not be exposed outside. We should switch to an event-based interface - // before adding another customer. - struct CBOR_EXPORT DataItemHeader { - // The major type decoded from the initial byte. - CBORValue::Type type; - - // The raw 5-bit additional information from the initial byte. - uint8_t additional_info; - - // The integer |value| decoded from the |additional_info| and the - // variable-length-encoded integer, if any. - uint64_t value; - }; - // CBOR nested depth sufficient for most use cases. static const int kCBORMaxDepth = 16; @@ -112,23 +93,28 @@ class CBOR_EXPORT CBORReader { DecoderError* error_code_out = nullptr, int max_nesting_level = kCBORMaxDepth); - // Reads and parses the header of CBOR data item from |input_data|. Optional - // |error_code_out| can be provided by the caller to obtain additional - // information about decoding failures, which is always available if an empty - // value is returned. Never fails with EXTRANEOUS_DATA, but informs the - // caller of how many bytes were consumed through |num_bytes_consumed|, which - // is set to zero on error. - static base::Optional<DataItemHeader> ReadDataItemHeader( - base::span<const uint8_t> input_data, - size_t* num_bytes_consumed = nullptr, - DecoderError* error_code_out = nullptr); - // Translates errors to human-readable error messages. static const char* ErrorCodeToString(DecoderError error_code); private: CBORReader(base::span<const uint8_t>::const_iterator it, const base::span<const uint8_t>::const_iterator end); + + // Encapsulates information extracted from the header of a CBOR data item, + // which consists of the initial byte, and a variable-length-encoded integer + // (if any). + struct DataItemHeader { + // The major type decoded from the initial byte. + CBORValue::Type type; + + // The raw 5-bit additional information from the initial byte. + uint8_t additional_info; + + // The integer |value| decoded from the |additional_info| and the + // variable-length-encoded integer, if any. + uint64_t value; + }; + base::Optional<DataItemHeader> DecodeDataItemHeader(); base::Optional<CBORValue> DecodeCompleteDataItem(int max_nesting_level); base::Optional<CBORValue> DecodeValueToNegative(uint64_t value); diff --git a/chromium/components/cbor/cbor_reader_unittest.cc b/chromium/components/cbor/cbor_reader_unittest.cc index 419cd91b96e..902b9b94030 100644 --- a/chromium/components/cbor/cbor_reader_unittest.cc +++ b/chromium/components/cbor/cbor_reader_unittest.cc @@ -27,92 +27,6 @@ std::vector<uint8_t> WithExtraneousData(base::span<const uint8_t> original) { } // namespace -TEST(CBORReaderTest, TestDecodeDataItemHeader) { - static const struct { - CBORReader::DataItemHeader expected_header; - const std::vector<uint8_t> cbor_data; - } kTestCases[] = { - {{CBORValue::Type::UNSIGNED, 0, 0}, {0x00}}, - {{CBORValue::Type::UNSIGNED, 24, 24}, {0x18, 0x18}}, - {{CBORValue::Type::UNSIGNED, 25, 12345}, {0x19, 0x30, 0x39}}, - {{CBORValue::Type::UNSIGNED, 27, 1234567890123456789ull}, - {0x1B, 0x11, 0x22, 0x10, 0xF4, 0x7D, 0xE9, 0x81, 0x15}}, - {{CBORValue::Type::NEGATIVE, 24, 255}, {0x38, 0xff}}, - {{CBORValue::Type::NEGATIVE, 26, 12345677}, - {0x3a, 0x00, 0xbc, 0x61, 0x4d}}, - {{CBORValue::Type::BYTE_STRING, 4, 4}, {0x44}}, - {{CBORValue::Type::STRING, 3, 3}, {0x63}}, - {{CBORValue::Type::ARRAY, 24, 25}, {0x98, 0x19}}, - {{CBORValue::Type::MAP, 4, 4}, {0xa4}}, - {{CBORValue::Type::SIMPLE_VALUE, - static_cast<uint8_t>(CBORValue::SimpleValue::FALSE_VALUE), 20}, - {0xf4}}, - }; - - int test_element_index = 0; - for (const auto& test_case : kTestCases) { - SCOPED_TRACE(testing::Message() << "testing case " << test_element_index++); - size_t consumed_bytes; - CBORReader::DecoderError error_code; - base::Optional<CBORReader::DataItemHeader> header = - CBORReader::ReadDataItemHeader(test_case.cbor_data, &consumed_bytes, - &error_code); - - ASSERT_TRUE(header.has_value()); - EXPECT_EQ(header->type, test_case.expected_header.type); - EXPECT_EQ(header->additional_info, - test_case.expected_header.additional_info); - EXPECT_EQ(header->value, test_case.expected_header.value); - EXPECT_EQ(consumed_bytes, test_case.cbor_data.size()); - EXPECT_EQ(error_code, CBORReader::DecoderError::CBOR_NO_ERROR); - - auto cbor_data_with_extra_byte = WithExtraneousData(test_case.cbor_data); - header = CBORReader::ReadDataItemHeader(cbor_data_with_extra_byte, - &consumed_bytes, &error_code); - ASSERT_TRUE(header.has_value()); - EXPECT_EQ(header->type, test_case.expected_header.type); - EXPECT_EQ(header->additional_info, - test_case.expected_header.additional_info); - EXPECT_EQ(header->value, test_case.expected_header.value); - EXPECT_EQ(consumed_bytes, test_case.cbor_data.size()); - EXPECT_EQ(error_code, CBORReader::DecoderError::CBOR_NO_ERROR); - } -} - -TEST(CBORReaderTest, TestDecodeIncompleteDataItemHeader) { - static const std::vector<uint8_t> kTestCases[] = { - // clang-format off - {0x18}, // unsigned with pending 1 byte of numeric value. - {0x99}, // array with pending 2 byte of numeric value (length). - {0xba}, // map with pending 4 byte of numeric value (length). - {0x5b}, // byte string with pending 4 byte of numeric value (length). - {0x3b}, // negative integer with pending 8 byte of numeric value. - {0x99, 0x01}, // array with pending 2 byte of numeric value (length), - // with only 1 byte of additional data. - {0xba, 0x01, 0x02, 0x03}, // map with pending 4 byte of numeric value - // (length), with only 3 bytes of additional - // data. - {0x3b, 0x01, 0x02, 0x03, - 0x04, 0x05, 0x06, 0x07}, // negative integer with pending 8 byte of - // numeric value, with only 7 bytes of - // additional data. - // clang-format on - }; - - int test_element_index = 0; - for (const auto& test_case : kTestCases) { - SCOPED_TRACE(testing::Message() << "testing case " << test_element_index++); - size_t consumed_bytes; - CBORReader::DecoderError error_code; - base::Optional<CBORReader::DataItemHeader> header = - CBORReader::ReadDataItemHeader(test_case, &consumed_bytes, &error_code); - - ASSERT_FALSE(header.has_value()); - EXPECT_EQ(consumed_bytes, 0u); - EXPECT_EQ(error_code, CBORReader::DecoderError::INCOMPLETE_CBOR_DATA); - } -} - TEST(CBORReaderTest, TestReadUint) { struct UintTestCase { const int64_t value; @@ -997,6 +911,20 @@ TEST(CBORReaderTest, TestIncompleteCBORDataError) { // CBOR map with single key value pair encoded with additional info of // length 2. {0xa2, 0x61, 0x61, 0x01}, + {0x18}, // unsigned with pending 1 byte of numeric value. + {0x99}, // array with pending 2 byte of numeric value (length). + {0xba}, // map with pending 4 byte of numeric value (length). + {0x5b}, // byte string with pending 4 byte of numeric value (length). + {0x3b}, // negative integer with pending 8 byte of numeric value. + {0x99, 0x01}, // array with pending 2 byte of numeric value (length), + // with only 1 byte of additional data. + {0xba, 0x01, 0x02, 0x03}, // map with pending 4 byte of numeric value + // (length), with only 3 bytes of additional + // data. + {0x3b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, + 0x07}, // negative integer with pending 8 byte of + // numeric value, with only 7 bytes of + // additional data. }; int test_element_index = 0; |