diff options
Diffstat (limited to 'src/google/protobuf/io/coded_stream_unittest.cc')
-rw-r--r-- | src/google/protobuf/io/coded_stream_unittest.cc | 109 |
1 files changed, 62 insertions, 47 deletions
diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc index a8108e45..1c8d3272 100644 --- a/src/google/protobuf/io/coded_stream_unittest.cc +++ b/src/google/protobuf/io/coded_stream_unittest.cc @@ -35,9 +35,6 @@ // This file contains tests and benchmarks. #include <memory> -#ifndef _SHARED_PTR_H -#include <google/protobuf/stubs/shared_ptr.h> -#endif #include <vector> #include <google/protobuf/io/coded_stream.h> @@ -58,10 +55,12 @@ #define ULL(x) GOOGLE_ULONGLONG(x) namespace google { + namespace protobuf { namespace io { namespace { + // =================================================================== // Data-Driven Test Infrastructure @@ -134,7 +133,7 @@ class CodedStreamTest : public testing::Test { // for further information. static void SetupTotalBytesLimitWarningTest( int total_bytes_limit, int warning_threshold, - vector<string>* out_errors, vector<string>* out_warnings); + std::vector<string>* out_errors, std::vector<string>* out_warnings); // Buffer used during most of the tests. This assumes tests run sequentially. static const int kBufferSize = 1024 * 64; @@ -244,7 +243,7 @@ TEST_F(CodedStreamTest, EmptyInputBeforeEos) { int count_; } in; CodedInputStream input(&in); - input.ReadTag(); + input.ReadTagNoLastTag(); EXPECT_TRUE(input.ConsumedEntireMessage()); } @@ -445,6 +444,21 @@ TEST_2D(CodedStreamTest, ReadVarint32Error, kVarintErrorCases, kBlockSizes) { EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value)); } +TEST_2D(CodedStreamTest, ReadVarint32Error_LeavesValueInInitializedState, + kVarintErrorCases, kBlockSizes) { + memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size); + ArrayInputStream input(buffer_, kVarintErrorCases_case.size, + kBlockSizes_case); + CodedInputStream coded_input(&input); + + uint32 value = 0; + EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value)); + // While the specific value following a failure is not critical, we do want to + // ensure that it doesn't get set to an uninitialized value. (This check fails + // in MSAN mode if value has been set to an uninitialized value.) + EXPECT_EQ(value, value); +} + TEST_2D(CodedStreamTest, ReadVarint64Error, kVarintErrorCases, kBlockSizes) { memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size); ArrayInputStream input(buffer_, kVarintErrorCases_case.size, @@ -455,6 +469,21 @@ TEST_2D(CodedStreamTest, ReadVarint64Error, kVarintErrorCases, kBlockSizes) { EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value)); } +TEST_2D(CodedStreamTest, ReadVarint64Error_LeavesValueInInitializedState, + kVarintErrorCases, kBlockSizes) { + memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size); + ArrayInputStream input(buffer_, kVarintErrorCases_case.size, + kBlockSizes_case); + CodedInputStream coded_input(&input); + + uint64 value = 0; + EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value)); + // While the specific value following a failure is not critical, we do want to + // ensure that it doesn't get set to an uninitialized value. (This check fails + // in MSAN mode if value has been set to an uninitialized value.) + EXPECT_EQ(value, value); +} + // ------------------------------------------------------------------- // VarintSize @@ -494,6 +523,28 @@ TEST_1D(CodedStreamTest, VarintSize64, kVarintSizeCases) { CodedOutputStream::VarintSize64(kVarintSizeCases_case.value)); } +TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) { + int expected = 1; + for (int i = 1; i < 32; i++) { + if (i % 7 == 0) { + expected += 1; + } + EXPECT_EQ(expected, + CodedOutputStream::VarintSize32(static_cast<uint32>(0x1u << i))); + } +} + +TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) { + int expected = 1; + for (int i = 1; i < 64; i++) { + if (i % 7 == 0) { + expected += 1; + } + EXPECT_EQ(expected, CodedOutputStream::VarintSize64( + static_cast<uint64>(0x1ull << i))); + } +} + // ------------------------------------------------------------------- // Fixed-size int tests @@ -683,7 +734,7 @@ TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnStack) { } TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnHeap) { - google::protobuf::scoped_array<uint8> buffer(new uint8[8]); + std::unique_ptr<uint8[]> buffer(new uint8[8]); CodedInputStream coded_input(buffer.get(), 8); string str; EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30)); @@ -1176,7 +1227,7 @@ TEST_F(CodedStreamTest, TotalBytesLimit) { EXPECT_TRUE(coded_input.ReadString(&str, 16)); EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit()); - vector<string> errors; + std::vector<string> errors; { ScopedMemoryLog error_log; @@ -1210,7 +1261,7 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { // Read a tag. Should fail, but report being a valid endpoint since it's // a regular limit. - EXPECT_EQ(0, coded_input.ReadTag()); + EXPECT_EQ(0, coded_input.ReadTagNoLastTag()); EXPECT_TRUE(coded_input.ConsumedEntireMessage()); // Pop the limit. @@ -1218,7 +1269,7 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { // Read a tag. Should fail, and report *not* being a valid endpoint, since // this time we're hitting the total bytes limit. - EXPECT_EQ(0, coded_input.ReadTag()); + EXPECT_EQ(0, coded_input.ReadTagNoLastTag()); EXPECT_FALSE(coded_input.ConsumedEntireMessage()); } @@ -1228,7 +1279,7 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { // vectors. void CodedStreamTest::SetupTotalBytesLimitWarningTest( int total_bytes_limit, int warning_threshold, - vector<string>* out_errors, vector<string>* out_warnings) { + std::vector<string>* out_errors, std::vector<string>* out_warnings) { ArrayInputStream raw_input(buffer_, sizeof(buffer_), 128); ScopedMemoryLog scoped_log; @@ -1243,39 +1294,6 @@ void CodedStreamTest::SetupTotalBytesLimitWarningTest( *out_warnings = scoped_log.GetMessages(WARNING); } -TEST_F(CodedStreamTest, TotalBytesLimitWarning) { - vector<string> errors; - vector<string> warnings; - SetupTotalBytesLimitWarningTest(10240, 1024, &errors, &warnings); - - EXPECT_EQ(0, errors.size()); - - ASSERT_EQ(2, warnings.size()); - EXPECT_PRED_FORMAT2(testing::IsSubstring, - "Reading dangerously large protocol message. If the message turns out to " - "be larger than 10240 bytes, parsing will be halted for security reasons.", - warnings[0]); - EXPECT_PRED_FORMAT2(testing::IsSubstring, - "The total number of bytes read was 2048", - warnings[1]); -} - -TEST_F(CodedStreamTest, TotalBytesLimitWarningDisabled) { - vector<string> errors; - vector<string> warnings; - - // Test with -1 - SetupTotalBytesLimitWarningTest(10240, -1, &errors, &warnings); - EXPECT_EQ(0, errors.size()); - EXPECT_EQ(0, warnings.size()); - - // Test again with -2, expecting the same result - SetupTotalBytesLimitWarningTest(10240, -2, &errors, &warnings); - EXPECT_EQ(0, errors.size()); - EXPECT_EQ(0, warnings.size()); -} - - TEST_F(CodedStreamTest, RecursionLimit) { ArrayInputStream input(buffer_, sizeof(buffer_)); CodedInputStream coded_input(&input); @@ -1361,7 +1379,7 @@ TEST_F(CodedStreamTest, InputOver2G) { // input.BackUp() with the correct number of bytes on destruction. ReallyBigInputStream input; - vector<string> errors; + std::vector<string> errors; { ScopedMemoryLog error_log; @@ -1376,9 +1394,6 @@ TEST_F(CodedStreamTest, InputOver2G) { EXPECT_EQ(0, errors.size()); } -// =================================================================== - - } // namespace } // namespace io } // namespace protobuf |