• 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 <memory>
32 #include <string>
33 #include <vector>
34 
35 #include <google/protobuf/test_util.h>
36 #include <google/protobuf/unittest.pb.h>
37 #include <google/protobuf/unittest_proto3_arena.pb.h>
38 #include <google/protobuf/unittest_proto3_optional.pb.h>
39 #include <google/protobuf/arena.h>
40 #include <google/protobuf/text_format.h>
41 #include <google/protobuf/testing/googletest.h>
42 #include <gtest/gtest.h>
43 #include <google/protobuf/stubs/strutil.h>
44 
45 using proto3_arena_unittest::TestAllTypes;
46 
47 namespace google {
48 namespace protobuf {
49 namespace {
50 // We selectively set/check a few representative fields rather than all fields
51 // as this test is only expected to cover the basics of arena support.
SetAllFields(TestAllTypes * m)52 void SetAllFields(TestAllTypes* m) {
53   m->set_optional_int32(100);
54   m->set_optional_string("asdf");
55   m->set_optional_bytes("jkl;");
56   m->mutable_optional_nested_message()->set_bb(42);
57   m->mutable_optional_foreign_message()->set_c(43);
58   m->set_optional_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
59   m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
60   m->mutable_optional_lazy_message()->set_bb(45);
61   m->add_repeated_int32(100);
62   m->add_repeated_string("asdf");
63   m->add_repeated_bytes("jkl;");
64   m->add_repeated_nested_message()->set_bb(46);
65   m->add_repeated_foreign_message()->set_c(47);
66   m->add_repeated_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
67   m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
68   m->add_repeated_lazy_message()->set_bb(49);
69 
70   m->set_oneof_uint32(1);
71   m->mutable_oneof_nested_message()->set_bb(50);
72   m->set_oneof_string("test");  // only this one remains set
73 }
74 
ExpectAllFieldsSet(const TestAllTypes & m)75 void ExpectAllFieldsSet(const TestAllTypes& m) {
76   EXPECT_EQ(100, m.optional_int32());
77   EXPECT_EQ("asdf", m.optional_string());
78   EXPECT_EQ("jkl;", m.optional_bytes());
79   EXPECT_EQ(true, m.has_optional_nested_message());
80   EXPECT_EQ(42, m.optional_nested_message().bb());
81   EXPECT_EQ(true, m.has_optional_foreign_message());
82   EXPECT_EQ(43, m.optional_foreign_message().c());
83   EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ, m.optional_nested_enum());
84   EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum());
85   EXPECT_EQ(true, m.has_optional_lazy_message());
86   EXPECT_EQ(45, m.optional_lazy_message().bb());
87 
88   EXPECT_EQ(1, m.repeated_int32_size());
89   EXPECT_EQ(100, m.repeated_int32(0));
90   EXPECT_EQ(1, m.repeated_string_size());
91   EXPECT_EQ("asdf", m.repeated_string(0));
92   EXPECT_EQ(1, m.repeated_bytes_size());
93   EXPECT_EQ("jkl;", m.repeated_bytes(0));
94   EXPECT_EQ(1, m.repeated_nested_message_size());
95   EXPECT_EQ(46, m.repeated_nested_message(0).bb());
96   EXPECT_EQ(1, m.repeated_foreign_message_size());
97   EXPECT_EQ(47, m.repeated_foreign_message(0).c());
98   EXPECT_EQ(1, m.repeated_nested_enum_size());
99   EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ,
100             m.repeated_nested_enum(0));
101   EXPECT_EQ(1, m.repeated_foreign_enum_size());
102   EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0));
103   EXPECT_EQ(1, m.repeated_lazy_message_size());
104   EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
105 
106   EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
107             m.oneof_field_case());
108   EXPECT_EQ("test", m.oneof_string());
109 }
110 
111 // In this file we only test some basic functionalities of arena support in
112 // proto3 and expect the arena support to be fully tested in proto2 unittests
113 // because proto3 shares most code with proto2.
114 
TEST(Proto3ArenaTest,Parsing)115 TEST(Proto3ArenaTest, Parsing) {
116   TestAllTypes original;
117   SetAllFields(&original);
118 
119   Arena arena;
120   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
121   arena_message->ParseFromString(original.SerializeAsString());
122   ExpectAllFieldsSet(*arena_message);
123 }
124 
TEST(Proto3ArenaTest,UnknownFields)125 TEST(Proto3ArenaTest, UnknownFields) {
126   TestAllTypes original;
127   SetAllFields(&original);
128 
129   Arena arena;
130   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
131   arena_message->ParseFromString(original.SerializeAsString());
132   ExpectAllFieldsSet(*arena_message);
133 
134   // In proto3 we can still get a pointer to the UnknownFieldSet through
135   // reflection API.
136   UnknownFieldSet* unknown_fields =
137       arena_message->GetReflection()->MutableUnknownFields(arena_message);
138   // We can modify this UnknownFieldSet.
139   unknown_fields->AddVarint(1, 2);
140   // And the unknown fields should be changed.
141   ASSERT_NE(original.ByteSizeLong(), arena_message->ByteSizeLong());
142   ASSERT_FALSE(
143       arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
144 }
145 
TEST(Proto3ArenaTest,Swap)146 TEST(Proto3ArenaTest, Swap) {
147   Arena arena1;
148   Arena arena2;
149 
150   // Test Swap().
151   TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
152   TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
153   arena1_message->Swap(arena2_message);
154   EXPECT_EQ(&arena1, arena1_message->GetArena());
155   EXPECT_EQ(&arena2, arena2_message->GetArena());
156 }
157 
TEST(Proto3ArenaTest,SetAllocatedMessage)158 TEST(Proto3ArenaTest, SetAllocatedMessage) {
159   Arena arena;
160   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
161   TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
162   nested->set_bb(118);
163   arena_message->set_allocated_optional_nested_message(nested);
164   EXPECT_EQ(118, arena_message->optional_nested_message().bb());
165 }
166 
TEST(Proto3ArenaTest,ReleaseMessage)167 TEST(Proto3ArenaTest, ReleaseMessage) {
168   Arena arena;
169   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
170   arena_message->mutable_optional_nested_message()->set_bb(118);
171   std::unique_ptr<TestAllTypes::NestedMessage> nested(
172       arena_message->release_optional_nested_message());
173   EXPECT_EQ(118, nested->bb());
174 }
175 
TEST(Proto3ArenaTest,MessageFieldClear)176 TEST(Proto3ArenaTest, MessageFieldClear) {
177   // GitHub issue #310: https://github.com/protocolbuffers/protobuf/issues/310
178   Arena arena;
179   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
180   arena_message->mutable_optional_nested_message()->set_bb(118);
181   // This should not crash, but prior to the bugfix, it tried to use `operator
182   // delete` the nested message (which is on the arena):
183   arena_message->Clear();
184 }
185 
TEST(Proto3ArenaTest,MessageFieldClearViaReflection)186 TEST(Proto3ArenaTest, MessageFieldClearViaReflection) {
187   Arena arena;
188   TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
189   const Reflection* r = message->GetReflection();
190   const Descriptor* d = message->GetDescriptor();
191   const FieldDescriptor* msg_field =
192       d->FindFieldByName("optional_nested_message");
193 
194   message->mutable_optional_nested_message()->set_bb(1);
195   r->ClearField(message, msg_field);
196   EXPECT_FALSE(message->has_optional_nested_message());
197   EXPECT_EQ(0, message->optional_nested_message().bb());
198 }
199 
TEST(Proto3OptionalTest,OptionalFields)200 TEST(Proto3OptionalTest, OptionalFields) {
201   protobuf_unittest::TestProto3Optional msg;
202   EXPECT_FALSE(msg.has_optional_int32());
203   msg.set_optional_int32(0);
204   EXPECT_TRUE(msg.has_optional_int32());
205 
206   std::string serialized;
207   msg.SerializeToString(&serialized);
208   EXPECT_GT(serialized.size(), 0);
209 
210   msg.clear_optional_int32();
211   EXPECT_FALSE(msg.has_optional_int32());
212   msg.SerializeToString(&serialized);
213   EXPECT_EQ(serialized.size(), 0);
214 }
215 
TEST(Proto3OptionalTest,OptionalFieldDescriptor)216 TEST(Proto3OptionalTest, OptionalFieldDescriptor) {
217   const Descriptor* d = protobuf_unittest::TestProto3Optional::descriptor();
218 
219   for (int i = 0; i < d->field_count(); i++) {
220     const FieldDescriptor* f = d->field(i);
221     if (HasPrefixString(f->name(), "singular")) {
222       EXPECT_FALSE(f->has_optional_keyword()) << f->full_name();
223       EXPECT_FALSE(f->has_presence()) << f->full_name();
224       EXPECT_FALSE(f->containing_oneof()) << f->full_name();
225     } else {
226       EXPECT_TRUE(f->has_optional_keyword()) << f->full_name();
227       EXPECT_TRUE(f->has_presence()) << f->full_name();
228       EXPECT_TRUE(f->containing_oneof()) << f->full_name();
229     }
230   }
231 }
232 
TEST(Proto3OptionalTest,OptionalField)233 TEST(Proto3OptionalTest, OptionalField) {
234   protobuf_unittest::TestProto3Optional msg;
235   EXPECT_FALSE(msg.has_optional_int32());
236   msg.set_optional_int32(0);
237   EXPECT_TRUE(msg.has_optional_int32());
238 
239   std::string serialized;
240   msg.SerializeToString(&serialized);
241   EXPECT_GT(serialized.size(), 0);
242 
243   msg.clear_optional_int32();
244   EXPECT_FALSE(msg.has_optional_int32());
245   msg.SerializeToString(&serialized);
246   EXPECT_EQ(serialized.size(), 0);
247 }
248 
TEST(Proto3OptionalTest,OptionalFieldReflection)249 TEST(Proto3OptionalTest, OptionalFieldReflection) {
250   // Tests that oneof reflection works on synthetic oneofs.
251   //
252   // We test this more deeply elsewhere by parsing/serializing TextFormat (which
253   // doesn't treat synthetic oneofs specially, so reflects over them normally).
254   protobuf_unittest::TestProto3Optional msg;
255   const google::protobuf::Descriptor* d = msg.GetDescriptor();
256   const google::protobuf::Reflection* r = msg.GetReflection();
257   const google::protobuf::FieldDescriptor* f = d->FindFieldByName("optional_int32");
258   const google::protobuf::OneofDescriptor* o = d->FindOneofByName("_optional_int32");
259   GOOGLE_CHECK(f);
260   GOOGLE_CHECK(o);
261   EXPECT_TRUE(o->is_synthetic());
262 
263   EXPECT_FALSE(r->HasField(msg, f));
264   EXPECT_FALSE(r->HasOneof(msg, o));
265   EXPECT_TRUE(r->GetOneofFieldDescriptor(msg, o) == nullptr);
266 
267   r->SetInt32(&msg, f, 123);
268   EXPECT_EQ(123, msg.optional_int32());
269   EXPECT_EQ(123, r->GetInt32(msg, f));
270   EXPECT_TRUE(r->HasField(msg, f));
271   EXPECT_TRUE(r->HasOneof(msg, o));
272   EXPECT_EQ(f, r->GetOneofFieldDescriptor(msg, o));
273 
274   std::vector<const FieldDescriptor*> fields;
275   r->ListFields(msg, &fields);
276   EXPECT_EQ(1, fields.size());
277   EXPECT_EQ(f, fields[0]);
278 
279   r->ClearOneof(&msg, o);
280   EXPECT_FALSE(r->HasField(msg, f));
281   EXPECT_FALSE(r->HasOneof(msg, o));
282   EXPECT_TRUE(r->GetOneofFieldDescriptor(msg, o) == nullptr);
283 
284   msg.set_optional_int32(123);
285   EXPECT_EQ(123, r->GetInt32(msg, f));
286   EXPECT_TRUE(r->HasField(msg, f));
287   EXPECT_TRUE(r->HasOneof(msg, o));
288   EXPECT_EQ(f, r->GetOneofFieldDescriptor(msg, o));
289 
290   r->ClearOneof(&msg, o);
291   EXPECT_FALSE(r->HasField(msg, f));
292   EXPECT_FALSE(r->HasOneof(msg, o));
293   EXPECT_TRUE(r->GetOneofFieldDescriptor(msg, o) == nullptr);
294 }
295 
296 // It's a regression test for b/160665543.
TEST(Proto3OptionalTest,ClearNonOptionalMessageField)297 TEST(Proto3OptionalTest, ClearNonOptionalMessageField) {
298   protobuf_unittest::TestProto3OptionalMessage msg;
299   msg.mutable_nested_message();
300   const google::protobuf::Descriptor* d = msg.GetDescriptor();
301   const google::protobuf::Reflection* r = msg.GetReflection();
302   const google::protobuf::FieldDescriptor* f = d->FindFieldByName("nested_message");
303   r->ClearField(&msg, f);
304 }
305 
TEST(Proto3OptionalTest,ClearOptionalMessageField)306 TEST(Proto3OptionalTest, ClearOptionalMessageField) {
307   protobuf_unittest::TestProto3OptionalMessage msg;
308   msg.mutable_optional_nested_message();
309   const google::protobuf::Descriptor* d = msg.GetDescriptor();
310   const google::protobuf::Reflection* r = msg.GetReflection();
311   const google::protobuf::FieldDescriptor* f =
312       d->FindFieldByName("optional_nested_message");
313   r->ClearField(&msg, f);
314 }
315 
TEST(Proto3OptionalTest,SwapNonOptionalMessageField)316 TEST(Proto3OptionalTest, SwapNonOptionalMessageField) {
317   protobuf_unittest::TestProto3OptionalMessage msg1;
318   protobuf_unittest::TestProto3OptionalMessage msg2;
319   msg1.mutable_nested_message();
320   const google::protobuf::Descriptor* d = msg1.GetDescriptor();
321   const google::protobuf::Reflection* r = msg1.GetReflection();
322   const google::protobuf::FieldDescriptor* f = d->FindFieldByName("nested_message");
323   r->SwapFields(&msg1, &msg2, {f});
324 }
325 
TEST(Proto3OptionalTest,SwapOptionalMessageField)326 TEST(Proto3OptionalTest, SwapOptionalMessageField) {
327   protobuf_unittest::TestProto3OptionalMessage msg1;
328   protobuf_unittest::TestProto3OptionalMessage msg2;
329   msg1.mutable_optional_nested_message();
330   const google::protobuf::Descriptor* d = msg1.GetDescriptor();
331   const google::protobuf::Reflection* r = msg1.GetReflection();
332   const google::protobuf::FieldDescriptor* f =
333       d->FindFieldByName("optional_nested_message");
334   r->SwapFields(&msg1, &msg2, {f});
335 }
336 
SetAllFieldsZero(protobuf_unittest::TestProto3Optional * msg)337 void SetAllFieldsZero(protobuf_unittest::TestProto3Optional* msg) {
338   msg->set_optional_int32(0);
339   msg->set_optional_int64(0);
340   msg->set_optional_uint32(0);
341   msg->set_optional_uint64(0);
342   msg->set_optional_sint32(0);
343   msg->set_optional_sint64(0);
344   msg->set_optional_fixed32(0);
345   msg->set_optional_fixed64(0);
346   msg->set_optional_sfixed32(0);
347   msg->set_optional_sfixed64(0);
348   msg->set_optional_float(0);
349   msg->set_optional_double(0);
350   msg->set_optional_bool(false);
351   msg->set_optional_string("");
352   msg->set_optional_bytes("");
353   msg->mutable_optional_nested_message();
354   msg->mutable_lazy_nested_message();
355   msg->set_optional_nested_enum(
356       protobuf_unittest::TestProto3Optional::UNSPECIFIED);
357 }
358 
SetAllFieldsNonZero(protobuf_unittest::TestProto3Optional * msg)359 void SetAllFieldsNonZero(protobuf_unittest::TestProto3Optional* msg) {
360   msg->set_optional_int32(101);
361   msg->set_optional_int64(102);
362   msg->set_optional_uint32(103);
363   msg->set_optional_uint64(104);
364   msg->set_optional_sint32(105);
365   msg->set_optional_sint64(106);
366   msg->set_optional_fixed32(107);
367   msg->set_optional_fixed64(108);
368   msg->set_optional_sfixed32(109);
369   msg->set_optional_sfixed64(110);
370   msg->set_optional_float(111);
371   msg->set_optional_double(112);
372   msg->set_optional_bool(true);
373   msg->set_optional_string("abc");
374   msg->set_optional_bytes("def");
375   msg->mutable_optional_nested_message();
376   msg->mutable_lazy_nested_message();
377   msg->set_optional_nested_enum(protobuf_unittest::TestProto3Optional::BAZ);
378 }
379 
TestAllFieldsZero(const protobuf_unittest::TestProto3Optional & msg)380 void TestAllFieldsZero(const protobuf_unittest::TestProto3Optional& msg) {
381   EXPECT_EQ(0, msg.optional_int32());
382   EXPECT_EQ(0, msg.optional_int64());
383   EXPECT_EQ(0, msg.optional_uint32());
384   EXPECT_EQ(0, msg.optional_uint64());
385   EXPECT_EQ(0, msg.optional_sint32());
386   EXPECT_EQ(0, msg.optional_sint64());
387   EXPECT_EQ(0, msg.optional_fixed32());
388   EXPECT_EQ(0, msg.optional_fixed64());
389   EXPECT_EQ(0, msg.optional_sfixed32());
390   EXPECT_EQ(0, msg.optional_sfixed64());
391   EXPECT_EQ(0, msg.optional_float());
392   EXPECT_EQ(0, msg.optional_double());
393   EXPECT_EQ(0, msg.optional_bool());
394   EXPECT_EQ("", msg.optional_string());
395   EXPECT_EQ("", msg.optional_bytes());
396   EXPECT_EQ(protobuf_unittest::TestProto3Optional::UNSPECIFIED,
397             msg.optional_nested_enum());
398 
399   const Reflection* r = msg.GetReflection();
400   const Descriptor* d = msg.GetDescriptor();
401   EXPECT_EQ("", r->GetString(msg, d->FindFieldByName("optional_string")));
402 }
403 
TestAllFieldsNonZero(const protobuf_unittest::TestProto3Optional & msg)404 void TestAllFieldsNonZero(const protobuf_unittest::TestProto3Optional& msg) {
405   EXPECT_EQ(101, msg.optional_int32());
406   EXPECT_EQ(102, msg.optional_int64());
407   EXPECT_EQ(103, msg.optional_uint32());
408   EXPECT_EQ(104, msg.optional_uint64());
409   EXPECT_EQ(105, msg.optional_sint32());
410   EXPECT_EQ(106, msg.optional_sint64());
411   EXPECT_EQ(107, msg.optional_fixed32());
412   EXPECT_EQ(108, msg.optional_fixed64());
413   EXPECT_EQ(109, msg.optional_sfixed32());
414   EXPECT_EQ(110, msg.optional_sfixed64());
415   EXPECT_EQ(111, msg.optional_float());
416   EXPECT_EQ(112, msg.optional_double());
417   EXPECT_EQ(true, msg.optional_bool());
418   EXPECT_EQ("abc", msg.optional_string());
419   EXPECT_EQ("def", msg.optional_bytes());
420   EXPECT_EQ(protobuf_unittest::TestProto3Optional::BAZ,
421             msg.optional_nested_enum());
422 }
423 
TestAllFieldsSet(const protobuf_unittest::TestProto3Optional & msg,bool set)424 void TestAllFieldsSet(const protobuf_unittest::TestProto3Optional& msg,
425                       bool set) {
426   EXPECT_EQ(set, msg.has_optional_int32());
427   EXPECT_EQ(set, msg.has_optional_int64());
428   EXPECT_EQ(set, msg.has_optional_uint32());
429   EXPECT_EQ(set, msg.has_optional_uint64());
430   EXPECT_EQ(set, msg.has_optional_sint32());
431   EXPECT_EQ(set, msg.has_optional_sint64());
432   EXPECT_EQ(set, msg.has_optional_fixed32());
433   EXPECT_EQ(set, msg.has_optional_fixed64());
434   EXPECT_EQ(set, msg.has_optional_sfixed32());
435   EXPECT_EQ(set, msg.has_optional_sfixed64());
436   EXPECT_EQ(set, msg.has_optional_float());
437   EXPECT_EQ(set, msg.has_optional_double());
438   EXPECT_EQ(set, msg.has_optional_bool());
439   EXPECT_EQ(set, msg.has_optional_string());
440   EXPECT_EQ(set, msg.has_optional_bytes());
441   EXPECT_EQ(set, msg.has_optional_nested_message());
442   EXPECT_EQ(set, msg.has_lazy_nested_message());
443   EXPECT_EQ(set, msg.has_optional_nested_enum());
444 }
445 
TEST(Proto3OptionalTest,BinaryRoundTrip)446 TEST(Proto3OptionalTest, BinaryRoundTrip) {
447   protobuf_unittest::TestProto3Optional msg;
448   TestAllFieldsSet(msg, false);
449   SetAllFieldsZero(&msg);
450   TestAllFieldsZero(msg);
451   TestAllFieldsSet(msg, true);
452 
453   protobuf_unittest::TestProto3Optional msg2;
454   std::string serialized;
455   msg.SerializeToString(&serialized);
456   EXPECT_TRUE(msg2.ParseFromString(serialized));
457   TestAllFieldsZero(msg2);
458   TestAllFieldsSet(msg2, true);
459 }
460 
TEST(Proto3OptionalTest,TextFormatRoundTripZeros)461 TEST(Proto3OptionalTest, TextFormatRoundTripZeros) {
462   protobuf_unittest::TestProto3Optional msg;
463   SetAllFieldsZero(&msg);
464 
465   protobuf_unittest::TestProto3Optional msg2;
466   std::string text;
467   EXPECT_TRUE(TextFormat::PrintToString(msg, &text));
468   EXPECT_TRUE(TextFormat::ParseFromString(text, &msg2));
469   TestAllFieldsSet(msg2, true);
470   TestAllFieldsZero(msg2);
471 }
472 
TEST(Proto3OptionalTest,TextFormatRoundTripNonZeros)473 TEST(Proto3OptionalTest, TextFormatRoundTripNonZeros) {
474   protobuf_unittest::TestProto3Optional msg;
475   SetAllFieldsNonZero(&msg);
476 
477   protobuf_unittest::TestProto3Optional msg2;
478   std::string text;
479   EXPECT_TRUE(TextFormat::PrintToString(msg, &text));
480   EXPECT_TRUE(TextFormat::ParseFromString(text, &msg2));
481   TestAllFieldsSet(msg2, true);
482   TestAllFieldsNonZero(msg2);
483 }
484 
TEST(Proto3OptionalTest,SwapRoundTripZero)485 TEST(Proto3OptionalTest, SwapRoundTripZero) {
486   protobuf_unittest::TestProto3Optional msg;
487   SetAllFieldsZero(&msg);
488   TestAllFieldsSet(msg, true);
489 
490   protobuf_unittest::TestProto3Optional msg2;
491   msg.Swap(&msg2);
492   TestAllFieldsSet(msg2, true);
493   TestAllFieldsZero(msg2);
494 }
495 
TEST(Proto3OptionalTest,SwapRoundTripNonZero)496 TEST(Proto3OptionalTest, SwapRoundTripNonZero) {
497   protobuf_unittest::TestProto3Optional msg;
498   SetAllFieldsNonZero(&msg);
499   TestAllFieldsSet(msg, true);
500 
501   protobuf_unittest::TestProto3Optional msg2;
502   msg.Swap(&msg2);
503   TestAllFieldsSet(msg2, true);
504   TestAllFieldsNonZero(msg2);
505 }
506 
TEST(Proto3OptionalTest,ReflectiveSwapRoundTrip)507 TEST(Proto3OptionalTest, ReflectiveSwapRoundTrip) {
508   protobuf_unittest::TestProto3Optional msg;
509   SetAllFieldsZero(&msg);
510   TestAllFieldsSet(msg, true);
511 
512   protobuf_unittest::TestProto3Optional msg2;
513   msg2.GetReflection()->Swap(&msg, &msg2);
514   TestAllFieldsSet(msg2, true);
515   TestAllFieldsZero(msg2);
516 }
517 
TEST(Proto3OptionalTest,PlainFields)518 TEST(Proto3OptionalTest, PlainFields) {
519   const Descriptor* d = TestAllTypes::descriptor();
520 
521   EXPECT_FALSE(d->FindFieldByName("optional_int32")->has_presence());
522   EXPECT_TRUE(d->FindFieldByName("oneof_nested_message")->has_presence());
523 }
524 
525 }  // namespace
526 }  // namespace protobuf
527 }  // namespace google
528