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#ifndef GOOGLE_PROTOBUF_TEST_UTIL_H__
36#define GOOGLE_PROTOBUF_TEST_UTIL_H__
37
38#include <stack>
39#include <string>
40#include <google/protobuf/message.h>
41#include <google/protobuf/unittest.pb.h>
42
43namespace google {
44namespace protobuf {
45
46namespace unittest = ::protobuf_unittest;
47namespace unittest_import = protobuf_unittest_import;
48
49class TestUtil {
50 public:
51  // Set every field in the message to a unique value.
52  static void SetAllFields(unittest::TestAllTypes* message);
53  static void SetOptionalFields(unittest::TestAllTypes* message);
54  static void AddRepeatedFields1(unittest::TestAllTypes* message);
55  static void AddRepeatedFields2(unittest::TestAllTypes* message);
56  static void SetDefaultFields(unittest::TestAllTypes* message);
57  static void SetAllExtensions(unittest::TestAllExtensions* message);
58  static void SetAllFieldsAndExtensions(unittest::TestFieldOrderings* message);
59  static void SetPackedFields(unittest::TestPackedTypes* message);
60  static void SetPackedExtensions(unittest::TestPackedExtensions* message);
61  static void SetUnpackedFields(unittest::TestUnpackedTypes* message);
62
63  // Use the repeated versions of the set_*() accessors to modify all the
64  // repeated fields of the messsage (which should already have been
65  // initialized with Set*Fields()).  Set*Fields() itself only tests
66  // the add_*() accessors.
67  static void ModifyRepeatedFields(unittest::TestAllTypes* message);
68  static void ModifyRepeatedExtensions(unittest::TestAllExtensions* message);
69  static void ModifyPackedFields(unittest::TestPackedTypes* message);
70  static void ModifyPackedExtensions(unittest::TestPackedExtensions* message);
71
72  // Check that all fields have the values that they should have after
73  // Set*Fields() is called.
74  static void ExpectAllFieldsSet(const unittest::TestAllTypes& message);
75  static void ExpectAllExtensionsSet(
76      const unittest::TestAllExtensions& message);
77  static void ExpectPackedFieldsSet(const unittest::TestPackedTypes& message);
78  static void ExpectPackedExtensionsSet(
79      const unittest::TestPackedExtensions& message);
80  static void ExpectUnpackedFieldsSet(
81      const unittest::TestUnpackedTypes& message);
82
83  // Expect that the message is modified as would be expected from
84  // Modify*Fields().
85  static void ExpectRepeatedFieldsModified(
86      const unittest::TestAllTypes& message);
87  static void ExpectRepeatedExtensionsModified(
88      const unittest::TestAllExtensions& message);
89  static void ExpectPackedFieldsModified(
90      const unittest::TestPackedTypes& message);
91  static void ExpectPackedExtensionsModified(
92      const unittest::TestPackedExtensions& message);
93
94  // Check that all fields have their default values.
95  static void ExpectClear(const unittest::TestAllTypes& message);
96  static void ExpectExtensionsClear(const unittest::TestAllExtensions& message);
97  static void ExpectPackedClear(const unittest::TestPackedTypes& message);
98  static void ExpectPackedExtensionsClear(
99      const unittest::TestPackedExtensions& message);
100
101  // Check that the passed-in serialization is the canonical serialization we
102  // expect for a TestFieldOrderings message filled in by
103  // SetAllFieldsAndExtensions().
104  static void ExpectAllFieldsAndExtensionsInOrder(const string& serialized);
105
106  // Check that all repeated fields have had their last elements removed.
107  static void ExpectLastRepeatedsRemoved(
108      const unittest::TestAllTypes& message);
109  static void ExpectLastRepeatedExtensionsRemoved(
110      const unittest::TestAllExtensions& message);
111  static void ExpectLastRepeatedsReleased(
112      const unittest::TestAllTypes& message);
113  static void ExpectLastRepeatedExtensionsReleased(
114      const unittest::TestAllExtensions& message);
115
116  // Check that all repeated fields have had their first and last elements
117  // swapped.
118  static void ExpectRepeatedsSwapped(const unittest::TestAllTypes& message);
119  static void ExpectRepeatedExtensionsSwapped(
120      const unittest::TestAllExtensions& message);
121
122  // Like above, but use the reflection interface.
123  class ReflectionTester {
124   public:
125    // base_descriptor must be a descriptor for TestAllTypes or
126    // TestAllExtensions.  In the former case, ReflectionTester fetches from
127    // it the FieldDescriptors needed to use the reflection interface.  In
128    // the latter case, ReflectionTester searches for extension fields in
129    // its file.
130    explicit ReflectionTester(const Descriptor* base_descriptor);
131
132    void SetAllFieldsViaReflection(Message* message);
133    void ModifyRepeatedFieldsViaReflection(Message* message);
134    void ExpectAllFieldsSetViaReflection(const Message& message);
135    void ExpectClearViaReflection(const Message& message);
136
137    void SetPackedFieldsViaReflection(Message* message);
138    void ModifyPackedFieldsViaReflection(Message* message);
139    void ExpectPackedFieldsSetViaReflection(const Message& message);
140    void ExpectPackedClearViaReflection(const Message& message);
141
142    void RemoveLastRepeatedsViaReflection(Message* message);
143    void ReleaseLastRepeatedsViaReflection(
144        Message* message, bool expect_extensions_notnull);
145    void SwapRepeatedsViaReflection(Message* message);
146
147    enum MessageReleaseState {
148      IS_NULL,
149      CAN_BE_NULL,
150      NOT_NULL,
151    };
152    void ExpectMessagesReleasedViaReflection(
153        Message* message, MessageReleaseState expected_release_state);
154
155   private:
156    const FieldDescriptor* F(const string& name);
157
158    const Descriptor* base_descriptor_;
159
160    const FieldDescriptor* group_a_;
161    const FieldDescriptor* repeated_group_a_;
162    const FieldDescriptor* nested_b_;
163    const FieldDescriptor* foreign_c_;
164    const FieldDescriptor* import_d_;
165    const FieldDescriptor* import_e_;
166
167    const EnumValueDescriptor* nested_foo_;
168    const EnumValueDescriptor* nested_bar_;
169    const EnumValueDescriptor* nested_baz_;
170    const EnumValueDescriptor* foreign_foo_;
171    const EnumValueDescriptor* foreign_bar_;
172    const EnumValueDescriptor* foreign_baz_;
173    const EnumValueDescriptor* import_foo_;
174    const EnumValueDescriptor* import_bar_;
175    const EnumValueDescriptor* import_baz_;
176
177    // We have to split this into three function otherwise it creates a stack
178    // frame so large that it triggers a warning.
179    void ExpectAllFieldsSetViaReflection1(const Message& message);
180    void ExpectAllFieldsSetViaReflection2(const Message& message);
181    void ExpectAllFieldsSetViaReflection3(const Message& message);
182
183    GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
184  };
185
186 private:
187  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TestUtil);
188};
189
190}  // namespace protobuf
191
192}  // namespace google
193#endif  // GOOGLE_PROTOBUF_TEST_UTIL_H__
194