52 #include <google/protobuf/port_def.inc>
57 #define ULL(x) PROTOBUF_ULONGLONG(x)
89 #define TEST_1D(FIXTURE, NAME, CASES) \
90 class FIXTURE##_##NAME##_DD : public FIXTURE { \
92 template <typename CaseType> \
93 void DoSingleCase(const CaseType& CASES##_case); \
96 TEST_F(FIXTURE##_##NAME##_DD, NAME) { \
97 for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \
98 SCOPED_TRACE(testing::Message() \
99 << #CASES " case #" << i << ": " << CASES[i]); \
100 DoSingleCase(CASES[i]); \
104 template <typename CaseType> \
105 void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType& CASES##_case)
107 #define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \
108 class FIXTURE##_##NAME##_DD : public FIXTURE { \
110 template <typename CaseType1, typename CaseType2> \
111 void DoSingleCase(const CaseType1& CASES1##_case, \
112 const CaseType2& CASES2##_case); \
115 TEST_F(FIXTURE##_##NAME##_DD, NAME) { \
116 for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \
117 for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \
118 SCOPED_TRACE(testing::Message() \
119 << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \
120 << #CASES2 " case #" << j << ": " << CASES2[j]); \
121 DoSingleCase(CASES1[i], CASES2[j]); \
126 template <typename CaseType1, typename CaseType2> \
127 void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \
128 const CaseType2& CASES2##_case)
146 const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024};
158 inline std::ostream&
operator<<(std::ostream& os,
const VarintCase& c) {
159 return os << c.value;
162 VarintCase kVarintCases[] = {
167 {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)},
168 {{0xbe, 0xf7, 0x92, 0x84, 0x0b},
170 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
174 {{0xbe, 0xf7, 0x92, 0x84, 0x1b},
176 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
178 {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49},
180 (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
181 (
ULL(0x43) << 28) | (
ULL(0x49) << 35) | (
ULL(0x24) << 42) |
184 {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01},
186 (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
187 (
ULL(0x3b) << 28) | (
ULL(0x56) << 35) | (
ULL(0x00) << 42) |
188 (
ULL(0x05) << 49) | (
ULL(0x26) << 56) | (
ULL(0x01) << 63)},
192 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
196 CodedInputStream coded_input(&
input);
207 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
211 CodedInputStream coded_input(&
input);
213 uint32 expected_value =
static_cast<uint32>(kVarintCases_case.value);
214 EXPECT_EQ(expected_value, coded_input.ReadTag());
216 EXPECT_TRUE(coded_input.LastTagWas(expected_value));
217 EXPECT_FALSE(coded_input.LastTagWas(expected_value + 1));
225 TEST_F(CodedStreamTest, EmptyInputBeforeEos) {
226 class In :
public ZeroCopyInputStream {
231 virtual bool Next(
const void**
data,
int*
size) {
241 virtual int64 ByteCount()
const {
return 0; }
244 CodedInputStream
input(&in);
245 input.ReadTagNoLastTag();
249 TEST_1D(CodedStreamTest, ExpectTag, kVarintCases) {
253 memcpy(
buffer_ + 1, kVarintCases_case.bytes, kVarintCases_case.size);
257 CodedInputStream coded_input(&
input);
262 coded_input.ReadRaw(&
dummy, 1);
265 uint32 expected_value =
static_cast<uint32>(kVarintCases_case.value);
268 if (kVarintCases_case.size <= 2) {
269 EXPECT_FALSE(coded_input.ExpectTag(expected_value + 1));
270 EXPECT_TRUE(coded_input.ExpectTag(expected_value));
276 if (kVarintCases_case.size <= 2) {
283 TEST_1D(CodedStreamTest, ExpectTagFromArray, kVarintCases) {
284 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
286 const uint32 expected_value =
static_cast<uint32>(kVarintCases_case.value);
289 if (kVarintCases_case.size <= 2) {
301 memcpy(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
305 CodedInputStream coded_input(&
input);
316 if (kVarintCases_case.value >
ULL(0x00000000FFFFFFFF)) {
324 CodedOutputStream coded_output(&
output);
326 coded_output.WriteVarint32(
static_cast<uint32>(kVarintCases_case.value));
329 EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
334 memcmp(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
341 CodedOutputStream coded_output(&
output);
343 coded_output.WriteVarint64(kVarintCases_case.value);
346 EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
351 memcmp(
buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
356 #if !defined(__GNUC__) || __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
358 int32 kSignExtendedVarintCases[] = {0, 1, -1, 1237894, -37895138};
360 TEST_2D(CodedStreamTest, WriteVarint32SignExtended, kSignExtendedVarintCases,
365 CodedOutputStream coded_output(&
output);
367 coded_output.WriteVarint32SignExtended(kSignExtendedVarintCases_case);
370 if (kSignExtendedVarintCases_case < 0) {
377 if (kSignExtendedVarintCases_case < 0) {
387 CodedInputStream coded_input(&
input);
404 struct VarintErrorCase {
410 inline std::ostream&
operator<<(std::ostream& os,
const VarintErrorCase& c) {
411 return os <<
"size " << c.size;
414 const VarintErrorCase kVarintErrorCases[] = {
423 {{0xf0, 0xab}, 2,
false},
426 {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2}, 6,
false},
429 {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01},
435 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
436 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
438 CodedInputStream coded_input(&
input);
441 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&
value));
444 TEST_2D(CodedStreamTest, ReadVarint32Error_LeavesValueInInitializedState,
446 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
447 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
449 CodedInputStream coded_input(&
input);
452 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&
value));
460 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
461 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
463 CodedInputStream coded_input(&
input);
466 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&
value));
469 TEST_2D(CodedStreamTest, ReadVarint64Error_LeavesValueInInitializedState,
471 memcpy(
buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
472 ArrayInputStream
input(
buffer_, kVarintErrorCases_case.size,
474 CodedInputStream coded_input(&
input);
477 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&
value));
487 struct VarintSizeCase {
492 inline std::ostream&
operator<<(std::ostream& os,
const VarintSizeCase& c) {
493 return os << c.value;
496 VarintSizeCase kVarintSizeCases[] = {
503 {
ULL(41256202580718336), 8},
504 {
ULL(11964378330978735131), 10},
507 TEST_1D(CodedStreamTest, VarintSize32, kVarintSizeCases) {
508 if (kVarintSizeCases_case.value > 0xffffffffu) {
515 static_cast<uint32>(kVarintSizeCases_case.value)));
523 TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) {
525 for (
int i = 1;
i < 32;
i++) {
534 TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) {
536 for (
int i = 1;
i < 64;
i++) {
541 static_cast<uint64>(0x1ull <<
i)));
558 inline std::ostream&
operator<<(std::ostream& os,
const Fixed32Case& c) {
559 return os <<
"0x" << std::hex << c.value << std::dec;
562 inline std::ostream&
operator<<(std::ostream& os,
const Fixed64Case& c) {
563 return os <<
"0x" << std::hex << c.value << std::dec;
566 Fixed32Case kFixed32Cases[] = {
567 {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdefu},
568 {{0x12, 0x34, 0x56, 0x78}, 0x78563412u},
571 Fixed64Case kFixed64Cases[] = {
572 {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78},
ULL(0x7856341290abcdef)},
573 {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
ULL(0x8877665544332211)},
577 memcpy(
buffer_, kFixed32Cases_case.bytes,
sizeof(kFixed32Cases_case.bytes));
581 CodedInputStream coded_input(&
input);
592 memcpy(
buffer_, kFixed64Cases_case.bytes,
sizeof(kFixed64Cases_case.bytes));
596 CodedInputStream coded_input(&
input);
610 CodedOutputStream coded_output(&
output);
612 coded_output.WriteLittleEndian32(kFixed32Cases_case.value);
626 CodedOutputStream coded_output(&
output);
628 coded_output.WriteLittleEndian64(kFixed64Cases_case.value);
640 TEST_1D(CodedStreamTest, ReadLittleEndian32FromArray, kFixed32Cases) {
641 memcpy(
buffer_, kFixed32Cases_case.bytes,
sizeof(kFixed32Cases_case.bytes));
650 TEST_1D(CodedStreamTest, ReadLittleEndian64FromArray, kFixed64Cases) {
651 memcpy(
buffer_, kFixed64Cases_case.bytes,
sizeof(kFixed64Cases_case.bytes));
663 const char kRawBytes[] =
"Some bytes which will be written and read raw.";
666 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
668 char read_buffer[
sizeof(kRawBytes)];
671 CodedInputStream coded_input(&
input);
673 EXPECT_TRUE(coded_input.ReadRaw(read_buffer,
sizeof(kRawBytes)));
674 EXPECT_EQ(0, memcmp(kRawBytes, read_buffer,
sizeof(kRawBytes)));
684 CodedOutputStream coded_output(&
output);
686 coded_output.WriteRaw(kRawBytes,
sizeof(kRawBytes));
689 EXPECT_EQ(
sizeof(kRawBytes), coded_output.ByteCount());
697 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
701 CodedInputStream coded_input(&
input);
716 CodedInputStream coded_input(&
input);
724 TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnStack) {
728 CodedInputStream coded_input(
buffer, 8);
733 TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnHeap) {
735 CodedInputStream coded_input(
buffer.get(), 8);
741 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
745 CodedInputStream coded_input(&
input);
746 coded_input.SetTotalBytesLimit(
sizeof(kRawBytes),
sizeof(kRawBytes));
747 EXPECT_EQ(
sizeof(kRawBytes), coded_input.BytesUntilTotalBytesLimit());
751 EXPECT_EQ(
sizeof(kRawBytes) - strlen(kRawBytes),
752 coded_input.BytesUntilTotalBytesLimit());
762 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
766 CodedInputStream coded_input(&
input);
767 coded_input.PushLimit(
sizeof(
buffer_));
779 TEST_F(CodedStreamTest, ReadStringNoReservationIfLimitsNotSet) {
780 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
787 CodedInputStream coded_input(&
input);
803 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsNegative) {
804 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
811 CodedInputStream coded_input(&
input);
812 coded_input.PushLimit(
sizeof(
buffer_));
823 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsLarge) {
824 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
831 CodedInputStream coded_input(&
input);
832 coded_input.PushLimit(
sizeof(
buffer_));
840 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheLimit) {
841 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
848 CodedInputStream coded_input(&
input);
849 coded_input.PushLimit(16);
860 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheTotalBytesLimit) {
861 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
868 CodedInputStream coded_input(&
input);
869 coded_input.SetTotalBytesLimit(16, 16);
881 ReadStringNoReservationSizeIsOverTheClosestLimit_GlobalLimitIsCloser) {
882 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
889 CodedInputStream coded_input(&
input);
890 coded_input.PushLimit(
sizeof(
buffer_));
891 coded_input.SetTotalBytesLimit(16, 16);
903 ReadStringNoReservationSizeIsOverTheClosestLimit_LocalLimitIsCloser) {
904 memcpy(
buffer_, kRawBytes,
sizeof(kRawBytes));
911 CodedInputStream coded_input(&
input);
912 coded_input.PushLimit(16);
929 const char kSkipTestBytes[] =
930 "<Before skipping><To be skipped><After skipping>";
933 memcpy(
buffer_, kSkipTestBytes,
sizeof(kSkipTestBytes));
937 CodedInputStream coded_input(&
input);
940 EXPECT_TRUE(coded_input.ReadString(&
str, strlen(
"<Before skipping>")));
942 EXPECT_TRUE(coded_input.Skip(strlen(
"<To be skipped>")));
943 EXPECT_TRUE(coded_input.ReadString(&
str, strlen(
"<After skipping>")));
953 TEST_F(CodedStreamTest, GetDirectBufferPointerInput) {
955 CodedInputStream coded_input(&
input);
982 TEST_F(CodedStreamTest, GetDirectBufferPointerInlineInput) {
984 CodedInputStream coded_input(&
input);
989 coded_input.GetDirectBufferPointerInline(&ptr, &
size);
994 coded_input.GetDirectBufferPointerInline(&ptr, &
size);
1000 coded_input.GetDirectBufferPointerInline(&ptr, &
size);
1007 coded_input.GetDirectBufferPointerInline(&ptr, &
size);
1012 TEST_F(CodedStreamTest, GetDirectBufferPointerOutput) {
1014 CodedOutputStream coded_output(&
output);
1054 CodedInputStream coded_input(&
input);
1056 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1061 EXPECT_EQ(8, coded_input.BytesUntilLimit());
1063 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1065 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1067 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1069 coded_input.PopLimit(limit);
1071 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1084 CodedInputStream coded_input(&
input);
1086 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1088 EXPECT_EQ(8, coded_input.BytesUntilLimit());
1094 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1096 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1098 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1100 coded_input.PopLimit(limit2);
1103 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1105 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1107 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1109 coded_input.PopLimit(limit1);
1112 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1126 CodedInputStream coded_input(&
input);
1128 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1130 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1138 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1140 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1142 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1144 coded_input.PopLimit(limit2);
1147 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1149 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1151 coded_input.PopLimit(limit1);
1154 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1161 TEST_F(CodedStreamTest, ExpectAtEnd) {
1164 CodedInputStream coded_input(&
input);
1174 coded_input.PopLimit(limit);
1178 TEST_F(CodedStreamTest, NegativeLimit) {
1181 CodedInputStream coded_input(&
input);
1186 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1187 coded_input.PopLimit(limit);
1190 TEST_F(CodedStreamTest, NegativeLimitAfterReading) {
1193 CodedInputStream coded_input(&
input);
1199 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1200 coded_input.PopLimit(limit);
1203 TEST_F(CodedStreamTest, OverflowLimit) {
1207 CodedInputStream coded_input(&
input);
1213 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1214 coded_input.PopLimit(limit);
1217 TEST_F(CodedStreamTest, TotalBytesLimit) {
1219 CodedInputStream coded_input(&
input);
1220 coded_input.SetTotalBytesLimit(16, -1);
1221 EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
1225 EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
1227 std::vector<std::string>
errors;
1230 ScopedMemoryLog error_log;
1237 "A protocol message was rejected because it was too big",
1240 coded_input.SetTotalBytesLimit(32, -1);
1241 EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
1243 EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
1246 TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) {
1250 CodedInputStream coded_input(&
input);
1253 coded_input.SetTotalBytesLimit(16, -1);
1262 EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
1266 coded_input.PopLimit(limit);
1270 EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
1274 TEST_F(CodedStreamTest, RecursionLimit) {
1276 CodedInputStream coded_input(&
input);
1277 coded_input.SetRecursionLimit(4);
1280 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1281 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1282 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1283 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1286 coded_input.DecrementRecursionDepth();
1288 coded_input.DecrementRecursionDepth();
1289 coded_input.DecrementRecursionDepth();
1290 coded_input.DecrementRecursionDepth();
1291 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1293 coded_input.DecrementRecursionDepth();
1294 coded_input.DecrementRecursionDepth();
1295 coded_input.DecrementRecursionDepth();
1296 coded_input.DecrementRecursionDepth();
1297 coded_input.DecrementRecursionDepth();
1298 coded_input.DecrementRecursionDepth();
1299 coded_input.DecrementRecursionDepth();
1300 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1301 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1302 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1303 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1306 coded_input.SetRecursionLimit(6);
1307 EXPECT_TRUE(coded_input.IncrementRecursionDepth());
1312 class ReallyBigInputStream :
public ZeroCopyInputStream {
1315 ~ReallyBigInputStream() {}
1318 bool Next(
const void**
data,
int*
size) {
1346 int64 ByteCount()
const {
1358 TEST_F(CodedStreamTest, InputOver2G) {
1361 ReallyBigInputStream
input;
1363 std::vector<std::string>
errors;
1366 ScopedMemoryLog error_log;
1367 CodedInputStream coded_input(&
input);