reflection_ops_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #include <google/protobuf/reflection_ops.h>
  34. #include <google/protobuf/descriptor.h>
  35. #include <google/protobuf/unittest.pb.h>
  36. #include <google/protobuf/test_util.h>
  37. #include <google/protobuf/stubs/logging.h>
  38. #include <google/protobuf/stubs/common.h>
  39. #include <google/protobuf/testing/googletest.h>
  40. #include <gtest/gtest.h>
  41. #include <google/protobuf/stubs/strutil.h>
  42. namespace google {
  43. namespace protobuf {
  44. namespace internal {
  45. namespace {
  46. TEST(ReflectionOpsTest, SanityCheck) {
  47. unittest::TestAllTypes message;
  48. TestUtil::SetAllFields(&message);
  49. TestUtil::ExpectAllFieldsSet(message);
  50. }
  51. TEST(ReflectionOpsTest, Copy) {
  52. unittest::TestAllTypes message, message2;
  53. TestUtil::SetAllFields(&message);
  54. ReflectionOps::Copy(message, &message2);
  55. TestUtil::ExpectAllFieldsSet(message2);
  56. // Copying from self should be a no-op.
  57. ReflectionOps::Copy(message2, &message2);
  58. TestUtil::ExpectAllFieldsSet(message2);
  59. }
  60. TEST(ReflectionOpsTest, CopyExtensions) {
  61. unittest::TestAllExtensions message, message2;
  62. TestUtil::SetAllExtensions(&message);
  63. ReflectionOps::Copy(message, &message2);
  64. TestUtil::ExpectAllExtensionsSet(message2);
  65. }
  66. TEST(ReflectionOpsTest, CopyOneof) {
  67. unittest::TestOneof2 message, message2;
  68. TestUtil::SetOneof1(&message);
  69. ReflectionOps::Copy(message, &message2);
  70. TestUtil::ExpectOneofSet1(message2);
  71. TestUtil::SetOneof2(&message);
  72. TestUtil::ExpectOneofSet2(message);
  73. ReflectionOps::Copy(message, &message2);
  74. TestUtil::ExpectOneofSet2(message2);
  75. }
  76. TEST(ReflectionOpsTest, Merge) {
  77. // Note: Copy is implemented in terms of Merge() so technically the Copy
  78. // test already tested most of this.
  79. unittest::TestAllTypes message, message2;
  80. TestUtil::SetAllFields(&message);
  81. // This field will test merging into an empty spot.
  82. message2.set_optional_int32(message.optional_int32());
  83. message.clear_optional_int32();
  84. // This tests overwriting.
  85. message2.set_optional_string(message.optional_string());
  86. message.set_optional_string("something else");
  87. // This tests concatenating.
  88. message2.add_repeated_int32(message.repeated_int32(1));
  89. int32 i = message.repeated_int32(0);
  90. message.clear_repeated_int32();
  91. message.add_repeated_int32(i);
  92. ReflectionOps::Merge(message2, &message);
  93. TestUtil::ExpectAllFieldsSet(message);
  94. }
  95. TEST(ReflectionOpsTest, MergeExtensions) {
  96. // Note: Copy is implemented in terms of Merge() so technically the Copy
  97. // test already tested most of this.
  98. unittest::TestAllExtensions message, message2;
  99. TestUtil::SetAllExtensions(&message);
  100. // This field will test merging into an empty spot.
  101. message2.SetExtension(unittest::optional_int32_extension,
  102. message.GetExtension(unittest::optional_int32_extension));
  103. message.ClearExtension(unittest::optional_int32_extension);
  104. // This tests overwriting.
  105. message2.SetExtension(unittest::optional_string_extension,
  106. message.GetExtension(unittest::optional_string_extension));
  107. message.SetExtension(unittest::optional_string_extension, "something else");
  108. // This tests concatenating.
  109. message2.AddExtension(unittest::repeated_int32_extension,
  110. message.GetExtension(unittest::repeated_int32_extension, 1));
  111. int32 i = message.GetExtension(unittest::repeated_int32_extension, 0);
  112. message.ClearExtension(unittest::repeated_int32_extension);
  113. message.AddExtension(unittest::repeated_int32_extension, i);
  114. ReflectionOps::Merge(message2, &message);
  115. TestUtil::ExpectAllExtensionsSet(message);
  116. }
  117. TEST(ReflectionOpsTest, MergeUnknown) {
  118. // Test that the messages' UnknownFieldSets are correctly merged.
  119. unittest::TestEmptyMessage message1, message2;
  120. message1.mutable_unknown_fields()->AddVarint(1234, 1);
  121. message2.mutable_unknown_fields()->AddVarint(1234, 2);
  122. ReflectionOps::Merge(message2, &message1);
  123. ASSERT_EQ(2, message1.unknown_fields().field_count());
  124. ASSERT_EQ(UnknownField::TYPE_VARINT,
  125. message1.unknown_fields().field(0).type());
  126. EXPECT_EQ(1, message1.unknown_fields().field(0).varint());
  127. ASSERT_EQ(UnknownField::TYPE_VARINT,
  128. message1.unknown_fields().field(1).type());
  129. EXPECT_EQ(2, message1.unknown_fields().field(1).varint());
  130. }
  131. TEST(ReflectionOpsTest, MergeOneof) {
  132. unittest::TestOneof2 message1, message2;
  133. TestUtil::SetOneof1(&message1);
  134. // Merge to empty message
  135. ReflectionOps::Merge(message1, &message2);
  136. TestUtil::ExpectOneofSet1(message2);
  137. // Merge with the same oneof fields
  138. ReflectionOps::Merge(message1, &message2);
  139. TestUtil::ExpectOneofSet1(message2);
  140. // Merge with different oneof fields
  141. TestUtil::SetOneof2(&message1);
  142. ReflectionOps::Merge(message1, &message2);
  143. TestUtil::ExpectOneofSet2(message2);
  144. }
  145. #ifdef PROTOBUF_HAS_DEATH_TEST
  146. TEST(ReflectionOpsTest, MergeFromSelf) {
  147. // Note: Copy is implemented in terms of Merge() so technically the Copy
  148. // test already tested most of this.
  149. unittest::TestAllTypes message;
  150. EXPECT_DEATH(
  151. ReflectionOps::Merge(message, &message),
  152. "&from");
  153. }
  154. #endif // PROTOBUF_HAS_DEATH_TEST
  155. TEST(ReflectionOpsTest, Clear) {
  156. unittest::TestAllTypes message;
  157. TestUtil::SetAllFields(&message);
  158. ReflectionOps::Clear(&message);
  159. TestUtil::ExpectClear(message);
  160. // Check that getting embedded messages returns the objects created during
  161. // SetAllFields() rather than default instances.
  162. EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
  163. &message.optionalgroup());
  164. EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
  165. &message.optional_nested_message());
  166. EXPECT_NE(&unittest::ForeignMessage::default_instance(),
  167. &message.optional_foreign_message());
  168. EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
  169. &message.optional_import_message());
  170. }
  171. TEST(ReflectionOpsTest, ClearExtensions) {
  172. unittest::TestAllExtensions message;
  173. TestUtil::SetAllExtensions(&message);
  174. ReflectionOps::Clear(&message);
  175. TestUtil::ExpectExtensionsClear(message);
  176. // Check that getting embedded messages returns the objects created during
  177. // SetAllExtensions() rather than default instances.
  178. EXPECT_NE(&unittest::OptionalGroup_extension::default_instance(),
  179. &message.GetExtension(unittest::optionalgroup_extension));
  180. EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
  181. &message.GetExtension(unittest::optional_nested_message_extension));
  182. EXPECT_NE(&unittest::ForeignMessage::default_instance(),
  183. &message.GetExtension(
  184. unittest::optional_foreign_message_extension));
  185. EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
  186. &message.GetExtension(unittest::optional_import_message_extension));
  187. }
  188. TEST(ReflectionOpsTest, ClearUnknown) {
  189. // Test that the message's UnknownFieldSet is correctly cleared.
  190. unittest::TestEmptyMessage message;
  191. message.mutable_unknown_fields()->AddVarint(1234, 1);
  192. ReflectionOps::Clear(&message);
  193. EXPECT_EQ(0, message.unknown_fields().field_count());
  194. }
  195. TEST(ReflectionOpsTest, ClearOneof) {
  196. unittest::TestOneof2 message;
  197. TestUtil::ExpectOneofClear(message);
  198. TestUtil::SetOneof1(&message);
  199. TestUtil::ExpectOneofSet1(message);
  200. ReflectionOps::Clear(&message);
  201. TestUtil::ExpectOneofClear(message);
  202. TestUtil::SetOneof1(&message);
  203. TestUtil::ExpectOneofSet1(message);
  204. TestUtil::SetOneof2(&message);
  205. TestUtil::ExpectOneofSet2(message);
  206. ReflectionOps::Clear(&message);
  207. TestUtil::ExpectOneofClear(message);
  208. }
  209. TEST(ReflectionOpsTest, DiscardUnknownFields) {
  210. unittest::TestAllTypes message;
  211. TestUtil::SetAllFields(&message);
  212. // Set some unknown fields in message.
  213. message.mutable_unknown_fields()
  214. ->AddVarint(123456, 654321);
  215. message.mutable_optional_nested_message()
  216. ->mutable_unknown_fields()
  217. ->AddVarint(123456, 654321);
  218. message.mutable_repeated_nested_message(0)
  219. ->mutable_unknown_fields()
  220. ->AddVarint(123456, 654321);
  221. EXPECT_EQ(1, message.unknown_fields().field_count());
  222. EXPECT_EQ(1, message.optional_nested_message()
  223. .unknown_fields().field_count());
  224. EXPECT_EQ(1, message.repeated_nested_message(0)
  225. .unknown_fields().field_count());
  226. // Discard them.
  227. ReflectionOps::DiscardUnknownFields(&message);
  228. TestUtil::ExpectAllFieldsSet(message);
  229. EXPECT_EQ(0, message.unknown_fields().field_count());
  230. EXPECT_EQ(0, message.optional_nested_message()
  231. .unknown_fields().field_count());
  232. EXPECT_EQ(0, message.repeated_nested_message(0)
  233. .unknown_fields().field_count());
  234. }
  235. TEST(ReflectionOpsTest, DiscardUnknownExtensions) {
  236. unittest::TestAllExtensions message;
  237. TestUtil::SetAllExtensions(&message);
  238. // Set some unknown fields.
  239. message.mutable_unknown_fields()
  240. ->AddVarint(123456, 654321);
  241. message.MutableExtension(unittest::optional_nested_message_extension)
  242. ->mutable_unknown_fields()
  243. ->AddVarint(123456, 654321);
  244. message.MutableExtension(unittest::repeated_nested_message_extension, 0)
  245. ->mutable_unknown_fields()
  246. ->AddVarint(123456, 654321);
  247. EXPECT_EQ(1, message.unknown_fields().field_count());
  248. EXPECT_EQ(1,
  249. message.GetExtension(unittest::optional_nested_message_extension)
  250. .unknown_fields().field_count());
  251. EXPECT_EQ(1,
  252. message.GetExtension(unittest::repeated_nested_message_extension, 0)
  253. .unknown_fields().field_count());
  254. // Discard them.
  255. ReflectionOps::DiscardUnknownFields(&message);
  256. TestUtil::ExpectAllExtensionsSet(message);
  257. EXPECT_EQ(0, message.unknown_fields().field_count());
  258. EXPECT_EQ(0,
  259. message.GetExtension(unittest::optional_nested_message_extension)
  260. .unknown_fields().field_count());
  261. EXPECT_EQ(0,
  262. message.GetExtension(unittest::repeated_nested_message_extension, 0)
  263. .unknown_fields().field_count());
  264. }
  265. TEST(ReflectionOpsTest, IsInitialized) {
  266. unittest::TestRequired message;
  267. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  268. message.set_a(1);
  269. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  270. message.set_b(2);
  271. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  272. message.set_c(3);
  273. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  274. }
  275. TEST(ReflectionOpsTest, ForeignIsInitialized) {
  276. unittest::TestRequiredForeign message;
  277. // Starts out initialized because the foreign message is itself an optional
  278. // field.
  279. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  280. // Once we create that field, the message is no longer initialized.
  281. message.mutable_optional_message();
  282. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  283. // Initialize it. Now we're initialized.
  284. message.mutable_optional_message()->set_a(1);
  285. message.mutable_optional_message()->set_b(2);
  286. message.mutable_optional_message()->set_c(3);
  287. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  288. // Add a repeated version of the message. No longer initialized.
  289. unittest::TestRequired* sub_message = message.add_repeated_message();
  290. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  291. // Initialize that repeated version.
  292. sub_message->set_a(1);
  293. sub_message->set_b(2);
  294. sub_message->set_c(3);
  295. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  296. }
  297. TEST(ReflectionOpsTest, ExtensionIsInitialized) {
  298. unittest::TestAllExtensions message;
  299. // Starts out initialized because the foreign message is itself an optional
  300. // field.
  301. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  302. // Once we create that field, the message is no longer initialized.
  303. message.MutableExtension(unittest::TestRequired::single);
  304. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  305. // Initialize it. Now we're initialized.
  306. message.MutableExtension(unittest::TestRequired::single)->set_a(1);
  307. message.MutableExtension(unittest::TestRequired::single)->set_b(2);
  308. message.MutableExtension(unittest::TestRequired::single)->set_c(3);
  309. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  310. // Add a repeated version of the message. No longer initialized.
  311. message.AddExtension(unittest::TestRequired::multi);
  312. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  313. // Initialize that repeated version.
  314. message.MutableExtension(unittest::TestRequired::multi, 0)->set_a(1);
  315. message.MutableExtension(unittest::TestRequired::multi, 0)->set_b(2);
  316. message.MutableExtension(unittest::TestRequired::multi, 0)->set_c(3);
  317. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  318. }
  319. TEST(ReflectionOpsTest, OneofIsInitialized) {
  320. unittest::TestRequiredOneof message;
  321. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  322. message.mutable_foo_message();
  323. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  324. message.set_foo_int(1);
  325. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  326. message.mutable_foo_message();
  327. EXPECT_FALSE(ReflectionOps::IsInitialized(message));
  328. message.mutable_foo_message()->set_required_double(0.1);
  329. EXPECT_TRUE(ReflectionOps::IsInitialized(message));
  330. }
  331. static string FindInitializationErrors(const Message& message) {
  332. std::vector<string> errors;
  333. ReflectionOps::FindInitializationErrors(message, "", &errors);
  334. return Join(errors, ",");
  335. }
  336. TEST(ReflectionOpsTest, FindInitializationErrors) {
  337. unittest::TestRequired message;
  338. EXPECT_EQ("a,b,c", FindInitializationErrors(message));
  339. }
  340. TEST(ReflectionOpsTest, FindForeignInitializationErrors) {
  341. unittest::TestRequiredForeign message;
  342. message.mutable_optional_message();
  343. message.add_repeated_message();
  344. message.add_repeated_message();
  345. EXPECT_EQ("optional_message.a,"
  346. "optional_message.b,"
  347. "optional_message.c,"
  348. "repeated_message[0].a,"
  349. "repeated_message[0].b,"
  350. "repeated_message[0].c,"
  351. "repeated_message[1].a,"
  352. "repeated_message[1].b,"
  353. "repeated_message[1].c",
  354. FindInitializationErrors(message));
  355. }
  356. TEST(ReflectionOpsTest, FindExtensionInitializationErrors) {
  357. unittest::TestAllExtensions message;
  358. message.MutableExtension(unittest::TestRequired::single);
  359. message.AddExtension(unittest::TestRequired::multi);
  360. message.AddExtension(unittest::TestRequired::multi);
  361. EXPECT_EQ("(protobuf_unittest.TestRequired.single).a,"
  362. "(protobuf_unittest.TestRequired.single).b,"
  363. "(protobuf_unittest.TestRequired.single).c,"
  364. "(protobuf_unittest.TestRequired.multi)[0].a,"
  365. "(protobuf_unittest.TestRequired.multi)[0].b,"
  366. "(protobuf_unittest.TestRequired.multi)[0].c,"
  367. "(protobuf_unittest.TestRequired.multi)[1].a,"
  368. "(protobuf_unittest.TestRequired.multi)[1].b,"
  369. "(protobuf_unittest.TestRequired.multi)[1].c",
  370. FindInitializationErrors(message));
  371. }
  372. TEST(ReflectionOpsTest, FindOneofInitializationErrors) {
  373. unittest::TestRequiredOneof message;
  374. message.mutable_foo_message();
  375. EXPECT_EQ("foo_message.required_double",
  376. FindInitializationErrors(message));
  377. }
  378. } // namespace
  379. } // namespace internal
  380. } // namespace protobuf
  381. } // namespace google