• 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 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/reflection_ops.h>
36 #include <google/protobuf/test_util.h>
37 #include <google/protobuf/unittest.pb.h>
38 #include <google/protobuf/descriptor.h>
39 
40 #include <google/protobuf/stubs/logging.h>
41 #include <google/protobuf/stubs/common.h>
42 #include <google/protobuf/testing/googletest.h>
43 #include <gtest/gtest.h>
44 
45 #include <google/protobuf/stubs/strutil.h>
46 
47 namespace google {
48 namespace protobuf {
49 namespace internal {
50 namespace {
51 
TEST(ReflectionOpsTest,SanityCheck)52 TEST(ReflectionOpsTest, SanityCheck) {
53   unittest::TestAllTypes message;
54 
55   TestUtil::SetAllFields(&message);
56   TestUtil::ExpectAllFieldsSet(message);
57 }
58 
TEST(ReflectionOpsTest,Copy)59 TEST(ReflectionOpsTest, Copy) {
60   unittest::TestAllTypes message, message2;
61 
62   TestUtil::SetAllFields(&message);
63 
64   ReflectionOps::Copy(message, &message2);
65 
66   TestUtil::ExpectAllFieldsSet(message2);
67 
68   // Copying from self should be a no-op.
69   ReflectionOps::Copy(message2, &message2);
70   TestUtil::ExpectAllFieldsSet(message2);
71 }
72 
TEST(ReflectionOpsTest,CopyExtensions)73 TEST(ReflectionOpsTest, CopyExtensions) {
74   unittest::TestAllExtensions message, message2;
75 
76   TestUtil::SetAllExtensions(&message);
77 
78   ReflectionOps::Copy(message, &message2);
79 
80   TestUtil::ExpectAllExtensionsSet(message2);
81 }
82 
TEST(ReflectionOpsTest,CopyOneof)83 TEST(ReflectionOpsTest, CopyOneof) {
84   unittest::TestOneof2 message, message2;
85   TestUtil::SetOneof1(&message);
86   ReflectionOps::Copy(message, &message2);
87   TestUtil::ExpectOneofSet1(message2);
88 
89   TestUtil::SetOneof2(&message);
90   TestUtil::ExpectOneofSet2(message);
91   ReflectionOps::Copy(message, &message2);
92   TestUtil::ExpectOneofSet2(message2);
93 }
94 
TEST(ReflectionOpsTest,Merge)95 TEST(ReflectionOpsTest, Merge) {
96   // Note:  Copy is implemented in terms of Merge() so technically the Copy
97   //   test already tested most of this.
98 
99   unittest::TestAllTypes message, message2;
100 
101   TestUtil::SetAllFields(&message);
102 
103   // This field will test merging into an empty spot.
104   message2.set_optional_int32(message.optional_int32());
105   message.clear_optional_int32();
106 
107   // This tests overwriting.
108   message2.set_optional_string(message.optional_string());
109   message.set_optional_string("something else");
110 
111   // This tests concatenating.
112   message2.add_repeated_int32(message.repeated_int32(1));
113   int32 i = message.repeated_int32(0);
114   message.clear_repeated_int32();
115   message.add_repeated_int32(i);
116 
117   ReflectionOps::Merge(message2, &message);
118 
119   TestUtil::ExpectAllFieldsSet(message);
120 }
121 
TEST(ReflectionOpsTest,MergeExtensions)122 TEST(ReflectionOpsTest, MergeExtensions) {
123   // Note:  Copy is implemented in terms of Merge() so technically the Copy
124   //   test already tested most of this.
125 
126   unittest::TestAllExtensions message, message2;
127 
128   TestUtil::SetAllExtensions(&message);
129 
130   // This field will test merging into an empty spot.
131   message2.SetExtension(
132       unittest::optional_int32_extension,
133       message.GetExtension(unittest::optional_int32_extension));
134   message.ClearExtension(unittest::optional_int32_extension);
135 
136   // This tests overwriting.
137   message2.SetExtension(
138       unittest::optional_string_extension,
139       message.GetExtension(unittest::optional_string_extension));
140   message.SetExtension(unittest::optional_string_extension, "something else");
141 
142   // This tests concatenating.
143   message2.AddExtension(
144       unittest::repeated_int32_extension,
145       message.GetExtension(unittest::repeated_int32_extension, 1));
146   int32 i = message.GetExtension(unittest::repeated_int32_extension, 0);
147   message.ClearExtension(unittest::repeated_int32_extension);
148   message.AddExtension(unittest::repeated_int32_extension, i);
149 
150   ReflectionOps::Merge(message2, &message);
151 
152   TestUtil::ExpectAllExtensionsSet(message);
153 }
154 
TEST(ReflectionOpsTest,MergeUnknown)155 TEST(ReflectionOpsTest, MergeUnknown) {
156   // Test that the messages' UnknownFieldSets are correctly merged.
157   unittest::TestEmptyMessage message1, message2;
158   message1.mutable_unknown_fields()->AddVarint(1234, 1);
159   message2.mutable_unknown_fields()->AddVarint(1234, 2);
160 
161   ReflectionOps::Merge(message2, &message1);
162 
163   ASSERT_EQ(2, message1.unknown_fields().field_count());
164   ASSERT_EQ(UnknownField::TYPE_VARINT,
165             message1.unknown_fields().field(0).type());
166   EXPECT_EQ(1, message1.unknown_fields().field(0).varint());
167   ASSERT_EQ(UnknownField::TYPE_VARINT,
168             message1.unknown_fields().field(1).type());
169   EXPECT_EQ(2, message1.unknown_fields().field(1).varint());
170 }
171 
TEST(ReflectionOpsTest,MergeOneof)172 TEST(ReflectionOpsTest, MergeOneof) {
173   unittest::TestOneof2 message1, message2;
174   TestUtil::SetOneof1(&message1);
175 
176   // Merge to empty message
177   ReflectionOps::Merge(message1, &message2);
178   TestUtil::ExpectOneofSet1(message2);
179 
180   // Merge with the same oneof fields
181   ReflectionOps::Merge(message1, &message2);
182   TestUtil::ExpectOneofSet1(message2);
183 
184   // Merge with different oneof fields
185   TestUtil::SetOneof2(&message1);
186   ReflectionOps::Merge(message1, &message2);
187   TestUtil::ExpectOneofSet2(message2);
188 }
189 
190 #ifdef PROTOBUF_HAS_DEATH_TEST
191 
TEST(ReflectionOpsTest,MergeFromSelf)192 TEST(ReflectionOpsTest, MergeFromSelf) {
193   // Note:  Copy is implemented in terms of Merge() so technically the Copy
194   //   test already tested most of this.
195 
196   unittest::TestAllTypes message;
197 
198   EXPECT_DEATH(ReflectionOps::Merge(message, &message), "&from");
199 }
200 
201 #endif  // PROTOBUF_HAS_DEATH_TEST
202 
TEST(ReflectionOpsTest,Clear)203 TEST(ReflectionOpsTest, Clear) {
204   unittest::TestAllTypes message;
205 
206   TestUtil::SetAllFields(&message);
207 
208   ReflectionOps::Clear(&message);
209 
210   TestUtil::ExpectClear(message);
211 
212   // Check that getting embedded messages returns the objects created during
213   // SetAllFields() rather than default instances.
214   EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
215             &message.optionalgroup());
216   EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
217             &message.optional_nested_message());
218   EXPECT_NE(&unittest::ForeignMessage::default_instance(),
219             &message.optional_foreign_message());
220   EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
221             &message.optional_import_message());
222 }
223 
TEST(ReflectionOpsTest,ClearExtensions)224 TEST(ReflectionOpsTest, ClearExtensions) {
225   unittest::TestAllExtensions message;
226 
227   TestUtil::SetAllExtensions(&message);
228 
229   ReflectionOps::Clear(&message);
230 
231   TestUtil::ExpectExtensionsClear(message);
232 
233   // Check that getting embedded messages returns the objects created during
234   // SetAllExtensions() rather than default instances.
235   EXPECT_NE(&unittest::OptionalGroup_extension::default_instance(),
236             &message.GetExtension(unittest::optionalgroup_extension));
237   EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
238             &message.GetExtension(unittest::optional_nested_message_extension));
239   EXPECT_NE(
240       &unittest::ForeignMessage::default_instance(),
241       &message.GetExtension(unittest::optional_foreign_message_extension));
242   EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
243             &message.GetExtension(unittest::optional_import_message_extension));
244 }
245 
TEST(ReflectionOpsTest,ClearUnknown)246 TEST(ReflectionOpsTest, ClearUnknown) {
247   // Test that the message's UnknownFieldSet is correctly cleared.
248   unittest::TestEmptyMessage message;
249   message.mutable_unknown_fields()->AddVarint(1234, 1);
250 
251   ReflectionOps::Clear(&message);
252 
253   EXPECT_EQ(0, message.unknown_fields().field_count());
254 }
255 
TEST(ReflectionOpsTest,ClearOneof)256 TEST(ReflectionOpsTest, ClearOneof) {
257   unittest::TestOneof2 message;
258 
259   TestUtil::ExpectOneofClear(message);
260   TestUtil::SetOneof1(&message);
261   TestUtil::ExpectOneofSet1(message);
262   ReflectionOps::Clear(&message);
263   TestUtil::ExpectOneofClear(message);
264 
265   TestUtil::SetOneof1(&message);
266   TestUtil::ExpectOneofSet1(message);
267   TestUtil::SetOneof2(&message);
268   TestUtil::ExpectOneofSet2(message);
269   ReflectionOps::Clear(&message);
270   TestUtil::ExpectOneofClear(message);
271 }
272 
TEST(ReflectionOpsTest,DiscardUnknownFields)273 TEST(ReflectionOpsTest, DiscardUnknownFields) {
274   unittest::TestAllTypes message;
275   TestUtil::SetAllFields(&message);
276 
277   // Set some unknown fields in message.
278   message.mutable_unknown_fields()->AddVarint(123456, 654321);
279   message.mutable_optional_nested_message()
280       ->mutable_unknown_fields()
281       ->AddVarint(123456, 654321);
282   message.mutable_repeated_nested_message(0)
283       ->mutable_unknown_fields()
284       ->AddVarint(123456, 654321);
285 
286   EXPECT_EQ(1, message.unknown_fields().field_count());
287   EXPECT_EQ(1,
288             message.optional_nested_message().unknown_fields().field_count());
289   EXPECT_EQ(1,
290             message.repeated_nested_message(0).unknown_fields().field_count());
291 
292   // Discard them.
293   ReflectionOps::DiscardUnknownFields(&message);
294   TestUtil::ExpectAllFieldsSet(message);
295 
296   EXPECT_EQ(0, message.unknown_fields().field_count());
297   EXPECT_EQ(0,
298             message.optional_nested_message().unknown_fields().field_count());
299   EXPECT_EQ(0,
300             message.repeated_nested_message(0).unknown_fields().field_count());
301 }
302 
TEST(ReflectionOpsTest,DiscardUnknownExtensions)303 TEST(ReflectionOpsTest, DiscardUnknownExtensions) {
304   unittest::TestAllExtensions message;
305   TestUtil::SetAllExtensions(&message);
306 
307   // Set some unknown fields.
308   message.mutable_unknown_fields()->AddVarint(123456, 654321);
309   message.MutableExtension(unittest::optional_nested_message_extension)
310       ->mutable_unknown_fields()
311       ->AddVarint(123456, 654321);
312   message.MutableExtension(unittest::repeated_nested_message_extension, 0)
313       ->mutable_unknown_fields()
314       ->AddVarint(123456, 654321);
315 
316   EXPECT_EQ(1, message.unknown_fields().field_count());
317   EXPECT_EQ(1, message.GetExtension(unittest::optional_nested_message_extension)
318                    .unknown_fields()
319                    .field_count());
320   EXPECT_EQ(1,
321             message.GetExtension(unittest::repeated_nested_message_extension, 0)
322                 .unknown_fields()
323                 .field_count());
324 
325   // Discard them.
326   ReflectionOps::DiscardUnknownFields(&message);
327   TestUtil::ExpectAllExtensionsSet(message);
328 
329   EXPECT_EQ(0, message.unknown_fields().field_count());
330   EXPECT_EQ(0, message.GetExtension(unittest::optional_nested_message_extension)
331                    .unknown_fields()
332                    .field_count());
333   EXPECT_EQ(0,
334             message.GetExtension(unittest::repeated_nested_message_extension, 0)
335                 .unknown_fields()
336                 .field_count());
337 }
338 
TEST(ReflectionOpsTest,IsInitialized)339 TEST(ReflectionOpsTest, IsInitialized) {
340   unittest::TestRequired message;
341 
342   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
343   message.set_a(1);
344   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
345   message.set_b(2);
346   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
347   message.set_c(3);
348   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
349 }
350 
TEST(ReflectionOpsTest,ForeignIsInitialized)351 TEST(ReflectionOpsTest, ForeignIsInitialized) {
352   unittest::TestRequiredForeign message;
353 
354   // Starts out initialized because the foreign message is itself an optional
355   // field.
356   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
357 
358   // Once we create that field, the message is no longer initialized.
359   message.mutable_optional_message();
360   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
361 
362   // Initialize it.  Now we're initialized.
363   message.mutable_optional_message()->set_a(1);
364   message.mutable_optional_message()->set_b(2);
365   message.mutable_optional_message()->set_c(3);
366   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
367 
368   // Add a repeated version of the message.  No longer initialized.
369   unittest::TestRequired* sub_message = message.add_repeated_message();
370   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
371 
372   // Initialize that repeated version.
373   sub_message->set_a(1);
374   sub_message->set_b(2);
375   sub_message->set_c(3);
376   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
377 }
378 
TEST(ReflectionOpsTest,ExtensionIsInitialized)379 TEST(ReflectionOpsTest, ExtensionIsInitialized) {
380   unittest::TestAllExtensions message;
381 
382   // Starts out initialized because the foreign message is itself an optional
383   // field.
384   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
385 
386   // Once we create that field, the message is no longer initialized.
387   message.MutableExtension(unittest::TestRequired::single);
388   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
389 
390   // Initialize it.  Now we're initialized.
391   message.MutableExtension(unittest::TestRequired::single)->set_a(1);
392   message.MutableExtension(unittest::TestRequired::single)->set_b(2);
393   message.MutableExtension(unittest::TestRequired::single)->set_c(3);
394   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
395 
396   // Add a repeated version of the message.  No longer initialized.
397   message.AddExtension(unittest::TestRequired::multi);
398   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
399 
400   // Initialize that repeated version.
401   message.MutableExtension(unittest::TestRequired::multi, 0)->set_a(1);
402   message.MutableExtension(unittest::TestRequired::multi, 0)->set_b(2);
403   message.MutableExtension(unittest::TestRequired::multi, 0)->set_c(3);
404   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
405 }
406 
TEST(ReflectionOpsTest,OneofIsInitialized)407 TEST(ReflectionOpsTest, OneofIsInitialized) {
408   unittest::TestRequiredOneof message;
409   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
410 
411   message.mutable_foo_message();
412   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
413 
414   message.set_foo_int(1);
415   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
416 
417   message.mutable_foo_message();
418   EXPECT_FALSE(ReflectionOps::IsInitialized(message));
419   message.mutable_foo_message()->set_required_double(0.1);
420   EXPECT_TRUE(ReflectionOps::IsInitialized(message));
421 }
422 
FindInitializationErrors(const Message & message)423 static std::string FindInitializationErrors(const Message& message) {
424   std::vector<std::string> errors;
425   ReflectionOps::FindInitializationErrors(message, "", &errors);
426   return Join(errors, ",");
427 }
428 
TEST(ReflectionOpsTest,FindInitializationErrors)429 TEST(ReflectionOpsTest, FindInitializationErrors) {
430   unittest::TestRequired message;
431   EXPECT_EQ("a,b,c", FindInitializationErrors(message));
432 }
433 
TEST(ReflectionOpsTest,FindForeignInitializationErrors)434 TEST(ReflectionOpsTest, FindForeignInitializationErrors) {
435   unittest::TestRequiredForeign message;
436   message.mutable_optional_message();
437   message.add_repeated_message();
438   message.add_repeated_message();
439   EXPECT_EQ(
440       "optional_message.a,"
441       "optional_message.b,"
442       "optional_message.c,"
443       "repeated_message[0].a,"
444       "repeated_message[0].b,"
445       "repeated_message[0].c,"
446       "repeated_message[1].a,"
447       "repeated_message[1].b,"
448       "repeated_message[1].c",
449       FindInitializationErrors(message));
450 }
451 
TEST(ReflectionOpsTest,FindExtensionInitializationErrors)452 TEST(ReflectionOpsTest, FindExtensionInitializationErrors) {
453   unittest::TestAllExtensions message;
454   message.MutableExtension(unittest::TestRequired::single);
455   message.AddExtension(unittest::TestRequired::multi);
456   message.AddExtension(unittest::TestRequired::multi);
457   EXPECT_EQ(
458       "(protobuf_unittest.TestRequired.single).a,"
459       "(protobuf_unittest.TestRequired.single).b,"
460       "(protobuf_unittest.TestRequired.single).c,"
461       "(protobuf_unittest.TestRequired.multi)[0].a,"
462       "(protobuf_unittest.TestRequired.multi)[0].b,"
463       "(protobuf_unittest.TestRequired.multi)[0].c,"
464       "(protobuf_unittest.TestRequired.multi)[1].a,"
465       "(protobuf_unittest.TestRequired.multi)[1].b,"
466       "(protobuf_unittest.TestRequired.multi)[1].c",
467       FindInitializationErrors(message));
468 }
469 
TEST(ReflectionOpsTest,FindOneofInitializationErrors)470 TEST(ReflectionOpsTest, FindOneofInitializationErrors) {
471   unittest::TestRequiredOneof message;
472   message.mutable_foo_message();
473   EXPECT_EQ("foo_message.required_double", FindInitializationErrors(message));
474 }
475 
476 }  // namespace
477 }  // namespace internal
478 }  // namespace protobuf
479 }  // namespace google
480