proto3_arena_unittest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <string>
  31. #include <memory>
  32. #include <vector>
  33. #include <google/protobuf/test_util.h>
  34. #include <google/protobuf/unittest.pb.h>
  35. #include <google/protobuf/unittest_proto3_arena.pb.h>
  36. #include <google/protobuf/arena.h>
  37. #include <google/protobuf/testing/googletest.h>
  38. #include <gtest/gtest.h>
  39. namespace google {
  40. using proto3_arena_unittest::TestAllTypes;
  41. namespace protobuf {
  42. namespace {
  43. // We selectively set/check a few representative fields rather than all fields
  44. // as this test is only expected to cover the basics of arena support.
  45. void SetAllFields(TestAllTypes* m) {
  46. m->set_optional_int32(100);
  47. m->set_optional_string("asdf");
  48. m->set_optional_bytes("jkl;");
  49. m->mutable_optional_nested_message()->set_bb(42);
  50. m->mutable_optional_foreign_message()->set_c(43);
  51. m->set_optional_nested_enum(
  52. proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ);
  53. m->set_optional_foreign_enum(
  54. proto3_arena_unittest::FOREIGN_BAZ);
  55. m->mutable_optional_lazy_message()->set_bb(45);
  56. m->add_repeated_int32(100);
  57. m->add_repeated_string("asdf");
  58. m->add_repeated_bytes("jkl;");
  59. m->add_repeated_nested_message()->set_bb(46);
  60. m->add_repeated_foreign_message()->set_c(47);
  61. m->add_repeated_nested_enum(
  62. proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ);
  63. m->add_repeated_foreign_enum(
  64. proto3_arena_unittest::FOREIGN_BAZ);
  65. m->add_repeated_lazy_message()->set_bb(49);
  66. m->set_oneof_uint32(1);
  67. m->mutable_oneof_nested_message()->set_bb(50);
  68. m->set_oneof_string("test"); // only this one remains set
  69. }
  70. void ExpectAllFieldsSet(const TestAllTypes& m) {
  71. EXPECT_EQ(100, m.optional_int32());
  72. EXPECT_EQ("asdf", m.optional_string());
  73. EXPECT_EQ("jkl;", m.optional_bytes());
  74. EXPECT_EQ(true, m.has_optional_nested_message());
  75. EXPECT_EQ(42, m.optional_nested_message().bb());
  76. EXPECT_EQ(true, m.has_optional_foreign_message());
  77. EXPECT_EQ(43, m.optional_foreign_message().c());
  78. EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ,
  79. m.optional_nested_enum());
  80. EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ,
  81. m.optional_foreign_enum());
  82. EXPECT_EQ(true, m.has_optional_lazy_message());
  83. EXPECT_EQ(45, m.optional_lazy_message().bb());
  84. EXPECT_EQ(1, m.repeated_int32_size());
  85. EXPECT_EQ(100, m.repeated_int32(0));
  86. EXPECT_EQ(1, m.repeated_string_size());
  87. EXPECT_EQ("asdf", m.repeated_string(0));
  88. EXPECT_EQ(1, m.repeated_bytes_size());
  89. EXPECT_EQ("jkl;", m.repeated_bytes(0));
  90. EXPECT_EQ(1, m.repeated_nested_message_size());
  91. EXPECT_EQ(46, m.repeated_nested_message(0).bb());
  92. EXPECT_EQ(1, m.repeated_foreign_message_size());
  93. EXPECT_EQ(47, m.repeated_foreign_message(0).c());
  94. EXPECT_EQ(1, m.repeated_nested_enum_size());
  95. EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_BAZ,
  96. m.repeated_nested_enum(0));
  97. EXPECT_EQ(1, m.repeated_foreign_enum_size());
  98. EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ,
  99. m.repeated_foreign_enum(0));
  100. EXPECT_EQ(1, m.repeated_lazy_message_size());
  101. EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
  102. EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
  103. m.oneof_field_case());
  104. EXPECT_EQ("test", m.oneof_string());
  105. }
  106. // In this file we only test some basic functionalities of arena support in
  107. // proto3 and expect the arena support to be fully tested in proto2 unittests
  108. // because proto3 shares most code with proto2.
  109. TEST(Proto3ArenaTest, Parsing) {
  110. TestAllTypes original;
  111. SetAllFields(&original);
  112. Arena arena;
  113. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  114. arena_message->ParseFromString(original.SerializeAsString());
  115. ExpectAllFieldsSet(*arena_message);
  116. }
  117. TEST(Proto3ArenaTest, UnknownFieldsDefaultDrop) {
  118. ::google::protobuf::internal::SetProto3PreserveUnknownsDefault(false);
  119. TestAllTypes original;
  120. SetAllFields(&original);
  121. Arena arena;
  122. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  123. arena_message->ParseFromString(original.SerializeAsString());
  124. ExpectAllFieldsSet(*arena_message);
  125. // In proto3 we can still get a pointer to the UnknownFieldSet through
  126. // reflection API.
  127. UnknownFieldSet* unknown_fields =
  128. arena_message->GetReflection()->MutableUnknownFields(arena_message);
  129. // We can modify this UnknownFieldSet.
  130. unknown_fields->AddVarint(1, 2);
  131. // But the change will never will serialized back.
  132. ASSERT_EQ(original.ByteSize(), arena_message->ByteSize());
  133. ASSERT_TRUE(
  134. arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
  135. }
  136. TEST(Proto3ArenaTest, UnknownFieldsDefaultPreserve) {
  137. ::google::protobuf::internal::SetProto3PreserveUnknownsDefault(true);
  138. TestAllTypes original;
  139. SetAllFields(&original);
  140. Arena arena;
  141. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  142. arena_message->ParseFromString(original.SerializeAsString());
  143. ExpectAllFieldsSet(*arena_message);
  144. // In proto3 we can still get a pointer to the UnknownFieldSet through
  145. // reflection API.
  146. UnknownFieldSet* unknown_fields =
  147. arena_message->GetReflection()->MutableUnknownFields(arena_message);
  148. // We can modify this UnknownFieldSet.
  149. unknown_fields->AddVarint(1, 2);
  150. // And the unknown fields should be changed.
  151. ASSERT_NE(original.ByteSize(), arena_message->ByteSize());
  152. ASSERT_FALSE(
  153. arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
  154. }
  155. TEST(Proto3ArenaTest, Swap) {
  156. Arena arena1;
  157. Arena arena2;
  158. // Test Swap().
  159. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  160. TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  161. arena1_message->Swap(arena2_message);
  162. EXPECT_EQ(&arena1, arena1_message->GetArena());
  163. EXPECT_EQ(&arena2, arena2_message->GetArena());
  164. }
  165. TEST(Proto3ArenaTest, SetAllocatedMessage) {
  166. Arena arena;
  167. TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  168. TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
  169. nested->set_bb(118);
  170. arena_message->set_allocated_optional_nested_message(nested);
  171. EXPECT_EQ(118, arena_message->optional_nested_message().bb());
  172. }
  173. TEST(Proto3ArenaTest, ReleaseMessage) {
  174. Arena arena;
  175. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  176. arena_message->mutable_optional_nested_message()->set_bb(118);
  177. std::unique_ptr<TestAllTypes::NestedMessage> nested(
  178. arena_message->release_optional_nested_message());
  179. EXPECT_EQ(118, nested->bb());
  180. }
  181. TEST(Proto3ArenaTest, MessageFieldClear) {
  182. // GitHub issue #310: https://github.com/google/protobuf/issues/310
  183. Arena arena;
  184. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  185. arena_message->mutable_optional_nested_message()->set_bb(118);
  186. // This should not crash, but prior to the bugfix, it tried to use `operator
  187. // delete` the nested message (which is on the arena):
  188. arena_message->Clear();
  189. }
  190. TEST(Proto3ArenaTest, MessageFieldClearViaReflection) {
  191. Arena arena;
  192. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  193. const Reflection* r = message->GetReflection();
  194. const Descriptor* d = message->GetDescriptor();
  195. const FieldDescriptor* msg_field = d->FindFieldByName(
  196. "optional_nested_message");
  197. message->mutable_optional_nested_message()->set_bb(1);
  198. r->ClearField(message, msg_field);
  199. EXPECT_FALSE(message->has_optional_nested_message());
  200. EXPECT_EQ(0, message->optional_nested_message().bb());
  201. }
  202. } // namespace
  203. } // namespace protobuf
  204. } // namespace google