• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 #include <string>
32 
33 #include <google/protobuf/unittest.pb.h>
34 #include <google/protobuf/unittest_no_field_presence.pb.h>
35 #include <google/protobuf/descriptor.pb.h>
36 #include <google/protobuf/descriptor.h>
37 #include <gtest/gtest.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace {
42 
43 // Helper: checks that all fields have default (zero/empty) values.
CheckDefaultValues(const proto2_nofieldpresence_unittest::TestAllTypes & m)44 void CheckDefaultValues(
45     const proto2_nofieldpresence_unittest::TestAllTypes& m) {
46   EXPECT_EQ(0, m.optional_int32());
47   EXPECT_EQ(0, m.optional_int64());
48   EXPECT_EQ(0, m.optional_uint32());
49   EXPECT_EQ(0, m.optional_uint64());
50   EXPECT_EQ(0, m.optional_sint32());
51   EXPECT_EQ(0, m.optional_sint64());
52   EXPECT_EQ(0, m.optional_fixed32());
53   EXPECT_EQ(0, m.optional_fixed64());
54   EXPECT_EQ(0, m.optional_sfixed32());
55   EXPECT_EQ(0, m.optional_sfixed64());
56   EXPECT_EQ(0, m.optional_float());
57   EXPECT_EQ(0, m.optional_double());
58   EXPECT_EQ(false, m.optional_bool());
59   EXPECT_EQ(0, m.optional_string().size());
60   EXPECT_EQ(0, m.optional_bytes().size());
61 
62   EXPECT_EQ(false, m.has_optional_nested_message());
63   // accessor for message fields returns default instance when not present
64   EXPECT_EQ(0, m.optional_nested_message().bb());
65   EXPECT_EQ(false, m.has_optional_proto2_message());
66   // Embedded proto2 messages still have proto2 semantics, e.g. non-zero default
67   // values. Here the submessage is not present but its accessor returns the
68   // default instance.
69   EXPECT_EQ(41, m.optional_proto2_message().default_int32());
70   EXPECT_EQ(false, m.has_optional_foreign_message());
71   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO,
72             m.optional_nested_enum());
73   EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_FOO,
74             m.optional_foreign_enum());
75 
76 
77   EXPECT_EQ(0, m.repeated_int32_size());
78   EXPECT_EQ(0, m.repeated_int64_size());
79   EXPECT_EQ(0, m.repeated_uint32_size());
80   EXPECT_EQ(0, m.repeated_uint64_size());
81   EXPECT_EQ(0, m.repeated_sint32_size());
82   EXPECT_EQ(0, m.repeated_sint64_size());
83   EXPECT_EQ(0, m.repeated_fixed32_size());
84   EXPECT_EQ(0, m.repeated_fixed64_size());
85   EXPECT_EQ(0, m.repeated_sfixed32_size());
86   EXPECT_EQ(0, m.repeated_sfixed64_size());
87   EXPECT_EQ(0, m.repeated_float_size());
88   EXPECT_EQ(0, m.repeated_double_size());
89   EXPECT_EQ(0, m.repeated_bool_size());
90   EXPECT_EQ(0, m.repeated_string_size());
91   EXPECT_EQ(0, m.repeated_bytes_size());
92   EXPECT_EQ(0, m.repeated_nested_message_size());
93   EXPECT_EQ(0, m.repeated_foreign_message_size());
94   EXPECT_EQ(0, m.repeated_proto2_message_size());
95   EXPECT_EQ(0, m.repeated_nested_enum_size());
96   EXPECT_EQ(0, m.repeated_foreign_enum_size());
97   EXPECT_EQ(0, m.repeated_lazy_message_size());
98   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::ONEOF_FIELD_NOT_SET,
99             m.oneof_field_case());
100 }
101 
FillValues(proto2_nofieldpresence_unittest::TestAllTypes * m)102 void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) {
103   m->set_optional_int32(100);
104   m->set_optional_int64(101);
105   m->set_optional_uint32(102);
106   m->set_optional_uint64(103);
107   m->set_optional_sint32(104);
108   m->set_optional_sint64(105);
109   m->set_optional_fixed32(106);
110   m->set_optional_fixed64(107);
111   m->set_optional_sfixed32(108);
112   m->set_optional_sfixed64(109);
113   m->set_optional_float(110.0);
114   m->set_optional_double(111.0);
115   m->set_optional_bool(true);
116   m->set_optional_string("asdf");
117   m->set_optional_bytes("jkl;");
118   m->mutable_optional_nested_message()->set_bb(42);
119   m->mutable_optional_foreign_message()->set_c(43);
120   m->mutable_optional_proto2_message()->set_optional_int32(44);
121   m->set_optional_nested_enum(
122       proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ);
123   m->set_optional_foreign_enum(proto2_nofieldpresence_unittest::FOREIGN_BAZ);
124   m->mutable_optional_lazy_message()->set_bb(45);
125   m->add_repeated_int32(100);
126   m->add_repeated_int64(101);
127   m->add_repeated_uint32(102);
128   m->add_repeated_uint64(103);
129   m->add_repeated_sint32(104);
130   m->add_repeated_sint64(105);
131   m->add_repeated_fixed32(106);
132   m->add_repeated_fixed64(107);
133   m->add_repeated_sfixed32(108);
134   m->add_repeated_sfixed64(109);
135   m->add_repeated_float(110.0);
136   m->add_repeated_double(111.0);
137   m->add_repeated_bool(true);
138   m->add_repeated_string("asdf");
139   m->add_repeated_bytes("jkl;");
140   m->add_repeated_nested_message()->set_bb(46);
141   m->add_repeated_foreign_message()->set_c(47);
142   m->add_repeated_proto2_message()->set_optional_int32(48);
143   m->add_repeated_nested_enum(
144       proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ);
145   m->add_repeated_foreign_enum(proto2_nofieldpresence_unittest::FOREIGN_BAZ);
146   m->add_repeated_lazy_message()->set_bb(49);
147 
148   m->set_oneof_uint32(1);
149   m->mutable_oneof_nested_message()->set_bb(50);
150   m->set_oneof_string("test");  // only this one remains set
151 }
152 
CheckNonDefaultValues(const proto2_nofieldpresence_unittest::TestAllTypes & m)153 void CheckNonDefaultValues(
154     const proto2_nofieldpresence_unittest::TestAllTypes& m) {
155   EXPECT_EQ(100, m.optional_int32());
156   EXPECT_EQ(101, m.optional_int64());
157   EXPECT_EQ(102, m.optional_uint32());
158   EXPECT_EQ(103, m.optional_uint64());
159   EXPECT_EQ(104, m.optional_sint32());
160   EXPECT_EQ(105, m.optional_sint64());
161   EXPECT_EQ(106, m.optional_fixed32());
162   EXPECT_EQ(107, m.optional_fixed64());
163   EXPECT_EQ(108, m.optional_sfixed32());
164   EXPECT_EQ(109, m.optional_sfixed64());
165   EXPECT_EQ(110.0, m.optional_float());
166   EXPECT_EQ(111.0, m.optional_double());
167   EXPECT_EQ(true, m.optional_bool());
168   EXPECT_EQ("asdf", m.optional_string());
169   EXPECT_EQ("jkl;", m.optional_bytes());
170   EXPECT_EQ(true, m.has_optional_nested_message());
171   EXPECT_EQ(42, m.optional_nested_message().bb());
172   EXPECT_EQ(true, m.has_optional_foreign_message());
173   EXPECT_EQ(43, m.optional_foreign_message().c());
174   EXPECT_EQ(true, m.has_optional_proto2_message());
175   EXPECT_EQ(44, m.optional_proto2_message().optional_int32());
176   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
177             m.optional_nested_enum());
178   EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_BAZ,
179             m.optional_foreign_enum());
180   EXPECT_EQ(true, m.has_optional_lazy_message());
181   EXPECT_EQ(45, m.optional_lazy_message().bb());
182 
183   EXPECT_EQ(1, m.repeated_int32_size());
184   EXPECT_EQ(100, m.repeated_int32(0));
185   EXPECT_EQ(1, m.repeated_int64_size());
186   EXPECT_EQ(101, m.repeated_int64(0));
187   EXPECT_EQ(1, m.repeated_uint32_size());
188   EXPECT_EQ(102, m.repeated_uint32(0));
189   EXPECT_EQ(1, m.repeated_uint64_size());
190   EXPECT_EQ(103, m.repeated_uint64(0));
191   EXPECT_EQ(1, m.repeated_sint32_size());
192   EXPECT_EQ(104, m.repeated_sint32(0));
193   EXPECT_EQ(1, m.repeated_sint64_size());
194   EXPECT_EQ(105, m.repeated_sint64(0));
195   EXPECT_EQ(1, m.repeated_fixed32_size());
196   EXPECT_EQ(106, m.repeated_fixed32(0));
197   EXPECT_EQ(1, m.repeated_fixed64_size());
198   EXPECT_EQ(107, m.repeated_fixed64(0));
199   EXPECT_EQ(1, m.repeated_sfixed32_size());
200   EXPECT_EQ(108, m.repeated_sfixed32(0));
201   EXPECT_EQ(1, m.repeated_sfixed64_size());
202   EXPECT_EQ(109, m.repeated_sfixed64(0));
203   EXPECT_EQ(1, m.repeated_float_size());
204   EXPECT_EQ(110.0, m.repeated_float(0));
205   EXPECT_EQ(1, m.repeated_double_size());
206   EXPECT_EQ(111.0, m.repeated_double(0));
207   EXPECT_EQ(1, m.repeated_bool_size());
208   EXPECT_EQ(true, m.repeated_bool(0));
209   EXPECT_EQ(1, m.repeated_string_size());
210   EXPECT_EQ("asdf", m.repeated_string(0));
211   EXPECT_EQ(1, m.repeated_bytes_size());
212   EXPECT_EQ("jkl;", m.repeated_bytes(0));
213   EXPECT_EQ(1, m.repeated_nested_message_size());
214   EXPECT_EQ(46, m.repeated_nested_message(0).bb());
215   EXPECT_EQ(1, m.repeated_foreign_message_size());
216   EXPECT_EQ(47, m.repeated_foreign_message(0).c());
217   EXPECT_EQ(1, m.repeated_proto2_message_size());
218   EXPECT_EQ(48, m.repeated_proto2_message(0).optional_int32());
219   EXPECT_EQ(1, m.repeated_nested_enum_size());
220   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
221             m.repeated_nested_enum(0));
222   EXPECT_EQ(1, m.repeated_foreign_enum_size());
223   EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_BAZ,
224             m.repeated_foreign_enum(0));
225   EXPECT_EQ(1, m.repeated_lazy_message_size());
226   EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
227 
228   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
229             m.oneof_field_case());
230   EXPECT_EQ("test", m.oneof_string());
231 }
232 
TEST(NoFieldPresenceTest,BasicMessageTest)233 TEST(NoFieldPresenceTest, BasicMessageTest) {
234   proto2_nofieldpresence_unittest::TestAllTypes message;
235   // Check default values, fill all fields, check values. We just want to
236   // exercise the basic getters/setter paths here to make sure no
237   // field-presence-related changes broke these.
238   CheckDefaultValues(message);
239   FillValues(&message);
240   CheckNonDefaultValues(message);
241 
242   // Clear() should be equivalent to getting a freshly-constructed message.
243   message.Clear();
244   CheckDefaultValues(message);
245 }
246 
TEST(NoFieldPresenceTest,MessageFieldPresenceTest)247 TEST(NoFieldPresenceTest, MessageFieldPresenceTest) {
248   // check that presence still works properly for message fields.
249   proto2_nofieldpresence_unittest::TestAllTypes message;
250   EXPECT_EQ(false, message.has_optional_nested_message());
251   // Getter should fetch default instance, and not cause the field to become
252   // present.
253   EXPECT_EQ(0, message.optional_nested_message().bb());
254   EXPECT_EQ(false, message.has_optional_nested_message());
255   message.mutable_optional_nested_message()->set_bb(42);
256   EXPECT_EQ(true, message.has_optional_nested_message());
257   message.clear_optional_nested_message();
258   EXPECT_EQ(false, message.has_optional_nested_message());
259 
260   // Likewise for a lazy message field.
261   EXPECT_EQ(false, message.has_optional_lazy_message());
262   // Getter should fetch default instance, and not cause the field to become
263   // present.
264   EXPECT_EQ(0, message.optional_lazy_message().bb());
265   EXPECT_EQ(false, message.has_optional_lazy_message());
266   message.mutable_optional_lazy_message()->set_bb(42);
267   EXPECT_EQ(true, message.has_optional_lazy_message());
268   message.clear_optional_lazy_message();
269   EXPECT_EQ(false, message.has_optional_lazy_message());
270 
271   // Test field presence of a message field on the default instance.
272   EXPECT_EQ(false,
273             proto2_nofieldpresence_unittest::TestAllTypes::default_instance()
274                 .has_optional_nested_message());
275 }
276 
TEST(NoFieldPresenceTest,ReflectionHasFieldTest)277 TEST(NoFieldPresenceTest, ReflectionHasFieldTest) {
278   // check that HasField reports true on all scalar fields. Check that it
279   // behaves properly for message fields.
280 
281   proto2_nofieldpresence_unittest::TestAllTypes message;
282   const Reflection* r = message.GetReflection();
283   const Descriptor* desc = message.GetDescriptor();
284 
285   // Check initial state: scalars not present (due to need to be consistent with
286   // MergeFrom()), message fields not present, oneofs not present.
287   for (int i = 0; i < desc->field_count(); i++) {
288     const FieldDescriptor* field = desc->field(i);
289     if (field->is_repeated()) continue;
290     EXPECT_EQ(false, r->HasField(message, field));
291   }
292 
293   // Test field presence of a message field on the default instance.
294   const FieldDescriptor* msg_field =
295       desc->FindFieldByName("optional_nested_message");
296   EXPECT_EQ(
297       false,
298       r->HasField(
299           proto2_nofieldpresence_unittest::TestAllTypes::default_instance(),
300           msg_field));
301 
302   // Fill all fields, expect everything to report true (check oneofs below).
303   FillValues(&message);
304   for (int i = 0; i < desc->field_count(); i++) {
305     const FieldDescriptor* field = desc->field(i);
306     if (field->is_repeated() || field->containing_oneof()) {
307       continue;
308     }
309     if (field->options().ctype() != FieldOptions::STRING) {
310       continue;
311     }
312     EXPECT_EQ(true, r->HasField(message, field));
313   }
314 
315   message.Clear();
316 
317   // Check zero/empty-means-not-present semantics.
318   const FieldDescriptor* field_int32 = desc->FindFieldByName("optional_int32");
319   const FieldDescriptor* field_double =
320       desc->FindFieldByName("optional_double");
321   const FieldDescriptor* field_string =
322       desc->FindFieldByName("optional_string");
323 
324   EXPECT_EQ(false, r->HasField(message, field_int32));
325   EXPECT_EQ(false, r->HasField(message, field_double));
326   EXPECT_EQ(false, r->HasField(message, field_string));
327 
328   message.set_optional_int32(42);
329   EXPECT_EQ(true, r->HasField(message, field_int32));
330   message.set_optional_int32(0);
331   EXPECT_EQ(false, r->HasField(message, field_int32));
332 
333   message.set_optional_double(42.0);
334   EXPECT_EQ(true, r->HasField(message, field_double));
335   message.set_optional_double(0.0);
336   EXPECT_EQ(false, r->HasField(message, field_double));
337 
338   message.set_optional_string("test");
339   EXPECT_EQ(true, r->HasField(message, field_string));
340   message.set_optional_string("");
341   EXPECT_EQ(false, r->HasField(message, field_string));
342 }
343 
TEST(NoFieldPresenceTest,ReflectionClearFieldTest)344 TEST(NoFieldPresenceTest, ReflectionClearFieldTest) {
345   proto2_nofieldpresence_unittest::TestAllTypes message;
346 
347   const Reflection* r = message.GetReflection();
348   const Descriptor* desc = message.GetDescriptor();
349 
350   const FieldDescriptor* field_int32 = desc->FindFieldByName("optional_int32");
351   const FieldDescriptor* field_double =
352       desc->FindFieldByName("optional_double");
353   const FieldDescriptor* field_string =
354       desc->FindFieldByName("optional_string");
355   const FieldDescriptor* field_message =
356       desc->FindFieldByName("optional_nested_message");
357   const FieldDescriptor* field_lazy =
358       desc->FindFieldByName("optional_lazy_message");
359 
360   message.set_optional_int32(42);
361   r->ClearField(&message, field_int32);
362   EXPECT_EQ(0, message.optional_int32());
363 
364   message.set_optional_double(42.0);
365   r->ClearField(&message, field_double);
366   EXPECT_EQ(0.0, message.optional_double());
367 
368   message.set_optional_string("test");
369   r->ClearField(&message, field_string);
370   EXPECT_EQ("", message.optional_string());
371 
372   message.mutable_optional_nested_message()->set_bb(1234);
373   r->ClearField(&message, field_message);
374   EXPECT_FALSE(message.has_optional_nested_message());
375   EXPECT_EQ(0, message.optional_nested_message().bb());
376 
377   message.mutable_optional_lazy_message()->set_bb(42);
378   r->ClearField(&message, field_lazy);
379   EXPECT_FALSE(message.has_optional_lazy_message());
380   EXPECT_EQ(0, message.optional_lazy_message().bb());
381 }
382 
TEST(NoFieldPresenceTest,HasFieldOneofsTest)383 TEST(NoFieldPresenceTest, HasFieldOneofsTest) {
384   // check that HasField behaves properly for oneofs.
385   proto2_nofieldpresence_unittest::TestAllTypes message;
386 
387   const Reflection* r = message.GetReflection();
388   const Descriptor* desc = message.GetDescriptor();
389   const FieldDescriptor* desc_oneof_uint32 =
390       desc->FindFieldByName("oneof_uint32");
391   const FieldDescriptor* desc_oneof_nested_message =
392       desc->FindFieldByName("oneof_nested_message");
393   const FieldDescriptor* desc_oneof_string =
394       desc->FindFieldByName("oneof_string");
395   GOOGLE_CHECK(desc_oneof_uint32 != nullptr);
396   GOOGLE_CHECK(desc_oneof_nested_message != nullptr);
397   GOOGLE_CHECK(desc_oneof_string != nullptr);
398 
399   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
400   EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
401   EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
402 
403   message.set_oneof_string("test");
404   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
405   EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
406   EXPECT_EQ(true, r->HasField(message, desc_oneof_string));
407   message.mutable_oneof_nested_message()->set_bb(42);
408   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
409   EXPECT_EQ(true, r->HasField(message, desc_oneof_nested_message));
410   EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
411 
412   message.Clear();
413   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
414   EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
415   EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
416 }
417 
TEST(NoFieldPresenceTest,DontSerializeDefaultValuesTest)418 TEST(NoFieldPresenceTest, DontSerializeDefaultValuesTest) {
419   // check that serialized data contains only non-zero numeric fields/non-empty
420   // string/byte fields.
421   proto2_nofieldpresence_unittest::TestAllTypes message;
422   std::string output;
423 
424   // All default values -> no output.
425   message.SerializeToString(&output);
426   EXPECT_EQ(0, output.size());
427 
428   // Zero values -> still no output.
429   message.set_optional_int32(0);
430   message.set_optional_int64(0);
431   message.set_optional_uint32(0);
432   message.set_optional_uint64(0);
433   message.set_optional_sint32(0);
434   message.set_optional_sint64(0);
435   message.set_optional_fixed32(0);
436   message.set_optional_fixed64(0);
437   message.set_optional_sfixed32(0);
438   message.set_optional_sfixed64(0);
439   message.set_optional_float(0);
440   message.set_optional_double(0);
441   message.set_optional_bool(0);
442   message.set_optional_string("");
443   message.set_optional_bytes("");
444   message.set_optional_nested_enum(
445       proto2_nofieldpresence_unittest::
446           TestAllTypes_NestedEnum_FOO);  // first enum entry
447   message.set_optional_foreign_enum(
448       proto2_nofieldpresence_unittest::FOREIGN_FOO);  // first enum entry
449 
450   message.SerializeToString(&output);
451   EXPECT_EQ(0, output.size());
452 
453   message.set_optional_int32(1);
454   message.SerializeToString(&output);
455   EXPECT_EQ(2, output.size());
456   EXPECT_EQ("\x08\x01", output);
457 
458   message.set_optional_int32(0);
459   message.SerializeToString(&output);
460   EXPECT_EQ(0, output.size());
461 }
462 
TEST(NoFieldPresenceTest,MergeFromIfNonzeroTest)463 TEST(NoFieldPresenceTest, MergeFromIfNonzeroTest) {
464   // check that MergeFrom copies if nonzero/nondefault only.
465   proto2_nofieldpresence_unittest::TestAllTypes source;
466   proto2_nofieldpresence_unittest::TestAllTypes dest;
467 
468   dest.set_optional_int32(42);
469   dest.set_optional_string("test");
470   source.set_optional_int32(0);
471   source.set_optional_string("");
472   // MergeFrom() copies only if present in serialization, i.e., non-zero.
473   dest.MergeFrom(source);
474   EXPECT_EQ(42, dest.optional_int32());
475   EXPECT_EQ("test", dest.optional_string());
476 
477   source.set_optional_int32(84);
478   source.set_optional_string("test2");
479   dest.MergeFrom(source);
480   EXPECT_EQ(84, dest.optional_int32());
481   EXPECT_EQ("test2", dest.optional_string());
482 }
483 
TEST(NoFieldPresenceTest,IsInitializedTest)484 TEST(NoFieldPresenceTest, IsInitializedTest) {
485   // Check that IsInitialized works properly.
486   proto2_nofieldpresence_unittest::TestProto2Required message;
487 
488   EXPECT_EQ(true, message.IsInitialized());
489   message.mutable_proto2()->set_a(1);
490   EXPECT_EQ(false, message.IsInitialized());
491   message.mutable_proto2()->set_b(1);
492   EXPECT_EQ(false, message.IsInitialized());
493   message.mutable_proto2()->set_c(1);
494   EXPECT_EQ(true, message.IsInitialized());
495 }
496 
TEST(NoFieldPresenceTest,LazyMessageFieldHasBit)497 TEST(NoFieldPresenceTest, LazyMessageFieldHasBit) {
498   // Check that has-bit interaction with lazy message works (has-bit before and
499   // after lazy decode).
500   proto2_nofieldpresence_unittest::TestAllTypes message;
501   const Reflection* r = message.GetReflection();
502   const Descriptor* desc = message.GetDescriptor();
503   const FieldDescriptor* field = desc->FindFieldByName("optional_lazy_message");
504   GOOGLE_CHECK(field != nullptr);
505 
506   EXPECT_EQ(false, message.has_optional_lazy_message());
507   EXPECT_EQ(false, r->HasField(message, field));
508 
509   message.mutable_optional_lazy_message()->set_bb(42);
510   EXPECT_EQ(true, message.has_optional_lazy_message());
511   EXPECT_EQ(true, r->HasField(message, field));
512 
513   // Serialize and parse with a new message object so that lazy field on new
514   // object is in unparsed state.
515   std::string output;
516   message.SerializeToString(&output);
517   proto2_nofieldpresence_unittest::TestAllTypes message2;
518   message2.ParseFromString(output);
519 
520   EXPECT_EQ(true, message2.has_optional_lazy_message());
521   EXPECT_EQ(true, r->HasField(message2, field));
522 
523   // Access field to force lazy parse.
524   EXPECT_EQ(42, message.optional_lazy_message().bb());
525   EXPECT_EQ(true, message2.has_optional_lazy_message());
526   EXPECT_EQ(true, r->HasField(message2, field));
527 }
528 
TEST(NoFieldPresenceTest,OneofPresence)529 TEST(NoFieldPresenceTest, OneofPresence) {
530   proto2_nofieldpresence_unittest::TestAllTypes message;
531   // oneof fields still have field presence -- ensure that this goes on the wire
532   // even though its value is the empty string.
533   message.set_oneof_string("");
534   std::string serialized;
535   message.SerializeToString(&serialized);
536   // Tag: 113 --> tag is (113 << 3) | 2 (length delimited) = 906
537   // varint: 0x8a 0x07
538   // Length: 0x00
539   EXPECT_EQ(3, serialized.size());
540   EXPECT_EQ(static_cast<char>(0x8a), serialized.at(0));
541   EXPECT_EQ(static_cast<char>(0x07), serialized.at(1));
542   EXPECT_EQ(static_cast<char>(0x00), serialized.at(2));
543 
544   message.Clear();
545   EXPECT_TRUE(message.ParseFromString(serialized));
546   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
547             message.oneof_field_case());
548 
549   // Also test int32 and enum fields.
550   message.Clear();
551   message.set_oneof_uint32(0);  // would not go on wire if ordinary field.
552   message.SerializeToString(&serialized);
553   EXPECT_EQ(3, serialized.size());
554   EXPECT_TRUE(message.ParseFromString(serialized));
555   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofUint32,
556             message.oneof_field_case());
557 
558   message.Clear();
559   message.set_oneof_enum(
560       proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO);  // default
561                                                                       // value.
562   message.SerializeToString(&serialized);
563   EXPECT_EQ(3, serialized.size());
564   EXPECT_TRUE(message.ParseFromString(serialized));
565   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofEnum,
566             message.oneof_field_case());
567 
568   message.Clear();
569   message.set_oneof_string("test");
570   message.clear_oneof_string();
571   EXPECT_EQ(0, message.ByteSize());
572 }
573 
574 }  // namespace
575 }  // namespace protobuf
576 }  // namespace google
577