1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
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 <google/protobuf/stubs/common.h>
44#include <google/protobuf/dynamic_message.h>
45#include <google/protobuf/descriptor.h>
46#include <google/protobuf/descriptor.pb.h>
47#include <google/protobuf/test_util.h>
48#include <google/protobuf/unittest.pb.h>
49
50#include <google/protobuf/testing/googletest.h>
51#include <gtest/gtest.h>
52
53namespace google {
54namespace protobuf {
55
56class DynamicMessageTest : public testing::Test {
57 protected:
58  DescriptorPool pool_;
59  DynamicMessageFactory factory_;
60  const Descriptor* descriptor_;
61  const Message* prototype_;
62  const Descriptor* extensions_descriptor_;
63  const Message* extensions_prototype_;
64  const Descriptor* packed_descriptor_;
65  const Message* packed_prototype_;
66
67  DynamicMessageTest(): factory_(&pool_) {}
68
69  virtual void SetUp() {
70    // We want to make sure that DynamicMessage works (particularly with
71    // extensions) even if we use descriptors that are *not* from compiled-in
72    // types, so we make copies of the descriptors for unittest.proto and
73    // unittest_import.proto.
74    FileDescriptorProto unittest_file;
75    FileDescriptorProto unittest_import_file;
76    FileDescriptorProto unittest_import_public_file;
77
78    unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file);
79    unittest_import::ImportMessage::descriptor()->file()->CopyTo(
80      &unittest_import_file);
81    unittest_import::PublicImportMessage::descriptor()->file()->CopyTo(
82      &unittest_import_public_file);
83
84    ASSERT_TRUE(pool_.BuildFile(unittest_import_public_file) != NULL);
85    ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL);
86    ASSERT_TRUE(pool_.BuildFile(unittest_file) != NULL);
87
88    descriptor_ = pool_.FindMessageTypeByName("protobuf_unittest.TestAllTypes");
89    ASSERT_TRUE(descriptor_ != NULL);
90    prototype_ = factory_.GetPrototype(descriptor_);
91
92    extensions_descriptor_ =
93      pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions");
94    ASSERT_TRUE(extensions_descriptor_ != NULL);
95    extensions_prototype_ = factory_.GetPrototype(extensions_descriptor_);
96
97    packed_descriptor_ =
98      pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes");
99    ASSERT_TRUE(packed_descriptor_ != NULL);
100    packed_prototype_ = factory_.GetPrototype(packed_descriptor_);
101  }
102};
103
104TEST_F(DynamicMessageTest, Descriptor) {
105  // Check that the descriptor on the DynamicMessage matches the descriptor
106  // passed to GetPrototype().
107  EXPECT_EQ(prototype_->GetDescriptor(), descriptor_);
108}
109
110TEST_F(DynamicMessageTest, OnePrototype) {
111  // Check that requesting the same prototype twice produces the same object.
112  EXPECT_EQ(prototype_, factory_.GetPrototype(descriptor_));
113}
114
115TEST_F(DynamicMessageTest, Defaults) {
116  // Check that all default values are set correctly in the initial message.
117  TestUtil::ReflectionTester reflection_tester(descriptor_);
118  reflection_tester.ExpectClearViaReflection(*prototype_);
119}
120
121TEST_F(DynamicMessageTest, IndependentOffsets) {
122  // Check that all fields have independent offsets by setting each
123  // one to a unique value then checking that they all still have those
124  // unique values (i.e. they don't stomp each other).
125  scoped_ptr<Message> message(prototype_->New());
126  TestUtil::ReflectionTester reflection_tester(descriptor_);
127
128  reflection_tester.SetAllFieldsViaReflection(message.get());
129  reflection_tester.ExpectAllFieldsSetViaReflection(*message);
130}
131
132TEST_F(DynamicMessageTest, Extensions) {
133  // Check that extensions work.
134  scoped_ptr<Message> message(extensions_prototype_->New());
135  TestUtil::ReflectionTester reflection_tester(extensions_descriptor_);
136
137  reflection_tester.SetAllFieldsViaReflection(message.get());
138  reflection_tester.ExpectAllFieldsSetViaReflection(*message);
139}
140
141TEST_F(DynamicMessageTest, PackedFields) {
142  // Check that packed fields work properly.
143  scoped_ptr<Message> message(packed_prototype_->New());
144  TestUtil::ReflectionTester reflection_tester(packed_descriptor_);
145
146  reflection_tester.SetPackedFieldsViaReflection(message.get());
147  reflection_tester.ExpectPackedFieldsSetViaReflection(*message);
148}
149
150TEST_F(DynamicMessageTest, SpaceUsed) {
151  // Test that SpaceUsed() works properly
152
153  // Since we share the implementation with generated messages, we don't need
154  // to test very much here.  Just make sure it appears to be working.
155
156  scoped_ptr<Message> message(prototype_->New());
157  TestUtil::ReflectionTester reflection_tester(descriptor_);
158
159  int initial_space_used = message->SpaceUsed();
160
161  reflection_tester.SetAllFieldsViaReflection(message.get());
162  EXPECT_LT(initial_space_used, message->SpaceUsed());
163}
164
165}  // namespace protobuf
166}  // namespace google
167