1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // Author: kenton@google.com (Kenton Varda)
9 // Based on original Protocol Buffers design by
10 // Sanjay Ghemawat, Jeff Dean, and others.
11 //
12 // To test GeneratedMessageReflection, we actually let the protocol compiler
13 // generate a full protocol message implementation and then test its
14 // reflection interface. This is much easier and more maintainable than
15 // trying to create our own Message class for GeneratedMessageReflection
16 // to wrap.
17 //
18 // The tests here closely mirror some of the tests in
19 // compiler/cpp/unittest, except using the reflection interface
20 // rather than generated accessors.
21
22 #include "google/protobuf/generated_message_reflection.h"
23
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30 #include "absl/flags/flag.h"
31 #include "absl/log/absl_check.h"
32 #include "absl/strings/cord.h"
33 #include "absl/strings/string_view.h"
34 #include "google/protobuf/arena.h"
35 #include "google/protobuf/descriptor.h"
36 #include "google/protobuf/map_test_util.h"
37 #include "google/protobuf/map_unittest.pb.h"
38 #include "google/protobuf/message.h"
39 #include "google/protobuf/port.h"
40 #include "google/protobuf/test_util.h"
41 #include "google/protobuf/unittest.pb.h"
42 #include "google/protobuf/unittest_mset.pb.h"
43 #include "google/protobuf/unittest_mset_wire_format.pb.h"
44 #include "google/protobuf/unittest_proto3.pb.h"
45
46 // Must be included last.
47 #include "google/protobuf/port_def.inc"
48
49 namespace google {
50 namespace protobuf {
51
52 class GeneratedMessageReflectionTestHelper {
53 public:
UnsafeShallowSwapFields(Message * lhs,Message * rhs,const std::vector<const FieldDescriptor * > & fields)54 static void UnsafeShallowSwapFields(
55 Message* lhs, Message* rhs,
56 const std::vector<const FieldDescriptor*>& fields) {
57 lhs->GetReflection()->UnsafeShallowSwapFields(lhs, rhs, fields);
58 }
IsLazyExtension(const Message & msg,const FieldDescriptor * ext)59 static bool IsLazyExtension(const Message& msg, const FieldDescriptor* ext) {
60 return msg.GetReflection()->IsLazyExtension(msg, ext);
61 }
IsLazyField(const Message & msg,const FieldDescriptor * field)62 static bool IsLazyField(const Message& msg, const FieldDescriptor* field) {
63 return msg.GetReflection()->IsLazyField(field);
64 }
IsEagerlyVerifiedLazyField(const Message & msg,const FieldDescriptor * field)65 static bool IsEagerlyVerifiedLazyField(const Message& msg,
66 const FieldDescriptor* field) {
67 return msg.GetReflection()->IsEagerlyVerifiedLazyField(field);
68 }
IsLazilyVerifiedLazyField(const Message & msg,const FieldDescriptor * field)69 static bool IsLazilyVerifiedLazyField(const Message& msg,
70 const FieldDescriptor* field) {
71 return msg.GetReflection()->IsLazilyVerifiedLazyField(field);
72 }
73 template <typename T>
GetRaw(const Message & msg,const FieldDescriptor * field)74 static const T& GetRaw(const Message& msg, const FieldDescriptor* field) {
75 const Reflection* reflection = msg.GetReflection();
76 return reflection->GetRaw<T>(msg, field);
77 }
78 };
79
80 namespace {
81
82 using ::testing::ElementsAre;
83 using ::testing::Pointee;
84 using ::testing::Property;
85
86 // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
F(const std::string & name)87 const FieldDescriptor* F(const std::string& name) {
88 const FieldDescriptor* result =
89 unittest::TestAllTypes::descriptor()->FindFieldByName(name);
90 ABSL_CHECK(result != nullptr);
91 return result;
92 }
93
TEST(GeneratedMessageReflectionTest,Defaults)94 TEST(GeneratedMessageReflectionTest, Defaults) {
95 // Check that all default values are set correctly in the initial message.
96 unittest::TestAllTypes message;
97 TestUtil::ReflectionTester reflection_tester(
98 unittest::TestAllTypes::descriptor());
99
100 reflection_tester.ExpectClearViaReflection(message);
101
102 const Reflection* reflection = message.GetReflection();
103
104 // Messages should return pointers to default instances until first use.
105 // (This is not checked by ExpectClear() since it is not actually true after
106 // the fields have been set and then cleared.)
107 EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
108 &reflection->GetMessage(message, F("optionalgroup")));
109 EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
110 &reflection->GetMessage(message, F("optional_nested_message")));
111 EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
112 &reflection->GetMessage(message, F("optional_foreign_message")));
113 EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
114 &reflection->GetMessage(message, F("optional_import_message")));
115 }
116
TEST(GeneratedMessageReflectionTest,Accessors)117 TEST(GeneratedMessageReflectionTest, Accessors) {
118 // Set every field to a unique value then go back and check all those
119 // values.
120 unittest::TestAllTypes message;
121 TestUtil::ReflectionTester reflection_tester(
122 unittest::TestAllTypes::descriptor());
123
124 reflection_tester.SetAllFieldsViaReflection(&message);
125 TestUtil::ExpectAllFieldsSet(message);
126 reflection_tester.ExpectAllFieldsSetViaReflection(message);
127
128 reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
129 TestUtil::ExpectRepeatedFieldsModified(message);
130 }
131
TEST(GeneratedMessageReflectionTest,GetStringReference)132 TEST(GeneratedMessageReflectionTest, GetStringReference) {
133 // Test that GetStringReference() returns the underlying string when it
134 // is a normal string field.
135 unittest::TestAllTypes message;
136 message.set_optional_string("foo");
137 message.add_repeated_string("foo");
138
139 const Reflection* reflection = message.GetReflection();
140 std::string scratch;
141
142 EXPECT_EQ(
143 &message.optional_string(),
144 &reflection->GetStringReference(message, F("optional_string"), &scratch))
145 << "For simple string fields, GetStringReference() should return a "
146 "reference to the underlying string.";
147 EXPECT_EQ(&message.repeated_string(0),
148 &reflection->GetRepeatedStringReference(
149 message, F("repeated_string"), 0, &scratch))
150 << "For simple string fields, GetRepeatedStringReference() should "
151 "return "
152 "a reference to the underlying string.";
153 }
154
TEST(GeneratedMessageReflectionTest,GetStringReferenceCopy)155 TEST(GeneratedMessageReflectionTest, GetStringReferenceCopy) {
156 // Test that GetStringReference() returns the scratch string when the
157 // underlying representation is not a normal string.
158 unittest::TestCord cord_message;
159 cord_message.set_optional_bytes_cord("bytes_cord");
160
161 const Reflection* cord_reflection = cord_message.GetReflection();
162 const Descriptor* descriptor = unittest::TestCord::descriptor();
163 std::string cord_scratch;
164 EXPECT_EQ(
165 &cord_scratch,
166 &cord_reflection->GetStringReference(
167 cord_message, descriptor->FindFieldByName("optional_bytes_cord"),
168 &cord_scratch));
169 }
170
TEST(GeneratedMessageReflectionTest,GetStringView)171 TEST(GeneratedMessageReflectionTest, GetStringView) {
172 unittest::TestAllTypes message;
173 TestUtil::SetAllFields(&message);
174
175 const Reflection* reflection = message.GetReflection();
176 Reflection::ScratchSpace scratch;
177
178 EXPECT_EQ("115",
179 reflection->GetStringView(message, F("optional_string"), scratch));
180 EXPECT_EQ("124", reflection->GetStringView(
181 message, F("optional_string_piece"), scratch));
182 EXPECT_EQ("125",
183 reflection->GetStringView(message, F("optional_cord"), scratch));
184 }
185
TEST(GeneratedMessageReflectionTest,GetStringViewWithExtensions)186 TEST(GeneratedMessageReflectionTest, GetStringViewWithExtensions) {
187 unittest::TestAllExtensions message;
188 google::protobuf::FileDescriptor const* descriptor_file =
189 message.GetDescriptor()->file();
190 google::protobuf::FieldDescriptor const* string_ext =
191 descriptor_file->FindExtensionByName("optional_string_extension");
192 google::protobuf::FieldDescriptor const* string_piece_ext =
193 descriptor_file->FindExtensionByName("optional_string_piece_extension");
194 google::protobuf::FieldDescriptor const* cord_ext =
195 descriptor_file->FindExtensionByName("optional_cord_extension");
196 message.SetExtension(protobuf_unittest::optional_string_extension, "foo");
197 message.SetExtension(protobuf_unittest::optional_string_piece_extension, "bar");
198 message.SetExtension(protobuf_unittest::optional_cord_extension, "baz");
199 const Reflection* reflection = message.GetReflection();
200 Reflection::ScratchSpace scratch;
201
202 EXPECT_EQ("foo", reflection->GetStringView(message, string_ext, scratch));
203 EXPECT_EQ("bar",
204 reflection->GetStringView(message, string_piece_ext, scratch));
205 EXPECT_EQ("baz", reflection->GetStringView(message, cord_ext, scratch));
206 }
207
TEST(GeneratedMessageReflectionTest,GetStringViewWithOneof)208 TEST(GeneratedMessageReflectionTest, GetStringViewWithOneof) {
209 unittest::TestOneof2 message;
210 const Reflection* reflection = message.GetReflection();
211 const FieldDescriptor* string_field =
212 message.GetDescriptor()->FindFieldByName("foo_string");
213 const FieldDescriptor* string_piece_field =
214 message.GetDescriptor()->FindFieldByName("foo_string_piece");
215 Reflection::ScratchSpace scratch;
216
217 message.set_foo_string("foo");
218 EXPECT_EQ("foo", reflection->GetStringView(message, string_field, scratch));
219 EXPECT_EQ("",
220 reflection->GetStringView(message, string_piece_field, scratch));
221
222 }
223
TEST(GeneratedMessageReflectionTest,GetRepeatedStringView)224 TEST(GeneratedMessageReflectionTest, GetRepeatedStringView) {
225 unittest::TestAllTypes message;
226 TestUtil::AddRepeatedFields1(&message);
227 TestUtil::AddRepeatedFields2(&message);
228
229 const Reflection* reflection = message.GetReflection();
230 Reflection::ScratchSpace scratch;
231
232 EXPECT_EQ("215", reflection->GetRepeatedStringView(
233 message, F("repeated_string"), 0, scratch));
234 EXPECT_EQ("224", reflection->GetRepeatedStringView(
235 message, F("repeated_string_piece"), 0, scratch));
236 EXPECT_EQ("225", reflection->GetRepeatedStringView(
237 message, F("repeated_cord"), 0, scratch));
238 }
239
TEST(GeneratedMessageReflectionTest,GetRepeatedStringViewWithExtensions)240 TEST(GeneratedMessageReflectionTest, GetRepeatedStringViewWithExtensions) {
241 unittest::TestAllExtensions message;
242 google::protobuf::FileDescriptor const* descriptor_file =
243 message.GetDescriptor()->file();
244 google::protobuf::FieldDescriptor const* string_ext =
245 descriptor_file->FindExtensionByName("repeated_string_extension");
246 google::protobuf::FieldDescriptor const* string_piece_ext =
247 descriptor_file->FindExtensionByName("repeated_string_piece_extension");
248 google::protobuf::FieldDescriptor const* cord_ext =
249 descriptor_file->FindExtensionByName("repeated_cord_extension");
250 message.AddExtension(protobuf_unittest::repeated_string_extension, "foo");
251 message.AddExtension(protobuf_unittest::repeated_string_piece_extension, "bar");
252 message.AddExtension(protobuf_unittest::repeated_cord_extension, "baz");
253 const Reflection* reflection = message.GetReflection();
254 Reflection::ScratchSpace scratch;
255
256 EXPECT_EQ("foo",
257 reflection->GetRepeatedStringView(message, string_ext, 0, scratch));
258 EXPECT_EQ("bar", reflection->GetRepeatedStringView(message, string_piece_ext,
259 0, scratch));
260 EXPECT_EQ("baz",
261 reflection->GetRepeatedStringView(message, cord_ext, 0, scratch));
262 }
263
264
265 class GeneratedMessageReflectionSwapTest : public testing::TestWithParam<bool> {
266 protected:
Swap(const Reflection * reflection,Message * lhs,Message * rhs)267 void Swap(const Reflection* reflection, Message* lhs, Message* rhs) {
268 if (GetParam()) {
269 reflection->UnsafeArenaSwap(lhs, rhs);
270 } else {
271 reflection->Swap(lhs, rhs);
272 }
273 }
SwapFields(const Reflection * reflection,Message * lhs,Message * rhs,const std::vector<const FieldDescriptor * > & fields)274 void SwapFields(const Reflection* reflection, Message* lhs, Message* rhs,
275 const std::vector<const FieldDescriptor*>& fields) {
276 if (GetParam()) {
277 reflection->UnsafeArenaSwapFields(lhs, rhs, fields);
278 } else {
279 reflection->SwapFields(lhs, rhs, fields);
280 }
281 }
282 };
283
284 // unsafe_shallow_swap: true -> UnsafeArena* API.
285 INSTANTIATE_TEST_SUITE_P(ReflectionSwap, GeneratedMessageReflectionSwapTest,
286 testing::Bool());
287
TEST_P(GeneratedMessageReflectionSwapTest,LhsSet)288 TEST_P(GeneratedMessageReflectionSwapTest, LhsSet) {
289 unittest::TestAllTypes lhs;
290 unittest::TestAllTypes rhs;
291
292 TestUtil::SetAllFields(&lhs);
293
294 Swap(lhs.GetReflection(), &lhs, &rhs);
295
296 TestUtil::ExpectClear(lhs);
297 TestUtil::ExpectAllFieldsSet(rhs);
298 }
299
TEST_P(GeneratedMessageReflectionSwapTest,BothSet)300 TEST_P(GeneratedMessageReflectionSwapTest, BothSet) {
301 unittest::TestAllTypes lhs;
302 unittest::TestAllTypes rhs;
303
304 TestUtil::SetAllFields(&lhs);
305 TestUtil::SetAllFields(&rhs);
306 TestUtil::ModifyRepeatedFields(&rhs);
307
308 const Reflection* reflection = lhs.GetReflection();
309 Swap(reflection, &lhs, &rhs);
310
311 TestUtil::ExpectRepeatedFieldsModified(lhs);
312 TestUtil::ExpectAllFieldsSet(rhs);
313
314 lhs.set_optional_int32(532819);
315
316 Swap(reflection, &lhs, &rhs);
317
318 EXPECT_EQ(532819, rhs.optional_int32());
319 }
320
TEST_P(GeneratedMessageReflectionSwapTest,LhsCleared)321 TEST_P(GeneratedMessageReflectionSwapTest, LhsCleared) {
322 unittest::TestAllTypes lhs;
323 unittest::TestAllTypes rhs;
324
325 TestUtil::SetAllFields(&lhs);
326
327 // For proto2 message, for message field, Clear only reset hasbits, but
328 // doesn't delete the underlying field.
329 lhs.Clear();
330
331 Swap(lhs.GetReflection(), &lhs, &rhs);
332
333 TestUtil::ExpectClear(rhs);
334 }
335
TEST_P(GeneratedMessageReflectionSwapTest,RhsCleared)336 TEST_P(GeneratedMessageReflectionSwapTest, RhsCleared) {
337 unittest::TestAllTypes lhs;
338 unittest::TestAllTypes rhs;
339
340 TestUtil::SetAllFields(&rhs);
341
342 // For proto2 message, for message field, Clear only reset hasbits, but
343 // doesn't delete the underlying field.
344 rhs.Clear();
345
346 Swap(lhs.GetReflection(), &lhs, &rhs);
347
348 TestUtil::ExpectClear(lhs);
349 }
350
TEST_P(GeneratedMessageReflectionSwapTest,Extensions)351 TEST_P(GeneratedMessageReflectionSwapTest, Extensions) {
352 unittest::TestAllExtensions lhs;
353 unittest::TestAllExtensions rhs;
354
355 TestUtil::SetAllExtensions(&lhs);
356
357 Swap(lhs.GetReflection(), &lhs, &rhs);
358
359 TestUtil::ExpectExtensionsClear(lhs);
360 TestUtil::ExpectAllExtensionsSet(rhs);
361 }
362
TEST_P(GeneratedMessageReflectionSwapTest,PackedExtensions)363 TEST_P(GeneratedMessageReflectionSwapTest, PackedExtensions) {
364 unittest::TestPackedExtensions lhs;
365 unittest::TestPackedExtensions rhs;
366
367 TestUtil::SetPackedExtensions(&lhs);
368
369 Swap(lhs.GetReflection(), &lhs, &rhs);
370
371 EXPECT_EQ(lhs.SerializeAsString(), "");
372
373 TestUtil::ExpectPackedExtensionsSet(rhs);
374 }
375
TEST_P(GeneratedMessageReflectionSwapTest,Unknown)376 TEST_P(GeneratedMessageReflectionSwapTest, Unknown) {
377 unittest::TestEmptyMessage lhs, rhs;
378
379 lhs.mutable_unknown_fields()->AddVarint(1234, 1);
380
381 EXPECT_EQ(1, lhs.unknown_fields().field_count());
382 EXPECT_EQ(0, rhs.unknown_fields().field_count());
383 Swap(lhs.GetReflection(), &lhs, &rhs);
384 EXPECT_EQ(0, lhs.unknown_fields().field_count());
385 EXPECT_EQ(1, rhs.unknown_fields().field_count());
386 }
387
TEST_P(GeneratedMessageReflectionSwapTest,Oneof)388 TEST_P(GeneratedMessageReflectionSwapTest, Oneof) {
389 unittest::TestOneof2 lhs, rhs;
390 TestUtil::SetOneof1(&lhs);
391
392 Swap(lhs.GetReflection(), &lhs, &rhs);
393
394 TestUtil::ExpectOneofClear(lhs);
395 TestUtil::ExpectOneofSet1(rhs);
396 }
397
TEST_P(GeneratedMessageReflectionSwapTest,OneofBothSet)398 TEST_P(GeneratedMessageReflectionSwapTest, OneofBothSet) {
399 unittest::TestOneof2 lhs, rhs;
400 TestUtil::SetOneof1(&lhs);
401 TestUtil::SetOneof2(&rhs);
402
403 Swap(lhs.GetReflection(), &lhs, &rhs);
404
405 TestUtil::ExpectOneofSet2(lhs);
406 TestUtil::ExpectOneofSet1(rhs);
407 }
408
TEST_P(GeneratedMessageReflectionSwapTest,SwapFields)409 TEST_P(GeneratedMessageReflectionSwapTest, SwapFields) {
410 std::unique_ptr<unittest::TestAllTypes> lhs(
411 Arena::Create<unittest::TestAllTypes>(nullptr));
412 std::unique_ptr<unittest::TestAllTypes> rhs(
413 Arena::Create<unittest::TestAllTypes>(nullptr));
414 lhs->set_optional_double(12.3);
415 lhs->mutable_repeated_int32()->Add(10);
416 lhs->mutable_repeated_int32()->Add(20);
417
418 rhs->set_optional_string("hello");
419 rhs->mutable_repeated_int64()->Add(30);
420
421 std::vector<const FieldDescriptor*> fields;
422 const Descriptor* descriptor = lhs->GetDescriptor();
423 fields.push_back(descriptor->FindFieldByName("optional_double"));
424 fields.push_back(descriptor->FindFieldByName("repeated_int32"));
425 fields.push_back(descriptor->FindFieldByName("optional_string"));
426 fields.push_back(descriptor->FindFieldByName("optional_uint64"));
427
428 SwapFields(lhs->GetReflection(), lhs.get(), rhs.get(), fields);
429
430 EXPECT_FALSE(lhs->has_optional_double());
431 EXPECT_EQ(0, lhs->repeated_int32_size());
432 EXPECT_TRUE(lhs->has_optional_string());
433 EXPECT_EQ("hello", lhs->optional_string());
434 EXPECT_EQ(0, lhs->repeated_int64_size());
435 EXPECT_FALSE(lhs->has_optional_uint64());
436
437 EXPECT_TRUE(rhs->has_optional_double());
438 EXPECT_EQ(12.3, rhs->optional_double());
439 EXPECT_EQ(2, rhs->repeated_int32_size());
440 EXPECT_EQ(10, rhs->repeated_int32(0));
441 EXPECT_EQ(20, rhs->repeated_int32(1));
442 EXPECT_FALSE(rhs->has_optional_string());
443 EXPECT_EQ(1, rhs->repeated_int64_size());
444 EXPECT_FALSE(rhs->has_optional_uint64());
445 }
446
TEST_P(GeneratedMessageReflectionSwapTest,SwapFieldsAll)447 TEST_P(GeneratedMessageReflectionSwapTest, SwapFieldsAll) {
448 std::unique_ptr<unittest::TestAllTypes> lhs(
449 Arena::Create<unittest::TestAllTypes>(nullptr));
450 std::unique_ptr<unittest::TestAllTypes> rhs(
451 Arena::Create<unittest::TestAllTypes>(nullptr));
452
453 TestUtil::SetAllFields(rhs.get());
454
455 std::vector<const FieldDescriptor*> fields;
456 const Reflection* reflection = lhs->GetReflection();
457 reflection->ListFields(*rhs, &fields);
458 SwapFields(reflection, lhs.get(), rhs.get(), fields);
459
460 TestUtil::ExpectAllFieldsSet(*lhs);
461 TestUtil::ExpectClear(*rhs);
462 }
463
TEST(GeneratedMessageReflectionTest,SwapFieldsAllOnDifferentArena)464 TEST(GeneratedMessageReflectionTest, SwapFieldsAllOnDifferentArena) {
465 Arena arena1, arena2;
466 auto* message1 = Arena::Create<unittest::TestAllTypes>(&arena1);
467 auto* message2 = Arena::Create<unittest::TestAllTypes>(&arena2);
468
469 TestUtil::SetAllFields(message2);
470
471 std::vector<const FieldDescriptor*> fields;
472 const Reflection* reflection = message1->GetReflection();
473 reflection->ListFields(*message2, &fields);
474 reflection->SwapFields(message1, message2, fields);
475
476 TestUtil::ExpectAllFieldsSet(*message1);
477 TestUtil::ExpectClear(*message2);
478 }
479
TEST(GeneratedMessageReflectionTest,SwapFieldsAllOnArenaHeap)480 TEST(GeneratedMessageReflectionTest, SwapFieldsAllOnArenaHeap) {
481 Arena arena;
482 auto* message1 = Arena::Create<unittest::TestAllTypes>(&arena);
483 std::unique_ptr<unittest::TestAllTypes> message2(
484 Arena::Create<unittest::TestAllTypes>(nullptr));
485
486 TestUtil::SetAllFields(message2.get());
487
488 std::vector<const FieldDescriptor*> fields;
489 const Reflection* reflection = message1->GetReflection();
490 reflection->ListFields(*message2, &fields);
491 reflection->SwapFields(message1, message2.get(), fields);
492
493 TestUtil::ExpectAllFieldsSet(*message1);
494 TestUtil::ExpectClear(*message2);
495 }
496
TEST(GeneratedMessageReflectionTest,SwapFieldsAllExtension)497 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtension) {
498 unittest::TestAllExtensions message1;
499 unittest::TestAllExtensions message2;
500
501 TestUtil::SetAllExtensions(&message1);
502
503 std::vector<const FieldDescriptor*> fields;
504 const Reflection* reflection = message1.GetReflection();
505 reflection->ListFields(message1, &fields);
506 reflection->SwapFields(&message1, &message2, fields);
507
508 TestUtil::ExpectExtensionsClear(message1);
509 TestUtil::ExpectAllExtensionsSet(message2);
510 }
511
TEST(GeneratedMessageReflectionTest,SwapFieldsAllExtensionArenaHeap)512 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtensionArenaHeap) {
513 Arena arena;
514
515 std::unique_ptr<unittest::TestAllExtensions> message1(
516 Arena::Create<unittest::TestAllExtensions>(nullptr));
517 auto* message2 = Arena::Create<unittest::TestAllExtensions>(&arena);
518
519 TestUtil::SetAllExtensions(message1.get());
520
521 std::vector<const FieldDescriptor*> fields;
522 const Reflection* reflection = message1->GetReflection();
523 reflection->ListFields(*message1, &fields);
524 reflection->SwapFields(message1.get(), message2, fields);
525
526 TestUtil::ExpectExtensionsClear(*message1);
527 TestUtil::ExpectAllExtensionsSet(*message2);
528 }
529
TEST(GeneratedMessageReflectionTest,UnsafeShallowSwapFieldsAll)530 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsAll) {
531 Arena arena;
532 auto* message1 = Arena::Create<unittest::TestAllTypes>(&arena);
533 auto* message2 = Arena::Create<unittest::TestAllTypes>(&arena);
534
535 TestUtil::SetAllFields(message2);
536
537 auto* kept_nested_message_ptr = message2->mutable_optional_nested_message();
538 auto* kept_foreign_message_ptr = message2->mutable_optional_foreign_message();
539 auto* kept_repeated_nested_message_ptr =
540 message2->mutable_repeated_nested_message(0);
541 auto* kept_repeated_foreign_message_ptr =
542 message2->mutable_repeated_foreign_message(0);
543
544 std::vector<const FieldDescriptor*> fields;
545 const Reflection* reflection = message1->GetReflection();
546 reflection->ListFields(*message2, &fields);
547 GeneratedMessageReflectionTestHelper::UnsafeShallowSwapFields(
548 message1, message2, fields);
549
550 TestUtil::ExpectAllFieldsSet(*message1);
551 TestUtil::ExpectClear(*message2);
552
553 // Expects the swap to be shallow. Expects pointer stability to the element of
554 // the repeated fields (not the container).
555 EXPECT_EQ(kept_nested_message_ptr,
556 message1->mutable_optional_nested_message());
557 EXPECT_EQ(kept_foreign_message_ptr,
558 message1->mutable_optional_foreign_message());
559 EXPECT_EQ(kept_repeated_nested_message_ptr,
560 message1->mutable_repeated_nested_message(0));
561 EXPECT_EQ(kept_repeated_foreign_message_ptr,
562 message1->mutable_repeated_foreign_message(0));
563 }
564
TEST(GeneratedMessageReflectionTest,UnsafeShallowSwapFieldsMap)565 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsMap) {
566 Arena arena;
567 auto* message1 = Arena::Create<unittest::TestMap>(&arena);
568 auto* message2 = Arena::Create<unittest::TestMap>(&arena);
569
570 MapTestUtil::SetMapFields(message2);
571
572 auto* kept_map_int32_fm_ptr =
573 &(*message2->mutable_map_int32_foreign_message())[0];
574
575 std::vector<const FieldDescriptor*> fields;
576 const Reflection* reflection = message1->GetReflection();
577 reflection->ListFields(*message2, &fields);
578 GeneratedMessageReflectionTestHelper::UnsafeShallowSwapFields(
579 message1, message2, fields);
580
581 MapTestUtil::ExpectMapFieldsSet(*message1);
582 MapTestUtil::ExpectClear(*message2);
583
584 // Expects the swap to be shallow.
585 EXPECT_EQ(kept_map_int32_fm_ptr,
586 &(*message1->mutable_map_int32_foreign_message())[0]);
587 }
588
TEST(GeneratedMessageReflectionTest,UnsafeShallowSwapFieldsAllExtension)589 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsAllExtension) {
590 Arena arena;
591 auto* message1 = Arena::Create<unittest::TestAllExtensions>(&arena);
592 auto* message2 = Arena::Create<unittest::TestAllExtensions>(&arena);
593
594 TestUtil::SetAllExtensions(message1);
595
596 auto* kept_nested_message_ext_ptr =
597 message1->MutableExtension(unittest::optional_nested_message_extension);
598 auto* kept_foreign_message_ext_ptr =
599 message1->MutableExtension(unittest::optional_foreign_message_extension);
600 auto* kept_repeated_nested_message_ext_ptr =
601 message1->MutableRepeatedExtension(
602 unittest::repeated_nested_message_extension);
603 auto* kept_repeated_foreign_message_ext_ptr =
604 message1->MutableRepeatedExtension(
605 unittest::repeated_foreign_message_extension);
606
607 std::vector<const FieldDescriptor*> fields;
608 const Reflection* reflection = message1->GetReflection();
609 reflection->ListFields(*message1, &fields);
610 GeneratedMessageReflectionTestHelper::UnsafeShallowSwapFields(
611 message1, message2, fields);
612
613 TestUtil::ExpectExtensionsClear(*message1);
614 TestUtil::ExpectAllExtensionsSet(*message2);
615
616 // Expects the swap to be shallow.
617 EXPECT_EQ(
618 kept_nested_message_ext_ptr,
619 message2->MutableExtension(unittest::optional_nested_message_extension));
620 EXPECT_EQ(
621 kept_foreign_message_ext_ptr,
622 message2->MutableExtension(unittest::optional_foreign_message_extension));
623 EXPECT_EQ(kept_repeated_nested_message_ext_ptr,
624 message2->MutableRepeatedExtension(
625 unittest::repeated_nested_message_extension));
626 EXPECT_EQ(kept_repeated_foreign_message_ext_ptr,
627 message2->MutableRepeatedExtension(
628 unittest::repeated_foreign_message_extension));
629 }
630
TEST(GeneratedMessageReflectionTest,SwapFieldsOneof)631 TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) {
632 unittest::TestOneof2 message1, message2;
633 TestUtil::SetOneof1(&message1);
634
635 std::vector<const FieldDescriptor*> fields;
636 const Descriptor* descriptor = message1.GetDescriptor();
637 for (int i = 0; i < descriptor->field_count(); i++) {
638 fields.push_back(descriptor->field(i));
639 }
640 const Reflection* reflection = message1.GetReflection();
641 reflection->SwapFields(&message1, &message2, fields);
642
643 TestUtil::ExpectOneofClear(message1);
644 TestUtil::ExpectOneofSet1(message2);
645 }
646
TEST(GeneratedMessageReflectionTest,UnsafeShallowSwapFieldsOneof)647 TEST(GeneratedMessageReflectionTest, UnsafeShallowSwapFieldsOneof) {
648 Arena arena;
649 auto* message1 = Arena::Create<unittest::TestOneof2>(&arena);
650 auto* message2 = Arena::Create<unittest::TestOneof2>(&arena);
651 TestUtil::SetOneof1(message1);
652
653 std::vector<const FieldDescriptor*> fields;
654 const Descriptor* descriptor = message1->GetDescriptor();
655 for (int i = 0; i < descriptor->field_count(); i++) {
656 fields.push_back(descriptor->field(i));
657 }
658 GeneratedMessageReflectionTestHelper::UnsafeShallowSwapFields(
659 message1, message2, fields);
660
661 TestUtil::ExpectOneofClear(*message1);
662 TestUtil::ExpectOneofSet1(*message2);
663 }
664
TEST(GeneratedMessageReflectionTest,UnsafeShallowSwapFieldsOneofExpectShallow)665 TEST(GeneratedMessageReflectionTest,
666 UnsafeShallowSwapFieldsOneofExpectShallow) {
667 Arena arena;
668 auto* message1 = Arena::Create<unittest::TestOneof2>(&arena);
669 auto* message2 = Arena::Create<unittest::TestOneof2>(&arena);
670 TestUtil::SetOneof1(message1);
671 message1->mutable_foo_message()->set_moo_int(1000);
672 auto* kept_foo_ptr = message1->mutable_foo_message();
673
674 std::vector<const FieldDescriptor*> fields;
675 const Descriptor* descriptor = message1->GetDescriptor();
676 for (int i = 0; i < descriptor->field_count(); i++) {
677 fields.push_back(descriptor->field(i));
678 }
679 GeneratedMessageReflectionTestHelper::UnsafeShallowSwapFields(
680 message1, message2, fields);
681
682 EXPECT_TRUE(message2->has_foo_message());
683 EXPECT_EQ(message2->foo_message().moo_int(), 1000);
684 EXPECT_EQ(kept_foo_ptr, message2->mutable_foo_message());
685 }
686
TEST(GeneratedMessageReflectionTest,RemoveLast)687 TEST(GeneratedMessageReflectionTest, RemoveLast) {
688 unittest::TestAllTypes message;
689 TestUtil::ReflectionTester reflection_tester(
690 unittest::TestAllTypes::descriptor());
691
692 TestUtil::SetAllFields(&message);
693
694 reflection_tester.RemoveLastRepeatedsViaReflection(&message);
695
696 TestUtil::ExpectLastRepeatedsRemoved(message);
697 }
698
TEST(GeneratedMessageReflectionTest,RemoveLastExtensions)699 TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) {
700 unittest::TestAllExtensions message;
701 TestUtil::ReflectionTester reflection_tester(
702 unittest::TestAllExtensions::descriptor());
703
704 TestUtil::SetAllExtensions(&message);
705
706 reflection_tester.RemoveLastRepeatedsViaReflection(&message);
707
708 TestUtil::ExpectLastRepeatedExtensionsRemoved(message);
709 }
710
TEST(GeneratedMessageReflectionTest,RemoveLastPackedExtensions)711 TEST(GeneratedMessageReflectionTest, RemoveLastPackedExtensions) {
712 unittest::TestPackedExtensions message;
713 TestUtil::ReflectionTester reflection_tester(
714 unittest::TestPackedExtensions::descriptor());
715
716 TestUtil::SetPackedExtensions(&message);
717
718 reflection_tester.RemoveLastRepeatedsViaReflection(&message);
719
720 TestUtil::ExpectLastRepeatedExtensionsRemoved(message);
721 }
722
TEST(GeneratedMessageReflectionTest,ReleaseLast)723 TEST(GeneratedMessageReflectionTest, ReleaseLast) {
724 unittest::TestAllTypes message;
725 const Descriptor* descriptor = message.GetDescriptor();
726 TestUtil::ReflectionTester reflection_tester(descriptor);
727
728 TestUtil::SetAllFields(&message);
729
730 reflection_tester.ReleaseLastRepeatedsViaReflection(&message, false);
731
732 TestUtil::ExpectLastRepeatedsReleased(message);
733
734 // Now test that we actually release the right message.
735 message.Clear();
736 TestUtil::SetAllFields(&message);
737 ASSERT_EQ(2, message.repeated_foreign_message_size());
738 const protobuf_unittest::ForeignMessage* expected =
739 message.mutable_repeated_foreign_message(1);
740 (void)expected; // unused in somce configurations
741 std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
742 &message, descriptor->FindFieldByName("repeated_foreign_message")));
743 if (!internal::DebugHardenForceCopyInRelease()) {
744 EXPECT_EQ(expected, released.get());
745 }
746 }
747
TEST(GeneratedMessageReflectionTest,ReleaseLastExtensions)748 TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) {
749 if (internal::DebugHardenForceCopyInRelease()) {
750 GTEST_SKIP() << "Won't work with FORCE_COPY_IN_RELEASE.";
751 }
752
753 unittest::TestAllExtensions message;
754 const Descriptor* descriptor = message.GetDescriptor();
755 TestUtil::ReflectionTester reflection_tester(descriptor);
756
757 TestUtil::SetAllExtensions(&message);
758
759 reflection_tester.ReleaseLastRepeatedsViaReflection(&message, true);
760
761 TestUtil::ExpectLastRepeatedExtensionsReleased(message);
762
763 // Now test that we actually release the right message.
764 message.Clear();
765 TestUtil::SetAllExtensions(&message);
766 ASSERT_EQ(
767 2, message.ExtensionSize(unittest::repeated_foreign_message_extension));
768 const protobuf_unittest::ForeignMessage* expected =
769 message.MutableExtension(unittest::repeated_foreign_message_extension, 1);
770 std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
771 &message, descriptor->file()->FindExtensionByName(
772 "repeated_foreign_message_extension")));
773 EXPECT_EQ(expected, released.get());
774 }
775
TEST(GeneratedMessageReflectionTest,SwapRepeatedElements)776 TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) {
777 unittest::TestAllTypes message;
778 TestUtil::ReflectionTester reflection_tester(
779 unittest::TestAllTypes::descriptor());
780
781 TestUtil::SetAllFields(&message);
782
783 // Swap and test that fields are all swapped.
784 reflection_tester.SwapRepeatedsViaReflection(&message);
785 TestUtil::ExpectRepeatedsSwapped(message);
786
787 // Swap back and test that fields are all back to original values.
788 reflection_tester.SwapRepeatedsViaReflection(&message);
789 TestUtil::ExpectAllFieldsSet(message);
790 }
791
TEST(GeneratedMessageReflectionTest,SwapRepeatedElementsExtension)792 TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) {
793 unittest::TestAllExtensions message;
794 TestUtil::ReflectionTester reflection_tester(
795 unittest::TestAllExtensions::descriptor());
796
797 TestUtil::SetAllExtensions(&message);
798
799 // Swap and test that fields are all swapped.
800 reflection_tester.SwapRepeatedsViaReflection(&message);
801 TestUtil::ExpectRepeatedExtensionsSwapped(message);
802
803 // Swap back and test that fields are all back to original values.
804 reflection_tester.SwapRepeatedsViaReflection(&message);
805 TestUtil::ExpectAllExtensionsSet(message);
806 }
807
TEST(GeneratedMessageReflectionTest,Extensions)808 TEST(GeneratedMessageReflectionTest, Extensions) {
809 // Set every extension to a unique value then go back and check all those
810 // values.
811 unittest::TestAllExtensions message;
812 TestUtil::ReflectionTester reflection_tester(
813 unittest::TestAllExtensions::descriptor());
814
815 reflection_tester.SetAllFieldsViaReflection(&message);
816 TestUtil::ExpectAllExtensionsSet(message);
817 reflection_tester.ExpectAllFieldsSetViaReflection(message);
818
819 reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
820 TestUtil::ExpectRepeatedExtensionsModified(message);
821 }
822
TEST(GeneratedMessageReflectionTest,PackedExtensions)823 TEST(GeneratedMessageReflectionTest, PackedExtensions) {
824 // Set every extension to a unique value then go back and check all those
825 // values.
826 unittest::TestPackedExtensions message;
827
828 // First set the extensions via the generated API (see b/366468123).
829 TestUtil::SetPackedExtensions(&message);
830 TestUtil::ExpectPackedExtensionsSet(message);
831 message.Clear();
832
833 TestUtil::ReflectionTester reflection_tester(
834 unittest::TestPackedExtensions::descriptor());
835
836 reflection_tester.SetPackedFieldsViaReflection(&message);
837 TestUtil::ExpectPackedExtensionsSet(message);
838 reflection_tester.ExpectPackedFieldsSetViaReflection(message);
839 }
840
TEST(GeneratedMessageReflectionTest,FindExtensionTypeByNumber)841 TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
842 const Reflection* reflection =
843 unittest::TestAllExtensions::default_instance().GetReflection();
844
845 const FieldDescriptor* extension1 =
846 unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
847 "optional_int32_extension");
848 const FieldDescriptor* extension2 =
849 unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
850 "repeated_string_extension");
851
852 EXPECT_EQ(extension1,
853 reflection->FindKnownExtensionByNumber(extension1->number()));
854 EXPECT_EQ(extension2,
855 reflection->FindKnownExtensionByNumber(extension2->number()));
856
857 // Non-existent extension.
858 EXPECT_TRUE(reflection->FindKnownExtensionByNumber(62341) == nullptr);
859
860 // Extensions of TestAllExtensions should not show up as extensions of
861 // other types.
862 EXPECT_TRUE(unittest::TestAllTypes::default_instance()
863 .GetReflection()
864 ->FindKnownExtensionByNumber(extension1->number()) ==
865 nullptr);
866 }
867
TEST(GeneratedMessageReflectionTest,FindKnownExtensionByName)868 TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
869 const Reflection* reflection =
870 unittest::TestAllExtensions::default_instance().GetReflection();
871
872 const FieldDescriptor* extension1 =
873 unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
874 "optional_int32_extension");
875 const FieldDescriptor* extension2 =
876 unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
877 "repeated_string_extension");
878
879 EXPECT_EQ(extension1,
880 reflection->FindKnownExtensionByName(extension1->full_name()));
881 EXPECT_EQ(extension2,
882 reflection->FindKnownExtensionByName(extension2->full_name()));
883
884 // Non-existent extension.
885 EXPECT_TRUE(reflection->FindKnownExtensionByName("no_such_ext") == nullptr);
886
887 // Extensions of TestAllExtensions should not show up as extensions of
888 // other types.
889 EXPECT_TRUE(unittest::TestAllTypes::default_instance()
890 .GetReflection()
891 ->FindKnownExtensionByName(extension1->full_name()) ==
892 nullptr);
893 }
894
895
TEST(GeneratedMessageReflectionTest,SetAllocatedMessageTest)896 TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) {
897 unittest::TestAllTypes from_message1;
898 unittest::TestAllTypes from_message2;
899 unittest::TestAllTypes to_message;
900 TestUtil::ReflectionTester reflection_tester(
901 unittest::TestAllTypes::descriptor());
902 reflection_tester.SetAllFieldsViaReflection(&from_message1);
903 reflection_tester.SetAllFieldsViaReflection(&from_message2);
904
905 // Before moving fields, we expect the nested messages to be nullptr.
906 reflection_tester.ExpectMessagesReleasedViaReflection(
907 &to_message, TestUtil::ReflectionTester::IS_NULL);
908
909 // After fields are moved we should get non-nullptr releases.
910 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
911 &from_message1, &to_message);
912 reflection_tester.ExpectMessagesReleasedViaReflection(
913 &to_message, TestUtil::ReflectionTester::NOT_NULL);
914
915 // Another move to make sure that we can SetAllocated several times.
916 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
917 &from_message2, &to_message);
918 reflection_tester.ExpectMessagesReleasedViaReflection(
919 &to_message, TestUtil::ReflectionTester::NOT_NULL);
920
921 // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
922 // releases to be nullptr again.
923 reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
924 &to_message);
925 reflection_tester.ExpectMessagesReleasedViaReflection(
926 &to_message, TestUtil::ReflectionTester::IS_NULL);
927 }
928
TEST(GeneratedMessageReflectionTest,SetAllocatedMessageOnArenaTest)929 TEST(GeneratedMessageReflectionTest, SetAllocatedMessageOnArenaTest) {
930 unittest::TestAllTypes from_message1;
931 unittest::TestAllTypes from_message2;
932 Arena arena;
933 unittest::TestAllTypes* to_message =
934 Arena::Create<unittest::TestAllTypes>(&arena);
935 TestUtil::ReflectionTester reflection_tester(
936 unittest::TestAllTypes::descriptor());
937 reflection_tester.SetAllFieldsViaReflection(&from_message1);
938 reflection_tester.SetAllFieldsViaReflection(&from_message2);
939
940 // Before moving fields, we expect the nested messages to be nullptr.
941 reflection_tester.ExpectMessagesReleasedViaReflection(
942 to_message, TestUtil::ReflectionTester::IS_NULL);
943
944 // After fields are moved we should get non-nullptr releases.
945 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
946 &from_message1, to_message);
947 reflection_tester.ExpectMessagesReleasedViaReflection(
948 to_message, TestUtil::ReflectionTester::NOT_NULL);
949
950 // Another move to make sure that we can SetAllocated several times.
951 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
952 &from_message2, to_message);
953 reflection_tester.ExpectMessagesReleasedViaReflection(
954 to_message, TestUtil::ReflectionTester::NOT_NULL);
955
956 // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
957 // releases to be nullptr again.
958 reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
959 to_message);
960 reflection_tester.ExpectMessagesReleasedViaReflection(
961 to_message, TestUtil::ReflectionTester::IS_NULL);
962 }
963
TEST(GeneratedMessageReflectionTest,SetAllocatedExtensionMessageTest)964 TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageTest) {
965 unittest::TestAllExtensions from_message1;
966 unittest::TestAllExtensions from_message2;
967 unittest::TestAllExtensions to_message;
968 TestUtil::ReflectionTester reflection_tester(
969 unittest::TestAllExtensions::descriptor());
970 reflection_tester.SetAllFieldsViaReflection(&from_message1);
971 reflection_tester.SetAllFieldsViaReflection(&from_message2);
972
973 // Before moving fields, we expect the nested messages to be nullptr.
974 reflection_tester.ExpectMessagesReleasedViaReflection(
975 &to_message, TestUtil::ReflectionTester::IS_NULL);
976
977 // After fields are moved we should get non-nullptr releases.
978 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
979 &from_message1, &to_message);
980 reflection_tester.ExpectMessagesReleasedViaReflection(
981 &to_message, TestUtil::ReflectionTester::NOT_NULL);
982
983 // Another move to make sure that we can SetAllocated several times.
984 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
985 &from_message2, &to_message);
986 reflection_tester.ExpectMessagesReleasedViaReflection(
987 &to_message, TestUtil::ReflectionTester::NOT_NULL);
988
989 // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
990 // releases to be nullptr again.
991 reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
992 &to_message);
993 reflection_tester.ExpectMessagesReleasedViaReflection(
994 &to_message, TestUtil::ReflectionTester::IS_NULL);
995 }
996
TEST(GeneratedMessageReflectionTest,SetAllocatedExtensionMessageOnArenaTest)997 TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageOnArenaTest) {
998 Arena arena;
999 unittest::TestAllExtensions* to_message =
1000 Arena::Create<unittest::TestAllExtensions>(&arena);
1001 unittest::TestAllExtensions from_message1;
1002 unittest::TestAllExtensions from_message2;
1003 TestUtil::ReflectionTester reflection_tester(
1004 unittest::TestAllExtensions::descriptor());
1005 reflection_tester.SetAllFieldsViaReflection(&from_message1);
1006 reflection_tester.SetAllFieldsViaReflection(&from_message2);
1007
1008 // Before moving fields, we expect the nested messages to be nullptr.
1009 reflection_tester.ExpectMessagesReleasedViaReflection(
1010 to_message, TestUtil::ReflectionTester::IS_NULL);
1011
1012 // After fields are moved we should get non-nullptr releases.
1013 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
1014 &from_message1, to_message);
1015 reflection_tester.ExpectMessagesReleasedViaReflection(
1016 to_message, TestUtil::ReflectionTester::NOT_NULL);
1017
1018 // Another move to make sure that we can SetAllocated several times.
1019 reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
1020 &from_message2, to_message);
1021 reflection_tester.ExpectMessagesReleasedViaReflection(
1022 to_message, TestUtil::ReflectionTester::NOT_NULL);
1023
1024 // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
1025 // releases to be nullptr again.
1026 reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
1027 to_message);
1028 reflection_tester.ExpectMessagesReleasedViaReflection(
1029 to_message, TestUtil::ReflectionTester::IS_NULL);
1030 }
1031
TEST(GeneratedMessageReflectionTest,AddRepeatedMessage)1032 TEST(GeneratedMessageReflectionTest, AddRepeatedMessage) {
1033 unittest::TestAllTypes message;
1034
1035 const Reflection* reflection = message.GetReflection();
1036 const Reflection* nested_reflection =
1037 unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
1038
1039 const FieldDescriptor* nested_bb =
1040 unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
1041 "bb");
1042
1043 Message* nested =
1044 reflection->AddMessage(&message, F("repeated_nested_message"));
1045 nested_reflection->SetInt32(nested, nested_bb, 11);
1046
1047 EXPECT_EQ(11, message.repeated_nested_message(0).bb());
1048 }
1049
TEST(GeneratedMessageReflectionTest,MutableRepeatedMessage)1050 TEST(GeneratedMessageReflectionTest, MutableRepeatedMessage) {
1051 unittest::TestAllTypes message;
1052
1053 const Reflection* reflection = message.GetReflection();
1054 const Reflection* nested_reflection =
1055 unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
1056
1057 const FieldDescriptor* nested_bb =
1058 unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
1059 "bb");
1060
1061 message.add_repeated_nested_message()->set_bb(12);
1062
1063 Message* nested = reflection->MutableRepeatedMessage(
1064 &message, F("repeated_nested_message"), 0);
1065 EXPECT_EQ(12, nested_reflection->GetInt32(*nested, nested_bb));
1066 nested_reflection->SetInt32(nested, nested_bb, 13);
1067 EXPECT_EQ(13, message.repeated_nested_message(0).bb());
1068 }
1069
TEST(GeneratedMessageReflectionTest,AddAllocatedMessage)1070 TEST(GeneratedMessageReflectionTest, AddAllocatedMessage) {
1071 unittest::TestAllTypes message;
1072
1073 const Reflection* reflection = message.GetReflection();
1074
1075 unittest::TestAllTypes::NestedMessage* nested =
1076 new unittest::TestAllTypes::NestedMessage();
1077 nested->set_bb(11);
1078 reflection->AddAllocatedMessage(&message, F("repeated_nested_message"),
1079 nested);
1080 EXPECT_EQ(1, message.repeated_nested_message_size());
1081 EXPECT_EQ(11, message.repeated_nested_message(0).bb());
1082 }
1083
TEST(GeneratedMessageReflectionTest,ListFieldsOneOf)1084 TEST(GeneratedMessageReflectionTest, ListFieldsOneOf) {
1085 unittest::TestOneof2 message;
1086 TestUtil::SetOneof1(&message);
1087
1088 const Reflection* reflection = message.GetReflection();
1089 std::vector<const FieldDescriptor*> fields;
1090 reflection->ListFields(message, &fields);
1091 EXPECT_EQ(4, fields.size());
1092 }
1093
TEST(GeneratedMessageReflectionTest,Oneof)1094 TEST(GeneratedMessageReflectionTest, Oneof) {
1095 unittest::TestOneof2 message;
1096 const Descriptor* descriptor = message.GetDescriptor();
1097 const Reflection* reflection = message.GetReflection();
1098
1099 // Check default values.
1100 EXPECT_EQ(
1101 0, reflection->GetInt32(message, descriptor->FindFieldByName("foo_int")));
1102 EXPECT_EQ("", reflection->GetString(
1103 message, descriptor->FindFieldByName("foo_string")));
1104 EXPECT_EQ("", reflection->GetString(message,
1105 descriptor->FindFieldByName("foo_cord")));
1106 EXPECT_EQ("", reflection->GetString(
1107 message, descriptor->FindFieldByName("foo_string_piece")));
1108 EXPECT_EQ("", reflection->GetString(
1109 message, descriptor->FindFieldByName("foo_bytes")));
1110 EXPECT_EQ(
1111 unittest::TestOneof2::FOO,
1112 reflection->GetEnum(message, descriptor->FindFieldByName("foo_enum"))
1113 ->number());
1114 EXPECT_EQ(&unittest::TestOneof2::NestedMessage::default_instance(),
1115 &reflection->GetMessage(
1116 message, descriptor->FindFieldByName("foo_message")));
1117 EXPECT_EQ(&unittest::TestOneof2::FooGroup::default_instance(),
1118 &reflection->GetMessage(message,
1119 descriptor->FindFieldByName("foogroup")));
1120 EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(),
1121 &reflection->GetMessage(
1122 message, descriptor->FindFieldByName("foo_lazy_message")));
1123 EXPECT_EQ("", reflection->GetString(
1124 message, descriptor->FindFieldByName("foo_bytes_cord")));
1125 EXPECT_EQ(
1126 5, reflection->GetInt32(message, descriptor->FindFieldByName("bar_int")));
1127 EXPECT_EQ("STRING", reflection->GetString(
1128 message, descriptor->FindFieldByName("bar_string")));
1129 EXPECT_EQ("CORD", reflection->GetString(
1130 message, descriptor->FindFieldByName("bar_cord")));
1131 EXPECT_EQ("SPIECE",
1132 reflection->GetString(
1133 message, descriptor->FindFieldByName("bar_string_piece")));
1134 EXPECT_EQ("BYTES", reflection->GetString(
1135 message, descriptor->FindFieldByName("bar_bytes")));
1136 EXPECT_EQ(
1137 unittest::TestOneof2::BAR,
1138 reflection->GetEnum(message, descriptor->FindFieldByName("bar_enum"))
1139 ->number());
1140
1141 // Check Set functions.
1142 reflection->SetInt32(&message, descriptor->FindFieldByName("foo_int"), 123);
1143 EXPECT_EQ(123, reflection->GetInt32(message,
1144 descriptor->FindFieldByName("foo_int")));
1145 reflection->SetString(&message, descriptor->FindFieldByName("foo_string"),
1146 "abc");
1147 EXPECT_EQ("abc", reflection->GetString(
1148 message, descriptor->FindFieldByName("foo_string")));
1149 reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes"),
1150 "bytes");
1151 EXPECT_EQ("bytes", reflection->GetString(
1152 message, descriptor->FindFieldByName("foo_bytes")));
1153 reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes_cord"),
1154 "bytes_cord");
1155 EXPECT_EQ("bytes_cord",
1156 reflection->GetString(
1157 message, descriptor->FindFieldByName("foo_bytes_cord")));
1158 reflection->SetString(&message, descriptor->FindFieldByName("bar_cord"),
1159 "change_cord");
1160 EXPECT_EQ(
1161 "change_cord",
1162 reflection->GetString(message, descriptor->FindFieldByName("bar_cord")));
1163 reflection->SetString(&message,
1164 descriptor->FindFieldByName("bar_string_piece"),
1165 "change_spiece");
1166 EXPECT_EQ("change_spiece",
1167 reflection->GetString(
1168 message, descriptor->FindFieldByName("bar_string_piece")));
1169
1170 message.clear_foo();
1171 message.clear_bar();
1172 TestUtil::ExpectOneofClear(message);
1173 }
1174
TEST(GeneratedMessageReflectionTest,SetAllocatedOneofMessageTest)1175 TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) {
1176 unittest::TestOneof2 from_message1;
1177 unittest::TestOneof2 from_message2;
1178 unittest::TestOneof2 to_message;
1179 const Descriptor* descriptor = unittest::TestOneof2::descriptor();
1180 const Reflection* reflection = to_message.GetReflection();
1181
1182 Message* released = reflection->ReleaseMessage(
1183 &to_message, descriptor->FindFieldByName("foo_lazy_message"));
1184 EXPECT_TRUE(released == nullptr);
1185 released = reflection->ReleaseMessage(
1186 &to_message, descriptor->FindFieldByName("foo_message"));
1187 EXPECT_TRUE(released == nullptr);
1188
1189 TestUtil::ReflectionTester::SetOneofViaReflection(&from_message1);
1190 TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1);
1191
1192 TestUtil::ReflectionTester::
1193 SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1,
1194 &to_message);
1195 const Message& sub_message = reflection->GetMessage(
1196 to_message, descriptor->FindFieldByName("foo_lazy_message"));
1197 (void)sub_message; // unused in somce configurations
1198 released = reflection->ReleaseMessage(
1199 &to_message, descriptor->FindFieldByName("foo_lazy_message"));
1200 EXPECT_TRUE(released != nullptr);
1201 if (!internal::DebugHardenForceCopyInRelease()) {
1202 EXPECT_EQ(&sub_message, released);
1203 }
1204 delete released;
1205
1206 TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2);
1207
1208 reflection->MutableMessage(&from_message2,
1209 descriptor->FindFieldByName("foo_message"));
1210
1211 TestUtil::ReflectionTester::
1212 SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2,
1213 &to_message);
1214
1215 const Message& sub_message2 = reflection->GetMessage(
1216 to_message, descriptor->FindFieldByName("foo_message"));
1217 (void)sub_message2; // unused in somce configurations
1218 released = reflection->ReleaseMessage(
1219 &to_message, descriptor->FindFieldByName("foo_message"));
1220 EXPECT_TRUE(released != nullptr);
1221 if (!internal::DebugHardenForceCopyInRelease()) {
1222 EXPECT_EQ(&sub_message2, released);
1223 }
1224 delete released;
1225 }
1226
TEST(GeneratedMessageReflectionTest,SetAllocatedOneofMessageOnArenaTest)1227 TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) {
1228 unittest::TestOneof2 from_message1;
1229 unittest::TestOneof2 from_message2;
1230 Arena arena;
1231 unittest::TestOneof2* to_message =
1232 Arena::Create<unittest::TestOneof2>(&arena);
1233 const Descriptor* descriptor = unittest::TestOneof2::descriptor();
1234 const Reflection* reflection = to_message->GetReflection();
1235
1236 Message* released = reflection->ReleaseMessage(
1237 to_message, descriptor->FindFieldByName("foo_lazy_message"));
1238 EXPECT_TRUE(released == nullptr);
1239 released = reflection->ReleaseMessage(
1240 to_message, descriptor->FindFieldByName("foo_message"));
1241 EXPECT_TRUE(released == nullptr);
1242
1243 TestUtil::ReflectionTester::SetOneofViaReflection(&from_message1);
1244 TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1);
1245
1246 TestUtil::ReflectionTester::
1247 SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1,
1248 to_message);
1249 const Message& sub_message = reflection->GetMessage(
1250 *to_message, descriptor->FindFieldByName("foo_lazy_message"));
1251 released = reflection->ReleaseMessage(
1252 to_message, descriptor->FindFieldByName("foo_lazy_message"));
1253 EXPECT_TRUE(released != nullptr);
1254 // Since sub_message is arena allocated, releasing it results in copying it
1255 // into new heap-allocated memory.
1256 EXPECT_NE(&sub_message, released);
1257 delete released;
1258
1259 TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2);
1260
1261 reflection->MutableMessage(&from_message2,
1262 descriptor->FindFieldByName("foo_message"));
1263
1264 TestUtil::ReflectionTester::
1265 SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2,
1266 to_message);
1267
1268 const Message& sub_message2 = reflection->GetMessage(
1269 *to_message, descriptor->FindFieldByName("foo_message"));
1270 released = reflection->ReleaseMessage(
1271 to_message, descriptor->FindFieldByName("foo_message"));
1272 EXPECT_TRUE(released != nullptr);
1273 // Since sub_message2 is arena allocated, releasing it results in copying it
1274 // into new heap-allocated memory.
1275 EXPECT_NE(&sub_message2, released);
1276 delete released;
1277 }
1278
TEST(GeneratedMessageReflectionTest,ReleaseMessageTest)1279 TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) {
1280 unittest::TestAllTypes message;
1281 TestUtil::ReflectionTester reflection_tester(
1282 unittest::TestAllTypes::descriptor());
1283
1284 // When nothing is set, we expect all released messages to be nullptr.
1285 reflection_tester.ExpectMessagesReleasedViaReflection(
1286 &message, TestUtil::ReflectionTester::IS_NULL);
1287
1288 // After fields are set we should get non-nullptr releases.
1289 reflection_tester.SetAllFieldsViaReflection(&message);
1290 reflection_tester.ExpectMessagesReleasedViaReflection(
1291 &message, TestUtil::ReflectionTester::NOT_NULL);
1292
1293 // After Clear() we may or may not get a message from ReleaseMessage().
1294 // This is implementation specific.
1295 reflection_tester.SetAllFieldsViaReflection(&message);
1296 message.Clear();
1297 reflection_tester.ExpectMessagesReleasedViaReflection(
1298 &message, TestUtil::ReflectionTester::CAN_BE_NULL);
1299
1300 // Test a different code path for setting after releasing.
1301 TestUtil::SetAllFields(&message);
1302 TestUtil::ExpectAllFieldsSet(message);
1303 }
1304
TEST(GeneratedMessageReflectionTest,ReleaseExtensionMessageTest)1305 TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) {
1306 unittest::TestAllExtensions message;
1307 TestUtil::ReflectionTester reflection_tester(
1308 unittest::TestAllExtensions::descriptor());
1309
1310 // When nothing is set, we expect all released messages to be nullptr.
1311 reflection_tester.ExpectMessagesReleasedViaReflection(
1312 &message, TestUtil::ReflectionTester::IS_NULL);
1313
1314 // After fields are set we should get non-nullptr releases.
1315 reflection_tester.SetAllFieldsViaReflection(&message);
1316 reflection_tester.ExpectMessagesReleasedViaReflection(
1317 &message, TestUtil::ReflectionTester::NOT_NULL);
1318
1319 // After Clear() we may or may not get a message from ReleaseMessage().
1320 // This is implementation specific.
1321 reflection_tester.SetAllFieldsViaReflection(&message);
1322 message.Clear();
1323 reflection_tester.ExpectMessagesReleasedViaReflection(
1324 &message, TestUtil::ReflectionTester::CAN_BE_NULL);
1325
1326 // Test a different code path for setting after releasing.
1327 TestUtil::SetAllExtensions(&message);
1328 TestUtil::ExpectAllExtensionsSet(message);
1329 }
1330
TEST(GeneratedMessageReflectionTest,ReleaseOneofMessageTest)1331 TEST(GeneratedMessageReflectionTest, ReleaseOneofMessageTest) {
1332 unittest::TestOneof2 message;
1333 TestUtil::ReflectionTester::SetOneofViaReflection(&message);
1334
1335 const Descriptor* descriptor = unittest::TestOneof2::descriptor();
1336 const Reflection* reflection = message.GetReflection();
1337 const Message& sub_message = reflection->GetMessage(
1338 message, descriptor->FindFieldByName("foo_lazy_message"));
1339 (void)sub_message; // unused in somce configurations
1340 Message* released = reflection->ReleaseMessage(
1341 &message, descriptor->FindFieldByName("foo_lazy_message"));
1342
1343 EXPECT_TRUE(released != nullptr);
1344 if (!internal::DebugHardenForceCopyInRelease()) {
1345 EXPECT_EQ(&sub_message, released);
1346 }
1347 delete released;
1348
1349 released = reflection->ReleaseMessage(
1350 &message, descriptor->FindFieldByName("foo_lazy_message"));
1351 EXPECT_TRUE(released == nullptr);
1352 }
1353
TEST(GeneratedMessageReflectionTest,ArenaReleaseMessageTest)1354 TEST(GeneratedMessageReflectionTest, ArenaReleaseMessageTest) {
1355 Arena arena;
1356 unittest::TestAllTypes* message =
1357 Arena::Create<unittest::TestAllTypes>(&arena);
1358 TestUtil::ReflectionTester reflection_tester(
1359 unittest::TestAllTypes::descriptor());
1360
1361 // When nothing is set, we expect all released messages to be nullptr.
1362 reflection_tester.ExpectMessagesReleasedViaReflection(
1363 message, TestUtil::ReflectionTester::IS_NULL);
1364
1365 // After fields are set we should get non-nullptr releases.
1366 reflection_tester.SetAllFieldsViaReflection(message);
1367 reflection_tester.ExpectMessagesReleasedViaReflection(
1368 message, TestUtil::ReflectionTester::NOT_NULL);
1369
1370 // After Clear() we may or may not get a message from ReleaseMessage().
1371 // This is implementation specific.
1372 reflection_tester.SetAllFieldsViaReflection(message);
1373 message->Clear();
1374 reflection_tester.ExpectMessagesReleasedViaReflection(
1375 message, TestUtil::ReflectionTester::CAN_BE_NULL);
1376 }
1377
TEST(GeneratedMessageReflectionTest,ArenaReleaseExtensionMessageTest)1378 TEST(GeneratedMessageReflectionTest, ArenaReleaseExtensionMessageTest) {
1379 Arena arena;
1380 unittest::TestAllExtensions* message =
1381 Arena::Create<unittest::TestAllExtensions>(&arena);
1382 TestUtil::ReflectionTester reflection_tester(
1383 unittest::TestAllExtensions::descriptor());
1384
1385 // When nothing is set, we expect all released messages to be nullptr.
1386 reflection_tester.ExpectMessagesReleasedViaReflection(
1387 message, TestUtil::ReflectionTester::IS_NULL);
1388
1389 // After fields are set we should get non-nullptr releases.
1390 reflection_tester.SetAllFieldsViaReflection(message);
1391 reflection_tester.ExpectMessagesReleasedViaReflection(
1392 message, TestUtil::ReflectionTester::NOT_NULL);
1393
1394 // After Clear() we may or may not get a message from ReleaseMessage().
1395 // This is implementation specific.
1396 reflection_tester.SetAllFieldsViaReflection(message);
1397 message->Clear();
1398 reflection_tester.ExpectMessagesReleasedViaReflection(
1399 message, TestUtil::ReflectionTester::CAN_BE_NULL);
1400 }
1401
TEST(GeneratedMessageReflectionTest,ArenaReleaseOneofMessageTest)1402 TEST(GeneratedMessageReflectionTest, ArenaReleaseOneofMessageTest) {
1403 Arena arena;
1404 unittest::TestOneof2* message = Arena::Create<unittest::TestOneof2>(&arena);
1405 TestUtil::ReflectionTester::SetOneofViaReflection(message);
1406
1407 const Descriptor* descriptor = unittest::TestOneof2::descriptor();
1408 const Reflection* reflection = message->GetReflection();
1409 Message* released = reflection->ReleaseMessage(
1410 message, descriptor->FindFieldByName("foo_lazy_message"));
1411
1412 EXPECT_TRUE(released != nullptr);
1413 delete released;
1414
1415 released = reflection->ReleaseMessage(
1416 message, descriptor->FindFieldByName("foo_lazy_message"));
1417 EXPECT_TRUE(released == nullptr);
1418 }
1419
1420 #if GTEST_HAS_DEATH_TEST
1421
TEST(GeneratedMessageReflectionTest,UsageErrors)1422 TEST(GeneratedMessageReflectionTest, UsageErrors) {
1423 unittest::TestAllTypes message;
1424 unittest::ForeignMessage foreign;
1425 const Reflection* reflection = message.GetReflection();
1426 const Descriptor* descriptor = message.GetDescriptor();
1427
1428 // Testing every single failure mode would be too much work. Let's just
1429 // check a few.
1430 EXPECT_DEATH(
1431 reflection->GetInt32(message,
1432 descriptor->FindFieldByName("optional_int64")),
1433 "Protocol Buffer reflection usage error:\n"
1434 " Method : google::protobuf::Reflection::GetInt32\n"
1435 " Message type: protobuf_unittest\\.TestAllTypes\n"
1436 " Field : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
1437 " Problem : Field is not the right type for this message:\n"
1438 " Expected : CPPTYPE_INT32\n"
1439 " Field type: CPPTYPE_INT64");
1440 EXPECT_DEATH(reflection->GetInt32(
1441 message, descriptor->FindFieldByName("repeated_int32")),
1442 "Protocol Buffer reflection usage error:\n"
1443 " Method : google::protobuf::Reflection::GetInt32\n"
1444 " Message type: protobuf_unittest.TestAllTypes\n"
1445 " Field : protobuf_unittest.TestAllTypes.repeated_int32\n"
1446 " Problem : Field is repeated; the method requires a "
1447 "singular field.");
1448 #ifndef NDEBUG
1449 EXPECT_DEATH(
1450 reflection->GetInt32(foreign,
1451 descriptor->FindFieldByName("optional_int32")),
1452 "Protocol Buffer reflection usage error:\n"
1453 " Method : google::protobuf::Reflection::GetInt32\n"
1454 " Expected type: protobuf_unittest.TestAllTypes\n"
1455 " Actual type : protobuf_unittest.ForeignMessage\n"
1456 " Field : protobuf_unittest.TestAllTypes.optional_int32\n"
1457 " Problem : Message is not the right object for reflection");
1458 #endif
1459 EXPECT_DEATH(
1460 reflection->GetInt32(
1461 message,
1462 unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
1463 "Protocol Buffer reflection usage error:\n"
1464 " Method : google::protobuf::Reflection::GetInt32\n"
1465 " Message type: protobuf_unittest.TestAllTypes\n"
1466 " Field : protobuf_unittest.ForeignMessage.c\n"
1467 " Problem : Field does not match message type.");
1468 EXPECT_DEATH(
1469 reflection->HasField(
1470 message,
1471 unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
1472 "Protocol Buffer reflection usage error:\n"
1473 " Method : google::protobuf::Reflection::HasField\n"
1474 " Message type: protobuf_unittest.TestAllTypes\n"
1475 " Field : protobuf_unittest.ForeignMessage.c\n"
1476 " Problem : Field does not match message type.");
1477 }
1478
1479 #endif // GTEST_HAS_DEATH_TEST
1480
1481 class GeneratedMessageReflectionCordAccessorsTest : public testing::Test {
1482 protected:
1483 const FieldDescriptor* optional_string_;
1484 const FieldDescriptor* optional_string_piece_;
1485 const FieldDescriptor* optional_cord_;
1486 const FieldDescriptor* repeated_string_;
1487 const FieldDescriptor* repeated_string_piece_;
1488 const FieldDescriptor* repeated_cord_;
1489 const FieldDescriptor* default_string_;
1490 const FieldDescriptor* default_string_piece_;
1491 const FieldDescriptor* default_cord_;
1492
1493 const FieldDescriptor* optional_string_extension_;
1494 const FieldDescriptor* repeated_string_extension_;
1495
1496 unittest::TestAllTypes message_;
1497 const Reflection* reflection_;
1498 unittest::TestAllExtensions extensions_message_;
1499 const Reflection* extensions_reflection_;
1500
SetUp()1501 void SetUp() override {
1502 const Descriptor* descriptor = unittest::TestAllTypes::descriptor();
1503
1504 optional_string_ = descriptor->FindFieldByName("optional_string");
1505 optional_string_piece_ =
1506 descriptor->FindFieldByName("optional_string_piece");
1507 optional_cord_ = descriptor->FindFieldByName("optional_cord");
1508 repeated_string_ = descriptor->FindFieldByName("repeated_string");
1509 repeated_string_piece_ =
1510 descriptor->FindFieldByName("repeated_string_piece");
1511 repeated_cord_ = descriptor->FindFieldByName("repeated_cord");
1512 default_string_ = descriptor->FindFieldByName("default_string");
1513 default_string_piece_ = descriptor->FindFieldByName("default_string_piece");
1514 default_cord_ = descriptor->FindFieldByName("default_cord");
1515
1516 optional_string_extension_ =
1517 descriptor->file()->FindExtensionByName("optional_string_extension");
1518 repeated_string_extension_ =
1519 descriptor->file()->FindExtensionByName("repeated_string_extension");
1520
1521 ASSERT_TRUE(optional_string_ != nullptr);
1522 ASSERT_TRUE(optional_string_piece_ != nullptr);
1523 ASSERT_TRUE(optional_cord_ != nullptr);
1524 ASSERT_TRUE(repeated_string_ != nullptr);
1525 ASSERT_TRUE(repeated_string_piece_ != nullptr);
1526 ASSERT_TRUE(repeated_cord_ != nullptr);
1527 ASSERT_TRUE(optional_string_extension_ != nullptr);
1528 ASSERT_TRUE(repeated_string_extension_ != nullptr);
1529
1530 reflection_ = message_.GetReflection();
1531 extensions_reflection_ = extensions_message_.GetReflection();
1532 }
1533 };
1534
TEST_F(GeneratedMessageReflectionCordAccessorsTest,GetCord)1535 TEST_F(GeneratedMessageReflectionCordAccessorsTest, GetCord) {
1536 message_.set_optional_string("foo");
1537
1538 extensions_message_.SetExtension(unittest::optional_string_extension, "moo");
1539
1540 EXPECT_EQ("foo", reflection_->GetCord(message_, optional_string_));
1541 EXPECT_EQ("moo", extensions_reflection_->GetCord(extensions_message_,
1542 optional_string_extension_));
1543
1544 EXPECT_EQ("hello", reflection_->GetCord(message_, default_string_));
1545 EXPECT_EQ("abc", reflection_->GetCord(message_, default_string_piece_));
1546 EXPECT_EQ("123", reflection_->GetCord(message_, default_cord_));
1547
1548
1549 unittest::TestCord message;
1550 const Descriptor* descriptor = unittest::TestCord::descriptor();
1551 const Reflection* reflection = message.GetReflection();
1552
1553 message.set_optional_bytes_cord("bytes_cord");
1554 EXPECT_EQ("bytes_cord",
1555 reflection->GetCord(
1556 message, descriptor->FindFieldByName("optional_bytes_cord")));
1557 }
1558
TEST_F(GeneratedMessageReflectionCordAccessorsTest,GetOneofCord)1559 TEST_F(GeneratedMessageReflectionCordAccessorsTest, GetOneofCord) {
1560 unittest::TestOneof2 message;
1561 const Descriptor* descriptor = unittest::TestOneof2::descriptor();
1562 const Reflection* reflection = message.GetReflection();
1563
1564 message.set_foo_bytes_cord("bytes_cord");
1565 EXPECT_EQ("bytes_cord",
1566 reflection->GetCord(message,
1567 descriptor->FindFieldByName("foo_bytes_cord")));
1568
1569 message.set_foo_string("foo");
1570 EXPECT_EQ("foo", reflection->GetCord(
1571 message, descriptor->FindFieldByName("foo_string")));
1572
1573 message.set_foo_bytes("bytes");
1574 EXPECT_EQ("bytes", reflection->GetCord(
1575 message, descriptor->FindFieldByName("foo_bytes")));
1576
1577 }
1578
TEST_F(GeneratedMessageReflectionCordAccessorsTest,SetStringFromCord)1579 TEST_F(GeneratedMessageReflectionCordAccessorsTest, SetStringFromCord) {
1580 reflection_->SetString(&message_, optional_string_, absl::Cord("foo"));
1581 reflection_->SetString(&message_, optional_string_piece_, absl::Cord("bar"));
1582 reflection_->SetString(&message_, optional_cord_, absl::Cord("baz"));
1583 extensions_reflection_->SetString(
1584 &extensions_message_, optional_string_extension_, absl::Cord("moo"));
1585
1586 EXPECT_TRUE(message_.has_optional_string());
1587 EXPECT_TRUE(message_.has_optional_string_piece());
1588 EXPECT_TRUE(message_.has_optional_cord());
1589 EXPECT_TRUE(
1590 extensions_message_.HasExtension(unittest::optional_string_extension));
1591
1592 EXPECT_EQ("foo", message_.optional_string());
1593 EXPECT_EQ("bar", std::string(
1594 reflection_->GetCord(message_, optional_string_piece_)));
1595 EXPECT_EQ("baz", std::string(reflection_->GetCord(message_, optional_cord_)));
1596 EXPECT_EQ("moo", extensions_message_.GetExtension(
1597 unittest::optional_string_extension));
1598
1599 unittest::TestCord message;
1600 const Descriptor* descriptor = unittest::TestCord::descriptor();
1601 const Reflection* reflection = message.GetReflection();
1602
1603 reflection->SetString(&message,
1604 descriptor->FindFieldByName("optional_bytes_cord"),
1605 absl::Cord("cord"));
1606 EXPECT_TRUE(message.has_optional_bytes_cord());
1607 EXPECT_EQ("cord", message.optional_bytes_cord());
1608 }
1609
TEST_F(GeneratedMessageReflectionCordAccessorsTest,SetOneofStringFromCord)1610 TEST_F(GeneratedMessageReflectionCordAccessorsTest, SetOneofStringFromCord) {
1611 unittest::TestOneof2 message;
1612 const Descriptor* descriptor = unittest::TestOneof2::descriptor();
1613 const Reflection* reflection = message.GetReflection();
1614
1615 reflection->SetString(&message, descriptor->FindFieldByName("foo_string"),
1616 absl::Cord("foo"));
1617 EXPECT_TRUE(message.has_foo_string());
1618 EXPECT_EQ("foo", message.foo_string());
1619
1620 reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes"),
1621 absl::Cord("bytes"));
1622 EXPECT_TRUE(message.has_foo_bytes());
1623 EXPECT_EQ("bytes", message.foo_bytes());
1624
1625 reflection->SetString(&message, descriptor->FindFieldByName("foo_cord"),
1626 absl::Cord("cord"));
1627 EXPECT_EQ("cord", std::string(reflection->GetCord(
1628 message, descriptor->FindFieldByName("foo_cord"))));
1629
1630 reflection->SetString(&message,
1631 descriptor->FindFieldByName("foo_string_piece"),
1632 absl::Cord("string_piece"));
1633 EXPECT_EQ("string_piece",
1634 reflection->GetCord(
1635 message, descriptor->FindFieldByName("foo_string_piece")));
1636
1637 reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes_cord"),
1638 absl::Cord("bytes_cord"));
1639 EXPECT_TRUE(message.has_foo_bytes_cord());
1640 EXPECT_EQ("bytes_cord", message.foo_bytes_cord());
1641 }
1642
TEST_F(GeneratedMessageReflectionCordAccessorsTest,CordSingularBytes)1643 TEST_F(GeneratedMessageReflectionCordAccessorsTest, CordSingularBytes) {
1644 unittest::TestCord message;
1645 absl::Cord cord_value("test");
1646 message.set_optional_bytes_cord(cord_value);
1647 EXPECT_EQ("test", message.optional_bytes_cord());
1648
1649 EXPECT_TRUE(message.has_optional_bytes_cord());
1650 message.clear_optional_bytes_cord();
1651 EXPECT_FALSE(message.has_optional_bytes_cord());
1652
1653 std::string string_value = "test";
1654 message.set_optional_bytes_cord(string_value);
1655 EXPECT_EQ("test", message.optional_bytes_cord());
1656 }
1657
TEST_F(GeneratedMessageReflectionCordAccessorsTest,CordSingularBytesDefault)1658 TEST_F(GeneratedMessageReflectionCordAccessorsTest, CordSingularBytesDefault) {
1659 unittest::TestCord message;
1660 EXPECT_EQ("hello", message.optional_bytes_cord_default());
1661 absl::Cord cord_value("world");
1662 message.set_optional_bytes_cord_default(cord_value);
1663 EXPECT_EQ("world", message.optional_bytes_cord_default());
1664 message.clear_optional_bytes_cord_default();
1665 EXPECT_EQ("hello", message.optional_bytes_cord_default());
1666 }
1667
TEST_F(GeneratedMessageReflectionCordAccessorsTest,CordSingularOneofBytes)1668 TEST_F(GeneratedMessageReflectionCordAccessorsTest, CordSingularOneofBytes) {
1669 unittest::TestOneof2 message;
1670 absl::Cord cord_value("test");
1671 message.set_foo_bytes_cord(cord_value);
1672 EXPECT_EQ("test", message.foo_bytes_cord());
1673
1674 EXPECT_TRUE(message.has_foo_bytes_cord());
1675 message.clear_foo();
1676 EXPECT_FALSE(message.has_foo_bytes_cord());
1677
1678 std::string string_value = "test";
1679 message.set_foo_bytes_cord(string_value);
1680 EXPECT_EQ("test", message.foo_bytes_cord());
1681 EXPECT_TRUE(message.has_foo_bytes_cord());
1682 }
1683
TEST_F(GeneratedMessageReflectionCordAccessorsTest,ClearOneofCord)1684 TEST_F(GeneratedMessageReflectionCordAccessorsTest, ClearOneofCord) {
1685 unittest::TestOneof2 message;
1686 absl::Cord cord_value("test");
1687 message.set_foo_bytes_cord(cord_value);
1688
1689 const Descriptor* descriptor = unittest::TestOneof2::descriptor();
1690 const Reflection* reflection = message.GetReflection();
1691
1692 EXPECT_TRUE(message.has_foo_bytes_cord());
1693 reflection->ClearOneof(&message, descriptor->FindOneofByName("foo"));
1694 EXPECT_FALSE(message.has_foo_bytes_cord());
1695 }
1696
1697
1698 using internal::IsDescendant;
1699
TEST(GeneratedMessageReflection,IsDescendantMessage)1700 TEST(GeneratedMessageReflection, IsDescendantMessage) {
1701 unittest::TestAllTypes msg1, msg2;
1702 TestUtil::SetAllFields(&msg1);
1703 msg2 = msg1;
1704
1705 EXPECT_TRUE(IsDescendant(msg1, msg1.optional_nested_message()));
1706 EXPECT_TRUE(IsDescendant(msg1, msg1.repeated_foreign_message(0)));
1707
1708 EXPECT_FALSE(IsDescendant(msg1, msg2.optional_nested_message()));
1709 EXPECT_FALSE(IsDescendant(msg1, msg2.repeated_foreign_message(0)));
1710 }
1711
TEST(GeneratedMessageReflection,IsDescendantMap)1712 TEST(GeneratedMessageReflection, IsDescendantMap) {
1713 unittest::TestMap msg1, msg2;
1714 (*msg1.mutable_map_int32_foreign_message())[0].set_c(100);
1715 TestUtil::SetAllFields(&(*msg1.mutable_map_int32_all_types())[0]);
1716 msg2 = msg1;
1717
1718 EXPECT_TRUE(IsDescendant(msg1, msg1.map_int32_foreign_message().at(0)));
1719 EXPECT_TRUE(IsDescendant(msg1, msg1.map_int32_all_types().at(0)));
1720
1721 EXPECT_FALSE(IsDescendant(msg1, msg2.map_int32_foreign_message().at(0)));
1722 EXPECT_FALSE(IsDescendant(msg1, msg2.map_int32_all_types().at(0)));
1723 }
1724
TEST(GeneratedMessageReflection,IsDescendantExtension)1725 TEST(GeneratedMessageReflection, IsDescendantExtension) {
1726 unittest::TestAllExtensions msg1, msg2;
1727 TestUtil::SetAllExtensions(&msg1);
1728 msg2 = msg1;
1729
1730 EXPECT_TRUE(IsDescendant(
1731 msg1, msg1.GetExtension(unittest::optional_nested_message_extension)));
1732 EXPECT_TRUE(IsDescendant(
1733 msg1,
1734 msg1.GetExtension(unittest::repeated_foreign_message_extension, 0)));
1735
1736 EXPECT_FALSE(IsDescendant(
1737 msg1, msg2.GetExtension(unittest::optional_nested_message_extension)));
1738 EXPECT_FALSE(IsDescendant(
1739 msg1,
1740 msg2.GetExtension(unittest::repeated_foreign_message_extension, 0)));
1741 }
1742
TEST(GeneratedMessageReflection,IsDescendantOneof)1743 TEST(GeneratedMessageReflection, IsDescendantOneof) {
1744 unittest::TestOneof msg1, msg2;
1745 TestUtil::SetAllFields(msg1.mutable_foo_message());
1746 msg2 = msg1;
1747
1748 EXPECT_TRUE(IsDescendant(msg1, msg1.foo_message().optional_nested_message()));
1749 EXPECT_TRUE(
1750 IsDescendant(msg1, msg1.foo_message().repeated_foreign_message(0)));
1751
1752 EXPECT_FALSE(
1753 IsDescendant(msg1, msg2.foo_message().optional_nested_message()));
1754 EXPECT_FALSE(
1755 IsDescendant(msg1, msg2.foo_message().repeated_foreign_message(0)));
1756 }
1757
TEST(GeneratedMessageReflection,ListFieldsSorted)1758 TEST(GeneratedMessageReflection, ListFieldsSorted) {
1759 unittest::TestFieldOrderings msg;
1760 const Reflection* reflection = msg.GetReflection();
1761 std::vector<const FieldDescriptor*> fields;
1762 msg.set_my_string("hello"); // tag 11
1763 msg.mutable_optional_nested_message(); // tag 200
1764 reflection->ListFields(msg, &fields);
1765 // No sorting, in order declaration.
1766 EXPECT_THAT(fields,
1767 ElementsAre(Pointee(Property(&FieldDescriptor::number, 11)),
1768 Pointee(Property(&FieldDescriptor::number, 200))));
1769 msg.set_my_int(4242); // tag 1
1770 reflection->ListFields(msg, &fields);
1771 // Sorting as fields are declared in order 11, 1, 200.
1772 EXPECT_THAT(fields,
1773 ElementsAre(Pointee(Property(&FieldDescriptor::number, 1)),
1774 Pointee(Property(&FieldDescriptor::number, 11)),
1775 Pointee(Property(&FieldDescriptor::number, 200))));
1776 msg.clear_optional_nested_message(); // tag 200
1777 msg.SetExtension(unittest::my_extension_int,
1778 424242); // tag 5 from extension
1779 reflection->ListFields(msg, &fields);
1780 // Sorting as extension tag is in between.
1781 EXPECT_THAT(fields,
1782 ElementsAre(Pointee(Property(&FieldDescriptor::number, 1)),
1783 Pointee(Property(&FieldDescriptor::number, 5)),
1784 Pointee(Property(&FieldDescriptor::number, 11))));
1785 msg.clear_my_string(); // tag 11.
1786 reflection->ListFields(msg, &fields);
1787 // No sorting as extension is bigger than tag 1.
1788 EXPECT_THAT(fields,
1789 ElementsAre(Pointee(Property(&FieldDescriptor::number, 1)),
1790 Pointee(Property(&FieldDescriptor::number, 5))));
1791 msg.set_my_float(1.0); // tag 101
1792 msg.SetExtension(unittest::my_extension_string,
1793 "hello"); // tag 50 from extension
1794 reflection->ListFields(msg, &fields);
1795 // Sorting of all as extensions are out of order and fields are in between.
1796 EXPECT_THAT(fields,
1797 ElementsAre(Pointee(Property(&FieldDescriptor::number, 1)),
1798 Pointee(Property(&FieldDescriptor::number, 5)),
1799 Pointee(Property(&FieldDescriptor::number, 50)),
1800 Pointee(Property(&FieldDescriptor::number, 101))));
1801 }
1802
TEST(GeneratedMessageReflection,SwapImplicitPresenceShouldWork)1803 TEST(GeneratedMessageReflection, SwapImplicitPresenceShouldWork) {
1804 proto3_unittest::TestHasbits lhs, rhs;
1805 rhs.mutable_child()->set_optional_int32(-1);
1806 lhs.GetReflection()->Swap(&lhs, &rhs);
1807 EXPECT_EQ(lhs.child().optional_int32(), -1);
1808 }
1809
1810 } // namespace
1811 } // namespace protobuf
1812 } // namespace google
1813
1814 #include "google/protobuf/port_undef.inc"
1815