| Index: third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc
|
| diff --git a/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc b/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc
|
| index 2daab194c12be5dc77c91d3cedf865f41d83e264..d1782e39a96e00d7a4e09363d34786e1973b9039 100644
|
| --- a/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc
|
| +++ b/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc
|
| @@ -1,6 +1,6 @@
|
| // Protocol Buffers - Google's data interchange format
|
| // Copyright 2008 Google Inc. All rights reserved.
|
| -// http://code.google.com/p/protobuf/
|
| +// https://developers.google.com/protocol-buffers/
|
| //
|
| // Redistribution and use in source and binary forms, with or without
|
| // modification, are permitted provided that the following conditions are
|
| @@ -34,6 +34,10 @@
|
| //
|
| // 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>
|
| @@ -41,6 +45,8 @@
|
| #include <limits.h>
|
|
|
| #include <google/protobuf/stubs/common.h>
|
| +#include <google/protobuf/stubs/logging.h>
|
| +#include <google/protobuf/stubs/scoped_ptr.h>
|
| #include <google/protobuf/testing/googletest.h>
|
| #include <gtest/gtest.h>
|
| #include <google/protobuf/io/zero_copy_stream_impl.h>
|
| @@ -144,6 +150,7 @@ uint8 CodedStreamTest::buffer_[CodedStreamTest::kBufferSize];
|
| // checks.
|
| const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024};
|
|
|
| +
|
| // -------------------------------------------------------------------
|
| // Varint tests.
|
|
|
| @@ -501,11 +508,11 @@ struct Fixed64Case {
|
| };
|
|
|
| inline std::ostream& operator<<(std::ostream& os, const Fixed32Case& c) {
|
| - return os << "0x" << hex << c.value << dec;
|
| + return os << "0x" << std::hex << c.value << std::dec;
|
| }
|
|
|
| inline std::ostream& operator<<(std::ostream& os, const Fixed64Case& c) {
|
| - return os << "0x" << hex << c.value << dec;
|
| + return os << "0x" << std::hex << c.value << std::dec;
|
| }
|
|
|
| Fixed32Case kFixed32Cases[] = {
|
| @@ -676,20 +683,203 @@ TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnStack) {
|
| }
|
|
|
| TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnHeap) {
|
| - scoped_array<uint8> buffer(new uint8[8]);
|
| + google::protobuf::scoped_array<uint8> buffer(new uint8[8]);
|
| CodedInputStream coded_input(buffer.get(), 8);
|
| string str;
|
| EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
|
| }
|
|
|
| +TEST_1D(CodedStreamTest, ReadStringReservesMemoryOnTotalLimit, kBlockSizes) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.SetTotalBytesLimit(sizeof(kRawBytes), sizeof(kRawBytes));
|
| + EXPECT_EQ(sizeof(kRawBytes), coded_input.BytesUntilTotalBytesLimit());
|
| +
|
| + string str;
|
| + EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
|
| + EXPECT_EQ(sizeof(kRawBytes) - strlen(kRawBytes),
|
| + coded_input.BytesUntilTotalBytesLimit());
|
| + EXPECT_EQ(kRawBytes, str);
|
| + // TODO(liujisi): Replace with a more meaningful test (see cl/60966023).
|
| + EXPECT_GE(str.capacity(), strlen(kRawBytes));
|
| + }
|
| +
|
| + EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
|
| +}
|
| +
|
| +TEST_1D(CodedStreamTest, ReadStringReservesMemoryOnPushedLimit, kBlockSizes) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.PushLimit(sizeof(buffer_));
|
| +
|
| + string str;
|
| + EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
|
| + EXPECT_EQ(kRawBytes, str);
|
| + // TODO(liujisi): Replace with a more meaningful test (see cl/60966023).
|
| + EXPECT_GE(str.capacity(), strlen(kRawBytes));
|
| + }
|
| +
|
| + EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
|
| +}
|
| +
|
| +TEST_F(CodedStreamTest, ReadStringNoReservationIfLimitsNotSet) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + // Buffer size in the input must be smaller than sizeof(kRawBytes),
|
| + // otherwise check against capacity will fail as ReadStringInline()
|
| + // will handle the reading and will reserve the memory as needed.
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), 32);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| +
|
| + string str;
|
| + EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
|
| + EXPECT_EQ(kRawBytes, str);
|
| + // Note: this check depends on string class implementation. It
|
| + // expects that string will allocate more than strlen(kRawBytes)
|
| + // if the content of kRawBytes is appended to string in small
|
| + // chunks.
|
| + // TODO(liujisi): Replace with a more meaningful test (see cl/60966023).
|
| + EXPECT_GE(str.capacity(), strlen(kRawBytes));
|
| + }
|
| +
|
| + EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
|
| +}
|
| +
|
| +TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsNegative) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + // Buffer size in the input must be smaller than sizeof(kRawBytes),
|
| + // otherwise check against capacity will fail as ReadStringInline()
|
| + // will handle the reading and will reserve the memory as needed.
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), 32);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.PushLimit(sizeof(buffer_));
|
| +
|
| + string str;
|
| + EXPECT_FALSE(coded_input.ReadString(&str, -1));
|
| + // Note: this check depends on string class implementation. It
|
| + // expects that string will always allocate the same amount of
|
| + // memory for an empty string.
|
| + EXPECT_EQ(string().capacity(), str.capacity());
|
| + }
|
| +}
|
| +
|
| +TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsLarge) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + // Buffer size in the input must be smaller than sizeof(kRawBytes),
|
| + // otherwise check against capacity will fail as ReadStringInline()
|
| + // will handle the reading and will reserve the memory as needed.
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), 32);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.PushLimit(sizeof(buffer_));
|
| +
|
| + string str;
|
| + EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
|
| + EXPECT_GT(1 << 30, str.capacity());
|
| + }
|
| +}
|
| +
|
| +TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheLimit) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + // Buffer size in the input must be smaller than sizeof(kRawBytes),
|
| + // otherwise check against capacity will fail as ReadStringInline()
|
| + // will handle the reading and will reserve the memory as needed.
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), 32);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.PushLimit(16);
|
| +
|
| + string str;
|
| + EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
|
| + // Note: this check depends on string class implementation. It
|
| + // expects that string will allocate less than strlen(kRawBytes)
|
| + // for an empty string.
|
| + EXPECT_GT(strlen(kRawBytes), str.capacity());
|
| + }
|
| +}
|
| +
|
| +TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheTotalBytesLimit) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + // Buffer size in the input must be smaller than sizeof(kRawBytes),
|
| + // otherwise check against capacity will fail as ReadStringInline()
|
| + // will handle the reading and will reserve the memory as needed.
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), 32);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.SetTotalBytesLimit(16, 16);
|
| +
|
| + string str;
|
| + EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
|
| + // Note: this check depends on string class implementation. It
|
| + // expects that string will allocate less than strlen(kRawBytes)
|
| + // for an empty string.
|
| + EXPECT_GT(strlen(kRawBytes), str.capacity());
|
| + }
|
| +}
|
| +
|
| +TEST_F(CodedStreamTest,
|
| + ReadStringNoReservationSizeIsOverTheClosestLimit_GlobalLimitIsCloser) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + // Buffer size in the input must be smaller than sizeof(kRawBytes),
|
| + // otherwise check against capacity will fail as ReadStringInline()
|
| + // will handle the reading and will reserve the memory as needed.
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), 32);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.PushLimit(sizeof(buffer_));
|
| + coded_input.SetTotalBytesLimit(16, 16);
|
| +
|
| + string str;
|
| + EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
|
| + // Note: this check depends on string class implementation. It
|
| + // expects that string will allocate less than strlen(kRawBytes)
|
| + // for an empty string.
|
| + EXPECT_GT(strlen(kRawBytes), str.capacity());
|
| + }
|
| +}
|
| +
|
| +TEST_F(CodedStreamTest,
|
| + ReadStringNoReservationSizeIsOverTheClosestLimit_LocalLimitIsCloser) {
|
| + memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
|
| + // Buffer size in the input must be smaller than sizeof(kRawBytes),
|
| + // otherwise check against capacity will fail as ReadStringInline()
|
| + // will handle the reading and will reserve the memory as needed.
|
| + ArrayInputStream input(buffer_, sizeof(buffer_), 32);
|
| +
|
| + {
|
| + CodedInputStream coded_input(&input);
|
| + coded_input.PushLimit(16);
|
| + coded_input.SetTotalBytesLimit(sizeof(buffer_), sizeof(buffer_));
|
| + EXPECT_EQ(sizeof(buffer_), coded_input.BytesUntilTotalBytesLimit());
|
| +
|
| + string str;
|
| + EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
|
| + // Note: this check depends on string class implementation. It
|
| + // expects that string will allocate less than strlen(kRawBytes)
|
| + // for an empty string.
|
| + EXPECT_GT(strlen(kRawBytes), str.capacity());
|
| + }
|
| +}
|
| +
|
|
|
| // -------------------------------------------------------------------
|
| // Skip
|
|
|
| const char kSkipTestBytes[] =
|
| "<Before skipping><To be skipped><After skipping>";
|
| -const char kSkipOutputTestBytes[] =
|
| - "-----------------<To be skipped>----------------";
|
|
|
| TEST_1D(CodedStreamTest, SkipInput, kBlockSizes) {
|
| memcpy(buffer_, kSkipTestBytes, sizeof(kSkipTestBytes));
|
| @@ -980,9 +1170,11 @@ TEST_F(CodedStreamTest, TotalBytesLimit) {
|
| ArrayInputStream input(buffer_, sizeof(buffer_));
|
| CodedInputStream coded_input(&input);
|
| coded_input.SetTotalBytesLimit(16, -1);
|
| + EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
|
|
|
| string str;
|
| EXPECT_TRUE(coded_input.ReadString(&str, 16));
|
| + EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
|
|
|
| vector<string> errors;
|
|
|
| @@ -997,7 +1189,9 @@ TEST_F(CodedStreamTest, TotalBytesLimit) {
|
| "A protocol message was rejected because it was too big", errors[0]);
|
|
|
| coded_input.SetTotalBytesLimit(32, -1);
|
| + EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
|
| EXPECT_TRUE(coded_input.ReadString(&str, 16));
|
| + EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
|
| }
|
|
|
| TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) {
|
|
|