extension_set_unittest.cc 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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/stubs/casts.h>
  34. #include <google/protobuf/stubs/strutil.h>
  35. #include <google/protobuf/test_util.h>
  36. #include <google/protobuf/unittest.pb.h>
  37. #include <google/protobuf/unittest_mset.pb.h>
  38. #include <google/protobuf/io/coded_stream.h>
  39. #include <google/protobuf/io/zero_copy_stream_impl.h>
  40. #include <google/protobuf/descriptor.pb.h>
  41. #include <google/protobuf/arena.h>
  42. #include <google/protobuf/descriptor.h>
  43. #include <google/protobuf/dynamic_message.h>
  44. #include <google/protobuf/extension_set.h>
  45. #include <google/protobuf/wire_format.h>
  46. #include <google/protobuf/stubs/logging.h>
  47. #include <google/protobuf/stubs/common.h>
  48. #include <google/protobuf/testing/googletest.h>
  49. #include <gtest/gtest.h>
  50. #include <google/protobuf/stubs/stl_util.h>
  51. namespace google {
  52. namespace protobuf {
  53. namespace internal {
  54. namespace {
  55. // This test closely mirrors google/protobuf/compiler/cpp/unittest.cc
  56. // except that it uses extensions rather than regular fields.
  57. TEST(ExtensionSetTest, Defaults) {
  58. // Check that all default values are set correctly in the initial message.
  59. unittest::TestAllExtensions message;
  60. TestUtil::ExpectExtensionsClear(message);
  61. // Messages should return pointers to default instances until first use.
  62. // (This is not checked by ExpectClear() since it is not actually true after
  63. // the fields have been set and then cleared.)
  64. EXPECT_EQ(&unittest::OptionalGroup_extension::default_instance(),
  65. &message.GetExtension(unittest::optionalgroup_extension));
  66. EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
  67. &message.GetExtension(unittest::optional_nested_message_extension));
  68. EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
  69. &message.GetExtension(
  70. unittest::optional_foreign_message_extension));
  71. EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
  72. &message.GetExtension(unittest::optional_import_message_extension));
  73. }
  74. TEST(ExtensionSetTest, Accessors) {
  75. // Set every field to a unique value then go back and check all those
  76. // values.
  77. unittest::TestAllExtensions message;
  78. TestUtil::SetAllExtensions(&message);
  79. TestUtil::ExpectAllExtensionsSet(message);
  80. TestUtil::ModifyRepeatedExtensions(&message);
  81. TestUtil::ExpectRepeatedExtensionsModified(message);
  82. }
  83. TEST(ExtensionSetTest, Clear) {
  84. // Set every field to a unique value, clear the message, then check that
  85. // it is cleared.
  86. unittest::TestAllExtensions message;
  87. TestUtil::SetAllExtensions(&message);
  88. message.Clear();
  89. TestUtil::ExpectExtensionsClear(message);
  90. // Unlike with the defaults test, we do NOT expect that requesting embedded
  91. // messages will return a pointer to the default instance. Instead, they
  92. // should return the objects that were created when mutable_blah() was
  93. // called.
  94. EXPECT_NE(&unittest::OptionalGroup_extension::default_instance(),
  95. &message.GetExtension(unittest::optionalgroup_extension));
  96. EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
  97. &message.GetExtension(unittest::optional_nested_message_extension));
  98. EXPECT_NE(&unittest::ForeignMessage::default_instance(),
  99. &message.GetExtension(
  100. unittest::optional_foreign_message_extension));
  101. EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
  102. &message.GetExtension(unittest::optional_import_message_extension));
  103. // Make sure setting stuff again after clearing works. (This takes slightly
  104. // different code paths since the objects are reused.)
  105. TestUtil::SetAllExtensions(&message);
  106. TestUtil::ExpectAllExtensionsSet(message);
  107. }
  108. TEST(ExtensionSetTest, ClearOneField) {
  109. // Set every field to a unique value, then clear one value and insure that
  110. // only that one value is cleared.
  111. unittest::TestAllExtensions message;
  112. TestUtil::SetAllExtensions(&message);
  113. int64 original_value =
  114. message.GetExtension(unittest::optional_int64_extension);
  115. // Clear the field and make sure it shows up as cleared.
  116. message.ClearExtension(unittest::optional_int64_extension);
  117. EXPECT_FALSE(message.HasExtension(unittest::optional_int64_extension));
  118. EXPECT_EQ(0, message.GetExtension(unittest::optional_int64_extension));
  119. // Other adjacent fields should not be cleared.
  120. EXPECT_TRUE(message.HasExtension(unittest::optional_int32_extension));
  121. EXPECT_TRUE(message.HasExtension(unittest::optional_uint32_extension));
  122. // Make sure if we set it again, then all fields are set.
  123. message.SetExtension(unittest::optional_int64_extension, original_value);
  124. TestUtil::ExpectAllExtensionsSet(message);
  125. }
  126. TEST(ExtensionSetTest, SetAllocatedExtension) {
  127. unittest::TestAllExtensions message;
  128. EXPECT_FALSE(message.HasExtension(
  129. unittest::optional_foreign_message_extension));
  130. // Add a extension using SetAllocatedExtension
  131. unittest::ForeignMessage* foreign_message = new unittest::ForeignMessage();
  132. message.SetAllocatedExtension(unittest::optional_foreign_message_extension,
  133. foreign_message);
  134. EXPECT_TRUE(message.HasExtension(
  135. unittest::optional_foreign_message_extension));
  136. EXPECT_EQ(foreign_message,
  137. message.MutableExtension(
  138. unittest::optional_foreign_message_extension));
  139. EXPECT_EQ(foreign_message,
  140. &message.GetExtension(
  141. unittest::optional_foreign_message_extension));
  142. // SetAllocatedExtension should delete the previously existing extension.
  143. // (We reply on unittest to check memory leaks for this case)
  144. message.SetAllocatedExtension(unittest::optional_foreign_message_extension,
  145. new unittest::ForeignMessage());
  146. // SetAllocatedExtension with a NULL parameter is equivalent to ClearExtenion.
  147. message.SetAllocatedExtension(unittest::optional_foreign_message_extension,
  148. NULL);
  149. EXPECT_FALSE(message.HasExtension(
  150. unittest::optional_foreign_message_extension));
  151. }
  152. TEST(ExtensionSetTest, ReleaseExtension) {
  153. proto2_wireformat_unittest::TestMessageSet message;
  154. EXPECT_FALSE(message.HasExtension(
  155. unittest::TestMessageSetExtension1::message_set_extension));
  156. // Add a extension using SetAllocatedExtension
  157. unittest::TestMessageSetExtension1* extension =
  158. new unittest::TestMessageSetExtension1();
  159. message.SetAllocatedExtension(
  160. unittest::TestMessageSetExtension1::message_set_extension,
  161. extension);
  162. EXPECT_TRUE(message.HasExtension(
  163. unittest::TestMessageSetExtension1::message_set_extension));
  164. // Release the extension using ReleaseExtension
  165. unittest::TestMessageSetExtension1* released_extension =
  166. message.ReleaseExtension(
  167. unittest::TestMessageSetExtension1::message_set_extension);
  168. EXPECT_EQ(extension, released_extension);
  169. EXPECT_FALSE(message.HasExtension(
  170. unittest::TestMessageSetExtension1::message_set_extension));
  171. // ReleaseExtension will return the underlying object even after
  172. // ClearExtension is called.
  173. message.SetAllocatedExtension(
  174. unittest::TestMessageSetExtension1::message_set_extension,
  175. extension);
  176. message.ClearExtension(
  177. unittest::TestMessageSetExtension1::message_set_extension);
  178. released_extension = message.ReleaseExtension(
  179. unittest::TestMessageSetExtension1::message_set_extension);
  180. EXPECT_TRUE(released_extension != NULL);
  181. delete released_extension;
  182. }
  183. TEST(ExtensionSetTest, ArenaUnsafeArenaSetAllocatedAndRelease) {
  184. ::google::protobuf::Arena arena;
  185. unittest::TestAllExtensions* message =
  186. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
  187. unittest::ForeignMessage extension;
  188. message->UnsafeArenaSetAllocatedExtension(
  189. unittest::optional_foreign_message_extension,
  190. &extension);
  191. // No copy when set.
  192. unittest::ForeignMessage* mutable_extension =
  193. message->MutableExtension(unittest::optional_foreign_message_extension);
  194. EXPECT_EQ(&extension, mutable_extension);
  195. // No copy when unsafe released.
  196. unittest::ForeignMessage* released_extension =
  197. message->UnsafeArenaReleaseExtension(
  198. unittest::optional_foreign_message_extension);
  199. EXPECT_EQ(&extension, released_extension);
  200. EXPECT_FALSE(message->HasExtension(
  201. unittest::optional_foreign_message_extension));
  202. // Set the ownership back and let the destructors run. It should not take
  203. // ownership, so this should not crash.
  204. message->UnsafeArenaSetAllocatedExtension(
  205. unittest::optional_foreign_message_extension,
  206. &extension);
  207. }
  208. TEST(ExtensionSetTest, UnsafeArenaSetAllocatedAndRelease) {
  209. unittest::TestAllExtensions message;
  210. unittest::ForeignMessage* extension = new unittest::ForeignMessage();
  211. message.UnsafeArenaSetAllocatedExtension(
  212. unittest::optional_foreign_message_extension,
  213. extension);
  214. // No copy when set.
  215. unittest::ForeignMessage* mutable_extension =
  216. message.MutableExtension(unittest::optional_foreign_message_extension);
  217. EXPECT_EQ(extension, mutable_extension);
  218. // No copy when unsafe released.
  219. unittest::ForeignMessage* released_extension =
  220. message.UnsafeArenaReleaseExtension(
  221. unittest::optional_foreign_message_extension);
  222. EXPECT_EQ(extension, released_extension);
  223. EXPECT_FALSE(message.HasExtension(
  224. unittest::optional_foreign_message_extension));
  225. // Set the ownership back and let the destructors run. It should take
  226. // ownership, so this should not leak.
  227. message.UnsafeArenaSetAllocatedExtension(
  228. unittest::optional_foreign_message_extension,
  229. extension);
  230. }
  231. TEST(ExtensionSetTest, ArenaUnsafeArenaReleaseOfHeapAlloc) {
  232. ::google::protobuf::Arena arena;
  233. unittest::TestAllExtensions* message =
  234. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
  235. unittest::ForeignMessage* extension = new unittest::ForeignMessage;
  236. message->SetAllocatedExtension(
  237. unittest::optional_foreign_message_extension,
  238. extension);
  239. // The arena should maintain ownership of the heap allocated proto because we
  240. // used UnsafeArenaReleaseExtension. The leak checker will ensure this.
  241. unittest::ForeignMessage* released_extension =
  242. message->UnsafeArenaReleaseExtension(
  243. unittest::optional_foreign_message_extension);
  244. EXPECT_EQ(extension, released_extension);
  245. EXPECT_FALSE(message->HasExtension(
  246. unittest::optional_foreign_message_extension));
  247. }
  248. TEST(ExtensionSetTest, CopyFrom) {
  249. unittest::TestAllExtensions message1, message2;
  250. TestUtil::SetAllExtensions(&message1);
  251. message2.CopyFrom(message1);
  252. TestUtil::ExpectAllExtensionsSet(message2);
  253. message2.CopyFrom(message1); // exercise copy when fields already exist
  254. TestUtil::ExpectAllExtensionsSet(message2);
  255. }
  256. TEST(ExtensionSetTest, CopyFromPacked) {
  257. unittest::TestPackedExtensions message1, message2;
  258. TestUtil::SetPackedExtensions(&message1);
  259. message2.CopyFrom(message1);
  260. TestUtil::ExpectPackedExtensionsSet(message2);
  261. message2.CopyFrom(message1); // exercise copy when fields already exist
  262. TestUtil::ExpectPackedExtensionsSet(message2);
  263. }
  264. TEST(ExtensionSetTest, CopyFromUpcasted) {
  265. unittest::TestAllExtensions message1, message2;
  266. const Message& upcasted_message = message1;
  267. TestUtil::SetAllExtensions(&message1);
  268. message2.CopyFrom(upcasted_message);
  269. TestUtil::ExpectAllExtensionsSet(message2);
  270. // exercise copy when fields already exist
  271. message2.CopyFrom(upcasted_message);
  272. TestUtil::ExpectAllExtensionsSet(message2);
  273. }
  274. TEST(ExtensionSetTest, SwapWithEmpty) {
  275. unittest::TestAllExtensions message1, message2;
  276. TestUtil::SetAllExtensions(&message1);
  277. TestUtil::ExpectAllExtensionsSet(message1);
  278. TestUtil::ExpectExtensionsClear(message2);
  279. message1.Swap(&message2);
  280. TestUtil::ExpectAllExtensionsSet(message2);
  281. TestUtil::ExpectExtensionsClear(message1);
  282. }
  283. TEST(ExtensionSetTest, SwapWithSelf) {
  284. unittest::TestAllExtensions message;
  285. TestUtil::SetAllExtensions(&message);
  286. TestUtil::ExpectAllExtensionsSet(message);
  287. message.Swap(&message);
  288. TestUtil::ExpectAllExtensionsSet(message);
  289. }
  290. TEST(ExtensionSetTest, SwapExtension) {
  291. unittest::TestAllExtensions message1;
  292. unittest::TestAllExtensions message2;
  293. TestUtil::SetAllExtensions(&message1);
  294. std::vector<const FieldDescriptor*> fields;
  295. // Swap empty fields.
  296. const Reflection* reflection = message1.GetReflection();
  297. reflection->SwapFields(&message1, &message2, fields);
  298. TestUtil::ExpectAllExtensionsSet(message1);
  299. TestUtil::ExpectExtensionsClear(message2);
  300. // Swap two extensions.
  301. fields.push_back(
  302. reflection->FindKnownExtensionByNumber(12));
  303. fields.push_back(
  304. reflection->FindKnownExtensionByNumber(25));
  305. reflection->SwapFields(&message1, &message2, fields);
  306. EXPECT_TRUE(message1.HasExtension(unittest::optional_int32_extension));
  307. EXPECT_FALSE(message1.HasExtension(unittest::optional_double_extension));
  308. EXPECT_FALSE(message1.HasExtension(unittest::optional_cord_extension));
  309. EXPECT_FALSE(message2.HasExtension(unittest::optional_int32_extension));
  310. EXPECT_TRUE(message2.HasExtension(unittest::optional_double_extension));
  311. EXPECT_TRUE(message2.HasExtension(unittest::optional_cord_extension));
  312. }
  313. TEST(ExtensionSetTest, SwapExtensionWithEmpty) {
  314. unittest::TestAllExtensions message1;
  315. unittest::TestAllExtensions message2;
  316. unittest::TestAllExtensions message3;
  317. TestUtil::SetAllExtensions(&message3);
  318. const Reflection* reflection = message3.GetReflection();
  319. std::vector<const FieldDescriptor*> fields;
  320. reflection->ListFields(message3, &fields);
  321. reflection->SwapFields(&message1, &message2, fields);
  322. TestUtil::ExpectExtensionsClear(message1);
  323. TestUtil::ExpectExtensionsClear(message2);
  324. }
  325. TEST(ExtensionSetTest, SwapExtensionBothFull) {
  326. unittest::TestAllExtensions message1;
  327. unittest::TestAllExtensions message2;
  328. TestUtil::SetAllExtensions(&message1);
  329. TestUtil::SetAllExtensions(&message2);
  330. const Reflection* reflection = message1.GetReflection();
  331. std::vector<const FieldDescriptor*> fields;
  332. reflection->ListFields(message1, &fields);
  333. reflection->SwapFields(&message1, &message2, fields);
  334. TestUtil::ExpectAllExtensionsSet(message1);
  335. TestUtil::ExpectAllExtensionsSet(message2);
  336. }
  337. TEST(ExtensionSetTest, ArenaSetAllExtension) {
  338. ::google::protobuf::Arena arena1;
  339. unittest::TestAllExtensions* message1 =
  340. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
  341. TestUtil::SetAllExtensions(message1);
  342. TestUtil::ExpectAllExtensionsSet(*message1);
  343. }
  344. TEST(ExtensionSetTest, ArenaCopyConstructor) {
  345. ::google::protobuf::Arena arena1;
  346. unittest::TestAllExtensions* message1 =
  347. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
  348. TestUtil::SetAllExtensions(message1);
  349. unittest::TestAllExtensions message2(*message1);
  350. arena1.Reset();
  351. TestUtil::ExpectAllExtensionsSet(message2);
  352. }
  353. TEST(ExtensionSetTest, ArenaMergeFrom) {
  354. ::google::protobuf::Arena arena1;
  355. unittest::TestAllExtensions* message1 =
  356. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
  357. TestUtil::SetAllExtensions(message1);
  358. unittest::TestAllExtensions message2;
  359. message2.MergeFrom(*message1);
  360. arena1.Reset();
  361. TestUtil::ExpectAllExtensionsSet(message2);
  362. }
  363. TEST(ExtensionSetTest, ArenaSetAllocatedMessageAndRelease) {
  364. ::google::protobuf::Arena arena;
  365. unittest::TestAllExtensions* message =
  366. ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
  367. EXPECT_FALSE(message->HasExtension(
  368. unittest::optional_foreign_message_extension));
  369. // Add a extension using SetAllocatedExtension
  370. unittest::ForeignMessage* foreign_message = new unittest::ForeignMessage();
  371. message->SetAllocatedExtension(unittest::optional_foreign_message_extension,
  372. foreign_message);
  373. // foreign_message is now owned by the arena.
  374. EXPECT_EQ(foreign_message,
  375. message->MutableExtension(
  376. unittest::optional_foreign_message_extension));
  377. // Underlying message is copied, and returned.
  378. unittest::ForeignMessage* released_message = message->ReleaseExtension(
  379. unittest::optional_foreign_message_extension);
  380. delete released_message;
  381. EXPECT_FALSE(message->HasExtension(
  382. unittest::optional_foreign_message_extension));
  383. }
  384. TEST(ExtensionSetTest, SwapExtensionBothFullWithArena) {
  385. ::google::protobuf::Arena arena1;
  386. std::unique_ptr<google::protobuf::Arena> arena2(new ::google::protobuf::Arena());
  387. unittest::TestAllExtensions* message1 =
  388. Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
  389. unittest::TestAllExtensions* message2 =
  390. Arena::CreateMessage<unittest::TestAllExtensions>(arena2.get());
  391. TestUtil::SetAllExtensions(message1);
  392. TestUtil::SetAllExtensions(message2);
  393. message1->SetExtension(unittest::optional_int32_extension, 1);
  394. message2->SetExtension(unittest::optional_int32_extension, 2);
  395. message1->Swap(message2);
  396. EXPECT_EQ(2, message1->GetExtension(unittest::optional_int32_extension));
  397. EXPECT_EQ(1, message2->GetExtension(unittest::optional_int32_extension));
  398. // Re-set the original values so ExpectAllExtensionsSet is happy.
  399. message1->SetExtension(unittest::optional_int32_extension, 101);
  400. message2->SetExtension(unittest::optional_int32_extension, 101);
  401. TestUtil::ExpectAllExtensionsSet(*message1);
  402. TestUtil::ExpectAllExtensionsSet(*message2);
  403. arena2.reset(NULL);
  404. TestUtil::ExpectAllExtensionsSet(*message1);
  405. // Test corner cases, when one is empty and other is not.
  406. ::google::protobuf::Arena arena3, arena4;
  407. unittest::TestAllExtensions* message3 =
  408. Arena::CreateMessage<unittest::TestAllExtensions>(&arena3);
  409. unittest::TestAllExtensions* message4 =
  410. Arena::CreateMessage<unittest::TestAllExtensions>(&arena4);
  411. TestUtil::SetAllExtensions(message3);
  412. message3->Swap(message4);
  413. arena3.Reset();
  414. TestUtil::ExpectAllExtensionsSet(*message4);
  415. }
  416. TEST(ExtensionSetTest, SwapFieldsOfExtensionBothFullWithArena) {
  417. google::protobuf::Arena arena1;
  418. google::protobuf::Arena* arena2 = new ::google::protobuf::Arena();
  419. unittest::TestAllExtensions* message1 =
  420. Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
  421. unittest::TestAllExtensions* message2 =
  422. Arena::CreateMessage<unittest::TestAllExtensions>(arena2);
  423. TestUtil::SetAllExtensions(message1);
  424. TestUtil::SetAllExtensions(message2);
  425. const Reflection* reflection = message1->GetReflection();
  426. std::vector<const FieldDescriptor*> fields;
  427. reflection->ListFields(*message1, &fields);
  428. reflection->SwapFields(message1, message2, fields);
  429. TestUtil::ExpectAllExtensionsSet(*message1);
  430. TestUtil::ExpectAllExtensionsSet(*message2);
  431. delete arena2;
  432. TestUtil::ExpectAllExtensionsSet(*message1);
  433. }
  434. TEST(ExtensionSetTest, SwapExtensionWithSelf) {
  435. unittest::TestAllExtensions message1;
  436. TestUtil::SetAllExtensions(&message1);
  437. std::vector<const FieldDescriptor*> fields;
  438. const Reflection* reflection = message1.GetReflection();
  439. reflection->ListFields(message1, &fields);
  440. reflection->SwapFields(&message1, &message1, fields);
  441. TestUtil::ExpectAllExtensionsSet(message1);
  442. }
  443. TEST(ExtensionSetTest, SerializationToArray) {
  444. // Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
  445. // compatibility of extensions.
  446. //
  447. // This checks serialization to a flat array by explicitly reserving space in
  448. // the string and calling the generated message's
  449. // SerializeWithCachedSizesToArray.
  450. unittest::TestAllExtensions source;
  451. unittest::TestAllTypes destination;
  452. TestUtil::SetAllExtensions(&source);
  453. int size = source.ByteSize();
  454. string data;
  455. data.resize(size);
  456. uint8* target = reinterpret_cast<uint8*>(string_as_array(&data));
  457. uint8* end = source.SerializeWithCachedSizesToArray(target);
  458. EXPECT_EQ(size, end - target);
  459. EXPECT_TRUE(destination.ParseFromString(data));
  460. TestUtil::ExpectAllFieldsSet(destination);
  461. }
  462. TEST(ExtensionSetTest, SerializationToStream) {
  463. // Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
  464. // compatibility of extensions.
  465. //
  466. // This checks serialization to an output stream by creating an array output
  467. // stream that can only buffer 1 byte at a time - this prevents the message
  468. // from ever jumping to the fast path, ensuring that serialization happens via
  469. // the CodedOutputStream.
  470. unittest::TestAllExtensions source;
  471. unittest::TestAllTypes destination;
  472. TestUtil::SetAllExtensions(&source);
  473. int size = source.ByteSize();
  474. string data;
  475. data.resize(size);
  476. {
  477. io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
  478. io::CodedOutputStream output_stream(&array_stream);
  479. source.SerializeWithCachedSizes(&output_stream);
  480. ASSERT_FALSE(output_stream.HadError());
  481. }
  482. EXPECT_TRUE(destination.ParseFromString(data));
  483. TestUtil::ExpectAllFieldsSet(destination);
  484. }
  485. TEST(ExtensionSetTest, PackedSerializationToArray) {
  486. // Serialize as TestPackedExtensions and parse as TestPackedTypes to insure
  487. // wire compatibility of extensions.
  488. //
  489. // This checks serialization to a flat array by explicitly reserving space in
  490. // the string and calling the generated message's
  491. // SerializeWithCachedSizesToArray.
  492. unittest::TestPackedExtensions source;
  493. unittest::TestPackedTypes destination;
  494. TestUtil::SetPackedExtensions(&source);
  495. int size = source.ByteSize();
  496. string data;
  497. data.resize(size);
  498. uint8* target = reinterpret_cast<uint8*>(string_as_array(&data));
  499. uint8* end = source.SerializeWithCachedSizesToArray(target);
  500. EXPECT_EQ(size, end - target);
  501. EXPECT_TRUE(destination.ParseFromString(data));
  502. TestUtil::ExpectPackedFieldsSet(destination);
  503. }
  504. TEST(ExtensionSetTest, PackedSerializationToStream) {
  505. // Serialize as TestPackedExtensions and parse as TestPackedTypes to insure
  506. // wire compatibility of extensions.
  507. //
  508. // This checks serialization to an output stream by creating an array output
  509. // stream that can only buffer 1 byte at a time - this prevents the message
  510. // from ever jumping to the fast path, ensuring that serialization happens via
  511. // the CodedOutputStream.
  512. unittest::TestPackedExtensions source;
  513. unittest::TestPackedTypes destination;
  514. TestUtil::SetPackedExtensions(&source);
  515. int size = source.ByteSize();
  516. string data;
  517. data.resize(size);
  518. {
  519. io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
  520. io::CodedOutputStream output_stream(&array_stream);
  521. source.SerializeWithCachedSizes(&output_stream);
  522. ASSERT_FALSE(output_stream.HadError());
  523. }
  524. EXPECT_TRUE(destination.ParseFromString(data));
  525. TestUtil::ExpectPackedFieldsSet(destination);
  526. }
  527. TEST(ExtensionSetTest, NestedExtensionGroup) {
  528. // Serialize as TestGroup and parse as TestGroupExtension.
  529. unittest::TestGroup source;
  530. unittest::TestGroupExtension destination;
  531. string data;
  532. source.mutable_optionalgroup()->set_a(117);
  533. source.set_optional_foreign_enum(unittest::FOREIGN_BAZ);
  534. source.SerializeToString(&data);
  535. EXPECT_TRUE(destination.ParseFromString(data));
  536. EXPECT_TRUE(destination.GetExtension(
  537. unittest::TestNestedExtension::optionalgroup_extension).has_a());
  538. EXPECT_EQ(117, destination.GetExtension(
  539. unittest::TestNestedExtension::optionalgroup_extension).a());
  540. EXPECT_TRUE(destination.HasExtension(
  541. unittest::TestNestedExtension::optional_foreign_enum_extension));
  542. EXPECT_EQ(unittest::FOREIGN_BAZ, destination.GetExtension(
  543. unittest::TestNestedExtension::optional_foreign_enum_extension));
  544. }
  545. TEST(ExtensionSetTest, Parsing) {
  546. // Serialize as TestAllTypes and parse as TestAllExtensions.
  547. unittest::TestAllTypes source;
  548. unittest::TestAllExtensions destination;
  549. string data;
  550. TestUtil::SetAllFields(&source);
  551. source.SerializeToString(&data);
  552. EXPECT_TRUE(destination.ParseFromString(data));
  553. TestUtil::SetOneofFields(&destination);
  554. TestUtil::ExpectAllExtensionsSet(destination);
  555. }
  556. TEST(ExtensionSetTest, PackedParsing) {
  557. // Serialize as TestPackedTypes and parse as TestPackedExtensions.
  558. unittest::TestPackedTypes source;
  559. unittest::TestPackedExtensions destination;
  560. string data;
  561. TestUtil::SetPackedFields(&source);
  562. source.SerializeToString(&data);
  563. EXPECT_TRUE(destination.ParseFromString(data));
  564. TestUtil::ExpectPackedExtensionsSet(destination);
  565. }
  566. TEST(ExtensionSetTest, PackedToUnpackedParsing) {
  567. unittest::TestPackedTypes source;
  568. unittest::TestUnpackedExtensions destination;
  569. string data;
  570. TestUtil::SetPackedFields(&source);
  571. source.SerializeToString(&data);
  572. EXPECT_TRUE(destination.ParseFromString(data));
  573. TestUtil::ExpectUnpackedExtensionsSet(destination);
  574. // Reserialize
  575. unittest::TestUnpackedTypes unpacked;
  576. TestUtil::SetUnpackedFields(&unpacked);
  577. EXPECT_TRUE(unpacked.SerializeAsString() == destination.SerializeAsString());
  578. // Make sure we can add extensions.
  579. destination.AddExtension(unittest::unpacked_int32_extension, 1);
  580. destination.AddExtension(unittest::unpacked_enum_extension,
  581. protobuf_unittest::FOREIGN_BAR);
  582. }
  583. TEST(ExtensionSetTest, UnpackedToPackedParsing) {
  584. unittest::TestUnpackedTypes source;
  585. unittest::TestPackedExtensions destination;
  586. string data;
  587. TestUtil::SetUnpackedFields(&source);
  588. source.SerializeToString(&data);
  589. EXPECT_TRUE(destination.ParseFromString(data));
  590. TestUtil::ExpectPackedExtensionsSet(destination);
  591. // Reserialize
  592. unittest::TestPackedTypes packed;
  593. TestUtil::SetPackedFields(&packed);
  594. EXPECT_TRUE(packed.SerializeAsString() == destination.SerializeAsString());
  595. // Make sure we can add extensions.
  596. destination.AddExtension(unittest::packed_int32_extension, 1);
  597. destination.AddExtension(unittest::packed_enum_extension,
  598. protobuf_unittest::FOREIGN_BAR);
  599. }
  600. TEST(ExtensionSetTest, IsInitialized) {
  601. // Test that IsInitialized() returns false if required fields in nested
  602. // extensions are missing.
  603. unittest::TestAllExtensions message;
  604. EXPECT_TRUE(message.IsInitialized());
  605. message.MutableExtension(unittest::TestRequired::single);
  606. EXPECT_FALSE(message.IsInitialized());
  607. message.MutableExtension(unittest::TestRequired::single)->set_a(1);
  608. EXPECT_FALSE(message.IsInitialized());
  609. message.MutableExtension(unittest::TestRequired::single)->set_b(2);
  610. EXPECT_FALSE(message.IsInitialized());
  611. message.MutableExtension(unittest::TestRequired::single)->set_c(3);
  612. EXPECT_TRUE(message.IsInitialized());
  613. message.AddExtension(unittest::TestRequired::multi);
  614. EXPECT_FALSE(message.IsInitialized());
  615. message.MutableExtension(unittest::TestRequired::multi, 0)->set_a(1);
  616. EXPECT_FALSE(message.IsInitialized());
  617. message.MutableExtension(unittest::TestRequired::multi, 0)->set_b(2);
  618. EXPECT_FALSE(message.IsInitialized());
  619. message.MutableExtension(unittest::TestRequired::multi, 0)->set_c(3);
  620. EXPECT_TRUE(message.IsInitialized());
  621. }
  622. TEST(ExtensionSetTest, MutableString) {
  623. // Test the mutable string accessors.
  624. unittest::TestAllExtensions message;
  625. message.MutableExtension(unittest::optional_string_extension)->assign("foo");
  626. EXPECT_TRUE(message.HasExtension(unittest::optional_string_extension));
  627. EXPECT_EQ("foo", message.GetExtension(unittest::optional_string_extension));
  628. message.AddExtension(unittest::repeated_string_extension)->assign("bar");
  629. ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_string_extension));
  630. EXPECT_EQ("bar",
  631. message.GetExtension(unittest::repeated_string_extension, 0));
  632. }
  633. TEST(ExtensionSetTest, SpaceUsedExcludingSelf) {
  634. // Scalar primitive extensions should increase the extension set size by a
  635. // minimum of the size of the primitive type.
  636. #define TEST_SCALAR_EXTENSIONS_SPACE_USED(type, value) \
  637. do { \
  638. unittest::TestAllExtensions message; \
  639. const int base_size = message.SpaceUsed(); \
  640. message.SetExtension(unittest::optional_##type##_extension, value); \
  641. int min_expected_size = base_size + \
  642. sizeof(message.GetExtension(unittest::optional_##type##_extension)); \
  643. EXPECT_LE(min_expected_size, message.SpaceUsed()); \
  644. } while (0)
  645. TEST_SCALAR_EXTENSIONS_SPACE_USED(int32 , 101);
  646. TEST_SCALAR_EXTENSIONS_SPACE_USED(int64 , 102);
  647. TEST_SCALAR_EXTENSIONS_SPACE_USED(uint32 , 103);
  648. TEST_SCALAR_EXTENSIONS_SPACE_USED(uint64 , 104);
  649. TEST_SCALAR_EXTENSIONS_SPACE_USED(sint32 , 105);
  650. TEST_SCALAR_EXTENSIONS_SPACE_USED(sint64 , 106);
  651. TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed32 , 107);
  652. TEST_SCALAR_EXTENSIONS_SPACE_USED(fixed64 , 108);
  653. TEST_SCALAR_EXTENSIONS_SPACE_USED(sfixed32, 109);
  654. TEST_SCALAR_EXTENSIONS_SPACE_USED(sfixed64, 110);
  655. TEST_SCALAR_EXTENSIONS_SPACE_USED(float , 111);
  656. TEST_SCALAR_EXTENSIONS_SPACE_USED(double , 112);
  657. TEST_SCALAR_EXTENSIONS_SPACE_USED(bool , true);
  658. #undef TEST_SCALAR_EXTENSIONS_SPACE_USED
  659. {
  660. unittest::TestAllExtensions message;
  661. const int base_size = message.SpaceUsed();
  662. message.SetExtension(unittest::optional_nested_enum_extension,
  663. unittest::TestAllTypes::FOO);
  664. int min_expected_size = base_size +
  665. sizeof(message.GetExtension(unittest::optional_nested_enum_extension));
  666. EXPECT_LE(min_expected_size, message.SpaceUsed());
  667. }
  668. {
  669. // Strings may cause extra allocations depending on their length; ensure
  670. // that gets included as well.
  671. unittest::TestAllExtensions message;
  672. const int base_size = message.SpaceUsed();
  673. const string s("this is a fairly large string that will cause some "
  674. "allocation in order to store it in the extension");
  675. message.SetExtension(unittest::optional_string_extension, s);
  676. int min_expected_size = base_size + s.length();
  677. EXPECT_LE(min_expected_size, message.SpaceUsed());
  678. }
  679. {
  680. // Messages also have additional allocation that need to be counted.
  681. unittest::TestAllExtensions message;
  682. const int base_size = message.SpaceUsed();
  683. unittest::ForeignMessage foreign;
  684. foreign.set_c(42);
  685. message.MutableExtension(unittest::optional_foreign_message_extension)->
  686. CopyFrom(foreign);
  687. int min_expected_size = base_size + foreign.SpaceUsed();
  688. EXPECT_LE(min_expected_size, message.SpaceUsed());
  689. }
  690. // Repeated primitive extensions will increase space used by at least a
  691. // RepeatedField<T>, and will cause additional allocations when the array
  692. // gets too big for the initial space.
  693. // This macro:
  694. // - Adds a value to the repeated extension, then clears it, establishing
  695. // the base size.
  696. // - Adds a small number of values, testing that it doesn't increase the
  697. // SpaceUsed()
  698. // - Adds a large number of values (requiring allocation in the repeated
  699. // field), and ensures that that allocation is included in SpaceUsed()
  700. #define TEST_REPEATED_EXTENSIONS_SPACE_USED(type, cpptype, value) \
  701. do { \
  702. unittest::TestAllExtensions message; \
  703. const int base_size = message.SpaceUsed(); \
  704. int min_expected_size = sizeof(RepeatedField<cpptype>) + base_size; \
  705. message.AddExtension(unittest::repeated_##type##_extension, value); \
  706. message.ClearExtension(unittest::repeated_##type##_extension); \
  707. const int empty_repeated_field_size = message.SpaceUsed(); \
  708. EXPECT_LE(min_expected_size, empty_repeated_field_size) << #type; \
  709. message.AddExtension(unittest::repeated_##type##_extension, value); \
  710. message.AddExtension(unittest::repeated_##type##_extension, value); \
  711. EXPECT_EQ(empty_repeated_field_size, message.SpaceUsed()) << #type; \
  712. message.ClearExtension(unittest::repeated_##type##_extension); \
  713. const int old_capacity = \
  714. message.GetRepeatedExtension(unittest::repeated_##type##_extension) \
  715. .Capacity(); \
  716. EXPECT_GE(old_capacity, kMinRepeatedFieldAllocationSize); \
  717. for (int i = 0; i < 16; ++i) { \
  718. message.AddExtension(unittest::repeated_##type##_extension, value); \
  719. } \
  720. int expected_size = sizeof(cpptype) * \
  721. (message.GetRepeatedExtension(unittest::repeated_##type##_extension) \
  722. .Capacity() - old_capacity) + empty_repeated_field_size; \
  723. EXPECT_LE(expected_size, message.SpaceUsed()) << #type; \
  724. } while (0)
  725. TEST_REPEATED_EXTENSIONS_SPACE_USED(int32 , int32 , 101);
  726. TEST_REPEATED_EXTENSIONS_SPACE_USED(int64 , int64 , 102);
  727. TEST_REPEATED_EXTENSIONS_SPACE_USED(uint32 , uint32, 103);
  728. TEST_REPEATED_EXTENSIONS_SPACE_USED(uint64 , uint64, 104);
  729. TEST_REPEATED_EXTENSIONS_SPACE_USED(sint32 , int32 , 105);
  730. TEST_REPEATED_EXTENSIONS_SPACE_USED(sint64 , int64 , 106);
  731. TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed32 , uint32, 107);
  732. TEST_REPEATED_EXTENSIONS_SPACE_USED(fixed64 , uint64, 108);
  733. TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed32, int32 , 109);
  734. TEST_REPEATED_EXTENSIONS_SPACE_USED(sfixed64, int64 , 110);
  735. TEST_REPEATED_EXTENSIONS_SPACE_USED(float , float , 111);
  736. TEST_REPEATED_EXTENSIONS_SPACE_USED(double , double, 112);
  737. TEST_REPEATED_EXTENSIONS_SPACE_USED(bool , bool , true);
  738. TEST_REPEATED_EXTENSIONS_SPACE_USED(nested_enum, int,
  739. unittest::TestAllTypes::FOO);
  740. #undef TEST_REPEATED_EXTENSIONS_SPACE_USED
  741. // Repeated strings
  742. {
  743. unittest::TestAllExtensions message;
  744. const int base_size = message.SpaceUsed();
  745. int min_expected_size = sizeof(RepeatedPtrField<string>) + base_size;
  746. const string value(256, 'x');
  747. // Once items are allocated, they may stick around even when cleared so
  748. // without the hardcore memory management accessors there isn't a notion of
  749. // the empty repeated field memory usage as there is with primitive types.
  750. for (int i = 0; i < 16; ++i) {
  751. message.AddExtension(unittest::repeated_string_extension, value);
  752. }
  753. min_expected_size += (sizeof(value) + value.size()) *
  754. (16 - kMinRepeatedFieldAllocationSize);
  755. EXPECT_LE(min_expected_size, message.SpaceUsed());
  756. }
  757. // Repeated messages
  758. {
  759. unittest::TestAllExtensions message;
  760. const int base_size = message.SpaceUsed();
  761. int min_expected_size = sizeof(RepeatedPtrField<unittest::ForeignMessage>) +
  762. base_size;
  763. unittest::ForeignMessage prototype;
  764. prototype.set_c(2);
  765. for (int i = 0; i < 16; ++i) {
  766. message.AddExtension(unittest::repeated_foreign_message_extension)->
  767. CopyFrom(prototype);
  768. }
  769. min_expected_size +=
  770. (16 - kMinRepeatedFieldAllocationSize) * prototype.SpaceUsed();
  771. EXPECT_LE(min_expected_size, message.SpaceUsed());
  772. }
  773. }
  774. // N.B.: We do not test range-based for here because we remain C++03 compatible.
  775. template<typename T, typename M, typename ID>
  776. inline T SumAllExtensions(const M& message, ID extension, T zero) {
  777. T sum = zero;
  778. typename RepeatedField<T>::const_iterator iter =
  779. message.GetRepeatedExtension(extension).begin();
  780. typename RepeatedField<T>::const_iterator end =
  781. message.GetRepeatedExtension(extension).end();
  782. for (; iter != end; ++iter) {
  783. sum += *iter;
  784. }
  785. return sum;
  786. }
  787. template<typename T, typename M, typename ID>
  788. inline void IncAllExtensions(M* message, ID extension,
  789. T val) {
  790. typename RepeatedField<T>::iterator iter =
  791. message->MutableRepeatedExtension(extension)->begin();
  792. typename RepeatedField<T>::iterator end =
  793. message->MutableRepeatedExtension(extension)->end();
  794. for (; iter != end; ++iter) {
  795. *iter += val;
  796. }
  797. }
  798. TEST(ExtensionSetTest, RepeatedFields) {
  799. unittest::TestAllExtensions message;
  800. // Test empty repeated-field case (b/12926163)
  801. ASSERT_EQ(0, message.GetRepeatedExtension(
  802. unittest::repeated_int32_extension).size());
  803. ASSERT_EQ(0, message.GetRepeatedExtension(
  804. unittest::repeated_nested_enum_extension).size());
  805. ASSERT_EQ(0, message.GetRepeatedExtension(
  806. unittest::repeated_string_extension).size());
  807. ASSERT_EQ(0, message.GetRepeatedExtension(
  808. unittest::repeated_nested_message_extension).size());
  809. unittest::TestAllTypes::NestedMessage nested_message;
  810. nested_message.set_bb(42);
  811. unittest::TestAllTypes::NestedEnum nested_enum =
  812. unittest::TestAllTypes::NestedEnum_MIN;
  813. for (int i = 0; i < 10; ++i) {
  814. message.AddExtension(unittest::repeated_int32_extension, 1);
  815. message.AddExtension(unittest::repeated_int64_extension, 2);
  816. message.AddExtension(unittest::repeated_uint32_extension, 3);
  817. message.AddExtension(unittest::repeated_uint64_extension, 4);
  818. message.AddExtension(unittest::repeated_sint32_extension, 5);
  819. message.AddExtension(unittest::repeated_sint64_extension, 6);
  820. message.AddExtension(unittest::repeated_fixed32_extension, 7);
  821. message.AddExtension(unittest::repeated_fixed64_extension, 8);
  822. message.AddExtension(unittest::repeated_sfixed32_extension, 7);
  823. message.AddExtension(unittest::repeated_sfixed64_extension, 8);
  824. message.AddExtension(unittest::repeated_float_extension, 9.0);
  825. message.AddExtension(unittest::repeated_double_extension, 10.0);
  826. message.AddExtension(unittest::repeated_bool_extension, true);
  827. message.AddExtension(unittest::repeated_nested_enum_extension, nested_enum);
  828. message.AddExtension(unittest::repeated_string_extension,
  829. ::std::string("test"));
  830. message.AddExtension(unittest::repeated_bytes_extension,
  831. ::std::string("test\xFF"));
  832. message.AddExtension(
  833. unittest::repeated_nested_message_extension)->CopyFrom(nested_message);
  834. message.AddExtension(unittest::repeated_nested_enum_extension,
  835. nested_enum);
  836. }
  837. ASSERT_EQ(10, SumAllExtensions<int32>(
  838. message, unittest::repeated_int32_extension, 0));
  839. IncAllExtensions<int32>(
  840. &message, unittest::repeated_int32_extension, 1);
  841. ASSERT_EQ(20, SumAllExtensions<int32>(
  842. message, unittest::repeated_int32_extension, 0));
  843. ASSERT_EQ(20, SumAllExtensions<int64>(
  844. message, unittest::repeated_int64_extension, 0));
  845. IncAllExtensions<int64>(
  846. &message, unittest::repeated_int64_extension, 1);
  847. ASSERT_EQ(30, SumAllExtensions<int64>(
  848. message, unittest::repeated_int64_extension, 0));
  849. ASSERT_EQ(30, SumAllExtensions<uint32>(
  850. message, unittest::repeated_uint32_extension, 0));
  851. IncAllExtensions<uint32>(
  852. &message, unittest::repeated_uint32_extension, 1);
  853. ASSERT_EQ(40, SumAllExtensions<uint32>(
  854. message, unittest::repeated_uint32_extension, 0));
  855. ASSERT_EQ(40, SumAllExtensions<uint64>(
  856. message, unittest::repeated_uint64_extension, 0));
  857. IncAllExtensions<uint64>(
  858. &message, unittest::repeated_uint64_extension, 1);
  859. ASSERT_EQ(50, SumAllExtensions<uint64>(
  860. message, unittest::repeated_uint64_extension, 0));
  861. ASSERT_EQ(50, SumAllExtensions<int32>(
  862. message, unittest::repeated_sint32_extension, 0));
  863. IncAllExtensions<int32>(
  864. &message, unittest::repeated_sint32_extension, 1);
  865. ASSERT_EQ(60, SumAllExtensions<int32>(
  866. message, unittest::repeated_sint32_extension, 0));
  867. ASSERT_EQ(60, SumAllExtensions<int64>(
  868. message, unittest::repeated_sint64_extension, 0));
  869. IncAllExtensions<int64>(
  870. &message, unittest::repeated_sint64_extension, 1);
  871. ASSERT_EQ(70, SumAllExtensions<int64>(
  872. message, unittest::repeated_sint64_extension, 0));
  873. ASSERT_EQ(70, SumAllExtensions<uint32>(
  874. message, unittest::repeated_fixed32_extension, 0));
  875. IncAllExtensions<uint32>(
  876. &message, unittest::repeated_fixed32_extension, 1);
  877. ASSERT_EQ(80, SumAllExtensions<uint32>(
  878. message, unittest::repeated_fixed32_extension, 0));
  879. ASSERT_EQ(80, SumAllExtensions<uint64>(
  880. message, unittest::repeated_fixed64_extension, 0));
  881. IncAllExtensions<uint64>(
  882. &message, unittest::repeated_fixed64_extension, 1);
  883. ASSERT_EQ(90, SumAllExtensions<uint64>(
  884. message, unittest::repeated_fixed64_extension, 0));
  885. // Usually, floating-point arithmetic cannot be trusted to be exact, so it is
  886. // a Bad Idea to assert equality in a test like this. However, we're dealing
  887. // with integers with a small number of significant mantissa bits, so we
  888. // should actually have exact precision here.
  889. ASSERT_EQ(90, SumAllExtensions<float>(
  890. message, unittest::repeated_float_extension, 0));
  891. IncAllExtensions<float>(
  892. &message, unittest::repeated_float_extension, 1);
  893. ASSERT_EQ(100, SumAllExtensions<float>(
  894. message, unittest::repeated_float_extension, 0));
  895. ASSERT_EQ(100, SumAllExtensions<double>(
  896. message, unittest::repeated_double_extension, 0));
  897. IncAllExtensions<double>(
  898. &message, unittest::repeated_double_extension, 1);
  899. ASSERT_EQ(110, SumAllExtensions<double>(
  900. message, unittest::repeated_double_extension, 0));
  901. RepeatedPtrField<::std::string>::iterator string_iter;
  902. RepeatedPtrField<::std::string>::iterator string_end;
  903. for (string_iter = message.MutableRepeatedExtension(
  904. unittest::repeated_string_extension)->begin(),
  905. string_end = message.MutableRepeatedExtension(
  906. unittest::repeated_string_extension)->end();
  907. string_iter != string_end; ++string_iter) {
  908. *string_iter += "test";
  909. }
  910. RepeatedPtrField<::std::string>::const_iterator string_const_iter;
  911. RepeatedPtrField<::std::string>::const_iterator string_const_end;
  912. for (string_const_iter = message.GetRepeatedExtension(
  913. unittest::repeated_string_extension).begin(),
  914. string_const_end = message.GetRepeatedExtension(
  915. unittest::repeated_string_extension).end();
  916. string_iter != string_end; ++string_iter) {
  917. ASSERT_TRUE(*string_iter == "testtest");
  918. }
  919. RepeatedField<unittest::TestAllTypes_NestedEnum>::iterator enum_iter;
  920. RepeatedField<unittest::TestAllTypes_NestedEnum>::iterator enum_end;
  921. for (enum_iter = message.MutableRepeatedExtension(
  922. unittest::repeated_nested_enum_extension)->begin(),
  923. enum_end = message.MutableRepeatedExtension(
  924. unittest::repeated_nested_enum_extension)->end();
  925. enum_iter != enum_end; ++enum_iter) {
  926. *enum_iter = unittest::TestAllTypes::NestedEnum_MAX;
  927. }
  928. RepeatedField<unittest::TestAllTypes_NestedEnum>::const_iterator
  929. enum_const_iter;
  930. RepeatedField<unittest::TestAllTypes_NestedEnum>::const_iterator
  931. enum_const_end;
  932. for (enum_const_iter = message.GetRepeatedExtension(
  933. unittest::repeated_nested_enum_extension).begin(),
  934. enum_const_end = message.GetRepeatedExtension(
  935. unittest::repeated_nested_enum_extension).end();
  936. enum_iter != enum_end; ++enum_iter) {
  937. ASSERT_EQ(*enum_const_iter, unittest::TestAllTypes::NestedEnum_MAX);
  938. }
  939. RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::iterator
  940. msg_iter;
  941. RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::iterator
  942. msg_end;
  943. for (msg_iter = message.MutableRepeatedExtension(
  944. unittest::repeated_nested_message_extension)->begin(),
  945. msg_end = message.MutableRepeatedExtension(
  946. unittest::repeated_nested_message_extension)->end();
  947. msg_iter != msg_end; ++msg_iter) {
  948. msg_iter->set_bb(1234);
  949. }
  950. RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::
  951. const_iterator msg_const_iter;
  952. RepeatedPtrField<unittest::TestAllTypes_NestedMessage>::
  953. const_iterator msg_const_end;
  954. for (msg_const_iter = message.GetRepeatedExtension(
  955. unittest::repeated_nested_message_extension).begin(),
  956. msg_const_end = message.GetRepeatedExtension(
  957. unittest::repeated_nested_message_extension).end();
  958. msg_const_iter != msg_const_end; ++msg_const_iter) {
  959. ASSERT_EQ(msg_const_iter->bb(), 1234);
  960. }
  961. // Test range-based for as well, but only if compiled as C++11.
  962. #if __cplusplus >= 201103L
  963. // Test one primitive field.
  964. for (auto& x : *message.MutableRepeatedExtension(
  965. unittest::repeated_int32_extension)) {
  966. x = 4321;
  967. }
  968. for (const auto& x : message.GetRepeatedExtension(
  969. unittest::repeated_int32_extension)) {
  970. ASSERT_EQ(x, 4321);
  971. }
  972. // Test one string field.
  973. for (auto& x : *message.MutableRepeatedExtension(
  974. unittest::repeated_string_extension)) {
  975. x = "test_range_based_for";
  976. }
  977. for (const auto& x : message.GetRepeatedExtension(
  978. unittest::repeated_string_extension)) {
  979. ASSERT_TRUE(x == "test_range_based_for");
  980. }
  981. // Test one message field.
  982. for (auto& x : *message.MutableRepeatedExtension(
  983. unittest::repeated_nested_message_extension)) {
  984. x.set_bb(4321);
  985. }
  986. for (const auto& x : *message.MutableRepeatedExtension(
  987. unittest::repeated_nested_message_extension)) {
  988. ASSERT_EQ(x.bb(), 4321);
  989. }
  990. #endif
  991. }
  992. // From b/12926163
  993. TEST(ExtensionSetTest, AbsentExtension) {
  994. unittest::TestAllExtensions message;
  995. message.MutableRepeatedExtension(unittest::repeated_nested_message_extension)
  996. ->Add()->set_bb(123);
  997. ASSERT_EQ(1, message.ExtensionSize(
  998. unittest::repeated_nested_message_extension));
  999. EXPECT_EQ(
  1000. 123, message.GetExtension(
  1001. unittest::repeated_nested_message_extension, 0).bb());
  1002. }
  1003. #ifdef PROTOBUF_HAS_DEATH_TEST
  1004. TEST(ExtensionSetTest, InvalidEnumDeath) {
  1005. unittest::TestAllExtensions message;
  1006. EXPECT_DEBUG_DEATH(
  1007. message.SetExtension(unittest::optional_foreign_enum_extension,
  1008. static_cast<unittest::ForeignEnum>(53)),
  1009. "IsValid");
  1010. }
  1011. #endif // PROTOBUF_HAS_DEATH_TEST
  1012. TEST(ExtensionSetTest, DynamicExtensions) {
  1013. // Test adding a dynamic extension to a compiled-in message object.
  1014. FileDescriptorProto dynamic_proto;
  1015. dynamic_proto.set_name("dynamic_extensions_test.proto");
  1016. dynamic_proto.add_dependency(
  1017. unittest::TestAllExtensions::descriptor()->file()->name());
  1018. dynamic_proto.set_package("dynamic_extensions");
  1019. // Copy the fields and nested types from TestDynamicExtensions into our new
  1020. // proto, converting the fields into extensions.
  1021. const Descriptor* template_descriptor =
  1022. unittest::TestDynamicExtensions::descriptor();
  1023. DescriptorProto template_descriptor_proto;
  1024. template_descriptor->CopyTo(&template_descriptor_proto);
  1025. dynamic_proto.mutable_message_type()->MergeFrom(
  1026. template_descriptor_proto.nested_type());
  1027. dynamic_proto.mutable_enum_type()->MergeFrom(
  1028. template_descriptor_proto.enum_type());
  1029. dynamic_proto.mutable_extension()->MergeFrom(
  1030. template_descriptor_proto.field());
  1031. // For each extension that we added...
  1032. for (int i = 0; i < dynamic_proto.extension_size(); i++) {
  1033. // Set its extendee to TestAllExtensions.
  1034. FieldDescriptorProto* extension = dynamic_proto.mutable_extension(i);
  1035. extension->set_extendee(
  1036. unittest::TestAllExtensions::descriptor()->full_name());
  1037. // If the field refers to one of the types nested in TestDynamicExtensions,
  1038. // make it refer to the type in our dynamic proto instead.
  1039. string prefix = "." + template_descriptor->full_name() + ".";
  1040. if (extension->has_type_name()) {
  1041. string* type_name = extension->mutable_type_name();
  1042. if (HasPrefixString(*type_name, prefix)) {
  1043. type_name->replace(0, prefix.size(), ".dynamic_extensions.");
  1044. }
  1045. }
  1046. }
  1047. // Now build the file, using the generated pool as an underlay.
  1048. DescriptorPool dynamic_pool(DescriptorPool::generated_pool());
  1049. const FileDescriptor* file = dynamic_pool.BuildFile(dynamic_proto);
  1050. ASSERT_TRUE(file != NULL);
  1051. DynamicMessageFactory dynamic_factory(&dynamic_pool);
  1052. dynamic_factory.SetDelegateToGeneratedFactory(true);
  1053. // Construct a message that we can parse with the extensions we defined.
  1054. // Since the extensions were based off of the fields of TestDynamicExtensions,
  1055. // we can use that message to create this test message.
  1056. string data;
  1057. {
  1058. unittest::TestDynamicExtensions message;
  1059. message.set_scalar_extension(123);
  1060. message.set_enum_extension(unittest::FOREIGN_BAR);
  1061. message.set_dynamic_enum_extension(
  1062. unittest::TestDynamicExtensions::DYNAMIC_BAZ);
  1063. message.mutable_message_extension()->set_c(456);
  1064. message.mutable_dynamic_message_extension()->set_dynamic_field(789);
  1065. message.add_repeated_extension("foo");
  1066. message.add_repeated_extension("bar");
  1067. message.add_packed_extension(12);
  1068. message.add_packed_extension(-34);
  1069. message.add_packed_extension(56);
  1070. message.add_packed_extension(-78);
  1071. // Also add some unknown fields.
  1072. // An unknown enum value (for a known field).
  1073. message.mutable_unknown_fields()->AddVarint(
  1074. unittest::TestDynamicExtensions::kDynamicEnumExtensionFieldNumber,
  1075. 12345);
  1076. // A regular unknown field.
  1077. message.mutable_unknown_fields()->AddLengthDelimited(54321, "unknown");
  1078. message.SerializeToString(&data);
  1079. }
  1080. // Now we can parse this using our dynamic extension definitions...
  1081. unittest::TestAllExtensions message;
  1082. {
  1083. io::ArrayInputStream raw_input(data.data(), data.size());
  1084. io::CodedInputStream input(&raw_input);
  1085. input.SetExtensionRegistry(&dynamic_pool, &dynamic_factory);
  1086. ASSERT_TRUE(message.ParseFromCodedStream(&input));
  1087. ASSERT_TRUE(input.ConsumedEntireMessage());
  1088. }
  1089. // Can we print it?
  1090. EXPECT_EQ(
  1091. "[dynamic_extensions.scalar_extension]: 123\n"
  1092. "[dynamic_extensions.enum_extension]: FOREIGN_BAR\n"
  1093. "[dynamic_extensions.dynamic_enum_extension]: DYNAMIC_BAZ\n"
  1094. "[dynamic_extensions.message_extension] {\n"
  1095. " c: 456\n"
  1096. "}\n"
  1097. "[dynamic_extensions.dynamic_message_extension] {\n"
  1098. " dynamic_field: 789\n"
  1099. "}\n"
  1100. "[dynamic_extensions.repeated_extension]: \"foo\"\n"
  1101. "[dynamic_extensions.repeated_extension]: \"bar\"\n"
  1102. "[dynamic_extensions.packed_extension]: 12\n"
  1103. "[dynamic_extensions.packed_extension]: -34\n"
  1104. "[dynamic_extensions.packed_extension]: 56\n"
  1105. "[dynamic_extensions.packed_extension]: -78\n"
  1106. "2002: 12345\n"
  1107. "54321: \"unknown\"\n",
  1108. message.DebugString());
  1109. // Can we serialize it?
  1110. // (Don't use EXPECT_EQ because we don't want to dump raw binary data to the
  1111. // terminal on failure.)
  1112. EXPECT_TRUE(message.SerializeAsString() == data);
  1113. // What if we parse using the reflection-based parser?
  1114. {
  1115. unittest::TestAllExtensions message2;
  1116. io::ArrayInputStream raw_input(data.data(), data.size());
  1117. io::CodedInputStream input(&raw_input);
  1118. input.SetExtensionRegistry(&dynamic_pool, &dynamic_factory);
  1119. ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &message2));
  1120. ASSERT_TRUE(input.ConsumedEntireMessage());
  1121. EXPECT_EQ(message.DebugString(), message2.DebugString());
  1122. }
  1123. // Are the embedded generated types actually using the generated objects?
  1124. {
  1125. const FieldDescriptor* message_extension =
  1126. file->FindExtensionByName("message_extension");
  1127. ASSERT_TRUE(message_extension != NULL);
  1128. const Message& sub_message =
  1129. message.GetReflection()->GetMessage(message, message_extension);
  1130. const unittest::ForeignMessage* typed_sub_message =
  1131. #ifdef GOOGLE_PROTOBUF_NO_RTTI
  1132. static_cast<const unittest::ForeignMessage*>(&sub_message);
  1133. #else
  1134. dynamic_cast<const unittest::ForeignMessage*>(&sub_message);
  1135. #endif
  1136. ASSERT_TRUE(typed_sub_message != NULL);
  1137. EXPECT_EQ(456, typed_sub_message->c());
  1138. }
  1139. // What does GetMessage() return for the embedded dynamic type if it isn't
  1140. // present?
  1141. {
  1142. const FieldDescriptor* dynamic_message_extension =
  1143. file->FindExtensionByName("dynamic_message_extension");
  1144. ASSERT_TRUE(dynamic_message_extension != NULL);
  1145. const Message& parent = unittest::TestAllExtensions::default_instance();
  1146. const Message& sub_message =
  1147. parent.GetReflection()->GetMessage(parent, dynamic_message_extension,
  1148. &dynamic_factory);
  1149. const Message* prototype =
  1150. dynamic_factory.GetPrototype(dynamic_message_extension->message_type());
  1151. EXPECT_EQ(prototype, &sub_message);
  1152. }
  1153. }
  1154. } // namespace
  1155. } // namespace internal
  1156. } // namespace protobuf
  1157. } // namespace google