no_field_presence_test.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <string>
  31. #include <google/protobuf/unittest_no_field_presence.pb.h>
  32. #include <google/protobuf/unittest.pb.h>
  33. #include <google/protobuf/descriptor.pb.h>
  34. #include <google/protobuf/descriptor.h>
  35. #include <gtest/gtest.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace {
  39. // Helper: checks that all fields have default (zero/empty) values.
  40. void CheckDefaultValues(
  41. const proto2_nofieldpresence_unittest::TestAllTypes& m) {
  42. EXPECT_EQ(0, m.optional_int32());
  43. EXPECT_EQ(0, m.optional_int64());
  44. EXPECT_EQ(0, m.optional_uint32());
  45. EXPECT_EQ(0, m.optional_uint64());
  46. EXPECT_EQ(0, m.optional_sint32());
  47. EXPECT_EQ(0, m.optional_sint64());
  48. EXPECT_EQ(0, m.optional_fixed32());
  49. EXPECT_EQ(0, m.optional_fixed64());
  50. EXPECT_EQ(0, m.optional_sfixed32());
  51. EXPECT_EQ(0, m.optional_sfixed64());
  52. EXPECT_EQ(0, m.optional_float());
  53. EXPECT_EQ(0, m.optional_double());
  54. EXPECT_EQ(false, m.optional_bool());
  55. EXPECT_EQ(0, m.optional_string().size());
  56. EXPECT_EQ(0, m.optional_bytes().size());
  57. EXPECT_EQ(false, m.has_optional_nested_message());
  58. // accessor for message fields returns default instance when not present
  59. EXPECT_EQ(0, m.optional_nested_message().bb());
  60. EXPECT_EQ(false, m.has_optional_proto2_message());
  61. // Embedded proto2 messages still have proto2 semantics, e.g. non-zero default
  62. // values. Here the submessage is not present but its accessor returns the
  63. // default instance.
  64. EXPECT_EQ(41, m.optional_proto2_message().default_int32());
  65. EXPECT_EQ(false, m.has_optional_foreign_message());
  66. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO,
  67. m.optional_nested_enum());
  68. EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_FOO,
  69. m.optional_foreign_enum());
  70. EXPECT_EQ(0, m.repeated_int32_size());
  71. EXPECT_EQ(0, m.repeated_int64_size());
  72. EXPECT_EQ(0, m.repeated_uint32_size());
  73. EXPECT_EQ(0, m.repeated_uint64_size());
  74. EXPECT_EQ(0, m.repeated_sint32_size());
  75. EXPECT_EQ(0, m.repeated_sint64_size());
  76. EXPECT_EQ(0, m.repeated_fixed32_size());
  77. EXPECT_EQ(0, m.repeated_fixed64_size());
  78. EXPECT_EQ(0, m.repeated_sfixed32_size());
  79. EXPECT_EQ(0, m.repeated_sfixed64_size());
  80. EXPECT_EQ(0, m.repeated_float_size());
  81. EXPECT_EQ(0, m.repeated_double_size());
  82. EXPECT_EQ(0, m.repeated_bool_size());
  83. EXPECT_EQ(0, m.repeated_string_size());
  84. EXPECT_EQ(0, m.repeated_bytes_size());
  85. EXPECT_EQ(0, m.repeated_nested_message_size());
  86. EXPECT_EQ(0, m.repeated_foreign_message_size());
  87. EXPECT_EQ(0, m.repeated_proto2_message_size());
  88. EXPECT_EQ(0, m.repeated_nested_enum_size());
  89. EXPECT_EQ(0, m.repeated_foreign_enum_size());
  90. EXPECT_EQ(0, m.repeated_lazy_message_size());
  91. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::ONEOF_FIELD_NOT_SET,
  92. m.oneof_field_case());
  93. }
  94. void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) {
  95. m->set_optional_int32(100);
  96. m->set_optional_int64(101);
  97. m->set_optional_uint32(102);
  98. m->set_optional_uint64(103);
  99. m->set_optional_sint32(104);
  100. m->set_optional_sint64(105);
  101. m->set_optional_fixed32(106);
  102. m->set_optional_fixed64(107);
  103. m->set_optional_sfixed32(108);
  104. m->set_optional_sfixed64(109);
  105. m->set_optional_float(110.0);
  106. m->set_optional_double(111.0);
  107. m->set_optional_bool(true);
  108. m->set_optional_string("asdf");
  109. m->set_optional_bytes("jkl;");
  110. m->mutable_optional_nested_message()->set_bb(42);
  111. m->mutable_optional_foreign_message()->set_c(43);
  112. m->mutable_optional_proto2_message()->set_optional_int32(44);
  113. m->set_optional_nested_enum(
  114. proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ);
  115. m->set_optional_foreign_enum(
  116. proto2_nofieldpresence_unittest::FOREIGN_BAZ);
  117. m->mutable_optional_lazy_message()->set_bb(45);
  118. m->add_repeated_int32(100);
  119. m->add_repeated_int64(101);
  120. m->add_repeated_uint32(102);
  121. m->add_repeated_uint64(103);
  122. m->add_repeated_sint32(104);
  123. m->add_repeated_sint64(105);
  124. m->add_repeated_fixed32(106);
  125. m->add_repeated_fixed64(107);
  126. m->add_repeated_sfixed32(108);
  127. m->add_repeated_sfixed64(109);
  128. m->add_repeated_float(110.0);
  129. m->add_repeated_double(111.0);
  130. m->add_repeated_bool(true);
  131. m->add_repeated_string("asdf");
  132. m->add_repeated_bytes("jkl;");
  133. m->add_repeated_nested_message()->set_bb(46);
  134. m->add_repeated_foreign_message()->set_c(47);
  135. m->add_repeated_proto2_message()->set_optional_int32(48);
  136. m->add_repeated_nested_enum(
  137. proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ);
  138. m->add_repeated_foreign_enum(
  139. proto2_nofieldpresence_unittest::FOREIGN_BAZ);
  140. m->add_repeated_lazy_message()->set_bb(49);
  141. m->set_oneof_uint32(1);
  142. m->mutable_oneof_nested_message()->set_bb(50);
  143. m->set_oneof_string("test"); // only this one remains set
  144. }
  145. void CheckNonDefaultValues(
  146. const proto2_nofieldpresence_unittest::TestAllTypes& m) {
  147. EXPECT_EQ(100, m.optional_int32());
  148. EXPECT_EQ(101, m.optional_int64());
  149. EXPECT_EQ(102, m.optional_uint32());
  150. EXPECT_EQ(103, m.optional_uint64());
  151. EXPECT_EQ(104, m.optional_sint32());
  152. EXPECT_EQ(105, m.optional_sint64());
  153. EXPECT_EQ(106, m.optional_fixed32());
  154. EXPECT_EQ(107, m.optional_fixed64());
  155. EXPECT_EQ(108, m.optional_sfixed32());
  156. EXPECT_EQ(109, m.optional_sfixed64());
  157. EXPECT_EQ(110.0, m.optional_float());
  158. EXPECT_EQ(111.0, m.optional_double());
  159. EXPECT_EQ(true, m.optional_bool());
  160. EXPECT_EQ("asdf", m.optional_string());
  161. EXPECT_EQ("jkl;", m.optional_bytes());
  162. EXPECT_EQ(true, m.has_optional_nested_message());
  163. EXPECT_EQ(42, m.optional_nested_message().bb());
  164. EXPECT_EQ(true, m.has_optional_foreign_message());
  165. EXPECT_EQ(43, m.optional_foreign_message().c());
  166. EXPECT_EQ(true, m.has_optional_proto2_message());
  167. EXPECT_EQ(44, m.optional_proto2_message().optional_int32());
  168. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
  169. m.optional_nested_enum());
  170. EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_BAZ,
  171. m.optional_foreign_enum());
  172. EXPECT_EQ(true, m.has_optional_lazy_message());
  173. EXPECT_EQ(45, m.optional_lazy_message().bb());
  174. EXPECT_EQ(1, m.repeated_int32_size());
  175. EXPECT_EQ(100, m.repeated_int32(0));
  176. EXPECT_EQ(1, m.repeated_int64_size());
  177. EXPECT_EQ(101, m.repeated_int64(0));
  178. EXPECT_EQ(1, m.repeated_uint32_size());
  179. EXPECT_EQ(102, m.repeated_uint32(0));
  180. EXPECT_EQ(1, m.repeated_uint64_size());
  181. EXPECT_EQ(103, m.repeated_uint64(0));
  182. EXPECT_EQ(1, m.repeated_sint32_size());
  183. EXPECT_EQ(104, m.repeated_sint32(0));
  184. EXPECT_EQ(1, m.repeated_sint64_size());
  185. EXPECT_EQ(105, m.repeated_sint64(0));
  186. EXPECT_EQ(1, m.repeated_fixed32_size());
  187. EXPECT_EQ(106, m.repeated_fixed32(0));
  188. EXPECT_EQ(1, m.repeated_fixed64_size());
  189. EXPECT_EQ(107, m.repeated_fixed64(0));
  190. EXPECT_EQ(1, m.repeated_sfixed32_size());
  191. EXPECT_EQ(108, m.repeated_sfixed32(0));
  192. EXPECT_EQ(1, m.repeated_sfixed64_size());
  193. EXPECT_EQ(109, m.repeated_sfixed64(0));
  194. EXPECT_EQ(1, m.repeated_float_size());
  195. EXPECT_EQ(110.0, m.repeated_float(0));
  196. EXPECT_EQ(1, m.repeated_double_size());
  197. EXPECT_EQ(111.0, m.repeated_double(0));
  198. EXPECT_EQ(1, m.repeated_bool_size());
  199. EXPECT_EQ(true, m.repeated_bool(0));
  200. EXPECT_EQ(1, m.repeated_string_size());
  201. EXPECT_EQ("asdf", m.repeated_string(0));
  202. EXPECT_EQ(1, m.repeated_bytes_size());
  203. EXPECT_EQ("jkl;", m.repeated_bytes(0));
  204. EXPECT_EQ(1, m.repeated_nested_message_size());
  205. EXPECT_EQ(46, m.repeated_nested_message(0).bb());
  206. EXPECT_EQ(1, m.repeated_foreign_message_size());
  207. EXPECT_EQ(47, m.repeated_foreign_message(0).c());
  208. EXPECT_EQ(1, m.repeated_proto2_message_size());
  209. EXPECT_EQ(48, m.repeated_proto2_message(0).optional_int32());
  210. EXPECT_EQ(1, m.repeated_nested_enum_size());
  211. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
  212. m.repeated_nested_enum(0));
  213. EXPECT_EQ(1, m.repeated_foreign_enum_size());
  214. EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_BAZ,
  215. m.repeated_foreign_enum(0));
  216. EXPECT_EQ(1, m.repeated_lazy_message_size());
  217. EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
  218. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
  219. m.oneof_field_case());
  220. EXPECT_EQ("test", m.oneof_string());
  221. }
  222. TEST(NoFieldPresenceTest, BasicMessageTest) {
  223. proto2_nofieldpresence_unittest::TestAllTypes message;
  224. // Check default values, fill all fields, check values. We just want to
  225. // exercise the basic getters/setter paths here to make sure no
  226. // field-presence-related changes broke these.
  227. CheckDefaultValues(message);
  228. FillValues(&message);
  229. CheckNonDefaultValues(message);
  230. // Clear() should be equivalent to getting a freshly-constructed message.
  231. message.Clear();
  232. CheckDefaultValues(message);
  233. }
  234. TEST(NoFieldPresenceTest, MessageFieldPresenceTest) {
  235. // check that presence still works properly for message fields.
  236. proto2_nofieldpresence_unittest::TestAllTypes message;
  237. EXPECT_EQ(false, message.has_optional_nested_message());
  238. // Getter should fetch default instance, and not cause the field to become
  239. // present.
  240. EXPECT_EQ(0, message.optional_nested_message().bb());
  241. EXPECT_EQ(false, message.has_optional_nested_message());
  242. message.mutable_optional_nested_message()->set_bb(42);
  243. EXPECT_EQ(true, message.has_optional_nested_message());
  244. message.clear_optional_nested_message();
  245. EXPECT_EQ(false, message.has_optional_nested_message());
  246. // Likewise for a lazy message field.
  247. EXPECT_EQ(false, message.has_optional_lazy_message());
  248. // Getter should fetch default instance, and not cause the field to become
  249. // present.
  250. EXPECT_EQ(0, message.optional_lazy_message().bb());
  251. EXPECT_EQ(false, message.has_optional_lazy_message());
  252. message.mutable_optional_lazy_message()->set_bb(42);
  253. EXPECT_EQ(true, message.has_optional_lazy_message());
  254. message.clear_optional_lazy_message();
  255. EXPECT_EQ(false, message.has_optional_lazy_message());
  256. // Test field presence of a message field on the default instance.
  257. EXPECT_EQ(false, proto2_nofieldpresence_unittest::TestAllTypes::
  258. default_instance().has_optional_nested_message());
  259. }
  260. TEST(NoFieldPresenceTest, ReflectionHasFieldTest) {
  261. // check that HasField reports true on all scalar fields. Check that it
  262. // behaves properly for message fields.
  263. proto2_nofieldpresence_unittest::TestAllTypes message;
  264. const google::protobuf::Reflection* r = message.GetReflection();
  265. const google::protobuf::Descriptor* desc = message.GetDescriptor();
  266. // Check initial state: scalars not present (due to need to be consistent with
  267. // MergeFrom()), message fields not present, oneofs not present.
  268. for (int i = 0; i < desc->field_count(); i++) {
  269. const google::protobuf::FieldDescriptor* field = desc->field(i);
  270. if (field->is_repeated()) continue;
  271. EXPECT_EQ(false, r->HasField(message, field));
  272. }
  273. // Test field presence of a message field on the default instance.
  274. const google::protobuf::FieldDescriptor* msg_field =
  275. desc->FindFieldByName("optional_nested_message");
  276. EXPECT_EQ(false, r->HasField(
  277. proto2_nofieldpresence_unittest::TestAllTypes::
  278. default_instance(), msg_field));
  279. // Fill all fields, expect everything to report true (check oneofs below).
  280. FillValues(&message);
  281. for (int i = 0; i < desc->field_count(); i++) {
  282. const google::protobuf::FieldDescriptor* field = desc->field(i);
  283. if (field->is_repeated() || field->containing_oneof()) {
  284. continue;
  285. }
  286. if (field->options().ctype() != google::protobuf::FieldOptions::STRING) {
  287. continue;
  288. }
  289. EXPECT_EQ(true, r->HasField(message, field));
  290. }
  291. message.Clear();
  292. // Check zero/empty-means-not-present semantics.
  293. const google::protobuf::FieldDescriptor* field_int32 = desc->FindFieldByName(
  294. "optional_int32");
  295. const google::protobuf::FieldDescriptor* field_double = desc->FindFieldByName(
  296. "optional_double");
  297. const google::protobuf::FieldDescriptor* field_string = desc->FindFieldByName(
  298. "optional_string");
  299. EXPECT_EQ(false, r->HasField(message, field_int32));
  300. EXPECT_EQ(false, r->HasField(message, field_double));
  301. EXPECT_EQ(false, r->HasField(message, field_string));
  302. message.set_optional_int32(42);
  303. EXPECT_EQ(true, r->HasField(message, field_int32));
  304. message.set_optional_int32(0);
  305. EXPECT_EQ(false, r->HasField(message, field_int32));
  306. message.set_optional_double(42.0);
  307. EXPECT_EQ(true, r->HasField(message, field_double));
  308. message.set_optional_double(0.0);
  309. EXPECT_EQ(false, r->HasField(message, field_double));
  310. message.set_optional_string("test");
  311. EXPECT_EQ(true, r->HasField(message, field_string));
  312. message.set_optional_string("");
  313. EXPECT_EQ(false, r->HasField(message, field_string));
  314. }
  315. TEST(NoFieldPresenceTest, ReflectionClearFieldTest) {
  316. proto2_nofieldpresence_unittest::TestAllTypes message;
  317. const google::protobuf::Reflection* r = message.GetReflection();
  318. const google::protobuf::Descriptor* desc = message.GetDescriptor();
  319. const google::protobuf::FieldDescriptor* field_int32 = desc->FindFieldByName(
  320. "optional_int32");
  321. const google::protobuf::FieldDescriptor* field_double = desc->FindFieldByName(
  322. "optional_double");
  323. const google::protobuf::FieldDescriptor* field_string = desc->FindFieldByName(
  324. "optional_string");
  325. const google::protobuf::FieldDescriptor* field_message = desc->FindFieldByName(
  326. "optional_nested_message");
  327. const google::protobuf::FieldDescriptor* field_lazy = desc->FindFieldByName(
  328. "optional_lazy_message");
  329. message.set_optional_int32(42);
  330. r->ClearField(&message, field_int32);
  331. EXPECT_EQ(0, message.optional_int32());
  332. message.set_optional_double(42.0);
  333. r->ClearField(&message, field_double);
  334. EXPECT_EQ(0.0, message.optional_double());
  335. message.set_optional_string("test");
  336. r->ClearField(&message, field_string);
  337. EXPECT_EQ("", message.optional_string());
  338. message.mutable_optional_nested_message()->set_bb(1234);
  339. r->ClearField(&message, field_message);
  340. EXPECT_FALSE(message.has_optional_nested_message());
  341. EXPECT_EQ(0, message.optional_nested_message().bb());
  342. message.mutable_optional_lazy_message()->set_bb(42);
  343. r->ClearField(&message, field_lazy);
  344. EXPECT_FALSE(message.has_optional_lazy_message());
  345. EXPECT_EQ(0, message.optional_lazy_message().bb());
  346. }
  347. TEST(NoFieldPresenceTest, HasFieldOneofsTest) {
  348. // check that HasField behaves properly for oneofs.
  349. proto2_nofieldpresence_unittest::TestAllTypes message;
  350. const google::protobuf::Reflection* r = message.GetReflection();
  351. const google::protobuf::Descriptor* desc = message.GetDescriptor();
  352. const google::protobuf::FieldDescriptor* desc_oneof_uint32 =
  353. desc->FindFieldByName("oneof_uint32");
  354. const google::protobuf::FieldDescriptor* desc_oneof_nested_message =
  355. desc->FindFieldByName("oneof_nested_message");
  356. const google::protobuf::FieldDescriptor* desc_oneof_string =
  357. desc->FindFieldByName("oneof_string");
  358. GOOGLE_CHECK_NOTNULL(desc_oneof_uint32);
  359. GOOGLE_CHECK_NOTNULL(desc_oneof_nested_message);
  360. GOOGLE_CHECK_NOTNULL(desc_oneof_string);
  361. EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
  362. EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
  363. EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
  364. message.set_oneof_string("test");
  365. EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
  366. EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
  367. EXPECT_EQ(true, r->HasField(message, desc_oneof_string));
  368. message.mutable_oneof_nested_message()->set_bb(42);
  369. EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
  370. EXPECT_EQ(true, r->HasField(message, desc_oneof_nested_message));
  371. EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
  372. message.Clear();
  373. EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
  374. EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
  375. EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
  376. }
  377. TEST(NoFieldPresenceTest, DontSerializeDefaultValuesTest) {
  378. // check that serialized data contains only non-zero numeric fields/non-empty
  379. // string/byte fields.
  380. proto2_nofieldpresence_unittest::TestAllTypes message;
  381. string output;
  382. // All default values -> no output.
  383. message.SerializeToString(&output);
  384. EXPECT_EQ(0, output.size());
  385. // Zero values -> still no output.
  386. message.set_optional_int32(0);
  387. message.set_optional_int64(0);
  388. message.set_optional_uint32(0);
  389. message.set_optional_uint64(0);
  390. message.set_optional_sint32(0);
  391. message.set_optional_sint64(0);
  392. message.set_optional_fixed32(0);
  393. message.set_optional_fixed64(0);
  394. message.set_optional_sfixed32(0);
  395. message.set_optional_sfixed64(0);
  396. message.set_optional_float(0);
  397. message.set_optional_double(0);
  398. message.set_optional_bool(0);
  399. message.set_optional_string("");
  400. message.set_optional_bytes("");
  401. message.set_optional_nested_enum(
  402. proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO); // first enum entry
  403. message.set_optional_foreign_enum(
  404. proto2_nofieldpresence_unittest::FOREIGN_FOO); // first enum entry
  405. message.SerializeToString(&output);
  406. EXPECT_EQ(0, output.size());
  407. message.set_optional_int32(1);
  408. message.SerializeToString(&output);
  409. EXPECT_EQ(2, output.size());
  410. EXPECT_EQ("\x08\x01", output);
  411. message.set_optional_int32(0);
  412. message.SerializeToString(&output);
  413. EXPECT_EQ(0, output.size());
  414. }
  415. TEST(NoFieldPresenceTest, MergeFromIfNonzeroTest) {
  416. // check that MergeFrom copies if nonzero/nondefault only.
  417. proto2_nofieldpresence_unittest::TestAllTypes source;
  418. proto2_nofieldpresence_unittest::TestAllTypes dest;
  419. dest.set_optional_int32(42);
  420. dest.set_optional_string("test");
  421. source.set_optional_int32(0);
  422. source.set_optional_string("");
  423. // MergeFrom() copies only if present in serialization, i.e., non-zero.
  424. dest.MergeFrom(source);
  425. EXPECT_EQ(42, dest.optional_int32());
  426. EXPECT_EQ("test", dest.optional_string());
  427. source.set_optional_int32(84);
  428. source.set_optional_string("test2");
  429. dest.MergeFrom(source);
  430. EXPECT_EQ(84, dest.optional_int32());
  431. EXPECT_EQ("test2", dest.optional_string());
  432. }
  433. TEST(NoFieldPresenceTest, IsInitializedTest) {
  434. // Check that IsInitialized works properly.
  435. proto2_nofieldpresence_unittest::TestProto2Required message;
  436. EXPECT_EQ(true, message.IsInitialized());
  437. message.mutable_proto2()->set_a(1);
  438. EXPECT_EQ(false, message.IsInitialized());
  439. message.mutable_proto2()->set_b(1);
  440. EXPECT_EQ(false, message.IsInitialized());
  441. message.mutable_proto2()->set_c(1);
  442. EXPECT_EQ(true, message.IsInitialized());
  443. }
  444. TEST(NoFieldPresenceTest, LazyMessageFieldHasBit) {
  445. // Check that has-bit interaction with lazy message works (has-bit before and
  446. // after lazy decode).
  447. proto2_nofieldpresence_unittest::TestAllTypes message;
  448. const google::protobuf::Reflection* r = message.GetReflection();
  449. const google::protobuf::Descriptor* desc = message.GetDescriptor();
  450. const google::protobuf::FieldDescriptor* field = desc->FindFieldByName(
  451. "optional_lazy_message");
  452. GOOGLE_CHECK_NOTNULL(field);
  453. EXPECT_EQ(false, message.has_optional_lazy_message());
  454. EXPECT_EQ(false, r->HasField(message, field));
  455. message.mutable_optional_lazy_message()->set_bb(42);
  456. EXPECT_EQ(true, message.has_optional_lazy_message());
  457. EXPECT_EQ(true, r->HasField(message, field));
  458. // Serialize and parse with a new message object so that lazy field on new
  459. // object is in unparsed state.
  460. string output;
  461. message.SerializeToString(&output);
  462. proto2_nofieldpresence_unittest::TestAllTypes message2;
  463. message2.ParseFromString(output);
  464. EXPECT_EQ(true, message2.has_optional_lazy_message());
  465. EXPECT_EQ(true, r->HasField(message2, field));
  466. // Access field to force lazy parse.
  467. EXPECT_EQ(42, message.optional_lazy_message().bb());
  468. EXPECT_EQ(true, message2.has_optional_lazy_message());
  469. EXPECT_EQ(true, r->HasField(message2, field));
  470. }
  471. TEST(NoFieldPresenceTest, OneofPresence) {
  472. proto2_nofieldpresence_unittest::TestAllTypes message;
  473. // oneof fields still have field presence -- ensure that this goes on the wire
  474. // even though its value is the empty string.
  475. message.set_oneof_string("");
  476. string serialized;
  477. message.SerializeToString(&serialized);
  478. // Tag: 113 --> tag is (113 << 3) | 2 (length delimited) = 906
  479. // varint: 0x8a 0x07
  480. // Length: 0x00
  481. EXPECT_EQ(3, serialized.size());
  482. EXPECT_EQ(static_cast<char>(0x8a), serialized.at(0));
  483. EXPECT_EQ(static_cast<char>(0x07), serialized.at(1));
  484. EXPECT_EQ(static_cast<char>(0x00), serialized.at(2));
  485. message.Clear();
  486. EXPECT_TRUE(message.ParseFromString(serialized));
  487. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
  488. message.oneof_field_case());
  489. // Also test int32 and enum fields.
  490. message.Clear();
  491. message.set_oneof_uint32(0); // would not go on wire if ordinary field.
  492. message.SerializeToString(&serialized);
  493. EXPECT_EQ(3, serialized.size());
  494. EXPECT_TRUE(message.ParseFromString(serialized));
  495. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofUint32,
  496. message.oneof_field_case());
  497. message.Clear();
  498. message.set_oneof_enum(proto2_nofieldpresence_unittest::
  499. TestAllTypes_NestedEnum_FOO); // default value.
  500. message.SerializeToString(&serialized);
  501. EXPECT_EQ(3, serialized.size());
  502. EXPECT_TRUE(message.ParseFromString(serialized));
  503. EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofEnum,
  504. message.oneof_field_case());
  505. message.Clear();
  506. message.set_oneof_string("test");
  507. message.clear_oneof_string();
  508. EXPECT_EQ(0, message.ByteSize());
  509. }
  510. } // namespace
  511. } // namespace protobuf
  512. } // namespace google