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 #include <google/protobuf/any_test.pb.h>
32 #include <google/protobuf/unittest.pb.h>
33 #include <gtest/gtest.h>
34
35
36 // Must be included last.
37 #include <google/protobuf/port_def.inc>
38
39 namespace google {
40 namespace protobuf {
41 namespace {
42
TEST(AnyMetadataTest,ConstInit)43 TEST(AnyMetadataTest, ConstInit) {
44 PROTOBUF_CONSTINIT static internal::AnyMetadata metadata(nullptr, nullptr);
45 (void)metadata;
46 }
47
TEST(AnyTest,TestPackAndUnpack)48 TEST(AnyTest, TestPackAndUnpack) {
49 protobuf_unittest::TestAny submessage;
50 submessage.set_int32_value(12345);
51 protobuf_unittest::TestAny message;
52 ASSERT_TRUE(message.mutable_any_value()->PackFrom(submessage));
53
54 std::string data = message.SerializeAsString();
55
56 ASSERT_TRUE(message.ParseFromString(data));
57 EXPECT_TRUE(message.has_any_value());
58 submessage.Clear();
59 ASSERT_TRUE(message.any_value().UnpackTo(&submessage));
60 EXPECT_EQ(12345, submessage.int32_value());
61 }
62
TEST(AnyTest,TestPackFromSerializationExceedsSizeLimit)63 TEST(AnyTest, TestPackFromSerializationExceedsSizeLimit) {
64 protobuf_unittest::TestAny submessage;
65 submessage.mutable_text()->resize(INT_MAX, 'a');
66 protobuf_unittest::TestAny message;
67 EXPECT_FALSE(message.mutable_any_value()->PackFrom(submessage));
68 }
69
TEST(AnyTest,TestUnpackWithTypeMismatch)70 TEST(AnyTest, TestUnpackWithTypeMismatch) {
71 protobuf_unittest::TestAny payload;
72 payload.set_int32_value(13);
73 google::protobuf::Any any;
74 any.PackFrom(payload);
75
76 // Attempt to unpack into the wrong type.
77 protobuf_unittest::TestAllTypes dest;
78 EXPECT_FALSE(any.UnpackTo(&dest));
79 }
80
TEST(AnyTest,TestPackAndUnpackAny)81 TEST(AnyTest, TestPackAndUnpackAny) {
82 // We can pack a Any message inside another Any message.
83 protobuf_unittest::TestAny submessage;
84 submessage.set_int32_value(12345);
85 google::protobuf::Any any;
86 any.PackFrom(submessage);
87 protobuf_unittest::TestAny message;
88 message.mutable_any_value()->PackFrom(any);
89
90 std::string data = message.SerializeAsString();
91
92 ASSERT_TRUE(message.ParseFromString(data));
93 EXPECT_TRUE(message.has_any_value());
94 any.Clear();
95 submessage.Clear();
96 ASSERT_TRUE(message.any_value().UnpackTo(&any));
97 ASSERT_TRUE(any.UnpackTo(&submessage));
98 EXPECT_EQ(12345, submessage.int32_value());
99 }
100
TEST(AnyTest,TestPackWithCustomTypeUrl)101 TEST(AnyTest, TestPackWithCustomTypeUrl) {
102 protobuf_unittest::TestAny submessage;
103 submessage.set_int32_value(12345);
104 google::protobuf::Any any;
105 // Pack with a custom type URL prefix.
106 any.PackFrom(submessage, "type.myservice.com");
107 EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
108 // Pack with a custom type URL prefix ending with '/'.
109 any.PackFrom(submessage, "type.myservice.com/");
110 EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
111 // Pack with an empty type URL prefix.
112 any.PackFrom(submessage, "");
113 EXPECT_EQ("/protobuf_unittest.TestAny", any.type_url());
114
115 // Test unpacking the type.
116 submessage.Clear();
117 EXPECT_TRUE(any.UnpackTo(&submessage));
118 EXPECT_EQ(12345, submessage.int32_value());
119 }
120
TEST(AnyTest,TestIs)121 TEST(AnyTest, TestIs) {
122 protobuf_unittest::TestAny submessage;
123 submessage.set_int32_value(12345);
124 google::protobuf::Any any;
125 any.PackFrom(submessage);
126 ASSERT_TRUE(any.ParseFromString(any.SerializeAsString()));
127 EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
128 EXPECT_FALSE(any.Is<google::protobuf::Any>());
129
130 protobuf_unittest::TestAny message;
131 message.mutable_any_value()->PackFrom(any);
132 ASSERT_TRUE(message.ParseFromString(message.SerializeAsString()));
133 EXPECT_FALSE(message.any_value().Is<protobuf_unittest::TestAny>());
134 EXPECT_TRUE(message.any_value().Is<google::protobuf::Any>());
135
136 any.set_type_url("/protobuf_unittest.TestAny");
137 EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
138 // The type URL must contain at least one "/".
139 any.set_type_url("protobuf_unittest.TestAny");
140 EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
141 // The type name after the slash must be fully qualified.
142 any.set_type_url("/TestAny");
143 EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
144 }
145
TEST(AnyTest,MoveConstructor)146 TEST(AnyTest, MoveConstructor) {
147 protobuf_unittest::TestAny payload;
148 payload.set_int32_value(12345);
149
150 google::protobuf::Any src;
151 src.PackFrom(payload);
152
153 const char* type_url = src.type_url().data();
154
155 google::protobuf::Any dst(std::move(src));
156 EXPECT_EQ(type_url, dst.type_url().data());
157 payload.Clear();
158 ASSERT_TRUE(dst.UnpackTo(&payload));
159 EXPECT_EQ(12345, payload.int32_value());
160 }
161
TEST(AnyTest,MoveAssignment)162 TEST(AnyTest, MoveAssignment) {
163 protobuf_unittest::TestAny payload;
164 payload.set_int32_value(12345);
165
166 google::protobuf::Any src;
167 src.PackFrom(payload);
168
169 const char* type_url = src.type_url().data();
170
171 google::protobuf::Any dst;
172 dst = std::move(src);
173 EXPECT_EQ(type_url, dst.type_url().data());
174 payload.Clear();
175 ASSERT_TRUE(dst.UnpackTo(&payload));
176 EXPECT_EQ(12345, payload.int32_value());
177 }
178
179
180 } // namespace
181 } // namespace protobuf
182 } // namespace google
183
184 #include <google/protobuf/port_undef.inc>
185