protobuf/src/google/protobuf/proto3_arena_unittest.cc
Go to the documentation of this file.
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 // Must be included last.
46 #include <google/protobuf/port_def.inc>
47 
48 using proto3_arena_unittest::ForeignMessage;
49 using proto3_arena_unittest::TestAllTypes;
50 
51 namespace google {
52 namespace protobuf {
53 
54 namespace internal {
55 
57  public:
58  template <typename T>
59  static Arena* GetOwningArena(const T& msg) {
60  return msg.GetOwningArena();
61  }
62 };
63 
64 } // namespace internal
65 
66 namespace {
67 // We selectively set/check a few representative fields rather than all fields
68 // as this test is only expected to cover the basics of arena support.
69 void SetAllFields(TestAllTypes* m) {
70  m->set_optional_int32(100);
71  m->set_optional_string("asdf");
72  m->set_optional_bytes("jkl;");
73  m->mutable_optional_nested_message()->set_bb(42);
74  m->mutable_optional_foreign_message()->set_c(43);
75  m->set_optional_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
76  m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
77  m->mutable_optional_lazy_message()->set_bb(45);
78  m->add_repeated_int32(100);
79  m->add_repeated_string("asdf");
80  m->add_repeated_bytes("jkl;");
81  m->add_repeated_nested_message()->set_bb(46);
82  m->add_repeated_foreign_message()->set_c(47);
83  m->add_repeated_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
84  m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
85  m->add_repeated_lazy_message()->set_bb(49);
86 
87  m->set_oneof_uint32(1);
88  m->mutable_oneof_nested_message()->set_bb(50);
89  m->set_oneof_string("test"); // only this one remains set
90 }
91 
92 void ExpectAllFieldsSet(const TestAllTypes& m) {
93  EXPECT_EQ(100, m.optional_int32());
94  EXPECT_EQ("asdf", m.optional_string());
95  EXPECT_EQ("jkl;", m.optional_bytes());
96  EXPECT_EQ(true, m.has_optional_nested_message());
97  EXPECT_EQ(42, m.optional_nested_message().bb());
98  EXPECT_EQ(true, m.has_optional_foreign_message());
99  EXPECT_EQ(43, m.optional_foreign_message().c());
100  EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ, m.optional_nested_enum());
101  EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum());
102  EXPECT_EQ(true, m.has_optional_lazy_message());
103  EXPECT_EQ(45, m.optional_lazy_message().bb());
104 
105  EXPECT_EQ(1, m.repeated_int32_size());
106  EXPECT_EQ(100, m.repeated_int32(0));
107  EXPECT_EQ(1, m.repeated_string_size());
108  EXPECT_EQ("asdf", m.repeated_string(0));
109  EXPECT_EQ(1, m.repeated_bytes_size());
110  EXPECT_EQ("jkl;", m.repeated_bytes(0));
111  EXPECT_EQ(1, m.repeated_nested_message_size());
112  EXPECT_EQ(46, m.repeated_nested_message(0).bb());
113  EXPECT_EQ(1, m.repeated_foreign_message_size());
114  EXPECT_EQ(47, m.repeated_foreign_message(0).c());
115  EXPECT_EQ(1, m.repeated_nested_enum_size());
116  EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ,
117  m.repeated_nested_enum(0));
118  EXPECT_EQ(1, m.repeated_foreign_enum_size());
119  EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0));
120  EXPECT_EQ(1, m.repeated_lazy_message_size());
121  EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
122 
123  EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
124  m.oneof_field_case());
125  EXPECT_EQ("test", m.oneof_string());
126 }
127 
128 // In this file we only test some basic functionalities of arena support in
129 // proto3 and expect the arena support to be fully tested in proto2 unittests
130 // because proto3 shares most code with proto2.
131 
132 TEST(Proto3ArenaTest, Parsing) {
133  TestAllTypes original;
134  SetAllFields(&original);
135 
136  Arena arena;
137  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
138  arena_message->ParseFromString(original.SerializeAsString());
139  ExpectAllFieldsSet(*arena_message);
140 }
141 
142 TEST(Proto3ArenaTest, UnknownFields) {
143  TestAllTypes original;
144  SetAllFields(&original);
145 
146  Arena arena;
147  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
148  arena_message->ParseFromString(original.SerializeAsString());
149  ExpectAllFieldsSet(*arena_message);
150 
151  // In proto3 we can still get a pointer to the UnknownFieldSet through
152  // reflection API.
153  UnknownFieldSet* unknown_fields =
154  arena_message->GetReflection()->MutableUnknownFields(arena_message);
155  // We can modify this UnknownFieldSet.
156  unknown_fields->AddVarint(1, 2);
157  // And the unknown fields should be changed.
158  ASSERT_NE(original.ByteSizeLong(), arena_message->ByteSizeLong());
159  ASSERT_FALSE(
160  arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
161 }
162 
163 TEST(Proto3ArenaTest, GetArena) {
164  Arena arena;
165 
166  // Tests arena-allocated message and submessages.
167  auto* arena_message1 = Arena::CreateMessage<TestAllTypes>(&arena);
168  auto* arena_submessage1 = arena_message1->mutable_optional_foreign_message();
169  auto* arena_repeated_submessage1 =
170  arena_message1->add_repeated_foreign_message();
171  EXPECT_EQ(&arena, arena_message1->GetArena());
172  EXPECT_EQ(&arena, arena_submessage1->GetArena());
173  EXPECT_EQ(&arena, arena_repeated_submessage1->GetArena());
174 
175  // Tests attached heap-allocated messages.
176  auto* arena_message2 = Arena::CreateMessage<TestAllTypes>(&arena);
177  arena_message2->set_allocated_optional_foreign_message(new ForeignMessage());
178  arena_message2->mutable_repeated_foreign_message()->AddAllocated(
179  new ForeignMessage());
180  const auto& submessage2 = arena_message2->optional_foreign_message();
181  const auto& repeated_submessage2 =
182  arena_message2->repeated_foreign_message(0);
183  EXPECT_EQ(nullptr, submessage2.GetArena());
184  EXPECT_EQ(nullptr, repeated_submessage2.GetArena());
185 
186  // Tests message created by Arena::Create.
187  auto* arena_message3 = Arena::Create<TestAllTypes>(&arena);
188  EXPECT_EQ(nullptr, arena_message3->GetArena());
189 }
190 
191 TEST(Proto3ArenaTest, GetArenaWithUnknown) {
192  Arena arena;
193 
194  // Tests arena-allocated message and submessages.
195  auto* arena_message1 = Arena::CreateMessage<TestAllTypes>(&arena);
196  arena_message1->GetReflection()->MutableUnknownFields(arena_message1);
197  auto* arena_submessage1 = arena_message1->mutable_optional_foreign_message();
198  arena_submessage1->GetReflection()->MutableUnknownFields(arena_submessage1);
199  auto* arena_repeated_submessage1 =
200  arena_message1->add_repeated_foreign_message();
201  arena_repeated_submessage1->GetReflection()->MutableUnknownFields(
202  arena_repeated_submessage1);
203  EXPECT_EQ(&arena, arena_message1->GetArena());
204  EXPECT_EQ(&arena, arena_submessage1->GetArena());
205  EXPECT_EQ(&arena, arena_repeated_submessage1->GetArena());
206 
207  // Tests attached heap-allocated messages.
208  auto* arena_message2 = Arena::CreateMessage<TestAllTypes>(&arena);
209  arena_message2->set_allocated_optional_foreign_message(new ForeignMessage());
210  arena_message2->mutable_repeated_foreign_message()->AddAllocated(
211  new ForeignMessage());
212  auto* submessage2 = arena_message2->mutable_optional_foreign_message();
213  submessage2->GetReflection()->MutableUnknownFields(submessage2);
214  auto* repeated_submessage2 =
215  arena_message2->mutable_repeated_foreign_message(0);
216  repeated_submessage2->GetReflection()->MutableUnknownFields(
217  repeated_submessage2);
218  EXPECT_EQ(nullptr, submessage2->GetArena());
219  EXPECT_EQ(nullptr, repeated_submessage2->GetArena());
220 }
221 
222 TEST(Proto3ArenaTest, Swap) {
223  Arena arena1;
224  Arena arena2;
225 
226  // Test Swap().
227  TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
228  TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
229  arena1_message->Swap(arena2_message);
230  EXPECT_EQ(&arena1, arena1_message->GetArena());
231  EXPECT_EQ(&arena2, arena2_message->GetArena());
232 }
233 
234 TEST(Proto3ArenaTest, SetAllocatedMessage) {
235  Arena arena;
236  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
237  TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
238  nested->set_bb(118);
239  arena_message->set_allocated_optional_nested_message(nested);
240  EXPECT_EQ(118, arena_message->optional_nested_message().bb());
241 }
242 
243 TEST(Proto3ArenaTest, ReleaseMessage) {
244  Arena arena;
245  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
246  arena_message->mutable_optional_nested_message()->set_bb(118);
247  std::unique_ptr<TestAllTypes::NestedMessage> nested(
248  arena_message->release_optional_nested_message());
249  EXPECT_EQ(118, nested->bb());
250 }
251 
252 TEST(Proto3ArenaTest, MessageFieldClear) {
253  // GitHub issue #310: https://github.com/protocolbuffers/protobuf/issues/310
254  Arena arena;
255  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
256  arena_message->mutable_optional_nested_message()->set_bb(118);
257  // This should not crash, but prior to the bugfix, it tried to use `operator
258  // delete` the nested message (which is on the arena):
259  arena_message->Clear();
260 }
261 
262 TEST(Proto3ArenaTest, MessageFieldClearViaReflection) {
263  Arena arena;
264  TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
265  const Reflection* r = message->GetReflection();
266  const Descriptor* d = message->GetDescriptor();
267  const FieldDescriptor* msg_field =
268  d->FindFieldByName("optional_nested_message");
269 
270  message->mutable_optional_nested_message()->set_bb(1);
271  r->ClearField(message, msg_field);
272  EXPECT_FALSE(message->has_optional_nested_message());
273  EXPECT_EQ(0, message->optional_nested_message().bb());
274 }
275 
276 TEST(Proto3OptionalTest, OptionalFields) {
277  protobuf_unittest::TestProto3Optional msg;
278  EXPECT_FALSE(msg.has_optional_int32());
279  msg.set_optional_int32(0);
280  EXPECT_TRUE(msg.has_optional_int32());
281 
282  std::string serialized;
283  msg.SerializeToString(&serialized);
284  EXPECT_GT(serialized.size(), 0);
285 
286  msg.clear_optional_int32();
287  EXPECT_FALSE(msg.has_optional_int32());
288  msg.SerializeToString(&serialized);
289  EXPECT_EQ(serialized.size(), 0);
290 }
291 
292 TEST(Proto3OptionalTest, OptionalFieldDescriptor) {
294 
295  for (int i = 0; i < d->field_count(); i++) {
296  const FieldDescriptor* f = d->field(i);
297  if (HasPrefixString(f->name(), "singular")) {
298  EXPECT_FALSE(f->has_optional_keyword()) << f->full_name();
299  EXPECT_FALSE(f->has_presence()) << f->full_name();
300  EXPECT_FALSE(f->containing_oneof()) << f->full_name();
301  } else {
302  EXPECT_TRUE(f->has_optional_keyword()) << f->full_name();
303  EXPECT_TRUE(f->has_presence()) << f->full_name();
304  EXPECT_TRUE(f->containing_oneof()) << f->full_name();
305  }
306  }
307 }
308 
309 TEST(Proto3OptionalTest, Extensions) {
311  const FieldDescriptor* no_optional = p->FindExtensionByName(
312  "protobuf_unittest.Proto3OptionalExtensions.ext_no_optional");
313  const FieldDescriptor* with_optional = p->FindExtensionByName(
314  "protobuf_unittest.Proto3OptionalExtensions.ext_with_optional");
315  GOOGLE_CHECK(no_optional);
316  GOOGLE_CHECK(with_optional);
317  EXPECT_FALSE(no_optional->has_optional_keyword());
318  EXPECT_TRUE(with_optional->has_optional_keyword());
319 
321  EXPECT_TRUE(d->options().HasExtension(
322  protobuf_unittest::Proto3OptionalExtensions::ext_no_optional));
323  EXPECT_TRUE(d->options().HasExtension(
324  protobuf_unittest::Proto3OptionalExtensions::ext_with_optional));
325  EXPECT_EQ(8, d->options().GetExtension(
326  protobuf_unittest::Proto3OptionalExtensions::ext_no_optional));
327  EXPECT_EQ(16,
328  d->options().GetExtension(
329  protobuf_unittest::Proto3OptionalExtensions::ext_with_optional));
330 
332  EXPECT_FALSE(d2->options().HasExtension(
333  protobuf_unittest::Proto3OptionalExtensions::ext_no_optional));
334  EXPECT_FALSE(d2->options().HasExtension(
335  protobuf_unittest::Proto3OptionalExtensions::ext_with_optional));
336 }
337 
338 TEST(Proto3OptionalTest, OptionalField) {
339  protobuf_unittest::TestProto3Optional msg;
340  EXPECT_FALSE(msg.has_optional_int32());
341  msg.set_optional_int32(0);
342  EXPECT_TRUE(msg.has_optional_int32());
343 
344  std::string serialized;
345  msg.SerializeToString(&serialized);
346  EXPECT_GT(serialized.size(), 0);
347 
348  msg.clear_optional_int32();
349  EXPECT_FALSE(msg.has_optional_int32());
350  msg.SerializeToString(&serialized);
351  EXPECT_EQ(serialized.size(), 0);
352 }
353 
354 TEST(Proto3OptionalTest, OptionalFieldReflection) {
355  // Tests that oneof reflection works on synthetic oneofs.
356  //
357  // We test this more deeply elsewhere by parsing/serializing TextFormat (which
358  // doesn't treat synthetic oneofs specially, so reflects over them normally).
359  protobuf_unittest::TestProto3Optional msg;
360  const google::protobuf::Descriptor* d = msg.GetDescriptor();
361  const google::protobuf::Reflection* r = msg.GetReflection();
362  const google::protobuf::FieldDescriptor* f = d->FindFieldByName("optional_int32");
363  const google::protobuf::OneofDescriptor* o = d->FindOneofByName("_optional_int32");
364  GOOGLE_CHECK(f);
365  GOOGLE_CHECK(o);
366  EXPECT_TRUE(o->is_synthetic());
367 
368  EXPECT_FALSE(r->HasField(msg, f));
369  EXPECT_FALSE(r->HasOneof(msg, o));
370  EXPECT_TRUE(r->GetOneofFieldDescriptor(msg, o) == nullptr);
371 
372  r->SetInt32(&msg, f, 123);
373  EXPECT_EQ(123, msg.optional_int32());
374  EXPECT_EQ(123, r->GetInt32(msg, f));
375  EXPECT_TRUE(r->HasField(msg, f));
376  EXPECT_TRUE(r->HasOneof(msg, o));
377  EXPECT_EQ(f, r->GetOneofFieldDescriptor(msg, o));
378 
379  std::vector<const FieldDescriptor*> fields;
380  r->ListFields(msg, &fields);
381  EXPECT_EQ(1, fields.size());
382  EXPECT_EQ(f, fields[0]);
383 
384  r->ClearOneof(&msg, o);
385  EXPECT_FALSE(r->HasField(msg, f));
386  EXPECT_FALSE(r->HasOneof(msg, o));
387  EXPECT_TRUE(r->GetOneofFieldDescriptor(msg, o) == nullptr);
388 
389  msg.set_optional_int32(123);
390  EXPECT_EQ(123, r->GetInt32(msg, f));
391  EXPECT_TRUE(r->HasField(msg, f));
392  EXPECT_TRUE(r->HasOneof(msg, o));
393  EXPECT_EQ(f, r->GetOneofFieldDescriptor(msg, o));
394 
395  r->ClearOneof(&msg, o);
396  EXPECT_FALSE(r->HasField(msg, f));
397  EXPECT_FALSE(r->HasOneof(msg, o));
398  EXPECT_TRUE(r->GetOneofFieldDescriptor(msg, o) == nullptr);
399 }
400 
401 // It's a regression test for b/160665543.
402 TEST(Proto3OptionalTest, ClearNonOptionalMessageField) {
403  protobuf_unittest::TestProto3OptionalMessage msg;
404  msg.mutable_nested_message();
405  const google::protobuf::Descriptor* d = msg.GetDescriptor();
406  const google::protobuf::Reflection* r = msg.GetReflection();
407  const google::protobuf::FieldDescriptor* f = d->FindFieldByName("nested_message");
408  r->ClearField(&msg, f);
409 }
410 
411 TEST(Proto3OptionalTest, ClearOptionalMessageField) {
412  protobuf_unittest::TestProto3OptionalMessage msg;
413  msg.mutable_optional_nested_message();
414  const google::protobuf::Descriptor* d = msg.GetDescriptor();
415  const google::protobuf::Reflection* r = msg.GetReflection();
417  d->FindFieldByName("optional_nested_message");
418  r->ClearField(&msg, f);
419 }
420 
421 TEST(Proto3OptionalTest, SwapNonOptionalMessageField) {
422  protobuf_unittest::TestProto3OptionalMessage msg1;
423  protobuf_unittest::TestProto3OptionalMessage msg2;
424  msg1.mutable_nested_message();
425  const google::protobuf::Descriptor* d = msg1.GetDescriptor();
426  const google::protobuf::Reflection* r = msg1.GetReflection();
427  const google::protobuf::FieldDescriptor* f = d->FindFieldByName("nested_message");
428  r->SwapFields(&msg1, &msg2, {f});
429 }
430 
431 TEST(Proto3OptionalTest, SwapOptionalMessageField) {
432  protobuf_unittest::TestProto3OptionalMessage msg1;
433  protobuf_unittest::TestProto3OptionalMessage msg2;
434  msg1.mutable_optional_nested_message();
435  const google::protobuf::Descriptor* d = msg1.GetDescriptor();
436  const google::protobuf::Reflection* r = msg1.GetReflection();
438  d->FindFieldByName("optional_nested_message");
439  r->SwapFields(&msg1, &msg2, {f});
440 }
441 
442 void SetAllFieldsZero(protobuf_unittest::TestProto3Optional* msg) {
443  msg->set_optional_int32(0);
444  msg->set_optional_int64(0);
445  msg->set_optional_uint32(0);
446  msg->set_optional_uint64(0);
447  msg->set_optional_sint32(0);
448  msg->set_optional_sint64(0);
449  msg->set_optional_fixed32(0);
450  msg->set_optional_fixed64(0);
451  msg->set_optional_sfixed32(0);
452  msg->set_optional_sfixed64(0);
453  msg->set_optional_float(0);
454  msg->set_optional_double(0);
455  msg->set_optional_bool(false);
456  msg->set_optional_string("");
457  msg->set_optional_bytes("");
458  msg->mutable_optional_nested_message();
459  msg->mutable_lazy_nested_message();
460  msg->set_optional_nested_enum(
461  protobuf_unittest::TestProto3Optional::UNSPECIFIED);
462 }
463 
464 void SetAllFieldsNonZero(protobuf_unittest::TestProto3Optional* msg) {
465  msg->set_optional_int32(101);
466  msg->set_optional_int64(102);
467  msg->set_optional_uint32(103);
468  msg->set_optional_uint64(104);
469  msg->set_optional_sint32(105);
470  msg->set_optional_sint64(106);
471  msg->set_optional_fixed32(107);
472  msg->set_optional_fixed64(108);
473  msg->set_optional_sfixed32(109);
474  msg->set_optional_sfixed64(110);
475  msg->set_optional_float(111);
476  msg->set_optional_double(112);
477  msg->set_optional_bool(true);
478  msg->set_optional_string("abc");
479  msg->set_optional_bytes("def");
480  msg->mutable_optional_nested_message();
481  msg->mutable_lazy_nested_message();
482  msg->set_optional_nested_enum(protobuf_unittest::TestProto3Optional::BAZ);
483 }
484 
485 void TestAllFieldsZero(const protobuf_unittest::TestProto3Optional& msg) {
486  EXPECT_EQ(0, msg.optional_int32());
487  EXPECT_EQ(0, msg.optional_int64());
488  EXPECT_EQ(0, msg.optional_uint32());
489  EXPECT_EQ(0, msg.optional_uint64());
490  EXPECT_EQ(0, msg.optional_sint32());
491  EXPECT_EQ(0, msg.optional_sint64());
492  EXPECT_EQ(0, msg.optional_fixed32());
493  EXPECT_EQ(0, msg.optional_fixed64());
494  EXPECT_EQ(0, msg.optional_sfixed32());
495  EXPECT_EQ(0, msg.optional_sfixed64());
496  EXPECT_EQ(0, msg.optional_float());
497  EXPECT_EQ(0, msg.optional_double());
498  EXPECT_EQ(0, msg.optional_bool());
499  EXPECT_EQ("", msg.optional_string());
500  EXPECT_EQ("", msg.optional_bytes());
501  EXPECT_EQ(protobuf_unittest::TestProto3Optional::UNSPECIFIED,
502  msg.optional_nested_enum());
503 
504  const Reflection* r = msg.GetReflection();
505  const Descriptor* d = msg.GetDescriptor();
506  EXPECT_EQ("", r->GetString(msg, d->FindFieldByName("optional_string")));
507 }
508 
509 void TestAllFieldsNonZero(const protobuf_unittest::TestProto3Optional& msg) {
510  EXPECT_EQ(101, msg.optional_int32());
511  EXPECT_EQ(102, msg.optional_int64());
512  EXPECT_EQ(103, msg.optional_uint32());
513  EXPECT_EQ(104, msg.optional_uint64());
514  EXPECT_EQ(105, msg.optional_sint32());
515  EXPECT_EQ(106, msg.optional_sint64());
516  EXPECT_EQ(107, msg.optional_fixed32());
517  EXPECT_EQ(108, msg.optional_fixed64());
518  EXPECT_EQ(109, msg.optional_sfixed32());
519  EXPECT_EQ(110, msg.optional_sfixed64());
520  EXPECT_EQ(111, msg.optional_float());
521  EXPECT_EQ(112, msg.optional_double());
522  EXPECT_EQ(true, msg.optional_bool());
523  EXPECT_EQ("abc", msg.optional_string());
524  EXPECT_EQ("def", msg.optional_bytes());
525  EXPECT_EQ(protobuf_unittest::TestProto3Optional::BAZ,
526  msg.optional_nested_enum());
527 }
528 
529 void TestAllFieldsSet(const protobuf_unittest::TestProto3Optional& msg,
530  bool set) {
531  EXPECT_EQ(set, msg.has_optional_int32());
532  EXPECT_EQ(set, msg.has_optional_int64());
533  EXPECT_EQ(set, msg.has_optional_uint32());
534  EXPECT_EQ(set, msg.has_optional_uint64());
535  EXPECT_EQ(set, msg.has_optional_sint32());
536  EXPECT_EQ(set, msg.has_optional_sint64());
537  EXPECT_EQ(set, msg.has_optional_fixed32());
538  EXPECT_EQ(set, msg.has_optional_fixed64());
539  EXPECT_EQ(set, msg.has_optional_sfixed32());
540  EXPECT_EQ(set, msg.has_optional_sfixed64());
541  EXPECT_EQ(set, msg.has_optional_float());
542  EXPECT_EQ(set, msg.has_optional_double());
543  EXPECT_EQ(set, msg.has_optional_bool());
544  EXPECT_EQ(set, msg.has_optional_string());
545  EXPECT_EQ(set, msg.has_optional_bytes());
546  EXPECT_EQ(set, msg.has_optional_nested_message());
547  EXPECT_EQ(set, msg.has_lazy_nested_message());
548  EXPECT_EQ(set, msg.has_optional_nested_enum());
549 }
550 
551 TEST(Proto3OptionalTest, BinaryRoundTrip) {
552  protobuf_unittest::TestProto3Optional msg;
553  TestAllFieldsSet(msg, false);
554  SetAllFieldsZero(&msg);
555  TestAllFieldsZero(msg);
556  TestAllFieldsSet(msg, true);
557 
558  protobuf_unittest::TestProto3Optional msg2;
559  std::string serialized;
560  msg.SerializeToString(&serialized);
561  EXPECT_TRUE(msg2.ParseFromString(serialized));
562  TestAllFieldsZero(msg2);
563  TestAllFieldsSet(msg2, true);
564 }
565 
566 TEST(Proto3OptionalTest, TextFormatRoundTripZeros) {
567  protobuf_unittest::TestProto3Optional msg;
568  SetAllFieldsZero(&msg);
569 
570  protobuf_unittest::TestProto3Optional msg2;
574  TestAllFieldsSet(msg2, true);
575  TestAllFieldsZero(msg2);
576 }
577 
578 TEST(Proto3OptionalTest, TextFormatRoundTripNonZeros) {
579  protobuf_unittest::TestProto3Optional msg;
580  SetAllFieldsNonZero(&msg);
581 
582  protobuf_unittest::TestProto3Optional msg2;
586  TestAllFieldsSet(msg2, true);
587  TestAllFieldsNonZero(msg2);
588 }
589 
590 TEST(Proto3OptionalTest, SwapRoundTripZero) {
591  protobuf_unittest::TestProto3Optional msg;
592  SetAllFieldsZero(&msg);
593  TestAllFieldsSet(msg, true);
594 
595  protobuf_unittest::TestProto3Optional msg2;
596  msg.Swap(&msg2);
597  TestAllFieldsSet(msg2, true);
598  TestAllFieldsZero(msg2);
599 }
600 
601 TEST(Proto3OptionalTest, SwapRoundTripNonZero) {
602  protobuf_unittest::TestProto3Optional msg;
603  SetAllFieldsNonZero(&msg);
604  TestAllFieldsSet(msg, true);
605 
606  protobuf_unittest::TestProto3Optional msg2;
607  msg.Swap(&msg2);
608  TestAllFieldsSet(msg2, true);
609  TestAllFieldsNonZero(msg2);
610 }
611 
612 TEST(Proto3OptionalTest, ReflectiveSwapRoundTrip) {
613  protobuf_unittest::TestProto3Optional msg;
614  SetAllFieldsZero(&msg);
615  TestAllFieldsSet(msg, true);
616 
617  protobuf_unittest::TestProto3Optional msg2;
618  msg2.GetReflection()->Swap(&msg, &msg2);
619  TestAllFieldsSet(msg2, true);
620  TestAllFieldsZero(msg2);
621 }
622 
623 TEST(Proto3OptionalTest, PlainFields) {
625 
626  EXPECT_FALSE(d->FindFieldByName("optional_int32")->has_presence());
627  EXPECT_TRUE(d->FindFieldByName("oneof_nested_message")->has_presence());
628 }
629 
630 } // namespace
631 } // namespace protobuf
632 } // namespace google
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
ASSERT_NE
#define ASSERT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2060
absl::swap_internal::Swap
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
Definition: abseil-cpp/absl/meta/type_traits.h:772
google::protobuf.internal::GetArena
Arena * GetArena(MessageLite *msg, int64 arena_offset)
Definition: bloaty/third_party/protobuf/src/google/protobuf/generated_message_table_driven_lite.h:86
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf.internal::Proto3ArenaTestHelper
Definition: protobuf/src/google/protobuf/proto3_arena_unittest.cc:56
EXPECT_GT
#define EXPECT_GT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2036
Arena
Definition: arena.c:39
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::OneofDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:843
xds_manager.p
p
Definition: xds_manager.py:60
google::protobuf::Reflection
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:397
google::protobuf::python::cmessage::UnknownFieldSet
static PyObject * UnknownFieldSet(CMessage *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:2512
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
T
#define T(upbtypeconst, upbtype, ctype, default_value)
Descriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:121
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
tests.google.protobuf.internal.test_util.SetAllFields
def SetAllFields(message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:182
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
google::protobuf.internal::Proto3ArenaTestHelper::GetOwningArena
static Arena * GetOwningArena(const T &msg)
Definition: protobuf/src/google/protobuf/proto3_arena_unittest.cc:59
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
google::protobuf.internal.python_message.Extensions
Extensions
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:584
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
d2
static const fe d2
Definition: curve25519_tables.h:39
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
google::protobuf::TextFormat::ParseFromString
static bool ParseFromString(const std::string &input, Message *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1485
tests.google.protobuf.internal.test_util.ExpectAllFieldsSet
def ExpectAllFieldsSet(test_case, message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:367
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_unittest.cc:156
google::protobuf::HasPrefixString
bool HasPrefixString(const string &str, const string &prefix)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.h:115
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
nested
static int nested
Definition: test-callback-stack.c:39
UnknownFields
std::vector< UnknownField > UnknownFields
Definition: upb/upb/util/compare_test.cc:41
google::protobuf::TextFormat::PrintToString
static bool PrintToString(const Message &message, std::string *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:2395
profile_analyzer.fields
list fields
Definition: profile_analyzer.py:266
fix_build_deps.r
r
Definition: fix_build_deps.py:491
google::protobuf::DescriptorPool::generated_pool
static const DescriptorPool * generated_pool()
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:1326
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:153
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
internal
Definition: benchmark/test/output_test_helper.cc:20
make_curve25519_tables.d
int d
Definition: make_curve25519_tables.py:53
regress.m
m
Definition: regress/regress.py:25
DescriptorPool
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:110
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:46