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