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 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,TestUnpackWithTypeMismatch)63 TEST(AnyTest, TestUnpackWithTypeMismatch) {
64 protobuf_unittest::TestAny payload;
65 payload.set_int32_value(13);
66 google::protobuf::Any any;
67 any.PackFrom(payload);
68
69 // Attempt to unpack into the wrong type.
70 protobuf_unittest::TestAllTypes dest;
71 EXPECT_FALSE(any.UnpackTo(&dest));
72 }
73
TEST(AnyTest,TestPackAndUnpackAny)74 TEST(AnyTest, TestPackAndUnpackAny) {
75 // We can pack a Any message inside another Any message.
76 protobuf_unittest::TestAny submessage;
77 submessage.set_int32_value(12345);
78 google::protobuf::Any any;
79 any.PackFrom(submessage);
80 protobuf_unittest::TestAny message;
81 message.mutable_any_value()->PackFrom(any);
82
83 std::string data = message.SerializeAsString();
84
85 ASSERT_TRUE(message.ParseFromString(data));
86 EXPECT_TRUE(message.has_any_value());
87 any.Clear();
88 submessage.Clear();
89 ASSERT_TRUE(message.any_value().UnpackTo(&any));
90 ASSERT_TRUE(any.UnpackTo(&submessage));
91 EXPECT_EQ(12345, submessage.int32_value());
92 }
93
TEST(AnyTest,TestPackWithCustomTypeUrl)94 TEST(AnyTest, TestPackWithCustomTypeUrl) {
95 protobuf_unittest::TestAny submessage;
96 submessage.set_int32_value(12345);
97 google::protobuf::Any any;
98 // Pack with a custom type URL prefix.
99 any.PackFrom(submessage, "type.myservice.com");
100 EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
101 // Pack with a custom type URL prefix ending with '/'.
102 any.PackFrom(submessage, "type.myservice.com/");
103 EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
104 // Pack with an empty type URL prefix.
105 any.PackFrom(submessage, "");
106 EXPECT_EQ("/protobuf_unittest.TestAny", any.type_url());
107
108 // Test unpacking the type.
109 submessage.Clear();
110 EXPECT_TRUE(any.UnpackTo(&submessage));
111 EXPECT_EQ(12345, submessage.int32_value());
112 }
113
TEST(AnyTest,TestIs)114 TEST(AnyTest, TestIs) {
115 protobuf_unittest::TestAny submessage;
116 submessage.set_int32_value(12345);
117 google::protobuf::Any any;
118 any.PackFrom(submessage);
119 ASSERT_TRUE(any.ParseFromString(any.SerializeAsString()));
120 EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
121 EXPECT_FALSE(any.Is<google::protobuf::Any>());
122
123 protobuf_unittest::TestAny message;
124 message.mutable_any_value()->PackFrom(any);
125 ASSERT_TRUE(message.ParseFromString(message.SerializeAsString()));
126 EXPECT_FALSE(message.any_value().Is<protobuf_unittest::TestAny>());
127 EXPECT_TRUE(message.any_value().Is<google::protobuf::Any>());
128
129 any.set_type_url("/protobuf_unittest.TestAny");
130 EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
131 // The type URL must contain at least one "/".
132 any.set_type_url("protobuf_unittest.TestAny");
133 EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
134 // The type name after the slash must be fully qualified.
135 any.set_type_url("/TestAny");
136 EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
137 }
138
TEST(AnyTest,MoveConstructor)139 TEST(AnyTest, MoveConstructor) {
140 protobuf_unittest::TestAny payload;
141 payload.set_int32_value(12345);
142
143 google::protobuf::Any src;
144 src.PackFrom(payload);
145
146 const char* type_url = src.type_url().data();
147
148 google::protobuf::Any dst(std::move(src));
149 EXPECT_EQ(type_url, dst.type_url().data());
150 payload.Clear();
151 ASSERT_TRUE(dst.UnpackTo(&payload));
152 EXPECT_EQ(12345, payload.int32_value());
153 }
154
TEST(AnyTest,MoveAssignment)155 TEST(AnyTest, MoveAssignment) {
156 protobuf_unittest::TestAny payload;
157 payload.set_int32_value(12345);
158
159 google::protobuf::Any src;
160 src.PackFrom(payload);
161
162 const char* type_url = src.type_url().data();
163
164 google::protobuf::Any dst;
165 dst = std::move(src);
166 EXPECT_EQ(type_url, dst.type_url().data());
167 payload.Clear();
168 ASSERT_TRUE(dst.UnpackTo(&payload));
169 EXPECT_EQ(12345, payload.int32_value());
170 }
171
172
173 } // namespace
174 } // namespace protobuf
175 } // namespace google
176