37 #include <google/protobuf/io/coded_stream.h>
44 #include <google/protobuf/stubs/common.h>
45 #include <google/protobuf/stubs/logging.h>
46 #include <google/protobuf/stubs/logging.h>
47 #include <google/protobuf/io/zero_copy_stream_impl.h>
48 #include <google/protobuf/testing/googletest.h>
49 #include <gtest/gtest.h>
50 #include <google/protobuf/stubs/casts.h>
52 #include <google/protobuf/port_def.inc>
84 #define TEST_1D(FIXTURE, NAME, CASES) \
85 class FIXTURE##_##NAME##_DD : public FIXTURE { \
87 template <typename CaseType> \
88 void DoSingleCase(const CaseType& CASES##_case); \
91 TEST_F(FIXTURE##_##NAME##_DD, NAME) { \
92 for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \
93 SCOPED_TRACE(testing::Message() \
94 << #CASES " case #" << i << ": " << CASES[i]); \
95 DoSingleCase(CASES[i]); \
99 template <typename CaseType> \
100 void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType& CASES##_case)
102 #define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \
103 class FIXTURE##_##NAME##_DD : public FIXTURE { \
105 template <typename CaseType1, typename CaseType2> \
106 void DoSingleCase(const CaseType1& CASES1##_case, \
107 const CaseType2& CASES2##_case); \
110 TEST_F(FIXTURE##_##NAME##_DD, NAME) { \
111 for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \
112 for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \
113 SCOPED_TRACE(testing::Message() \
114 << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \
115 << #CASES2 " case #" << j << ": " << CASES2[j]); \
116 DoSingleCase(CASES1[i], CASES2[j]); \
121 template <typename CaseType1, typename CaseType2> \
122 void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \
123 const CaseType2& CASES2##_case)
141 const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024};
153 inline std::ostream&
operator<<(std::ostream& os,
const VarintCase& c) {
154 return os <<
c.value;
157 VarintCase kVarintCases[] = {
162 {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)},
163 {{0xbe, 0xf7, 0x92, 0x84, 0x0b},
165 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
169 {{0xbe, 0xf7, 0x92, 0x84, 0x1b},
171 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
173 {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49},
175 (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
179 {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01},
181 (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
188 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
203 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
209 uint32 expected_value =
static_cast<uint32>(kVarintCases_case.value);
210 EXPECT_EQ(expected_value, coded_input.ReadTag());
212 EXPECT_TRUE(coded_input.LastTagWas(expected_value));
213 EXPECT_FALSE(coded_input.LastTagWas(expected_value + 1));
221 TEST_F(CodedStreamTest, EmptyInputBeforeEos) {
237 int64_t ByteCount()
const override {
return 0; }
241 input.ReadTagNoLastTag();
249 memcpy(
buffer_ + 1, kVarintCases_case.bytes, kVarintCases_case.size);
258 coded_input.ReadRaw(&
dummy, 1);
261 uint32 expected_value =
static_cast<uint32>(kVarintCases_case.value);
264 if (kVarintCases_case.size <= 2) {
265 EXPECT_FALSE(coded_input.ExpectTag(expected_value + 1));
266 EXPECT_TRUE(coded_input.ExpectTag(expected_value));
272 if (kVarintCases_case.size <= 2) {
279 TEST_1D(CodedStreamTest, ExpectTagFromArray, kVarintCases) {
280 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
282 const uint32 expected_value =
static_cast<uint32>(kVarintCases_case.value);
285 if (kVarintCases_case.size <= 2) {
297 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
312 if (kVarintCases_case.value >
uint64_t{0x00000000FFFFFFFFu}) {
322 coded_output.WriteVarint32(
static_cast<uint32>(kVarintCases_case.value));
325 EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
330 memcmp(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
339 coded_output.WriteVarint64(kVarintCases_case.value);
342 EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
347 memcmp(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
352 #if !defined(__GNUC__) || __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
354 int32 kSignExtendedVarintCases[] = {0, 1, -1, 1237894, -37895138};
356 TEST_2D(CodedStreamTest, WriteVarint32SignExtended, kSignExtendedVarintCases,
363 coded_output.WriteVarint32SignExtended(kSignExtendedVarintCases_case);
366 if (kSignExtendedVarintCases_case < 0) {
373 if (kSignExtendedVarintCases_case < 0) {
400 struct VarintErrorCase {
406 inline std::ostream&
operator<<(std::ostream& os,
const VarintErrorCase& c) {
407 return os <<
"size " <<
c.size;
410 const VarintErrorCase kVarintErrorCases[] = {
419 {{0xf0, 0xab}, 2,
false},
422 {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2}, 6,
false},
425 {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01},
431 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
432 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
437 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&
value));
440 TEST_2D(CodedStreamTest, ReadVarint32Error_LeavesValueInInitializedState,
442 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
443 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
448 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&
value));
456 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
457 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
462 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&
value));
465 TEST_2D(CodedStreamTest, ReadVarint64Error_LeavesValueInInitializedState,
467 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
468 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
473 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&
value));
483 struct VarintSizeCase {
488 inline std::ostream&
operator<<(std::ostream& os,
const VarintSizeCase& c) {
489 return os <<
c.value;
492 VarintSizeCase kVarintSizeCases[] = {
503 TEST_1D(CodedStreamTest, VarintSize32, kVarintSizeCases) {
504 if (kVarintSizeCases_case.value > 0xffffffffu) {
511 static_cast<uint32>(kVarintSizeCases_case.value)));
519 TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) {
521 for (
int i = 1;
i < 32;
i++) {
530 TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) {
532 for (
int i = 1;
i < 64;
i++) {
537 static_cast<uint64>(0x1ull << i)));
554 inline std::ostream&
operator<<(std::ostream& os,
const Fixed32Case& c) {
555 return os <<
"0x" << std::hex <<
c.value << std::dec;
558 inline std::ostream&
operator<<(std::ostream& os,
const Fixed64Case& c) {
559 return os <<
"0x" << std::hex <<
c.value << std::dec;
562 Fixed32Case kFixed32Cases[] = {
563 {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdef
u},
564 {{0x12, 0x34, 0x56, 0x78}, 0x78563412
u},
567 Fixed64Case kFixed64Cases[] = {
568 {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78},
570 {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
575 memcpy(
buffer_, kFixed32Cases_case.bytes,
sizeof(kFixed32Cases_case.bytes));
590 memcpy(
buffer_, kFixed64Cases_case.bytes,
sizeof(kFixed64Cases_case.bytes));
610 coded_output.WriteLittleEndian32(kFixed32Cases_case.value);
626 coded_output.WriteLittleEndian64(kFixed64Cases_case.value);
638 TEST_1D(CodedStreamTest, ReadLittleEndian32FromArray, kFixed32Cases) {
639 memcpy(
buffer_, kFixed32Cases_case.bytes,
sizeof(kFixed32Cases_case.bytes));
648 TEST_1D(CodedStreamTest, ReadLittleEndian64FromArray, kFixed64Cases) {
649 memcpy(
buffer_, kFixed64Cases_case.bytes,
sizeof(kFixed64Cases_case.bytes));
661 const char kRawBytes[] =
"Some bytes which will be written and read raw.";
671 EXPECT_TRUE(coded_input.ReadRaw(read_buffer,
sizeof(kRawBytes)));
672 EXPECT_EQ(0, memcmp(kRawBytes, read_buffer,
sizeof(kRawBytes)));
684 coded_output.WriteRaw(kRawBytes,
sizeof(kRawBytes));
687 EXPECT_EQ(
sizeof(kRawBytes), coded_output.ByteCount());
722 TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnStack) {
731 TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnHeap) {
744 coded_input.SetTotalBytesLimit(
sizeof(kRawBytes));
745 EXPECT_EQ(
sizeof(kRawBytes), coded_input.BytesUntilTotalBytesLimit());
749 EXPECT_EQ(
sizeof(kRawBytes) - strlen(kRawBytes),
750 coded_input.BytesUntilTotalBytesLimit());
765 coded_input.PushLimit(
sizeof(
buffer_));
777 TEST_F(CodedStreamTest, ReadStringNoReservationIfLimitsNotSet) {
801 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsNegative) {
810 coded_input.PushLimit(
sizeof(
buffer_));
821 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsLarge) {
830 coded_input.PushLimit(
sizeof(
buffer_));
838 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheLimit) {
847 coded_input.PushLimit(16);
858 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheTotalBytesLimit) {
867 coded_input.SetTotalBytesLimit(16);
879 ReadStringNoReservationSizeIsOverTheClosestLimit_GlobalLimitIsCloser) {
888 coded_input.PushLimit(
sizeof(
buffer_));
889 coded_input.SetTotalBytesLimit(16);
901 ReadStringNoReservationSizeIsOverTheClosestLimit_LocalLimitIsCloser) {
910 coded_input.PushLimit(16);
911 coded_input.SetTotalBytesLimit(
sizeof(
buffer_));
927 const char kSkipTestBytes[] =
928 "<Before skipping><To be skipped><After skipping>";
938 EXPECT_TRUE(coded_input.ReadString(&
str, strlen(
"<Before skipping>")));
940 EXPECT_TRUE(coded_input.Skip(strlen(
"<To be skipped>")));
941 EXPECT_TRUE(coded_input.ReadString(&
str, strlen(
"<After skipping>")));
951 TEST_F(CodedStreamTest, GetDirectBufferPointerInput) {
980 TEST_F(CodedStreamTest, GetDirectBufferPointerInlineInput) {
987 coded_input.GetDirectBufferPointerInline(&
ptr, &
size);
992 coded_input.GetDirectBufferPointerInline(&
ptr, &
size);
998 coded_input.GetDirectBufferPointerInline(&
ptr, &
size);
1005 coded_input.GetDirectBufferPointerInline(&
ptr, &
size);
1019 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1024 EXPECT_EQ(8, coded_input.BytesUntilLimit());
1026 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1028 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1030 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1032 coded_input.PopLimit(limit);
1034 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1049 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1051 EXPECT_EQ(8, coded_input.BytesUntilLimit());
1057 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1059 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1061 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1063 coded_input.PopLimit(limit2);
1066 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1068 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1070 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1072 coded_input.PopLimit(limit1);
1075 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1091 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1093 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1101 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1103 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1105 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1107 coded_input.PopLimit(limit2);
1110 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1112 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1114 coded_input.PopLimit(limit1);
1117 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1124 TEST_F(CodedStreamTest, ExpectAtEnd) {
1137 coded_input.PopLimit(limit);
1141 TEST_F(CodedStreamTest, NegativeLimit) {
1149 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1150 coded_input.PopLimit(limit);
1153 TEST_F(CodedStreamTest, NegativeLimitAfterReading) {
1162 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1163 coded_input.PopLimit(limit);
1166 TEST_F(CodedStreamTest, OverflowLimit) {
1176 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1177 coded_input.PopLimit(limit);
1180 TEST_F(CodedStreamTest, TotalBytesLimit) {
1183 coded_input.SetTotalBytesLimit(16);
1184 EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
1188 EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
1190 std::vector<std::string>
errors;
1193 ScopedMemoryLog error_log;
1200 "A protocol message was rejected because it was too big",
1203 coded_input.SetTotalBytesLimit(32);
1204 EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
1206 EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
1209 TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) {
1216 coded_input.SetTotalBytesLimit(16);
1225 EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
1229 coded_input.PopLimit(limit);
1233 EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
1237 TEST_F(CodedStreamTest, RecursionLimit) {
1240 coded_input.SetRecursionLimit(4);
1243 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1244 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1245 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1246 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1249 coded_input.DecrementRecursionDepth();
1251 coded_input.DecrementRecursionDepth();
1252 coded_input.DecrementRecursionDepth();
1253 coded_input.DecrementRecursionDepth();
1254 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1256 coded_input.DecrementRecursionDepth();
1257 coded_input.DecrementRecursionDepth();
1258 coded_input.DecrementRecursionDepth();
1259 coded_input.DecrementRecursionDepth();
1260 coded_input.DecrementRecursionDepth();
1261 coded_input.DecrementRecursionDepth();
1262 coded_input.DecrementRecursionDepth();
1263 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1264 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1265 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1266 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1269 coded_input.SetRecursionLimit(6);
1270 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1278 ~ReallyBigInputStream() {}
1309 int64_t ByteCount()
const override {
1321 TEST_F(CodedStreamTest, InputOver2G) {
1324 ReallyBigInputStream
input;
1326 std::vector<std::string>
errors;
1329 ScopedMemoryLog error_log;
1346 #include <google/protobuf/port_undef.inc>