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