诸暨麻将添加redis
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

634 行
22 KiB

  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 needs to be included as .inc as it depends on certain macros being
  35. // defined prior to its inclusion.
  36. #include <google/protobuf/message.h>
  37. #include <fcntl.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40. #ifndef _MSC_VER
  41. #include <unistd.h>
  42. #endif
  43. #include <fstream>
  44. #include <sstream>
  45. #include <google/protobuf/stubs/logging.h>
  46. #include <google/protobuf/stubs/common.h>
  47. #include <google/protobuf/stubs/logging.h>
  48. #include <google/protobuf/test_util2.h>
  49. #include <google/protobuf/io/coded_stream.h>
  50. #include <google/protobuf/io/zero_copy_stream.h>
  51. #include <google/protobuf/io/zero_copy_stream_impl.h>
  52. #include <google/protobuf/descriptor.pb.h>
  53. #include <google/protobuf/arena.h>
  54. #include <google/protobuf/descriptor.h>
  55. #include <google/protobuf/generated_message_reflection.h>
  56. #include <google/protobuf/testing/googletest.h>
  57. #include <gtest/gtest.h>
  58. #include <google/protobuf/io/io_win32.h>
  59. namespace google {
  60. namespace protobuf {
  61. #if defined(_WIN32)
  62. // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
  63. // them like we do below.
  64. using google::protobuf::io::win32::close;
  65. using google::protobuf::io::win32::open;
  66. #endif
  67. #ifndef O_BINARY
  68. #ifdef _O_BINARY
  69. #define O_BINARY _O_BINARY
  70. #else
  71. #define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
  72. #endif
  73. #endif
  74. TEST(MESSAGE_TEST_NAME, SerializeHelpers) {
  75. // TODO(kenton): Test more helpers? They're all two-liners so it seems
  76. // like a waste of time.
  77. UNITTEST::TestAllTypes message;
  78. TestUtil::SetAllFields(&message);
  79. std::stringstream stream;
  80. std::string str1("foo");
  81. std::string str2("bar");
  82. EXPECT_TRUE(message.SerializeToString(&str1));
  83. EXPECT_TRUE(message.AppendToString(&str2));
  84. EXPECT_TRUE(message.SerializeToOstream(&stream));
  85. EXPECT_EQ(str1.size() + 3, str2.size());
  86. EXPECT_EQ("bar", str2.substr(0, 3));
  87. // Don't use EXPECT_EQ because we don't want to dump raw binary data to
  88. // stdout.
  89. EXPECT_TRUE(str2.substr(3) == str1);
  90. // GCC gives some sort of error if we try to just do stream.str() == str1.
  91. std::string temp = stream.str();
  92. EXPECT_TRUE(temp == str1);
  93. EXPECT_TRUE(message.SerializeAsString() == str1);
  94. }
  95. TEST(MESSAGE_TEST_NAME, SerializeToBrokenOstream) {
  96. std::ofstream out;
  97. UNITTEST::TestAllTypes message;
  98. message.set_optional_int32(123);
  99. EXPECT_FALSE(message.SerializeToOstream(&out));
  100. }
  101. TEST(MESSAGE_TEST_NAME, ParseFromFileDescriptor) {
  102. std::string filename =
  103. TestUtil::GetTestDataPath("net/proto2/internal/testdata/golden_message");
  104. int file = open(filename.c_str(), O_RDONLY | O_BINARY);
  105. ASSERT_GE(file, 0);
  106. UNITTEST::TestAllTypes message;
  107. EXPECT_TRUE(message.ParseFromFileDescriptor(file));
  108. TestUtil::ExpectAllFieldsSet(message);
  109. EXPECT_GE(close(file), 0);
  110. }
  111. TEST(MESSAGE_TEST_NAME, ParsePackedFromFileDescriptor) {
  112. std::string filename = TestUtil::GetTestDataPath(
  113. "net/proto2/internal/testdata/golden_packed_fields_message");
  114. int file = open(filename.c_str(), O_RDONLY | O_BINARY);
  115. ASSERT_GE(file, 0);
  116. UNITTEST::TestPackedTypes message;
  117. EXPECT_TRUE(message.ParseFromFileDescriptor(file));
  118. TestUtil::ExpectPackedFieldsSet(message);
  119. EXPECT_GE(close(file), 0);
  120. }
  121. TEST(MESSAGE_TEST_NAME, ParseHelpers) {
  122. // TODO(kenton): Test more helpers? They're all two-liners so it seems
  123. // like a waste of time.
  124. std::string data;
  125. {
  126. // Set up.
  127. UNITTEST::TestAllTypes message;
  128. TestUtil::SetAllFields(&message);
  129. message.SerializeToString(&data);
  130. }
  131. {
  132. // Test ParseFromString.
  133. UNITTEST::TestAllTypes message;
  134. EXPECT_TRUE(message.ParseFromString(data));
  135. TestUtil::ExpectAllFieldsSet(message);
  136. }
  137. {
  138. // Test ParseFromIstream.
  139. UNITTEST::TestAllTypes message;
  140. std::stringstream stream(data);
  141. EXPECT_TRUE(message.ParseFromIstream(&stream));
  142. EXPECT_TRUE(stream.eof());
  143. TestUtil::ExpectAllFieldsSet(message);
  144. }
  145. {
  146. // Test ParseFromBoundedZeroCopyStream.
  147. std::string data_with_junk(data);
  148. data_with_junk.append("some junk on the end");
  149. io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size());
  150. UNITTEST::TestAllTypes message;
  151. EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size()));
  152. TestUtil::ExpectAllFieldsSet(message);
  153. }
  154. {
  155. // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if
  156. // EOF is reached before the expected number of bytes.
  157. io::ArrayInputStream stream(data.data(), data.size());
  158. UNITTEST::TestAllTypes message;
  159. EXPECT_FALSE(
  160. message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1));
  161. }
  162. }
  163. TEST(MESSAGE_TEST_NAME, ParseFailsIfNotInitialized) {
  164. UNITTEST::TestRequired message;
  165. std::vector<std::string> errors;
  166. {
  167. ScopedMemoryLog log;
  168. EXPECT_FALSE(message.ParseFromString(""));
  169. errors = log.GetMessages(ERROR);
  170. }
  171. ASSERT_EQ(1, errors.size());
  172. EXPECT_EQ(
  173. "Can't parse message of type \"" + std::string(UNITTEST_PACKAGE_NAME) +
  174. ".TestRequired\" because it is missing required fields: a, b, c",
  175. errors[0]);
  176. }
  177. TEST(MESSAGE_TEST_NAME, BypassInitializationCheckOnParse) {
  178. UNITTEST::TestRequired message;
  179. io::ArrayInputStream raw_input(nullptr, 0);
  180. io::CodedInputStream input(&raw_input);
  181. EXPECT_TRUE(message.MergePartialFromCodedStream(&input));
  182. }
  183. TEST(MESSAGE_TEST_NAME, InitializationErrorString) {
  184. UNITTEST::TestRequired message;
  185. EXPECT_EQ("a, b, c", message.InitializationErrorString());
  186. }
  187. TEST(MESSAGE_TEST_NAME, DynamicCastToGenerated) {
  188. UNITTEST::TestAllTypes test_all_types;
  189. Message* test_all_types_pointer = &test_all_types;
  190. EXPECT_EQ(&test_all_types, DynamicCastToGenerated<UNITTEST::TestAllTypes>(
  191. test_all_types_pointer));
  192. EXPECT_EQ(nullptr, DynamicCastToGenerated<UNITTEST::TestRequired>(
  193. test_all_types_pointer));
  194. const Message* test_all_types_pointer_const = &test_all_types;
  195. EXPECT_EQ(&test_all_types,
  196. DynamicCastToGenerated<const UNITTEST::TestAllTypes>(
  197. test_all_types_pointer_const));
  198. EXPECT_EQ(nullptr, DynamicCastToGenerated<const UNITTEST::TestRequired>(
  199. test_all_types_pointer_const));
  200. }
  201. #ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet.
  202. TEST(MESSAGE_TEST_NAME, SerializeFailsIfNotInitialized) {
  203. UNITTEST::TestRequired message;
  204. std::string data;
  205. EXPECT_DEBUG_DEATH(EXPECT_TRUE(message.SerializeToString(&data)),
  206. "Can't serialize message of type \"" +
  207. std::string(UNITTEST_PACKAGE_NAME) +
  208. ".TestRequired\" because "
  209. "it is missing required fields: a, b, c");
  210. }
  211. TEST(MESSAGE_TEST_NAME, CheckInitialized) {
  212. UNITTEST::TestRequired message;
  213. EXPECT_DEATH(message.CheckInitialized(),
  214. "Message of type \"" + std::string(UNITTEST_PACKAGE_NAME) +
  215. ".TestRequired\" is missing required "
  216. "fields: a, b, c");
  217. }
  218. #endif // PROTOBUF_HAS_DEATH_TEST
  219. namespace {
  220. // An input stream that repeats a std::string's content for a number of times.
  221. // It helps us create a really large input without consuming too much memory.
  222. // Used to test the parsing behavior when the input size exceeds 2G or close to
  223. // it.
  224. class RepeatedInputStream : public io::ZeroCopyInputStream {
  225. public:
  226. RepeatedInputStream(const std::string& data, size_t count)
  227. : data_(data), count_(count), position_(0), total_byte_count_(0) {}
  228. bool Next(const void** data, int* size) override {
  229. if (position_ == data_.size()) {
  230. if (--count_ == 0) {
  231. return false;
  232. }
  233. position_ = 0;
  234. }
  235. *data = &data_[position_];
  236. *size = static_cast<int>(data_.size() - position_);
  237. position_ = data_.size();
  238. total_byte_count_ += *size;
  239. return true;
  240. }
  241. void BackUp(int count) override {
  242. position_ -= static_cast<size_t>(count);
  243. total_byte_count_ -= count;
  244. }
  245. bool Skip(int count) override {
  246. while (count > 0) {
  247. const void* data;
  248. int size;
  249. if (!Next(&data, &size)) {
  250. break;
  251. }
  252. if (size >= count) {
  253. BackUp(size - count);
  254. return true;
  255. } else {
  256. count -= size;
  257. }
  258. }
  259. return false;
  260. }
  261. int64_t ByteCount() const override { return total_byte_count_; }
  262. private:
  263. std::string data_;
  264. size_t count_; // The number of strings that haven't been consuemd.
  265. size_t position_; // Position in the std::string for the next read.
  266. int64 total_byte_count_;
  267. };
  268. } // namespace
  269. TEST(MESSAGE_TEST_NAME, TestParseMessagesCloseTo2G) {
  270. // Create a message with a large std::string field.
  271. std::string value = std::string(64 * 1024 * 1024, 'x');
  272. UNITTEST::TestAllTypes message;
  273. message.set_optional_string(value);
  274. // Repeat this message in the input stream to make the total input size
  275. // close to 2G.
  276. std::string data = message.SerializeAsString();
  277. size_t count = static_cast<size_t>(kint32max) / data.size();
  278. RepeatedInputStream input(data, count);
  279. // The parsing should succeed.
  280. UNITTEST::TestAllTypes result;
  281. EXPECT_TRUE(result.ParseFromZeroCopyStream(&input));
  282. // When there are multiple occurences of a singulr field, the last one
  283. // should win.
  284. EXPECT_EQ(value, result.optional_string());
  285. }
  286. TEST(MESSAGE_TEST_NAME, TestParseMessagesOver2G) {
  287. // Create a message with a large std::string field.
  288. std::string value = std::string(64 * 1024 * 1024, 'x');
  289. UNITTEST::TestAllTypes message;
  290. message.set_optional_string(value);
  291. // Repeat this message in the input stream to make the total input size
  292. // larger than 2G.
  293. std::string data = message.SerializeAsString();
  294. size_t count = static_cast<size_t>(kint32max) / data.size() + 1;
  295. RepeatedInputStream input(data, count);
  296. // The parsing should fail.
  297. UNITTEST::TestAllTypes result;
  298. EXPECT_FALSE(result.ParseFromZeroCopyStream(&input));
  299. }
  300. TEST(MESSAGE_TEST_NAME, BypassInitializationCheckOnSerialize) {
  301. UNITTEST::TestRequired message;
  302. io::ArrayOutputStream raw_output(nullptr, 0);
  303. io::CodedOutputStream output(&raw_output);
  304. EXPECT_TRUE(message.SerializePartialToCodedStream(&output));
  305. }
  306. TEST(MESSAGE_TEST_NAME, FindInitializationErrors) {
  307. UNITTEST::TestRequired message;
  308. std::vector<std::string> errors;
  309. message.FindInitializationErrors(&errors);
  310. ASSERT_EQ(3, errors.size());
  311. EXPECT_EQ("a", errors[0]);
  312. EXPECT_EQ("b", errors[1]);
  313. EXPECT_EQ("c", errors[2]);
  314. }
  315. TEST(MESSAGE_TEST_NAME, ParseFailsOnInvalidMessageEnd) {
  316. UNITTEST::TestAllTypes message;
  317. // Control case.
  318. EXPECT_TRUE(message.ParseFromArray("", 0));
  319. // The byte is a valid varint, but not a valid tag (zero).
  320. EXPECT_FALSE(message.ParseFromArray("\0", 1));
  321. // The byte is a malformed varint.
  322. EXPECT_FALSE(message.ParseFromArray("\200", 1));
  323. // The byte is an endgroup tag, but we aren't parsing a group.
  324. EXPECT_FALSE(message.ParseFromArray("\014", 1));
  325. }
  326. // Regression test for b/23630858
  327. TEST(MESSAGE_TEST_NAME, MessageIsStillValidAfterParseFails) {
  328. UNITTEST::TestAllTypes message;
  329. // 9 0xFFs for the "optional_uint64" field.
  330. std::string invalid_data = "\x20\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
  331. EXPECT_FALSE(message.ParseFromString(invalid_data));
  332. message.Clear();
  333. EXPECT_EQ(0, message.optional_uint64());
  334. // invalid data for field "optional_string". Length prefix is 1 but no
  335. // payload.
  336. std::string invalid_string_data = "\x72\x01";
  337. {
  338. Arena arena;
  339. UNITTEST::TestAllTypes* arena_message =
  340. Arena::CreateMessage<UNITTEST::TestAllTypes>(&arena);
  341. EXPECT_FALSE(arena_message->ParseFromString(invalid_string_data));
  342. arena_message->Clear();
  343. EXPECT_EQ("", arena_message->optional_string());
  344. }
  345. }
  346. namespace {
  347. void ExpectMessageMerged(const UNITTEST::TestAllTypes& message) {
  348. EXPECT_EQ(3, message.optional_int32());
  349. EXPECT_EQ(2, message.optional_int64());
  350. EXPECT_EQ("hello", message.optional_string());
  351. }
  352. void AssignParsingMergeMessages(UNITTEST::TestAllTypes* msg1,
  353. UNITTEST::TestAllTypes* msg2,
  354. UNITTEST::TestAllTypes* msg3) {
  355. msg1->set_optional_int32(1);
  356. msg2->set_optional_int64(2);
  357. msg3->set_optional_int32(3);
  358. msg3->set_optional_string("hello");
  359. }
  360. } // namespace
  361. // Test that if an optional or required message/group field appears multiple
  362. // times in the input, they need to be merged.
  363. TEST(MESSAGE_TEST_NAME, ParsingMerge) {
  364. UNITTEST::TestParsingMerge::RepeatedFieldsGenerator generator;
  365. UNITTEST::TestAllTypes* msg1;
  366. UNITTEST::TestAllTypes* msg2;
  367. UNITTEST::TestAllTypes* msg3;
  368. #define ASSIGN_REPEATED_FIELD(FIELD) \
  369. msg1 = generator.add_##FIELD(); \
  370. msg2 = generator.add_##FIELD(); \
  371. msg3 = generator.add_##FIELD(); \
  372. AssignParsingMergeMessages(msg1, msg2, msg3)
  373. ASSIGN_REPEATED_FIELD(field1);
  374. ASSIGN_REPEATED_FIELD(field2);
  375. ASSIGN_REPEATED_FIELD(field3);
  376. ASSIGN_REPEATED_FIELD(ext1);
  377. ASSIGN_REPEATED_FIELD(ext2);
  378. #undef ASSIGN_REPEATED_FIELD
  379. #define ASSIGN_REPEATED_GROUP(FIELD) \
  380. msg1 = generator.add_##FIELD()->mutable_field1(); \
  381. msg2 = generator.add_##FIELD()->mutable_field1(); \
  382. msg3 = generator.add_##FIELD()->mutable_field1(); \
  383. AssignParsingMergeMessages(msg1, msg2, msg3)
  384. ASSIGN_REPEATED_GROUP(group1);
  385. ASSIGN_REPEATED_GROUP(group2);
  386. #undef ASSIGN_REPEATED_GROUP
  387. std::string buffer;
  388. generator.SerializeToString(&buffer);
  389. UNITTEST::TestParsingMerge parsing_merge;
  390. parsing_merge.ParseFromString(buffer);
  391. // Required and optional fields should be merged.
  392. ExpectMessageMerged(parsing_merge.required_all_types());
  393. ExpectMessageMerged(parsing_merge.optional_all_types());
  394. ExpectMessageMerged(parsing_merge.optionalgroup().optional_group_all_types());
  395. ExpectMessageMerged(
  396. parsing_merge.GetExtension(UNITTEST::TestParsingMerge::optional_ext));
  397. // Repeated fields should not be merged.
  398. EXPECT_EQ(3, parsing_merge.repeated_all_types_size());
  399. EXPECT_EQ(3, parsing_merge.repeatedgroup_size());
  400. EXPECT_EQ(
  401. 3, parsing_merge.ExtensionSize(UNITTEST::TestParsingMerge::repeated_ext));
  402. }
  403. TEST(MESSAGE_TEST_NAME, MergeFrom) {
  404. UNITTEST::TestAllTypes source, dest;
  405. // Optional fields
  406. source.set_optional_int32(1); // only source
  407. source.set_optional_int64(2); // both source and dest
  408. dest.set_optional_int64(3);
  409. dest.set_optional_uint32(4); // only dest
  410. // Optional fields with defaults
  411. source.set_default_int32(13); // only source
  412. source.set_default_int64(14); // both source and dest
  413. dest.set_default_int64(15);
  414. dest.set_default_uint32(16); // only dest
  415. // Repeated fields
  416. source.add_repeated_int32(5); // only source
  417. source.add_repeated_int32(6);
  418. source.add_repeated_int64(7); // both source and dest
  419. source.add_repeated_int64(8);
  420. dest.add_repeated_int64(9);
  421. dest.add_repeated_int64(10);
  422. dest.add_repeated_uint32(11); // only dest
  423. dest.add_repeated_uint32(12);
  424. dest.MergeFrom(source);
  425. // Optional fields: source overwrites dest if source is specified
  426. EXPECT_EQ(1, dest.optional_int32()); // only source: use source
  427. EXPECT_EQ(2, dest.optional_int64()); // source and dest: use source
  428. EXPECT_EQ(4, dest.optional_uint32()); // only dest: use dest
  429. EXPECT_EQ(0, dest.optional_uint64()); // neither: use default
  430. // Optional fields with defaults
  431. EXPECT_EQ(13, dest.default_int32()); // only source: use source
  432. EXPECT_EQ(14, dest.default_int64()); // source and dest: use source
  433. EXPECT_EQ(16, dest.default_uint32()); // only dest: use dest
  434. EXPECT_EQ(44, dest.default_uint64()); // neither: use default
  435. // Repeated fields: concatenate source onto the end of dest
  436. ASSERT_EQ(2, dest.repeated_int32_size());
  437. EXPECT_EQ(5, dest.repeated_int32(0));
  438. EXPECT_EQ(6, dest.repeated_int32(1));
  439. ASSERT_EQ(4, dest.repeated_int64_size());
  440. EXPECT_EQ(9, dest.repeated_int64(0));
  441. EXPECT_EQ(10, dest.repeated_int64(1));
  442. EXPECT_EQ(7, dest.repeated_int64(2));
  443. EXPECT_EQ(8, dest.repeated_int64(3));
  444. ASSERT_EQ(2, dest.repeated_uint32_size());
  445. EXPECT_EQ(11, dest.repeated_uint32(0));
  446. EXPECT_EQ(12, dest.repeated_uint32(1));
  447. ASSERT_EQ(0, dest.repeated_uint64_size());
  448. }
  449. TEST(MESSAGE_TEST_NAME, IsInitialized) {
  450. UNITTEST::TestIsInitialized msg;
  451. EXPECT_TRUE(msg.IsInitialized());
  452. UNITTEST::TestIsInitialized::SubMessage* sub_message =
  453. msg.mutable_sub_message();
  454. EXPECT_TRUE(msg.IsInitialized());
  455. UNITTEST::TestIsInitialized::SubMessage::SubGroup* sub_group =
  456. sub_message->mutable_subgroup();
  457. EXPECT_FALSE(msg.IsInitialized());
  458. sub_group->set_i(1);
  459. EXPECT_TRUE(msg.IsInitialized());
  460. }
  461. TEST(MESSAGE_FACTORY_TEST_NAME, GeneratedFactoryLookup) {
  462. EXPECT_EQ(MessageFactory::generated_factory()->GetPrototype(
  463. UNITTEST::TestAllTypes::descriptor()),
  464. &UNITTEST::TestAllTypes::default_instance());
  465. }
  466. TEST(MESSAGE_FACTORY_TEST_NAME, GeneratedFactoryUnknownType) {
  467. // Construct a new descriptor.
  468. DescriptorPool pool;
  469. FileDescriptorProto file;
  470. file.set_name("foo.proto");
  471. file.add_message_type()->set_name("Foo");
  472. const Descriptor* descriptor = pool.BuildFile(file)->message_type(0);
  473. // Trying to construct it should return nullptr.
  474. EXPECT_TRUE(MessageFactory::generated_factory()->GetPrototype(descriptor) ==
  475. nullptr);
  476. }
  477. TEST(MESSAGE_TEST_NAME, MOMIParserEdgeCases) {
  478. {
  479. UNITTEST::TestAllTypes msg;
  480. // Parser ends in last 16 bytes of buffer due to a 0.
  481. std::string data;
  482. // 12 bytes of data
  483. for (int i = 0; i < 4; i++) data += "\370\1\1";
  484. // 13 byte is terminator
  485. data += '\0'; // Terminator
  486. // followed by the rest of the stream
  487. // space is ascii 32 so no end group
  488. data += std::string(30, ' ');
  489. io::ArrayInputStream zcis(data.data(), data.size(), 17);
  490. io::CodedInputStream cis(&zcis);
  491. EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis));
  492. EXPECT_EQ(cis.CurrentPosition(), 3 * 4 + 1);
  493. }
  494. {
  495. // Parser ends in last 16 bytes of buffer due to a end-group.
  496. // Must use a message that is a group. Otherwise ending on a group end is
  497. // a failure.
  498. UNITTEST::TestAllTypes::OptionalGroup msg;
  499. std::string data;
  500. for (int i = 0; i < 3; i++) data += "\370\1\1";
  501. data += '\14'; // Octal end-group tag 12 (1 * 8 + 4(
  502. data += std::string(30, ' ');
  503. io::ArrayInputStream zcis(data.data(), data.size(), 17);
  504. io::CodedInputStream cis(&zcis);
  505. EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis));
  506. EXPECT_EQ(cis.CurrentPosition(), 3 * 3 + 1);
  507. EXPECT_TRUE(cis.LastTagWas(12));
  508. }
  509. {
  510. // Parser ends in last 16 bytes of buffer due to a end-group. But is inside
  511. // a length delimited field.
  512. // a failure.
  513. UNITTEST::TestAllTypes::OptionalGroup msg;
  514. std::string data;
  515. data += "\22\3foo";
  516. data += '\14'; // Octal end-group tag 12 (1 * 8 + 4(
  517. data += std::string(30, ' ');
  518. io::ArrayInputStream zcis(data.data(), data.size(), 17);
  519. io::CodedInputStream cis(&zcis);
  520. EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis));
  521. EXPECT_EQ(cis.CurrentPosition(), 6);
  522. EXPECT_TRUE(cis.LastTagWas(12));
  523. }
  524. {
  525. // Parser fails when ending on 0 if from ZeroCopyInputStream
  526. UNITTEST::TestAllTypes msg;
  527. std::string data;
  528. // 12 bytes of data
  529. for (int i = 0; i < 4; i++) data += "\370\1\1";
  530. // 13 byte is terminator
  531. data += '\0'; // Terminator
  532. data += std::string(30, ' ');
  533. io::ArrayInputStream zcis(data.data(), data.size(), 17);
  534. EXPECT_FALSE(msg.ParsePartialFromZeroCopyStream(&zcis));
  535. }
  536. }
  537. } // namespace protobuf
  538. } // namespace google