diff options
author | Adam Cozzette <acozzette@google.com> | 2016-11-17 16:48:38 -0800 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2016-11-17 16:59:59 -0800 |
commit | 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 (patch) | |
tree | 0276f81f8848a05d84cd7e287b43d665e30f04e3 /src/google/protobuf/io/coded_stream_unittest.cc | |
parent | e28286fa05d8327fd6c5aa70cfb3be558f0932b8 (diff) |
Integrated internal changes from Google
Diffstat (limited to 'src/google/protobuf/io/coded_stream_unittest.cc')
-rw-r--r-- | src/google/protobuf/io/coded_stream_unittest.cc | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc index 9f1ebc3f..31574d5b 100644 --- a/src/google/protobuf/io/coded_stream_unittest.cc +++ b/src/google/protobuf/io/coded_stream_unittest.cc @@ -135,7 +135,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; @@ -245,7 +245,7 @@ TEST_F(CodedStreamTest, EmptyInputBeforeEos) { int count_; } in; CodedInputStream input(&in); - input.ReadTag(); + input.ReadTagNoLastTag(); EXPECT_TRUE(input.ConsumedEntireMessage()); } @@ -446,6 +446,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, @@ -456,6 +471,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 @@ -495,6 +525,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 @@ -1177,7 +1229,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; @@ -1211,7 +1263,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. @@ -1219,7 +1271,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()); } @@ -1229,7 +1281,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; @@ -1245,25 +1297,21 @@ void CodedStreamTest::SetupTotalBytesLimitWarningTest( } TEST_F(CodedStreamTest, TotalBytesLimitWarning) { - vector<string> errors; - vector<string> warnings; + std::vector<string> errors; + std::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_EQ(1, warnings.size()); EXPECT_PRED_FORMAT2(testing::IsSubstring, "The total number of bytes read was 2048", - warnings[1]); + warnings[0]); } TEST_F(CodedStreamTest, TotalBytesLimitWarningDisabled) { - vector<string> errors; - vector<string> warnings; + std::vector<string> errors; + std::vector<string> warnings; // Test with -1 SetupTotalBytesLimitWarningTest(10240, -1, &errors, &warnings); @@ -1362,7 +1410,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; |