protobuf/src/google/protobuf/any_test.cc
Go to the documentation of this file.
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 
43 TEST(AnyMetadataTest, ConstInit) {
44  PROTOBUF_CONSTINIT static internal::AnyMetadata metadata(nullptr, nullptr);
45  (void)metadata;
46 }
47 
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 
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 
70 TEST(AnyTest, TestUnpackWithTypeMismatch) {
71  protobuf_unittest::TestAny payload;
72  payload.set_int32_value(13);
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 
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);
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 
101 TEST(AnyTest, TestPackWithCustomTypeUrl) {
102  protobuf_unittest::TestAny submessage;
103  submessage.set_int32_value(12345);
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 
121 TEST(AnyTest, TestIs) {
122  protobuf_unittest::TestAny submessage;
123  submessage.set_int32_value(12345);
125  any.PackFrom(submessage);
126  ASSERT_TRUE(any.ParseFromString(any.SerializeAsString()));
127  EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
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 
146 TEST(AnyTest, MoveConstructor) {
147  protobuf_unittest::TestAny payload;
148  payload.set_int32_value(12345);
149 
151  src.PackFrom(payload);
152 
153  const char* type_url = src.type_url().data();
154 
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 
162 TEST(AnyTest, MoveAssignment) {
163  protobuf_unittest::TestAny payload;
164  payload.set_int32_value(12345);
165 
167  src.PackFrom(payload);
168 
169  const char* type_url = src.type_url().data();
170 
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>
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
Any
struct Any Any
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:633
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
metadata
Definition: cq_verifier.cc:48
metadata
struct metadata metadata
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_unittest.cc:156
tests.qps.qps_worker.dest
dest
Definition: qps_worker.py:45
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
type_url
string * type_url
Definition: bloaty/third_party/protobuf/conformance/conformance_cpp.cc:72
Any::PackFrom
void PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message &message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/any.pb.cc:88
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
gen_server_registered_method_bad_client_test_body.payload
list payload
Definition: gen_server_registered_method_bad_client_test_body.py:40
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:41