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 #include <string>
     32 #include <memory>
     33 #ifndef _SHARED_PTR_H
     34 #include <google/protobuf/stubs/shared_ptr.h>
     35 #endif
     36 #include <vector>
     37 
     38 #include <google/protobuf/test_util.h>
     39 #include <google/protobuf/unittest.pb.h>
     40 #include <google/protobuf/unittest_proto3_arena.pb.h>
     41 #include <google/protobuf/arena.h>
     42 #include <google/protobuf/testing/googletest.h>
     43 #include <gtest/gtest.h>
     44 
     45 namespace google {
     46 using proto3_arena_unittest::TestAllTypes;
     47 
     48 namespace protobuf {
     49 namespace {
     50 // We selectively set/check a few representative fields rather than all fields
     51 // as this test is only expected to cover the basics of arena support.
     52 void SetAllFields(TestAllTypes* m) {
     53   m->set_optional_int32(100);
     54   m->set_optional_string("asdf");
     55   m->set_optional_bytes("jkl;");
     56   m->mutable_optional_nested_message()->set_bb(42);
     57   m->mutable_optional_foreign_message()->set_c(43);
     58   m->set_optional_nested_enum(
     59       proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ);
     60   m->set_optional_foreign_enum(
     61       proto3_arena_unittest::FOREIGN_BAZ);
     62   m->mutable_optional_lazy_message()->set_bb(45);
     63   m->add_repeated_int32(100);
     64   m->add_repeated_string("asdf");
     65   m->add_repeated_bytes("jkl;");
     66   m->add_repeated_nested_message()->set_bb(46);
     67   m->add_repeated_foreign_message()->set_c(47);
     68   m->add_repeated_nested_enum(
     69       proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ);
     70   m->add_repeated_foreign_enum(
     71       proto3_arena_unittest::FOREIGN_BAZ);
     72   m->add_repeated_lazy_message()->set_bb(49);
     73 
     74   m->set_oneof_uint32(1);
     75   m->mutable_oneof_nested_message()->set_bb(50);
     76   m->set_oneof_string("test");  // only this one remains set
     77 }
     78 
     79 void ExpectAllFieldsSet(const TestAllTypes& m) {
     80   EXPECT_EQ(100, m.optional_int32());
     81   EXPECT_EQ("asdf", m.optional_string());
     82   EXPECT_EQ("jkl;", m.optional_bytes());
     83   EXPECT_EQ(true, m.has_optional_nested_message());
     84   EXPECT_EQ(42, m.optional_nested_message().bb());
     85   EXPECT_EQ(true, m.has_optional_foreign_message());
     86   EXPECT_EQ(43, m.optional_foreign_message().c());
     87   EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ,
     88             m.optional_nested_enum());
     89   EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ,
     90             m.optional_foreign_enum());
     91   EXPECT_EQ(true, m.has_optional_lazy_message());
     92   EXPECT_EQ(45, m.optional_lazy_message().bb());
     93 
     94   EXPECT_EQ(1, m.repeated_int32_size());
     95   EXPECT_EQ(100, m.repeated_int32(0));
     96   EXPECT_EQ(1, m.repeated_string_size());
     97   EXPECT_EQ("asdf", m.repeated_string(0));
     98   EXPECT_EQ(1, m.repeated_bytes_size());
     99   EXPECT_EQ("jkl;", m.repeated_bytes(0));
    100   EXPECT_EQ(1, m.repeated_nested_message_size());
    101   EXPECT_EQ(46, m.repeated_nested_message(0).bb());
    102   EXPECT_EQ(1, m.repeated_foreign_message_size());
    103   EXPECT_EQ(47, m.repeated_foreign_message(0).c());
    104   EXPECT_EQ(1, m.repeated_nested_enum_size());
    105   EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ,
    106             m.repeated_nested_enum(0));
    107   EXPECT_EQ(1, m.repeated_foreign_enum_size());
    108   EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ,
    109             m.repeated_foreign_enum(0));
    110   EXPECT_EQ(1, m.repeated_lazy_message_size());
    111   EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
    112 
    113   EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
    114             m.oneof_field_case());
    115   EXPECT_EQ("test", m.oneof_string());
    116 }
    117 
    118 // In this file we only test some basic functionalities of arena support in
    119 // proto3 and expect the arena support to be fully tested in proto2 unittests
    120 // because proto3 shares most code with proto2.
    121 
    122 TEST(Proto3ArenaTest, Parsing) {
    123   TestAllTypes original;
    124   SetAllFields(&original);
    125 
    126   Arena arena;
    127   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
    128   arena_message->ParseFromString(original.SerializeAsString());
    129   ExpectAllFieldsSet(*arena_message);
    130 }
    131 
    132 TEST(Proto3ArenaTest, UnknownFields) {
    133   TestAllTypes original;
    134   SetAllFields(&original);
    135 
    136   Arena arena;
    137   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
    138   arena_message->ParseFromString(original.SerializeAsString());
    139   ExpectAllFieldsSet(*arena_message);
    140 
    141   // In proto3 we can still get a pointer to the UnknownFieldSet through
    142   // reflection API.
    143   UnknownFieldSet* unknown_fields =
    144       arena_message->GetReflection()->MutableUnknownFields(arena_message);
    145   // We can modify this UnknownFieldSet.
    146   unknown_fields->AddVarint(1, 2);
    147   // But the change will never will serialized back.
    148   ASSERT_EQ(original.ByteSize(), arena_message->ByteSize());
    149   ASSERT_TRUE(
    150       arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
    151 }
    152 
    153 TEST(Proto3ArenaTest, Swap) {
    154   Arena arena1;
    155   Arena arena2;
    156 
    157   // Test Swap().
    158   TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
    159   TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
    160   arena1_message->Swap(arena2_message);
    161   EXPECT_EQ(&arena1, arena1_message->GetArena());
    162   EXPECT_EQ(&arena2, arena2_message->GetArena());
    163 }
    164 
    165 TEST(Proto3ArenaTest, SetAllocatedMessage) {
    166   Arena arena;
    167   TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
    168   TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
    169   nested->set_bb(118);
    170   arena_message->set_allocated_optional_nested_message(nested);
    171   EXPECT_EQ(118, arena_message->optional_nested_message().bb());
    172 }
    173 
    174 TEST(Proto3ArenaTest, ReleaseMessage) {
    175   Arena arena;
    176   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
    177   arena_message->mutable_optional_nested_message()->set_bb(118);
    178   google::protobuf::scoped_ptr<TestAllTypes::NestedMessage> nested(
    179       arena_message->release_optional_nested_message());
    180   EXPECT_EQ(118, nested->bb());
    181 }
    182 
    183 TEST(Proto3ArenaTest, MessageFieldClear) {
    184   // GitHub issue #310: https://github.com/google/protobuf/issues/310
    185   Arena arena;
    186   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
    187   arena_message->mutable_optional_nested_message()->set_bb(118);
    188   // This should not crash, but prior to the bugfix, it tried to use `operator
    189   // delete` the nested message (which is on the arena):
    190   arena_message->Clear();
    191 }
    192 
    193 TEST(Proto3ArenaTest, MessageFieldClearViaReflection) {
    194   Arena arena;
    195   TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
    196   const Reflection* r = message->GetReflection();
    197   const Descriptor* d = message->GetDescriptor();
    198   const FieldDescriptor* msg_field = d->FindFieldByName(
    199       "optional_nested_message");
    200 
    201   message->mutable_optional_nested_message()->set_bb(1);
    202   r->ClearField(message, msg_field);
    203   EXPECT_FALSE(message->has_optional_nested_message());
    204   EXPECT_EQ(0, message->optional_nested_message().bb());
    205 }
    206 
    207 }  // namespace
    208 }  // namespace protobuf
    209 }  // namespace google
    210