• 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_no_field_presence.pb.h>
34 #include <google/protobuf/unittest.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(
124       proto2_nofieldpresence_unittest::FOREIGN_BAZ);
125   m->mutable_optional_lazy_message()->set_bb(45);
126   m->add_repeated_int32(100);
127   m->add_repeated_int64(101);
128   m->add_repeated_uint32(102);
129   m->add_repeated_uint64(103);
130   m->add_repeated_sint32(104);
131   m->add_repeated_sint64(105);
132   m->add_repeated_fixed32(106);
133   m->add_repeated_fixed64(107);
134   m->add_repeated_sfixed32(108);
135   m->add_repeated_sfixed64(109);
136   m->add_repeated_float(110.0);
137   m->add_repeated_double(111.0);
138   m->add_repeated_bool(true);
139   m->add_repeated_string("asdf");
140   m->add_repeated_bytes("jkl;");
141   m->add_repeated_nested_message()->set_bb(46);
142   m->add_repeated_foreign_message()->set_c(47);
143   m->add_repeated_proto2_message()->set_optional_int32(48);
144   m->add_repeated_nested_enum(
145       proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ);
146   m->add_repeated_foreign_enum(
147       proto2_nofieldpresence_unittest::FOREIGN_BAZ);
148   m->add_repeated_lazy_message()->set_bb(49);
149 
150   m->set_oneof_uint32(1);
151   m->mutable_oneof_nested_message()->set_bb(50);
152   m->set_oneof_string("test");  // only this one remains set
153 }
154 
CheckNonDefaultValues(const proto2_nofieldpresence_unittest::TestAllTypes & m)155 void CheckNonDefaultValues(
156 const proto2_nofieldpresence_unittest::TestAllTypes& m) {
157   EXPECT_EQ(100, m.optional_int32());
158   EXPECT_EQ(101, m.optional_int64());
159   EXPECT_EQ(102, m.optional_uint32());
160   EXPECT_EQ(103, m.optional_uint64());
161   EXPECT_EQ(104, m.optional_sint32());
162   EXPECT_EQ(105, m.optional_sint64());
163   EXPECT_EQ(106, m.optional_fixed32());
164   EXPECT_EQ(107, m.optional_fixed64());
165   EXPECT_EQ(108, m.optional_sfixed32());
166   EXPECT_EQ(109, m.optional_sfixed64());
167   EXPECT_EQ(110.0, m.optional_float());
168   EXPECT_EQ(111.0, m.optional_double());
169   EXPECT_EQ(true, m.optional_bool());
170   EXPECT_EQ("asdf", m.optional_string());
171   EXPECT_EQ("jkl;", m.optional_bytes());
172   EXPECT_EQ(true, m.has_optional_nested_message());
173   EXPECT_EQ(42, m.optional_nested_message().bb());
174   EXPECT_EQ(true, m.has_optional_foreign_message());
175   EXPECT_EQ(43, m.optional_foreign_message().c());
176   EXPECT_EQ(true, m.has_optional_proto2_message());
177   EXPECT_EQ(44, m.optional_proto2_message().optional_int32());
178   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
179             m.optional_nested_enum());
180   EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_BAZ,
181             m.optional_foreign_enum());
182   EXPECT_EQ(true, m.has_optional_lazy_message());
183   EXPECT_EQ(45, m.optional_lazy_message().bb());
184 
185   EXPECT_EQ(1, m.repeated_int32_size());
186   EXPECT_EQ(100, m.repeated_int32(0));
187   EXPECT_EQ(1, m.repeated_int64_size());
188   EXPECT_EQ(101, m.repeated_int64(0));
189   EXPECT_EQ(1, m.repeated_uint32_size());
190   EXPECT_EQ(102, m.repeated_uint32(0));
191   EXPECT_EQ(1, m.repeated_uint64_size());
192   EXPECT_EQ(103, m.repeated_uint64(0));
193   EXPECT_EQ(1, m.repeated_sint32_size());
194   EXPECT_EQ(104, m.repeated_sint32(0));
195   EXPECT_EQ(1, m.repeated_sint64_size());
196   EXPECT_EQ(105, m.repeated_sint64(0));
197   EXPECT_EQ(1, m.repeated_fixed32_size());
198   EXPECT_EQ(106, m.repeated_fixed32(0));
199   EXPECT_EQ(1, m.repeated_fixed64_size());
200   EXPECT_EQ(107, m.repeated_fixed64(0));
201   EXPECT_EQ(1, m.repeated_sfixed32_size());
202   EXPECT_EQ(108, m.repeated_sfixed32(0));
203   EXPECT_EQ(1, m.repeated_sfixed64_size());
204   EXPECT_EQ(109, m.repeated_sfixed64(0));
205   EXPECT_EQ(1, m.repeated_float_size());
206   EXPECT_EQ(110.0, m.repeated_float(0));
207   EXPECT_EQ(1, m.repeated_double_size());
208   EXPECT_EQ(111.0, m.repeated_double(0));
209   EXPECT_EQ(1, m.repeated_bool_size());
210   EXPECT_EQ(true, m.repeated_bool(0));
211   EXPECT_EQ(1, m.repeated_string_size());
212   EXPECT_EQ("asdf", m.repeated_string(0));
213   EXPECT_EQ(1, m.repeated_bytes_size());
214   EXPECT_EQ("jkl;", m.repeated_bytes(0));
215   EXPECT_EQ(1, m.repeated_nested_message_size());
216   EXPECT_EQ(46, m.repeated_nested_message(0).bb());
217   EXPECT_EQ(1, m.repeated_foreign_message_size());
218   EXPECT_EQ(47, m.repeated_foreign_message(0).c());
219   EXPECT_EQ(1, m.repeated_proto2_message_size());
220   EXPECT_EQ(48, m.repeated_proto2_message(0).optional_int32());
221   EXPECT_EQ(1, m.repeated_nested_enum_size());
222   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
223             m.repeated_nested_enum(0));
224   EXPECT_EQ(1, m.repeated_foreign_enum_size());
225   EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_BAZ,
226             m.repeated_foreign_enum(0));
227   EXPECT_EQ(1, m.repeated_lazy_message_size());
228   EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
229 
230   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
231             m.oneof_field_case());
232   EXPECT_EQ("test", m.oneof_string());
233 }
234 
TEST(NoFieldPresenceTest,BasicMessageTest)235 TEST(NoFieldPresenceTest, BasicMessageTest) {
236   proto2_nofieldpresence_unittest::TestAllTypes message;
237   // Check default values, fill all fields, check values. We just want to
238   // exercise the basic getters/setter paths here to make sure no
239   // field-presence-related changes broke these.
240   CheckDefaultValues(message);
241   FillValues(&message);
242   CheckNonDefaultValues(message);
243 
244   // Clear() should be equivalent to getting a freshly-constructed message.
245   message.Clear();
246   CheckDefaultValues(message);
247 }
248 
TEST(NoFieldPresenceTest,MessageFieldPresenceTest)249 TEST(NoFieldPresenceTest, MessageFieldPresenceTest) {
250   // check that presence still works properly for message fields.
251   proto2_nofieldpresence_unittest::TestAllTypes message;
252   EXPECT_EQ(false, message.has_optional_nested_message());
253   // Getter should fetch default instance, and not cause the field to become
254   // present.
255   EXPECT_EQ(0, message.optional_nested_message().bb());
256   EXPECT_EQ(false, message.has_optional_nested_message());
257   message.mutable_optional_nested_message()->set_bb(42);
258   EXPECT_EQ(true, message.has_optional_nested_message());
259   message.clear_optional_nested_message();
260   EXPECT_EQ(false, message.has_optional_nested_message());
261 
262   // Likewise for a lazy message field.
263   EXPECT_EQ(false, message.has_optional_lazy_message());
264   // Getter should fetch default instance, and not cause the field to become
265   // present.
266   EXPECT_EQ(0, message.optional_lazy_message().bb());
267   EXPECT_EQ(false, message.has_optional_lazy_message());
268   message.mutable_optional_lazy_message()->set_bb(42);
269   EXPECT_EQ(true, message.has_optional_lazy_message());
270   message.clear_optional_lazy_message();
271   EXPECT_EQ(false, message.has_optional_lazy_message());
272 
273   // Test field presence of a message field on the default instance.
274   EXPECT_EQ(false, proto2_nofieldpresence_unittest::TestAllTypes::
275             default_instance().has_optional_nested_message());
276 }
277 
TEST(NoFieldPresenceTest,ReflectionHasFieldTest)278 TEST(NoFieldPresenceTest, ReflectionHasFieldTest) {
279   // check that HasField reports true on all scalar fields. Check that it
280   // behaves properly for message fields.
281 
282   proto2_nofieldpresence_unittest::TestAllTypes message;
283   const google::protobuf::Reflection* r = message.GetReflection();
284   const google::protobuf::Descriptor* desc = message.GetDescriptor();
285 
286   // Check initial state: scalars not present (due to need to be consistent with
287   // MergeFrom()), message fields not present, oneofs not present.
288   for (int i = 0; i < desc->field_count(); i++) {
289     const google::protobuf::FieldDescriptor* field = desc->field(i);
290     if (field->is_repeated()) continue;
291     EXPECT_EQ(false, r->HasField(message, field));
292   }
293 
294   // Test field presence of a message field on the default instance.
295   const google::protobuf::FieldDescriptor* msg_field =
296       desc->FindFieldByName("optional_nested_message");
297   EXPECT_EQ(false, r->HasField(
298       proto2_nofieldpresence_unittest::TestAllTypes::
299       default_instance(), msg_field));
300 
301   // Fill all fields, expect everything to report true (check oneofs below).
302   FillValues(&message);
303   for (int i = 0; i < desc->field_count(); i++) {
304     const google::protobuf::FieldDescriptor* field = desc->field(i);
305     if (field->is_repeated() || field->containing_oneof()) {
306       continue;
307     }
308     if (field->options().ctype() != google::protobuf::FieldOptions::STRING) {
309       continue;
310     }
311     EXPECT_EQ(true, r->HasField(message, field));
312   }
313 
314   message.Clear();
315 
316   // Check zero/empty-means-not-present semantics.
317   const google::protobuf::FieldDescriptor* field_int32 = desc->FindFieldByName(
318       "optional_int32");
319   const google::protobuf::FieldDescriptor* field_double = desc->FindFieldByName(
320       "optional_double");
321   const google::protobuf::FieldDescriptor* field_string = desc->FindFieldByName(
322       "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 google::protobuf::Reflection* r = message.GetReflection();
348   const google::protobuf::Descriptor* desc = message.GetDescriptor();
349 
350   const google::protobuf::FieldDescriptor* field_int32 = desc->FindFieldByName(
351       "optional_int32");
352   const google::protobuf::FieldDescriptor* field_double = desc->FindFieldByName(
353       "optional_double");
354   const google::protobuf::FieldDescriptor* field_string = desc->FindFieldByName(
355       "optional_string");
356   const google::protobuf::FieldDescriptor* field_message = desc->FindFieldByName(
357       "optional_nested_message");
358   const google::protobuf::FieldDescriptor* field_lazy = desc->FindFieldByName(
359       "optional_lazy_message");
360 
361   message.set_optional_int32(42);
362   r->ClearField(&message, field_int32);
363   EXPECT_EQ(0, message.optional_int32());
364 
365   message.set_optional_double(42.0);
366   r->ClearField(&message, field_double);
367   EXPECT_EQ(0.0, message.optional_double());
368 
369   message.set_optional_string("test");
370   r->ClearField(&message, field_string);
371   EXPECT_EQ("", message.optional_string());
372 
373   message.mutable_optional_nested_message()->set_bb(1234);
374   r->ClearField(&message, field_message);
375   EXPECT_FALSE(message.has_optional_nested_message());
376   EXPECT_EQ(0, message.optional_nested_message().bb());
377 
378   message.mutable_optional_lazy_message()->set_bb(42);
379   r->ClearField(&message, field_lazy);
380   EXPECT_FALSE(message.has_optional_lazy_message());
381   EXPECT_EQ(0, message.optional_lazy_message().bb());
382 }
383 
TEST(NoFieldPresenceTest,HasFieldOneofsTest)384 TEST(NoFieldPresenceTest, HasFieldOneofsTest) {
385   // check that HasField behaves properly for oneofs.
386   proto2_nofieldpresence_unittest::TestAllTypes message;
387 
388   const google::protobuf::Reflection* r = message.GetReflection();
389   const google::protobuf::Descriptor* desc = message.GetDescriptor();
390   const google::protobuf::FieldDescriptor* desc_oneof_uint32 =
391       desc->FindFieldByName("oneof_uint32");
392   const google::protobuf::FieldDescriptor* desc_oneof_nested_message =
393       desc->FindFieldByName("oneof_nested_message");
394   const google::protobuf::FieldDescriptor* desc_oneof_string =
395       desc->FindFieldByName("oneof_string");
396   GOOGLE_CHECK_NOTNULL(desc_oneof_uint32);
397   GOOGLE_CHECK_NOTNULL(desc_oneof_nested_message);
398   GOOGLE_CHECK_NOTNULL(desc_oneof_string);
399 
400   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
401   EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
402   EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
403 
404   message.set_oneof_string("test");
405   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
406   EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
407   EXPECT_EQ(true, r->HasField(message, desc_oneof_string));
408   message.mutable_oneof_nested_message()->set_bb(42);
409   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
410   EXPECT_EQ(true, r->HasField(message, desc_oneof_nested_message));
411   EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
412 
413   message.Clear();
414   EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
415   EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
416   EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
417 }
418 
TEST(NoFieldPresenceTest,DontSerializeDefaultValuesTest)419 TEST(NoFieldPresenceTest, DontSerializeDefaultValuesTest) {
420   // check that serialized data contains only non-zero numeric fields/non-empty
421   // string/byte fields.
422   proto2_nofieldpresence_unittest::TestAllTypes message;
423   string output;
424 
425   // All default values -> no output.
426   message.SerializeToString(&output);
427   EXPECT_EQ(0, output.size());
428 
429   // Zero values -> still no output.
430   message.set_optional_int32(0);
431   message.set_optional_int64(0);
432   message.set_optional_uint32(0);
433   message.set_optional_uint64(0);
434   message.set_optional_sint32(0);
435   message.set_optional_sint64(0);
436   message.set_optional_fixed32(0);
437   message.set_optional_fixed64(0);
438   message.set_optional_sfixed32(0);
439   message.set_optional_sfixed64(0);
440   message.set_optional_float(0);
441   message.set_optional_double(0);
442   message.set_optional_bool(0);
443   message.set_optional_string("");
444   message.set_optional_bytes("");
445   message.set_optional_nested_enum(
446       proto2_nofieldpresence_unittest::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 google::protobuf::Reflection* r = message.GetReflection();
502   const google::protobuf::Descriptor* desc = message.GetDescriptor();
503   const google::protobuf::FieldDescriptor* field = desc->FindFieldByName(
504       "optional_lazy_message");
505   GOOGLE_CHECK_NOTNULL(field);
506 
507   EXPECT_EQ(false, message.has_optional_lazy_message());
508   EXPECT_EQ(false, r->HasField(message, field));
509 
510   message.mutable_optional_lazy_message()->set_bb(42);
511   EXPECT_EQ(true, message.has_optional_lazy_message());
512   EXPECT_EQ(true, r->HasField(message, field));
513 
514   // Serialize and parse with a new message object so that lazy field on new
515   // object is in unparsed state.
516   string output;
517   message.SerializeToString(&output);
518   proto2_nofieldpresence_unittest::TestAllTypes message2;
519   message2.ParseFromString(output);
520 
521   EXPECT_EQ(true, message2.has_optional_lazy_message());
522   EXPECT_EQ(true, r->HasField(message2, field));
523 
524   // Access field to force lazy parse.
525   EXPECT_EQ(42, message.optional_lazy_message().bb());
526   EXPECT_EQ(true, message2.has_optional_lazy_message());
527   EXPECT_EQ(true, r->HasField(message2, field));
528 }
529 
TEST(NoFieldPresenceTest,OneofPresence)530 TEST(NoFieldPresenceTest, OneofPresence) {
531   proto2_nofieldpresence_unittest::TestAllTypes message;
532   // oneof fields still have field presence -- ensure that this goes on the wire
533   // even though its value is the empty string.
534   message.set_oneof_string("");
535   string serialized;
536   message.SerializeToString(&serialized);
537   // Tag: 113 --> tag is (113 << 3) | 2 (length delimited) = 906
538   // varint: 0x8a 0x07
539   // Length: 0x00
540   EXPECT_EQ(3, serialized.size());
541   EXPECT_EQ(static_cast<char>(0x8a), serialized.at(0));
542   EXPECT_EQ(static_cast<char>(0x07), serialized.at(1));
543   EXPECT_EQ(static_cast<char>(0x00), serialized.at(2));
544 
545   message.Clear();
546   EXPECT_TRUE(message.ParseFromString(serialized));
547   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
548             message.oneof_field_case());
549 
550   // Also test int32 and enum fields.
551   message.Clear();
552   message.set_oneof_uint32(0);  // would not go on wire if ordinary field.
553   message.SerializeToString(&serialized);
554   EXPECT_EQ(3, serialized.size());
555   EXPECT_TRUE(message.ParseFromString(serialized));
556   EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofUint32,
557             message.oneof_field_case());
558 
559   message.Clear();
560   message.set_oneof_enum(proto2_nofieldpresence_unittest::
561                          TestAllTypes_NestedEnum_FOO);  // default 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 
577 }  // namespace google
578