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