1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/stubs/common.h"
9 #include <gtest/gtest.h>
10 #include "google/protobuf/arena.h"
11 #include "google/protobuf/test_util.h"
12 #include "google/protobuf/unittest.pb.h"
13
14 namespace google {
15 namespace protobuf {
16 namespace compiler {
17 namespace cpp {
18 namespace {
19
TEST(CopyMessageTest,CopyConstructor)20 TEST(CopyMessageTest, CopyConstructor) {
21 protobuf_unittest::TestAllTypes message1;
22 TestUtil::SetAllFields(&message1);
23 protobuf_unittest::TestAllTypes message2(message1);
24 TestUtil::ExpectAllFieldsSet(message2);
25 }
26
TEST(CopyMessageTest,ArenaEnabledCopyConstructorNull)27 TEST(CopyMessageTest, ArenaEnabledCopyConstructorNull) {
28 protobuf_unittest::TestAllTypes message1;
29 TestUtil::SetAllFields(&message1);
30 protobuf_unittest::TestAllTypes* message2 =
31 Arena::Create<protobuf_unittest::TestAllTypes>(nullptr, message1);
32 TestUtil::ExpectAllFieldsSet(*message2);
33 delete message2;
34 }
35
TEST(CopyMessageTest,ArenaEnabledCopyConstructor)36 TEST(CopyMessageTest, ArenaEnabledCopyConstructor) {
37 protobuf_unittest::TestAllTypes message1;
38 TestUtil::SetAllFields(&message1);
39 Arena arena;
40 protobuf_unittest::TestAllTypes* message2 =
41 Arena::Create<protobuf_unittest::TestAllTypes>(&arena, message1);
42 TestUtil::ExpectAllFieldsSet(*message2);
43 }
44
TEST(CopyMessageTest,ArenaEnabledCopyConstructorArenaLeakTest)45 TEST(CopyMessageTest, ArenaEnabledCopyConstructorArenaLeakTest) {
46 // Set possible leaking field types for TestAllTypes with values
47 // guaranteed to not be inlined string or Cord values.
48 // TestAllTypes has unconditional ArenaDtor registration.
49 protobuf_unittest::TestAllTypes message1;
50
51 message1.set_optional_string(std::string(1000, 'a'));
52 message1.add_repeated_string(std::string(1000, 'd'));
53
54 Arena arena;
55 protobuf_unittest::TestAllTypes* message2 =
56 Arena::Create<protobuf_unittest::TestAllTypes>(&arena, message1);
57
58 EXPECT_EQ(message2->optional_string(), message1.optional_string());
59 EXPECT_EQ(message2->repeated_string(0), message1.repeated_string(0));
60 }
61
62 } // namespace
63 } // namespace cpp
64 } // namespace compiler
65 } // namespace protobuf
66 } // namespace google
67