lite_unittest.cc 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. #include <string>
  32. #include <iostream>
  33. #include <google/protobuf/stubs/logging.h>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/arena_test_util.h>
  36. #include <google/protobuf/map_lite_test_util.h>
  37. #include <google/protobuf/map_lite_unittest.pb.h>
  38. #include <google/protobuf/test_util_lite.h>
  39. #include <google/protobuf/unittest_lite.pb.h>
  40. #include <google/protobuf/io/coded_stream.h>
  41. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  42. #include <google/protobuf/wire_format_lite.h>
  43. #include <google/protobuf/wire_format_lite_inl.h>
  44. #include <gtest/gtest.h>
  45. #include <google/protobuf/stubs/strutil.h>
  46. using std::string;
  47. namespace {
  48. // Helper methods to test parsing merge behavior.
  49. void ExpectMessageMerged(const google::protobuf::unittest::TestAllTypesLite& message) {
  50. EXPECT_EQ(message.optional_int32(), 3);
  51. EXPECT_EQ(message.optional_int64(), 2);
  52. EXPECT_EQ(message.optional_string(), "hello");
  53. }
  54. void AssignParsingMergeMessages(
  55. google::protobuf::unittest::TestAllTypesLite* msg1,
  56. google::protobuf::unittest::TestAllTypesLite* msg2,
  57. google::protobuf::unittest::TestAllTypesLite* msg3) {
  58. msg1->set_optional_int32(1);
  59. msg2->set_optional_int64(2);
  60. msg3->set_optional_int32(3);
  61. msg3->set_optional_string("hello");
  62. }
  63. void SetAllTypesInEmptyMessageUnknownFields(
  64. google::protobuf::unittest::TestEmptyMessageLite* empty_message) {
  65. protobuf_unittest::TestAllTypesLite message;
  66. google::protobuf::TestUtilLite::ExpectClear(message);
  67. google::protobuf::TestUtilLite::SetAllFields(&message);
  68. string data = message.SerializeAsString();
  69. empty_message->ParseFromString(data);
  70. }
  71. void SetSomeTypesInEmptyMessageUnknownFields(
  72. google::protobuf::unittest::TestEmptyMessageLite* empty_message) {
  73. protobuf_unittest::TestAllTypesLite message;
  74. google::protobuf::TestUtilLite::ExpectClear(message);
  75. message.set_optional_int32(101);
  76. message.set_optional_int64(102);
  77. message.set_optional_uint32(103);
  78. message.set_optional_uint64(104);
  79. string data = message.SerializeAsString();
  80. empty_message->ParseFromString(data);
  81. }
  82. } // namespace
  83. TEST(Lite, AllLite1) {
  84. string data;
  85. {
  86. protobuf_unittest::TestAllTypesLite message, message2, message3;
  87. google::protobuf::TestUtilLite::ExpectClear(message);
  88. google::protobuf::TestUtilLite::SetAllFields(&message);
  89. message2.CopyFrom(message);
  90. data = message.SerializeAsString();
  91. message3.ParseFromString(data);
  92. google::protobuf::TestUtilLite::ExpectAllFieldsSet(message);
  93. google::protobuf::TestUtilLite::ExpectAllFieldsSet(message2);
  94. google::protobuf::TestUtilLite::ExpectAllFieldsSet(message3);
  95. google::protobuf::TestUtilLite::ModifyRepeatedFields(&message);
  96. google::protobuf::TestUtilLite::ExpectRepeatedFieldsModified(message);
  97. message.Clear();
  98. google::protobuf::TestUtilLite::ExpectClear(message);
  99. }
  100. }
  101. TEST(Lite, AllLite2) {
  102. string data;
  103. {
  104. protobuf_unittest::TestAllExtensionsLite message, message2, message3;
  105. google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
  106. google::protobuf::TestUtilLite::SetAllExtensions(&message);
  107. message2.CopyFrom(message);
  108. string extensions_data = message.SerializeAsString();
  109. message3.ParseFromString(extensions_data);
  110. google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message);
  111. google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message2);
  112. google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message3);
  113. google::protobuf::TestUtilLite::ModifyRepeatedExtensions(&message);
  114. google::protobuf::TestUtilLite::ExpectRepeatedExtensionsModified(message);
  115. message.Clear();
  116. google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
  117. }
  118. }
  119. TEST(Lite, AllLite3) {
  120. string data, packed_data;
  121. {
  122. protobuf_unittest::TestPackedTypesLite message, message2, message3;
  123. google::protobuf::TestUtilLite::ExpectPackedClear(message);
  124. google::protobuf::TestUtilLite::SetPackedFields(&message);
  125. message2.CopyFrom(message);
  126. packed_data = message.SerializeAsString();
  127. message3.ParseFromString(packed_data);
  128. google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message);
  129. google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message2);
  130. google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message3);
  131. google::protobuf::TestUtilLite::ModifyPackedFields(&message);
  132. google::protobuf::TestUtilLite::ExpectPackedFieldsModified(message);
  133. message.Clear();
  134. google::protobuf::TestUtilLite::ExpectPackedClear(message);
  135. }
  136. {
  137. protobuf_unittest::TestPackedExtensionsLite message, message2, message3;
  138. google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
  139. google::protobuf::TestUtilLite::SetPackedExtensions(&message);
  140. message2.CopyFrom(message);
  141. string packed_extensions_data = message.SerializeAsString();
  142. EXPECT_EQ(packed_extensions_data, packed_data);
  143. message3.ParseFromString(packed_extensions_data);
  144. google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message);
  145. google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2);
  146. google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message3);
  147. google::protobuf::TestUtilLite::ModifyPackedExtensions(&message);
  148. google::protobuf::TestUtilLite::ExpectPackedExtensionsModified(message);
  149. message.Clear();
  150. google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
  151. }
  152. }
  153. TEST(Lite, AllLite5) {
  154. string data;
  155. {
  156. // Test that if an optional or required message/group field appears multiple
  157. // times in the input, they need to be merged.
  158. google::protobuf::unittest::TestParsingMergeLite::RepeatedFieldsGenerator generator;
  159. google::protobuf::unittest::TestAllTypesLite* msg1;
  160. google::protobuf::unittest::TestAllTypesLite* msg2;
  161. google::protobuf::unittest::TestAllTypesLite* msg3;
  162. #define ASSIGN_REPEATED_FIELD(FIELD) \
  163. msg1 = generator.add_##FIELD(); \
  164. msg2 = generator.add_##FIELD(); \
  165. msg3 = generator.add_##FIELD(); \
  166. AssignParsingMergeMessages(msg1, msg2, msg3)
  167. ASSIGN_REPEATED_FIELD(field1);
  168. ASSIGN_REPEATED_FIELD(field2);
  169. ASSIGN_REPEATED_FIELD(field3);
  170. ASSIGN_REPEATED_FIELD(ext1);
  171. ASSIGN_REPEATED_FIELD(ext2);
  172. #undef ASSIGN_REPEATED_FIELD
  173. #define ASSIGN_REPEATED_GROUP(FIELD) \
  174. msg1 = generator.add_##FIELD()->mutable_field1(); \
  175. msg2 = generator.add_##FIELD()->mutable_field1(); \
  176. msg3 = generator.add_##FIELD()->mutable_field1(); \
  177. AssignParsingMergeMessages(msg1, msg2, msg3)
  178. ASSIGN_REPEATED_GROUP(group1);
  179. ASSIGN_REPEATED_GROUP(group2);
  180. #undef ASSIGN_REPEATED_GROUP
  181. string buffer;
  182. generator.SerializeToString(&buffer);
  183. google::protobuf::unittest::TestParsingMergeLite parsing_merge;
  184. parsing_merge.ParseFromString(buffer);
  185. // Required and optional fields should be merged.
  186. ExpectMessageMerged(parsing_merge.required_all_types());
  187. ExpectMessageMerged(parsing_merge.optional_all_types());
  188. ExpectMessageMerged(
  189. parsing_merge.optionalgroup().optional_group_all_types());
  190. ExpectMessageMerged(parsing_merge.GetExtension(
  191. google::protobuf::unittest::TestParsingMergeLite::optional_ext));
  192. // Repeated fields should not be merged.
  193. EXPECT_EQ(parsing_merge.repeated_all_types_size(), 3);
  194. EXPECT_EQ(parsing_merge.repeatedgroup_size(), 3);
  195. EXPECT_EQ(parsing_merge.ExtensionSize(
  196. google::protobuf::unittest::TestParsingMergeLite::repeated_ext),
  197. 3);
  198. }
  199. }
  200. TEST(Lite, AllLite6) {
  201. string data;
  202. // Test unknown fields support for lite messages.
  203. {
  204. protobuf_unittest::TestAllTypesLite message, message2;
  205. protobuf_unittest::TestEmptyMessageLite empty_message;
  206. google::protobuf::TestUtilLite::ExpectClear(message);
  207. google::protobuf::TestUtilLite::SetAllFields(&message);
  208. data = message.SerializeAsString();
  209. empty_message.ParseFromString(data);
  210. data.clear();
  211. data = empty_message.SerializeAsString();
  212. message2.ParseFromString(data);
  213. data = message2.SerializeAsString();
  214. google::protobuf::TestUtilLite::ExpectAllFieldsSet(message2);
  215. message.Clear();
  216. google::protobuf::TestUtilLite::ExpectClear(message);
  217. }
  218. }
  219. TEST(Lite, AllLite7) {
  220. string data;
  221. {
  222. protobuf_unittest::TestAllExtensionsLite message, message2;
  223. protobuf_unittest::TestEmptyMessageLite empty_message;
  224. google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
  225. google::protobuf::TestUtilLite::SetAllExtensions(&message);
  226. data = message.SerializeAsString();
  227. empty_message.ParseFromString(data);
  228. data.clear();
  229. data = empty_message.SerializeAsString();
  230. message2.ParseFromString(data);
  231. data = message2.SerializeAsString();
  232. google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message2);
  233. message.Clear();
  234. google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
  235. }
  236. }
  237. TEST(Lite, AllLite8) {
  238. string data;
  239. {
  240. protobuf_unittest::TestPackedTypesLite message, message2;
  241. protobuf_unittest::TestEmptyMessageLite empty_message;
  242. google::protobuf::TestUtilLite::ExpectPackedClear(message);
  243. google::protobuf::TestUtilLite::SetPackedFields(&message);
  244. data = message.SerializeAsString();
  245. empty_message.ParseFromString(data);
  246. data.clear();
  247. data = empty_message.SerializeAsString();
  248. message2.ParseFromString(data);
  249. data = message2.SerializeAsString();
  250. google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message2);
  251. message.Clear();
  252. google::protobuf::TestUtilLite::ExpectPackedClear(message);
  253. }
  254. }
  255. TEST(Lite, AllLite9) {
  256. string data;
  257. {
  258. protobuf_unittest::TestPackedExtensionsLite message, message2;
  259. protobuf_unittest::TestEmptyMessageLite empty_message;
  260. google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
  261. google::protobuf::TestUtilLite::SetPackedExtensions(&message);
  262. data = message.SerializeAsString();
  263. empty_message.ParseFromString(data);
  264. data.clear();
  265. data = empty_message.SerializeAsString();
  266. message2.ParseFromString(data);
  267. data = message2.SerializeAsString();
  268. google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2);
  269. message.Clear();
  270. google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
  271. }
  272. }
  273. TEST(Lite, AllLite10) {
  274. string data;
  275. {
  276. // Test Unknown fields swap
  277. protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
  278. SetAllTypesInEmptyMessageUnknownFields(&empty_message);
  279. SetSomeTypesInEmptyMessageUnknownFields(&empty_message2);
  280. data = empty_message.SerializeAsString();
  281. string data2 = empty_message2.SerializeAsString();
  282. empty_message.Swap(&empty_message2);
  283. EXPECT_EQ(data, empty_message2.SerializeAsString());
  284. EXPECT_EQ(data2, empty_message.SerializeAsString());
  285. }
  286. }
  287. TEST(Lite, AllLite11) {
  288. string data;
  289. {
  290. // Test unknown fields swap with self
  291. protobuf_unittest::TestEmptyMessageLite empty_message;
  292. SetAllTypesInEmptyMessageUnknownFields(&empty_message);
  293. data = empty_message.SerializeAsString();
  294. empty_message.Swap(&empty_message);
  295. EXPECT_EQ(data, empty_message.SerializeAsString());
  296. }
  297. }
  298. TEST(Lite, AllLite12) {
  299. string data;
  300. {
  301. // Test MergeFrom with unknown fields
  302. protobuf_unittest::TestAllTypesLite message, message2;
  303. protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
  304. message.set_optional_int32(101);
  305. message.add_repeated_int32(201);
  306. message.set_optional_nested_enum(google::protobuf::unittest::TestAllTypesLite::BAZ);
  307. message2.set_optional_int64(102);
  308. message2.add_repeated_int64(202);
  309. message2.set_optional_foreign_enum(google::protobuf::unittest::FOREIGN_LITE_BAZ);
  310. data = message.SerializeAsString();
  311. empty_message.ParseFromString(data);
  312. data = message2.SerializeAsString();
  313. empty_message2.ParseFromString(data);
  314. message.MergeFrom(message2);
  315. empty_message.MergeFrom(empty_message2);
  316. data = empty_message.SerializeAsString();
  317. message2.ParseFromString(data);
  318. // We do not compare the serialized output of a normal message and a lite
  319. // message because the order of fields do not match. We convert lite message
  320. // back into normal message, then compare.
  321. EXPECT_EQ(message.SerializeAsString(), message2.SerializeAsString());
  322. }
  323. }
  324. TEST(Lite, AllLite13) {
  325. string data;
  326. {
  327. // Test unknown enum value
  328. protobuf_unittest::TestAllTypesLite message;
  329. string buffer;
  330. {
  331. google::protobuf::io::StringOutputStream output_stream(&buffer);
  332. google::protobuf::io::CodedOutputStream coded_output(&output_stream);
  333. google::protobuf::internal::WireFormatLite::WriteTag(
  334. protobuf_unittest::TestAllTypesLite::kOptionalNestedEnumFieldNumber,
  335. google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
  336. coded_output.WriteVarint32(10);
  337. google::protobuf::internal::WireFormatLite::WriteTag(
  338. protobuf_unittest::TestAllTypesLite::kRepeatedNestedEnumFieldNumber,
  339. google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
  340. coded_output.WriteVarint32(20);
  341. }
  342. message.ParseFromString(buffer);
  343. data = message.SerializeAsString();
  344. EXPECT_EQ(data, buffer);
  345. }
  346. }
  347. TEST(Lite, AllLite14) {
  348. string data;
  349. {
  350. // Test Clear with unknown fields
  351. protobuf_unittest::TestEmptyMessageLite empty_message;
  352. SetAllTypesInEmptyMessageUnknownFields(&empty_message);
  353. empty_message.Clear();
  354. EXPECT_EQ(0, empty_message.unknown_fields().size());
  355. }
  356. }
  357. // Tests for map lite =============================================
  358. TEST(Lite, AllLite15) {
  359. string data;
  360. {
  361. // Accessors
  362. protobuf_unittest::TestMapLite message;
  363. google::protobuf::MapLiteTestUtil::SetMapFields(&message);
  364. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message);
  365. google::protobuf::MapLiteTestUtil::ModifyMapFields(&message);
  366. google::protobuf::MapLiteTestUtil::ExpectMapFieldsModified(message);
  367. }
  368. }
  369. TEST(Lite, AllLite16) {
  370. string data;
  371. {
  372. // SetMapFieldsInitialized
  373. protobuf_unittest::TestMapLite message;
  374. google::protobuf::MapLiteTestUtil::SetMapFieldsInitialized(&message);
  375. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSetInitialized(message);
  376. }
  377. }
  378. TEST(Lite, AllLite17) {
  379. string data;
  380. {
  381. // Clear
  382. protobuf_unittest::TestMapLite message;
  383. google::protobuf::MapLiteTestUtil::SetMapFields(&message);
  384. message.Clear();
  385. google::protobuf::MapLiteTestUtil::ExpectClear(message);
  386. }
  387. }
  388. TEST(Lite, AllLite18) {
  389. string data;
  390. {
  391. // ClearMessageMap
  392. protobuf_unittest::TestMessageMapLite message;
  393. // Creates a TestAllTypes with default value
  394. google::protobuf::TestUtilLite::ExpectClear(
  395. (*message.mutable_map_int32_message())[0]);
  396. }
  397. }
  398. TEST(Lite, AllLite19) {
  399. string data;
  400. {
  401. // CopyFrom
  402. protobuf_unittest::TestMapLite message1, message2;
  403. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  404. message2.CopyFrom(message1);
  405. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  406. // Copying from self should be a no-op.
  407. message2.CopyFrom(message2);
  408. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  409. }
  410. }
  411. TEST(Lite, AllLite20) {
  412. string data;
  413. {
  414. // CopyFromMessageMap
  415. protobuf_unittest::TestMessageMapLite message1, message2;
  416. (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
  417. (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
  418. message1.CopyFrom(message2);
  419. // Checks repeated field is overwritten.
  420. EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
  421. EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
  422. }
  423. }
  424. TEST(Lite, AllLite21) {
  425. string data;
  426. {
  427. // SwapWithEmpty
  428. protobuf_unittest::TestMapLite message1, message2;
  429. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  430. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message1);
  431. google::protobuf::MapLiteTestUtil::ExpectClear(message2);
  432. message1.Swap(&message2);
  433. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  434. google::protobuf::MapLiteTestUtil::ExpectClear(message1);
  435. }
  436. }
  437. TEST(Lite, AllLite22) {
  438. string data;
  439. {
  440. // SwapWithSelf
  441. protobuf_unittest::TestMapLite message;
  442. google::protobuf::MapLiteTestUtil::SetMapFields(&message);
  443. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message);
  444. message.Swap(&message);
  445. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message);
  446. }
  447. }
  448. TEST(Lite, AllLite23) {
  449. string data;
  450. {
  451. // SwapWithOther
  452. protobuf_unittest::TestMapLite message1, message2;
  453. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  454. google::protobuf::MapLiteTestUtil::SetMapFields(&message2);
  455. google::protobuf::MapLiteTestUtil::ModifyMapFields(&message2);
  456. message1.Swap(&message2);
  457. google::protobuf::MapLiteTestUtil::ExpectMapFieldsModified(message1);
  458. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  459. }
  460. }
  461. TEST(Lite, AllLite24) {
  462. string data;
  463. {
  464. // CopyConstructor
  465. protobuf_unittest::TestMapLite message1;
  466. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  467. protobuf_unittest::TestMapLite message2(message1);
  468. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  469. }
  470. }
  471. TEST(Lite, AllLite25) {
  472. string data;
  473. {
  474. // CopyAssignmentOperator
  475. protobuf_unittest::TestMapLite message1;
  476. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  477. protobuf_unittest::TestMapLite message2;
  478. message2 = message1;
  479. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  480. // Make sure that self-assignment does something sane.
  481. message2.operator=(message2);
  482. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  483. }
  484. }
  485. TEST(Lite, AllLite26) {
  486. string data;
  487. {
  488. // NonEmptyMergeFrom
  489. protobuf_unittest::TestMapLite message1, message2;
  490. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  491. // This field will test merging into an empty spot.
  492. (*message2.mutable_map_int32_int32())[1] = 1;
  493. message1.mutable_map_int32_int32()->erase(1);
  494. // This tests overwriting.
  495. (*message2.mutable_map_int32_double())[1] = 1;
  496. (*message1.mutable_map_int32_double())[1] = 2;
  497. message1.MergeFrom(message2);
  498. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message1);
  499. }
  500. }
  501. TEST(Lite, AllLite27) {
  502. string data;
  503. {
  504. // MergeFromMessageMap
  505. protobuf_unittest::TestMessageMapLite message1, message2;
  506. (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
  507. (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
  508. message1.MergeFrom(message2);
  509. // Checks repeated field is overwritten.
  510. EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
  511. EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
  512. }
  513. }
  514. TEST(Lite, AllLite28) {
  515. string data;
  516. {
  517. // Test the generated SerializeWithCachedSizesToArray()
  518. protobuf_unittest::TestMapLite message1, message2;
  519. string data;
  520. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  521. int size = message1.ByteSize();
  522. data.resize(size);
  523. ::google::protobuf::uint8* start = reinterpret_cast<::google::protobuf::uint8*>(::google::protobuf::string_as_array(&data));
  524. ::google::protobuf::uint8* end = message1.SerializeWithCachedSizesToArray(start);
  525. EXPECT_EQ(size, end - start);
  526. EXPECT_TRUE(message2.ParseFromString(data));
  527. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  528. }
  529. }
  530. TEST(Lite, AllLite29) {
  531. string data;
  532. {
  533. // Test the generated SerializeWithCachedSizes()
  534. protobuf_unittest::TestMapLite message1, message2;
  535. google::protobuf::MapLiteTestUtil::SetMapFields(&message1);
  536. int size = message1.ByteSize();
  537. string data;
  538. data.resize(size);
  539. {
  540. // Allow the output stream to buffer only one byte at a time.
  541. google::protobuf::io::ArrayOutputStream array_stream(
  542. ::google::protobuf::string_as_array(&data), size, 1);
  543. google::protobuf::io::CodedOutputStream output_stream(&array_stream);
  544. message1.SerializeWithCachedSizes(&output_stream);
  545. EXPECT_FALSE(output_stream.HadError());
  546. EXPECT_EQ(size, output_stream.ByteCount());
  547. }
  548. EXPECT_TRUE(message2.ParseFromString(data));
  549. google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2);
  550. }
  551. }
  552. TEST(Lite, AllLite32) {
  553. string data;
  554. {
  555. // Proto2UnknownEnum
  556. protobuf_unittest::TestEnumMapPlusExtraLite from;
  557. (*from.mutable_known_map_field())[0] =
  558. protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE;
  559. (*from.mutable_unknown_map_field())[0] =
  560. protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE;
  561. string data;
  562. from.SerializeToString(&data);
  563. protobuf_unittest::TestEnumMapLite to;
  564. EXPECT_TRUE(to.ParseFromString(data));
  565. EXPECT_EQ(0, to.unknown_map_field().size());
  566. EXPECT_FALSE(to.mutable_unknown_fields()->empty());
  567. EXPECT_EQ(1, to.known_map_field().size());
  568. EXPECT_EQ(protobuf_unittest::PROTO2_MAP_ENUM_FOO_LITE,
  569. to.known_map_field().at(0));
  570. data.clear();
  571. from.Clear();
  572. to.SerializeToString(&data);
  573. EXPECT_TRUE(from.ParseFromString(data));
  574. EXPECT_EQ(1, from.known_map_field().size());
  575. EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE,
  576. from.known_map_field().at(0));
  577. EXPECT_EQ(1, from.unknown_map_field().size());
  578. EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE,
  579. from.unknown_map_field().at(0));
  580. }
  581. }
  582. TEST(Lite, AllLite33) {
  583. string data;
  584. {
  585. // StandardWireFormat
  586. protobuf_unittest::TestMapLite message;
  587. string data = "\x0A\x04\x08\x01\x10\x01";
  588. EXPECT_TRUE(message.ParseFromString(data));
  589. EXPECT_EQ(1, message.map_int32_int32().size());
  590. EXPECT_EQ(1, message.map_int32_int32().at(1));
  591. }
  592. }
  593. TEST(Lite, AllLite34) {
  594. string data;
  595. {
  596. // UnorderedWireFormat
  597. protobuf_unittest::TestMapLite message;
  598. // put value before key in wire format
  599. string data = "\x0A\x04\x10\x01\x08\x02";
  600. EXPECT_TRUE(message.ParseFromString(data));
  601. EXPECT_EQ(1, message.map_int32_int32().size());
  602. EXPECT_EQ(1, message.map_int32_int32().at(2));
  603. }
  604. }
  605. TEST(Lite, AllLite35) {
  606. string data;
  607. {
  608. // DuplicatedKeyWireFormat
  609. protobuf_unittest::TestMapLite message;
  610. // Two key fields in wire format
  611. string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
  612. EXPECT_TRUE(message.ParseFromString(data));
  613. EXPECT_EQ(1, message.map_int32_int32().size());
  614. EXPECT_EQ(1, message.map_int32_int32().at(2));
  615. }
  616. }
  617. TEST(Lite, AllLite36) {
  618. string data;
  619. {
  620. // DuplicatedValueWireFormat
  621. protobuf_unittest::TestMapLite message;
  622. // Two value fields in wire format
  623. string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
  624. EXPECT_TRUE(message.ParseFromString(data));
  625. EXPECT_EQ(1, message.map_int32_int32().size());
  626. EXPECT_EQ(2, message.map_int32_int32().at(1));
  627. }
  628. }
  629. TEST(Lite, AllLite37) {
  630. string data;
  631. {
  632. // MissedKeyWireFormat
  633. protobuf_unittest::TestMapLite message;
  634. // No key field in wire format
  635. string data = "\x0A\x02\x10\x01";
  636. EXPECT_TRUE(message.ParseFromString(data));
  637. EXPECT_EQ(1, message.map_int32_int32().size());
  638. EXPECT_EQ(1, message.map_int32_int32().at(0));
  639. }
  640. }
  641. TEST(Lite, AllLite38) {
  642. string data;
  643. {
  644. // MissedValueWireFormat
  645. protobuf_unittest::TestMapLite message;
  646. // No value field in wire format
  647. string data = "\x0A\x02\x08\x01";
  648. EXPECT_TRUE(message.ParseFromString(data));
  649. EXPECT_EQ(1, message.map_int32_int32().size());
  650. EXPECT_EQ(0, message.map_int32_int32().at(1));
  651. }
  652. }
  653. TEST(Lite, AllLite39) {
  654. string data;
  655. {
  656. // UnknownFieldWireFormat
  657. protobuf_unittest::TestMapLite message;
  658. // Unknown field in wire format
  659. string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
  660. EXPECT_TRUE(message.ParseFromString(data));
  661. EXPECT_EQ(1, message.map_int32_int32().size());
  662. EXPECT_EQ(3, message.map_int32_int32().at(2));
  663. }
  664. }
  665. TEST(Lite, AllLite40) {
  666. string data;
  667. {
  668. // CorruptedWireFormat
  669. protobuf_unittest::TestMapLite message;
  670. // corrupted data in wire format
  671. string data = "\x0A\x06\x08\x02\x11\x03";
  672. EXPECT_FALSE(message.ParseFromString(data));
  673. }
  674. }
  675. TEST(Lite, AllLite41) {
  676. string data;
  677. {
  678. // IsInitialized
  679. protobuf_unittest::TestRequiredMessageMapLite map_message;
  680. // Add an uninitialized message.
  681. (*map_message.mutable_map_field())[0];
  682. EXPECT_FALSE(map_message.IsInitialized());
  683. // Initialize uninitialized message
  684. (*map_message.mutable_map_field())[0].set_a(0);
  685. (*map_message.mutable_map_field())[0].set_b(0);
  686. (*map_message.mutable_map_field())[0].set_c(0);
  687. EXPECT_TRUE(map_message.IsInitialized());
  688. }
  689. }
  690. TEST(Lite, AllLite42) {
  691. string data;
  692. {
  693. // Check that adding more values to enum does not corrupt message
  694. // when passed through an old client.
  695. protobuf_unittest::V2MessageLite v2_message;
  696. v2_message.set_int_field(800);
  697. // Set enum field to the value not understood by the old client.
  698. v2_message.set_enum_field(protobuf_unittest::V2_SECOND);
  699. string v2_bytes = v2_message.SerializeAsString();
  700. protobuf_unittest::V1MessageLite v1_message;
  701. v1_message.ParseFromString(v2_bytes);
  702. EXPECT_TRUE(v1_message.IsInitialized());
  703. EXPECT_EQ(v1_message.int_field(), v2_message.int_field());
  704. // V1 client does not understand V2_SECOND value, so it discards it and
  705. // uses default value instead.
  706. EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST);
  707. // However, when re-serialized, it should preserve enum value.
  708. string v1_bytes = v1_message.SerializeAsString();
  709. protobuf_unittest::V2MessageLite same_v2_message;
  710. same_v2_message.ParseFromString(v1_bytes);
  711. EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field());
  712. EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field());
  713. }
  714. }
  715. // Test that when parsing a oneof, we can successfully clear whatever already
  716. // happened to be stored in the oneof.
  717. TEST(Lite, AllLite43) {
  718. protobuf_unittest::TestOneofParsingLite message1;
  719. message1.set_oneof_int32(17);
  720. string serialized;
  721. EXPECT_TRUE(message1.SerializeToString(&serialized));
  722. // Submessage
  723. {
  724. protobuf_unittest::TestOneofParsingLite message2;
  725. message2.mutable_oneof_submessage();
  726. google::protobuf::io::CodedInputStream input_stream(
  727. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
  728. EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
  729. EXPECT_EQ(17, message2.oneof_int32());
  730. }
  731. // String
  732. {
  733. protobuf_unittest::TestOneofParsingLite message2;
  734. message2.set_oneof_string("string");
  735. google::protobuf::io::CodedInputStream input_stream(
  736. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
  737. EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
  738. EXPECT_EQ(17, message2.oneof_int32());
  739. }
  740. // Bytes
  741. {
  742. protobuf_unittest::TestOneofParsingLite message2;
  743. message2.set_oneof_bytes("bytes");
  744. google::protobuf::io::CodedInputStream input_stream(
  745. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
  746. EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
  747. EXPECT_EQ(17, message2.oneof_int32());
  748. }
  749. }
  750. // Verify that we can successfully parse fields of various types within oneof
  751. // fields. We also verify that we can parse the same data twice into the same
  752. // message.
  753. TEST(Lite, AllLite44) {
  754. // Int32
  755. {
  756. protobuf_unittest::TestOneofParsingLite original;
  757. original.set_oneof_int32(17);
  758. string serialized;
  759. EXPECT_TRUE(original.SerializeToString(&serialized));
  760. protobuf_unittest::TestOneofParsingLite parsed;
  761. for (int i = 0; i < 2; ++i) {
  762. google::protobuf::io::CodedInputStream input_stream(
  763. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  764. serialized.size());
  765. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  766. EXPECT_EQ(17, parsed.oneof_int32());
  767. }
  768. }
  769. // Submessage
  770. {
  771. protobuf_unittest::TestOneofParsingLite original;
  772. original.mutable_oneof_submessage()->set_optional_int32(5);
  773. string serialized;
  774. EXPECT_TRUE(original.SerializeToString(&serialized));
  775. protobuf_unittest::TestOneofParsingLite parsed;
  776. for (int i = 0; i < 2; ++i) {
  777. google::protobuf::io::CodedInputStream input_stream(
  778. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  779. serialized.size());
  780. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  781. EXPECT_EQ(5, parsed.oneof_submessage().optional_int32());
  782. }
  783. }
  784. // String
  785. {
  786. protobuf_unittest::TestOneofParsingLite original;
  787. original.set_oneof_string("string");
  788. string serialized;
  789. EXPECT_TRUE(original.SerializeToString(&serialized));
  790. protobuf_unittest::TestOneofParsingLite parsed;
  791. for (int i = 0; i < 2; ++i) {
  792. google::protobuf::io::CodedInputStream input_stream(
  793. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  794. serialized.size());
  795. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  796. EXPECT_EQ("string", parsed.oneof_string());
  797. }
  798. }
  799. // Bytes
  800. {
  801. protobuf_unittest::TestOneofParsingLite original;
  802. original.set_oneof_bytes("bytes");
  803. string serialized;
  804. EXPECT_TRUE(original.SerializeToString(&serialized));
  805. protobuf_unittest::TestOneofParsingLite parsed;
  806. for (int i = 0; i < 2; ++i) {
  807. google::protobuf::io::CodedInputStream input_stream(
  808. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  809. serialized.size());
  810. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  811. EXPECT_EQ("bytes", parsed.oneof_bytes());
  812. }
  813. }
  814. // Enum
  815. {
  816. protobuf_unittest::TestOneofParsingLite original;
  817. original.set_oneof_enum(protobuf_unittest::V2_SECOND);
  818. string serialized;
  819. EXPECT_TRUE(original.SerializeToString(&serialized));
  820. protobuf_unittest::TestOneofParsingLite parsed;
  821. for (int i = 0; i < 2; ++i) {
  822. google::protobuf::io::CodedInputStream input_stream(
  823. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  824. serialized.size());
  825. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  826. EXPECT_EQ(protobuf_unittest::V2_SECOND, parsed.oneof_enum());
  827. }
  828. }
  829. std::cout << "PASS" << std::endl;
  830. }
  831. TEST(Lite, AllLite45) {
  832. // Test unknown fields are not discarded upon parsing.
  833. string data = "\20\1"; // varint 1 with field number 2
  834. protobuf_unittest::ForeignMessageLite a;
  835. EXPECT_TRUE(a.ParseFromString(data));
  836. google::protobuf::io::CodedInputStream input_stream(
  837. reinterpret_cast<const ::google::protobuf::uint8*>(data.data()), data.size());
  838. EXPECT_TRUE(a.MergePartialFromCodedStream(&input_stream));
  839. string serialized = a.SerializeAsString();
  840. EXPECT_EQ(serialized.substr(0, 2), data);
  841. EXPECT_EQ(serialized.substr(2), data);
  842. }
  843. // The following two tests check for wire compatibility between packed and
  844. // unpacked repeated fields. There used to be a bug in the generated parsing
  845. // code that caused us to calculate the highest possible tag number without
  846. // taking into account that a repeated field might not be in the packed (or
  847. // unpacked) state we expect. These tests specifically check for that issue by
  848. // making sure we can parse repeated fields when the tag is higher than we would
  849. // expect.
  850. TEST(Lite, AllLite46) {
  851. protobuf_unittest::PackedInt32 packed;
  852. packed.add_repeated_int32(42);
  853. string serialized;
  854. ASSERT_TRUE(packed.SerializeToString(&serialized));
  855. protobuf_unittest::NonPackedInt32 non_packed;
  856. ASSERT_TRUE(non_packed.ParseFromString(serialized));
  857. ASSERT_EQ(1, non_packed.repeated_int32_size());
  858. EXPECT_EQ(42, non_packed.repeated_int32(0));
  859. }
  860. TEST(Lite, AllLite47) {
  861. protobuf_unittest::NonPackedFixed32 non_packed;
  862. non_packed.add_repeated_fixed32(42);
  863. string serialized;
  864. ASSERT_TRUE(non_packed.SerializeToString(&serialized));
  865. protobuf_unittest::PackedFixed32 packed;
  866. ASSERT_TRUE(packed.ParseFromString(serialized));
  867. ASSERT_EQ(1, packed.repeated_fixed32_size());
  868. EXPECT_EQ(42, packed.repeated_fixed32(0));
  869. }