Home | History | Annotate | Download | only in protobuf
      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 (at) 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 #include <memory>
     47 #ifndef _SHARED_PTR_H
     48 #include <google/protobuf/stubs/shared_ptr.h>
     49 #endif
     50 
     51 #include <google/protobuf/descriptor.h>
     52 #include <google/protobuf/test_util.h>
     53 #include <google/protobuf/unittest.pb.h>
     54 
     55 #include <google/protobuf/stubs/logging.h>
     56 #include <google/protobuf/stubs/common.h>
     57 #include <google/protobuf/testing/googletest.h>
     58 #include <gtest/gtest.h>
     59 
     60 namespace google {
     61 namespace protobuf {
     62 
     63 namespace {
     64 
     65 // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
     66 const FieldDescriptor* F(const string& name) {
     67   const FieldDescriptor* result =
     68     unittest::TestAllTypes::descriptor()->FindFieldByName(name);
     69   GOOGLE_CHECK(result != NULL);
     70   return result;
     71 }
     72 
     73 TEST(GeneratedMessageReflectionTest, Defaults) {
     74   // Check that all default values are set correctly in the initial message.
     75   unittest::TestAllTypes message;
     76   TestUtil::ReflectionTester reflection_tester(
     77     unittest::TestAllTypes::descriptor());
     78 
     79   reflection_tester.ExpectClearViaReflection(message);
     80 
     81   const Reflection* reflection = message.GetReflection();
     82 
     83   // Messages should return pointers to default instances until first use.
     84   // (This is not checked by ExpectClear() since it is not actually true after
     85   // the fields have been set and then cleared.)
     86   EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
     87             &reflection->GetMessage(message, F("optionalgroup")));
     88   EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
     89             &reflection->GetMessage(message, F("optional_nested_message")));
     90   EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
     91             &reflection->GetMessage(message, F("optional_foreign_message")));
     92   EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
     93             &reflection->GetMessage(message, F("optional_import_message")));
     94 }
     95 
     96 TEST(GeneratedMessageReflectionTest, Accessors) {
     97   // Set every field to a unique value then go back and check all those
     98   // values.
     99   unittest::TestAllTypes message;
    100   TestUtil::ReflectionTester reflection_tester(
    101     unittest::TestAllTypes::descriptor());
    102 
    103   reflection_tester.SetAllFieldsViaReflection(&message);
    104   TestUtil::ExpectAllFieldsSet(message);
    105   reflection_tester.ExpectAllFieldsSetViaReflection(message);
    106 
    107   reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
    108   TestUtil::ExpectRepeatedFieldsModified(message);
    109 }
    110 
    111 TEST(GeneratedMessageReflectionTest, GetStringReference) {
    112   // Test that GetStringReference() returns the underlying string when it is
    113   // a normal string field.
    114   unittest::TestAllTypes message;
    115   message.set_optional_string("foo");
    116   message.add_repeated_string("foo");
    117 
    118   const Reflection* reflection = message.GetReflection();
    119   string scratch;
    120 
    121   EXPECT_EQ(&message.optional_string(),
    122       &reflection->GetStringReference(message, F("optional_string"), &scratch))
    123     << "For simple string fields, GetStringReference() should return a "
    124        "reference to the underlying string.";
    125   EXPECT_EQ(&message.repeated_string(0),
    126       &reflection->GetRepeatedStringReference(message, F("repeated_string"),
    127                                               0, &scratch))
    128     << "For simple string fields, GetRepeatedStringReference() should return "
    129        "a reference to the underlying string.";
    130 }
    131 
    132 
    133 TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) {
    134   // Check that after setting all fields and then clearing, getting an
    135   // embedded message does NOT return the default instance.
    136   unittest::TestAllTypes message;
    137   TestUtil::ReflectionTester reflection_tester(
    138     unittest::TestAllTypes::descriptor());
    139 
    140   TestUtil::SetAllFields(&message);
    141   message.Clear();
    142 
    143   const Reflection* reflection = message.GetReflection();
    144 
    145   EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
    146             &reflection->GetMessage(message, F("optionalgroup")));
    147   EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
    148             &reflection->GetMessage(message, F("optional_nested_message")));
    149   EXPECT_NE(&unittest::ForeignMessage::default_instance(),
    150             &reflection->GetMessage(message, F("optional_foreign_message")));
    151   EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
    152             &reflection->GetMessage(message, F("optional_import_message")));
    153 }
    154 
    155 
    156 TEST(GeneratedMessageReflectionTest, Swap) {
    157   unittest::TestAllTypes message1;
    158   unittest::TestAllTypes message2;
    159 
    160   TestUtil::SetAllFields(&message1);
    161 
    162   const Reflection* reflection = message1.GetReflection();
    163   reflection->Swap(&message1, &message2);
    164 
    165   TestUtil::ExpectClear(message1);
    166   TestUtil::ExpectAllFieldsSet(message2);
    167 }
    168 
    169 TEST(GeneratedMessageReflectionTest, SwapWithBothSet) {
    170   unittest::TestAllTypes message1;
    171   unittest::TestAllTypes message2;
    172 
    173   TestUtil::SetAllFields(&message1);
    174   TestUtil::SetAllFields(&message2);
    175   TestUtil::ModifyRepeatedFields(&message2);
    176 
    177   const Reflection* reflection = message1.GetReflection();
    178   reflection->Swap(&message1, &message2);
    179 
    180   TestUtil::ExpectRepeatedFieldsModified(message1);
    181   TestUtil::ExpectAllFieldsSet(message2);
    182 
    183   message1.set_optional_int32(532819);
    184 
    185   reflection->Swap(&message1, &message2);
    186 
    187   EXPECT_EQ(532819, message2.optional_int32());
    188 }
    189 
    190 TEST(GeneratedMessageReflectionTest, SwapExtensions) {
    191   unittest::TestAllExtensions message1;
    192   unittest::TestAllExtensions message2;
    193 
    194   TestUtil::SetAllExtensions(&message1);
    195 
    196   const Reflection* reflection = message1.GetReflection();
    197   reflection->Swap(&message1, &message2);
    198 
    199   TestUtil::ExpectExtensionsClear(message1);
    200   TestUtil::ExpectAllExtensionsSet(message2);
    201 }
    202 
    203 TEST(GeneratedMessageReflectionTest, SwapUnknown) {
    204   unittest::TestEmptyMessage message1, message2;
    205 
    206   message1.mutable_unknown_fields()->AddVarint(1234, 1);
    207 
    208   EXPECT_EQ(1, message1.unknown_fields().field_count());
    209   EXPECT_EQ(0, message2.unknown_fields().field_count());
    210   const Reflection* reflection = message1.GetReflection();
    211   reflection->Swap(&message1, &message2);
    212   EXPECT_EQ(0, message1.unknown_fields().field_count());
    213   EXPECT_EQ(1, message2.unknown_fields().field_count());
    214 }
    215 
    216 TEST(GeneratedMessageReflectionTest, SwapFields) {
    217   unittest::TestAllTypes message1, message2;
    218   message1.set_optional_double(12.3);
    219   message1.mutable_repeated_int32()->Add(10);
    220   message1.mutable_repeated_int32()->Add(20);
    221 
    222   message2.set_optional_string("hello");
    223   message2.mutable_repeated_int64()->Add(30);
    224 
    225   vector<const FieldDescriptor*> fields;
    226   const Descriptor* descriptor = message1.GetDescriptor();
    227   fields.push_back(descriptor->FindFieldByName("optional_double"));
    228   fields.push_back(descriptor->FindFieldByName("repeated_int32"));
    229   fields.push_back(descriptor->FindFieldByName("optional_string"));
    230   fields.push_back(descriptor->FindFieldByName("optional_uint64"));
    231 
    232   const Reflection* reflection = message1.GetReflection();
    233   reflection->SwapFields(&message1, &message2, fields);
    234 
    235   EXPECT_FALSE(message1.has_optional_double());
    236   EXPECT_EQ(0, message1.repeated_int32_size());
    237   EXPECT_TRUE(message1.has_optional_string());
    238   EXPECT_EQ("hello", message1.optional_string());
    239   EXPECT_EQ(0, message1.repeated_int64_size());
    240   EXPECT_FALSE(message1.has_optional_uint64());
    241 
    242   EXPECT_TRUE(message2.has_optional_double());
    243   EXPECT_EQ(12.3, message2.optional_double());
    244   EXPECT_EQ(2, message2.repeated_int32_size());
    245   EXPECT_EQ(10, message2.repeated_int32(0));
    246   EXPECT_EQ(20, message2.repeated_int32(1));
    247   EXPECT_FALSE(message2.has_optional_string());
    248   EXPECT_EQ(1, message2.repeated_int64_size());
    249   EXPECT_FALSE(message2.has_optional_uint64());
    250 }
    251 
    252 TEST(GeneratedMessageReflectionTest, SwapFieldsAll) {
    253   unittest::TestAllTypes message1;
    254   unittest::TestAllTypes message2;
    255 
    256   TestUtil::SetAllFields(&message2);
    257 
    258   vector<const FieldDescriptor*> fields;
    259   const Reflection* reflection = message1.GetReflection();
    260   reflection->ListFields(message2, &fields);
    261   reflection->SwapFields(&message1, &message2, fields);
    262 
    263   TestUtil::ExpectAllFieldsSet(message1);
    264   TestUtil::ExpectClear(message2);
    265 }
    266 
    267 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtension) {
    268   unittest::TestAllExtensions message1;
    269   unittest::TestAllExtensions message2;
    270 
    271   TestUtil::SetAllExtensions(&message1);
    272 
    273   vector<const FieldDescriptor*> fields;
    274   const Reflection* reflection = message1.GetReflection();
    275   reflection->ListFields(message1, &fields);
    276   reflection->SwapFields(&message1, &message2, fields);
    277 
    278   TestUtil::ExpectExtensionsClear(message1);
    279   TestUtil::ExpectAllExtensionsSet(message2);
    280 }
    281 
    282 TEST(GeneratedMessageReflectionTest, SwapOneof) {
    283   unittest::TestOneof2 message1, message2;
    284   TestUtil::SetOneof1(&message1);
    285 
    286   const Reflection* reflection = message1.GetReflection();
    287   reflection->Swap(&message1, &message2);
    288 
    289   TestUtil::ExpectOneofClear(message1);
    290   TestUtil::ExpectOneofSet1(message2);
    291 }
    292 
    293 TEST(GeneratedMessageReflectionTest, SwapOneofBothSet) {
    294   unittest::TestOneof2 message1, message2;
    295   TestUtil::SetOneof1(&message1);
    296   TestUtil::SetOneof2(&message2);
    297 
    298   const Reflection* reflection = message1.GetReflection();
    299   reflection->Swap(&message1, &message2);
    300 
    301   TestUtil::ExpectOneofSet2(message1);
    302   TestUtil::ExpectOneofSet1(message2);
    303 }
    304 
    305 TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) {
    306   unittest::TestOneof2 message1, message2;
    307   TestUtil::SetOneof1(&message1);
    308 
    309   vector<const FieldDescriptor*> fields;
    310   const Descriptor* descriptor = message1.GetDescriptor();
    311   for (int i = 0; i < descriptor->field_count(); i++) {
    312     fields.push_back(descriptor->field(i));
    313   }
    314   const Reflection* reflection = message1.GetReflection();
    315   reflection->SwapFields(&message1, &message2, fields);
    316 
    317   TestUtil::ExpectOneofClear(message1);
    318   TestUtil::ExpectOneofSet1(message2);
    319 }
    320 
    321 TEST(GeneratedMessageReflectionTest, RemoveLast) {
    322   unittest::TestAllTypes message;
    323   TestUtil::ReflectionTester reflection_tester(
    324     unittest::TestAllTypes::descriptor());
    325 
    326   TestUtil::SetAllFields(&message);
    327 
    328   reflection_tester.RemoveLastRepeatedsViaReflection(&message);
    329 
    330   TestUtil::ExpectLastRepeatedsRemoved(message);
    331 }
    332 
    333 TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) {
    334   unittest::TestAllExtensions message;
    335   TestUtil::ReflectionTester reflection_tester(
    336     unittest::TestAllExtensions::descriptor());
    337 
    338   TestUtil::SetAllExtensions(&message);
    339 
    340   reflection_tester.RemoveLastRepeatedsViaReflection(&message);
    341 
    342   TestUtil::ExpectLastRepeatedExtensionsRemoved(message);
    343 }
    344 
    345 TEST(GeneratedMessageReflectionTest, ReleaseLast) {
    346   unittest::TestAllTypes message;
    347   const Descriptor* descriptor = message.GetDescriptor();
    348   TestUtil::ReflectionTester reflection_tester(descriptor);
    349 
    350   TestUtil::SetAllFields(&message);
    351 
    352   reflection_tester.ReleaseLastRepeatedsViaReflection(&message, false);
    353 
    354   TestUtil::ExpectLastRepeatedsReleased(message);
    355 
    356   // Now test that we actually release the right message.
    357   message.Clear();
    358   TestUtil::SetAllFields(&message);
    359   ASSERT_EQ(2, message.repeated_foreign_message_size());
    360   const protobuf_unittest::ForeignMessage* expected =
    361       message.mutable_repeated_foreign_message(1);
    362   google::protobuf::scoped_ptr<Message> released(message.GetReflection()->ReleaseLast(
    363       &message, descriptor->FindFieldByName("repeated_foreign_message")));
    364   EXPECT_EQ(expected, released.get());
    365 }
    366 
    367 TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) {
    368   unittest::TestAllExtensions message;
    369   const Descriptor* descriptor = message.GetDescriptor();
    370   TestUtil::ReflectionTester reflection_tester(descriptor);
    371 
    372   TestUtil::SetAllExtensions(&message);
    373 
    374   reflection_tester.ReleaseLastRepeatedsViaReflection(&message, true);
    375 
    376   TestUtil::ExpectLastRepeatedExtensionsReleased(message);
    377 
    378   // Now test that we actually release the right message.
    379   message.Clear();
    380   TestUtil::SetAllExtensions(&message);
    381   ASSERT_EQ(2, message.ExtensionSize(
    382       unittest::repeated_foreign_message_extension));
    383   const protobuf_unittest::ForeignMessage* expected = message.MutableExtension(
    384       unittest::repeated_foreign_message_extension, 1);
    385   google::protobuf::scoped_ptr<Message> released(message.GetReflection()->ReleaseLast(
    386       &message, descriptor->file()->FindExtensionByName(
    387                     "repeated_foreign_message_extension")));
    388   EXPECT_EQ(expected, released.get());
    389 
    390 }
    391 
    392 TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) {
    393   unittest::TestAllTypes message;
    394   TestUtil::ReflectionTester reflection_tester(
    395     unittest::TestAllTypes::descriptor());
    396 
    397   TestUtil::SetAllFields(&message);
    398 
    399   // Swap and test that fields are all swapped.
    400   reflection_tester.SwapRepeatedsViaReflection(&message);
    401   TestUtil::ExpectRepeatedsSwapped(message);
    402 
    403   // Swap back and test that fields are all back to original values.
    404   reflection_tester.SwapRepeatedsViaReflection(&message);
    405   TestUtil::ExpectAllFieldsSet(message);
    406 }
    407 
    408 TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) {
    409   unittest::TestAllExtensions message;
    410   TestUtil::ReflectionTester reflection_tester(
    411     unittest::TestAllExtensions::descriptor());
    412 
    413   TestUtil::SetAllExtensions(&message);
    414 
    415   // Swap and test that fields are all swapped.
    416   reflection_tester.SwapRepeatedsViaReflection(&message);
    417   TestUtil::ExpectRepeatedExtensionsSwapped(message);
    418 
    419   // Swap back and test that fields are all back to original values.
    420   reflection_tester.SwapRepeatedsViaReflection(&message);
    421   TestUtil::ExpectAllExtensionsSet(message);
    422 }
    423 
    424 TEST(GeneratedMessageReflectionTest, Extensions) {
    425   // Set every extension to a unique value then go back and check all those
    426   // values.
    427   unittest::TestAllExtensions message;
    428   TestUtil::ReflectionTester reflection_tester(
    429     unittest::TestAllExtensions::descriptor());
    430 
    431   reflection_tester.SetAllFieldsViaReflection(&message);
    432   TestUtil::ExpectAllExtensionsSet(message);
    433   reflection_tester.ExpectAllFieldsSetViaReflection(message);
    434 
    435   reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
    436   TestUtil::ExpectRepeatedExtensionsModified(message);
    437 }
    438 
    439 TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
    440   const Reflection* reflection =
    441     unittest::TestAllExtensions::default_instance().GetReflection();
    442 
    443   const FieldDescriptor* extension1 =
    444     unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
    445       "optional_int32_extension");
    446   const FieldDescriptor* extension2 =
    447     unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
    448       "repeated_string_extension");
    449 
    450   EXPECT_EQ(extension1,
    451             reflection->FindKnownExtensionByNumber(extension1->number()));
    452   EXPECT_EQ(extension2,
    453             reflection->FindKnownExtensionByNumber(extension2->number()));
    454 
    455   // Non-existent extension.
    456   EXPECT_TRUE(reflection->FindKnownExtensionByNumber(62341) == NULL);
    457 
    458   // Extensions of TestAllExtensions should not show up as extensions of
    459   // other types.
    460   EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
    461               FindKnownExtensionByNumber(extension1->number()) == NULL);
    462 }
    463 
    464 TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
    465   const Reflection* reflection =
    466     unittest::TestAllExtensions::default_instance().GetReflection();
    467 
    468   const FieldDescriptor* extension1 =
    469     unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
    470       "optional_int32_extension");
    471   const FieldDescriptor* extension2 =
    472     unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
    473       "repeated_string_extension");
    474 
    475   EXPECT_EQ(extension1,
    476             reflection->FindKnownExtensionByName(extension1->full_name()));
    477   EXPECT_EQ(extension2,
    478             reflection->FindKnownExtensionByName(extension2->full_name()));
    479 
    480   // Non-existent extension.
    481   EXPECT_TRUE(reflection->FindKnownExtensionByName("no_such_ext") == NULL);
    482 
    483   // Extensions of TestAllExtensions should not show up as extensions of
    484   // other types.
    485   EXPECT_TRUE(unittest::TestAllTypes::default_instance().GetReflection()->
    486               FindKnownExtensionByName(extension1->full_name()) == NULL);
    487 }
    488 
    489 TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) {
    490   unittest::TestAllTypes from_message1;
    491   unittest::TestAllTypes from_message2;
    492   unittest::TestAllTypes to_message;
    493   TestUtil::ReflectionTester reflection_tester(
    494     unittest::TestAllTypes::descriptor());
    495   reflection_tester.SetAllFieldsViaReflection(&from_message1);
    496   reflection_tester.SetAllFieldsViaReflection(&from_message2);
    497 
    498   // Before moving fields, we expect the nested messages to be NULL.
    499   reflection_tester.ExpectMessagesReleasedViaReflection(
    500       &to_message, TestUtil::ReflectionTester::IS_NULL);
    501 
    502   // After fields are moved we should get non-NULL releases.
    503   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
    504       &from_message1, &to_message);
    505   reflection_tester.ExpectMessagesReleasedViaReflection(
    506       &to_message, TestUtil::ReflectionTester::NOT_NULL);
    507 
    508   // Another move to make sure that we can SetAllocated several times.
    509   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
    510       &from_message2, &to_message);
    511   reflection_tester.ExpectMessagesReleasedViaReflection(
    512       &to_message, TestUtil::ReflectionTester::NOT_NULL);
    513 
    514   // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
    515   // releases to be NULL again.
    516   reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
    517       &to_message);
    518   reflection_tester.ExpectMessagesReleasedViaReflection(
    519       &to_message, TestUtil::ReflectionTester::IS_NULL);
    520 }
    521 
    522 TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageTest) {
    523   unittest::TestAllExtensions from_message1;
    524   unittest::TestAllExtensions from_message2;
    525   unittest::TestAllExtensions to_message;
    526   TestUtil::ReflectionTester reflection_tester(
    527     unittest::TestAllExtensions::descriptor());
    528   reflection_tester.SetAllFieldsViaReflection(&from_message1);
    529   reflection_tester.SetAllFieldsViaReflection(&from_message2);
    530 
    531   // Before moving fields, we expect the nested messages to be NULL.
    532   reflection_tester.ExpectMessagesReleasedViaReflection(
    533       &to_message, TestUtil::ReflectionTester::IS_NULL);
    534 
    535   // After fields are moved we should get non-NULL releases.
    536   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
    537       &from_message1, &to_message);
    538   reflection_tester.ExpectMessagesReleasedViaReflection(
    539       &to_message, TestUtil::ReflectionTester::NOT_NULL);
    540 
    541   // Another move to make sure that we can SetAllocated several times.
    542   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
    543       &from_message2, &to_message);
    544   reflection_tester.ExpectMessagesReleasedViaReflection(
    545       &to_message, TestUtil::ReflectionTester::NOT_NULL);
    546 
    547   // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
    548   // releases to be NULL again.
    549   reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
    550       &to_message);
    551   reflection_tester.ExpectMessagesReleasedViaReflection(
    552       &to_message, TestUtil::ReflectionTester::IS_NULL);
    553 }
    554 
    555 TEST(GeneratedMessageReflectionTest, AddRepeatedMessage) {
    556   unittest::TestAllTypes message;
    557 
    558   const Reflection* reflection = message.GetReflection();
    559   const Reflection* nested_reflection =
    560       unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
    561 
    562   const FieldDescriptor* nested_bb =
    563       unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
    564           "bb");
    565 
    566   Message* nested = reflection->AddMessage(
    567       &message, F("repeated_nested_message"));
    568   nested_reflection->SetInt32(nested, nested_bb, 11);
    569 
    570   EXPECT_EQ(11, message.repeated_nested_message(0).bb());
    571 }
    572 
    573 TEST(GeneratedMessageReflectionTest, MutableRepeatedMessage) {
    574   unittest::TestAllTypes message;
    575 
    576   const Reflection* reflection = message.GetReflection();
    577   const Reflection* nested_reflection =
    578       unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
    579 
    580   const FieldDescriptor* nested_bb =
    581       unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
    582           "bb");
    583 
    584   message.add_repeated_nested_message()->set_bb(12);
    585 
    586   Message* nested = reflection->MutableRepeatedMessage(
    587       &message, F("repeated_nested_message"), 0);
    588   EXPECT_EQ(12, nested_reflection->GetInt32(*nested, nested_bb));
    589   nested_reflection->SetInt32(nested, nested_bb, 13);
    590   EXPECT_EQ(13, message.repeated_nested_message(0).bb());
    591 }
    592 
    593 TEST(GeneratedMessageReflectionTest, AddAllocatedMessage) {
    594   unittest::TestAllTypes message;
    595 
    596   const Reflection* reflection = message.GetReflection();
    597 
    598   unittest::TestAllTypes::NestedMessage* nested =
    599       new unittest::TestAllTypes::NestedMessage();
    600   nested->set_bb(11);
    601   reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), nested);
    602   EXPECT_EQ(1, message.repeated_nested_message_size());
    603   EXPECT_EQ(11, message.repeated_nested_message(0).bb());
    604 }
    605 
    606 TEST(GeneratedMessageReflectionTest, ListFieldsOneOf) {
    607   unittest::TestOneof2 message;
    608   TestUtil::SetOneof1(&message);
    609 
    610   const Reflection* reflection = message.GetReflection();
    611   vector<const FieldDescriptor*> fields;
    612   reflection->ListFields(message, &fields);
    613   EXPECT_EQ(4, fields.size());
    614 }
    615 
    616 TEST(GeneratedMessageReflectionTest, Oneof) {
    617   unittest::TestOneof2 message;
    618   const Descriptor* descriptor = message.GetDescriptor();
    619   const Reflection* reflection = message.GetReflection();
    620 
    621   // Check default values.
    622   EXPECT_EQ(0, reflection->GetInt32(
    623       message, descriptor->FindFieldByName("foo_int")));
    624   EXPECT_EQ("", reflection->GetString(
    625       message, descriptor->FindFieldByName("foo_string")));
    626   EXPECT_EQ("", reflection->GetString(
    627       message, descriptor->FindFieldByName("foo_cord")));
    628   EXPECT_EQ("", reflection->GetString(
    629       message, descriptor->FindFieldByName("foo_string_piece")));
    630   EXPECT_EQ("", reflection->GetString(
    631       message, descriptor->FindFieldByName("foo_bytes")));
    632   EXPECT_EQ(unittest::TestOneof2::FOO, reflection->GetEnum(
    633       message, descriptor->FindFieldByName("foo_enum"))->number());
    634   EXPECT_EQ(&unittest::TestOneof2::NestedMessage::default_instance(),
    635             &reflection->GetMessage(
    636                 message, descriptor->FindFieldByName("foo_message")));
    637   EXPECT_EQ(&unittest::TestOneof2::FooGroup::default_instance(),
    638             &reflection->GetMessage(
    639                 message, descriptor->FindFieldByName("foogroup")));
    640   EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(),
    641             &reflection->GetMessage(
    642                 message, descriptor->FindFieldByName("foo_lazy_message")));
    643   EXPECT_EQ(5, reflection->GetInt32(
    644       message, descriptor->FindFieldByName("bar_int")));
    645   EXPECT_EQ("STRING", reflection->GetString(
    646       message, descriptor->FindFieldByName("bar_string")));
    647   EXPECT_EQ("CORD", reflection->GetString(
    648       message, descriptor->FindFieldByName("bar_cord")));
    649   EXPECT_EQ("SPIECE", reflection->GetString(
    650       message, descriptor->FindFieldByName("bar_string_piece")));
    651   EXPECT_EQ("BYTES", reflection->GetString(
    652       message, descriptor->FindFieldByName("bar_bytes")));
    653   EXPECT_EQ(unittest::TestOneof2::BAR, reflection->GetEnum(
    654       message, descriptor->FindFieldByName("bar_enum"))->number());
    655 
    656   // Check Set functions.
    657   reflection->SetInt32(
    658       &message, descriptor->FindFieldByName("foo_int"), 123);
    659   EXPECT_EQ(123, reflection->GetInt32(
    660       message, descriptor->FindFieldByName("foo_int")));
    661   reflection->SetString(
    662       &message, descriptor->FindFieldByName("foo_string"), "abc");
    663   EXPECT_EQ("abc", reflection->GetString(
    664       message, descriptor->FindFieldByName("foo_string")));
    665   reflection->SetString(
    666       &message, descriptor->FindFieldByName("foo_bytes"), "bytes");
    667   EXPECT_EQ("bytes", reflection->GetString(
    668       message, descriptor->FindFieldByName("foo_bytes")));
    669   reflection->SetString(
    670       &message, descriptor->FindFieldByName("bar_cord"), "change_cord");
    671   EXPECT_EQ("change_cord", reflection->GetString(
    672       message, descriptor->FindFieldByName("bar_cord")));
    673   reflection->SetString(
    674       &message, descriptor->FindFieldByName("bar_string_piece"),
    675       "change_spiece");
    676   EXPECT_EQ("change_spiece", reflection->GetString(
    677       message, descriptor->FindFieldByName("bar_string_piece")));
    678 }
    679 
    680 TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) {
    681   unittest::TestOneof2 from_message1;
    682   unittest::TestOneof2 from_message2;
    683   unittest::TestOneof2 to_message;
    684   const Descriptor* descriptor = unittest::TestOneof2::descriptor();
    685   const Reflection* reflection = to_message.GetReflection();
    686 
    687   Message* released = reflection->ReleaseMessage(
    688       &to_message, descriptor->FindFieldByName("foo_lazy_message"));
    689   EXPECT_TRUE(released == NULL);
    690   released = reflection->ReleaseMessage(
    691       &to_message, descriptor->FindFieldByName("foo_message"));
    692   EXPECT_TRUE(released == NULL);
    693 
    694   TestUtil::ReflectionTester::SetOneofViaReflection(&from_message1);
    695   TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1);
    696 
    697   TestUtil::ReflectionTester::
    698       SetAllocatedOptionalMessageFieldsToMessageViaReflection(
    699           &from_message1, &to_message);
    700   const Message& sub_message = reflection->GetMessage(
    701       to_message, descriptor->FindFieldByName("foo_lazy_message"));
    702   released = reflection->ReleaseMessage(
    703       &to_message, descriptor->FindFieldByName("foo_lazy_message"));
    704   EXPECT_TRUE(released != NULL);
    705   EXPECT_EQ(&sub_message, released);
    706   delete released;
    707 
    708   TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2);
    709 
    710   reflection->MutableMessage(
    711       &from_message2, descriptor->FindFieldByName("foo_message"));
    712 
    713   TestUtil::ReflectionTester::
    714       SetAllocatedOptionalMessageFieldsToMessageViaReflection(
    715           &from_message2, &to_message);
    716 
    717   const Message& sub_message2 = reflection->GetMessage(
    718       to_message, descriptor->FindFieldByName("foo_message"));
    719   released = reflection->ReleaseMessage(
    720       &to_message, descriptor->FindFieldByName("foo_message"));
    721   EXPECT_TRUE(released != NULL);
    722   EXPECT_EQ(&sub_message2, released);
    723   delete released;
    724 }
    725 
    726 TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) {
    727   unittest::TestAllTypes message;
    728   TestUtil::ReflectionTester reflection_tester(
    729     unittest::TestAllTypes::descriptor());
    730 
    731   // When nothing is set, we expect all released messages to be NULL.
    732   reflection_tester.ExpectMessagesReleasedViaReflection(
    733       &message, TestUtil::ReflectionTester::IS_NULL);
    734 
    735   // After fields are set we should get non-NULL releases.
    736   reflection_tester.SetAllFieldsViaReflection(&message);
    737   reflection_tester.ExpectMessagesReleasedViaReflection(
    738       &message, TestUtil::ReflectionTester::NOT_NULL);
    739 
    740   // After Clear() we may or may not get a message from ReleaseMessage().
    741   // This is implementation specific.
    742   reflection_tester.SetAllFieldsViaReflection(&message);
    743   message.Clear();
    744   reflection_tester.ExpectMessagesReleasedViaReflection(
    745       &message, TestUtil::ReflectionTester::CAN_BE_NULL);
    746 
    747   // Test a different code path for setting after releasing.
    748   TestUtil::SetAllFields(&message);
    749   TestUtil::ExpectAllFieldsSet(message);
    750 }
    751 
    752 TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) {
    753   unittest::TestAllExtensions message;
    754   TestUtil::ReflectionTester reflection_tester(
    755     unittest::TestAllExtensions::descriptor());
    756 
    757   // When nothing is set, we expect all released messages to be NULL.
    758   reflection_tester.ExpectMessagesReleasedViaReflection(
    759       &message, TestUtil::ReflectionTester::IS_NULL);
    760 
    761   // After fields are set we should get non-NULL releases.
    762   reflection_tester.SetAllFieldsViaReflection(&message);
    763   reflection_tester.ExpectMessagesReleasedViaReflection(
    764       &message, TestUtil::ReflectionTester::NOT_NULL);
    765 
    766   // After Clear() we may or may not get a message from ReleaseMessage().
    767   // This is implementation specific.
    768   reflection_tester.SetAllFieldsViaReflection(&message);
    769   message.Clear();
    770   reflection_tester.ExpectMessagesReleasedViaReflection(
    771       &message, TestUtil::ReflectionTester::CAN_BE_NULL);
    772 
    773   // Test a different code path for setting after releasing.
    774   TestUtil::SetAllExtensions(&message);
    775   TestUtil::ExpectAllExtensionsSet(message);
    776 }
    777 
    778 TEST(GeneratedMessageReflectionTest, ReleaseOneofMessageTest) {
    779   unittest::TestOneof2 message;
    780   TestUtil::ReflectionTester::SetOneofViaReflection(&message);
    781 
    782   const Descriptor* descriptor = unittest::TestOneof2::descriptor();
    783   const Reflection* reflection = message.GetReflection();
    784   const Message& sub_message = reflection->GetMessage(
    785       message, descriptor->FindFieldByName("foo_lazy_message"));
    786   Message* released = reflection->ReleaseMessage(
    787       &message, descriptor->FindFieldByName("foo_lazy_message"));
    788 
    789   EXPECT_TRUE(released != NULL);
    790   EXPECT_EQ(&sub_message, released);
    791   delete released;
    792 
    793   released = reflection->ReleaseMessage(
    794       &message, descriptor->FindFieldByName("foo_lazy_message"));
    795   EXPECT_TRUE(released == NULL);
    796 }
    797 
    798 #ifdef PROTOBUF_HAS_DEATH_TEST
    799 
    800 TEST(GeneratedMessageReflectionTest, UsageErrors) {
    801   unittest::TestAllTypes message;
    802   const Reflection* reflection = message.GetReflection();
    803   const Descriptor* descriptor = message.GetDescriptor();
    804 
    805 #define f(NAME) descriptor->FindFieldByName(NAME)
    806 
    807   // Testing every single failure mode would be too much work.  Let's just
    808   // check a few.
    809   EXPECT_DEATH(
    810     reflection->GetInt32(
    811       message, descriptor->FindFieldByName("optional_int64")),
    812     "Protocol Buffer reflection usage error:\n"
    813     "  Method      : google::protobuf::Reflection::GetInt32\n"
    814     "  Message type: protobuf_unittest\\.TestAllTypes\n"
    815     "  Field       : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
    816     "  Problem     : Field is not the right type for this message:\n"
    817     "    Expected  : CPPTYPE_INT32\n"
    818     "    Field type: CPPTYPE_INT64");
    819   EXPECT_DEATH(
    820     reflection->GetInt32(
    821       message, descriptor->FindFieldByName("repeated_int32")),
    822     "Protocol Buffer reflection usage error:\n"
    823     "  Method      : google::protobuf::Reflection::GetInt32\n"
    824     "  Message type: protobuf_unittest.TestAllTypes\n"
    825     "  Field       : protobuf_unittest.TestAllTypes.repeated_int32\n"
    826     "  Problem     : Field is repeated; the method requires a singular field.");
    827   EXPECT_DEATH(
    828     reflection->GetInt32(
    829       message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
    830     "Protocol Buffer reflection usage error:\n"
    831     "  Method      : google::protobuf::Reflection::GetInt32\n"
    832     "  Message type: protobuf_unittest.TestAllTypes\n"
    833     "  Field       : protobuf_unittest.ForeignMessage.c\n"
    834     "  Problem     : Field does not match message type.");
    835   EXPECT_DEATH(
    836     reflection->HasField(
    837       message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
    838     "Protocol Buffer reflection usage error:\n"
    839     "  Method      : google::protobuf::Reflection::HasField\n"
    840     "  Message type: protobuf_unittest.TestAllTypes\n"
    841     "  Field       : protobuf_unittest.ForeignMessage.c\n"
    842     "  Problem     : Field does not match message type.");
    843 
    844 #undef f
    845 }
    846 
    847 #endif  // PROTOBUF_HAS_DEATH_TEST
    848 
    849 
    850 }  // namespace
    851 }  // namespace protobuf
    852 }  // namespace google
    853