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
45 #include <google/protobuf/test_util.h>
46 #include <google/protobuf/unittest.pb.h>
47 #include <google/protobuf/unittest_no_field_presence.pb.h>
48 #include <google/protobuf/descriptor.pb.h>
49 #include <google/protobuf/descriptor.h>
50 #include <google/protobuf/dynamic_message.h>
51
52 #include <google/protobuf/stubs/logging.h>
53 #include <google/protobuf/stubs/common.h>
54 #include <google/protobuf/testing/googletest.h>
55 #include <gtest/gtest.h>
56
57 namespace google {
58 namespace protobuf {
59
60 class DynamicMessageTest : public ::testing::TestWithParam<bool> {
61 protected:
62 DescriptorPool pool_;
63 DynamicMessageFactory factory_;
64 const Descriptor* descriptor_;
65 const Message* prototype_;
66 const Descriptor* extensions_descriptor_;
67 const Message* extensions_prototype_;
68 const Descriptor* packed_descriptor_;
69 const Message* packed_prototype_;
70 const Descriptor* oneof_descriptor_;
71 const Message* oneof_prototype_;
72 const Descriptor* proto3_descriptor_;
73 const Message* proto3_prototype_;
74
DynamicMessageTest()75 DynamicMessageTest() : factory_(&pool_) {}
76
SetUp()77 virtual void SetUp() {
78 // We want to make sure that DynamicMessage works (particularly with
79 // extensions) even if we use descriptors that are *not* from compiled-in
80 // types, so we make copies of the descriptors for unittest.proto and
81 // unittest_import.proto.
82 FileDescriptorProto unittest_file;
83 FileDescriptorProto unittest_import_file;
84 FileDescriptorProto unittest_import_public_file;
85 FileDescriptorProto unittest_no_field_presence_file;
86
87 unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file);
88 unittest_import::ImportMessage::descriptor()->file()->CopyTo(
89 &unittest_import_file);
90 unittest_import::PublicImportMessage::descriptor()->file()->CopyTo(
91 &unittest_import_public_file);
92 proto2_nofieldpresence_unittest::TestAllTypes::descriptor()->file()->CopyTo(
93 &unittest_no_field_presence_file);
94
95 ASSERT_TRUE(pool_.BuildFile(unittest_import_public_file) != NULL);
96 ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL);
97 ASSERT_TRUE(pool_.BuildFile(unittest_file) != NULL);
98 ASSERT_TRUE(pool_.BuildFile(unittest_no_field_presence_file) != NULL);
99
100 descriptor_ = pool_.FindMessageTypeByName("protobuf_unittest.TestAllTypes");
101 ASSERT_TRUE(descriptor_ != NULL);
102 prototype_ = factory_.GetPrototype(descriptor_);
103
104 extensions_descriptor_ =
105 pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions");
106 ASSERT_TRUE(extensions_descriptor_ != NULL);
107 extensions_prototype_ = factory_.GetPrototype(extensions_descriptor_);
108
109 packed_descriptor_ =
110 pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes");
111 ASSERT_TRUE(packed_descriptor_ != NULL);
112 packed_prototype_ = factory_.GetPrototype(packed_descriptor_);
113
114 oneof_descriptor_ =
115 pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2");
116 ASSERT_TRUE(oneof_descriptor_ != NULL);
117 oneof_prototype_ = factory_.GetPrototype(oneof_descriptor_);
118
119 proto3_descriptor_ = pool_.FindMessageTypeByName(
120 "proto2_nofieldpresence_unittest.TestAllTypes");
121 ASSERT_TRUE(proto3_descriptor_ != NULL);
122 proto3_prototype_ = factory_.GetPrototype(proto3_descriptor_);
123 }
124 };
125
TEST_F(DynamicMessageTest,Descriptor)126 TEST_F(DynamicMessageTest, Descriptor) {
127 // Check that the descriptor on the DynamicMessage matches the descriptor
128 // passed to GetPrototype().
129 EXPECT_EQ(prototype_->GetDescriptor(), descriptor_);
130 }
131
TEST_F(DynamicMessageTest,OnePrototype)132 TEST_F(DynamicMessageTest, OnePrototype) {
133 // Check that requesting the same prototype twice produces the same object.
134 EXPECT_EQ(prototype_, factory_.GetPrototype(descriptor_));
135 }
136
TEST_F(DynamicMessageTest,Defaults)137 TEST_F(DynamicMessageTest, Defaults) {
138 // Check that all default values are set correctly in the initial message.
139 TestUtil::ReflectionTester reflection_tester(descriptor_);
140 reflection_tester.ExpectClearViaReflection(*prototype_);
141 }
142
TEST_P(DynamicMessageTest,IndependentOffsets)143 TEST_P(DynamicMessageTest, IndependentOffsets) {
144 // Check that all fields have independent offsets by setting each
145 // one to a unique value then checking that they all still have those
146 // unique values (i.e. they don't stomp each other).
147 Arena arena;
148 Message* message = prototype_->New(GetParam() ? &arena : NULL);
149 TestUtil::ReflectionTester reflection_tester(descriptor_);
150
151 reflection_tester.SetAllFieldsViaReflection(message);
152 reflection_tester.ExpectAllFieldsSetViaReflection(*message);
153
154 if (!GetParam()) {
155 delete message;
156 }
157 }
158
TEST_P(DynamicMessageTest,Extensions)159 TEST_P(DynamicMessageTest, Extensions) {
160 // Check that extensions work.
161 Arena arena;
162 Message* message = extensions_prototype_->New(GetParam() ? &arena : NULL);
163 TestUtil::ReflectionTester reflection_tester(extensions_descriptor_);
164
165 reflection_tester.SetAllFieldsViaReflection(message);
166 reflection_tester.ExpectAllFieldsSetViaReflection(*message);
167
168 if (!GetParam()) {
169 delete message;
170 }
171 }
172
TEST_P(DynamicMessageTest,PackedFields)173 TEST_P(DynamicMessageTest, PackedFields) {
174 // Check that packed fields work properly.
175 Arena arena;
176 Message* message = packed_prototype_->New(GetParam() ? &arena : NULL);
177 TestUtil::ReflectionTester reflection_tester(packed_descriptor_);
178
179 reflection_tester.SetPackedFieldsViaReflection(message);
180 reflection_tester.ExpectPackedFieldsSetViaReflection(*message);
181
182 if (!GetParam()) {
183 delete message;
184 }
185 }
186
TEST_P(DynamicMessageTest,Oneof)187 TEST_P(DynamicMessageTest, Oneof) {
188 // Check that oneof fields work properly.
189 Arena arena;
190 Message* message = oneof_prototype_->New(GetParam() ? &arena : NULL);
191
192 // Check default values.
193 const Descriptor* descriptor = message->GetDescriptor();
194 const Reflection* reflection = message->GetReflection();
195 EXPECT_EQ(0, reflection->GetInt32(*message,
196 descriptor->FindFieldByName("foo_int")));
197 EXPECT_EQ("", reflection->GetString(
198 *message, descriptor->FindFieldByName("foo_string")));
199 EXPECT_EQ("", reflection->GetString(*message,
200 descriptor->FindFieldByName("foo_cord")));
201 EXPECT_EQ("", reflection->GetString(
202 *message, descriptor->FindFieldByName("foo_string_piece")));
203 EXPECT_EQ("", reflection->GetString(
204 *message, descriptor->FindFieldByName("foo_bytes")));
205 EXPECT_EQ(
206 unittest::TestOneof2::FOO,
207 reflection->GetEnum(*message, descriptor->FindFieldByName("foo_enum"))
208 ->number());
209 const Descriptor* nested_descriptor;
210 const Message* nested_prototype;
211 nested_descriptor =
212 pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.NestedMessage");
213 nested_prototype = factory_.GetPrototype(nested_descriptor);
214 EXPECT_EQ(nested_prototype,
215 &reflection->GetMessage(
216 *message, descriptor->FindFieldByName("foo_message")));
217 const Descriptor* foogroup_descriptor;
218 const Message* foogroup_prototype;
219 foogroup_descriptor =
220 pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.FooGroup");
221 foogroup_prototype = factory_.GetPrototype(foogroup_descriptor);
222 EXPECT_EQ(foogroup_prototype,
223 &reflection->GetMessage(*message,
224 descriptor->FindFieldByName("foogroup")));
225 EXPECT_NE(foogroup_prototype,
226 &reflection->GetMessage(
227 *message, descriptor->FindFieldByName("foo_lazy_message")));
228 EXPECT_EQ(5, reflection->GetInt32(*message,
229 descriptor->FindFieldByName("bar_int")));
230 EXPECT_EQ("STRING", reflection->GetString(
231 *message, descriptor->FindFieldByName("bar_string")));
232 EXPECT_EQ("CORD", reflection->GetString(
233 *message, descriptor->FindFieldByName("bar_cord")));
234 EXPECT_EQ("SPIECE",
235 reflection->GetString(
236 *message, descriptor->FindFieldByName("bar_string_piece")));
237 EXPECT_EQ("BYTES", reflection->GetString(
238 *message, descriptor->FindFieldByName("bar_bytes")));
239 EXPECT_EQ(
240 unittest::TestOneof2::BAR,
241 reflection->GetEnum(*message, descriptor->FindFieldByName("bar_enum"))
242 ->number());
243
244 // Check set functions.
245 TestUtil::ReflectionTester reflection_tester(oneof_descriptor_);
246 reflection_tester.SetOneofViaReflection(message);
247 reflection_tester.ExpectOneofSetViaReflection(*message);
248
249 if (!GetParam()) {
250 delete message;
251 }
252 }
253
TEST_P(DynamicMessageTest,SpaceUsed)254 TEST_P(DynamicMessageTest, SpaceUsed) {
255 // Test that SpaceUsedLong() works properly
256
257 // Since we share the implementation with generated messages, we don't need
258 // to test very much here. Just make sure it appears to be working.
259
260 Arena arena;
261 Message* message = prototype_->New(GetParam() ? &arena : NULL);
262 TestUtil::ReflectionTester reflection_tester(descriptor_);
263
264 size_t initial_space_used = message->SpaceUsedLong();
265
266 reflection_tester.SetAllFieldsViaReflection(message);
267 EXPECT_LT(initial_space_used, message->SpaceUsedLong());
268
269 if (!GetParam()) {
270 delete message;
271 }
272 }
273
TEST_F(DynamicMessageTest,Arena)274 TEST_F(DynamicMessageTest, Arena) {
275 Arena arena;
276 Message* message = prototype_->New(&arena);
277 Message* extension_message = extensions_prototype_->New(&arena);
278 Message* packed_message = packed_prototype_->New(&arena);
279 Message* oneof_message = oneof_prototype_->New(&arena);
280
281 // avoid unused-variable error.
282 (void)message;
283 (void)extension_message;
284 (void)packed_message;
285 (void)oneof_message;
286 // Return without freeing: should not leak.
287 }
288
TEST_F(DynamicMessageTest,Proto3)289 TEST_F(DynamicMessageTest, Proto3) {
290 Message* message = proto3_prototype_->New();
291 const Reflection* refl = message->GetReflection();
292 const Descriptor* desc = message->GetDescriptor();
293
294 // Just test a single primitive and single message field here to make sure we
295 // are getting the no-field-presence semantics elsewhere. DynamicMessage uses
296 // GeneratedMessageReflection under the hood, so the rest should be fine as
297 // long as GMR recognizes that we're using a proto3 message.
298 const FieldDescriptor* optional_int32 =
299 desc->FindFieldByName("optional_int32");
300 const FieldDescriptor* optional_msg =
301 desc->FindFieldByName("optional_nested_message");
302 EXPECT_TRUE(optional_int32 != NULL);
303 EXPECT_TRUE(optional_msg != NULL);
304
305 EXPECT_EQ(false, refl->HasField(*message, optional_int32));
306 refl->SetInt32(message, optional_int32, 42);
307 EXPECT_EQ(true, refl->HasField(*message, optional_int32));
308 refl->SetInt32(message, optional_int32, 0);
309 EXPECT_EQ(false, refl->HasField(*message, optional_int32));
310
311 EXPECT_EQ(false, refl->HasField(*message, optional_msg));
312 refl->MutableMessage(message, optional_msg);
313 EXPECT_EQ(true, refl->HasField(*message, optional_msg));
314 delete refl->ReleaseMessage(message, optional_msg);
315 EXPECT_EQ(false, refl->HasField(*message, optional_msg));
316
317 // Also ensure that the default instance handles field presence properly.
318 EXPECT_EQ(false, refl->HasField(*proto3_prototype_, optional_msg));
319
320 delete message;
321 }
322
323 INSTANTIATE_TEST_SUITE_P(UseArena, DynamicMessageTest, ::testing::Bool());
324
325 } // namespace protobuf
326 } // namespace google
327