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