• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <memory>
32 #include <string>
33 #include <vector>
34 
35 #include <google/protobuf/test_util.h>
36 #include <google/protobuf/unittest.pb.h>
37 #include <google/protobuf/unittest_proto3_arena.pb.h>
38 #include <google/protobuf/arena.h>
39 #include <google/protobuf/testing/googletest.h>
40 #include <gtest/gtest.h>
41 
42 using proto3_arena_unittest::TestAllTypes;
43 
44 namespace google {
45 namespace protobuf {
46 namespace {
47 // We selectively set/check a few representative fields rather than all fields
48 // as this test is only expected to cover the basics of arena support.
SetAllFields(TestAllTypes * m)49 void SetAllFields(TestAllTypes* m) {
50   m->set_optional_int32(100);
51   m->set_optional_string("asdf");
52   m->set_optional_bytes("jkl;");
53   m->mutable_optional_nested_message()->set_bb(42);
54   m->mutable_optional_foreign_message()->set_c(43);
55   m->set_optional_nested_enum(
56       proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ);
57   m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
58   m->mutable_optional_lazy_message()->set_bb(45);
59   m->add_repeated_int32(100);
60   m->add_repeated_string("asdf");
61   m->add_repeated_bytes("jkl;");
62   m->add_repeated_nested_message()->set_bb(46);
63   m->add_repeated_foreign_message()->set_c(47);
64   m->add_repeated_nested_enum(
65       proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ);
66   m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
67   m->add_repeated_lazy_message()->set_bb(49);
68 
69   m->set_oneof_uint32(1);
70   m->mutable_oneof_nested_message()->set_bb(50);
71   m->set_oneof_string("test");  // only this one remains set
72 }
73 
ExpectAllFieldsSet(const TestAllTypes & m)74 void ExpectAllFieldsSet(const TestAllTypes& m) {
75   EXPECT_EQ(100, m.optional_int32());
76   EXPECT_EQ("asdf", m.optional_string());
77   EXPECT_EQ("jkl;", m.optional_bytes());
78   EXPECT_EQ(true, m.has_optional_nested_message());
79   EXPECT_EQ(42, m.optional_nested_message().bb());
80   EXPECT_EQ(true, m.has_optional_foreign_message());
81   EXPECT_EQ(43, m.optional_foreign_message().c());
82   EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ,
83             m.optional_nested_enum());
84   EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum());
85   EXPECT_EQ(true, m.has_optional_lazy_message());
86   EXPECT_EQ(45, m.optional_lazy_message().bb());
87 
88   EXPECT_EQ(1, m.repeated_int32_size());
89   EXPECT_EQ(100, m.repeated_int32(0));
90   EXPECT_EQ(1, m.repeated_string_size());
91   EXPECT_EQ("asdf", m.repeated_string(0));
92   EXPECT_EQ(1, m.repeated_bytes_size());
93   EXPECT_EQ("jkl;", m.repeated_bytes(0));
94   EXPECT_EQ(1, m.repeated_nested_message_size());
95   EXPECT_EQ(46, m.repeated_nested_message(0).bb());
96   EXPECT_EQ(1, m.repeated_foreign_message_size());
97   EXPECT_EQ(47, m.repeated_foreign_message(0).c());
98   EXPECT_EQ(1, m.repeated_nested_enum_size());
99   EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ,
100             m.repeated_nested_enum(0));
101   EXPECT_EQ(1, m.repeated_foreign_enum_size());
102   EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0));
103   EXPECT_EQ(1, m.repeated_lazy_message_size());
104   EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
105 
106   EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
107             m.oneof_field_case());
108   EXPECT_EQ("test", m.oneof_string());
109 }
110 
111 // In this file we only test some basic functionalities of arena support in
112 // proto3 and expect the arena support to be fully tested in proto2 unittests
113 // because proto3 shares most code with proto2.
114 
TEST(Proto3ArenaTest,Parsing)115 TEST(Proto3ArenaTest, Parsing) {
116   TestAllTypes original;
117   SetAllFields(&original);
118 
119   Arena arena;
120   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
121   arena_message->ParseFromString(original.SerializeAsString());
122   ExpectAllFieldsSet(*arena_message);
123 }
124 
TEST(Proto3ArenaTest,UnknownFields)125 TEST(Proto3ArenaTest, UnknownFields) {
126   TestAllTypes original;
127   SetAllFields(&original);
128 
129   Arena arena;
130   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
131   arena_message->ParseFromString(original.SerializeAsString());
132   ExpectAllFieldsSet(*arena_message);
133 
134   // In proto3 we can still get a pointer to the UnknownFieldSet through
135   // reflection API.
136   UnknownFieldSet* unknown_fields =
137       arena_message->GetReflection()->MutableUnknownFields(arena_message);
138   // We can modify this UnknownFieldSet.
139   unknown_fields->AddVarint(1, 2);
140   // And the unknown fields should be changed.
141   ASSERT_NE(original.ByteSize(), arena_message->ByteSize());
142   ASSERT_FALSE(
143       arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
144 }
145 
TEST(Proto3ArenaTest,Swap)146 TEST(Proto3ArenaTest, Swap) {
147   Arena arena1;
148   Arena arena2;
149 
150   // Test Swap().
151   TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
152   TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
153   arena1_message->Swap(arena2_message);
154   EXPECT_EQ(&arena1, arena1_message->GetArena());
155   EXPECT_EQ(&arena2, arena2_message->GetArena());
156 }
157 
TEST(Proto3ArenaTest,SetAllocatedMessage)158 TEST(Proto3ArenaTest, SetAllocatedMessage) {
159   Arena arena;
160   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
161   TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
162   nested->set_bb(118);
163   arena_message->set_allocated_optional_nested_message(nested);
164   EXPECT_EQ(118, arena_message->optional_nested_message().bb());
165 }
166 
TEST(Proto3ArenaTest,ReleaseMessage)167 TEST(Proto3ArenaTest, ReleaseMessage) {
168   Arena arena;
169   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
170   arena_message->mutable_optional_nested_message()->set_bb(118);
171   std::unique_ptr<TestAllTypes::NestedMessage> nested(
172       arena_message->release_optional_nested_message());
173   EXPECT_EQ(118, nested->bb());
174 }
175 
TEST(Proto3ArenaTest,MessageFieldClear)176 TEST(Proto3ArenaTest, MessageFieldClear) {
177   // GitHub issue #310: https://github.com/protocolbuffers/protobuf/issues/310
178   Arena arena;
179   TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
180   arena_message->mutable_optional_nested_message()->set_bb(118);
181   // This should not crash, but prior to the bugfix, it tried to use `operator
182   // delete` the nested message (which is on the arena):
183   arena_message->Clear();
184 }
185 
TEST(Proto3ArenaTest,MessageFieldClearViaReflection)186 TEST(Proto3ArenaTest, MessageFieldClearViaReflection) {
187   Arena arena;
188   TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
189   const Reflection* r = message->GetReflection();
190   const Descriptor* d = message->GetDescriptor();
191   const FieldDescriptor* msg_field =
192       d->FindFieldByName("optional_nested_message");
193 
194   message->mutable_optional_nested_message()->set_bb(1);
195   r->ClearField(message, msg_field);
196   EXPECT_FALSE(message->has_optional_nested_message());
197   EXPECT_EQ(0, message->optional_nested_message().bb());
198 }
199 
200 }  // namespace
201 }  // namespace protobuf
202 }  // namespace google
203