/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/wire_format_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 905 lines · 628 code · 147 blank · 130 comment · 16 complexity · d2d00f5b1f5164c44ae8de5ac0e4c28a MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. #include <google/protobuf/wire_format.h>
  34. #include <google/protobuf/wire_format_lite_inl.h>
  35. #include <google/protobuf/descriptor.h>
  36. #include <google/protobuf/io/zero_copy_stream_impl.h>
  37. #include <google/protobuf/io/coded_stream.h>
  38. #include <google/protobuf/unittest.pb.h>
  39. #include <google/protobuf/unittest_mset.pb.h>
  40. #include <google/protobuf/test_util.h>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/testing/googletest.h>
  43. #include <gtest/gtest.h>
  44. #include <google/protobuf/stubs/stl_util-inl.h>
  45. namespace google {
  46. namespace protobuf {
  47. namespace internal {
  48. namespace {
  49. TEST(WireFormatTest, EnumsInSync) {
  50. // Verify that WireFormatLite::FieldType and WireFormatLite::CppType match
  51. // FieldDescriptor::Type and FieldDescriptor::CppType.
  52. EXPECT_EQ(implicit_cast<int>(FieldDescriptor::MAX_TYPE),
  53. implicit_cast<int>(WireFormatLite::MAX_FIELD_TYPE));
  54. EXPECT_EQ(implicit_cast<int>(FieldDescriptor::MAX_CPPTYPE),
  55. implicit_cast<int>(WireFormatLite::MAX_CPPTYPE));
  56. for (int i = 1; i <= WireFormatLite::MAX_FIELD_TYPE; i++) {
  57. EXPECT_EQ(
  58. implicit_cast<int>(FieldDescriptor::TypeToCppType(
  59. static_cast<FieldDescriptor::Type>(i))),
  60. implicit_cast<int>(WireFormatLite::FieldTypeToCppType(
  61. static_cast<WireFormatLite::FieldType>(i))));
  62. }
  63. }
  64. TEST(WireFormatTest, MaxFieldNumber) {
  65. // Make sure the max field number constant is accurate.
  66. EXPECT_EQ((1 << (32 - WireFormatLite::kTagTypeBits)) - 1,
  67. FieldDescriptor::kMaxNumber);
  68. }
  69. TEST(WireFormatTest, Parse) {
  70. unittest::TestAllTypes source, dest;
  71. string data;
  72. // Serialize using the generated code.
  73. TestUtil::SetAllFields(&source);
  74. source.SerializeToString(&data);
  75. // Parse using WireFormat.
  76. io::ArrayInputStream raw_input(data.data(), data.size());
  77. io::CodedInputStream input(&raw_input);
  78. WireFormat::ParseAndMergePartial(&input, &dest);
  79. // Check.
  80. TestUtil::ExpectAllFieldsSet(dest);
  81. }
  82. TEST(WireFormatTest, ParseExtensions) {
  83. unittest::TestAllExtensions source, dest;
  84. string data;
  85. // Serialize using the generated code.
  86. TestUtil::SetAllExtensions(&source);
  87. source.SerializeToString(&data);
  88. // Parse using WireFormat.
  89. io::ArrayInputStream raw_input(data.data(), data.size());
  90. io::CodedInputStream input(&raw_input);
  91. WireFormat::ParseAndMergePartial(&input, &dest);
  92. // Check.
  93. TestUtil::ExpectAllExtensionsSet(dest);
  94. }
  95. TEST(WireFormatTest, ParsePacked) {
  96. unittest::TestPackedTypes source, dest;
  97. string data;
  98. // Serialize using the generated code.
  99. TestUtil::SetPackedFields(&source);
  100. source.SerializeToString(&data);
  101. // Parse using WireFormat.
  102. io::ArrayInputStream raw_input(data.data(), data.size());
  103. io::CodedInputStream input(&raw_input);
  104. WireFormat::ParseAndMergePartial(&input, &dest);
  105. // Check.
  106. TestUtil::ExpectPackedFieldsSet(dest);
  107. }
  108. TEST(WireFormatTest, ParsePackedFromUnpacked) {
  109. // Serialize using the generated code.
  110. unittest::TestUnpackedTypes source;
  111. TestUtil::SetUnpackedFields(&source);
  112. string data = source.SerializeAsString();
  113. // Parse using WireFormat.
  114. unittest::TestPackedTypes dest;
  115. io::ArrayInputStream raw_input(data.data(), data.size());
  116. io::CodedInputStream input(&raw_input);
  117. WireFormat::ParseAndMergePartial(&input, &dest);
  118. // Check.
  119. TestUtil::ExpectPackedFieldsSet(dest);
  120. }
  121. TEST(WireFormatTest, ParseUnpackedFromPacked) {
  122. // Serialize using the generated code.
  123. unittest::TestPackedTypes source;
  124. TestUtil::SetPackedFields(&source);
  125. string data = source.SerializeAsString();
  126. // Parse using WireFormat.
  127. unittest::TestUnpackedTypes dest;
  128. io::ArrayInputStream raw_input(data.data(), data.size());
  129. io::CodedInputStream input(&raw_input);
  130. WireFormat::ParseAndMergePartial(&input, &dest);
  131. // Check.
  132. TestUtil::ExpectUnpackedFieldsSet(dest);
  133. }
  134. TEST(WireFormatTest, ParsePackedExtensions) {
  135. unittest::TestPackedExtensions source, dest;
  136. string data;
  137. // Serialize using the generated code.
  138. TestUtil::SetPackedExtensions(&source);
  139. source.SerializeToString(&data);
  140. // Parse using WireFormat.
  141. io::ArrayInputStream raw_input(data.data(), data.size());
  142. io::CodedInputStream input(&raw_input);
  143. WireFormat::ParseAndMergePartial(&input, &dest);
  144. // Check.
  145. TestUtil::ExpectPackedExtensionsSet(dest);
  146. }
  147. TEST(WireFormatTest, ByteSize) {
  148. unittest::TestAllTypes message;
  149. TestUtil::SetAllFields(&message);
  150. EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
  151. message.Clear();
  152. EXPECT_EQ(0, message.ByteSize());
  153. EXPECT_EQ(0, WireFormat::ByteSize(message));
  154. }
  155. TEST(WireFormatTest, ByteSizeExtensions) {
  156. unittest::TestAllExtensions message;
  157. TestUtil::SetAllExtensions(&message);
  158. EXPECT_EQ(message.ByteSize(),
  159. WireFormat::ByteSize(message));
  160. message.Clear();
  161. EXPECT_EQ(0, message.ByteSize());
  162. EXPECT_EQ(0, WireFormat::ByteSize(message));
  163. }
  164. TEST(WireFormatTest, ByteSizePacked) {
  165. unittest::TestPackedTypes message;
  166. TestUtil::SetPackedFields(&message);
  167. EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
  168. message.Clear();
  169. EXPECT_EQ(0, message.ByteSize());
  170. EXPECT_EQ(0, WireFormat::ByteSize(message));
  171. }
  172. TEST(WireFormatTest, ByteSizePackedExtensions) {
  173. unittest::TestPackedExtensions message;
  174. TestUtil::SetPackedExtensions(&message);
  175. EXPECT_EQ(message.ByteSize(),
  176. WireFormat::ByteSize(message));
  177. message.Clear();
  178. EXPECT_EQ(0, message.ByteSize());
  179. EXPECT_EQ(0, WireFormat::ByteSize(message));
  180. }
  181. TEST(WireFormatTest, Serialize) {
  182. unittest::TestAllTypes message;
  183. string generated_data;
  184. string dynamic_data;
  185. TestUtil::SetAllFields(&message);
  186. int size = message.ByteSize();
  187. // Serialize using the generated code.
  188. {
  189. io::StringOutputStream raw_output(&generated_data);
  190. io::CodedOutputStream output(&raw_output);
  191. message.SerializeWithCachedSizes(&output);
  192. ASSERT_FALSE(output.HadError());
  193. }
  194. // Serialize using WireFormat.
  195. {
  196. io::StringOutputStream raw_output(&dynamic_data);
  197. io::CodedOutputStream output(&raw_output);
  198. WireFormat::SerializeWithCachedSizes(message, size, &output);
  199. ASSERT_FALSE(output.HadError());
  200. }
  201. // Should be the same.
  202. // Don't use EXPECT_EQ here because we're comparing raw binary data and
  203. // we really don't want it dumped to stdout on failure.
  204. EXPECT_TRUE(dynamic_data == generated_data);
  205. }
  206. TEST(WireFormatTest, SerializeExtensions) {
  207. unittest::TestAllExtensions message;
  208. string generated_data;
  209. string dynamic_data;
  210. TestUtil::SetAllExtensions(&message);
  211. int size = message.ByteSize();
  212. // Serialize using the generated code.
  213. {
  214. io::StringOutputStream raw_output(&generated_data);
  215. io::CodedOutputStream output(&raw_output);
  216. message.SerializeWithCachedSizes(&output);
  217. ASSERT_FALSE(output.HadError());
  218. }
  219. // Serialize using WireFormat.
  220. {
  221. io::StringOutputStream raw_output(&dynamic_data);
  222. io::CodedOutputStream output(&raw_output);
  223. WireFormat::SerializeWithCachedSizes(message, size, &output);
  224. ASSERT_FALSE(output.HadError());
  225. }
  226. // Should be the same.
  227. // Don't use EXPECT_EQ here because we're comparing raw binary data and
  228. // we really don't want it dumped to stdout on failure.
  229. EXPECT_TRUE(dynamic_data == generated_data);
  230. }
  231. TEST(WireFormatTest, SerializeFieldsAndExtensions) {
  232. unittest::TestFieldOrderings message;
  233. string generated_data;
  234. string dynamic_data;
  235. TestUtil::SetAllFieldsAndExtensions(&message);
  236. int size = message.ByteSize();
  237. // Serialize using the generated code.
  238. {
  239. io::StringOutputStream raw_output(&generated_data);
  240. io::CodedOutputStream output(&raw_output);
  241. message.SerializeWithCachedSizes(&output);
  242. ASSERT_FALSE(output.HadError());
  243. }
  244. // Serialize using WireFormat.
  245. {
  246. io::StringOutputStream raw_output(&dynamic_data);
  247. io::CodedOutputStream output(&raw_output);
  248. WireFormat::SerializeWithCachedSizes(message, size, &output);
  249. ASSERT_FALSE(output.HadError());
  250. }
  251. // Should be the same.
  252. // Don't use EXPECT_EQ here because we're comparing raw binary data and
  253. // we really don't want it dumped to stdout on failure.
  254. EXPECT_TRUE(dynamic_data == generated_data);
  255. // Should output in canonical order.
  256. TestUtil::ExpectAllFieldsAndExtensionsInOrder(dynamic_data);
  257. TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data);
  258. }
  259. TEST(WireFormatTest, ParseMultipleExtensionRanges) {
  260. // Make sure we can parse a message that contains multiple extensions ranges.
  261. unittest::TestFieldOrderings source;
  262. string data;
  263. TestUtil::SetAllFieldsAndExtensions(&source);
  264. source.SerializeToString(&data);
  265. {
  266. unittest::TestFieldOrderings dest;
  267. EXPECT_TRUE(dest.ParseFromString(data));
  268. EXPECT_EQ(source.DebugString(), dest.DebugString());
  269. }
  270. // Also test using reflection-based parsing.
  271. {
  272. unittest::TestFieldOrderings dest;
  273. io::ArrayInputStream raw_input(data.data(), data.size());
  274. io::CodedInputStream coded_input(&raw_input);
  275. EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest));
  276. EXPECT_EQ(source.DebugString(), dest.DebugString());
  277. }
  278. }
  279. const int kUnknownTypeId = 1550055;
  280. TEST(WireFormatTest, SerializeMessageSet) {
  281. // Set up a TestMessageSet with two known messages and an unknown one.
  282. unittest::TestMessageSet message_set;
  283. message_set.MutableExtension(
  284. unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
  285. message_set.MutableExtension(
  286. unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
  287. message_set.mutable_unknown_fields()->AddLengthDelimited(
  288. kUnknownTypeId, "bar");
  289. string data;
  290. ASSERT_TRUE(message_set.SerializeToString(&data));
  291. // Parse back using RawMessageSet and check the contents.
  292. unittest::RawMessageSet raw;
  293. ASSERT_TRUE(raw.ParseFromString(data));
  294. EXPECT_EQ(0, raw.unknown_fields().field_count());
  295. ASSERT_EQ(3, raw.item_size());
  296. EXPECT_EQ(
  297. unittest::TestMessageSetExtension1::descriptor()->extension(0)->number(),
  298. raw.item(0).type_id());
  299. EXPECT_EQ(
  300. unittest::TestMessageSetExtension2::descriptor()->extension(0)->number(),
  301. raw.item(1).type_id());
  302. EXPECT_EQ(kUnknownTypeId, raw.item(2).type_id());
  303. unittest::TestMessageSetExtension1 message1;
  304. EXPECT_TRUE(message1.ParseFromString(raw.item(0).message()));
  305. EXPECT_EQ(123, message1.i());
  306. unittest::TestMessageSetExtension2 message2;
  307. EXPECT_TRUE(message2.ParseFromString(raw.item(1).message()));
  308. EXPECT_EQ("foo", message2.str());
  309. EXPECT_EQ("bar", raw.item(2).message());
  310. }
  311. TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) {
  312. // Serialize a MessageSet to a stream and to a flat array using generated
  313. // code, and also using WireFormat, and check that the results are equal.
  314. // Set up a TestMessageSet with two known messages and an unknown one, as
  315. // above.
  316. unittest::TestMessageSet message_set;
  317. message_set.MutableExtension(
  318. unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
  319. message_set.MutableExtension(
  320. unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
  321. message_set.mutable_unknown_fields()->AddLengthDelimited(
  322. kUnknownTypeId, "bar");
  323. int size = message_set.ByteSize();
  324. EXPECT_EQ(size, message_set.GetCachedSize());
  325. ASSERT_EQ(size, WireFormat::ByteSize(message_set));
  326. string flat_data;
  327. string stream_data;
  328. string dynamic_data;
  329. flat_data.resize(size);
  330. stream_data.resize(size);
  331. // Serialize to flat array
  332. {
  333. uint8* target = reinterpret_cast<uint8*>(string_as_array(&flat_data));
  334. uint8* end = message_set.SerializeWithCachedSizesToArray(target);
  335. EXPECT_EQ(size, end - target);
  336. }
  337. // Serialize to buffer
  338. {
  339. io::ArrayOutputStream array_stream(string_as_array(&stream_data), size, 1);
  340. io::CodedOutputStream output_stream(&array_stream);
  341. message_set.SerializeWithCachedSizes(&output_stream);
  342. ASSERT_FALSE(output_stream.HadError());
  343. }
  344. // Serialize to buffer with WireFormat.
  345. {
  346. io::StringOutputStream string_stream(&dynamic_data);
  347. io::CodedOutputStream output_stream(&string_stream);
  348. WireFormat::SerializeWithCachedSizes(message_set, size, &output_stream);
  349. ASSERT_FALSE(output_stream.HadError());
  350. }
  351. EXPECT_TRUE(flat_data == stream_data);
  352. EXPECT_TRUE(flat_data == dynamic_data);
  353. }
  354. TEST(WireFormatTest, ParseMessageSet) {
  355. // Set up a RawMessageSet with two known messages and an unknown one.
  356. unittest::RawMessageSet raw;
  357. {
  358. unittest::RawMessageSet::Item* item = raw.add_item();
  359. item->set_type_id(
  360. unittest::TestMessageSetExtension1::descriptor()->extension(0)->number());
  361. unittest::TestMessageSetExtension1 message;
  362. message.set_i(123);
  363. message.SerializeToString(item->mutable_message());
  364. }
  365. {
  366. unittest::RawMessageSet::Item* item = raw.add_item();
  367. item->set_type_id(
  368. unittest::TestMessageSetExtension2::descriptor()->extension(0)->number());
  369. unittest::TestMessageSetExtension2 message;
  370. message.set_str("foo");
  371. message.SerializeToString(item->mutable_message());
  372. }
  373. {
  374. unittest::RawMessageSet::Item* item = raw.add_item();
  375. item->set_type_id(kUnknownTypeId);
  376. item->set_message("bar");
  377. }
  378. string data;
  379. ASSERT_TRUE(raw.SerializeToString(&data));
  380. // Parse as a TestMessageSet and check the contents.
  381. unittest::TestMessageSet message_set;
  382. ASSERT_TRUE(message_set.ParseFromString(data));
  383. EXPECT_EQ(123, message_set.GetExtension(
  384. unittest::TestMessageSetExtension1::message_set_extension).i());
  385. EXPECT_EQ("foo", message_set.GetExtension(
  386. unittest::TestMessageSetExtension2::message_set_extension).str());
  387. ASSERT_EQ(1, message_set.unknown_fields().field_count());
  388. ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED,
  389. message_set.unknown_fields().field(0).type());
  390. EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited());
  391. // Also parse using WireFormat.
  392. unittest::TestMessageSet dynamic_message_set;
  393. io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()),
  394. data.size());
  395. ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set));
  396. EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString());
  397. }
  398. TEST(WireFormatTest, RecursionLimit) {
  399. unittest::TestRecursiveMessage message;
  400. message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1);
  401. string data;
  402. message.SerializeToString(&data);
  403. {
  404. io::ArrayInputStream raw_input(data.data(), data.size());
  405. io::CodedInputStream input(&raw_input);
  406. input.SetRecursionLimit(4);
  407. unittest::TestRecursiveMessage message2;
  408. EXPECT_TRUE(message2.ParseFromCodedStream(&input));
  409. }
  410. {
  411. io::ArrayInputStream raw_input(data.data(), data.size());
  412. io::CodedInputStream input(&raw_input);
  413. input.SetRecursionLimit(3);
  414. unittest::TestRecursiveMessage message2;
  415. EXPECT_FALSE(message2.ParseFromCodedStream(&input));
  416. }
  417. }
  418. TEST(WireFormatTest, UnknownFieldRecursionLimit) {
  419. unittest::TestEmptyMessage message;
  420. message.mutable_unknown_fields()
  421. ->AddGroup(1234)
  422. ->AddGroup(1234)
  423. ->AddGroup(1234)
  424. ->AddGroup(1234)
  425. ->AddVarint(1234, 123);
  426. string data;
  427. message.SerializeToString(&data);
  428. {
  429. io::ArrayInputStream raw_input(data.data(), data.size());
  430. io::CodedInputStream input(&raw_input);
  431. input.SetRecursionLimit(4);
  432. unittest::TestEmptyMessage message2;
  433. EXPECT_TRUE(message2.ParseFromCodedStream(&input));
  434. }
  435. {
  436. io::ArrayInputStream raw_input(data.data(), data.size());
  437. io::CodedInputStream input(&raw_input);
  438. input.SetRecursionLimit(3);
  439. unittest::TestEmptyMessage message2;
  440. EXPECT_FALSE(message2.ParseFromCodedStream(&input));
  441. }
  442. }
  443. TEST(WireFormatTest, ZigZag) {
  444. // avoid line-wrapping
  445. #define LL(x) GOOGLE_LONGLONG(x)
  446. #define ULL(x) GOOGLE_ULONGLONG(x)
  447. #define ZigZagEncode32(x) WireFormatLite::ZigZagEncode32(x)
  448. #define ZigZagDecode32(x) WireFormatLite::ZigZagDecode32(x)
  449. #define ZigZagEncode64(x) WireFormatLite::ZigZagEncode64(x)
  450. #define ZigZagDecode64(x) WireFormatLite::ZigZagDecode64(x)
  451. EXPECT_EQ(0u, ZigZagEncode32( 0));
  452. EXPECT_EQ(1u, ZigZagEncode32(-1));
  453. EXPECT_EQ(2u, ZigZagEncode32( 1));
  454. EXPECT_EQ(3u, ZigZagEncode32(-2));
  455. EXPECT_EQ(0x7FFFFFFEu, ZigZagEncode32(0x3FFFFFFF));
  456. EXPECT_EQ(0x7FFFFFFFu, ZigZagEncode32(0xC0000000));
  457. EXPECT_EQ(0xFFFFFFFEu, ZigZagEncode32(0x7FFFFFFF));
  458. EXPECT_EQ(0xFFFFFFFFu, ZigZagEncode32(0x80000000));
  459. EXPECT_EQ( 0, ZigZagDecode32(0u));
  460. EXPECT_EQ(-1, ZigZagDecode32(1u));
  461. EXPECT_EQ( 1, ZigZagDecode32(2u));
  462. EXPECT_EQ(-2, ZigZagDecode32(3u));
  463. EXPECT_EQ(0x3FFFFFFF, ZigZagDecode32(0x7FFFFFFEu));
  464. EXPECT_EQ(0xC0000000, ZigZagDecode32(0x7FFFFFFFu));
  465. EXPECT_EQ(0x7FFFFFFF, ZigZagDecode32(0xFFFFFFFEu));
  466. EXPECT_EQ(0x80000000, ZigZagDecode32(0xFFFFFFFFu));
  467. EXPECT_EQ(0u, ZigZagEncode64( 0));
  468. EXPECT_EQ(1u, ZigZagEncode64(-1));
  469. EXPECT_EQ(2u, ZigZagEncode64( 1));
  470. EXPECT_EQ(3u, ZigZagEncode64(-2));
  471. EXPECT_EQ(ULL(0x000000007FFFFFFE), ZigZagEncode64(LL(0x000000003FFFFFFF)));
  472. EXPECT_EQ(ULL(0x000000007FFFFFFF), ZigZagEncode64(LL(0xFFFFFFFFC0000000)));
  473. EXPECT_EQ(ULL(0x00000000FFFFFFFE), ZigZagEncode64(LL(0x000000007FFFFFFF)));
  474. EXPECT_EQ(ULL(0x00000000FFFFFFFF), ZigZagEncode64(LL(0xFFFFFFFF80000000)));
  475. EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFE), ZigZagEncode64(LL(0x7FFFFFFFFFFFFFFF)));
  476. EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFF), ZigZagEncode64(LL(0x8000000000000000)));
  477. EXPECT_EQ( 0, ZigZagDecode64(0u));
  478. EXPECT_EQ(-1, ZigZagDecode64(1u));
  479. EXPECT_EQ( 1, ZigZagDecode64(2u));
  480. EXPECT_EQ(-2, ZigZagDecode64(3u));
  481. EXPECT_EQ(LL(0x000000003FFFFFFF), ZigZagDecode64(ULL(0x000000007FFFFFFE)));
  482. EXPECT_EQ(LL(0xFFFFFFFFC0000000), ZigZagDecode64(ULL(0x000000007FFFFFFF)));
  483. EXPECT_EQ(LL(0x000000007FFFFFFF), ZigZagDecode64(ULL(0x00000000FFFFFFFE)));
  484. EXPECT_EQ(LL(0xFFFFFFFF80000000), ZigZagDecode64(ULL(0x00000000FFFFFFFF)));
  485. EXPECT_EQ(LL(0x7FFFFFFFFFFFFFFF), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFE)));
  486. EXPECT_EQ(LL(0x8000000000000000), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFF)));
  487. // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1)
  488. // were chosen semi-randomly via keyboard bashing.
  489. EXPECT_EQ( 0, ZigZagDecode32(ZigZagEncode32( 0)));
  490. EXPECT_EQ( 1, ZigZagDecode32(ZigZagEncode32( 1)));
  491. EXPECT_EQ( -1, ZigZagDecode32(ZigZagEncode32( -1)));
  492. EXPECT_EQ(14927, ZigZagDecode32(ZigZagEncode32(14927)));
  493. EXPECT_EQ(-3612, ZigZagDecode32(ZigZagEncode32(-3612)));
  494. EXPECT_EQ( 0, ZigZagDecode64(ZigZagEncode64( 0)));
  495. EXPECT_EQ( 1, ZigZagDecode64(ZigZagEncode64( 1)));
  496. EXPECT_EQ( -1, ZigZagDecode64(ZigZagEncode64( -1)));
  497. EXPECT_EQ(14927, ZigZagDecode64(ZigZagEncode64(14927)));
  498. EXPECT_EQ(-3612, ZigZagDecode64(ZigZagEncode64(-3612)));
  499. EXPECT_EQ(LL(856912304801416), ZigZagDecode64(ZigZagEncode64(
  500. LL(856912304801416))));
  501. EXPECT_EQ(LL(-75123905439571256), ZigZagDecode64(ZigZagEncode64(
  502. LL(-75123905439571256))));
  503. }
  504. TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) {
  505. // At one point checks would trigger when parsing repeated fixed scalar
  506. // fields.
  507. protobuf_unittest::TestRepeatedScalarDifferentTagSizes msg1, msg2;
  508. for (int i = 0; i < 100; ++i) {
  509. msg1.add_repeated_fixed32(i);
  510. msg1.add_repeated_int32(i);
  511. msg1.add_repeated_fixed64(i);
  512. msg1.add_repeated_int64(i);
  513. msg1.add_repeated_float(i);
  514. msg1.add_repeated_uint64(i);
  515. }
  516. // Make sure that we have a variety of tag sizes.
  517. const google::protobuf::Descriptor* desc = msg1.GetDescriptor();
  518. const google::protobuf::FieldDescriptor* field;
  519. field = desc->FindFieldByName("repeated_fixed32");
  520. ASSERT_TRUE(field != NULL);
  521. ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
  522. field = desc->FindFieldByName("repeated_int32");
  523. ASSERT_TRUE(field != NULL);
  524. ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
  525. field = desc->FindFieldByName("repeated_fixed64");
  526. ASSERT_TRUE(field != NULL);
  527. ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
  528. field = desc->FindFieldByName("repeated_int64");
  529. ASSERT_TRUE(field != NULL);
  530. ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
  531. field = desc->FindFieldByName("repeated_float");
  532. ASSERT_TRUE(field != NULL);
  533. ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
  534. field = desc->FindFieldByName("repeated_uint64");
  535. ASSERT_TRUE(field != NULL);
  536. ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
  537. EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString()));
  538. EXPECT_EQ(msg1.DebugString(), msg2.DebugString());
  539. }
  540. class WireFormatInvalidInputTest : public testing::Test {
  541. protected:
  542. // Make a serialized TestAllTypes in which the field optional_nested_message
  543. // contains exactly the given bytes, which may be invalid.
  544. string MakeInvalidEmbeddedMessage(const char* bytes, int size) {
  545. const FieldDescriptor* field =
  546. unittest::TestAllTypes::descriptor()->FindFieldByName(
  547. "optional_nested_message");
  548. GOOGLE_CHECK(field != NULL);
  549. string result;
  550. {
  551. io::StringOutputStream raw_output(&result);
  552. io::CodedOutputStream output(&raw_output);
  553. WireFormatLite::WriteBytes(field->number(), string(bytes, size), &output);
  554. }
  555. return result;
  556. }
  557. // Make a serialized TestAllTypes in which the field optionalgroup
  558. // contains exactly the given bytes -- which may be invalid -- and
  559. // possibly no end tag.
  560. string MakeInvalidGroup(const char* bytes, int size, bool include_end_tag) {
  561. const FieldDescriptor* field =
  562. unittest::TestAllTypes::descriptor()->FindFieldByName(
  563. "optionalgroup");
  564. GOOGLE_CHECK(field != NULL);
  565. string result;
  566. {
  567. io::StringOutputStream raw_output(&result);
  568. io::CodedOutputStream output(&raw_output);
  569. output.WriteVarint32(WireFormat::MakeTag(field));
  570. output.WriteString(string(bytes, size));
  571. if (include_end_tag) {
  572. output.WriteVarint32(WireFormatLite::MakeTag(
  573. field->number(), WireFormatLite::WIRETYPE_END_GROUP));
  574. }
  575. }
  576. return result;
  577. }
  578. };
  579. TEST_F(WireFormatInvalidInputTest, InvalidSubMessage) {
  580. unittest::TestAllTypes message;
  581. // Control case.
  582. EXPECT_TRUE(message.ParseFromString(MakeInvalidEmbeddedMessage("", 0)));
  583. // The byte is a valid varint, but not a valid tag (zero).
  584. EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\0", 1)));
  585. // The byte is a malformed varint.
  586. EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\200", 1)));
  587. // The byte is an endgroup tag, but we aren't parsing a group.
  588. EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\014", 1)));
  589. // The byte is a valid varint but not a valid tag (bad wire type).
  590. EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\017", 1)));
  591. }
  592. TEST_F(WireFormatInvalidInputTest, InvalidGroup) {
  593. unittest::TestAllTypes message;
  594. // Control case.
  595. EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
  596. // Missing end tag. Groups cannot end at EOF.
  597. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
  598. // The byte is a valid varint, but not a valid tag (zero).
  599. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
  600. // The byte is a malformed varint.
  601. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
  602. // The byte is an endgroup tag, but not the right one for this group.
  603. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
  604. // The byte is a valid varint but not a valid tag (bad wire type).
  605. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
  606. }
  607. TEST_F(WireFormatInvalidInputTest, InvalidUnknownGroup) {
  608. // Use TestEmptyMessage so that the group made by MakeInvalidGroup will not
  609. // be a known tag number.
  610. unittest::TestEmptyMessage message;
  611. // Control case.
  612. EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
  613. // Missing end tag. Groups cannot end at EOF.
  614. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
  615. // The byte is a valid varint, but not a valid tag (zero).
  616. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
  617. // The byte is a malformed varint.
  618. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
  619. // The byte is an endgroup tag, but not the right one for this group.
  620. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
  621. // The byte is a valid varint but not a valid tag (bad wire type).
  622. EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
  623. }
  624. TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) {
  625. // Test a bug fix: SkipMessage should fail if the message contains a string
  626. // whose length would extend beyond the message end.
  627. unittest::TestAllTypes message;
  628. message.set_optional_string("foo foo foo foo");
  629. string data;
  630. message.SerializeToString(&data);
  631. // Chop some bytes off the end.
  632. data.resize(data.size() - 4);
  633. // Try to skip it. Note that the bug was only present when parsing to an
  634. // UnknownFieldSet.
  635. io::ArrayInputStream raw_input(data.data(), data.size());
  636. io::CodedInputStream coded_input(&raw_input);
  637. UnknownFieldSet unknown_fields;
  638. EXPECT_FALSE(WireFormat::SkipMessage(&coded_input, &unknown_fields));
  639. }
  640. // Test differences between string and bytes.
  641. // Value of a string type must be valid UTF-8 string. When UTF-8
  642. // validation is enabled (GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED):
  643. // WriteInvalidUTF8String: see error message.
  644. // ReadInvalidUTF8String: see error message.
  645. // WriteValidUTF8String: fine.
  646. // ReadValidUTF8String: fine.
  647. // WriteAnyBytes: fine.
  648. // ReadAnyBytes: fine.
  649. const char * kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0";
  650. // This used to be "Valid UTF-8: \x01\x02\u8C37\u6B4C", but MSVC seems to
  651. // interpret \u differently from GCC.
  652. const char * kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214";
  653. template<typename T>
  654. bool WriteMessage(const char *value, T *message, string *wire_buffer) {
  655. message->set_data(value);
  656. wire_buffer->clear();
  657. message->AppendToString(wire_buffer);
  658. return (wire_buffer->size() > 0);
  659. }
  660. template<typename T>
  661. bool ReadMessage(const string &wire_buffer, T *message) {
  662. return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
  663. }
  664. TEST(Utf8ValidationTest, WriteInvalidUTF8String) {
  665. string wire_buffer;
  666. protobuf_unittest::OneString input;
  667. vector<string> errors;
  668. {
  669. ScopedMemoryLog log;
  670. WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
  671. errors = log.GetMessages(ERROR);
  672. }
  673. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  674. ASSERT_EQ(1, errors.size());
  675. EXPECT_EQ("Encountered string containing invalid UTF-8 data while "
  676. "serializing protocol buffer. Strings must contain only UTF-8; "
  677. "use the 'bytes' type for raw bytes.",
  678. errors[0]);
  679. #else
  680. ASSERT_EQ(0, errors.size());
  681. #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  682. }
  683. TEST(Utf8ValidationTest, ReadInvalidUTF8String) {
  684. string wire_buffer;
  685. protobuf_unittest::OneString input;
  686. WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
  687. protobuf_unittest::OneString output;
  688. vector<string> errors;
  689. {
  690. ScopedMemoryLog log;
  691. ReadMessage(wire_buffer, &output);
  692. errors = log.GetMessages(ERROR);
  693. }
  694. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  695. ASSERT_EQ(1, errors.size());
  696. EXPECT_EQ("Encountered string containing invalid UTF-8 data while "
  697. "parsing protocol buffer. Strings must contain only UTF-8; "
  698. "use the 'bytes' type for raw bytes.",
  699. errors[0]);
  700. #else
  701. ASSERT_EQ(0, errors.size());
  702. #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  703. }
  704. TEST(Utf8ValidationTest, WriteValidUTF8String) {
  705. string wire_buffer;
  706. protobuf_unittest::OneString input;
  707. vector<string> errors;
  708. {
  709. ScopedMemoryLog log;
  710. WriteMessage(kValidUTF8String, &input, &wire_buffer);
  711. errors = log.GetMessages(ERROR);
  712. }
  713. ASSERT_EQ(0, errors.size());
  714. }
  715. TEST(Utf8ValidationTest, ReadValidUTF8String) {
  716. string wire_buffer;
  717. protobuf_unittest::OneString input;
  718. WriteMessage(kValidUTF8String, &input, &wire_buffer);
  719. protobuf_unittest::OneString output;
  720. vector<string> errors;
  721. {
  722. ScopedMemoryLog log;
  723. ReadMessage(wire_buffer, &output);
  724. errors = log.GetMessages(ERROR);
  725. }
  726. ASSERT_EQ(0, errors.size());
  727. EXPECT_EQ(input.data(), output.data());
  728. }
  729. // Bytes: anything can pass as bytes, use invalid UTF-8 string to test
  730. TEST(Utf8ValidationTest, WriteArbitraryBytes) {
  731. string wire_buffer;
  732. protobuf_unittest::OneBytes input;
  733. vector<string> errors;
  734. {
  735. ScopedMemoryLog log;
  736. WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
  737. errors = log.GetMessages(ERROR);
  738. }
  739. ASSERT_EQ(0, errors.size());
  740. }
  741. TEST(Utf8ValidationTest, ReadArbitraryBytes) {
  742. string wire_buffer;
  743. protobuf_unittest::OneBytes input;
  744. WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
  745. protobuf_unittest::OneBytes output;
  746. vector<string> errors;
  747. {
  748. ScopedMemoryLog log;
  749. ReadMessage(wire_buffer, &output);
  750. errors = log.GetMessages(ERROR);
  751. }
  752. ASSERT_EQ(0, errors.size());
  753. EXPECT_EQ(input.data(), output.data());
  754. }
  755. } // namespace
  756. } // namespace internal
  757. } // namespace protobuf
  758. } // namespace google