drop_unknown_fields_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include <memory>
  31. #include <google/protobuf/unittest_drop_unknown_fields.pb.h>
  32. #include <google/protobuf/dynamic_message.h>
  33. #include <google/protobuf/message_lite.h>
  34. #include <gtest/gtest.h>
  35. namespace google {
  36. using unittest_drop_unknown_fields::Foo;
  37. using unittest_drop_unknown_fields::FooWithExtraFields;
  38. namespace protobuf {
  39. TEST(DropUnknownFieldsTest, GeneratedMessageDefaultDrop) {
  40. ::google::protobuf::internal::SetProto3PreserveUnknownsDefault(false);
  41. FooWithExtraFields foo_with_extra_fields;
  42. foo_with_extra_fields.set_int32_value(1);
  43. foo_with_extra_fields.set_enum_value(FooWithExtraFields::QUX);
  44. foo_with_extra_fields.set_extra_int32_value(2);
  45. Foo foo;
  46. ASSERT_TRUE(foo.ParseFromString(foo_with_extra_fields.SerializeAsString()));
  47. EXPECT_EQ(1, foo.int32_value());
  48. EXPECT_EQ(static_cast<int>(FooWithExtraFields::QUX),
  49. static_cast<int>(foo.enum_value()));
  50. EXPECT_TRUE(foo.GetReflection()->GetUnknownFields(foo).empty());
  51. ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo.SerializeAsString()));
  52. EXPECT_EQ(1, foo_with_extra_fields.int32_value());
  53. EXPECT_EQ(FooWithExtraFields::QUX, foo_with_extra_fields.enum_value());
  54. // The "extra_int32_value" field should be lost.
  55. EXPECT_EQ(0, foo_with_extra_fields.extra_int32_value());
  56. }
  57. TEST(DropUnknownFieldsTest, GeneratedMessageDefaultPreserve) {
  58. ::google::protobuf::internal::SetProto3PreserveUnknownsDefault(true);
  59. FooWithExtraFields foo_with_extra_fields;
  60. foo_with_extra_fields.set_int32_value(1);
  61. foo_with_extra_fields.set_enum_value(FooWithExtraFields::QUX);
  62. foo_with_extra_fields.set_extra_int32_value(2);
  63. Foo foo;
  64. ASSERT_TRUE(foo.ParseFromString(foo_with_extra_fields.SerializeAsString()));
  65. EXPECT_EQ(1, foo.int32_value());
  66. EXPECT_EQ(static_cast<int>(FooWithExtraFields::QUX),
  67. static_cast<int>(foo.enum_value()));
  68. EXPECT_FALSE(foo.GetReflection()->GetUnknownFields(foo).empty());
  69. ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo.SerializeAsString()));
  70. EXPECT_EQ(1, foo_with_extra_fields.int32_value());
  71. EXPECT_EQ(FooWithExtraFields::QUX, foo_with_extra_fields.enum_value());
  72. // The "extra_int32_value" field should not be lost.
  73. EXPECT_EQ(2, foo_with_extra_fields.extra_int32_value());
  74. }
  75. TEST(DropUnknownFieldsTest, DynamicMessageDefaultDrop) {
  76. internal::SetProto3PreserveUnknownsDefault(false);
  77. FooWithExtraFields foo_with_extra_fields;
  78. foo_with_extra_fields.set_int32_value(1);
  79. foo_with_extra_fields.set_enum_value(FooWithExtraFields::QUX);
  80. foo_with_extra_fields.set_extra_int32_value(2);
  81. google::protobuf::DynamicMessageFactory factory;
  82. std::unique_ptr<google::protobuf::Message> foo(
  83. factory.GetPrototype(Foo::descriptor())->New());
  84. ASSERT_TRUE(foo->ParseFromString(foo_with_extra_fields.SerializeAsString()));
  85. EXPECT_TRUE(foo->GetReflection()->GetUnknownFields(*foo).empty());
  86. ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo->SerializeAsString()));
  87. EXPECT_EQ(1, foo_with_extra_fields.int32_value());
  88. EXPECT_EQ(FooWithExtraFields::QUX, foo_with_extra_fields.enum_value());
  89. // The "extra_int32_value" field should be lost.
  90. EXPECT_EQ(0, foo_with_extra_fields.extra_int32_value());
  91. }
  92. TEST(DropUnknownFieldsTest, DynamicMessageDefaultPreserve) {
  93. internal::SetProto3PreserveUnknownsDefault(true);
  94. FooWithExtraFields foo_with_extra_fields;
  95. foo_with_extra_fields.set_int32_value(1);
  96. foo_with_extra_fields.set_enum_value(FooWithExtraFields::QUX);
  97. foo_with_extra_fields.set_extra_int32_value(2);
  98. google::protobuf::DynamicMessageFactory factory;
  99. std::unique_ptr<google::protobuf::Message> foo(
  100. factory.GetPrototype(Foo::descriptor())->New());
  101. ASSERT_TRUE(foo->ParseFromString(foo_with_extra_fields.SerializeAsString()));
  102. EXPECT_FALSE(foo->GetReflection()->GetUnknownFields(*foo).empty());
  103. ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo->SerializeAsString()));
  104. EXPECT_EQ(1, foo_with_extra_fields.int32_value());
  105. EXPECT_EQ(FooWithExtraFields::QUX, foo_with_extra_fields.enum_value());
  106. // The "extra_int32_value" field should not be lost.
  107. EXPECT_EQ(2, foo_with_extra_fields.extra_int32_value());
  108. }
  109. } // namespace protobuf
  110. } // namespace google