1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // Author: kenton@google.com (Kenton Varda)
9 // Based on original Protocol Buffers design by
10 // Sanjay Ghemawat, Jeff Dean, and others.
11 //
12 // This file contains tests and benchmarks.
13
14 #include "google/protobuf/io/coded_stream.h"
15
16 #include <limits.h>
17
18 #include <algorithm>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <cstring>
23 #include <ios>
24 #include <memory>
25 #include <ostream>
26 #include <string>
27 #include <tuple>
28 #include <utility>
29 #include <vector>
30
31 #include <gmock/gmock.h>
32 #include <gtest/gtest.h>
33 #include "absl/base/casts.h"
34 #include "absl/base/log_severity.h"
35 #include "absl/base/macros.h"
36 #include "absl/log/absl_log.h"
37 #include "absl/log/scoped_mock_log.h"
38 #include "absl/strings/cord.h"
39 #include "absl/strings/str_cat.h"
40 #include "absl/strings/string_view.h"
41 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
42
43 #include "google/protobuf/testing/googletest.h"
44
45
46 // Must be included last.
47 #include "google/protobuf/port_def.inc"
48
49 namespace google {
50 namespace protobuf {
51 namespace io {
52 namespace {
53
54 class CodedStreamTest : public testing::Test {
55 protected:
56 // Buffer used during most of the tests. This assumes tests run sequentially.
57 static constexpr int kBufferSize = 1024 * 64;
58 static uint8_t buffer_[kBufferSize];
59 };
60
61 uint8_t CodedStreamTest::buffer_[CodedStreamTest::kBufferSize] = {};
62
63 // We test each operation over a variety of block sizes to insure that
64 // we test cases where reads or writes cross buffer boundaries, cases
65 // where they don't, and cases where there is so much buffer left that
66 // we can use special optimized paths that don't worry about bounds
67 // checks.
68 const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024};
69
70 // In several ReadCord test functions, we either clear the Cord before ReadCord
71 // calls or not.
72 const bool kResetCords[] = {false, true};
73
74 // -------------------------------------------------------------------
75 // Varint tests.
76
77 struct VarintCase {
78 uint8_t bytes[10]; // Encoded bytes.
79 size_t size; // Encoded size, in bytes.
80 uint64_t value; // Parsed value.
81 };
82
operator <<(std::ostream & os,const VarintCase & c)83 inline std::ostream& operator<<(std::ostream& os, const VarintCase& c) {
84 return os << c.value;
85 }
86
87 VarintCase kVarintCases[] = {
88 // 32-bit values
89 {{0x00}, 1, 0},
90 {{0x01}, 1, 1},
91 {{0x7f}, 1, 127},
92 {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)}, // 14882
93 {{0xbe, 0xf7, 0x92, 0x84, 0x0b},
94 5, // 2961488830
95 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
96 (uint64_t{0x0bu} << 28)},
97
98 // 64-bit
99 {{0xbe, 0xf7, 0x92, 0x84, 0x1b},
100 5, // 7256456126
101 (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
102 (uint64_t{0x1bu} << 28)},
103 {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49},
104 8, // 41256202580718336
105 (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
106 (uint64_t{0x43u} << 28) | (uint64_t{0x49u} << 35) |
107 (uint64_t{0x24u} << 42) | (uint64_t{0x49u} << 49)},
108 // 11964378330978735131
109 {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01},
110 10,
111 (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
112 (uint64_t{0x3bu} << 28) | (uint64_t{0x56u} << 35) |
113 (uint64_t{0x00u} << 42) | (uint64_t{0x05u} << 49) |
114 (uint64_t{0x26u} << 56) | (uint64_t{0x01u} << 63)},
115 };
116
117 class VarintCasesWithSizes
118 : public CodedStreamTest,
119 public testing::WithParamInterface<std::tuple<VarintCase, int>> {};
120
TEST_P(VarintCasesWithSizes,ReadVarint32)121 TEST_P(VarintCasesWithSizes, ReadVarint32) {
122 const VarintCase& kVarintCases_case = std::get<0>(GetParam());
123 const int& kBlockSizes_case = std::get<1>(GetParam());
124 memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
125 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
126
127 {
128 CodedInputStream coded_input(&input);
129
130 uint32_t value;
131 EXPECT_TRUE(coded_input.ReadVarint32(&value));
132 EXPECT_EQ(static_cast<uint32_t>(kVarintCases_case.value), value);
133 }
134
135 EXPECT_EQ(kVarintCases_case.size, input.ByteCount());
136 }
137
TEST_P(VarintCasesWithSizes,ReadTag)138 TEST_P(VarintCasesWithSizes, ReadTag) {
139 const VarintCase& kVarintCases_case = std::get<0>(GetParam());
140 const int& kBlockSizes_case = std::get<1>(GetParam());
141 memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
142 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
143
144 {
145 CodedInputStream coded_input(&input);
146
147 uint32_t expected_value = static_cast<uint32_t>(kVarintCases_case.value);
148 EXPECT_EQ(expected_value, coded_input.ReadTag());
149
150 EXPECT_TRUE(coded_input.LastTagWas(expected_value));
151 EXPECT_FALSE(coded_input.LastTagWas(expected_value + 1));
152 }
153
154 EXPECT_EQ(kVarintCases_case.size, input.ByteCount());
155 }
156
157 // This is the regression test that verifies that there is no issues
158 // with the empty input buffers handling.
TEST_F(CodedStreamTest,EmptyInputBeforeEos)159 TEST_F(CodedStreamTest, EmptyInputBeforeEos) {
160 class In : public ZeroCopyInputStream {
161 public:
162 In() : count_(0) {}
163
164 private:
165 bool Next(const void** data, int* size) override {
166 *data = nullptr;
167 *size = 0;
168 return count_++ < 2;
169 }
170 void BackUp(int count) override {
171 ABSL_LOG(FATAL) << "Tests never call this.";
172 }
173 bool Skip(int count) override {
174 ABSL_LOG(FATAL) << "Tests never call this.";
175 return false;
176 }
177 int64_t ByteCount() const override { return 0; }
178 int count_;
179 } in;
180 CodedInputStream input(&in);
181 input.ReadTagNoLastTag();
182 EXPECT_TRUE(input.ConsumedEntireMessage());
183 }
184
185 class VarintCases : public CodedStreamTest,
186 public testing::WithParamInterface<VarintCase> {};
187
TEST_P(VarintCases,ExpectTag)188 TEST_P(VarintCases, ExpectTag) {
189 const VarintCase& kVarintCases_case = GetParam();
190 // Leave one byte at the beginning of the buffer so we can read it
191 // to force the first buffer to be loaded.
192 buffer_[0] = '\0';
193 memcpy(buffer_ + 1, kVarintCases_case.bytes, kVarintCases_case.size);
194 ArrayInputStream input(buffer_, sizeof(buffer_));
195
196 {
197 CodedInputStream coded_input(&input);
198
199 // Read one byte to force coded_input.Refill() to be called. Otherwise,
200 // ExpectTag() will return a false negative.
201 uint8_t dummy;
202 coded_input.ReadRaw(&dummy, 1);
203 EXPECT_EQ((uint)'\0', (uint)dummy);
204
205 uint32_t expected_value = static_cast<uint32_t>(kVarintCases_case.value);
206
207 // ExpectTag() produces false negatives for large values.
208 if (kVarintCases_case.size <= 2) {
209 EXPECT_FALSE(coded_input.ExpectTag(expected_value + 1));
210 EXPECT_TRUE(coded_input.ExpectTag(expected_value));
211 } else {
212 EXPECT_FALSE(coded_input.ExpectTag(expected_value));
213 }
214 }
215
216 if (kVarintCases_case.size <= 2) {
217 EXPECT_EQ(kVarintCases_case.size + 1, input.ByteCount());
218 } else {
219 EXPECT_EQ(1, input.ByteCount());
220 }
221 }
222
TEST_P(VarintCases,ExpectTagFromArray)223 TEST_P(VarintCases, ExpectTagFromArray) {
224 const VarintCase& kVarintCases_case = GetParam();
225 memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
226
227 const uint32_t expected_value =
228 static_cast<uint32_t>(kVarintCases_case.value);
229
230 // If the expectation succeeds, it should return a pointer past the tag.
231 if (kVarintCases_case.size <= 2) {
232 EXPECT_TRUE(nullptr == CodedInputStream::ExpectTagFromArray(
233 buffer_, expected_value + 1));
234 EXPECT_TRUE(buffer_ + kVarintCases_case.size ==
235 CodedInputStream::ExpectTagFromArray(buffer_, expected_value));
236 } else {
237 EXPECT_TRUE(nullptr ==
238 CodedInputStream::ExpectTagFromArray(buffer_, expected_value));
239 }
240 }
241
TEST_P(VarintCasesWithSizes,ReadVarint64)242 TEST_P(VarintCasesWithSizes, ReadVarint64) {
243 const VarintCase& kVarintCases_case = std::get<0>(GetParam());
244 const int& kBlockSizes_case = std::get<1>(GetParam());
245 memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
246 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
247
248 {
249 CodedInputStream coded_input(&input);
250
251 uint64_t value;
252 EXPECT_TRUE(coded_input.ReadVarint64(&value));
253 EXPECT_EQ(kVarintCases_case.value, value);
254 }
255
256 EXPECT_EQ(kVarintCases_case.size, input.ByteCount());
257 }
258
TEST_P(VarintCasesWithSizes,WriteVarint32)259 TEST_P(VarintCasesWithSizes, WriteVarint32) {
260 const VarintCase& kVarintCases_case = std::get<0>(GetParam());
261 const int& kBlockSizes_case = std::get<1>(GetParam());
262 if (kVarintCases_case.value > uint64_t{0x00000000FFFFFFFFu}) {
263 // Skip this test for the 64-bit values.
264 return;
265 }
266
267 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
268
269 {
270 CodedOutputStream coded_output(&output);
271
272 coded_output.WriteVarint32(static_cast<uint32_t>(kVarintCases_case.value));
273 EXPECT_FALSE(coded_output.HadError());
274
275 EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
276 }
277
278 EXPECT_EQ(kVarintCases_case.size, output.ByteCount());
279 EXPECT_EQ(0,
280 memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
281 }
282
TEST_P(VarintCasesWithSizes,WriteVarint64)283 TEST_P(VarintCasesWithSizes, WriteVarint64) {
284 const VarintCase& kVarintCases_case = std::get<0>(GetParam());
285 const int& kBlockSizes_case = std::get<1>(GetParam());
286 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
287
288 {
289 CodedOutputStream coded_output(&output);
290
291 coded_output.WriteVarint64(kVarintCases_case.value);
292 EXPECT_FALSE(coded_output.HadError());
293
294 EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
295 }
296
297 EXPECT_EQ(kVarintCases_case.size, output.ByteCount());
298 EXPECT_EQ(0,
299 memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
300 }
301
302 class SignedVarintCasesWithSizes
303 : public CodedStreamTest,
304 public testing::WithParamInterface<std::tuple<int32_t, int>> {};
305
306 int32_t kSignExtendedVarintCases[] = {0, 1, -1, 1237894, -37895138};
307
TEST_P(SignedVarintCasesWithSizes,WriteVarint32SignExtended)308 TEST_P(SignedVarintCasesWithSizes, WriteVarint32SignExtended) {
309 const int32_t& kSignExtendedVarintCases_case = std::get<0>(GetParam());
310 const int& kBlockSizes_case = std::get<1>(GetParam());
311 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
312
313 {
314 CodedOutputStream coded_output(&output);
315
316 coded_output.WriteVarint32SignExtended(kSignExtendedVarintCases_case);
317 EXPECT_FALSE(coded_output.HadError());
318
319 if (kSignExtendedVarintCases_case < 0) {
320 EXPECT_EQ(10, coded_output.ByteCount());
321 } else {
322 EXPECT_LE(coded_output.ByteCount(), 5);
323 }
324 }
325
326 if (kSignExtendedVarintCases_case < 0) {
327 EXPECT_EQ(10, output.ByteCount());
328 } else {
329 EXPECT_LE(output.ByteCount(), 5);
330 }
331
332 // Read value back in as a varint64 and insure it matches.
333 ArrayInputStream input(buffer_, sizeof(buffer_));
334
335 {
336 CodedInputStream coded_input(&input);
337
338 uint64_t value;
339 EXPECT_TRUE(coded_input.ReadVarint64(&value));
340
341 EXPECT_EQ(kSignExtendedVarintCases_case, static_cast<int64_t>(value));
342 }
343
344 EXPECT_EQ(output.ByteCount(), input.ByteCount());
345 }
346
347
348 // -------------------------------------------------------------------
349 // Varint failure test.
350
351 struct VarintErrorCase {
352 uint8_t bytes[12];
353 size_t size;
354 bool can_parse;
355 const char* name;
356 };
357
operator <<(std::ostream & os,const VarintErrorCase & c)358 inline std::ostream& operator<<(std::ostream& os, const VarintErrorCase& c) {
359 return os << "size " << c.size;
360 }
361
362 const VarintErrorCase kVarintErrorCases[] = {
363 // Control case. (Insures that there isn't something else wrong that
364 // makes parsing always fail.)
365 {{0x00}, 1, true, "Control"},
366
367 // No input data.
368 {{}, 0, false, "No_Input"},
369
370 // Input ends unexpectedly.
371 {{0xf0, 0xab}, 2, false, "Input_Ends_Unexpectedly"},
372
373 // Input ends unexpectedly after 32 bits.
374 {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2},
375 6,
376 false,
377 "Input_Ends_Unexpectedly_After_32_Bits"},
378
379 // Longer than 10 bytes.
380 {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01},
381 11,
382 false,
383 "Longer_Than_10_Bytes"},
384 };
385
386 class VarintErrorCasesWithSizes
387 : public CodedStreamTest,
388 public testing::WithParamInterface<std::tuple<VarintErrorCase, int>> {};
389
TEST_P(VarintErrorCasesWithSizes,ReadVarint32Error)390 TEST_P(VarintErrorCasesWithSizes, ReadVarint32Error) {
391 VarintErrorCase kVarintErrorCases_case = std::get<0>(GetParam());
392 int kBlockSizes_case = std::get<1>(GetParam());
393 memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
394 ArrayInputStream input(buffer_, static_cast<int>(kVarintErrorCases_case.size),
395 kBlockSizes_case);
396 CodedInputStream coded_input(&input);
397
398 uint32_t value;
399 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value));
400 }
401
TEST_P(VarintErrorCasesWithSizes,ReadVarint32Error_LeavesValueInInitializedState)402 TEST_P(VarintErrorCasesWithSizes,
403 ReadVarint32Error_LeavesValueInInitializedState) {
404 VarintErrorCase kVarintErrorCases_case = std::get<0>(GetParam());
405 int kBlockSizes_case = std::get<1>(GetParam());
406 memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
407 ArrayInputStream input(buffer_, static_cast<int>(kVarintErrorCases_case.size),
408 kBlockSizes_case);
409 CodedInputStream coded_input(&input);
410
411 uint32_t value = 0;
412 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value));
413 // While the specific value following a failure is not critical, we do want to
414 // ensure that it doesn't get set to an uninitialized value. (This check fails
415 // in MSAN mode if value has been set to an uninitialized value.)
416 EXPECT_EQ(value, value);
417 }
418
TEST_P(VarintErrorCasesWithSizes,ReadVarint64Error)419 TEST_P(VarintErrorCasesWithSizes, ReadVarint64Error) {
420 VarintErrorCase kVarintErrorCases_case = std::get<0>(GetParam());
421 int kBlockSizes_case = std::get<1>(GetParam());
422 memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
423 ArrayInputStream input(buffer_, static_cast<int>(kVarintErrorCases_case.size),
424 kBlockSizes_case);
425 CodedInputStream coded_input(&input);
426
427 uint64_t value;
428 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value));
429 }
430
TEST_P(VarintErrorCasesWithSizes,ReadVarint64Error_LeavesValueInInitializedState)431 TEST_P(VarintErrorCasesWithSizes,
432 ReadVarint64Error_LeavesValueInInitializedState) {
433 VarintErrorCase kVarintErrorCases_case = std::get<0>(GetParam());
434 int kBlockSizes_case = std::get<1>(GetParam());
435 memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
436 ArrayInputStream input(buffer_, static_cast<int>(kVarintErrorCases_case.size),
437 kBlockSizes_case);
438 CodedInputStream coded_input(&input);
439
440 uint64_t value = 0;
441 EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value));
442 // While the specific value following a failure is not critical, we do want to
443 // ensure that it doesn't get set to an uninitialized value. (This check fails
444 // in MSAN mode if value has been set to an uninitialized value.)
445 EXPECT_EQ(value, value);
446 }
447
448 // -------------------------------------------------------------------
449 // VarintSize
450
451 struct VarintSizeCase {
452 uint64_t value;
453 int size;
454 };
455
operator <<(std::ostream & os,const VarintSizeCase & c)456 inline std::ostream& operator<<(std::ostream& os, const VarintSizeCase& c) {
457 return os << c.value;
458 }
459
460 VarintSizeCase kVarintSizeCases[] = {
461 {0u, 1},
462 {1u, 1},
463 {127u, 1},
464 {128u, 2},
465 {758923u, 3},
466 {4000000000u, 5},
467 {uint64_t{41256202580718336u}, 8},
468 {uint64_t{11964378330978735131u}, 10},
469 };
470
471 class VarintSizeCases : public CodedStreamTest,
472 public testing::WithParamInterface<VarintSizeCase> {};
473
TEST_P(VarintSizeCases,VarintSize32)474 TEST_P(VarintSizeCases, VarintSize32) {
475 VarintSizeCase kVarintSizeCases_case = GetParam();
476 if (kVarintSizeCases_case.value > 0xffffffffu) {
477 // Skip 64-bit values.
478 return;
479 }
480
481 EXPECT_EQ(kVarintSizeCases_case.size,
482 CodedOutputStream::VarintSize32(
483 static_cast<uint32_t>(kVarintSizeCases_case.value)));
484 }
485
TEST_P(VarintSizeCases,VarintSize64)486 TEST_P(VarintSizeCases, VarintSize64) {
487 VarintSizeCase kVarintSizeCases_case = GetParam();
488 EXPECT_EQ(kVarintSizeCases_case.size,
489 CodedOutputStream::VarintSize64(kVarintSizeCases_case.value));
490 }
491
TEST_F(CodedStreamTest,VarintSize32PowersOfTwo)492 TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) {
493 int expected = 1;
494 for (int i = 1; i < 32; i++) {
495 if (i % 7 == 0) {
496 expected += 1;
497 }
498 EXPECT_EQ(expected, CodedOutputStream::VarintSize32(
499 static_cast<uint32_t>(0x1u << i)));
500 }
501 }
502
TEST_F(CodedStreamTest,VarintSize64PowersOfTwo)503 TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) {
504 int expected = 1;
505 for (int i = 1; i < 64; i++) {
506 if (i % 7 == 0) {
507 expected += 1;
508 }
509 EXPECT_EQ(expected, CodedOutputStream::VarintSize64(uint64_t{1} << i));
510 }
511 }
512
513 // -------------------------------------------------------------------
514 // Fixed-size int tests
515
516 struct Fixed16Case {
517 uint8_t bytes[sizeof(uint16_t)]; // Encoded bytes.
518 uint32_t value; // Parsed value.
519 };
520
521 struct Fixed32Case {
522 uint8_t bytes[sizeof(uint32_t)]; // Encoded bytes.
523 uint32_t value; // Parsed value.
524 };
525
526 struct Fixed64Case {
527 uint8_t bytes[sizeof(uint64_t)]; // Encoded bytes.
528 uint64_t value; // Parsed value.
529 };
530
531 class Fixed16Cases : public CodedStreamTest,
532 public testing::WithParamInterface<Fixed16Case> {};
533
534 class Fixed16CasesWithSizes
535 : public CodedStreamTest,
536 public testing::WithParamInterface<std::tuple<Fixed16Case, int>> {};
537
538 class Fixed32Cases : public CodedStreamTest,
539 public testing::WithParamInterface<Fixed32Case> {};
540
541 class Fixed32CasesWithSizes
542 : public CodedStreamTest,
543 public testing::WithParamInterface<std::tuple<Fixed32Case, int>> {};
544
545 class Fixed64Cases : public CodedStreamTest,
546 public testing::WithParamInterface<Fixed64Case> {};
547
548 class Fixed64CasesWithSizes
549 : public CodedStreamTest,
550 public testing::WithParamInterface<std::tuple<Fixed64Case, int>> {};
551
operator <<(std::ostream & os,const Fixed32Case & c)552 inline std::ostream& operator<<(std::ostream& os, const Fixed32Case& c) {
553 return os << "0x" << std::hex << c.value << std::dec;
554 }
555
operator <<(std::ostream & os,const Fixed64Case & c)556 inline std::ostream& operator<<(std::ostream& os, const Fixed64Case& c) {
557 return os << "0x" << std::hex << c.value << std::dec;
558 }
559
560 Fixed16Case kFixed16Cases[] = {
561 {{0xef, 0xcd}, 0xcdefu},
562 {{0x12, 0x34}, 0x3412u},
563 };
564
565 Fixed32Case kFixed32Cases[] = {
566 {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdefu},
567 {{0x12, 0x34, 0x56, 0x78}, 0x78563412u},
568 };
569
570 Fixed64Case kFixed64Cases[] = {
571 {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78},
572 uint64_t{0x7856341290abcdefu}},
573 {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
574 uint64_t{0x8877665544332211u}},
575 };
576
TEST_P(Fixed16CasesWithSizes,ReadLittleEndian16)577 TEST_P(Fixed16CasesWithSizes, ReadLittleEndian16) {
578 Fixed16Case kFixed16Cases_case = std::get<0>(GetParam());
579 int kBlockSizes_case = std::get<1>(GetParam());
580 memcpy(buffer_, kFixed16Cases_case.bytes, sizeof(kFixed16Cases_case.bytes));
581 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
582
583 {
584 CodedInputStream coded_input(&input);
585
586 uint16_t value;
587 EXPECT_TRUE(coded_input.ReadLittleEndian16(&value));
588 EXPECT_EQ(kFixed16Cases_case.value, value);
589 }
590
591 EXPECT_EQ(sizeof(uint16_t), input.ByteCount());
592 }
593
TEST_P(Fixed32CasesWithSizes,ReadLittleEndian32)594 TEST_P(Fixed32CasesWithSizes, ReadLittleEndian32) {
595 Fixed32Case kFixed32Cases_case = std::get<0>(GetParam());
596 int kBlockSizes_case = std::get<1>(GetParam());
597 memcpy(buffer_, kFixed32Cases_case.bytes, sizeof(kFixed32Cases_case.bytes));
598 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
599
600 {
601 CodedInputStream coded_input(&input);
602
603 uint32_t value;
604 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
605 EXPECT_EQ(kFixed32Cases_case.value, value);
606 }
607
608 EXPECT_EQ(sizeof(uint32_t), input.ByteCount());
609 }
610
TEST_P(Fixed64CasesWithSizes,ReadLittleEndian64)611 TEST_P(Fixed64CasesWithSizes, ReadLittleEndian64) {
612 Fixed64Case kFixed64Cases_case = std::get<0>(GetParam());
613 int kBlockSizes_case = std::get<1>(GetParam());
614 memcpy(buffer_, kFixed64Cases_case.bytes, sizeof(kFixed64Cases_case.bytes));
615 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
616
617 {
618 CodedInputStream coded_input(&input);
619
620 uint64_t value;
621 EXPECT_TRUE(coded_input.ReadLittleEndian64(&value));
622 EXPECT_EQ(kFixed64Cases_case.value, value);
623 }
624
625 EXPECT_EQ(sizeof(uint64_t), input.ByteCount());
626 }
627
TEST_P(Fixed16CasesWithSizes,WriteLittleEndian16)628 TEST_P(Fixed16CasesWithSizes, WriteLittleEndian16) {
629 Fixed16Case kFixed16Cases_case = std::get<0>(GetParam());
630 int kBlockSizes_case = std::get<1>(GetParam());
631 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
632
633 {
634 CodedOutputStream coded_output(&output);
635
636 coded_output.WriteLittleEndian16(kFixed16Cases_case.value);
637 EXPECT_FALSE(coded_output.HadError());
638
639 EXPECT_EQ(sizeof(uint16_t), coded_output.ByteCount());
640 }
641
642 EXPECT_EQ(sizeof(uint16_t), output.ByteCount());
643 EXPECT_EQ(0, memcmp(buffer_, kFixed16Cases_case.bytes, sizeof(uint16_t)));
644 }
645
TEST_P(Fixed32CasesWithSizes,WriteLittleEndian32)646 TEST_P(Fixed32CasesWithSizes, WriteLittleEndian32) {
647 Fixed32Case kFixed32Cases_case = std::get<0>(GetParam());
648 int kBlockSizes_case = std::get<1>(GetParam());
649 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
650
651 {
652 CodedOutputStream coded_output(&output);
653
654 coded_output.WriteLittleEndian32(kFixed32Cases_case.value);
655 EXPECT_FALSE(coded_output.HadError());
656
657 EXPECT_EQ(sizeof(uint32_t), coded_output.ByteCount());
658 }
659
660 EXPECT_EQ(sizeof(uint32_t), output.ByteCount());
661 EXPECT_EQ(0, memcmp(buffer_, kFixed32Cases_case.bytes, sizeof(uint32_t)));
662 }
663
TEST_P(Fixed64CasesWithSizes,WriteLittleEndian64)664 TEST_P(Fixed64CasesWithSizes, WriteLittleEndian64) {
665 Fixed64Case kFixed64Cases_case = std::get<0>(GetParam());
666 int kBlockSizes_case = std::get<1>(GetParam());
667 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
668
669 {
670 CodedOutputStream coded_output(&output);
671
672 coded_output.WriteLittleEndian64(kFixed64Cases_case.value);
673 EXPECT_FALSE(coded_output.HadError());
674
675 EXPECT_EQ(sizeof(uint64_t), coded_output.ByteCount());
676 }
677
678 EXPECT_EQ(sizeof(uint64_t), output.ByteCount());
679 EXPECT_EQ(0, memcmp(buffer_, kFixed64Cases_case.bytes, sizeof(uint64_t)));
680 }
681
682 // Tests using the static methods to read fixed-size values from raw arrays.
683
TEST_P(Fixed16Cases,ReadLittleEndian16FromArray)684 TEST_P(Fixed16Cases, ReadLittleEndian16FromArray) {
685 Fixed16Case kFixed16Cases_case = GetParam();
686 memcpy(buffer_, kFixed16Cases_case.bytes, sizeof(kFixed16Cases_case.bytes));
687
688 uint16_t value;
689 const uint8_t* end =
690 CodedInputStream::ReadLittleEndian16FromArray(buffer_, &value);
691 EXPECT_EQ(kFixed16Cases_case.value, value);
692 EXPECT_TRUE(end == buffer_ + sizeof(value));
693 }
694
TEST_P(Fixed32Cases,ReadLittleEndian32FromArray)695 TEST_P(Fixed32Cases, ReadLittleEndian32FromArray) {
696 Fixed32Case kFixed32Cases_case = GetParam();
697 memcpy(buffer_, kFixed32Cases_case.bytes, sizeof(kFixed32Cases_case.bytes));
698
699 uint32_t value;
700 const uint8_t* end =
701 CodedInputStream::ReadLittleEndian32FromArray(buffer_, &value);
702 EXPECT_EQ(kFixed32Cases_case.value, value);
703 EXPECT_TRUE(end == buffer_ + sizeof(value));
704 }
705
TEST_P(Fixed64Cases,ReadLittleEndian64FromArray)706 TEST_P(Fixed64Cases, ReadLittleEndian64FromArray) {
707 Fixed64Case kFixed64Cases_case = GetParam();
708 memcpy(buffer_, kFixed64Cases_case.bytes, sizeof(kFixed64Cases_case.bytes));
709
710 uint64_t value;
711 const uint8_t* end =
712 CodedInputStream::ReadLittleEndian64FromArray(buffer_, &value);
713 EXPECT_EQ(kFixed64Cases_case.value, value);
714 EXPECT_TRUE(end == buffer_ + sizeof(value));
715 }
716
717 // -------------------------------------------------------------------
718 // Raw reads and writes
719
720 const char kRawBytes[] = "Some bytes which will be written and read raw.";
721
722 class BlockSizes : public CodedStreamTest,
723 public testing::WithParamInterface<int> {};
724
TEST_P(BlockSizes,ReadRaw)725 TEST_P(BlockSizes, ReadRaw) {
726 int kBlockSizes_case = GetParam();
727 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
728 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
729 char read_buffer[sizeof(kRawBytes)];
730
731 {
732 CodedInputStream coded_input(&input);
733
734 EXPECT_TRUE(coded_input.ReadRaw(read_buffer, sizeof(kRawBytes)));
735 EXPECT_EQ(0, memcmp(kRawBytes, read_buffer, sizeof(kRawBytes)));
736 }
737
738 EXPECT_EQ(sizeof(kRawBytes), input.ByteCount());
739 }
740
TEST_P(BlockSizes,WriteRaw)741 TEST_P(BlockSizes, WriteRaw) {
742 int kBlockSizes_case = GetParam();
743 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
744
745 {
746 CodedOutputStream coded_output(&output);
747
748 coded_output.WriteRaw(kRawBytes, sizeof(kRawBytes));
749 EXPECT_FALSE(coded_output.HadError());
750
751 EXPECT_EQ(sizeof(kRawBytes), coded_output.ByteCount());
752 }
753
754 EXPECT_EQ(sizeof(kRawBytes), output.ByteCount());
755 EXPECT_EQ(0, memcmp(buffer_, kRawBytes, sizeof(kRawBytes)));
756 }
757
TEST_P(BlockSizes,ReadString)758 TEST_P(BlockSizes, ReadString) {
759 int kBlockSizes_case = GetParam();
760 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
761 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
762
763 {
764 CodedInputStream coded_input(&input);
765
766 std::string str;
767 EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
768 EXPECT_EQ(kRawBytes, str);
769 }
770
771 EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
772 }
773
774 // Check to make sure ReadString doesn't crash on impossibly large strings.
TEST_P(BlockSizes,ReadStringImpossiblyLarge)775 TEST_P(BlockSizes, ReadStringImpossiblyLarge) {
776 int kBlockSizes_case = GetParam();
777 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
778
779 {
780 CodedInputStream coded_input(&input);
781
782 std::string str;
783 // Try to read a gigabyte.
784 EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
785 }
786 }
787
TEST_F(CodedStreamTest,ReadStringImpossiblyLargeFromStringOnStack)788 TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnStack) {
789 // Same test as above, except directly use a buffer. This used to cause
790 // crashes while the above did not.
791 uint8_t buffer[8] = {};
792 CodedInputStream coded_input(buffer, 8);
793 std::string str;
794 EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
795 }
796
TEST_F(CodedStreamTest,ReadStringImpossiblyLargeFromStringOnHeap)797 TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnHeap) {
798 std::unique_ptr<uint8_t[]> buffer(new uint8_t[8]);
799 CodedInputStream coded_input(buffer.get(), 8);
800 std::string str;
801 EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
802 }
803
TEST_P(BlockSizes,ReadStringReservesMemoryOnTotalLimit)804 TEST_P(BlockSizes, ReadStringReservesMemoryOnTotalLimit) {
805 int kBlockSizes_case = GetParam();
806 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
807 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
808
809 {
810 CodedInputStream coded_input(&input);
811 coded_input.SetTotalBytesLimit(sizeof(kRawBytes));
812 EXPECT_EQ(sizeof(kRawBytes), coded_input.BytesUntilTotalBytesLimit());
813
814 std::string str;
815 EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
816 EXPECT_EQ(sizeof(kRawBytes) - strlen(kRawBytes),
817 coded_input.BytesUntilTotalBytesLimit());
818 EXPECT_EQ(kRawBytes, str);
819 // TODO: Replace with a more meaningful test (see cl/60966023).
820 EXPECT_GE(str.capacity(), strlen(kRawBytes));
821 }
822
823 EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
824 }
825
TEST_P(BlockSizes,ReadStringReservesMemoryOnPushedLimit)826 TEST_P(BlockSizes, ReadStringReservesMemoryOnPushedLimit) {
827 int kBlockSizes_case = GetParam();
828 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
829 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
830
831 {
832 CodedInputStream coded_input(&input);
833 coded_input.PushLimit(sizeof(buffer_));
834
835 std::string str;
836 EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
837 EXPECT_EQ(kRawBytes, str);
838 // TODO: Replace with a more meaningful test (see cl/60966023).
839 EXPECT_GE(str.capacity(), strlen(kRawBytes));
840 }
841
842 EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
843 }
844
TEST_F(CodedStreamTest,ReadStringNoReservationIfLimitsNotSet)845 TEST_F(CodedStreamTest, ReadStringNoReservationIfLimitsNotSet) {
846 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
847 // Buffer size in the input must be smaller than sizeof(kRawBytes),
848 // otherwise check against capacity will fail as ReadStringInline()
849 // will handle the reading and will reserve the memory as needed.
850 ArrayInputStream input(buffer_, sizeof(buffer_), 32);
851
852 {
853 CodedInputStream coded_input(&input);
854
855 std::string str;
856 EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
857 EXPECT_EQ(kRawBytes, str);
858 // Note: this check depends on string class implementation. It
859 // expects that string will allocate more than strlen(kRawBytes)
860 // if the content of kRawBytes is appended to string in small
861 // chunks.
862 // TODO: Replace with a more meaningful test (see cl/60966023).
863 EXPECT_GE(str.capacity(), strlen(kRawBytes));
864 }
865
866 EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
867 }
868
TEST_F(CodedStreamTest,ReadStringNoReservationSizeIsNegative)869 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsNegative) {
870 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
871 // Buffer size in the input must be smaller than sizeof(kRawBytes),
872 // otherwise check against capacity will fail as ReadStringInline()
873 // will handle the reading and will reserve the memory as needed.
874 ArrayInputStream input(buffer_, sizeof(buffer_), 32);
875
876 {
877 CodedInputStream coded_input(&input);
878 coded_input.PushLimit(sizeof(buffer_));
879
880 std::string str;
881 EXPECT_FALSE(coded_input.ReadString(&str, -1));
882 // Note: this check depends on string class implementation. It
883 // expects that string will always allocate the same amount of
884 // memory for an empty string.
885 EXPECT_EQ(std::string().capacity(), str.capacity());
886 }
887 }
888
TEST_F(CodedStreamTest,ReadStringNoReservationSizeIsLarge)889 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsLarge) {
890 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
891 // Buffer size in the input must be smaller than sizeof(kRawBytes),
892 // otherwise check against capacity will fail as ReadStringInline()
893 // will handle the reading and will reserve the memory as needed.
894 ArrayInputStream input(buffer_, sizeof(buffer_), 32);
895
896 {
897 CodedInputStream coded_input(&input);
898 coded_input.PushLimit(sizeof(buffer_));
899
900 std::string str;
901 EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
902 EXPECT_GT(1 << 30, str.capacity());
903 }
904 }
905
TEST_F(CodedStreamTest,ReadStringNoReservationSizeIsOverTheLimit)906 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheLimit) {
907 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
908 // Buffer size in the input must be smaller than sizeof(kRawBytes),
909 // otherwise check against capacity will fail as ReadStringInline()
910 // will handle the reading and will reserve the memory as needed.
911 ArrayInputStream input(buffer_, sizeof(buffer_), 32);
912
913 {
914 CodedInputStream coded_input(&input);
915 coded_input.PushLimit(16);
916
917 std::string str;
918 EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
919 // Note: this check depends on string class implementation. It
920 // expects that string will allocate less than strlen(kRawBytes)
921 // for an empty string.
922 EXPECT_GT(strlen(kRawBytes), str.capacity());
923 }
924 }
925
TEST_F(CodedStreamTest,ReadStringNoReservationSizeIsOverTheTotalBytesLimit)926 TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheTotalBytesLimit) {
927 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
928 // Buffer size in the input must be smaller than sizeof(kRawBytes),
929 // otherwise check against capacity will fail as ReadStringInline()
930 // will handle the reading and will reserve the memory as needed.
931 ArrayInputStream input(buffer_, sizeof(buffer_), 32);
932
933 {
934 CodedInputStream coded_input(&input);
935 coded_input.SetTotalBytesLimit(16);
936
937 std::string str;
938 EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
939 // Note: this check depends on string class implementation. It
940 // expects that string will allocate less than strlen(kRawBytes)
941 // for an empty string.
942 EXPECT_GT(strlen(kRawBytes), str.capacity());
943 }
944 }
945
TEST_F(CodedStreamTest,ReadStringNoReservationSizeIsOverTheClosestLimit_GlobalLimitIsCloser)946 TEST_F(CodedStreamTest,
947 ReadStringNoReservationSizeIsOverTheClosestLimit_GlobalLimitIsCloser) {
948 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
949 // Buffer size in the input must be smaller than sizeof(kRawBytes),
950 // otherwise check against capacity will fail as ReadStringInline()
951 // will handle the reading and will reserve the memory as needed.
952 ArrayInputStream input(buffer_, sizeof(buffer_), 32);
953
954 {
955 CodedInputStream coded_input(&input);
956 coded_input.PushLimit(sizeof(buffer_));
957 coded_input.SetTotalBytesLimit(16);
958
959 std::string str;
960 EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
961 // Note: this check depends on string class implementation. It
962 // expects that string will allocate less than strlen(kRawBytes)
963 // for an empty string.
964 EXPECT_GT(strlen(kRawBytes), str.capacity());
965 }
966 }
967
TEST_F(CodedStreamTest,ReadStringNoReservationSizeIsOverTheClosestLimit_LocalLimitIsCloser)968 TEST_F(CodedStreamTest,
969 ReadStringNoReservationSizeIsOverTheClosestLimit_LocalLimitIsCloser) {
970 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
971 // Buffer size in the input must be smaller than sizeof(kRawBytes),
972 // otherwise check against capacity will fail as ReadStringInline()
973 // will handle the reading and will reserve the memory as needed.
974 ArrayInputStream input(buffer_, sizeof(buffer_), 32);
975
976 {
977 CodedInputStream coded_input(&input);
978 coded_input.PushLimit(16);
979 coded_input.SetTotalBytesLimit(sizeof(buffer_));
980 EXPECT_EQ(sizeof(buffer_), coded_input.BytesUntilTotalBytesLimit());
981
982 std::string str;
983 EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
984 // Note: this check depends on string class implementation. It
985 // expects that string will allocate less than strlen(kRawBytes)
986 // for an empty string.
987 EXPECT_GT(strlen(kRawBytes), str.capacity());
988 }
989 }
990
991
992 // -------------------------------------------------------------------
993 // Cord reads and writes
994
995 class BlockSizesWithResetCords
996 : public CodedStreamTest,
997 public testing::WithParamInterface<std::tuple<int, bool>> {};
998
999 class ResetCords : public CodedStreamTest,
1000 public testing::WithParamInterface<bool> {};
1001
TEST_P(BlockSizes,ReadCord)1002 TEST_P(BlockSizes, ReadCord) {
1003 int kBlockSizes_case = GetParam();
1004 memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
1005 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
1006
1007 {
1008 CodedInputStream coded_input(&input);
1009
1010 absl::Cord cord;
1011 EXPECT_TRUE(coded_input.ReadCord(&cord, strlen(kRawBytes)));
1012 EXPECT_EQ(absl::Cord(kRawBytes), cord);
1013 }
1014
1015 EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
1016 }
1017
TEST_P(BlockSizes,ReadCordReuseCord)1018 TEST_P(BlockSizes, ReadCordReuseCord) {
1019 ASSERT_GT(sizeof(buffer_), 1362 * sizeof(kRawBytes));
1020 int kBlockSizes_case = GetParam();
1021 for (size_t i = 0; i < 1362; i++) {
1022 memcpy(buffer_ + i * sizeof(kRawBytes), kRawBytes, sizeof(kRawBytes));
1023 }
1024 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
1025
1026 int total_read = 0;
1027 {
1028 const char* buffer_str = reinterpret_cast<const char*>(buffer_);
1029 CodedInputStream coded_input(&input);
1030 static const int kSizes[] = {0, 1, 2, 3, 4, 5, 6, 7,
1031 8, 9, 10, 11, 12, 13, 14, 15,
1032 16, 17, 50, 100, 1023, 1024, 8000, 16000};
1033 int total_size = 0;
1034 std::vector<int> sizes;
1035 for (size_t i = 0; i < ABSL_ARRAYSIZE(kSizes); ++i) {
1036 sizes.push_back(kSizes[i]);
1037 total_size += kSizes[i];
1038 }
1039 ASSERT_GE(1362 * sizeof(kRawBytes), total_size * 2);
1040
1041 absl::Cord reused_cord;
1042 for (int pass = 0; pass < 2; ++pass) {
1043 for (size_t i = 0; i < sizes.size(); ++i) {
1044 const int size = sizes[i];
1045 EXPECT_TRUE(coded_input.ReadCord(&reused_cord, size));
1046 EXPECT_EQ(size, reused_cord.size());
1047 EXPECT_EQ(
1048 std::string(buffer_str + total_read, static_cast<size_t>(size)),
1049 std::string(reused_cord));
1050 total_read += size;
1051 }
1052 std::reverse(sizes.begin(), sizes.end()); // Second pass is in reverse.
1053 }
1054 }
1055 EXPECT_EQ(total_read, input.ByteCount());
1056 }
1057
TEST_P(BlockSizesWithResetCords,ReadCordWithLimit)1058 TEST_P(BlockSizesWithResetCords, ReadCordWithLimit) {
1059 int kBlockSizes_case = std::get<0>(GetParam());
1060 bool kResetCords_case = std::get<1>(GetParam());
1061 memcpy(buffer_, kRawBytes, strlen(kRawBytes));
1062 ArrayInputStream input(buffer_, strlen(kRawBytes), kBlockSizes_case);
1063 CodedInputStream coded_input(&input);
1064
1065 CodedInputStream::Limit limit = coded_input.PushLimit(10);
1066 absl::Cord cord;
1067 EXPECT_TRUE(coded_input.ReadCord(&cord, 5));
1068 EXPECT_EQ(5, coded_input.BytesUntilLimit());
1069 if (kResetCords_case) cord.Clear();
1070 EXPECT_TRUE(coded_input.ReadCord(&cord, 4));
1071 EXPECT_EQ(1, coded_input.BytesUntilLimit());
1072 if (kResetCords_case) cord.Clear();
1073 EXPECT_FALSE(coded_input.ReadCord(&cord, 2));
1074 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1075 EXPECT_EQ(1, cord.size());
1076
1077 coded_input.PopLimit(limit);
1078
1079 if (kResetCords_case) cord.Clear();
1080 EXPECT_TRUE(coded_input.ReadCord(&cord, strlen(kRawBytes) - 10));
1081 EXPECT_EQ(std::string(kRawBytes + 10), std::string(cord));
1082 }
1083
TEST_P(ResetCords,ReadLargeCord)1084 TEST_P(ResetCords, ReadLargeCord) {
1085 bool kResetCords_case = GetParam();
1086 absl::Cord large_cord;
1087 for (int i = 0; i < 1024; i++) {
1088 large_cord.Append(kRawBytes);
1089 }
1090 CordInputStream input(&large_cord);
1091
1092 {
1093 CodedInputStream coded_input(&input);
1094
1095 absl::Cord cord;
1096 if (!kResetCords_case) cord.Append(absl::string_view("value"));
1097 EXPECT_TRUE(
1098 coded_input.ReadCord(&cord, static_cast<int>(large_cord.size())));
1099 EXPECT_EQ(large_cord, cord);
1100 }
1101
1102 EXPECT_EQ(large_cord.size(), input.ByteCount());
1103 }
1104
1105 // Check to make sure ReadString doesn't crash on impossibly large strings.
TEST_P(BlockSizesWithResetCords,ReadCordImpossiblyLarge)1106 TEST_P(BlockSizesWithResetCords, ReadCordImpossiblyLarge) {
1107 int kBlockSizes_case = std::get<0>(GetParam());
1108 bool kResetCords_case = std::get<1>(GetParam());
1109 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
1110
1111 {
1112 CodedInputStream coded_input(&input);
1113
1114 absl::Cord cord;
1115 if (!kResetCords_case) cord.Append(absl::string_view("value"));
1116 // Try to read a gigabyte. This should fail because the input is only
1117 // sizeof(buffer_) bytes.
1118 EXPECT_FALSE(coded_input.ReadCord(&cord, 1 << 30));
1119 }
1120 }
1121
TEST_P(BlockSizes,WriteCord)1122 TEST_P(BlockSizes, WriteCord) {
1123 int kBlockSizes_case = GetParam();
1124 ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
1125
1126 {
1127 CodedOutputStream coded_output(&output);
1128
1129 absl::Cord cord(kRawBytes);
1130 coded_output.WriteCord(cord);
1131 EXPECT_FALSE(coded_output.HadError());
1132
1133 EXPECT_EQ(strlen(kRawBytes), coded_output.ByteCount());
1134 }
1135
1136 EXPECT_EQ(strlen(kRawBytes), output.ByteCount());
1137 EXPECT_EQ(0, memcmp(buffer_, kRawBytes, strlen(kRawBytes)));
1138 }
1139
TEST_F(CodedStreamTest,WriteLargeCord)1140 TEST_F(CodedStreamTest, WriteLargeCord) {
1141 absl::Cord large_cord;
1142 for (int i = 0; i < 1024; i++) {
1143 large_cord.Append(kRawBytes);
1144 }
1145
1146 CordOutputStream output;
1147 {
1148 CodedOutputStream coded_output(&output);
1149
1150 coded_output.WriteCord(large_cord);
1151 EXPECT_FALSE(coded_output.HadError());
1152
1153 EXPECT_EQ(large_cord.size(), coded_output.ByteCount());
1154 EXPECT_EQ(large_cord.size(), output.ByteCount());
1155 }
1156 absl::Cord output_cord = output.Consume();
1157 EXPECT_EQ(large_cord, output_cord);
1158 }
1159
TEST_F(CodedStreamTest,Trim)1160 TEST_F(CodedStreamTest, Trim) {
1161 CordOutputStream cord_output;
1162 CodedOutputStream coded_output(&cord_output);
1163
1164 // Verify that any initially reserved output buffers created when the output
1165 // streams were created are trimmed on an initial Trim call.
1166 coded_output.Trim();
1167 EXPECT_EQ(0, coded_output.ByteCount());
1168
1169 // Write a single byte to the coded stream, ensure the cord stream has been
1170 // advanced, and then verify Trim() does the right thing.
1171 const char kTestData[] = "abcdef";
1172 coded_output.WriteRaw(kTestData, 1);
1173 coded_output.Trim();
1174 EXPECT_EQ(1, coded_output.ByteCount());
1175
1176 // Write some more data to the coded stream, Trim() it, and verify
1177 // everything behaves as expected.
1178 coded_output.WriteRaw(kTestData, sizeof(kTestData));
1179 coded_output.Trim();
1180 EXPECT_EQ(1 + sizeof(kTestData), coded_output.ByteCount());
1181
1182 absl::Cord cord = cord_output.Consume();
1183 EXPECT_EQ(1 + sizeof(kTestData), cord.size());
1184 }
1185
1186
1187 // -------------------------------------------------------------------
1188 // Skip
1189
1190 const char kSkipTestBytes[] =
1191 "<Before skipping><To be skipped><After skipping>";
1192
TEST_P(BlockSizes,SkipInput)1193 TEST_P(BlockSizes, SkipInput) {
1194 int kBlockSizes_case = GetParam();
1195 memcpy(buffer_, kSkipTestBytes, sizeof(kSkipTestBytes));
1196 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
1197
1198 {
1199 CodedInputStream coded_input(&input);
1200
1201 std::string str;
1202 EXPECT_TRUE(coded_input.ReadString(&str, strlen("<Before skipping>")));
1203 EXPECT_EQ("<Before skipping>", str);
1204 EXPECT_TRUE(coded_input.Skip(strlen("<To be skipped>")));
1205 EXPECT_TRUE(coded_input.ReadString(&str, strlen("<After skipping>")));
1206 EXPECT_EQ("<After skipping>", str);
1207 }
1208
1209 EXPECT_EQ(strlen(kSkipTestBytes), input.ByteCount());
1210 }
1211
1212 // -------------------------------------------------------------------
1213 // GetDirectBufferPointer
1214
TEST_F(CodedStreamTest,GetDirectBufferPointerInput)1215 TEST_F(CodedStreamTest, GetDirectBufferPointerInput) {
1216 ArrayInputStream input(buffer_, sizeof(buffer_), 8);
1217 CodedInputStream coded_input(&input);
1218
1219 const void* ptr;
1220 int size;
1221
1222 EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
1223 EXPECT_EQ(buffer_, ptr);
1224 EXPECT_EQ(8, size);
1225
1226 // Peeking again should return the same pointer.
1227 EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
1228 EXPECT_EQ(buffer_, ptr);
1229 EXPECT_EQ(8, size);
1230
1231 // Skip forward in the same buffer then peek again.
1232 EXPECT_TRUE(coded_input.Skip(3));
1233 EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
1234 EXPECT_EQ(buffer_ + 3, ptr);
1235 EXPECT_EQ(5, size);
1236
1237 // Skip to end of buffer and peek -- should get next buffer.
1238 EXPECT_TRUE(coded_input.Skip(5));
1239 EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
1240 EXPECT_EQ(buffer_ + 8, ptr);
1241 EXPECT_EQ(8, size);
1242 }
1243
TEST_F(CodedStreamTest,GetDirectBufferPointerInlineInput)1244 TEST_F(CodedStreamTest, GetDirectBufferPointerInlineInput) {
1245 ArrayInputStream input(buffer_, sizeof(buffer_), 8);
1246 CodedInputStream coded_input(&input);
1247
1248 const void* ptr;
1249 int size;
1250
1251 coded_input.GetDirectBufferPointerInline(&ptr, &size);
1252 EXPECT_EQ(buffer_, ptr);
1253 EXPECT_EQ(8, size);
1254
1255 // Peeking again should return the same pointer.
1256 coded_input.GetDirectBufferPointerInline(&ptr, &size);
1257 EXPECT_EQ(buffer_, ptr);
1258 EXPECT_EQ(8, size);
1259
1260 // Skip forward in the same buffer then peek again.
1261 EXPECT_TRUE(coded_input.Skip(3));
1262 coded_input.GetDirectBufferPointerInline(&ptr, &size);
1263 EXPECT_EQ(buffer_ + 3, ptr);
1264 EXPECT_EQ(5, size);
1265
1266 // Skip to end of buffer and peek -- should return false and provide an empty
1267 // buffer. It does not try to Refresh().
1268 EXPECT_TRUE(coded_input.Skip(5));
1269 coded_input.GetDirectBufferPointerInline(&ptr, &size);
1270 EXPECT_EQ(buffer_ + 8, ptr);
1271 EXPECT_EQ(0, size);
1272 }
1273
1274 // -------------------------------------------------------------------
1275 // Limits
1276
TEST_P(BlockSizes,BasicLimit)1277 TEST_P(BlockSizes, BasicLimit) {
1278 int kBlockSizes_case = GetParam();
1279 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
1280
1281 {
1282 CodedInputStream coded_input(&input);
1283
1284 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1285 CodedInputStream::Limit limit = coded_input.PushLimit(8);
1286
1287 // Read until we hit the limit.
1288 uint32_t value;
1289 EXPECT_EQ(8, coded_input.BytesUntilLimit());
1290 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1291 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1292 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1293 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1294 EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
1295 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1296
1297 coded_input.PopLimit(limit);
1298
1299 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1300 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1301 }
1302
1303 EXPECT_EQ(12, input.ByteCount());
1304 }
1305
1306 // Test what happens when we push two limits where the second (top) one is
1307 // shorter.
TEST_P(BlockSizes,SmallLimitOnTopOfBigLimit)1308 TEST_P(BlockSizes, SmallLimitOnTopOfBigLimit) {
1309 int kBlockSizes_case = GetParam();
1310 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
1311
1312 {
1313 CodedInputStream coded_input(&input);
1314
1315 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1316 CodedInputStream::Limit limit1 = coded_input.PushLimit(8);
1317 EXPECT_EQ(8, coded_input.BytesUntilLimit());
1318 CodedInputStream::Limit limit2 = coded_input.PushLimit(4);
1319
1320 uint32_t value;
1321
1322 // Read until we hit limit2, the top and shortest limit.
1323 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1324 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1325 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1326 EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
1327 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1328
1329 coded_input.PopLimit(limit2);
1330
1331 // Read until we hit limit1.
1332 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1333 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1334 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1335 EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
1336 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1337
1338 coded_input.PopLimit(limit1);
1339
1340 // No more limits.
1341 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1342 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1343 }
1344
1345 EXPECT_EQ(12, input.ByteCount());
1346 }
1347
1348 // Test what happens when we push two limits where the second (top) one is
1349 // longer. In this case, the top limit is shortened to match the previous
1350 // limit.
TEST_P(BlockSizes,BigLimitOnTopOfSmallLimit)1351 TEST_P(BlockSizes, BigLimitOnTopOfSmallLimit) {
1352 int kBlockSizes_case = GetParam();
1353 ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
1354
1355 {
1356 CodedInputStream coded_input(&input);
1357
1358 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1359 CodedInputStream::Limit limit1 = coded_input.PushLimit(4);
1360 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1361 CodedInputStream::Limit limit2 = coded_input.PushLimit(8);
1362
1363 uint32_t value;
1364
1365 // Read until we hit limit2. Except, wait! limit1 is shorter, so
1366 // we end up hitting that first, despite having 4 bytes to go on
1367 // limit2.
1368 EXPECT_EQ(4, coded_input.BytesUntilLimit());
1369 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1370 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1371 EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
1372 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1373
1374 coded_input.PopLimit(limit2);
1375
1376 // OK, popped limit2, now limit1 is on top, which we've already hit.
1377 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1378 EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
1379 EXPECT_EQ(0, coded_input.BytesUntilLimit());
1380
1381 coded_input.PopLimit(limit1);
1382
1383 // No more limits.
1384 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1385 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1386 }
1387
1388 EXPECT_EQ(8, input.ByteCount());
1389 }
1390
TEST_F(CodedStreamTest,ExpectAtEnd)1391 TEST_F(CodedStreamTest, ExpectAtEnd) {
1392 // Test ExpectAtEnd(), which is based on limits.
1393 ArrayInputStream input(buffer_, sizeof(buffer_));
1394 CodedInputStream coded_input(&input);
1395
1396 EXPECT_FALSE(coded_input.ExpectAtEnd());
1397
1398 CodedInputStream::Limit limit = coded_input.PushLimit(4);
1399
1400 uint32_t value;
1401 EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
1402 EXPECT_TRUE(coded_input.ExpectAtEnd());
1403
1404 coded_input.PopLimit(limit);
1405 EXPECT_FALSE(coded_input.ExpectAtEnd());
1406 }
1407
TEST_F(CodedStreamTest,NegativeLimit)1408 TEST_F(CodedStreamTest, NegativeLimit) {
1409 // Check what happens when we push a negative limit.
1410 ArrayInputStream input(buffer_, sizeof(buffer_));
1411 CodedInputStream coded_input(&input);
1412
1413 CodedInputStream::Limit limit = coded_input.PushLimit(-1234);
1414 // BytesUntilLimit() returns -1 to mean "no limit", which actually means
1415 // "the limit is INT_MAX relative to the beginning of the stream".
1416 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1417 coded_input.PopLimit(limit);
1418 }
1419
TEST_F(CodedStreamTest,NegativeLimitAfterReading)1420 TEST_F(CodedStreamTest, NegativeLimitAfterReading) {
1421 // Check what happens when we push a negative limit.
1422 ArrayInputStream input(buffer_, sizeof(buffer_));
1423 CodedInputStream coded_input(&input);
1424 ASSERT_TRUE(coded_input.Skip(128));
1425
1426 CodedInputStream::Limit limit = coded_input.PushLimit(-64);
1427 // BytesUntilLimit() returns -1 to mean "no limit", which actually means
1428 // "the limit is INT_MAX relative to the beginning of the stream".
1429 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1430 coded_input.PopLimit(limit);
1431 }
1432
TEST_F(CodedStreamTest,OverflowLimit)1433 TEST_F(CodedStreamTest, OverflowLimit) {
1434 // Check what happens when we push a limit large enough that its absolute
1435 // position is more than 2GB into the stream.
1436 ArrayInputStream input(buffer_, sizeof(buffer_));
1437 CodedInputStream coded_input(&input);
1438 ASSERT_TRUE(coded_input.Skip(128));
1439
1440 CodedInputStream::Limit limit = coded_input.PushLimit(INT_MAX);
1441 // BytesUntilLimit() returns -1 to mean "no limit", which actually means
1442 // "the limit is INT_MAX relative to the beginning of the stream".
1443 EXPECT_EQ(-1, coded_input.BytesUntilLimit());
1444 coded_input.PopLimit(limit);
1445 }
1446
TEST_F(CodedStreamTest,TotalBytesLimit)1447 TEST_F(CodedStreamTest, TotalBytesLimit) {
1448 ArrayInputStream input(buffer_, sizeof(buffer_));
1449 CodedInputStream coded_input(&input);
1450 coded_input.SetTotalBytesLimit(16);
1451 EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
1452
1453 std::string str;
1454 EXPECT_TRUE(coded_input.ReadString(&str, 16));
1455 EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
1456
1457 {
1458 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
1459 EXPECT_CALL(
1460 log,
1461 Log(absl::LogSeverity::kError, testing::_,
1462 testing::HasSubstr(
1463 "A protocol message was rejected because it was too big")));
1464 log.StartCapturingLogs();
1465 EXPECT_FALSE(coded_input.ReadString(&str, 1));
1466 }
1467
1468 coded_input.SetTotalBytesLimit(32);
1469 EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
1470 EXPECT_TRUE(coded_input.ReadString(&str, 16));
1471 EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
1472 }
1473
TEST_F(CodedStreamTest,TotalBytesLimitNotValidMessageEnd)1474 TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) {
1475 // total_bytes_limit_ is not a valid place for a message to end.
1476
1477 ArrayInputStream input(buffer_, sizeof(buffer_));
1478 CodedInputStream coded_input(&input);
1479
1480 // Set both total_bytes_limit and a regular limit at 16 bytes.
1481 coded_input.SetTotalBytesLimit(16);
1482 CodedInputStream::Limit limit = coded_input.PushLimit(16);
1483
1484 // Read 16 bytes.
1485 std::string str;
1486 EXPECT_TRUE(coded_input.ReadString(&str, 16));
1487
1488 // Read a tag. Should fail, but report being a valid endpoint since it's
1489 // a regular limit.
1490 EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
1491 EXPECT_TRUE(coded_input.ConsumedEntireMessage());
1492
1493 // Pop the limit.
1494 coded_input.PopLimit(limit);
1495
1496 // Read a tag. Should fail, and report *not* being a valid endpoint, since
1497 // this time we're hitting the total bytes limit.
1498 EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
1499 EXPECT_FALSE(coded_input.ConsumedEntireMessage());
1500 }
1501
TEST_F(CodedStreamTest,RecursionLimit)1502 TEST_F(CodedStreamTest, RecursionLimit) {
1503 ArrayInputStream input(buffer_, sizeof(buffer_));
1504 CodedInputStream coded_input(&input);
1505 coded_input.SetRecursionLimit(4);
1506
1507 // This is way too much testing for a counter.
1508 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1
1509 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2
1510 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3
1511 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4
1512 EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5
1513 EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6
1514 coded_input.DecrementRecursionDepth(); // 5
1515 EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6
1516 coded_input.DecrementRecursionDepth(); // 5
1517 coded_input.DecrementRecursionDepth(); // 4
1518 coded_input.DecrementRecursionDepth(); // 3
1519 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4
1520 EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5
1521 coded_input.DecrementRecursionDepth(); // 4
1522 coded_input.DecrementRecursionDepth(); // 3
1523 coded_input.DecrementRecursionDepth(); // 2
1524 coded_input.DecrementRecursionDepth(); // 1
1525 coded_input.DecrementRecursionDepth(); // 0
1526 coded_input.DecrementRecursionDepth(); // 0
1527 coded_input.DecrementRecursionDepth(); // 0
1528 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1
1529 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2
1530 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3
1531 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4
1532 EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5
1533
1534 coded_input.SetRecursionLimit(6);
1535 EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 6
1536 EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 7
1537 }
1538
1539
1540 class ReallyBigInputStream : public ZeroCopyInputStream {
1541 public:
ReallyBigInputStream()1542 ReallyBigInputStream() : backup_amount_(0), buffer_count_(0) {}
1543
1544 // implements ZeroCopyInputStream ----------------------------------
Next(const void ** data,int * size)1545 bool Next(const void** data, int* size) override {
1546 // We only expect BackUp() to be called at the end.
1547 EXPECT_EQ(0, backup_amount_);
1548
1549 switch (buffer_count_++) {
1550 case 0:
1551 *data = buffer_;
1552 *size = sizeof(buffer_);
1553 return true;
1554 case 1:
1555 // Return an enormously large buffer that, when combined with the 1k
1556 // returned already, should overflow the total_bytes_read_ counter in
1557 // CodedInputStream. Note that we'll only read the first 1024 bytes
1558 // of this buffer so it's OK that we have it point at buffer_.
1559 *data = buffer_;
1560 *size = INT_MAX;
1561 return true;
1562 default:
1563 return false;
1564 }
1565 }
1566
BackUp(int count)1567 void BackUp(int count) override { backup_amount_ = count; }
1568
Skip(int count)1569 bool Skip(int count) override {
1570 ABSL_LOG(FATAL) << "Not implemented.";
1571 return false;
1572 }
ByteCount() const1573 int64_t ByteCount() const override {
1574 ABSL_LOG(FATAL) << "Not implemented.";
1575 return 0;
1576 }
1577
1578 int backup_amount_;
1579
1580 private:
1581 char buffer_[1024] = {};
1582 int64_t buffer_count_;
1583 };
1584
TEST_F(CodedStreamTest,InputOver2G)1585 TEST_F(CodedStreamTest, InputOver2G) {
1586 // CodedInputStream should gracefully handle input over 2G and call
1587 // input.BackUp() with the correct number of bytes on destruction.
1588 ReallyBigInputStream input;
1589
1590 {
1591 absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
1592 EXPECT_CALL(log, Log(absl::LogSeverity::kError, testing::_, testing::_))
1593 .Times(0);
1594 log.StartCapturingLogs();
1595 CodedInputStream coded_input(&input);
1596 std::string str;
1597 EXPECT_TRUE(coded_input.ReadString(&str, 512));
1598 EXPECT_TRUE(coded_input.ReadString(&str, 1024));
1599 }
1600
1601 EXPECT_EQ(INT_MAX - 512, input.backup_amount_);
1602 }
1603
1604 INSTANTIATE_TEST_SUITE_P(
1605 CodedStreamUnitTest, ResetCords, testing::ValuesIn(kResetCords),
__anon2153cf6b0202(const testing::TestParamInfo<ResetCords::ParamType>& param_info) 1606 [](const testing::TestParamInfo<ResetCords::ParamType>& param_info) {
1607 return absl::StrCat("ResetCords_", param_info.param ? "true" : "false");
1608 });
1609 INSTANTIATE_TEST_SUITE_P(
1610 CodedStreamUnitTest, BlockSizesWithResetCords,
1611 testing::Combine(testing::ValuesIn(kBlockSizes),
1612 testing::ValuesIn(kResetCords)),
1613 [](const testing::TestParamInfo<BlockSizesWithResetCords::ParamType>&
__anon2153cf6b0302(const testing::TestParamInfo<BlockSizesWithResetCords::ParamType>& param_info) 1614 param_info) {
1615 return absl::StrCat("BlockSize_", std::get<0>(param_info.param),
1616 "_ResetCords_",
1617 std::get<1>(param_info.param) ? "true" : "false");
1618 });
1619 INSTANTIATE_TEST_SUITE_P(
1620 CodedStreamUnitTest, VarintErrorCasesWithSizes,
1621 testing::Combine(testing::ValuesIn(kVarintErrorCases),
1622 testing::ValuesIn(kBlockSizes)),
1623 [](const testing::TestParamInfo<VarintErrorCasesWithSizes::ParamType>&
__anon2153cf6b0402(const testing::TestParamInfo<VarintErrorCasesWithSizes::ParamType>& param_info) 1624 param_info) {
1625 return absl::StrCat("VarintErrorCase_",
1626 std::get<0>(param_info.param).name, "_BlockSize_",
1627 std::get<1>(param_info.param));
1628 });
1629 INSTANTIATE_TEST_SUITE_P(
1630 CodedStreamUnitTest, VarintSizeCases, testing::ValuesIn(kVarintSizeCases),
__anon2153cf6b0502(const testing::TestParamInfo<VarintSizeCases::ParamType>& param_info) 1631 [](const testing::TestParamInfo<VarintSizeCases::ParamType>& param_info) {
1632 return absl::StrCat("VarintSizeCase_Value_", param_info.param.value);
1633 });
1634 INSTANTIATE_TEST_SUITE_P(
1635 CodedStreamUnitTest, VarintCases, testing::ValuesIn(kVarintCases),
__anon2153cf6b0602(const testing::TestParamInfo<VarintCases::ParamType>& param_info) 1636 [](const testing::TestParamInfo<VarintCases::ParamType>& param_info) {
1637 return absl::StrCat("VarintCase_Value_", param_info.param.value);
1638 });
1639 INSTANTIATE_TEST_SUITE_P(
1640 CodedStreamUnitTest, VarintCasesWithSizes,
1641 testing::Combine(testing::ValuesIn(kVarintCases),
1642 testing::ValuesIn(kBlockSizes)),
1643 [](const testing::TestParamInfo<VarintCasesWithSizes::ParamType>&
__anon2153cf6b0702(const testing::TestParamInfo<VarintCasesWithSizes::ParamType>& param_info) 1644 param_info) {
1645 return absl::StrCat("VarintCase_Value_",
1646 std::get<0>(param_info.param).value, "_BlockSize_",
1647 std::get<1>(param_info.param));
1648 });
1649 INSTANTIATE_TEST_SUITE_P(
1650 CodedStreamUnitTest, BlockSizes, testing::ValuesIn(kBlockSizes),
__anon2153cf6b0802(const testing::TestParamInfo<BlockSizes::ParamType>& param_info) 1651 [](const testing::TestParamInfo<BlockSizes::ParamType>& param_info) {
1652 return absl::StrCat("BlockSize_", param_info.param);
1653 });
1654 INSTANTIATE_TEST_SUITE_P(
1655 CodedStreamUnitTest, SignedVarintCasesWithSizes,
1656 testing::Combine(testing::ValuesIn(kSignExtendedVarintCases),
1657 testing::ValuesIn(kBlockSizes)),
1658 [](const testing::TestParamInfo<SignedVarintCasesWithSizes::ParamType>&
__anon2153cf6b0902(const testing::TestParamInfo<SignedVarintCasesWithSizes::ParamType>& param_info) 1659 param_info) {
1660 return absl::StrCat("SignedVarintCase_Value_",
1661 std::get<0>(param_info.param) < 0 ? "_Negative_" : "",
1662 std::abs(std::get<0>(param_info.param)),
1663 "_BlockSize_", std::get<1>(param_info.param));
1664 });
1665 INSTANTIATE_TEST_SUITE_P(
1666 CodedStreamUnitTest, Fixed16Cases, testing::ValuesIn(kFixed16Cases),
__anon2153cf6b0a02(const testing::TestParamInfo<Fixed16Cases::ParamType>& param_info) 1667 [](const testing::TestParamInfo<Fixed16Cases::ParamType>& param_info) {
1668 return absl::StrCat("Fixed16Case_Value_", param_info.param.value);
1669 });
1670 INSTANTIATE_TEST_SUITE_P(
1671 CodedStreamUnitTest, Fixed32Cases, testing::ValuesIn(kFixed32Cases),
__anon2153cf6b0b02(const testing::TestParamInfo<Fixed32Cases::ParamType>& param_info) 1672 [](const testing::TestParamInfo<Fixed32Cases::ParamType>& param_info) {
1673 return absl::StrCat("Fixed32Case_Value_", param_info.param.value);
1674 });
1675 INSTANTIATE_TEST_SUITE_P(
1676 CodedStreamUnitTest, Fixed64Cases, testing::ValuesIn(kFixed64Cases),
__anon2153cf6b0c02(const testing::TestParamInfo<Fixed64Cases::ParamType>& param_info) 1677 [](const testing::TestParamInfo<Fixed64Cases::ParamType>& param_info) {
1678 return absl::StrCat("Fixed64Case_Value_", param_info.param.value);
1679 });
1680 INSTANTIATE_TEST_SUITE_P(
1681 CodedStreamUnitTest, Fixed16CasesWithSizes,
1682 testing::Combine(testing::ValuesIn(kFixed16Cases),
1683 testing::ValuesIn(kBlockSizes)),
1684 [](const testing::TestParamInfo<Fixed16CasesWithSizes::ParamType>&
__anon2153cf6b0d02(const testing::TestParamInfo<Fixed16CasesWithSizes::ParamType>& param_info) 1685 param_info) {
1686 return absl::StrCat("Fixed16Case_Value_",
1687 std::get<0>(param_info.param).value, "_BlockSize_",
1688 std::get<1>(param_info.param));
1689 });
1690 INSTANTIATE_TEST_SUITE_P(
1691 CodedStreamUnitTest, Fixed32CasesWithSizes,
1692 testing::Combine(testing::ValuesIn(kFixed32Cases),
1693 testing::ValuesIn(kBlockSizes)),
1694 [](const testing::TestParamInfo<Fixed32CasesWithSizes::ParamType>&
__anon2153cf6b0e02(const testing::TestParamInfo<Fixed32CasesWithSizes::ParamType>& param_info) 1695 param_info) {
1696 return absl::StrCat("Fixed32Case_Value_",
1697 std::get<0>(param_info.param).value, "_BlockSize_",
1698 std::get<1>(param_info.param));
1699 });
1700 INSTANTIATE_TEST_SUITE_P(
1701 CodedStreamUnitTest, Fixed64CasesWithSizes,
1702 testing::Combine(testing::ValuesIn(kFixed64Cases),
1703 testing::ValuesIn(kBlockSizes)),
1704 [](const testing::TestParamInfo<Fixed64CasesWithSizes::ParamType>&
__anon2153cf6b0f02(const testing::TestParamInfo<Fixed64CasesWithSizes::ParamType>& param_info) 1705 param_info) {
1706 return absl::StrCat("Fixed64Case_Value_",
1707 std::get<0>(param_info.param).value, "_BlockSize_",
1708 std::get<1>(param_info.param));
1709 });
1710
1711 } // namespace
1712 } // namespace io
1713 } // namespace protobuf
1714 } // namespace google
1715
1716 #include "google/protobuf/port_undef.inc"
1717