1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // Since the reflection interface for DynamicMessage is implemented by
36 // GenericMessageReflection, the only thing we really have to test is
37 // that DynamicMessage correctly sets up the information that
38 // GenericMessageReflection needs to use. So, we focus on that in this
39 // test. Other tests, such as generic_message_reflection_unittest and
40 // reflection_ops_unittest, cover the rest of the functionality used by
41 // DynamicMessage.
42
43 #include <memory>
44 #ifndef _SHARED_PTR_H
45 #include <google/protobuf/stubs/shared_ptr.h>
46 #endif
47
48 #include <google/protobuf/dynamic_message.h>
49 #include <google/protobuf/descriptor.h>
50 #include <google/protobuf/descriptor.pb.h>
51 #include <google/protobuf/test_util.h>
52 #include <google/protobuf/unittest.pb.h>
53 #include <google/protobuf/unittest_no_field_presence.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 class DynamicMessageTest : public testing::Test {
64 protected:
65 DescriptorPool pool_;
66 DynamicMessageFactory factory_;
67 const Descriptor* descriptor_;
68 const Message* prototype_;
69 const Descriptor* extensions_descriptor_;
70 const Message* extensions_prototype_;
71 const Descriptor* packed_descriptor_;
72 const Message* packed_prototype_;
73 const Descriptor* oneof_descriptor_;
74 const Message* oneof_prototype_;
75 const Descriptor* proto3_descriptor_;
76 const Message* proto3_prototype_;
77
DynamicMessageTest()78 DynamicMessageTest(): factory_(&pool_) {}
79
SetUp()80 virtual void SetUp() {
81 // We want to make sure that DynamicMessage works (particularly with
82 // extensions) even if we use descriptors that are *not* from compiled-in
83 // types, so we make copies of the descriptors for unittest.proto and
84 // unittest_import.proto.
85 FileDescriptorProto unittest_file;
86 FileDescriptorProto unittest_import_file;
87 FileDescriptorProto unittest_import_public_file;
88 FileDescriptorProto unittest_no_field_presence_file;
89
90 unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file);
91 unittest_import::ImportMessage::descriptor()->file()->CopyTo(
92 &unittest_import_file);
93 unittest_import::PublicImportMessage::descriptor()->file()->CopyTo(
94 &unittest_import_public_file);
95 proto2_nofieldpresence_unittest::TestAllTypes::descriptor()->
96 file()->CopyTo(&unittest_no_field_presence_file);
97
98 ASSERT_TRUE(pool_.BuildFile(unittest_import_public_file) != NULL);
99 ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL);
100 ASSERT_TRUE(pool_.BuildFile(unittest_file) != NULL);
101 ASSERT_TRUE(pool_.BuildFile(unittest_no_field_presence_file) != NULL);
102
103 descriptor_ = pool_.FindMessageTypeByName("protobuf_unittest.TestAllTypes");
104 ASSERT_TRUE(descriptor_ != NULL);
105 prototype_ = factory_.GetPrototype(descriptor_);
106
107 extensions_descriptor_ =
108 pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions");
109 ASSERT_TRUE(extensions_descriptor_ != NULL);
110 extensions_prototype_ = factory_.GetPrototype(extensions_descriptor_);
111
112 packed_descriptor_ =
113 pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes");
114 ASSERT_TRUE(packed_descriptor_ != NULL);
115 packed_prototype_ = factory_.GetPrototype(packed_descriptor_);
116
117 oneof_descriptor_ =
118 pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2");
119 ASSERT_TRUE(oneof_descriptor_ != NULL);
120 oneof_prototype_ = factory_.GetPrototype(oneof_descriptor_);
121
122 proto3_descriptor_ =
123 pool_.FindMessageTypeByName(
124 "proto2_nofieldpresence_unittest.TestAllTypes");
125 ASSERT_TRUE(proto3_descriptor_ != NULL);
126 proto3_prototype_ = factory_.GetPrototype(proto3_descriptor_);
127 }
128 };
129
TEST_F(DynamicMessageTest,Descriptor)130 TEST_F(DynamicMessageTest, Descriptor) {
131 // Check that the descriptor on the DynamicMessage matches the descriptor
132 // passed to GetPrototype().
133 EXPECT_EQ(prototype_->GetDescriptor(), descriptor_);
134 }
135
TEST_F(DynamicMessageTest,OnePrototype)136 TEST_F(DynamicMessageTest, OnePrototype) {
137 // Check that requesting the same prototype twice produces the same object.
138 EXPECT_EQ(prototype_, factory_.GetPrototype(descriptor_));
139 }
140
TEST_F(DynamicMessageTest,Defaults)141 TEST_F(DynamicMessageTest, Defaults) {
142 // Check that all default values are set correctly in the initial message.
143 TestUtil::ReflectionTester reflection_tester(descriptor_);
144 reflection_tester.ExpectClearViaReflection(*prototype_);
145 }
146
TEST_F(DynamicMessageTest,IndependentOffsets)147 TEST_F(DynamicMessageTest, IndependentOffsets) {
148 // Check that all fields have independent offsets by setting each
149 // one to a unique value then checking that they all still have those
150 // unique values (i.e. they don't stomp each other).
151 google::protobuf::scoped_ptr<Message> message(prototype_->New());
152 TestUtil::ReflectionTester reflection_tester(descriptor_);
153
154 reflection_tester.SetAllFieldsViaReflection(message.get());
155 reflection_tester.ExpectAllFieldsSetViaReflection(*message);
156 }
157
TEST_F(DynamicMessageTest,Extensions)158 TEST_F(DynamicMessageTest, Extensions) {
159 // Check that extensions work.
160 google::protobuf::scoped_ptr<Message> message(extensions_prototype_->New());
161 TestUtil::ReflectionTester reflection_tester(extensions_descriptor_);
162
163 reflection_tester.SetAllFieldsViaReflection(message.get());
164 reflection_tester.ExpectAllFieldsSetViaReflection(*message);
165 }
166
TEST_F(DynamicMessageTest,PackedFields)167 TEST_F(DynamicMessageTest, PackedFields) {
168 // Check that packed fields work properly.
169 google::protobuf::scoped_ptr<Message> message(packed_prototype_->New());
170 TestUtil::ReflectionTester reflection_tester(packed_descriptor_);
171
172 reflection_tester.SetPackedFieldsViaReflection(message.get());
173 reflection_tester.ExpectPackedFieldsSetViaReflection(*message);
174 }
175
TEST_F(DynamicMessageTest,Oneof)176 TEST_F(DynamicMessageTest, Oneof) {
177 // Check that oneof fields work properly.
178 google::protobuf::scoped_ptr<Message> message(oneof_prototype_->New());
179
180 // Check default values.
181 const Descriptor* descriptor = message->GetDescriptor();
182 const Reflection* reflection = message->GetReflection();
183 EXPECT_EQ(0, reflection->GetInt32(
184 *message, descriptor->FindFieldByName("foo_int")));
185 EXPECT_EQ("", reflection->GetString(
186 *message, descriptor->FindFieldByName("foo_string")));
187 EXPECT_EQ("", reflection->GetString(
188 *message, descriptor->FindFieldByName("foo_cord")));
189 EXPECT_EQ("", reflection->GetString(
190 *message, descriptor->FindFieldByName("foo_string_piece")));
191 EXPECT_EQ("", reflection->GetString(
192 *message, descriptor->FindFieldByName("foo_bytes")));
193 EXPECT_EQ(unittest::TestOneof2::FOO, reflection->GetEnum(
194 *message, descriptor->FindFieldByName("foo_enum"))->number());
195 const Descriptor* nested_descriptor;
196 const Message* nested_prototype;
197 nested_descriptor =
198 pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.NestedMessage");
199 nested_prototype = factory_.GetPrototype(nested_descriptor);
200 EXPECT_EQ(nested_prototype,
201 &reflection->GetMessage(
202 *message, descriptor->FindFieldByName("foo_message")));
203 const Descriptor* foogroup_descriptor;
204 const Message* foogroup_prototype;
205 foogroup_descriptor =
206 pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.FooGroup");
207 foogroup_prototype = factory_.GetPrototype(foogroup_descriptor);
208 EXPECT_EQ(foogroup_prototype,
209 &reflection->GetMessage(
210 *message, descriptor->FindFieldByName("foogroup")));
211 EXPECT_NE(foogroup_prototype,
212 &reflection->GetMessage(
213 *message, descriptor->FindFieldByName("foo_lazy_message")));
214 EXPECT_EQ(5, reflection->GetInt32(
215 *message, descriptor->FindFieldByName("bar_int")));
216 EXPECT_EQ("STRING", reflection->GetString(
217 *message, descriptor->FindFieldByName("bar_string")));
218 EXPECT_EQ("CORD", reflection->GetString(
219 *message, descriptor->FindFieldByName("bar_cord")));
220 EXPECT_EQ("SPIECE", reflection->GetString(
221 *message, descriptor->FindFieldByName("bar_string_piece")));
222 EXPECT_EQ("BYTES", reflection->GetString(
223 *message, descriptor->FindFieldByName("bar_bytes")));
224 EXPECT_EQ(unittest::TestOneof2::BAR, reflection->GetEnum(
225 *message, descriptor->FindFieldByName("bar_enum"))->number());
226
227 // Check set functions.
228 TestUtil::ReflectionTester reflection_tester(oneof_descriptor_);
229 reflection_tester.SetOneofViaReflection(message.get());
230 reflection_tester.ExpectOneofSetViaReflection(*message);
231 }
232
TEST_F(DynamicMessageTest,SpaceUsed)233 TEST_F(DynamicMessageTest, SpaceUsed) {
234 // Test that SpaceUsed() works properly
235
236 // Since we share the implementation with generated messages, we don't need
237 // to test very much here. Just make sure it appears to be working.
238
239 google::protobuf::scoped_ptr<Message> message(prototype_->New());
240 TestUtil::ReflectionTester reflection_tester(descriptor_);
241
242 int initial_space_used = message->SpaceUsed();
243
244 reflection_tester.SetAllFieldsViaReflection(message.get());
245 EXPECT_LT(initial_space_used, message->SpaceUsed());
246 }
247
TEST_F(DynamicMessageTest,Arena)248 TEST_F(DynamicMessageTest, Arena) {
249 Arena arena;
250 Message* message = prototype_->New(&arena);
251 (void)message; // avoid unused-variable error.
252 // Return without freeing: should not leak.
253 }
254
TEST_F(DynamicMessageTest,Proto3)255 TEST_F(DynamicMessageTest, Proto3) {
256 Message* message = proto3_prototype_->New();
257 const Reflection* refl = message->GetReflection();
258 const Descriptor* desc = message->GetDescriptor();
259
260 // Just test a single primtive and single message field here to make sure we
261 // are getting the no-field-presence semantics elsewhere. DynamicMessage uses
262 // GeneratedMessageReflection under the hood, so the rest should be fine as
263 // long as GMR recognizes that we're using a proto3 message.
264 const FieldDescriptor* optional_int32 =
265 desc->FindFieldByName("optional_int32");
266 const FieldDescriptor* optional_msg =
267 desc->FindFieldByName("optional_nested_message");
268 EXPECT_TRUE(optional_int32 != NULL);
269 EXPECT_TRUE(optional_msg != NULL);
270
271 EXPECT_EQ(false, refl->HasField(*message, optional_int32));
272 refl->SetInt32(message, optional_int32, 42);
273 EXPECT_EQ(true, refl->HasField(*message, optional_int32));
274 refl->SetInt32(message, optional_int32, 0);
275 EXPECT_EQ(false, refl->HasField(*message, optional_int32));
276
277 EXPECT_EQ(false, refl->HasField(*message, optional_msg));
278 refl->MutableMessage(message, optional_msg);
279 EXPECT_EQ(true, refl->HasField(*message, optional_msg));
280 delete refl->ReleaseMessage(message, optional_msg);
281 EXPECT_EQ(false, refl->HasField(*message, optional_msg));
282
283 // Also ensure that the default instance handles field presence properly.
284 EXPECT_EQ(false, refl->HasField(*proto3_prototype_, optional_msg));
285
286 delete message;
287 }
288
289
290 } // namespace protobuf
291 } // namespace google
292