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