protobuf/src/google/protobuf/generated_message_reflection_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 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // To test GeneratedMessageReflection, we actually let the protocol compiler
36 // generate a full protocol message implementation and then test its
37 // reflection interface. This is much easier and more maintainable than
38 // trying to create our own Message class for GeneratedMessageReflection
39 // to wrap.
40 //
41 // The tests here closely mirror some of the tests in
42 // compiler/cpp/unittest, except using the reflection interface
43 // rather than generated accessors.
44 
45 #include <google/protobuf/generated_message_reflection.h>
46 
47 #include <memory>
48 
49 #include <google/protobuf/stubs/logging.h>
50 #include <google/protobuf/stubs/common.h>
51 #include <google/protobuf/map_test_util.h>
52 #include <google/protobuf/map_unittest.pb.h>
53 #include <google/protobuf/test_util.h>
54 #include <google/protobuf/unittest.pb.h>
55 #include <google/protobuf/unittest_mset.pb.h>
56 #include <google/protobuf/unittest_mset_wire_format.pb.h>
57 #include <google/protobuf/arena.h>
58 #include <google/protobuf/descriptor.h>
59 #include <google/protobuf/testing/googletest.h>
60 #include <gtest/gtest.h>
61 
62 // Must be included last.
63 #include <google/protobuf/port_def.inc>
64 
65 namespace google {
66 namespace protobuf {
67 
69  public:
71  Message* lhs, Message* rhs,
72  const std::vector<const FieldDescriptor*>& fields) {
73  lhs->GetReflection()->UnsafeShallowSwapFields(lhs, rhs, fields);
74  }
75  static bool IsLazyExtension(const Message& msg, const FieldDescriptor* ext) {
76  return msg.GetReflection()->IsLazyExtension(msg, ext);
77  }
78 };
79 
80 namespace {
81 
82 // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
83 const FieldDescriptor* F(const std::string& name) {
84  const FieldDescriptor* result =
85  unittest::TestAllTypes::descriptor()->FindFieldByName(name);
86  GOOGLE_CHECK(result != nullptr);
87  return result;
88 }
89 
90 TEST(GeneratedMessageReflectionTest, Defaults) {
91  // Check that all default values are set correctly in the initial message.
92  unittest::TestAllTypes message;
93  TestUtil::ReflectionTester reflection_tester(
95 
96  reflection_tester.ExpectClearViaReflection(message);
97 
98  const Reflection* reflection = message.GetReflection();
99 
100  // Messages should return pointers to default instances until first use.
101  // (This is not checked by ExpectClear() since it is not actually true after
102  // the fields have been set and then cleared.)
103  EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
104  &reflection->GetMessage(message, F("optionalgroup")));
105  EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
106  &reflection->GetMessage(message, F("optional_nested_message")));
107  EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
108  &reflection->GetMessage(message, F("optional_foreign_message")));
109  EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
110  &reflection->GetMessage(message, F("optional_import_message")));
111 }
112 
113 TEST(GeneratedMessageReflectionTest, Accessors) {
114  // Set every field to a unique value then go back and check all those
115  // values.
116  unittest::TestAllTypes message;
117  TestUtil::ReflectionTester reflection_tester(
119 
120  reflection_tester.SetAllFieldsViaReflection(&message);
122  reflection_tester.ExpectAllFieldsSetViaReflection(message);
123 
124  reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
125  TestUtil::ExpectRepeatedFieldsModified(message);
126 }
127 
128 TEST(GeneratedMessageReflectionTest, GetStringReference) {
129  // Test that GetStringReference() returns the underlying string when it
130  // is a normal string field.
131  unittest::TestAllTypes message;
132  message.set_optional_string("foo");
133  message.add_repeated_string("foo");
134 
135  const Reflection* reflection = message.GetReflection();
137 
138  EXPECT_EQ(
139  &message.optional_string(),
140  &reflection->GetStringReference(message, F("optional_string"), &scratch))
141  << "For simple string fields, GetStringReference() should return a "
142  "reference to the underlying string.";
143  EXPECT_EQ(&message.repeated_string(0),
144  &reflection->GetRepeatedStringReference(
145  message, F("repeated_string"), 0, &scratch))
146  << "For simple string fields, GetRepeatedStringReference() should "
147  "return "
148  "a reference to the underlying string.";
149 }
150 
151 
152 TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) {
153  // Check that after setting all fields and then clearing, getting an
154  // embedded message does NOT return the default instance.
155  unittest::TestAllTypes message;
156  TestUtil::ReflectionTester reflection_tester(
158 
160  message.Clear();
161 
162  const Reflection* reflection = message.GetReflection();
163 
164  EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
165  &reflection->GetMessage(message, F("optionalgroup")));
166  EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
167  &reflection->GetMessage(message, F("optional_nested_message")));
168  EXPECT_NE(&unittest::ForeignMessage::default_instance(),
169  &reflection->GetMessage(message, F("optional_foreign_message")));
170  EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
171  &reflection->GetMessage(message, F("optional_import_message")));
172 }
173 
174 class GeneratedMessageReflectionSwapTest : public testing::TestWithParam<bool> {
175  protected:
176  void Swap(const Reflection* reflection, Message* lhs, Message* rhs) {
177  if (GetParam()) {
178  reflection->UnsafeArenaSwap(lhs, rhs);
179  } else {
180  reflection->Swap(lhs, rhs);
181  }
182  }
183  void SwapFields(const Reflection* reflection, Message* lhs, Message* rhs,
184  const std::vector<const FieldDescriptor*>& fields) {
185  if (GetParam()) {
186  reflection->UnsafeArenaSwapFields(lhs, rhs, fields);
187  } else {
188  reflection->SwapFields(lhs, rhs, fields);
189  }
190  }
191 };
192 
193 // unsafe_shallow_swap: true -> UnsafeArena* API.
194 INSTANTIATE_TEST_SUITE_P(ReflectionSwap, GeneratedMessageReflectionSwapTest,
195  testing::Bool());
196 
197 TEST_P(GeneratedMessageReflectionSwapTest, LhsSet) {
198  unittest::TestAllTypes lhs;
199  unittest::TestAllTypes rhs;
200 
202 
203  Swap(lhs.GetReflection(), &lhs, &rhs);
204 
205  TestUtil::ExpectClear(lhs);
207 }
208 
209 TEST_P(GeneratedMessageReflectionSwapTest, BothSet) {
210  unittest::TestAllTypes lhs;
211  unittest::TestAllTypes rhs;
212 
215  TestUtil::ModifyRepeatedFields(&rhs);
216 
217  const Reflection* reflection = lhs.GetReflection();
218  Swap(reflection, &lhs, &rhs);
219 
220  TestUtil::ExpectRepeatedFieldsModified(lhs);
222 
223  lhs.set_optional_int32(532819);
224 
225  Swap(reflection, &lhs, &rhs);
226 
227  EXPECT_EQ(532819, rhs.optional_int32());
228 }
229 
230 TEST_P(GeneratedMessageReflectionSwapTest, LhsCleared) {
231  unittest::TestAllTypes lhs;
232  unittest::TestAllTypes rhs;
233 
235 
236  // For proto2 message, for message field, Clear only reset hasbits, but
237  // doesn't delete the underlying field.
238  lhs.Clear();
239 
240  Swap(lhs.GetReflection(), &lhs, &rhs);
241 
242  TestUtil::ExpectClear(rhs);
243 }
244 
245 TEST_P(GeneratedMessageReflectionSwapTest, RhsCleared) {
246  unittest::TestAllTypes lhs;
247  unittest::TestAllTypes rhs;
248 
250 
251  // For proto2 message, for message field, Clear only reset hasbits, but
252  // doesn't delete the underlying field.
253  rhs.Clear();
254 
255  Swap(lhs.GetReflection(), &lhs, &rhs);
256 
257  TestUtil::ExpectClear(lhs);
258 }
259 
260 TEST_P(GeneratedMessageReflectionSwapTest, Extensions) {
261  unittest::TestAllExtensions lhs;
262  unittest::TestAllExtensions rhs;
263 
265 
266  Swap(lhs.GetReflection(), &lhs, &rhs);
267 
268  TestUtil::ExpectExtensionsClear(lhs);
269  TestUtil::ExpectAllExtensionsSet(rhs);
270 }
271 
272 TEST_P(GeneratedMessageReflectionSwapTest, Unknown) {
273  unittest::TestEmptyMessage lhs, rhs;
274 
275  lhs.mutable_unknown_fields()->AddVarint(1234, 1);
276 
277  EXPECT_EQ(1, lhs.unknown_fields().field_count());
278  EXPECT_EQ(0, rhs.unknown_fields().field_count());
279  Swap(lhs.GetReflection(), &lhs, &rhs);
280  EXPECT_EQ(0, lhs.unknown_fields().field_count());
281  EXPECT_EQ(1, rhs.unknown_fields().field_count());
282 }
283 
284 TEST_P(GeneratedMessageReflectionSwapTest, Oneof) {
285  unittest::TestOneof2 lhs, rhs;
286  TestUtil::SetOneof1(&lhs);
287 
288  Swap(lhs.GetReflection(), &lhs, &rhs);
289 
290  TestUtil::ExpectOneofClear(lhs);
291  TestUtil::ExpectOneofSet1(rhs);
292 }
293 
294 TEST_P(GeneratedMessageReflectionSwapTest, OneofBothSet) {
295  unittest::TestOneof2 lhs, rhs;
296  TestUtil::SetOneof1(&lhs);
297  TestUtil::SetOneof2(&rhs);
298 
299  Swap(lhs.GetReflection(), &lhs, &rhs);
300 
301  TestUtil::ExpectOneofSet2(lhs);
302  TestUtil::ExpectOneofSet1(rhs);
303 }
304 
305 TEST_P(GeneratedMessageReflectionSwapTest, SwapFields) {
306  unittest::TestAllTypes lhs, rhs;
307  lhs.set_optional_double(12.3);
308  lhs.mutable_repeated_int32()->Add(10);
309  lhs.mutable_repeated_int32()->Add(20);
310 
311  rhs.set_optional_string("hello");
312  rhs.mutable_repeated_int64()->Add(30);
313 
314  std::vector<const FieldDescriptor*> fields;
315  const Descriptor* descriptor = lhs.GetDescriptor();
316  fields.push_back(descriptor->FindFieldByName("optional_double"));
317  fields.push_back(descriptor->FindFieldByName("repeated_int32"));
318  fields.push_back(descriptor->FindFieldByName("optional_string"));
319  fields.push_back(descriptor->FindFieldByName("optional_uint64"));
320 
321  SwapFields(lhs.GetReflection(), &lhs, &rhs, fields);
322 
323  EXPECT_FALSE(lhs.has_optional_double());
324  EXPECT_EQ(0, lhs.repeated_int32_size());
325  EXPECT_TRUE(lhs.has_optional_string());
326  EXPECT_EQ("hello", lhs.optional_string());
327  EXPECT_EQ(0, lhs.repeated_int64_size());
328  EXPECT_FALSE(lhs.has_optional_uint64());
329 
330  EXPECT_TRUE(rhs.has_optional_double());
331  EXPECT_EQ(12.3, rhs.optional_double());
332  EXPECT_EQ(2, rhs.repeated_int32_size());
333  EXPECT_EQ(10, rhs.repeated_int32(0));
334  EXPECT_EQ(20, rhs.repeated_int32(1));
335  EXPECT_FALSE(rhs.has_optional_string());
336  EXPECT_EQ(1, rhs.repeated_int64_size());
337  EXPECT_FALSE(rhs.has_optional_uint64());
338 }
339 
340 TEST_P(GeneratedMessageReflectionSwapTest, SwapFieldsAll) {
341  unittest::TestAllTypes lhs;
342  unittest::TestAllTypes rhs;
343 
345 
346  std::vector<const FieldDescriptor*> fields;
347  const Reflection* reflection = lhs.GetReflection();
348  reflection->ListFields(rhs, &fields);
349  SwapFields(reflection, &lhs, &rhs, fields);
350 
352  TestUtil::ExpectClear(rhs);
353 }
354 
355 TEST(GeneratedMessageReflectionTest, SwapFieldsAllOnDifferentArena) {
356  Arena arena1, arena2;
357  auto* message1 = Arena::CreateMessage<unittest::TestAllTypes>(&arena1);
358  auto* message2 = Arena::CreateMessage<unittest::TestAllTypes>(&arena2);
359 
360  TestUtil::SetAllFields(message2);
361 
362  std::vector<const FieldDescriptor*> fields;
363  const Reflection* reflection = message1->GetReflection();
364  reflection->ListFields(*message2, &fields);
365  reflection->SwapFields(message1, message2, fields);
366 
367  TestUtil::ExpectAllFieldsSet(*message1);
368  TestUtil::ExpectClear(*message2);
369 }
370 
371 TEST(GeneratedMessageReflectionTest, SwapFieldsAllOnArenaHeap) {
372  Arena arena;
373  auto* message1 = Arena::CreateMessage<unittest::TestAllTypes>(&arena);
374  std::unique_ptr<unittest::TestAllTypes> message2(
375  Arena::CreateMessage<unittest::TestAllTypes>(nullptr));
376 
377  TestUtil::SetAllFields(message2.get());
378 
379  std::vector<const FieldDescriptor*> fields;
380  const Reflection* reflection = message1->GetReflection();
381  reflection->ListFields(*message2, &fields);
382  reflection->SwapFields(message1, message2.get(), fields);
383 
384  TestUtil::ExpectAllFieldsSet(*message1);
385  TestUtil::ExpectClear(*message2);
386 }
387 
388 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtension) {
389  unittest::TestAllExtensions message1;
390  unittest::TestAllExtensions message2;
391 
392  TestUtil::SetAllExtensions(&message1);
393 
394  std::vector<const FieldDescriptor*> fields;
395  const Reflection* reflection = message1.GetReflection();
396  reflection->ListFields(message1, &fields);
397  reflection->SwapFields(&message1, &message2, fields);
398 
399  TestUtil::ExpectExtensionsClear(message1);
400  TestUtil::ExpectAllExtensionsSet(message2);
401 }
402 
403 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtensionArenaHeap) {
404  Arena arena;
405 
406  std::unique_ptr<unittest::TestAllExtensions> message1(
407  Arena::CreateMessage<unittest::TestAllExtensions>(nullptr));
408  auto* message2 = Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
409 
410  TestUtil::SetAllExtensions(message1.get());
411 
412  std::vector<const FieldDescriptor*> fields;
413  const Reflection* reflection = message1->GetReflection();
414  reflection->ListFields(*message1, &fields);
415  reflection->SwapFields(message1.get(), message2, fields);
416 
417  TestUtil::ExpectExtensionsClear(*message1);
418  TestUtil::ExpectAllExtensionsSet(*message2);
419 }
420 
421 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsAll) {
422  Arena arena;
423  auto* message1 = Arena::CreateMessage<unittest::TestAllTypes>(&arena);
424  auto* message2 = Arena::CreateMessage<unittest::TestAllTypes>(&arena);
425 
426  TestUtil::SetAllFields(message2);
427 
428  auto* kept_nested_message_ptr = message2->mutable_optional_nested_message();
429  auto* kept_foreign_message_ptr = message2->mutable_optional_foreign_message();
430  auto* kept_repeated_nested_message_ptr =
431  message2->mutable_repeated_nested_message(0);
432  auto* kept_repeated_foreign_message_ptr =
433  message2->mutable_repeated_foreign_message(0);
434 
435  std::vector<const FieldDescriptor*> fields;
436  const Reflection* reflection = message1->GetReflection();
437  reflection->ListFields(*message2, &fields);
439  message1, message2, fields);
440 
441  TestUtil::ExpectAllFieldsSet(*message1);
442  TestUtil::ExpectClear(*message2);
443 
444  // Expects the swap to be shallow. Expects pointer stability to the element of
445  // the repeated fields (not the container).
446  EXPECT_EQ(kept_nested_message_ptr,
447  message1->mutable_optional_nested_message());
448  EXPECT_EQ(kept_foreign_message_ptr,
449  message1->mutable_optional_foreign_message());
450  EXPECT_EQ(kept_repeated_nested_message_ptr,
451  message1->mutable_repeated_nested_message(0));
452  EXPECT_EQ(kept_repeated_foreign_message_ptr,
453  message1->mutable_repeated_foreign_message(0));
454 }
455 
456 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsMap) {
457  Arena arena;
458  auto* message1 = Arena::CreateMessage<unittest::TestMap>(&arena);
459  auto* message2 = Arena::CreateMessage<unittest::TestMap>(&arena);
460 
461  MapTestUtil::SetMapFields(message2);
462 
463  auto* kept_map_int32_fm_ptr =
464  &(*message2->mutable_map_int32_foreign_message())[0];
465 
466  std::vector<const FieldDescriptor*> fields;
467  const Reflection* reflection = message1->GetReflection();
468  reflection->ListFields(*message2, &fields);
470  message1, message2, fields);
471 
472  MapTestUtil::ExpectMapFieldsSet(*message1);
473  MapTestUtil::ExpectClear(*message2);
474 
475  // Expects the swap to be shallow.
476  EXPECT_EQ(kept_map_int32_fm_ptr,
477  &(*message1->mutable_map_int32_foreign_message())[0]);
478 }
479 
480 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsAllExtension) {
481  Arena arena;
482  auto* message1 = Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
483  auto* message2 = Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
484 
485  TestUtil::SetAllExtensions(message1);
486 
487  auto* kept_nested_message_ext_ptr =
488  message1->MutableExtension(unittest::optional_nested_message_extension);
489  auto* kept_foreign_message_ext_ptr =
490  message1->MutableExtension(unittest::optional_foreign_message_extension);
491  auto* kept_repeated_nested_message_ext_ptr =
492  message1->MutableRepeatedExtension(
493  unittest::repeated_nested_message_extension);
494  auto* kept_repeated_foreign_message_ext_ptr =
495  message1->MutableRepeatedExtension(
496  unittest::repeated_foreign_message_extension);
497 
498  std::vector<const FieldDescriptor*> fields;
499  const Reflection* reflection = message1->GetReflection();
500  reflection->ListFields(*message1, &fields);
502  message1, message2, fields);
503 
504  TestUtil::ExpectExtensionsClear(*message1);
505  TestUtil::ExpectAllExtensionsSet(*message2);
506 
507  // Expects the swap to be shallow.
508  EXPECT_EQ(
509  kept_nested_message_ext_ptr,
510  message2->MutableExtension(unittest::optional_nested_message_extension));
511  EXPECT_EQ(
512  kept_foreign_message_ext_ptr,
513  message2->MutableExtension(unittest::optional_foreign_message_extension));
514  EXPECT_EQ(kept_repeated_nested_message_ext_ptr,
515  message2->MutableRepeatedExtension(
516  unittest::repeated_nested_message_extension));
517  EXPECT_EQ(kept_repeated_foreign_message_ext_ptr,
518  message2->MutableRepeatedExtension(
519  unittest::repeated_foreign_message_extension));
520 }
521 
522 TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) {
523  unittest::TestOneof2 message1, message2;
524  TestUtil::SetOneof1(&message1);
525 
526  std::vector<const FieldDescriptor*> fields;
527  const Descriptor* descriptor = message1.GetDescriptor();
528  for (int i = 0; i < descriptor->field_count(); i++) {
529  fields.push_back(descriptor->field(i));
530  }
531  const Reflection* reflection = message1.GetReflection();
532  reflection->SwapFields(&message1, &message2, fields);
533 
534  TestUtil::ExpectOneofClear(message1);
535  TestUtil::ExpectOneofSet1(message2);
536 }
537 
538 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsOneof) {
539  Arena arena;
540  auto* message1 = Arena::CreateMessage<unittest::TestOneof2>(&arena);
541  auto* message2 = Arena::CreateMessage<unittest::TestOneof2>(&arena);
542  TestUtil::SetOneof1(message1);
543 
544  std::vector<const FieldDescriptor*> fields;
545  const Descriptor* descriptor = message1->GetDescriptor();
546  for (int i = 0; i < descriptor->field_count(); i++) {
547  fields.push_back(descriptor->field(i));
548  }
550  message1, message2, fields);
551 
552  TestUtil::ExpectOneofClear(*message1);
553  TestUtil::ExpectOneofSet1(*message2);
554 }
555 
556 TEST(GeneratedMessageReflectionTest,
557  UnsafeShallowSwapFieldsOneofExpectShallow) {
558  Arena arena;
559  auto* message1 = Arena::CreateMessage<unittest::TestOneof2>(&arena);
560  auto* message2 = Arena::CreateMessage<unittest::TestOneof2>(&arena);
561  TestUtil::SetOneof1(message1);
562  message1->mutable_foo_message()->set_qux_int(1000);
563  auto* kept_foo_ptr = message1->mutable_foo_message();
564 
565  std::vector<const FieldDescriptor*> fields;
566  const Descriptor* descriptor = message1->GetDescriptor();
567  for (int i = 0; i < descriptor->field_count(); i++) {
568  fields.push_back(descriptor->field(i));
569  }
571  message1, message2, fields);
572 
573  EXPECT_TRUE(message2->has_foo_message());
574  EXPECT_EQ(message2->foo_message().qux_int(), 1000);
575  EXPECT_EQ(kept_foo_ptr, message2->mutable_foo_message());
576 }
577 
578 TEST(GeneratedMessageReflectionTest, RemoveLast) {
579  unittest::TestAllTypes message;
580  TestUtil::ReflectionTester reflection_tester(
582 
584 
585  reflection_tester.RemoveLastRepeatedsViaReflection(&message);
586 
587  TestUtil::ExpectLastRepeatedsRemoved(message);
588 }
589 
590 TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) {
591  unittest::TestAllExtensions message;
592  TestUtil::ReflectionTester reflection_tester(
594 
596 
597  reflection_tester.RemoveLastRepeatedsViaReflection(&message);
598 
599  TestUtil::ExpectLastRepeatedExtensionsRemoved(message);
600 }
601 
602 TEST(GeneratedMessageReflectionTest, ReleaseLast) {
603  unittest::TestAllTypes message;
604  const Descriptor* descriptor = message.GetDescriptor();
605  TestUtil::ReflectionTester reflection_tester(descriptor);
606 
608 
609  reflection_tester.ReleaseLastRepeatedsViaReflection(&message, false);
610 
611  TestUtil::ExpectLastRepeatedsReleased(message);
612 
613  // Now test that we actually release the right message.
614  message.Clear();
616  ASSERT_EQ(2, message.repeated_foreign_message_size());
617  const protobuf_unittest::ForeignMessage* expected =
618  message.mutable_repeated_foreign_message(1);
619  (void)expected; // unused in somce configurations
620  std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
621  &message, descriptor->FindFieldByName("repeated_foreign_message")));
622  EXPECT_EQ(expected, released.get());
623 }
624 
625 TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) {
626  unittest::TestAllExtensions message;
627  const Descriptor* descriptor = message.GetDescriptor();
628  TestUtil::ReflectionTester reflection_tester(descriptor);
629 
631 
632  reflection_tester.ReleaseLastRepeatedsViaReflection(&message, true);
633 
634  TestUtil::ExpectLastRepeatedExtensionsReleased(message);
635 
636  // Now test that we actually release the right message.
637  message.Clear();
639  ASSERT_EQ(
640  2, message.ExtensionSize(unittest::repeated_foreign_message_extension));
641  const protobuf_unittest::ForeignMessage* expected =
642  message.MutableExtension(unittest::repeated_foreign_message_extension, 1);
643  std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
644  &message, descriptor->file()->FindExtensionByName(
645  "repeated_foreign_message_extension")));
646  EXPECT_EQ(expected, released.get());
647 }
648 
649 TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) {
650  unittest::TestAllTypes message;
651  TestUtil::ReflectionTester reflection_tester(
653 
655 
656  // Swap and test that fields are all swapped.
657  reflection_tester.SwapRepeatedsViaReflection(&message);
658  TestUtil::ExpectRepeatedsSwapped(message);
659 
660  // Swap back and test that fields are all back to original values.
661  reflection_tester.SwapRepeatedsViaReflection(&message);
663 }
664 
665 TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) {
666  unittest::TestAllExtensions message;
667  TestUtil::ReflectionTester reflection_tester(
669 
671 
672  // Swap and test that fields are all swapped.
673  reflection_tester.SwapRepeatedsViaReflection(&message);
674  TestUtil::ExpectRepeatedExtensionsSwapped(message);
675 
676  // Swap back and test that fields are all back to original values.
677  reflection_tester.SwapRepeatedsViaReflection(&message);
678  TestUtil::ExpectAllExtensionsSet(message);
679 }
680 
681 TEST(GeneratedMessageReflectionTest, Extensions) {
682  // Set every extension to a unique value then go back and check all those
683  // values.
684  unittest::TestAllExtensions message;
685  TestUtil::ReflectionTester reflection_tester(
687 
688  reflection_tester.SetAllFieldsViaReflection(&message);
689  TestUtil::ExpectAllExtensionsSet(message);
690  reflection_tester.ExpectAllFieldsSetViaReflection(message);
691 
692  reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
693  TestUtil::ExpectRepeatedExtensionsModified(message);
694 }
695 
696 TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
697  const Reflection* reflection =
698  unittest::TestAllExtensions::default_instance().GetReflection();
699 
700  const FieldDescriptor* extension1 =
701  unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
702  "optional_int32_extension");
703  const FieldDescriptor* extension2 =
704  unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
705  "repeated_string_extension");
706 
707  EXPECT_EQ(extension1,
708  reflection->FindKnownExtensionByNumber(extension1->number()));
709  EXPECT_EQ(extension2,
710  reflection->FindKnownExtensionByNumber(extension2->number()));
711 
712  // Non-existent extension.
713  EXPECT_TRUE(reflection->FindKnownExtensionByNumber(62341) == nullptr);
714 
715  // Extensions of TestAllExtensions should not show up as extensions of
716  // other types.
717  EXPECT_TRUE(unittest::TestAllTypes::default_instance()
718  .GetReflection()
719  ->FindKnownExtensionByNumber(extension1->number()) ==
720  nullptr);
721 }
722 
723 TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
724  const Reflection* reflection =
725  unittest::TestAllExtensions::default_instance().GetReflection();
726 
727  const FieldDescriptor* extension1 =
728  unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
729  "optional_int32_extension");
730  const FieldDescriptor* extension2 =
731  unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
732  "repeated_string_extension");
733 
734  EXPECT_EQ(extension1,
735  reflection->FindKnownExtensionByName(extension1->full_name()));
736  EXPECT_EQ(extension2,
737  reflection->FindKnownExtensionByName(extension2->full_name()));
738 
739  // Non-existent extension.
740  EXPECT_TRUE(reflection->FindKnownExtensionByName("no_such_ext") == nullptr);
741 
742  // Extensions of TestAllExtensions should not show up as extensions of
743  // other types.
744  EXPECT_TRUE(unittest::TestAllTypes::default_instance()
745  .GetReflection()
746  ->FindKnownExtensionByName(extension1->full_name()) ==
747  nullptr);
748 }
749 
750 
751 TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) {
752  unittest::TestAllTypes from_message1;
753  unittest::TestAllTypes from_message2;
754  unittest::TestAllTypes to_message;
755  TestUtil::ReflectionTester reflection_tester(
757  reflection_tester.SetAllFieldsViaReflection(&from_message1);
758  reflection_tester.SetAllFieldsViaReflection(&from_message2);
759 
760  // Before moving fields, we expect the nested messages to be nullptr.
761  reflection_tester.ExpectMessagesReleasedViaReflection(
763 
764  // After fields are moved we should get non-nullptr releases.
765  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
766  &from_message1, &to_message);
767  reflection_tester.ExpectMessagesReleasedViaReflection(
769 
770  // Another move to make sure that we can SetAllocated several times.
771  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
772  &from_message2, &to_message);
773  reflection_tester.ExpectMessagesReleasedViaReflection(
775 
776  // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
777  // releases to be nullptr again.
778  reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
779  &to_message);
780  reflection_tester.ExpectMessagesReleasedViaReflection(
782 }
783 
784 TEST(GeneratedMessageReflectionTest, SetAllocatedMessageOnArenaTest) {
785  unittest::TestAllTypes from_message1;
786  unittest::TestAllTypes from_message2;
787  Arena arena;
788  unittest::TestAllTypes* to_message =
789  Arena::CreateMessage<unittest::TestAllTypes>(&arena);
790  TestUtil::ReflectionTester reflection_tester(
792  reflection_tester.SetAllFieldsViaReflection(&from_message1);
793  reflection_tester.SetAllFieldsViaReflection(&from_message2);
794 
795  // Before moving fields, we expect the nested messages to be nullptr.
796  reflection_tester.ExpectMessagesReleasedViaReflection(
798 
799  // After fields are moved we should get non-nullptr releases.
800  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
801  &from_message1, to_message);
802  reflection_tester.ExpectMessagesReleasedViaReflection(
804 
805  // Another move to make sure that we can SetAllocated several times.
806  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
807  &from_message2, to_message);
808  reflection_tester.ExpectMessagesReleasedViaReflection(
810 
811  // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
812  // releases to be nullptr again.
813  reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
814  to_message);
815  reflection_tester.ExpectMessagesReleasedViaReflection(
817 }
818 
819 TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageTest) {
820  unittest::TestAllExtensions from_message1;
821  unittest::TestAllExtensions from_message2;
822  unittest::TestAllExtensions to_message;
823  TestUtil::ReflectionTester reflection_tester(
825  reflection_tester.SetAllFieldsViaReflection(&from_message1);
826  reflection_tester.SetAllFieldsViaReflection(&from_message2);
827 
828  // Before moving fields, we expect the nested messages to be nullptr.
829  reflection_tester.ExpectMessagesReleasedViaReflection(
831 
832  // After fields are moved we should get non-nullptr releases.
833  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
834  &from_message1, &to_message);
835  reflection_tester.ExpectMessagesReleasedViaReflection(
837 
838  // Another move to make sure that we can SetAllocated several times.
839  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
840  &from_message2, &to_message);
841  reflection_tester.ExpectMessagesReleasedViaReflection(
843 
844  // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
845  // releases to be nullptr again.
846  reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
847  &to_message);
848  reflection_tester.ExpectMessagesReleasedViaReflection(
850 }
851 
852 TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageOnArenaTest) {
853  Arena arena;
854  unittest::TestAllExtensions* to_message =
855  Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
856  unittest::TestAllExtensions from_message1;
857  unittest::TestAllExtensions from_message2;
858  TestUtil::ReflectionTester reflection_tester(
860  reflection_tester.SetAllFieldsViaReflection(&from_message1);
861  reflection_tester.SetAllFieldsViaReflection(&from_message2);
862 
863  // Before moving fields, we expect the nested messages to be nullptr.
864  reflection_tester.ExpectMessagesReleasedViaReflection(
866 
867  // After fields are moved we should get non-nullptr releases.
868  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
869  &from_message1, to_message);
870  reflection_tester.ExpectMessagesReleasedViaReflection(
872 
873  // Another move to make sure that we can SetAllocated several times.
874  reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
875  &from_message2, to_message);
876  reflection_tester.ExpectMessagesReleasedViaReflection(
878 
879  // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
880  // releases to be nullptr again.
881  reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
882  to_message);
883  reflection_tester.ExpectMessagesReleasedViaReflection(
885 }
886 
887 TEST(GeneratedMessageReflectionTest, AddRepeatedMessage) {
888  unittest::TestAllTypes message;
889 
890  const Reflection* reflection = message.GetReflection();
891  const Reflection* nested_reflection =
892  unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
893 
894  const FieldDescriptor* nested_bb =
896  "bb");
897 
898  Message* nested =
899  reflection->AddMessage(&message, F("repeated_nested_message"));
900  nested_reflection->SetInt32(nested, nested_bb, 11);
901 
902  EXPECT_EQ(11, message.repeated_nested_message(0).bb());
903 }
904 
905 TEST(GeneratedMessageReflectionTest, MutableRepeatedMessage) {
906  unittest::TestAllTypes message;
907 
908  const Reflection* reflection = message.GetReflection();
909  const Reflection* nested_reflection =
910  unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
911 
912  const FieldDescriptor* nested_bb =
914  "bb");
915 
916  message.add_repeated_nested_message()->set_bb(12);
917 
918  Message* nested = reflection->MutableRepeatedMessage(
919  &message, F("repeated_nested_message"), 0);
920  EXPECT_EQ(12, nested_reflection->GetInt32(*nested, nested_bb));
921  nested_reflection->SetInt32(nested, nested_bb, 13);
922  EXPECT_EQ(13, message.repeated_nested_message(0).bb());
923 }
924 
925 TEST(GeneratedMessageReflectionTest, AddAllocatedMessage) {
926  unittest::TestAllTypes message;
927 
928  const Reflection* reflection = message.GetReflection();
929 
930  unittest::TestAllTypes::NestedMessage* nested =
931  new unittest::TestAllTypes::NestedMessage();
932  nested->set_bb(11);
933  reflection->AddAllocatedMessage(&message, F("repeated_nested_message"),
934  nested);
935  EXPECT_EQ(1, message.repeated_nested_message_size());
936  EXPECT_EQ(11, message.repeated_nested_message(0).bb());
937 }
938 
939 TEST(GeneratedMessageReflectionTest, ListFieldsOneOf) {
940  unittest::TestOneof2 message;
941  TestUtil::SetOneof1(&message);
942 
943  const Reflection* reflection = message.GetReflection();
944  std::vector<const FieldDescriptor*> fields;
945  reflection->ListFields(message, &fields);
946  EXPECT_EQ(4, fields.size());
947 }
948 
949 TEST(GeneratedMessageReflectionTest, Oneof) {
950  unittest::TestOneof2 message;
951  const Descriptor* descriptor = message.GetDescriptor();
952  const Reflection* reflection = message.GetReflection();
953 
954  // Check default values.
955  EXPECT_EQ(
956  0, reflection->GetInt32(message, descriptor->FindFieldByName("foo_int")));
957  EXPECT_EQ("", reflection->GetString(
958  message, descriptor->FindFieldByName("foo_string")));
959  EXPECT_EQ("", reflection->GetString(message,
960  descriptor->FindFieldByName("foo_cord")));
961  EXPECT_EQ("", reflection->GetString(
962  message, descriptor->FindFieldByName("foo_string_piece")));
963  EXPECT_EQ("", reflection->GetString(
964  message, descriptor->FindFieldByName("foo_bytes")));
965  EXPECT_EQ(
966  unittest::TestOneof2::FOO,
967  reflection->GetEnum(message, descriptor->FindFieldByName("foo_enum"))
968  ->number());
969  EXPECT_EQ(&unittest::TestOneof2::NestedMessage::default_instance(),
970  &reflection->GetMessage(
971  message, descriptor->FindFieldByName("foo_message")));
972  EXPECT_EQ(&unittest::TestOneof2::FooGroup::default_instance(),
973  &reflection->GetMessage(message,
974  descriptor->FindFieldByName("foogroup")));
975  EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(),
976  &reflection->GetMessage(
977  message, descriptor->FindFieldByName("foo_lazy_message")));
978  EXPECT_EQ(
979  5, reflection->GetInt32(message, descriptor->FindFieldByName("bar_int")));
980  EXPECT_EQ("STRING", reflection->GetString(
981  message, descriptor->FindFieldByName("bar_string")));
982  EXPECT_EQ("CORD", reflection->GetString(
983  message, descriptor->FindFieldByName("bar_cord")));
984  EXPECT_EQ("SPIECE",
985  reflection->GetString(
986  message, descriptor->FindFieldByName("bar_string_piece")));
987  EXPECT_EQ("BYTES", reflection->GetString(
988  message, descriptor->FindFieldByName("bar_bytes")));
989  EXPECT_EQ(
990  unittest::TestOneof2::BAR,
991  reflection->GetEnum(message, descriptor->FindFieldByName("bar_enum"))
992  ->number());
993 
994  // Check Set functions.
995  reflection->SetInt32(&message, descriptor->FindFieldByName("foo_int"), 123);
996  EXPECT_EQ(123, reflection->GetInt32(message,
997  descriptor->FindFieldByName("foo_int")));
998  reflection->SetString(&message, descriptor->FindFieldByName("foo_string"),
999  "abc");
1000  EXPECT_EQ("abc", reflection->GetString(
1001  message, descriptor->FindFieldByName("foo_string")));
1002  reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes"),
1003  "bytes");
1004  EXPECT_EQ("bytes", reflection->GetString(
1005  message, descriptor->FindFieldByName("foo_bytes")));
1006  reflection->SetString(&message, descriptor->FindFieldByName("bar_cord"),
1007  "change_cord");
1008  EXPECT_EQ(
1009  "change_cord",
1010  reflection->GetString(message, descriptor->FindFieldByName("bar_cord")));
1011  reflection->SetString(&message,
1012  descriptor->FindFieldByName("bar_string_piece"),
1013  "change_spiece");
1014  EXPECT_EQ("change_spiece",
1015  reflection->GetString(
1016  message, descriptor->FindFieldByName("bar_string_piece")));
1017 
1018  message.clear_foo();
1019  message.clear_bar();
1020  TestUtil::ExpectOneofClear(message);
1021 }
1022 
1023 TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) {
1024  unittest::TestOneof2 from_message1;
1025  unittest::TestOneof2 from_message2;
1026  unittest::TestOneof2 to_message;
1028  const Reflection* reflection = to_message.GetReflection();
1029 
1030  Message* released = reflection->ReleaseMessage(
1031  &to_message, descriptor->FindFieldByName("foo_lazy_message"));
1032  EXPECT_TRUE(released == nullptr);
1033  released = reflection->ReleaseMessage(
1034  &to_message, descriptor->FindFieldByName("foo_message"));
1035  EXPECT_TRUE(released == nullptr);
1036 
1039 
1042  &to_message);
1043  const Message& sub_message = reflection->GetMessage(
1044  to_message, descriptor->FindFieldByName("foo_lazy_message"));
1045  (void)sub_message; // unused in somce configurations
1046  released = reflection->ReleaseMessage(
1047  &to_message, descriptor->FindFieldByName("foo_lazy_message"));
1048  EXPECT_TRUE(released != nullptr);
1049  EXPECT_EQ(&sub_message, released);
1050  delete released;
1051 
1053 
1054  reflection->MutableMessage(&from_message2,
1055  descriptor->FindFieldByName("foo_message"));
1056 
1059  &to_message);
1060 
1061  const Message& sub_message2 = reflection->GetMessage(
1062  to_message, descriptor->FindFieldByName("foo_message"));
1063  (void)sub_message2; // unused in somce configurations
1064  released = reflection->ReleaseMessage(
1065  &to_message, descriptor->FindFieldByName("foo_message"));
1066  EXPECT_TRUE(released != nullptr);
1067  EXPECT_EQ(&sub_message2, released);
1068  delete released;
1069 }
1070 
1071 TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) {
1072  unittest::TestOneof2 from_message1;
1073  unittest::TestOneof2 from_message2;
1074  Arena arena;
1075  unittest::TestOneof2* to_message =
1076  Arena::CreateMessage<unittest::TestOneof2>(&arena);
1078  const Reflection* reflection = to_message->GetReflection();
1079 
1080  Message* released = reflection->ReleaseMessage(
1081  to_message, descriptor->FindFieldByName("foo_lazy_message"));
1082  EXPECT_TRUE(released == nullptr);
1083  released = reflection->ReleaseMessage(
1084  to_message, descriptor->FindFieldByName("foo_message"));
1085  EXPECT_TRUE(released == nullptr);
1086 
1089 
1092  to_message);
1093  const Message& sub_message = reflection->GetMessage(
1094  *to_message, descriptor->FindFieldByName("foo_lazy_message"));
1095  released = reflection->ReleaseMessage(
1096  to_message, descriptor->FindFieldByName("foo_lazy_message"));
1097  EXPECT_TRUE(released != nullptr);
1098  // Since sub_message is arena allocated, releasing it results in copying it
1099  // into new heap-allocated memory.
1100  EXPECT_NE(&sub_message, released);
1101  delete released;
1102 
1104 
1105  reflection->MutableMessage(&from_message2,
1106  descriptor->FindFieldByName("foo_message"));
1107 
1110  to_message);
1111 
1112  const Message& sub_message2 = reflection->GetMessage(
1113  *to_message, descriptor->FindFieldByName("foo_message"));
1114  released = reflection->ReleaseMessage(
1115  to_message, descriptor->FindFieldByName("foo_message"));
1116  EXPECT_TRUE(released != nullptr);
1117  // Since sub_message2 is arena allocated, releasing it results in copying it
1118  // into new heap-allocated memory.
1119  EXPECT_NE(&sub_message2, released);
1120  delete released;
1121 }
1122 
1123 TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) {
1124  unittest::TestAllTypes message;
1125  TestUtil::ReflectionTester reflection_tester(
1127 
1128  // When nothing is set, we expect all released messages to be nullptr.
1129  reflection_tester.ExpectMessagesReleasedViaReflection(
1131 
1132  // After fields are set we should get non-nullptr releases.
1133  reflection_tester.SetAllFieldsViaReflection(&message);
1134  reflection_tester.ExpectMessagesReleasedViaReflection(
1136 
1137  // After Clear() we may or may not get a message from ReleaseMessage().
1138  // This is implementation specific.
1139  reflection_tester.SetAllFieldsViaReflection(&message);
1140  message.Clear();
1141  reflection_tester.ExpectMessagesReleasedViaReflection(
1143 
1144  // Test a different code path for setting after releasing.
1147 }
1148 
1149 TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) {
1150  unittest::TestAllExtensions message;
1151  TestUtil::ReflectionTester reflection_tester(
1153 
1154  // When nothing is set, we expect all released messages to be nullptr.
1155  reflection_tester.ExpectMessagesReleasedViaReflection(
1157 
1158  // After fields are set we should get non-nullptr releases.
1159  reflection_tester.SetAllFieldsViaReflection(&message);
1160  reflection_tester.ExpectMessagesReleasedViaReflection(
1162 
1163  // After Clear() we may or may not get a message from ReleaseMessage().
1164  // This is implementation specific.
1165  reflection_tester.SetAllFieldsViaReflection(&message);
1166  message.Clear();
1167  reflection_tester.ExpectMessagesReleasedViaReflection(
1169 
1170  // Test a different code path for setting after releasing.
1172  TestUtil::ExpectAllExtensionsSet(message);
1173 }
1174 
1175 TEST(GeneratedMessageReflectionTest, ReleaseOneofMessageTest) {
1176  unittest::TestOneof2 message;
1178 
1180  const Reflection* reflection = message.GetReflection();
1181  const Message& sub_message = reflection->GetMessage(
1182  message, descriptor->FindFieldByName("foo_lazy_message"));
1183  (void)sub_message; // unused in somce configurations
1184  Message* released = reflection->ReleaseMessage(
1185  &message, descriptor->FindFieldByName("foo_lazy_message"));
1186 
1187  EXPECT_TRUE(released != nullptr);
1188  EXPECT_EQ(&sub_message, released);
1189  delete released;
1190 
1191  released = reflection->ReleaseMessage(
1192  &message, descriptor->FindFieldByName("foo_lazy_message"));
1193  EXPECT_TRUE(released == nullptr);
1194 }
1195 
1196 TEST(GeneratedMessageReflectionTest, ArenaReleaseMessageTest) {
1197  Arena arena;
1198  unittest::TestAllTypes* message =
1199  Arena::CreateMessage<unittest::TestAllTypes>(&arena);
1200  TestUtil::ReflectionTester reflection_tester(
1202 
1203  // When nothing is set, we expect all released messages to be nullptr.
1204  reflection_tester.ExpectMessagesReleasedViaReflection(
1206 
1207  // After fields are set we should get non-nullptr releases.
1208  reflection_tester.SetAllFieldsViaReflection(message);
1209  reflection_tester.ExpectMessagesReleasedViaReflection(
1211 
1212  // After Clear() we may or may not get a message from ReleaseMessage().
1213  // This is implementation specific.
1214  reflection_tester.SetAllFieldsViaReflection(message);
1215  message->Clear();
1216  reflection_tester.ExpectMessagesReleasedViaReflection(
1218 }
1219 
1220 TEST(GeneratedMessageReflectionTest, ArenaReleaseExtensionMessageTest) {
1221  Arena arena;
1222  unittest::TestAllExtensions* message =
1223  Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
1224  TestUtil::ReflectionTester reflection_tester(
1226 
1227  // When nothing is set, we expect all released messages to be nullptr.
1228  reflection_tester.ExpectMessagesReleasedViaReflection(
1230 
1231  // After fields are set we should get non-nullptr releases.
1232  reflection_tester.SetAllFieldsViaReflection(message);
1233  reflection_tester.ExpectMessagesReleasedViaReflection(
1235 
1236  // After Clear() we may or may not get a message from ReleaseMessage().
1237  // This is implementation specific.
1238  reflection_tester.SetAllFieldsViaReflection(message);
1239  message->Clear();
1240  reflection_tester.ExpectMessagesReleasedViaReflection(
1242 }
1243 
1244 TEST(GeneratedMessageReflectionTest, ArenaReleaseOneofMessageTest) {
1245  Arena arena;
1246  unittest::TestOneof2* message =
1247  Arena::CreateMessage<unittest::TestOneof2>(&arena);
1249 
1251  const Reflection* reflection = message->GetReflection();
1252  Message* released = reflection->ReleaseMessage(
1253  message, descriptor->FindFieldByName("foo_lazy_message"));
1254 
1255  EXPECT_TRUE(released != nullptr);
1256  delete released;
1257 
1258  released = reflection->ReleaseMessage(
1259  message, descriptor->FindFieldByName("foo_lazy_message"));
1260  EXPECT_TRUE(released == nullptr);
1261 }
1262 
1263 #ifdef PROTOBUF_HAS_DEATH_TEST
1264 
1265 TEST(GeneratedMessageReflectionTest, UsageErrors) {
1266  unittest::TestAllTypes message;
1267  const Reflection* reflection = message.GetReflection();
1268  const Descriptor* descriptor = message.GetDescriptor();
1269 
1270 #define f(NAME) descriptor->FindFieldByName(NAME)
1271 
1272  // Testing every single failure mode would be too much work. Let's just
1273  // check a few.
1274  EXPECT_DEATH(
1275  reflection->GetInt32(message,
1276  descriptor->FindFieldByName("optional_int64")),
1277  "Protocol Buffer reflection usage error:\n"
1278  " Method : google::protobuf::Reflection::GetInt32\n"
1279  " Message type: protobuf_unittest\\.TestAllTypes\n"
1280  " Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
1281  " Problem : Field is not the right type for this message:\n"
1282  " Expected : CPPTYPE_INT32\n"
1283  " Field type: CPPTYPE_INT64");
1284  EXPECT_DEATH(reflection->GetInt32(
1285  message, descriptor->FindFieldByName("repeated_int32")),
1286  "Protocol Buffer reflection usage error:\n"
1287  " Method : google::protobuf::Reflection::GetInt32\n"
1288  " Message type: protobuf_unittest.TestAllTypes\n"
1289  " Field : protobuf_unittest.TestAllTypes.repeated_int32\n"
1290  " Problem : Field is repeated; the method requires a "
1291  "singular field.");
1292  EXPECT_DEATH(
1293  reflection->GetInt32(
1294  message,
1295  unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
1296  "Protocol Buffer reflection usage error:\n"
1297  " Method : google::protobuf::Reflection::GetInt32\n"
1298  " Message type: protobuf_unittest.TestAllTypes\n"
1299  " Field : protobuf_unittest.ForeignMessage.c\n"
1300  " Problem : Field does not match message type.");
1301  EXPECT_DEATH(
1302  reflection->HasField(
1303  message,
1304  unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
1305  "Protocol Buffer reflection usage error:\n"
1306  " Method : google::protobuf::Reflection::HasField\n"
1307  " Message type: protobuf_unittest.TestAllTypes\n"
1308  " Field : protobuf_unittest.ForeignMessage.c\n"
1309  " Problem : Field does not match message type.");
1310 
1311 #undef f
1312 }
1313 
1314 #endif // PROTOBUF_HAS_DEATH_TEST
1315 
1316 
1317 } // namespace
1318 } // namespace protobuf
1319 } // namespace google
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
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::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::GeneratedMessageReflectionTestHelper::IsLazyExtension
static bool IsLazyExtension(const Message &msg, const FieldDescriptor *ext)
Definition: protobuf/src/google/protobuf/generated_message_reflection_unittest.cc:75
tests.google.protobuf.internal.test_util.SetAllExtensions
def SetAllExtensions(message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:187
google::protobuf::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(UseArena, DynamicMessageTest, ::testing::Bool())
ext
void * ext
Definition: x509v3.h:87
google::protobuf::GeneratedMessageReflectionTestHelper::UnsafeShallowSwapFields
static void UnsafeShallowSwapFields(Message *lhs, Message *rhs, const std::vector< const FieldDescriptor * > &fields)
Definition: protobuf/src/google/protobuf/generated_message_reflection_unittest.cc:70
google::protobuf::Message::GetReflection
const Reflection * GetReflection() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:332
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::Reflection::UnsafeShallowSwapFields
void UnsafeShallowSwapFields(Message *message1, Message *message2, const std::vector< const FieldDescriptor * > &fields) const
Definition: protobuf/src/google/protobuf/generated_message_reflection.cc:1045
setup.name
name
Definition: setup.py:542
google::protobuf::GeneratedMessageReflectionTestHelper
Definition: protobuf/src/google/protobuf/generated_message_reflection_unittest.cc:68
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
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
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
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
Oneof
struct Oneof Oneof
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:664
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
google::protobuf::TEST_P
TEST_P(DynamicMessageTest, IndependentOffsets)
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message_unittest.cc:143
google::protobuf::TestUtil::ReflectionTester::IS_NULL
@ IS_NULL
Definition: bloaty/third_party/protobuf/src/google/protobuf/test_util.h:83
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
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::TestUtil::ReflectionTester::ExpectOneofSetViaReflection
static void ExpectOneofSetViaReflection(const Message &message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/test_util.h:360
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_unittest.cc:156
F
#define F(b, c, d)
Definition: md4.c:112
google::protobuf::TestUtil::ReflectionTester::CAN_BE_NULL
@ CAN_BE_NULL
Definition: bloaty/third_party/protobuf/src/google/protobuf/test_util.h:84
google::protobuf::TestUtil::ReflectionTester::SetAllocatedOptionalMessageFieldsToMessageViaReflection
static void SetAllocatedOptionalMessageFieldsToMessageViaReflection(Message *from_message, Message *to_message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/test_util.h:1175
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
nested
static int nested
Definition: test-callback-stack.c:39
google::protobuf::TestUtil::ReflectionTester::NOT_NULL
@ NOT_NULL
Definition: bloaty/third_party/protobuf/src/google/protobuf/test_util.h:85
google::protobuf::Message
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:205
scratch
static char scratch[256]
Definition: test-random.c:27
google::protobuf::TestUtil::ReflectionTester::SetOneofViaReflection
static void SetOneofViaReflection(Message *message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/test_util.h:343
profile_analyzer.fields
list fields
Definition: profile_analyzer.py:266
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:153
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
testing::Bool
internal::ParamGenerator< bool > Bool()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:359
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
Message
Definition: protobuf/php/ext/google/protobuf/message.c:53
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:26