coded_stream_unittest.cc 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file contains tests and benchmarks.
  35. #include <memory>
  36. #include <vector>
  37. #include <google/protobuf/io/coded_stream.h>
  38. #include <limits.h>
  39. #include <google/protobuf/stubs/common.h>
  40. #include <google/protobuf/stubs/logging.h>
  41. #include <google/protobuf/stubs/logging.h>
  42. #include <google/protobuf/testing/googletest.h>
  43. #include <gtest/gtest.h>
  44. #include <google/protobuf/io/zero_copy_stream_impl.h>
  45. // This declares an unsigned long long integer literal in a portable way.
  46. // (The original macro is way too big and ruins my formatting.)
  47. #undef ULL
  48. #define ULL(x) GOOGLE_ULONGLONG(x)
  49. namespace google {
  50. namespace protobuf {
  51. namespace io {
  52. namespace {
  53. // ===================================================================
  54. // Data-Driven Test Infrastructure
  55. // TEST_1D and TEST_2D are macros I'd eventually like to see added to
  56. // gTest. These macros can be used to declare tests which should be
  57. // run multiple times, once for each item in some input array. TEST_1D
  58. // tests all cases in a single input array. TEST_2D tests all
  59. // combinations of cases from two arrays. The arrays must be statically
  60. // defined such that the GOOGLE_ARRAYSIZE() macro works on them. Example:
  61. //
  62. // int kCases[] = {1, 2, 3, 4}
  63. // TEST_1D(MyFixture, MyTest, kCases) {
  64. // EXPECT_GT(kCases_case, 0);
  65. // }
  66. //
  67. // This test iterates through the numbers 1, 2, 3, and 4 and tests that
  68. // they are all grater than zero. In case of failure, the exact case
  69. // which failed will be printed. The case type must be printable using
  70. // ostream::operator<<.
  71. // TODO(kenton): gTest now supports "parameterized tests" which would be
  72. // a better way to accomplish this. Rewrite when time permits.
  73. #define TEST_1D(FIXTURE, NAME, CASES) \
  74. class FIXTURE##_##NAME##_DD : public FIXTURE { \
  75. protected: \
  76. template <typename CaseType> \
  77. void DoSingleCase(const CaseType& CASES##_case); \
  78. }; \
  79. \
  80. TEST_F(FIXTURE##_##NAME##_DD, NAME) { \
  81. for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES); i++) { \
  82. SCOPED_TRACE(testing::Message() \
  83. << #CASES " case #" << i << ": " << CASES[i]); \
  84. DoSingleCase(CASES[i]); \
  85. } \
  86. } \
  87. \
  88. template <typename CaseType> \
  89. void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType& CASES##_case)
  90. #define TEST_2D(FIXTURE, NAME, CASES1, CASES2) \
  91. class FIXTURE##_##NAME##_DD : public FIXTURE { \
  92. protected: \
  93. template <typename CaseType1, typename CaseType2> \
  94. void DoSingleCase(const CaseType1& CASES1##_case, \
  95. const CaseType2& CASES2##_case); \
  96. }; \
  97. \
  98. TEST_F(FIXTURE##_##NAME##_DD, NAME) { \
  99. for (int i = 0; i < GOOGLE_ARRAYSIZE(CASES1); i++) { \
  100. for (int j = 0; j < GOOGLE_ARRAYSIZE(CASES2); j++) { \
  101. SCOPED_TRACE(testing::Message() \
  102. << #CASES1 " case #" << i << ": " << CASES1[i] << ", " \
  103. << #CASES2 " case #" << j << ": " << CASES2[j]); \
  104. DoSingleCase(CASES1[i], CASES2[j]); \
  105. } \
  106. } \
  107. } \
  108. \
  109. template <typename CaseType1, typename CaseType2> \
  110. void FIXTURE##_##NAME##_DD::DoSingleCase(const CaseType1& CASES1##_case, \
  111. const CaseType2& CASES2##_case)
  112. // ===================================================================
  113. class CodedStreamTest : public testing::Test {
  114. protected:
  115. // Helper method used by tests for bytes warning. See implementation comment
  116. // for further information.
  117. static void SetupTotalBytesLimitWarningTest(
  118. int total_bytes_limit, int warning_threshold,
  119. std::vector<string>* out_errors, std::vector<string>* out_warnings);
  120. // Buffer used during most of the tests. This assumes tests run sequentially.
  121. static const int kBufferSize = 1024 * 64;
  122. static uint8 buffer_[kBufferSize];
  123. };
  124. uint8 CodedStreamTest::buffer_[CodedStreamTest::kBufferSize];
  125. // We test each operation over a variety of block sizes to insure that
  126. // we test cases where reads or writes cross buffer boundaries, cases
  127. // where they don't, and cases where there is so much buffer left that
  128. // we can use special optimized paths that don't worry about bounds
  129. // checks.
  130. const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024};
  131. // -------------------------------------------------------------------
  132. // Varint tests.
  133. struct VarintCase {
  134. uint8 bytes[10]; // Encoded bytes.
  135. int size; // Encoded size, in bytes.
  136. uint64 value; // Parsed value.
  137. };
  138. inline std::ostream& operator<<(std::ostream& os, const VarintCase& c) {
  139. return os << c.value;
  140. }
  141. VarintCase kVarintCases[] = {
  142. // 32-bit values
  143. {{0x00} , 1, 0},
  144. {{0x01} , 1, 1},
  145. {{0x7f} , 1, 127},
  146. {{0xa2, 0x74}, 2, (0x22 << 0) | (0x74 << 7)}, // 14882
  147. {{0xbe, 0xf7, 0x92, 0x84, 0x0b}, 5, // 2961488830
  148. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  149. (ULL(0x0b) << 28)},
  150. // 64-bit
  151. {{0xbe, 0xf7, 0x92, 0x84, 0x1b}, 5, // 7256456126
  152. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  153. (ULL(0x1b) << 28)},
  154. {{0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49}, 8, // 41256202580718336
  155. (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
  156. (ULL(0x43) << 28) | (ULL(0x49) << 35) | (ULL(0x24) << 42) |
  157. (ULL(0x49) << 49)},
  158. // 11964378330978735131
  159. {{0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01}, 10,
  160. (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
  161. (ULL(0x3b) << 28) | (ULL(0x56) << 35) | (ULL(0x00) << 42) |
  162. (ULL(0x05) << 49) | (ULL(0x26) << 56) | (ULL(0x01) << 63)},
  163. };
  164. TEST_2D(CodedStreamTest, ReadVarint32, kVarintCases, kBlockSizes) {
  165. memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
  166. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  167. {
  168. CodedInputStream coded_input(&input);
  169. uint32 value;
  170. EXPECT_TRUE(coded_input.ReadVarint32(&value));
  171. EXPECT_EQ(static_cast<uint32>(kVarintCases_case.value), value);
  172. }
  173. EXPECT_EQ(kVarintCases_case.size, input.ByteCount());
  174. }
  175. TEST_2D(CodedStreamTest, ReadTag, kVarintCases, kBlockSizes) {
  176. memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
  177. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  178. {
  179. CodedInputStream coded_input(&input);
  180. uint32 expected_value = static_cast<uint32>(kVarintCases_case.value);
  181. EXPECT_EQ(expected_value, coded_input.ReadTag());
  182. EXPECT_TRUE(coded_input.LastTagWas(expected_value));
  183. EXPECT_FALSE(coded_input.LastTagWas(expected_value + 1));
  184. }
  185. EXPECT_EQ(kVarintCases_case.size, input.ByteCount());
  186. }
  187. // This is the regression test that verifies that there is no issues
  188. // with the empty input buffers handling.
  189. TEST_F(CodedStreamTest, EmptyInputBeforeEos) {
  190. class In : public ZeroCopyInputStream {
  191. public:
  192. In() : count_(0) {}
  193. private:
  194. virtual bool Next(const void** data, int* size) {
  195. *data = NULL;
  196. *size = 0;
  197. return count_++ < 2;
  198. }
  199. virtual void BackUp(int count) {
  200. GOOGLE_LOG(FATAL) << "Tests never call this.";
  201. }
  202. virtual bool Skip(int count) {
  203. GOOGLE_LOG(FATAL) << "Tests never call this.";
  204. return false;
  205. }
  206. virtual int64 ByteCount() const { return 0; }
  207. int count_;
  208. } in;
  209. CodedInputStream input(&in);
  210. input.ReadTagNoLastTag();
  211. EXPECT_TRUE(input.ConsumedEntireMessage());
  212. }
  213. TEST_1D(CodedStreamTest, ExpectTag, kVarintCases) {
  214. // Leave one byte at the beginning of the buffer so we can read it
  215. // to force the first buffer to be loaded.
  216. buffer_[0] = '\0';
  217. memcpy(buffer_ + 1, kVarintCases_case.bytes, kVarintCases_case.size);
  218. ArrayInputStream input(buffer_, sizeof(buffer_));
  219. {
  220. CodedInputStream coded_input(&input);
  221. // Read one byte to force coded_input.Refill() to be called. Otherwise,
  222. // ExpectTag() will return a false negative.
  223. uint8 dummy;
  224. coded_input.ReadRaw(&dummy, 1);
  225. EXPECT_EQ((uint)'\0', (uint)dummy);
  226. uint32 expected_value = static_cast<uint32>(kVarintCases_case.value);
  227. // ExpectTag() produces false negatives for large values.
  228. if (kVarintCases_case.size <= 2) {
  229. EXPECT_FALSE(coded_input.ExpectTag(expected_value + 1));
  230. EXPECT_TRUE(coded_input.ExpectTag(expected_value));
  231. } else {
  232. EXPECT_FALSE(coded_input.ExpectTag(expected_value));
  233. }
  234. }
  235. if (kVarintCases_case.size <= 2) {
  236. EXPECT_EQ(kVarintCases_case.size + 1, input.ByteCount());
  237. } else {
  238. EXPECT_EQ(1, input.ByteCount());
  239. }
  240. }
  241. TEST_1D(CodedStreamTest, ExpectTagFromArray, kVarintCases) {
  242. memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
  243. const uint32 expected_value = static_cast<uint32>(kVarintCases_case.value);
  244. // If the expectation succeeds, it should return a pointer past the tag.
  245. if (kVarintCases_case.size <= 2) {
  246. EXPECT_TRUE(NULL ==
  247. CodedInputStream::ExpectTagFromArray(buffer_,
  248. expected_value + 1));
  249. EXPECT_TRUE(buffer_ + kVarintCases_case.size ==
  250. CodedInputStream::ExpectTagFromArray(buffer_, expected_value));
  251. } else {
  252. EXPECT_TRUE(NULL ==
  253. CodedInputStream::ExpectTagFromArray(buffer_, expected_value));
  254. }
  255. }
  256. TEST_2D(CodedStreamTest, ReadVarint64, kVarintCases, kBlockSizes) {
  257. memcpy(buffer_, kVarintCases_case.bytes, kVarintCases_case.size);
  258. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  259. {
  260. CodedInputStream coded_input(&input);
  261. uint64 value;
  262. EXPECT_TRUE(coded_input.ReadVarint64(&value));
  263. EXPECT_EQ(kVarintCases_case.value, value);
  264. }
  265. EXPECT_EQ(kVarintCases_case.size, input.ByteCount());
  266. }
  267. TEST_2D(CodedStreamTest, WriteVarint32, kVarintCases, kBlockSizes) {
  268. if (kVarintCases_case.value > ULL(0x00000000FFFFFFFF)) {
  269. // Skip this test for the 64-bit values.
  270. return;
  271. }
  272. ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
  273. {
  274. CodedOutputStream coded_output(&output);
  275. coded_output.WriteVarint32(static_cast<uint32>(kVarintCases_case.value));
  276. EXPECT_FALSE(coded_output.HadError());
  277. EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
  278. }
  279. EXPECT_EQ(kVarintCases_case.size, output.ByteCount());
  280. EXPECT_EQ(0,
  281. memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
  282. }
  283. TEST_2D(CodedStreamTest, WriteVarint64, kVarintCases, kBlockSizes) {
  284. ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
  285. {
  286. CodedOutputStream coded_output(&output);
  287. coded_output.WriteVarint64(kVarintCases_case.value);
  288. EXPECT_FALSE(coded_output.HadError());
  289. EXPECT_EQ(kVarintCases_case.size, coded_output.ByteCount());
  290. }
  291. EXPECT_EQ(kVarintCases_case.size, output.ByteCount());
  292. EXPECT_EQ(0,
  293. memcmp(buffer_, kVarintCases_case.bytes, kVarintCases_case.size));
  294. }
  295. // This test causes gcc 3.3.5 (and earlier?) to give the cryptic error:
  296. // "sorry, unimplemented: `method_call_expr' not supported by dump_expr"
  297. #if !defined(__GNUC__) || __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3)
  298. int32 kSignExtendedVarintCases[] = {
  299. 0, 1, -1, 1237894, -37895138
  300. };
  301. TEST_2D(CodedStreamTest, WriteVarint32SignExtended,
  302. kSignExtendedVarintCases, kBlockSizes) {
  303. ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
  304. {
  305. CodedOutputStream coded_output(&output);
  306. coded_output.WriteVarint32SignExtended(kSignExtendedVarintCases_case);
  307. EXPECT_FALSE(coded_output.HadError());
  308. if (kSignExtendedVarintCases_case < 0) {
  309. EXPECT_EQ(10, coded_output.ByteCount());
  310. } else {
  311. EXPECT_LE(coded_output.ByteCount(), 5);
  312. }
  313. }
  314. if (kSignExtendedVarintCases_case < 0) {
  315. EXPECT_EQ(10, output.ByteCount());
  316. } else {
  317. EXPECT_LE(output.ByteCount(), 5);
  318. }
  319. // Read value back in as a varint64 and insure it matches.
  320. ArrayInputStream input(buffer_, sizeof(buffer_));
  321. {
  322. CodedInputStream coded_input(&input);
  323. uint64 value;
  324. EXPECT_TRUE(coded_input.ReadVarint64(&value));
  325. EXPECT_EQ(kSignExtendedVarintCases_case, static_cast<int64>(value));
  326. }
  327. EXPECT_EQ(output.ByteCount(), input.ByteCount());
  328. }
  329. #endif
  330. // -------------------------------------------------------------------
  331. // Varint failure test.
  332. struct VarintErrorCase {
  333. uint8 bytes[12];
  334. int size;
  335. bool can_parse;
  336. };
  337. inline std::ostream& operator<<(std::ostream& os, const VarintErrorCase& c) {
  338. return os << "size " << c.size;
  339. }
  340. const VarintErrorCase kVarintErrorCases[] = {
  341. // Control case. (Insures that there isn't something else wrong that
  342. // makes parsing always fail.)
  343. {{0x00}, 1, true},
  344. // No input data.
  345. {{}, 0, false},
  346. // Input ends unexpectedly.
  347. {{0xf0, 0xab}, 2, false},
  348. // Input ends unexpectedly after 32 bits.
  349. {{0xf0, 0xab, 0xc9, 0x9a, 0xf8, 0xb2}, 6, false},
  350. // Longer than 10 bytes.
  351. {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01},
  352. 11, false},
  353. };
  354. TEST_2D(CodedStreamTest, ReadVarint32Error, kVarintErrorCases, kBlockSizes) {
  355. memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
  356. ArrayInputStream input(buffer_, kVarintErrorCases_case.size,
  357. kBlockSizes_case);
  358. CodedInputStream coded_input(&input);
  359. uint32 value;
  360. EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value));
  361. }
  362. TEST_2D(CodedStreamTest, ReadVarint32Error_LeavesValueInInitializedState,
  363. kVarintErrorCases, kBlockSizes) {
  364. memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
  365. ArrayInputStream input(buffer_, kVarintErrorCases_case.size,
  366. kBlockSizes_case);
  367. CodedInputStream coded_input(&input);
  368. uint32 value = 0;
  369. EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value));
  370. // While the specific value following a failure is not critical, we do want to
  371. // ensure that it doesn't get set to an uninitialized value. (This check fails
  372. // in MSAN mode if value has been set to an uninitialized value.)
  373. EXPECT_EQ(value, value);
  374. }
  375. TEST_2D(CodedStreamTest, ReadVarint64Error, kVarintErrorCases, kBlockSizes) {
  376. memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
  377. ArrayInputStream input(buffer_, kVarintErrorCases_case.size,
  378. kBlockSizes_case);
  379. CodedInputStream coded_input(&input);
  380. uint64 value;
  381. EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value));
  382. }
  383. TEST_2D(CodedStreamTest, ReadVarint64Error_LeavesValueInInitializedState,
  384. kVarintErrorCases, kBlockSizes) {
  385. memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size);
  386. ArrayInputStream input(buffer_, kVarintErrorCases_case.size,
  387. kBlockSizes_case);
  388. CodedInputStream coded_input(&input);
  389. uint64 value = 0;
  390. EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value));
  391. // While the specific value following a failure is not critical, we do want to
  392. // ensure that it doesn't get set to an uninitialized value. (This check fails
  393. // in MSAN mode if value has been set to an uninitialized value.)
  394. EXPECT_EQ(value, value);
  395. }
  396. // -------------------------------------------------------------------
  397. // VarintSize
  398. struct VarintSizeCase {
  399. uint64 value;
  400. int size;
  401. };
  402. inline std::ostream& operator<<(std::ostream& os, const VarintSizeCase& c) {
  403. return os << c.value;
  404. }
  405. VarintSizeCase kVarintSizeCases[] = {
  406. {0u, 1},
  407. {1u, 1},
  408. {127u, 1},
  409. {128u, 2},
  410. {758923u, 3},
  411. {4000000000u, 5},
  412. {ULL(41256202580718336), 8},
  413. {ULL(11964378330978735131), 10},
  414. };
  415. TEST_1D(CodedStreamTest, VarintSize32, kVarintSizeCases) {
  416. if (kVarintSizeCases_case.value > 0xffffffffu) {
  417. // Skip 64-bit values.
  418. return;
  419. }
  420. EXPECT_EQ(kVarintSizeCases_case.size,
  421. CodedOutputStream::VarintSize32(
  422. static_cast<uint32>(kVarintSizeCases_case.value)));
  423. }
  424. TEST_1D(CodedStreamTest, VarintSize64, kVarintSizeCases) {
  425. EXPECT_EQ(kVarintSizeCases_case.size,
  426. CodedOutputStream::VarintSize64(kVarintSizeCases_case.value));
  427. }
  428. TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) {
  429. int expected = 1;
  430. for (int i = 1; i < 32; i++) {
  431. if (i % 7 == 0) {
  432. expected += 1;
  433. }
  434. EXPECT_EQ(expected,
  435. CodedOutputStream::VarintSize32(static_cast<uint32>(0x1u << i)));
  436. }
  437. }
  438. TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) {
  439. int expected = 1;
  440. for (int i = 1; i < 64; i++) {
  441. if (i % 7 == 0) {
  442. expected += 1;
  443. }
  444. EXPECT_EQ(expected, CodedOutputStream::VarintSize64(
  445. static_cast<uint64>(0x1ull << i)));
  446. }
  447. }
  448. // -------------------------------------------------------------------
  449. // Fixed-size int tests
  450. struct Fixed32Case {
  451. uint8 bytes[sizeof(uint32)]; // Encoded bytes.
  452. uint32 value; // Parsed value.
  453. };
  454. struct Fixed64Case {
  455. uint8 bytes[sizeof(uint64)]; // Encoded bytes.
  456. uint64 value; // Parsed value.
  457. };
  458. inline std::ostream& operator<<(std::ostream& os, const Fixed32Case& c) {
  459. return os << "0x" << std::hex << c.value << std::dec;
  460. }
  461. inline std::ostream& operator<<(std::ostream& os, const Fixed64Case& c) {
  462. return os << "0x" << std::hex << c.value << std::dec;
  463. }
  464. Fixed32Case kFixed32Cases[] = {
  465. {{0xef, 0xcd, 0xab, 0x90}, 0x90abcdefu},
  466. {{0x12, 0x34, 0x56, 0x78}, 0x78563412u},
  467. };
  468. Fixed64Case kFixed64Cases[] = {
  469. {{0xef, 0xcd, 0xab, 0x90, 0x12, 0x34, 0x56, 0x78}, ULL(0x7856341290abcdef)},
  470. {{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, ULL(0x8877665544332211)},
  471. };
  472. TEST_2D(CodedStreamTest, ReadLittleEndian32, kFixed32Cases, kBlockSizes) {
  473. memcpy(buffer_, kFixed32Cases_case.bytes, sizeof(kFixed32Cases_case.bytes));
  474. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  475. {
  476. CodedInputStream coded_input(&input);
  477. uint32 value;
  478. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  479. EXPECT_EQ(kFixed32Cases_case.value, value);
  480. }
  481. EXPECT_EQ(sizeof(uint32), input.ByteCount());
  482. }
  483. TEST_2D(CodedStreamTest, ReadLittleEndian64, kFixed64Cases, kBlockSizes) {
  484. memcpy(buffer_, kFixed64Cases_case.bytes, sizeof(kFixed64Cases_case.bytes));
  485. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  486. {
  487. CodedInputStream coded_input(&input);
  488. uint64 value;
  489. EXPECT_TRUE(coded_input.ReadLittleEndian64(&value));
  490. EXPECT_EQ(kFixed64Cases_case.value, value);
  491. }
  492. EXPECT_EQ(sizeof(uint64), input.ByteCount());
  493. }
  494. TEST_2D(CodedStreamTest, WriteLittleEndian32, kFixed32Cases, kBlockSizes) {
  495. ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
  496. {
  497. CodedOutputStream coded_output(&output);
  498. coded_output.WriteLittleEndian32(kFixed32Cases_case.value);
  499. EXPECT_FALSE(coded_output.HadError());
  500. EXPECT_EQ(sizeof(uint32), coded_output.ByteCount());
  501. }
  502. EXPECT_EQ(sizeof(uint32), output.ByteCount());
  503. EXPECT_EQ(0, memcmp(buffer_, kFixed32Cases_case.bytes, sizeof(uint32)));
  504. }
  505. TEST_2D(CodedStreamTest, WriteLittleEndian64, kFixed64Cases, kBlockSizes) {
  506. ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
  507. {
  508. CodedOutputStream coded_output(&output);
  509. coded_output.WriteLittleEndian64(kFixed64Cases_case.value);
  510. EXPECT_FALSE(coded_output.HadError());
  511. EXPECT_EQ(sizeof(uint64), coded_output.ByteCount());
  512. }
  513. EXPECT_EQ(sizeof(uint64), output.ByteCount());
  514. EXPECT_EQ(0, memcmp(buffer_, kFixed64Cases_case.bytes, sizeof(uint64)));
  515. }
  516. // Tests using the static methods to read fixed-size values from raw arrays.
  517. TEST_1D(CodedStreamTest, ReadLittleEndian32FromArray, kFixed32Cases) {
  518. memcpy(buffer_, kFixed32Cases_case.bytes, sizeof(kFixed32Cases_case.bytes));
  519. uint32 value;
  520. const uint8* end = CodedInputStream::ReadLittleEndian32FromArray(
  521. buffer_, &value);
  522. EXPECT_EQ(kFixed32Cases_case.value, value);
  523. EXPECT_TRUE(end == buffer_ + sizeof(value));
  524. }
  525. TEST_1D(CodedStreamTest, ReadLittleEndian64FromArray, kFixed64Cases) {
  526. memcpy(buffer_, kFixed64Cases_case.bytes, sizeof(kFixed64Cases_case.bytes));
  527. uint64 value;
  528. const uint8* end = CodedInputStream::ReadLittleEndian64FromArray(
  529. buffer_, &value);
  530. EXPECT_EQ(kFixed64Cases_case.value, value);
  531. EXPECT_TRUE(end == buffer_ + sizeof(value));
  532. }
  533. // -------------------------------------------------------------------
  534. // Raw reads and writes
  535. const char kRawBytes[] = "Some bytes which will be written and read raw.";
  536. TEST_1D(CodedStreamTest, ReadRaw, kBlockSizes) {
  537. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  538. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  539. char read_buffer[sizeof(kRawBytes)];
  540. {
  541. CodedInputStream coded_input(&input);
  542. EXPECT_TRUE(coded_input.ReadRaw(read_buffer, sizeof(kRawBytes)));
  543. EXPECT_EQ(0, memcmp(kRawBytes, read_buffer, sizeof(kRawBytes)));
  544. }
  545. EXPECT_EQ(sizeof(kRawBytes), input.ByteCount());
  546. }
  547. TEST_1D(CodedStreamTest, WriteRaw, kBlockSizes) {
  548. ArrayOutputStream output(buffer_, sizeof(buffer_), kBlockSizes_case);
  549. {
  550. CodedOutputStream coded_output(&output);
  551. coded_output.WriteRaw(kRawBytes, sizeof(kRawBytes));
  552. EXPECT_FALSE(coded_output.HadError());
  553. EXPECT_EQ(sizeof(kRawBytes), coded_output.ByteCount());
  554. }
  555. EXPECT_EQ(sizeof(kRawBytes), output.ByteCount());
  556. EXPECT_EQ(0, memcmp(buffer_, kRawBytes, sizeof(kRawBytes)));
  557. }
  558. TEST_1D(CodedStreamTest, ReadString, kBlockSizes) {
  559. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  560. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  561. {
  562. CodedInputStream coded_input(&input);
  563. string str;
  564. EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
  565. EXPECT_EQ(kRawBytes, str);
  566. }
  567. EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
  568. }
  569. // Check to make sure ReadString doesn't crash on impossibly large strings.
  570. TEST_1D(CodedStreamTest, ReadStringImpossiblyLarge, kBlockSizes) {
  571. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  572. {
  573. CodedInputStream coded_input(&input);
  574. string str;
  575. // Try to read a gigabyte.
  576. EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
  577. }
  578. }
  579. TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnStack) {
  580. // Same test as above, except directly use a buffer. This used to cause
  581. // crashes while the above did not.
  582. uint8 buffer[8];
  583. CodedInputStream coded_input(buffer, 8);
  584. string str;
  585. EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
  586. }
  587. TEST_F(CodedStreamTest, ReadStringImpossiblyLargeFromStringOnHeap) {
  588. std::unique_ptr<uint8[]> buffer(new uint8[8]);
  589. CodedInputStream coded_input(buffer.get(), 8);
  590. string str;
  591. EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
  592. }
  593. TEST_1D(CodedStreamTest, ReadStringReservesMemoryOnTotalLimit, kBlockSizes) {
  594. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  595. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  596. {
  597. CodedInputStream coded_input(&input);
  598. coded_input.SetTotalBytesLimit(sizeof(kRawBytes), sizeof(kRawBytes));
  599. EXPECT_EQ(sizeof(kRawBytes), coded_input.BytesUntilTotalBytesLimit());
  600. string str;
  601. EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
  602. EXPECT_EQ(sizeof(kRawBytes) - strlen(kRawBytes),
  603. coded_input.BytesUntilTotalBytesLimit());
  604. EXPECT_EQ(kRawBytes, str);
  605. // TODO(liujisi): Replace with a more meaningful test (see cl/60966023).
  606. EXPECT_GE(str.capacity(), strlen(kRawBytes));
  607. }
  608. EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
  609. }
  610. TEST_1D(CodedStreamTest, ReadStringReservesMemoryOnPushedLimit, kBlockSizes) {
  611. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  612. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  613. {
  614. CodedInputStream coded_input(&input);
  615. coded_input.PushLimit(sizeof(buffer_));
  616. string str;
  617. EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
  618. EXPECT_EQ(kRawBytes, str);
  619. // TODO(liujisi): Replace with a more meaningful test (see cl/60966023).
  620. EXPECT_GE(str.capacity(), strlen(kRawBytes));
  621. }
  622. EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
  623. }
  624. TEST_F(CodedStreamTest, ReadStringNoReservationIfLimitsNotSet) {
  625. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  626. // Buffer size in the input must be smaller than sizeof(kRawBytes),
  627. // otherwise check against capacity will fail as ReadStringInline()
  628. // will handle the reading and will reserve the memory as needed.
  629. ArrayInputStream input(buffer_, sizeof(buffer_), 32);
  630. {
  631. CodedInputStream coded_input(&input);
  632. string str;
  633. EXPECT_TRUE(coded_input.ReadString(&str, strlen(kRawBytes)));
  634. EXPECT_EQ(kRawBytes, str);
  635. // Note: this check depends on string class implementation. It
  636. // expects that string will allocate more than strlen(kRawBytes)
  637. // if the content of kRawBytes is appended to string in small
  638. // chunks.
  639. // TODO(liujisi): Replace with a more meaningful test (see cl/60966023).
  640. EXPECT_GE(str.capacity(), strlen(kRawBytes));
  641. }
  642. EXPECT_EQ(strlen(kRawBytes), input.ByteCount());
  643. }
  644. TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsNegative) {
  645. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  646. // Buffer size in the input must be smaller than sizeof(kRawBytes),
  647. // otherwise check against capacity will fail as ReadStringInline()
  648. // will handle the reading and will reserve the memory as needed.
  649. ArrayInputStream input(buffer_, sizeof(buffer_), 32);
  650. {
  651. CodedInputStream coded_input(&input);
  652. coded_input.PushLimit(sizeof(buffer_));
  653. string str;
  654. EXPECT_FALSE(coded_input.ReadString(&str, -1));
  655. // Note: this check depends on string class implementation. It
  656. // expects that string will always allocate the same amount of
  657. // memory for an empty string.
  658. EXPECT_EQ(string().capacity(), str.capacity());
  659. }
  660. }
  661. TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsLarge) {
  662. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  663. // Buffer size in the input must be smaller than sizeof(kRawBytes),
  664. // otherwise check against capacity will fail as ReadStringInline()
  665. // will handle the reading and will reserve the memory as needed.
  666. ArrayInputStream input(buffer_, sizeof(buffer_), 32);
  667. {
  668. CodedInputStream coded_input(&input);
  669. coded_input.PushLimit(sizeof(buffer_));
  670. string str;
  671. EXPECT_FALSE(coded_input.ReadString(&str, 1 << 30));
  672. EXPECT_GT(1 << 30, str.capacity());
  673. }
  674. }
  675. TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheLimit) {
  676. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  677. // Buffer size in the input must be smaller than sizeof(kRawBytes),
  678. // otherwise check against capacity will fail as ReadStringInline()
  679. // will handle the reading and will reserve the memory as needed.
  680. ArrayInputStream input(buffer_, sizeof(buffer_), 32);
  681. {
  682. CodedInputStream coded_input(&input);
  683. coded_input.PushLimit(16);
  684. string str;
  685. EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
  686. // Note: this check depends on string class implementation. It
  687. // expects that string will allocate less than strlen(kRawBytes)
  688. // for an empty string.
  689. EXPECT_GT(strlen(kRawBytes), str.capacity());
  690. }
  691. }
  692. TEST_F(CodedStreamTest, ReadStringNoReservationSizeIsOverTheTotalBytesLimit) {
  693. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  694. // Buffer size in the input must be smaller than sizeof(kRawBytes),
  695. // otherwise check against capacity will fail as ReadStringInline()
  696. // will handle the reading and will reserve the memory as needed.
  697. ArrayInputStream input(buffer_, sizeof(buffer_), 32);
  698. {
  699. CodedInputStream coded_input(&input);
  700. coded_input.SetTotalBytesLimit(16, 16);
  701. string str;
  702. EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
  703. // Note: this check depends on string class implementation. It
  704. // expects that string will allocate less than strlen(kRawBytes)
  705. // for an empty string.
  706. EXPECT_GT(strlen(kRawBytes), str.capacity());
  707. }
  708. }
  709. TEST_F(CodedStreamTest,
  710. ReadStringNoReservationSizeIsOverTheClosestLimit_GlobalLimitIsCloser) {
  711. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  712. // Buffer size in the input must be smaller than sizeof(kRawBytes),
  713. // otherwise check against capacity will fail as ReadStringInline()
  714. // will handle the reading and will reserve the memory as needed.
  715. ArrayInputStream input(buffer_, sizeof(buffer_), 32);
  716. {
  717. CodedInputStream coded_input(&input);
  718. coded_input.PushLimit(sizeof(buffer_));
  719. coded_input.SetTotalBytesLimit(16, 16);
  720. string str;
  721. EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
  722. // Note: this check depends on string class implementation. It
  723. // expects that string will allocate less than strlen(kRawBytes)
  724. // for an empty string.
  725. EXPECT_GT(strlen(kRawBytes), str.capacity());
  726. }
  727. }
  728. TEST_F(CodedStreamTest,
  729. ReadStringNoReservationSizeIsOverTheClosestLimit_LocalLimitIsCloser) {
  730. memcpy(buffer_, kRawBytes, sizeof(kRawBytes));
  731. // Buffer size in the input must be smaller than sizeof(kRawBytes),
  732. // otherwise check against capacity will fail as ReadStringInline()
  733. // will handle the reading and will reserve the memory as needed.
  734. ArrayInputStream input(buffer_, sizeof(buffer_), 32);
  735. {
  736. CodedInputStream coded_input(&input);
  737. coded_input.PushLimit(16);
  738. coded_input.SetTotalBytesLimit(sizeof(buffer_), sizeof(buffer_));
  739. EXPECT_EQ(sizeof(buffer_), coded_input.BytesUntilTotalBytesLimit());
  740. string str;
  741. EXPECT_FALSE(coded_input.ReadString(&str, strlen(kRawBytes)));
  742. // Note: this check depends on string class implementation. It
  743. // expects that string will allocate less than strlen(kRawBytes)
  744. // for an empty string.
  745. EXPECT_GT(strlen(kRawBytes), str.capacity());
  746. }
  747. }
  748. // -------------------------------------------------------------------
  749. // Skip
  750. const char kSkipTestBytes[] =
  751. "<Before skipping><To be skipped><After skipping>";
  752. TEST_1D(CodedStreamTest, SkipInput, kBlockSizes) {
  753. memcpy(buffer_, kSkipTestBytes, sizeof(kSkipTestBytes));
  754. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  755. {
  756. CodedInputStream coded_input(&input);
  757. string str;
  758. EXPECT_TRUE(coded_input.ReadString(&str, strlen("<Before skipping>")));
  759. EXPECT_EQ("<Before skipping>", str);
  760. EXPECT_TRUE(coded_input.Skip(strlen("<To be skipped>")));
  761. EXPECT_TRUE(coded_input.ReadString(&str, strlen("<After skipping>")));
  762. EXPECT_EQ("<After skipping>", str);
  763. }
  764. EXPECT_EQ(strlen(kSkipTestBytes), input.ByteCount());
  765. }
  766. // -------------------------------------------------------------------
  767. // GetDirectBufferPointer
  768. TEST_F(CodedStreamTest, GetDirectBufferPointerInput) {
  769. ArrayInputStream input(buffer_, sizeof(buffer_), 8);
  770. CodedInputStream coded_input(&input);
  771. const void* ptr;
  772. int size;
  773. EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
  774. EXPECT_EQ(buffer_, ptr);
  775. EXPECT_EQ(8, size);
  776. // Peeking again should return the same pointer.
  777. EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
  778. EXPECT_EQ(buffer_, ptr);
  779. EXPECT_EQ(8, size);
  780. // Skip forward in the same buffer then peek again.
  781. EXPECT_TRUE(coded_input.Skip(3));
  782. EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
  783. EXPECT_EQ(buffer_ + 3, ptr);
  784. EXPECT_EQ(5, size);
  785. // Skip to end of buffer and peek -- should get next buffer.
  786. EXPECT_TRUE(coded_input.Skip(5));
  787. EXPECT_TRUE(coded_input.GetDirectBufferPointer(&ptr, &size));
  788. EXPECT_EQ(buffer_ + 8, ptr);
  789. EXPECT_EQ(8, size);
  790. }
  791. TEST_F(CodedStreamTest, GetDirectBufferPointerInlineInput) {
  792. ArrayInputStream input(buffer_, sizeof(buffer_), 8);
  793. CodedInputStream coded_input(&input);
  794. const void* ptr;
  795. int size;
  796. coded_input.GetDirectBufferPointerInline(&ptr, &size);
  797. EXPECT_EQ(buffer_, ptr);
  798. EXPECT_EQ(8, size);
  799. // Peeking again should return the same pointer.
  800. coded_input.GetDirectBufferPointerInline(&ptr, &size);
  801. EXPECT_EQ(buffer_, ptr);
  802. EXPECT_EQ(8, size);
  803. // Skip forward in the same buffer then peek again.
  804. EXPECT_TRUE(coded_input.Skip(3));
  805. coded_input.GetDirectBufferPointerInline(&ptr, &size);
  806. EXPECT_EQ(buffer_ + 3, ptr);
  807. EXPECT_EQ(5, size);
  808. // Skip to end of buffer and peek -- should return false and provide an empty
  809. // buffer. It does not try to Refresh().
  810. EXPECT_TRUE(coded_input.Skip(5));
  811. coded_input.GetDirectBufferPointerInline(&ptr, &size);
  812. EXPECT_EQ(buffer_ + 8, ptr);
  813. EXPECT_EQ(0, size);
  814. }
  815. TEST_F(CodedStreamTest, GetDirectBufferPointerOutput) {
  816. ArrayOutputStream output(buffer_, sizeof(buffer_), 8);
  817. CodedOutputStream coded_output(&output);
  818. void* ptr;
  819. int size;
  820. EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size));
  821. EXPECT_EQ(buffer_, ptr);
  822. EXPECT_EQ(8, size);
  823. // Peeking again should return the same pointer.
  824. EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size));
  825. EXPECT_EQ(buffer_, ptr);
  826. EXPECT_EQ(8, size);
  827. // Skip forward in the same buffer then peek again.
  828. EXPECT_TRUE(coded_output.Skip(3));
  829. EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size));
  830. EXPECT_EQ(buffer_ + 3, ptr);
  831. EXPECT_EQ(5, size);
  832. // Skip to end of buffer and peek -- should get next buffer.
  833. EXPECT_TRUE(coded_output.Skip(5));
  834. EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size));
  835. EXPECT_EQ(buffer_ + 8, ptr);
  836. EXPECT_EQ(8, size);
  837. // Skip over multiple buffers.
  838. EXPECT_TRUE(coded_output.Skip(22));
  839. EXPECT_TRUE(coded_output.GetDirectBufferPointer(&ptr, &size));
  840. EXPECT_EQ(buffer_ + 30, ptr);
  841. EXPECT_EQ(2, size);
  842. }
  843. // -------------------------------------------------------------------
  844. // Limits
  845. TEST_1D(CodedStreamTest, BasicLimit, kBlockSizes) {
  846. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  847. {
  848. CodedInputStream coded_input(&input);
  849. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  850. CodedInputStream::Limit limit = coded_input.PushLimit(8);
  851. // Read until we hit the limit.
  852. uint32 value;
  853. EXPECT_EQ(8, coded_input.BytesUntilLimit());
  854. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  855. EXPECT_EQ(4, coded_input.BytesUntilLimit());
  856. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  857. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  858. EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
  859. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  860. coded_input.PopLimit(limit);
  861. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  862. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  863. }
  864. EXPECT_EQ(12, input.ByteCount());
  865. }
  866. // Test what happens when we push two limits where the second (top) one is
  867. // shorter.
  868. TEST_1D(CodedStreamTest, SmallLimitOnTopOfBigLimit, kBlockSizes) {
  869. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  870. {
  871. CodedInputStream coded_input(&input);
  872. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  873. CodedInputStream::Limit limit1 = coded_input.PushLimit(8);
  874. EXPECT_EQ(8, coded_input.BytesUntilLimit());
  875. CodedInputStream::Limit limit2 = coded_input.PushLimit(4);
  876. uint32 value;
  877. // Read until we hit limit2, the top and shortest limit.
  878. EXPECT_EQ(4, coded_input.BytesUntilLimit());
  879. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  880. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  881. EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
  882. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  883. coded_input.PopLimit(limit2);
  884. // Read until we hit limit1.
  885. EXPECT_EQ(4, coded_input.BytesUntilLimit());
  886. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  887. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  888. EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
  889. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  890. coded_input.PopLimit(limit1);
  891. // No more limits.
  892. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  893. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  894. }
  895. EXPECT_EQ(12, input.ByteCount());
  896. }
  897. // Test what happens when we push two limits where the second (top) one is
  898. // longer. In this case, the top limit is shortened to match the previous
  899. // limit.
  900. TEST_1D(CodedStreamTest, BigLimitOnTopOfSmallLimit, kBlockSizes) {
  901. ArrayInputStream input(buffer_, sizeof(buffer_), kBlockSizes_case);
  902. {
  903. CodedInputStream coded_input(&input);
  904. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  905. CodedInputStream::Limit limit1 = coded_input.PushLimit(4);
  906. EXPECT_EQ(4, coded_input.BytesUntilLimit());
  907. CodedInputStream::Limit limit2 = coded_input.PushLimit(8);
  908. uint32 value;
  909. // Read until we hit limit2. Except, wait! limit1 is shorter, so
  910. // we end up hitting that first, despite having 4 bytes to go on
  911. // limit2.
  912. EXPECT_EQ(4, coded_input.BytesUntilLimit());
  913. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  914. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  915. EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
  916. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  917. coded_input.PopLimit(limit2);
  918. // OK, popped limit2, now limit1 is on top, which we've already hit.
  919. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  920. EXPECT_FALSE(coded_input.ReadLittleEndian32(&value));
  921. EXPECT_EQ(0, coded_input.BytesUntilLimit());
  922. coded_input.PopLimit(limit1);
  923. // No more limits.
  924. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  925. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  926. }
  927. EXPECT_EQ(8, input.ByteCount());
  928. }
  929. TEST_F(CodedStreamTest, ExpectAtEnd) {
  930. // Test ExpectAtEnd(), which is based on limits.
  931. ArrayInputStream input(buffer_, sizeof(buffer_));
  932. CodedInputStream coded_input(&input);
  933. EXPECT_FALSE(coded_input.ExpectAtEnd());
  934. CodedInputStream::Limit limit = coded_input.PushLimit(4);
  935. uint32 value;
  936. EXPECT_TRUE(coded_input.ReadLittleEndian32(&value));
  937. EXPECT_TRUE(coded_input.ExpectAtEnd());
  938. coded_input.PopLimit(limit);
  939. EXPECT_FALSE(coded_input.ExpectAtEnd());
  940. }
  941. TEST_F(CodedStreamTest, NegativeLimit) {
  942. // Check what happens when we push a negative limit.
  943. ArrayInputStream input(buffer_, sizeof(buffer_));
  944. CodedInputStream coded_input(&input);
  945. CodedInputStream::Limit limit = coded_input.PushLimit(-1234);
  946. // BytesUntilLimit() returns -1 to mean "no limit", which actually means
  947. // "the limit is INT_MAX relative to the beginning of the stream".
  948. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  949. coded_input.PopLimit(limit);
  950. }
  951. TEST_F(CodedStreamTest, NegativeLimitAfterReading) {
  952. // Check what happens when we push a negative limit.
  953. ArrayInputStream input(buffer_, sizeof(buffer_));
  954. CodedInputStream coded_input(&input);
  955. ASSERT_TRUE(coded_input.Skip(128));
  956. CodedInputStream::Limit limit = coded_input.PushLimit(-64);
  957. // BytesUntilLimit() returns -1 to mean "no limit", which actually means
  958. // "the limit is INT_MAX relative to the beginning of the stream".
  959. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  960. coded_input.PopLimit(limit);
  961. }
  962. TEST_F(CodedStreamTest, OverflowLimit) {
  963. // Check what happens when we push a limit large enough that its absolute
  964. // position is more than 2GB into the stream.
  965. ArrayInputStream input(buffer_, sizeof(buffer_));
  966. CodedInputStream coded_input(&input);
  967. ASSERT_TRUE(coded_input.Skip(128));
  968. CodedInputStream::Limit limit = coded_input.PushLimit(INT_MAX);
  969. // BytesUntilLimit() returns -1 to mean "no limit", which actually means
  970. // "the limit is INT_MAX relative to the beginning of the stream".
  971. EXPECT_EQ(-1, coded_input.BytesUntilLimit());
  972. coded_input.PopLimit(limit);
  973. }
  974. TEST_F(CodedStreamTest, TotalBytesLimit) {
  975. ArrayInputStream input(buffer_, sizeof(buffer_));
  976. CodedInputStream coded_input(&input);
  977. coded_input.SetTotalBytesLimit(16, -1);
  978. EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
  979. string str;
  980. EXPECT_TRUE(coded_input.ReadString(&str, 16));
  981. EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
  982. std::vector<string> errors;
  983. {
  984. ScopedMemoryLog error_log;
  985. EXPECT_FALSE(coded_input.ReadString(&str, 1));
  986. errors = error_log.GetMessages(ERROR);
  987. }
  988. ASSERT_EQ(1, errors.size());
  989. EXPECT_PRED_FORMAT2(testing::IsSubstring,
  990. "A protocol message was rejected because it was too big", errors[0]);
  991. coded_input.SetTotalBytesLimit(32, -1);
  992. EXPECT_EQ(16, coded_input.BytesUntilTotalBytesLimit());
  993. EXPECT_TRUE(coded_input.ReadString(&str, 16));
  994. EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit());
  995. }
  996. TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) {
  997. // total_bytes_limit_ is not a valid place for a message to end.
  998. ArrayInputStream input(buffer_, sizeof(buffer_));
  999. CodedInputStream coded_input(&input);
  1000. // Set both total_bytes_limit and a regular limit at 16 bytes.
  1001. coded_input.SetTotalBytesLimit(16, -1);
  1002. CodedInputStream::Limit limit = coded_input.PushLimit(16);
  1003. // Read 16 bytes.
  1004. string str;
  1005. EXPECT_TRUE(coded_input.ReadString(&str, 16));
  1006. // Read a tag. Should fail, but report being a valid endpoint since it's
  1007. // a regular limit.
  1008. EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
  1009. EXPECT_TRUE(coded_input.ConsumedEntireMessage());
  1010. // Pop the limit.
  1011. coded_input.PopLimit(limit);
  1012. // Read a tag. Should fail, and report *not* being a valid endpoint, since
  1013. // this time we're hitting the total bytes limit.
  1014. EXPECT_EQ(0, coded_input.ReadTagNoLastTag());
  1015. EXPECT_FALSE(coded_input.ConsumedEntireMessage());
  1016. }
  1017. // This method is used by the tests below.
  1018. // It constructs a CodedInputStream with the given limits and tries to read 2KiB
  1019. // of data from it. Then it returns the logged errors and warnings in the given
  1020. // vectors.
  1021. void CodedStreamTest::SetupTotalBytesLimitWarningTest(
  1022. int total_bytes_limit, int warning_threshold,
  1023. std::vector<string>* out_errors, std::vector<string>* out_warnings) {
  1024. ArrayInputStream raw_input(buffer_, sizeof(buffer_), 128);
  1025. ScopedMemoryLog scoped_log;
  1026. {
  1027. CodedInputStream input(&raw_input);
  1028. input.SetTotalBytesLimit(total_bytes_limit, warning_threshold);
  1029. string str;
  1030. EXPECT_TRUE(input.ReadString(&str, 2048));
  1031. }
  1032. *out_errors = scoped_log.GetMessages(ERROR);
  1033. *out_warnings = scoped_log.GetMessages(WARNING);
  1034. }
  1035. TEST_F(CodedStreamTest, RecursionLimit) {
  1036. ArrayInputStream input(buffer_, sizeof(buffer_));
  1037. CodedInputStream coded_input(&input);
  1038. coded_input.SetRecursionLimit(4);
  1039. // This is way too much testing for a counter.
  1040. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1
  1041. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2
  1042. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3
  1043. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4
  1044. EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5
  1045. EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6
  1046. coded_input.DecrementRecursionDepth(); // 5
  1047. EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 6
  1048. coded_input.DecrementRecursionDepth(); // 5
  1049. coded_input.DecrementRecursionDepth(); // 4
  1050. coded_input.DecrementRecursionDepth(); // 3
  1051. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4
  1052. EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5
  1053. coded_input.DecrementRecursionDepth(); // 4
  1054. coded_input.DecrementRecursionDepth(); // 3
  1055. coded_input.DecrementRecursionDepth(); // 2
  1056. coded_input.DecrementRecursionDepth(); // 1
  1057. coded_input.DecrementRecursionDepth(); // 0
  1058. coded_input.DecrementRecursionDepth(); // 0
  1059. coded_input.DecrementRecursionDepth(); // 0
  1060. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 1
  1061. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 2
  1062. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 3
  1063. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 4
  1064. EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 5
  1065. coded_input.SetRecursionLimit(6);
  1066. EXPECT_TRUE(coded_input.IncrementRecursionDepth()); // 6
  1067. EXPECT_FALSE(coded_input.IncrementRecursionDepth()); // 7
  1068. }
  1069. class ReallyBigInputStream : public ZeroCopyInputStream {
  1070. public:
  1071. ReallyBigInputStream() : backup_amount_(0), buffer_count_(0) {}
  1072. ~ReallyBigInputStream() {}
  1073. // implements ZeroCopyInputStream ----------------------------------
  1074. bool Next(const void** data, int* size) {
  1075. // We only expect BackUp() to be called at the end.
  1076. EXPECT_EQ(0, backup_amount_);
  1077. switch (buffer_count_++) {
  1078. case 0:
  1079. *data = buffer_;
  1080. *size = sizeof(buffer_);
  1081. return true;
  1082. case 1:
  1083. // Return an enormously large buffer that, when combined with the 1k
  1084. // returned already, should overflow the total_bytes_read_ counter in
  1085. // CodedInputStream. Note that we'll only read the first 1024 bytes
  1086. // of this buffer so it's OK that we have it point at buffer_.
  1087. *data = buffer_;
  1088. *size = INT_MAX;
  1089. return true;
  1090. default:
  1091. return false;
  1092. }
  1093. }
  1094. void BackUp(int count) {
  1095. backup_amount_ = count;
  1096. }
  1097. bool Skip(int count) { GOOGLE_LOG(FATAL) << "Not implemented."; return false; }
  1098. int64 ByteCount() const { GOOGLE_LOG(FATAL) << "Not implemented."; return 0; }
  1099. int backup_amount_;
  1100. private:
  1101. char buffer_[1024];
  1102. int64 buffer_count_;
  1103. };
  1104. TEST_F(CodedStreamTest, InputOver2G) {
  1105. // CodedInputStream should gracefully handle input over 2G and call
  1106. // input.BackUp() with the correct number of bytes on destruction.
  1107. ReallyBigInputStream input;
  1108. std::vector<string> errors;
  1109. {
  1110. ScopedMemoryLog error_log;
  1111. CodedInputStream coded_input(&input);
  1112. string str;
  1113. EXPECT_TRUE(coded_input.ReadString(&str, 512));
  1114. EXPECT_TRUE(coded_input.ReadString(&str, 1024));
  1115. errors = error_log.GetMessages(ERROR);
  1116. }
  1117. EXPECT_EQ(INT_MAX - 512, input.backup_amount_);
  1118. EXPECT_EQ(0, errors.size());
  1119. }
  1120. } // namespace
  1121. } // namespace io
  1122. } // namespace protobuf
  1123. } // namespace google