message_unittest.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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/io/coded_stream.h>
  46. #include <google/protobuf/io/zero_copy_stream.h>
  47. #include <google/protobuf/io/zero_copy_stream_impl.h>
  48. #include <google/protobuf/descriptor.pb.h>
  49. #include <google/protobuf/arena.h>
  50. #include <google/protobuf/descriptor.h>
  51. #include <google/protobuf/generated_message_reflection.h>
  52. #include <google/protobuf/stubs/logging.h>
  53. #include <google/protobuf/stubs/common.h>
  54. #include <google/protobuf/stubs/logging.h>
  55. #include <google/protobuf/testing/googletest.h>
  56. #include <gtest/gtest.h>
  57. #include <google/protobuf/stubs/io_win32.h>
  58. namespace google {
  59. namespace protobuf {
  60. #if defined(_WIN32)
  61. // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
  62. // them like we do below.
  63. using google::protobuf::internal::win32::close;
  64. using google::protobuf::internal::win32::open;
  65. #endif
  66. #ifndef O_BINARY
  67. #ifdef _O_BINARY
  68. #define O_BINARY _O_BINARY
  69. #else
  70. #define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
  71. #endif
  72. #endif
  73. TEST(MESSAGE_TEST_NAME, SerializeHelpers) {
  74. // TODO(kenton): Test more helpers? They're all two-liners so it seems
  75. // like a waste of time.
  76. UNITTEST::TestAllTypes message;
  77. TestUtil::SetAllFields(&message);
  78. std::stringstream stream;
  79. string str1("foo");
  80. string str2("bar");
  81. EXPECT_TRUE(message.SerializeToString(&str1));
  82. EXPECT_TRUE(message.AppendToString(&str2));
  83. EXPECT_TRUE(message.SerializeToOstream(&stream));
  84. EXPECT_EQ(str1.size() + 3, str2.size());
  85. EXPECT_EQ("bar", str2.substr(0, 3));
  86. // Don't use EXPECT_EQ because we don't want to dump raw binary data to
  87. // stdout.
  88. EXPECT_TRUE(str2.substr(3) == str1);
  89. // GCC gives some sort of error if we try to just do stream.str() == str1.
  90. string temp = stream.str();
  91. EXPECT_TRUE(temp == str1);
  92. EXPECT_TRUE(message.SerializeAsString() == str1);
  93. }
  94. TEST(MESSAGE_TEST_NAME, SerializeToBrokenOstream) {
  95. std::ofstream out;
  96. UNITTEST::TestAllTypes message;
  97. message.set_optional_int32(123);
  98. EXPECT_FALSE(message.SerializeToOstream(&out));
  99. }
  100. TEST(MESSAGE_TEST_NAME, ParseFromFileDescriptor) {
  101. string filename = TestSourceDir() +
  102. "/google/protobuf/testdata/golden_message";
  103. int file = open(filename.c_str(), O_RDONLY | O_BINARY);
  104. ASSERT_GE(file, 0);
  105. UNITTEST::TestAllTypes message;
  106. EXPECT_TRUE(message.ParseFromFileDescriptor(file));
  107. TestUtil::ExpectAllFieldsSet(message);
  108. EXPECT_GE(close(file), 0);
  109. }
  110. TEST(MESSAGE_TEST_NAME, ParsePackedFromFileDescriptor) {
  111. string filename =
  112. TestSourceDir() +
  113. "/google/protobuf/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. 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. 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<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 \"" + 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. google::protobuf::Message* test_all_types_pointer = &test_all_types;
  190. EXPECT_EQ(&test_all_types,
  191. google::protobuf::internal::DynamicCastToGenerated<UNITTEST::TestAllTypes>(
  192. test_all_types_pointer));
  193. EXPECT_EQ(nullptr,
  194. google::protobuf::internal::DynamicCastToGenerated<UNITTEST::TestRequired>(
  195. test_all_types_pointer));
  196. const google::protobuf::Message* test_all_types_pointer_const = &test_all_types;
  197. EXPECT_EQ(
  198. &test_all_types,
  199. google::protobuf::internal::DynamicCastToGenerated<const UNITTEST::TestAllTypes>(
  200. test_all_types_pointer_const));
  201. EXPECT_EQ(
  202. nullptr,
  203. google::protobuf::internal::DynamicCastToGenerated<const UNITTEST::TestRequired>(
  204. test_all_types_pointer_const));
  205. }
  206. #ifdef PROTOBUF_HAS_DEATH_TEST // death tests do not work on Windows yet.
  207. TEST(MESSAGE_TEST_NAME, SerializeFailsIfNotInitialized) {
  208. UNITTEST::TestRequired message;
  209. string data;
  210. EXPECT_DEBUG_DEATH(EXPECT_TRUE(message.SerializeToString(&data)),
  211. "Can't serialize message of type \"" +
  212. string(UNITTEST_PACKAGE_NAME) +
  213. ".TestRequired\" because "
  214. "it is missing required fields: a, b, c");
  215. }
  216. TEST(MESSAGE_TEST_NAME, CheckInitialized) {
  217. UNITTEST::TestRequired message;
  218. EXPECT_DEATH(message.CheckInitialized(),
  219. "Message of type \"" +
  220. string(UNITTEST_PACKAGE_NAME) +
  221. ".TestRequired\" is missing required "
  222. "fields: a, b, c");
  223. }
  224. #endif // PROTOBUF_HAS_DEATH_TEST
  225. namespace {
  226. // An input stream that repeats a string's content for a number of times. It
  227. // helps us create a really large input without consuming too much memory. Used
  228. // to test the parsing behavior when the input size exceeds 2G or close to it.
  229. class RepeatedInputStream : public io::ZeroCopyInputStream {
  230. public:
  231. RepeatedInputStream(const string& data, size_t count)
  232. : data_(data), count_(count), position_(0), total_byte_count_(0) {}
  233. virtual bool Next(const void** data, int* size) {
  234. if (position_ == data_.size()) {
  235. if (--count_ == 0) {
  236. return false;
  237. }
  238. position_ = 0;
  239. }
  240. *data = &data_[position_];
  241. *size = static_cast<int>(data_.size() - position_);
  242. position_ = data_.size();
  243. total_byte_count_ += *size;
  244. return true;
  245. }
  246. virtual void BackUp(int count) {
  247. position_ -= static_cast<size_t>(count);
  248. total_byte_count_ -= count;
  249. }
  250. virtual bool Skip(int count) {
  251. while (count > 0) {
  252. const void* data;
  253. int size;
  254. if (!Next(&data, &size)) {
  255. break;
  256. }
  257. if (size >= count) {
  258. BackUp(size - count);
  259. return true;
  260. } else {
  261. count -= size;
  262. }
  263. }
  264. return false;
  265. }
  266. virtual int64 ByteCount() const { return total_byte_count_; }
  267. private:
  268. string data_;
  269. size_t count_; // The number of strings that haven't been consuemd.
  270. size_t position_; // Position in the string for the next read.
  271. int64 total_byte_count_;
  272. };
  273. } // namespace
  274. TEST(MESSAGE_TEST_NAME, TestParseMessagesCloseTo2G) {
  275. // Create a message with a large string field.
  276. string value = string(64 * 1024 * 1024, 'x');
  277. UNITTEST::TestAllTypes message;
  278. message.set_optional_string(value);
  279. // Repeat this message in the input stream to make the total input size
  280. // close to 2G.
  281. string data = message.SerializeAsString();
  282. size_t count = static_cast<size_t>(kint32max) / data.size();
  283. RepeatedInputStream input(data, count);
  284. // The parsing should succeed.
  285. UNITTEST::TestAllTypes result;
  286. EXPECT_TRUE(result.ParseFromZeroCopyStream(&input));
  287. // When there are multiple occurences of a singulr field, the last one
  288. // should win.
  289. EXPECT_EQ(value, result.optional_string());
  290. }
  291. TEST(MESSAGE_TEST_NAME, TestParseMessagesOver2G) {
  292. // Create a message with a large string field.
  293. string value = string(64 * 1024 * 1024, 'x');
  294. UNITTEST::TestAllTypes message;
  295. message.set_optional_string(value);
  296. // Repeat this message in the input stream to make the total input size
  297. // larger than 2G.
  298. string data = message.SerializeAsString();
  299. size_t count = static_cast<size_t>(kint32max) / data.size() + 1;
  300. RepeatedInputStream input(data, count);
  301. // The parsing should fail.
  302. UNITTEST::TestAllTypes result;
  303. EXPECT_FALSE(result.ParseFromZeroCopyStream(&input));
  304. }
  305. TEST(MESSAGE_TEST_NAME, BypassInitializationCheckOnSerialize) {
  306. UNITTEST::TestRequired message;
  307. io::ArrayOutputStream raw_output(nullptr, 0);
  308. io::CodedOutputStream output(&raw_output);
  309. EXPECT_TRUE(message.SerializePartialToCodedStream(&output));
  310. }
  311. TEST(MESSAGE_TEST_NAME, FindInitializationErrors) {
  312. UNITTEST::TestRequired message;
  313. std::vector<string> errors;
  314. message.FindInitializationErrors(&errors);
  315. ASSERT_EQ(3, errors.size());
  316. EXPECT_EQ("a", errors[0]);
  317. EXPECT_EQ("b", errors[1]);
  318. EXPECT_EQ("c", errors[2]);
  319. }
  320. TEST(MESSAGE_TEST_NAME, ParseFailsOnInvalidMessageEnd) {
  321. UNITTEST::TestAllTypes message;
  322. // Control case.
  323. EXPECT_TRUE(message.ParseFromArray("", 0));
  324. // The byte is a valid varint, but not a valid tag (zero).
  325. EXPECT_FALSE(message.ParseFromArray("\0", 1));
  326. // The byte is a malformed varint.
  327. EXPECT_FALSE(message.ParseFromArray("\200", 1));
  328. // The byte is an endgroup tag, but we aren't parsing a group.
  329. EXPECT_FALSE(message.ParseFromArray("\014", 1));
  330. }
  331. // Regression test for b/23630858
  332. TEST(MESSAGE_TEST_NAME, MessageIsStillValidAfterParseFails) {
  333. UNITTEST::TestAllTypes message;
  334. // 9 0xFFs for the "optional_uint64" field.
  335. string invalid_data = "\x20\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
  336. EXPECT_FALSE(message.ParseFromString(invalid_data));
  337. message.Clear();
  338. EXPECT_EQ(0, message.optional_uint64());
  339. // invalid data for field "optional_string". Length prefix is 1 but no
  340. // payload.
  341. string invalid_string_data = "\x72\x01";
  342. {
  343. google::protobuf::Arena arena;
  344. UNITTEST::TestAllTypes* arena_message =
  345. google::protobuf::Arena::CreateMessage<UNITTEST::TestAllTypes>(&arena);
  346. EXPECT_FALSE(arena_message->ParseFromString(invalid_string_data));
  347. arena_message->Clear();
  348. EXPECT_EQ("", arena_message->optional_string());
  349. }
  350. }
  351. namespace {
  352. void ExpectMessageMerged(const UNITTEST::TestAllTypes& message) {
  353. EXPECT_EQ(3, message.optional_int32());
  354. EXPECT_EQ(2, message.optional_int64());
  355. EXPECT_EQ("hello", message.optional_string());
  356. }
  357. void AssignParsingMergeMessages(UNITTEST::TestAllTypes* msg1,
  358. UNITTEST::TestAllTypes* msg2,
  359. UNITTEST::TestAllTypes* msg3) {
  360. msg1->set_optional_int32(1);
  361. msg2->set_optional_int64(2);
  362. msg3->set_optional_int32(3);
  363. msg3->set_optional_string("hello");
  364. }
  365. } // namespace
  366. // Test that if an optional or required message/group field appears multiple
  367. // times in the input, they need to be merged.
  368. TEST(MESSAGE_TEST_NAME, ParsingMerge) {
  369. UNITTEST::TestParsingMerge::RepeatedFieldsGenerator generator;
  370. UNITTEST::TestAllTypes* msg1;
  371. UNITTEST::TestAllTypes* msg2;
  372. UNITTEST::TestAllTypes* msg3;
  373. #define ASSIGN_REPEATED_FIELD(FIELD) \
  374. msg1 = generator.add_##FIELD(); \
  375. msg2 = generator.add_##FIELD(); \
  376. msg3 = generator.add_##FIELD(); \
  377. AssignParsingMergeMessages(msg1, msg2, msg3)
  378. ASSIGN_REPEATED_FIELD(field1);
  379. ASSIGN_REPEATED_FIELD(field2);
  380. ASSIGN_REPEATED_FIELD(field3);
  381. ASSIGN_REPEATED_FIELD(ext1);
  382. ASSIGN_REPEATED_FIELD(ext2);
  383. #undef ASSIGN_REPEATED_FIELD
  384. #define ASSIGN_REPEATED_GROUP(FIELD) \
  385. msg1 = generator.add_##FIELD()->mutable_field1(); \
  386. msg2 = generator.add_##FIELD()->mutable_field1(); \
  387. msg3 = generator.add_##FIELD()->mutable_field1(); \
  388. AssignParsingMergeMessages(msg1, msg2, msg3)
  389. ASSIGN_REPEATED_GROUP(group1);
  390. ASSIGN_REPEATED_GROUP(group2);
  391. #undef ASSIGN_REPEATED_GROUP
  392. string buffer;
  393. generator.SerializeToString(&buffer);
  394. UNITTEST::TestParsingMerge parsing_merge;
  395. parsing_merge.ParseFromString(buffer);
  396. // Required and optional fields should be merged.
  397. ExpectMessageMerged(parsing_merge.required_all_types());
  398. ExpectMessageMerged(parsing_merge.optional_all_types());
  399. ExpectMessageMerged(parsing_merge.optionalgroup().optional_group_all_types());
  400. ExpectMessageMerged(
  401. parsing_merge.GetExtension(UNITTEST::TestParsingMerge::optional_ext));
  402. // Repeated fields should not be merged.
  403. EXPECT_EQ(3, parsing_merge.repeated_all_types_size());
  404. EXPECT_EQ(3, parsing_merge.repeatedgroup_size());
  405. EXPECT_EQ(
  406. 3, parsing_merge.ExtensionSize(UNITTEST::TestParsingMerge::repeated_ext));
  407. }
  408. TEST(MESSAGE_TEST_NAME, MergeFrom) {
  409. UNITTEST::TestAllTypes source, dest;
  410. // Optional fields
  411. source.set_optional_int32(1); // only source
  412. source.set_optional_int64(2); // both source and dest
  413. dest.set_optional_int64(3);
  414. dest.set_optional_uint32(4); // only dest
  415. // Optional fields with defaults
  416. source.set_default_int32(13); // only source
  417. source.set_default_int64(14); // both source and dest
  418. dest.set_default_int64(15);
  419. dest.set_default_uint32(16); // only dest
  420. // Repeated fields
  421. source.add_repeated_int32(5); // only source
  422. source.add_repeated_int32(6);
  423. source.add_repeated_int64(7); // both source and dest
  424. source.add_repeated_int64(8);
  425. dest.add_repeated_int64(9);
  426. dest.add_repeated_int64(10);
  427. dest.add_repeated_uint32(11); // only dest
  428. dest.add_repeated_uint32(12);
  429. dest.MergeFrom(source);
  430. // Optional fields: source overwrites dest if source is specified
  431. EXPECT_EQ(1, dest.optional_int32()); // only source: use source
  432. EXPECT_EQ(2, dest.optional_int64()); // source and dest: use source
  433. EXPECT_EQ(4, dest.optional_uint32()); // only dest: use dest
  434. EXPECT_EQ(0, dest.optional_uint64()); // neither: use default
  435. // Optional fields with defaults
  436. EXPECT_EQ(13, dest.default_int32()); // only source: use source
  437. EXPECT_EQ(14, dest.default_int64()); // source and dest: use source
  438. EXPECT_EQ(16, dest.default_uint32()); // only dest: use dest
  439. EXPECT_EQ(44, dest.default_uint64()); // neither: use default
  440. // Repeated fields: concatenate source onto the end of dest
  441. ASSERT_EQ(2, dest.repeated_int32_size());
  442. EXPECT_EQ(5, dest.repeated_int32(0));
  443. EXPECT_EQ(6, dest.repeated_int32(1));
  444. ASSERT_EQ(4, dest.repeated_int64_size());
  445. EXPECT_EQ(9, dest.repeated_int64(0));
  446. EXPECT_EQ(10, dest.repeated_int64(1));
  447. EXPECT_EQ(7, dest.repeated_int64(2));
  448. EXPECT_EQ(8, dest.repeated_int64(3));
  449. ASSERT_EQ(2, dest.repeated_uint32_size());
  450. EXPECT_EQ(11, dest.repeated_uint32(0));
  451. EXPECT_EQ(12, dest.repeated_uint32(1));
  452. ASSERT_EQ(0, dest.repeated_uint64_size());
  453. }
  454. TEST(MESSAGE_TEST_NAME, IsInitialized) {
  455. UNITTEST::TestIsInitialized msg;
  456. EXPECT_TRUE(msg.IsInitialized());
  457. UNITTEST::TestIsInitialized::SubMessage* sub_message =
  458. msg.mutable_sub_message();
  459. EXPECT_TRUE(msg.IsInitialized());
  460. UNITTEST::TestIsInitialized::SubMessage::SubGroup* sub_group =
  461. sub_message->mutable_subgroup();
  462. EXPECT_FALSE(msg.IsInitialized());
  463. sub_group->set_i(1);
  464. EXPECT_TRUE(msg.IsInitialized());
  465. }
  466. TEST(MESSAGE_FACTORY_TEST_NAME, GeneratedFactoryLookup) {
  467. EXPECT_EQ(MessageFactory::generated_factory()->GetPrototype(
  468. UNITTEST::TestAllTypes::descriptor()),
  469. &UNITTEST::TestAllTypes::default_instance());
  470. }
  471. TEST(MESSAGE_FACTORY_TEST_NAME, GeneratedFactoryUnknownType) {
  472. // Construct a new descriptor.
  473. DescriptorPool pool;
  474. FileDescriptorProto file;
  475. file.set_name("foo.proto");
  476. file.add_message_type()->set_name("Foo");
  477. const Descriptor* descriptor = pool.BuildFile(file)->message_type(0);
  478. // Trying to construct it should return nullptr.
  479. EXPECT_TRUE(MessageFactory::generated_factory()->GetPrototype(descriptor) ==
  480. nullptr);
  481. }
  482. } // namespace protobuf
  483. } // namespace google