• 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/wire_format.h>
36 
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/stubs/logging.h>
40 #include <google/protobuf/test_util.h>
41 #include <google/protobuf/test_util2.h>
42 #include <google/protobuf/unittest.pb.h>
43 #include <google/protobuf/unittest_mset.pb.h>
44 #include <google/protobuf/unittest_mset_wire_format.pb.h>
45 #include <google/protobuf/unittest_proto3_arena.pb.h>
46 #include <google/protobuf/io/coded_stream.h>
47 #include <google/protobuf/io/zero_copy_stream_impl.h>
48 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
49 #include <google/protobuf/descriptor.h>
50 #include <google/protobuf/wire_format_lite.h>
51 #include <google/protobuf/testing/googletest.h>
52 #include <gmock/gmock.h>
53 #include <gtest/gtest.h>
54 #include <google/protobuf/stubs/casts.h>
55 #include <google/protobuf/stubs/strutil.h>
56 #include <google/protobuf/stubs/stl_util.h>
57 
58 // clang-format off
59 #include <google/protobuf/port_def.inc>
60 // clang-format on
61 
62 namespace google {
63 namespace protobuf {
64 namespace internal {
65 namespace {
66 
TEST(WireFormatTest,EnumsInSync)67 TEST(WireFormatTest, EnumsInSync) {
68   // Verify that WireFormatLite::FieldType and WireFormatLite::CppType match
69   // FieldDescriptor::Type and FieldDescriptor::CppType.
70 
71   EXPECT_EQ(implicit_cast<int>(FieldDescriptor::MAX_TYPE),
72             implicit_cast<int>(WireFormatLite::MAX_FIELD_TYPE));
73   EXPECT_EQ(implicit_cast<int>(FieldDescriptor::MAX_CPPTYPE),
74             implicit_cast<int>(WireFormatLite::MAX_CPPTYPE));
75 
76   for (int i = 1; i <= WireFormatLite::MAX_FIELD_TYPE; i++) {
77     EXPECT_EQ(implicit_cast<int>(FieldDescriptor::TypeToCppType(
78                   static_cast<FieldDescriptor::Type>(i))),
79               implicit_cast<int>(WireFormatLite::FieldTypeToCppType(
80                   static_cast<WireFormatLite::FieldType>(i))));
81   }
82 }
83 
TEST(WireFormatTest,MaxFieldNumber)84 TEST(WireFormatTest, MaxFieldNumber) {
85   // Make sure the max field number constant is accurate.
86   EXPECT_EQ((1 << (32 - WireFormatLite::kTagTypeBits)) - 1,
87             FieldDescriptor::kMaxNumber);
88 }
89 
TEST(WireFormatTest,Parse)90 TEST(WireFormatTest, Parse) {
91   unittest::TestAllTypes source, dest;
92   std::string data;
93 
94   // Serialize using the generated code.
95   TestUtil::SetAllFields(&source);
96   source.SerializeToString(&data);
97 
98   // Parse using WireFormat.
99   io::ArrayInputStream raw_input(data.data(), data.size());
100   io::CodedInputStream input(&raw_input);
101   WireFormat::ParseAndMergePartial(&input, &dest);
102 
103   // Check.
104   TestUtil::ExpectAllFieldsSet(dest);
105 }
106 
TEST(WireFormatTest,ParseExtensions)107 TEST(WireFormatTest, ParseExtensions) {
108   unittest::TestAllExtensions source, dest;
109   std::string data;
110 
111   // Serialize using the generated code.
112   TestUtil::SetAllExtensions(&source);
113   source.SerializeToString(&data);
114 
115   // Parse using WireFormat.
116   io::ArrayInputStream raw_input(data.data(), data.size());
117   io::CodedInputStream input(&raw_input);
118   WireFormat::ParseAndMergePartial(&input, &dest);
119 
120   // Check.
121   TestUtil::ExpectAllExtensionsSet(dest);
122 }
123 
TEST(WireFormatTest,ParsePacked)124 TEST(WireFormatTest, ParsePacked) {
125   unittest::TestPackedTypes source, dest;
126   std::string data;
127 
128   // Serialize using the generated code.
129   TestUtil::SetPackedFields(&source);
130   source.SerializeToString(&data);
131 
132   // Parse using WireFormat.
133   io::ArrayInputStream raw_input(data.data(), data.size());
134   io::CodedInputStream input(&raw_input);
135   WireFormat::ParseAndMergePartial(&input, &dest);
136 
137   // Check.
138   TestUtil::ExpectPackedFieldsSet(dest);
139 }
140 
TEST(WireFormatTest,ParsePackedFromUnpacked)141 TEST(WireFormatTest, ParsePackedFromUnpacked) {
142   // Serialize using the generated code.
143   unittest::TestUnpackedTypes source;
144   TestUtil::SetUnpackedFields(&source);
145   std::string data = source.SerializeAsString();
146 
147   // Parse using WireFormat.
148   unittest::TestPackedTypes dest;
149   io::ArrayInputStream raw_input(data.data(), data.size());
150   io::CodedInputStream input(&raw_input);
151   WireFormat::ParseAndMergePartial(&input, &dest);
152 
153   // Check.
154   TestUtil::ExpectPackedFieldsSet(dest);
155 }
156 
TEST(WireFormatTest,ParseUnpackedFromPacked)157 TEST(WireFormatTest, ParseUnpackedFromPacked) {
158   // Serialize using the generated code.
159   unittest::TestPackedTypes source;
160   TestUtil::SetPackedFields(&source);
161   std::string data = source.SerializeAsString();
162 
163   // Parse using WireFormat.
164   unittest::TestUnpackedTypes dest;
165   io::ArrayInputStream raw_input(data.data(), data.size());
166   io::CodedInputStream input(&raw_input);
167   WireFormat::ParseAndMergePartial(&input, &dest);
168 
169   // Check.
170   TestUtil::ExpectUnpackedFieldsSet(dest);
171 }
172 
TEST(WireFormatTest,ParsePackedExtensions)173 TEST(WireFormatTest, ParsePackedExtensions) {
174   unittest::TestPackedExtensions source, dest;
175   std::string data;
176 
177   // Serialize using the generated code.
178   TestUtil::SetPackedExtensions(&source);
179   source.SerializeToString(&data);
180 
181   // Parse using WireFormat.
182   io::ArrayInputStream raw_input(data.data(), data.size());
183   io::CodedInputStream input(&raw_input);
184   WireFormat::ParseAndMergePartial(&input, &dest);
185 
186   // Check.
187   TestUtil::ExpectPackedExtensionsSet(dest);
188 }
189 
TEST(WireFormatTest,ParseOneof)190 TEST(WireFormatTest, ParseOneof) {
191   unittest::TestOneof2 source, dest;
192   std::string data;
193 
194   // Serialize using the generated code.
195   TestUtil::SetOneof1(&source);
196   source.SerializeToString(&data);
197 
198   // Parse using WireFormat.
199   io::ArrayInputStream raw_input(data.data(), data.size());
200   io::CodedInputStream input(&raw_input);
201   WireFormat::ParseAndMergePartial(&input, &dest);
202 
203   // Check.
204   TestUtil::ExpectOneofSet1(dest);
205 }
206 
TEST(WireFormatTest,OneofOnlySetLast)207 TEST(WireFormatTest, OneofOnlySetLast) {
208   unittest::TestOneofBackwardsCompatible source;
209   unittest::TestOneof oneof_dest;
210   std::string data;
211 
212   // Set two fields
213   source.set_foo_int(100);
214   source.set_foo_string("101");
215 
216   // Serialize and parse to oneof message. Generated serializer may not order
217   // fields in tag order. Use WireFormat::SerializeWithCachedSizes instead as
218   // it sorts fields beforehand.
219   {
220     io::StringOutputStream raw_output(&data);
221     io::CodedOutputStream output(&raw_output);
222     WireFormat::SerializeWithCachedSizes(source, source.ByteSizeLong(),
223                                          &output);
224     ASSERT_FALSE(output.HadError());
225   }
226   io::ArrayInputStream raw_input(data.data(), data.size());
227   io::CodedInputStream input(&raw_input);
228   WireFormat::ParseAndMergePartial(&input, &oneof_dest);
229 
230   // Only the last field is set.
231   EXPECT_FALSE(oneof_dest.has_foo_int());
232   EXPECT_TRUE(oneof_dest.has_foo_string());
233 }
234 
TEST(WireFormatTest,ByteSize)235 TEST(WireFormatTest, ByteSize) {
236   unittest::TestAllTypes message;
237   TestUtil::SetAllFields(&message);
238 
239   EXPECT_EQ(message.ByteSizeLong(), WireFormat::ByteSize(message));
240   message.Clear();
241   EXPECT_EQ(0, message.ByteSizeLong());
242   EXPECT_EQ(0, WireFormat::ByteSize(message));
243 }
244 
TEST(WireFormatTest,ByteSizeExtensions)245 TEST(WireFormatTest, ByteSizeExtensions) {
246   unittest::TestAllExtensions message;
247   TestUtil::SetAllExtensions(&message);
248 
249   EXPECT_EQ(message.ByteSizeLong(), WireFormat::ByteSize(message));
250   message.Clear();
251   EXPECT_EQ(0, message.ByteSizeLong());
252   EXPECT_EQ(0, WireFormat::ByteSize(message));
253 }
254 
TEST(WireFormatTest,ByteSizePacked)255 TEST(WireFormatTest, ByteSizePacked) {
256   unittest::TestPackedTypes message;
257   TestUtil::SetPackedFields(&message);
258 
259   EXPECT_EQ(message.ByteSizeLong(), WireFormat::ByteSize(message));
260   message.Clear();
261   EXPECT_EQ(0, message.ByteSizeLong());
262   EXPECT_EQ(0, WireFormat::ByteSize(message));
263 }
264 
TEST(WireFormatTest,ByteSizePackedExtensions)265 TEST(WireFormatTest, ByteSizePackedExtensions) {
266   unittest::TestPackedExtensions message;
267   TestUtil::SetPackedExtensions(&message);
268 
269   EXPECT_EQ(message.ByteSizeLong(), WireFormat::ByteSize(message));
270   message.Clear();
271   EXPECT_EQ(0, message.ByteSizeLong());
272   EXPECT_EQ(0, WireFormat::ByteSize(message));
273 }
274 
TEST(WireFormatTest,ByteSizeOneof)275 TEST(WireFormatTest, ByteSizeOneof) {
276   unittest::TestOneof2 message;
277   TestUtil::SetOneof1(&message);
278 
279   EXPECT_EQ(message.ByteSizeLong(), WireFormat::ByteSize(message));
280   message.Clear();
281 
282   EXPECT_EQ(0, message.ByteSizeLong());
283   EXPECT_EQ(0, WireFormat::ByteSize(message));
284 }
285 
TEST(WireFormatTest,Serialize)286 TEST(WireFormatTest, Serialize) {
287   unittest::TestAllTypes message;
288   std::string generated_data;
289   std::string dynamic_data;
290 
291   TestUtil::SetAllFields(&message);
292   size_t size = message.ByteSizeLong();
293 
294   // Serialize using the generated code.
295   {
296     io::StringOutputStream raw_output(&generated_data);
297     io::CodedOutputStream output(&raw_output);
298     message.SerializeWithCachedSizes(&output);
299     ASSERT_FALSE(output.HadError());
300   }
301 
302   // Serialize using WireFormat.
303   {
304     io::StringOutputStream raw_output(&dynamic_data);
305     io::CodedOutputStream output(&raw_output);
306     WireFormat::SerializeWithCachedSizes(message, size, &output);
307     ASSERT_FALSE(output.HadError());
308   }
309 
310   // Should parse to the same message.
311   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, generated_data));
312   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, dynamic_data));
313 }
314 
TEST(WireFormatTest,SerializeExtensions)315 TEST(WireFormatTest, SerializeExtensions) {
316   unittest::TestAllExtensions message;
317   std::string generated_data;
318   std::string dynamic_data;
319 
320   TestUtil::SetAllExtensions(&message);
321   size_t size = message.ByteSizeLong();
322 
323   // Serialize using the generated code.
324   {
325     io::StringOutputStream raw_output(&generated_data);
326     io::CodedOutputStream output(&raw_output);
327     message.SerializeWithCachedSizes(&output);
328     ASSERT_FALSE(output.HadError());
329   }
330 
331   // Serialize using WireFormat.
332   {
333     io::StringOutputStream raw_output(&dynamic_data);
334     io::CodedOutputStream output(&raw_output);
335     WireFormat::SerializeWithCachedSizes(message, size, &output);
336     ASSERT_FALSE(output.HadError());
337   }
338 
339   // Should parse to the same message.
340   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, generated_data));
341   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, dynamic_data));
342 }
343 
TEST(WireFormatTest,SerializeFieldsAndExtensions)344 TEST(WireFormatTest, SerializeFieldsAndExtensions) {
345   unittest::TestFieldOrderings message;
346   std::string generated_data;
347   std::string dynamic_data;
348 
349   TestUtil::SetAllFieldsAndExtensions(&message);
350   size_t size = message.ByteSizeLong();
351 
352   // Serialize using the generated code.
353   {
354     io::StringOutputStream raw_output(&generated_data);
355     io::CodedOutputStream output(&raw_output);
356     message.SerializeWithCachedSizes(&output);
357     ASSERT_FALSE(output.HadError());
358   }
359 
360   // Serialize using WireFormat.
361   {
362     io::StringOutputStream raw_output(&dynamic_data);
363     io::CodedOutputStream output(&raw_output);
364     WireFormat::SerializeWithCachedSizes(message, size, &output);
365     ASSERT_FALSE(output.HadError());
366   }
367 
368   // Should parse to the same message.
369   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, generated_data));
370   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, dynamic_data));
371 }
372 
TEST(WireFormatTest,SerializeOneof)373 TEST(WireFormatTest, SerializeOneof) {
374   unittest::TestOneof2 message;
375   std::string generated_data;
376   std::string dynamic_data;
377 
378   TestUtil::SetOneof1(&message);
379   size_t size = message.ByteSizeLong();
380 
381   // Serialize using the generated code.
382   {
383     io::StringOutputStream raw_output(&generated_data);
384     io::CodedOutputStream output(&raw_output);
385     message.SerializeWithCachedSizes(&output);
386     ASSERT_FALSE(output.HadError());
387   }
388 
389   // Serialize using WireFormat.
390   {
391     io::StringOutputStream raw_output(&dynamic_data);
392     io::CodedOutputStream output(&raw_output);
393     WireFormat::SerializeWithCachedSizes(message, size, &output);
394     ASSERT_FALSE(output.HadError());
395   }
396 
397   // Should parse to the same message.
398   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, generated_data));
399   EXPECT_TRUE(TestUtil::EqualsToSerialized(message, dynamic_data));
400 }
401 
TEST(WireFormatTest,ParseMultipleExtensionRanges)402 TEST(WireFormatTest, ParseMultipleExtensionRanges) {
403   // Make sure we can parse a message that contains multiple extensions ranges.
404   unittest::TestFieldOrderings source;
405   std::string data;
406 
407   TestUtil::SetAllFieldsAndExtensions(&source);
408   source.SerializeToString(&data);
409 
410   {
411     unittest::TestFieldOrderings dest;
412     EXPECT_TRUE(dest.ParseFromString(data));
413     EXPECT_EQ(source.DebugString(), dest.DebugString());
414   }
415 
416   // Also test using reflection-based parsing.
417   {
418     unittest::TestFieldOrderings dest;
419     io::ArrayInputStream raw_input(data.data(), data.size());
420     io::CodedInputStream coded_input(&raw_input);
421     EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest));
422     EXPECT_EQ(source.DebugString(), dest.DebugString());
423   }
424 }
425 
426 const int kUnknownTypeId = 1550055;
427 
TEST(WireFormatTest,SerializeMessageSet)428 TEST(WireFormatTest, SerializeMessageSet) {
429   // Set up a TestMessageSet with two known messages and an unknown one.
430   proto2_wireformat_unittest::TestMessageSet message_set;
431   message_set
432       .MutableExtension(
433           unittest::TestMessageSetExtension1::message_set_extension)
434       ->set_i(123);
435   message_set
436       .MutableExtension(
437           unittest::TestMessageSetExtension2::message_set_extension)
438       ->set_str("foo");
439   message_set.mutable_unknown_fields()->AddLengthDelimited(kUnknownTypeId,
440                                                            "bar");
441 
442   std::string data;
443   ASSERT_TRUE(message_set.SerializeToString(&data));
444 
445   // Parse back using RawMessageSet and check the contents.
446   unittest::RawMessageSet raw;
447   ASSERT_TRUE(raw.ParseFromString(data));
448 
449   EXPECT_EQ(0, raw.unknown_fields().field_count());
450 
451   ASSERT_EQ(3, raw.item_size());
452   EXPECT_EQ(
453       unittest::TestMessageSetExtension1::descriptor()->extension(0)->number(),
454       raw.item(0).type_id());
455   EXPECT_EQ(
456       unittest::TestMessageSetExtension2::descriptor()->extension(0)->number(),
457       raw.item(1).type_id());
458   EXPECT_EQ(kUnknownTypeId, raw.item(2).type_id());
459 
460   unittest::TestMessageSetExtension1 message1;
461   EXPECT_TRUE(message1.ParseFromString(raw.item(0).message()));
462   EXPECT_EQ(123, message1.i());
463 
464   unittest::TestMessageSetExtension2 message2;
465   EXPECT_TRUE(message2.ParseFromString(raw.item(1).message()));
466   EXPECT_EQ("foo", message2.str());
467 
468   EXPECT_EQ("bar", raw.item(2).message());
469 }
470 
TEST(WireFormatTest,SerializeMessageSetVariousWaysAreEqual)471 TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) {
472   // Serialize a MessageSet to a stream and to a flat array using generated
473   // code, and also using WireFormat, and check that the results are equal.
474   // Set up a TestMessageSet with two known messages and an unknown one, as
475   // above.
476 
477   proto2_wireformat_unittest::TestMessageSet message_set;
478   message_set
479       .MutableExtension(
480           unittest::TestMessageSetExtension1::message_set_extension)
481       ->set_i(123);
482   message_set
483       .MutableExtension(
484           unittest::TestMessageSetExtension2::message_set_extension)
485       ->set_str("foo");
486   message_set.mutable_unknown_fields()->AddLengthDelimited(kUnknownTypeId,
487                                                            "bar");
488 
489   size_t size = message_set.ByteSizeLong();
490   EXPECT_EQ(size, message_set.GetCachedSize());
491   ASSERT_EQ(size, WireFormat::ByteSize(message_set));
492 
493   std::string flat_data;
494   std::string stream_data;
495   std::string dynamic_data;
496   flat_data.resize(size);
497   stream_data.resize(size);
498 
499   // Serialize to flat array
500   {
501     uint8* target = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&flat_data));
502     uint8* end = message_set.SerializeWithCachedSizesToArray(target);
503     EXPECT_EQ(size, end - target);
504   }
505 
506   // Serialize to buffer
507   {
508     io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&stream_data), size,
509                                        1);
510     io::CodedOutputStream output_stream(&array_stream);
511     message_set.SerializeWithCachedSizes(&output_stream);
512     ASSERT_FALSE(output_stream.HadError());
513   }
514 
515   // Serialize to buffer with WireFormat.
516   {
517     io::StringOutputStream string_stream(&dynamic_data);
518     io::CodedOutputStream output_stream(&string_stream);
519     WireFormat::SerializeWithCachedSizes(message_set, size, &output_stream);
520     ASSERT_FALSE(output_stream.HadError());
521   }
522 
523   EXPECT_TRUE(flat_data == stream_data);
524   EXPECT_TRUE(flat_data == dynamic_data);
525 }
526 
TEST(WireFormatTest,ParseMessageSet)527 TEST(WireFormatTest, ParseMessageSet) {
528   // Set up a RawMessageSet with two known messages and an unknown one.
529   unittest::RawMessageSet raw;
530 
531   {
532     unittest::RawMessageSet::Item* item = raw.add_item();
533     item->set_type_id(unittest::TestMessageSetExtension1::descriptor()
534                           ->extension(0)
535                           ->number());
536     unittest::TestMessageSetExtension1 message;
537     message.set_i(123);
538     message.SerializeToString(item->mutable_message());
539   }
540 
541   {
542     unittest::RawMessageSet::Item* item = raw.add_item();
543     item->set_type_id(unittest::TestMessageSetExtension2::descriptor()
544                           ->extension(0)
545                           ->number());
546     unittest::TestMessageSetExtension2 message;
547     message.set_str("foo");
548     message.SerializeToString(item->mutable_message());
549   }
550 
551   {
552     unittest::RawMessageSet::Item* item = raw.add_item();
553     item->set_type_id(kUnknownTypeId);
554     item->set_message("bar");
555   }
556 
557   std::string data;
558   ASSERT_TRUE(raw.SerializeToString(&data));
559 
560   // Parse as a TestMessageSet and check the contents.
561   proto2_wireformat_unittest::TestMessageSet message_set;
562   ASSERT_TRUE(message_set.ParseFromString(data));
563 
564   EXPECT_EQ(123,
565             message_set
566                 .GetExtension(
567                     unittest::TestMessageSetExtension1::message_set_extension)
568                 .i());
569   EXPECT_EQ("foo",
570             message_set
571                 .GetExtension(
572                     unittest::TestMessageSetExtension2::message_set_extension)
573                 .str());
574 
575   ASSERT_EQ(1, message_set.unknown_fields().field_count());
576   ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED,
577             message_set.unknown_fields().field(0).type());
578   EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited());
579 
580   // Also parse using WireFormat.
581   proto2_wireformat_unittest::TestMessageSet dynamic_message_set;
582   io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()),
583                              data.size());
584   ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set));
585   EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString());
586 }
587 
TEST(WireFormatTest,ParseMessageSetWithReverseTagOrder)588 TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) {
589   std::string data;
590   {
591     unittest::TestMessageSetExtension1 message;
592     message.set_i(123);
593     // Build a MessageSet manually with its message content put before its
594     // type_id.
595     io::StringOutputStream output_stream(&data);
596     io::CodedOutputStream coded_output(&output_stream);
597     coded_output.WriteTag(WireFormatLite::kMessageSetItemStartTag);
598     // Write the message content first.
599     WireFormatLite::WriteTag(WireFormatLite::kMessageSetMessageNumber,
600                              WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
601                              &coded_output);
602     coded_output.WriteVarint32(message.ByteSizeLong());
603     message.SerializeWithCachedSizes(&coded_output);
604     // Write the type id.
605     uint32 type_id = message.GetDescriptor()->extension(0)->number();
606     WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber,
607                                 type_id, &coded_output);
608     coded_output.WriteTag(WireFormatLite::kMessageSetItemEndTag);
609   }
610   {
611     proto2_wireformat_unittest::TestMessageSet message_set;
612     ASSERT_TRUE(message_set.ParseFromString(data));
613 
614     EXPECT_EQ(123,
615               message_set
616                   .GetExtension(
617                       unittest::TestMessageSetExtension1::message_set_extension)
618                   .i());
619   }
620   {
621     // Test parse the message via Reflection.
622     proto2_wireformat_unittest::TestMessageSet message_set;
623     io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()),
624                                data.size());
625     EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set));
626     EXPECT_TRUE(input.ConsumedEntireMessage());
627 
628     EXPECT_EQ(123,
629               message_set
630                   .GetExtension(
631                       unittest::TestMessageSetExtension1::message_set_extension)
632                   .i());
633   }
634 }
635 
636 void SerializeReverseOrder(
637     const proto2_wireformat_unittest::TestMessageSet& mset,
638     io::CodedOutputStream* coded_output);
639 
SerializeReverseOrder(const unittest::TestMessageSetExtension1 & message,io::CodedOutputStream * coded_output)640 void SerializeReverseOrder(const unittest::TestMessageSetExtension1& message,
641                            io::CodedOutputStream* coded_output) {
642   WireFormatLite::WriteTag(15,  // i
643                            WireFormatLite::WIRETYPE_VARINT, coded_output);
644   coded_output->WriteVarint32(message.i());
645   WireFormatLite::WriteTag(16,  // recursive
646                            WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
647                            coded_output);
648   coded_output->WriteVarint32(message.recursive().GetCachedSize());
649   SerializeReverseOrder(message.recursive(), coded_output);
650 }
651 
SerializeReverseOrder(const proto2_wireformat_unittest::TestMessageSet & mset,io::CodedOutputStream * coded_output)652 void SerializeReverseOrder(
653     const proto2_wireformat_unittest::TestMessageSet& mset,
654     io::CodedOutputStream* coded_output) {
655   if (!mset.HasExtension(
656           unittest::TestMessageSetExtension1::message_set_extension))
657     return;
658   coded_output->WriteTag(WireFormatLite::kMessageSetItemStartTag);
659   // Write the message content first.
660   WireFormatLite::WriteTag(WireFormatLite::kMessageSetMessageNumber,
661                            WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
662                            coded_output);
663   auto& message = mset.GetExtension(
664       unittest::TestMessageSetExtension1::message_set_extension);
665   coded_output->WriteVarint32(message.GetCachedSize());
666   SerializeReverseOrder(message, coded_output);
667   // Write the type id.
668   uint32 type_id = message.GetDescriptor()->extension(0)->number();
669   WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber, type_id,
670                               coded_output);
671   coded_output->WriteTag(WireFormatLite::kMessageSetItemEndTag);
672 }
673 
TEST(WireFormatTest,ParseMessageSetWithDeepRecReverseOrder)674 TEST(WireFormatTest, ParseMessageSetWithDeepRecReverseOrder) {
675   std::string data;
676   {
677     proto2_wireformat_unittest::TestMessageSet message_set;
678     proto2_wireformat_unittest::TestMessageSet* mset = &message_set;
679     for (int i = 0; i < 200; i++) {
680       auto m = mset->MutableExtension(
681           unittest::TestMessageSetExtension1::message_set_extension);
682       m->set_i(i);
683       mset = m->mutable_recursive();
684     }
685     message_set.ByteSizeLong();
686     // Serialize with reverse payload tag order
687     io::StringOutputStream output_stream(&data);
688     io::CodedOutputStream coded_output(&output_stream);
689     SerializeReverseOrder(message_set, &coded_output);
690   }
691   proto2_wireformat_unittest::TestMessageSet message_set;
692   EXPECT_FALSE(message_set.ParseFromString(data));
693 }
694 
TEST(WireFormatTest,ParseBrokenMessageSet)695 TEST(WireFormatTest, ParseBrokenMessageSet) {
696   proto2_wireformat_unittest::TestMessageSet message_set;
697   std::string input("goodbye");  // Invalid wire format data.
698   EXPECT_FALSE(message_set.ParseFromString(input));
699 }
700 
TEST(WireFormatTest,RecursionLimit)701 TEST(WireFormatTest, RecursionLimit) {
702   unittest::TestRecursiveMessage message;
703   message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1);
704   std::string data;
705   message.SerializeToString(&data);
706 
707   {
708     io::ArrayInputStream raw_input(data.data(), data.size());
709     io::CodedInputStream input(&raw_input);
710     input.SetRecursionLimit(4);
711     unittest::TestRecursiveMessage message2;
712     EXPECT_TRUE(message2.ParseFromCodedStream(&input));
713   }
714 
715   {
716     io::ArrayInputStream raw_input(data.data(), data.size());
717     io::CodedInputStream input(&raw_input);
718     input.SetRecursionLimit(3);
719     unittest::TestRecursiveMessage message2;
720     EXPECT_FALSE(message2.ParseFromCodedStream(&input));
721   }
722 }
723 
TEST(WireFormatTest,UnknownFieldRecursionLimit)724 TEST(WireFormatTest, UnknownFieldRecursionLimit) {
725   unittest::TestEmptyMessage message;
726   message.mutable_unknown_fields()
727       ->AddGroup(1234)
728       ->AddGroup(1234)
729       ->AddGroup(1234)
730       ->AddGroup(1234)
731       ->AddVarint(1234, 123);
732   std::string data;
733   message.SerializeToString(&data);
734 
735   {
736     io::ArrayInputStream raw_input(data.data(), data.size());
737     io::CodedInputStream input(&raw_input);
738     input.SetRecursionLimit(4);
739     unittest::TestEmptyMessage message2;
740     EXPECT_TRUE(message2.ParseFromCodedStream(&input));
741   }
742 
743   {
744     io::ArrayInputStream raw_input(data.data(), data.size());
745     io::CodedInputStream input(&raw_input);
746     input.SetRecursionLimit(3);
747     unittest::TestEmptyMessage message2;
748     EXPECT_FALSE(message2.ParseFromCodedStream(&input));
749   }
750 }
751 
TEST(WireFormatTest,ZigZag)752 TEST(WireFormatTest, ZigZag) {
753 // avoid line-wrapping
754 #define LL(x) PROTOBUF_LONGLONG(x)
755 #define ULL(x) PROTOBUF_ULONGLONG(x)
756 #define ZigZagEncode32(x) WireFormatLite::ZigZagEncode32(x)
757 #define ZigZagDecode32(x) WireFormatLite::ZigZagDecode32(x)
758 #define ZigZagEncode64(x) WireFormatLite::ZigZagEncode64(x)
759 #define ZigZagDecode64(x) WireFormatLite::ZigZagDecode64(x)
760 
761   EXPECT_EQ(0u, ZigZagEncode32(0));
762   EXPECT_EQ(1u, ZigZagEncode32(-1));
763   EXPECT_EQ(2u, ZigZagEncode32(1));
764   EXPECT_EQ(3u, ZigZagEncode32(-2));
765   EXPECT_EQ(0x7FFFFFFEu, ZigZagEncode32(0x3FFFFFFF));
766   EXPECT_EQ(0x7FFFFFFFu, ZigZagEncode32(0xC0000000));
767   EXPECT_EQ(0xFFFFFFFEu, ZigZagEncode32(0x7FFFFFFF));
768   EXPECT_EQ(0xFFFFFFFFu, ZigZagEncode32(0x80000000));
769 
770   EXPECT_EQ(0, ZigZagDecode32(0u));
771   EXPECT_EQ(-1, ZigZagDecode32(1u));
772   EXPECT_EQ(1, ZigZagDecode32(2u));
773   EXPECT_EQ(-2, ZigZagDecode32(3u));
774   EXPECT_EQ(0x3FFFFFFF, ZigZagDecode32(0x7FFFFFFEu));
775   EXPECT_EQ(0xC0000000, ZigZagDecode32(0x7FFFFFFFu));
776   EXPECT_EQ(0x7FFFFFFF, ZigZagDecode32(0xFFFFFFFEu));
777   EXPECT_EQ(0x80000000, ZigZagDecode32(0xFFFFFFFFu));
778 
779   EXPECT_EQ(0u, ZigZagEncode64(0));
780   EXPECT_EQ(1u, ZigZagEncode64(-1));
781   EXPECT_EQ(2u, ZigZagEncode64(1));
782   EXPECT_EQ(3u, ZigZagEncode64(-2));
783   EXPECT_EQ(ULL(0x000000007FFFFFFE), ZigZagEncode64(LL(0x000000003FFFFFFF)));
784   EXPECT_EQ(ULL(0x000000007FFFFFFF), ZigZagEncode64(LL(0xFFFFFFFFC0000000)));
785   EXPECT_EQ(ULL(0x00000000FFFFFFFE), ZigZagEncode64(LL(0x000000007FFFFFFF)));
786   EXPECT_EQ(ULL(0x00000000FFFFFFFF), ZigZagEncode64(LL(0xFFFFFFFF80000000)));
787   EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFE), ZigZagEncode64(LL(0x7FFFFFFFFFFFFFFF)));
788   EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFF), ZigZagEncode64(LL(0x8000000000000000)));
789 
790   EXPECT_EQ(0, ZigZagDecode64(0u));
791   EXPECT_EQ(-1, ZigZagDecode64(1u));
792   EXPECT_EQ(1, ZigZagDecode64(2u));
793   EXPECT_EQ(-2, ZigZagDecode64(3u));
794   EXPECT_EQ(LL(0x000000003FFFFFFF), ZigZagDecode64(ULL(0x000000007FFFFFFE)));
795   EXPECT_EQ(LL(0xFFFFFFFFC0000000), ZigZagDecode64(ULL(0x000000007FFFFFFF)));
796   EXPECT_EQ(LL(0x000000007FFFFFFF), ZigZagDecode64(ULL(0x00000000FFFFFFFE)));
797   EXPECT_EQ(LL(0xFFFFFFFF80000000), ZigZagDecode64(ULL(0x00000000FFFFFFFF)));
798   EXPECT_EQ(LL(0x7FFFFFFFFFFFFFFF), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFE)));
799   EXPECT_EQ(LL(0x8000000000000000), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFF)));
800 
801   // Some easier-to-verify round-trip tests.  The inputs (other than 0, 1, -1)
802   // were chosen semi-randomly via keyboard bashing.
803   EXPECT_EQ(0, ZigZagDecode32(ZigZagEncode32(0)));
804   EXPECT_EQ(1, ZigZagDecode32(ZigZagEncode32(1)));
805   EXPECT_EQ(-1, ZigZagDecode32(ZigZagEncode32(-1)));
806   EXPECT_EQ(14927, ZigZagDecode32(ZigZagEncode32(14927)));
807   EXPECT_EQ(-3612, ZigZagDecode32(ZigZagEncode32(-3612)));
808 
809   EXPECT_EQ(0, ZigZagDecode64(ZigZagEncode64(0)));
810   EXPECT_EQ(1, ZigZagDecode64(ZigZagEncode64(1)));
811   EXPECT_EQ(-1, ZigZagDecode64(ZigZagEncode64(-1)));
812   EXPECT_EQ(14927, ZigZagDecode64(ZigZagEncode64(14927)));
813   EXPECT_EQ(-3612, ZigZagDecode64(ZigZagEncode64(-3612)));
814 
815   EXPECT_EQ(LL(856912304801416),
816             ZigZagDecode64(ZigZagEncode64(LL(856912304801416))));
817   EXPECT_EQ(LL(-75123905439571256),
818             ZigZagDecode64(ZigZagEncode64(LL(-75123905439571256))));
819 }
820 
TEST(WireFormatTest,RepeatedScalarsDifferentTagSizes)821 TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) {
822   // At one point checks would trigger when parsing repeated fixed scalar
823   // fields.
824   protobuf_unittest::TestRepeatedScalarDifferentTagSizes msg1, msg2;
825   for (int i = 0; i < 100; ++i) {
826     msg1.add_repeated_fixed32(i);
827     msg1.add_repeated_int32(i);
828     msg1.add_repeated_fixed64(i);
829     msg1.add_repeated_int64(i);
830     msg1.add_repeated_float(i);
831     msg1.add_repeated_uint64(i);
832   }
833 
834   // Make sure that we have a variety of tag sizes.
835   const Descriptor* desc = msg1.GetDescriptor();
836   const FieldDescriptor* field;
837   field = desc->FindFieldByName("repeated_fixed32");
838   ASSERT_TRUE(field != NULL);
839   ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
840   field = desc->FindFieldByName("repeated_int32");
841   ASSERT_TRUE(field != NULL);
842   ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
843   field = desc->FindFieldByName("repeated_fixed64");
844   ASSERT_TRUE(field != NULL);
845   ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
846   field = desc->FindFieldByName("repeated_int64");
847   ASSERT_TRUE(field != NULL);
848   ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
849   field = desc->FindFieldByName("repeated_float");
850   ASSERT_TRUE(field != NULL);
851   ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
852   field = desc->FindFieldByName("repeated_uint64");
853   ASSERT_TRUE(field != NULL);
854   ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
855 
856   EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString()));
857   EXPECT_EQ(msg1.DebugString(), msg2.DebugString());
858 }
859 
TEST(WireFormatTest,CompatibleTypes)860 TEST(WireFormatTest, CompatibleTypes) {
861   const int64 data = 0x100000000LL;
862   unittest::Int64Message msg1;
863   msg1.set_data(data);
864   std::string serialized;
865   msg1.SerializeToString(&serialized);
866 
867   // Test int64 is compatible with bool
868   unittest::BoolMessage msg2;
869   ASSERT_TRUE(msg2.ParseFromString(serialized));
870   ASSERT_EQ(static_cast<bool>(data), msg2.data());
871 
872   // Test int64 is compatible with uint64
873   unittest::Uint64Message msg3;
874   ASSERT_TRUE(msg3.ParseFromString(serialized));
875   ASSERT_EQ(static_cast<uint64>(data), msg3.data());
876 
877   // Test int64 is compatible with int32
878   unittest::Int32Message msg4;
879   ASSERT_TRUE(msg4.ParseFromString(serialized));
880   ASSERT_EQ(static_cast<int32>(data), msg4.data());
881 
882   // Test int64 is compatible with uint32
883   unittest::Uint32Message msg5;
884   ASSERT_TRUE(msg5.ParseFromString(serialized));
885   ASSERT_EQ(static_cast<uint32>(data), msg5.data());
886 }
887 
888 class Proto3PrimitiveRepeatedWireFormatTest : public ::testing::Test {
889  protected:
Proto3PrimitiveRepeatedWireFormatTest()890   Proto3PrimitiveRepeatedWireFormatTest()
891       : packedTestAllTypes_(
892             "\xFA\x01\x01\x01"
893             "\x82\x02\x01\x01"
894             "\x8A\x02\x01\x01"
895             "\x92\x02\x01\x01"
896             "\x9A\x02\x01\x02"
897             "\xA2\x02\x01\x02"
898             "\xAA\x02\x04\x01\x00\x00\x00"
899             "\xB2\x02\x08\x01\x00\x00\x00\x00\x00\x00\x00"
900             "\xBA\x02\x04\x01\x00\x00\x00"
901             "\xC2\x02\x08\x01\x00\x00\x00\x00\x00\x00\x00"
902             "\xCA\x02\x04\x00\x00\x80\x3f"
903             "\xD2\x02\x08\x00\x00\x00\x00\x00\x00\xf0\x3f"
904             "\xDA\x02\x01\x01"
905             "\x9A\x03\x01\x01",
906             86),
907         packedTestUnpackedTypes_(
908             "\x0A\x01\x01"
909             "\x12\x01\x01"
910             "\x1A\x01\x01"
911             "\x22\x01\x01"
912             "\x2A\x01\x02"
913             "\x32\x01\x02"
914             "\x3A\x04\x01\x00\x00\x00"
915             "\x42\x08\x01\x00\x00\x00\x00\x00\x00\x00"
916             "\x4A\x04\x01\x00\x00\x00"
917             "\x52\x08\x01\x00\x00\x00\x00\x00\x00\x00"
918             "\x5A\x04\x00\x00\x80\x3f"
919             "\x62\x08\x00\x00\x00\x00\x00\x00\xf0\x3f"
920             "\x6A\x01\x01"
921             "\x72\x01\x01",
922             72),
923         unpackedTestAllTypes_(
924             "\xF8\x01\x01"
925             "\x80\x02\x01"
926             "\x88\x02\x01"
927             "\x90\x02\x01"
928             "\x98\x02\x02"
929             "\xA0\x02\x02"
930             "\xAD\x02\x01\x00\x00\x00"
931             "\xB1\x02\x01\x00\x00\x00\x00\x00\x00\x00"
932             "\xBD\x02\x01\x00\x00\x00"
933             "\xC1\x02\x01\x00\x00\x00\x00\x00\x00\x00"
934             "\xCD\x02\x00\x00\x80\x3f"
935             "\xD1\x02\x00\x00\x00\x00\x00\x00\xf0\x3f"
936             "\xD8\x02\x01"
937             "\x98\x03\x01",
938             72),
939         unpackedTestUnpackedTypes_(
940             "\x08\x01"
941             "\x10\x01"
942             "\x18\x01"
943             "\x20\x01"
944             "\x28\x02"
945             "\x30\x02"
946             "\x3D\x01\x00\x00\x00"
947             "\x41\x01\x00\x00\x00\x00\x00\x00\x00"
948             "\x4D\x01\x00\x00\x00"
949             "\x51\x01\x00\x00\x00\x00\x00\x00\x00"
950             "\x5D\x00\x00\x80\x3f"
951             "\x61\x00\x00\x00\x00\x00\x00\xf0\x3f"
952             "\x68\x01"
953             "\x70\x01",
954             58) {}
955   template <class Proto>
SetProto3PrimitiveRepeatedFields(Proto * message)956   void SetProto3PrimitiveRepeatedFields(Proto* message) {
957     message->add_repeated_int32(1);
958     message->add_repeated_int64(1);
959     message->add_repeated_uint32(1);
960     message->add_repeated_uint64(1);
961     message->add_repeated_sint32(1);
962     message->add_repeated_sint64(1);
963     message->add_repeated_fixed32(1);
964     message->add_repeated_fixed64(1);
965     message->add_repeated_sfixed32(1);
966     message->add_repeated_sfixed64(1);
967     message->add_repeated_float(1.0);
968     message->add_repeated_double(1.0);
969     message->add_repeated_bool(true);
970     message->add_repeated_nested_enum(proto3_arena_unittest::TestAllTypes::FOO);
971   }
972 
973   template <class Proto>
ExpectProto3PrimitiveRepeatedFieldsSet(const Proto & message)974   void ExpectProto3PrimitiveRepeatedFieldsSet(const Proto& message) {
975     EXPECT_EQ(1, message.repeated_int32(0));
976     EXPECT_EQ(1, message.repeated_int64(0));
977     EXPECT_EQ(1, message.repeated_uint32(0));
978     EXPECT_EQ(1, message.repeated_uint64(0));
979     EXPECT_EQ(1, message.repeated_sint32(0));
980     EXPECT_EQ(1, message.repeated_sint64(0));
981     EXPECT_EQ(1, message.repeated_fixed32(0));
982     EXPECT_EQ(1, message.repeated_fixed64(0));
983     EXPECT_EQ(1, message.repeated_sfixed32(0));
984     EXPECT_EQ(1, message.repeated_sfixed64(0));
985     EXPECT_EQ(1.0, message.repeated_float(0));
986     EXPECT_EQ(1.0, message.repeated_double(0));
987     EXPECT_EQ(true, message.repeated_bool(0));
988     EXPECT_EQ(proto3_arena_unittest::TestAllTypes::FOO,
989               message.repeated_nested_enum(0));
990   }
991 
992   template <class Proto>
TestSerialization(Proto * message,const std::string & expected)993   void TestSerialization(Proto* message, const std::string& expected) {
994     SetProto3PrimitiveRepeatedFields(message);
995 
996     size_t size = message->ByteSizeLong();
997 
998     // Serialize using the generated code.
999     std::string generated_data;
1000     {
1001       io::StringOutputStream raw_output(&generated_data);
1002       io::CodedOutputStream output(&raw_output);
1003       message->SerializeWithCachedSizes(&output);
1004       ASSERT_FALSE(output.HadError());
1005     }
1006     EXPECT_TRUE(TestUtil::EqualsToSerialized(*message, generated_data));
1007 
1008     // Serialize using the dynamic code.
1009     std::string dynamic_data;
1010     {
1011       io::StringOutputStream raw_output(&dynamic_data);
1012       io::CodedOutputStream output(&raw_output);
1013       WireFormat::SerializeWithCachedSizes(*message, size, &output);
1014       ASSERT_FALSE(output.HadError());
1015     }
1016     EXPECT_TRUE(expected == dynamic_data);
1017   }
1018 
1019   template <class Proto>
TestParsing(Proto * message,const std::string & compatible_data)1020   void TestParsing(Proto* message, const std::string& compatible_data) {
1021     message->Clear();
1022     message->ParseFromString(compatible_data);
1023     ExpectProto3PrimitiveRepeatedFieldsSet(*message);
1024 
1025     message->Clear();
1026     io::CodedInputStream input(
1027         reinterpret_cast<const uint8*>(compatible_data.data()),
1028         compatible_data.size());
1029     WireFormat::ParseAndMergePartial(&input, message);
1030     ExpectProto3PrimitiveRepeatedFieldsSet(*message);
1031   }
1032 
1033   const std::string packedTestAllTypes_;
1034   const std::string packedTestUnpackedTypes_;
1035   const std::string unpackedTestAllTypes_;
1036   const std::string unpackedTestUnpackedTypes_;
1037 };
1038 
TEST_F(Proto3PrimitiveRepeatedWireFormatTest,Proto3PrimitiveRepeated)1039 TEST_F(Proto3PrimitiveRepeatedWireFormatTest, Proto3PrimitiveRepeated) {
1040   proto3_arena_unittest::TestAllTypes packed_message;
1041   proto3_arena_unittest::TestUnpackedTypes unpacked_message;
1042   TestSerialization(&packed_message, packedTestAllTypes_);
1043   TestParsing(&packed_message, packedTestAllTypes_);
1044   TestParsing(&packed_message, unpackedTestAllTypes_);
1045   TestSerialization(&unpacked_message, unpackedTestUnpackedTypes_);
1046   TestParsing(&unpacked_message, packedTestUnpackedTypes_);
1047   TestParsing(&unpacked_message, unpackedTestUnpackedTypes_);
1048 }
1049 
1050 class WireFormatInvalidInputTest : public testing::Test {
1051  protected:
1052   // Make a serialized TestAllTypes in which the field optional_nested_message
1053   // contains exactly the given bytes, which may be invalid.
MakeInvalidEmbeddedMessage(const char * bytes,int size)1054   std::string MakeInvalidEmbeddedMessage(const char* bytes, int size) {
1055     const FieldDescriptor* field =
1056         unittest::TestAllTypes::descriptor()->FindFieldByName(
1057             "optional_nested_message");
1058     GOOGLE_CHECK(field != NULL);
1059 
1060     std::string result;
1061 
1062     {
1063       io::StringOutputStream raw_output(&result);
1064       io::CodedOutputStream output(&raw_output);
1065 
1066       WireFormatLite::WriteBytes(field->number(), std::string(bytes, size),
1067                                  &output);
1068     }
1069 
1070     return result;
1071   }
1072 
1073   // Make a serialized TestAllTypes in which the field optionalgroup
1074   // contains exactly the given bytes -- which may be invalid -- and
1075   // possibly no end tag.
MakeInvalidGroup(const char * bytes,int size,bool include_end_tag)1076   std::string MakeInvalidGroup(const char* bytes, int size,
1077                                bool include_end_tag) {
1078     const FieldDescriptor* field =
1079         unittest::TestAllTypes::descriptor()->FindFieldByName("optionalgroup");
1080     GOOGLE_CHECK(field != NULL);
1081 
1082     std::string result;
1083 
1084     {
1085       io::StringOutputStream raw_output(&result);
1086       io::CodedOutputStream output(&raw_output);
1087 
1088       output.WriteVarint32(WireFormat::MakeTag(field));
1089       output.WriteString(std::string(bytes, size));
1090       if (include_end_tag) {
1091         output.WriteVarint32(WireFormatLite::MakeTag(
1092             field->number(), WireFormatLite::WIRETYPE_END_GROUP));
1093       }
1094     }
1095 
1096     return result;
1097   }
1098 };
1099 
TEST_F(WireFormatInvalidInputTest,InvalidSubMessage)1100 TEST_F(WireFormatInvalidInputTest, InvalidSubMessage) {
1101   unittest::TestAllTypes message;
1102 
1103   // Control case.
1104   EXPECT_TRUE(message.ParseFromString(MakeInvalidEmbeddedMessage("", 0)));
1105 
1106   // The byte is a valid varint, but not a valid tag (zero).
1107   EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\0", 1)));
1108 
1109   // The byte is a malformed varint.
1110   EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\200", 1)));
1111 
1112   // The byte is an endgroup tag, but we aren't parsing a group.
1113   EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\014", 1)));
1114 
1115   // The byte is a valid varint but not a valid tag (bad wire type).
1116   EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\017", 1)));
1117 }
1118 
TEST_F(WireFormatInvalidInputTest,InvalidMessageWithExtraZero)1119 TEST_F(WireFormatInvalidInputTest, InvalidMessageWithExtraZero) {
1120   std::string data;
1121   {
1122     // Serialize a valid proto
1123     unittest::TestAllTypes message;
1124     message.set_optional_int32(1);
1125     message.SerializeToString(&data);
1126     data.push_back(0);  // Append invalid zero tag
1127   }
1128 
1129   // Control case.
1130   {
1131     io::ArrayInputStream ais(data.data(), data.size());
1132     io::CodedInputStream is(&ais);
1133     unittest::TestAllTypes message;
1134     // It should fail but currently passes.
1135     EXPECT_TRUE(message.MergePartialFromCodedStream(&is));
1136     // Parsing from the string should fail.
1137     EXPECT_FALSE(message.ParseFromString(data));
1138   }
1139 }
1140 
TEST_F(WireFormatInvalidInputTest,InvalidGroup)1141 TEST_F(WireFormatInvalidInputTest, InvalidGroup) {
1142   unittest::TestAllTypes message;
1143 
1144   // Control case.
1145   EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
1146 
1147   // Missing end tag.  Groups cannot end at EOF.
1148   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
1149 
1150   // The byte is a valid varint, but not a valid tag (zero).
1151   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
1152 
1153   // The byte is a malformed varint.
1154   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
1155 
1156   // The byte is an endgroup tag, but not the right one for this group.
1157   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
1158 
1159   // The byte is a valid varint but not a valid tag (bad wire type).
1160   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
1161 }
1162 
TEST_F(WireFormatInvalidInputTest,InvalidUnknownGroup)1163 TEST_F(WireFormatInvalidInputTest, InvalidUnknownGroup) {
1164   // Use TestEmptyMessage so that the group made by MakeInvalidGroup will not
1165   // be a known tag number.
1166   unittest::TestEmptyMessage message;
1167 
1168   // Control case.
1169   EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
1170 
1171   // Missing end tag.  Groups cannot end at EOF.
1172   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
1173 
1174   // The byte is a valid varint, but not a valid tag (zero).
1175   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
1176 
1177   // The byte is a malformed varint.
1178   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
1179 
1180   // The byte is an endgroup tag, but not the right one for this group.
1181   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
1182 
1183   // The byte is a valid varint but not a valid tag (bad wire type).
1184   EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
1185 }
1186 
TEST_F(WireFormatInvalidInputTest,InvalidStringInUnknownGroup)1187 TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) {
1188   // Test a bug fix:  SkipMessage should fail if the message contains a
1189   // string whose length would extend beyond the message end.
1190 
1191   unittest::TestAllTypes message;
1192   message.set_optional_string("foo foo foo foo");
1193   std::string data;
1194   message.SerializeToString(&data);
1195 
1196   // Chop some bytes off the end.
1197   data.resize(data.size() - 4);
1198 
1199   // Try to skip it.  Note that the bug was only present when parsing to an
1200   // UnknownFieldSet.
1201   io::ArrayInputStream raw_input(data.data(), data.size());
1202   io::CodedInputStream coded_input(&raw_input);
1203   UnknownFieldSet unknown_fields;
1204   EXPECT_FALSE(WireFormat::SkipMessage(&coded_input, &unknown_fields));
1205 }
1206 
1207 // Test differences between string and bytes.
1208 // Value of a string type must be valid UTF-8 string.  When UTF-8
1209 // validation is enabled (GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED):
1210 // WriteInvalidUTF8String:  see error message.
1211 // ReadInvalidUTF8String:  see error message.
1212 // WriteValidUTF8String: fine.
1213 // ReadValidUTF8String:  fine.
1214 // WriteAnyBytes: fine.
1215 // ReadAnyBytes: fine.
1216 const char* kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0";
1217 // This used to be "Valid UTF-8: \x01\x02\u8C37\u6B4C", but MSVC seems to
1218 // interpret \u differently from GCC.
1219 const char* kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214";
1220 
1221 template <typename T>
WriteMessage(const char * value,T * message,std::string * wire_buffer)1222 bool WriteMessage(const char* value, T* message, std::string* wire_buffer) {
1223   message->set_data(value);
1224   wire_buffer->clear();
1225   message->AppendToString(wire_buffer);
1226   return (wire_buffer->size() > 0);
1227 }
1228 
1229 template <typename T>
ReadMessage(const std::string & wire_buffer,T * message)1230 bool ReadMessage(const std::string& wire_buffer, T* message) {
1231   return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
1232 }
1233 
1234 class Utf8ValidationTest : public ::testing::Test {
1235  protected:
Utf8ValidationTest()1236   Utf8ValidationTest() {}
~Utf8ValidationTest()1237   virtual ~Utf8ValidationTest() {}
SetUp()1238   virtual void SetUp() {
1239   }
1240 
1241 };
1242 
TEST_F(Utf8ValidationTest,WriteInvalidUTF8String)1243 TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) {
1244   std::string wire_buffer;
1245   protobuf_unittest::OneString input;
1246   std::vector<std::string> errors;
1247   {
1248     ScopedMemoryLog log;
1249     WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1250     errors = log.GetMessages(ERROR);
1251   }
1252 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1253   ASSERT_EQ(1, errors.size());
1254   EXPECT_TRUE(
1255       HasPrefixString(errors[0],
1256                        "String field 'protobuf_unittest.OneString.data' "
1257                        "contains invalid UTF-8 data when "
1258                        "serializing a protocol buffer. Use the "
1259                        "'bytes' type if you intend to send raw bytes."));
1260 #else
1261   ASSERT_EQ(0, errors.size());
1262 #endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1263 }
1264 
1265 
TEST_F(Utf8ValidationTest,ReadInvalidUTF8String)1266 TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) {
1267   std::string wire_buffer;
1268   protobuf_unittest::OneString input;
1269   WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1270   protobuf_unittest::OneString output;
1271   std::vector<std::string> errors;
1272   {
1273     ScopedMemoryLog log;
1274     ReadMessage(wire_buffer, &output);
1275     errors = log.GetMessages(ERROR);
1276   }
1277 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1278   ASSERT_EQ(1, errors.size());
1279   EXPECT_TRUE(
1280       HasPrefixString(errors[0],
1281                        "String field 'protobuf_unittest.OneString.data' "
1282                        "contains invalid UTF-8 data when "
1283                        "parsing a protocol buffer. Use the "
1284                        "'bytes' type if you intend to send raw bytes."));
1285 
1286 #else
1287   ASSERT_EQ(0, errors.size());
1288 #endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1289 }
1290 
1291 
TEST_F(Utf8ValidationTest,WriteValidUTF8String)1292 TEST_F(Utf8ValidationTest, WriteValidUTF8String) {
1293   std::string wire_buffer;
1294   protobuf_unittest::OneString input;
1295   std::vector<std::string> errors;
1296   {
1297     ScopedMemoryLog log;
1298     WriteMessage(kValidUTF8String, &input, &wire_buffer);
1299     errors = log.GetMessages(ERROR);
1300   }
1301   ASSERT_EQ(0, errors.size());
1302 }
1303 
TEST_F(Utf8ValidationTest,ReadValidUTF8String)1304 TEST_F(Utf8ValidationTest, ReadValidUTF8String) {
1305   std::string wire_buffer;
1306   protobuf_unittest::OneString input;
1307   WriteMessage(kValidUTF8String, &input, &wire_buffer);
1308   protobuf_unittest::OneString output;
1309   std::vector<std::string> errors;
1310   {
1311     ScopedMemoryLog log;
1312     ReadMessage(wire_buffer, &output);
1313     errors = log.GetMessages(ERROR);
1314   }
1315   ASSERT_EQ(0, errors.size());
1316   EXPECT_EQ(input.data(), output.data());
1317 }
1318 
1319 // Bytes: anything can pass as bytes, use invalid UTF-8 string to test
TEST_F(Utf8ValidationTest,WriteArbitraryBytes)1320 TEST_F(Utf8ValidationTest, WriteArbitraryBytes) {
1321   std::string wire_buffer;
1322   protobuf_unittest::OneBytes input;
1323   std::vector<std::string> errors;
1324   {
1325     ScopedMemoryLog log;
1326     WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1327     errors = log.GetMessages(ERROR);
1328   }
1329   ASSERT_EQ(0, errors.size());
1330 }
1331 
TEST_F(Utf8ValidationTest,ReadArbitraryBytes)1332 TEST_F(Utf8ValidationTest, ReadArbitraryBytes) {
1333   std::string wire_buffer;
1334   protobuf_unittest::OneBytes input;
1335   WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1336   protobuf_unittest::OneBytes output;
1337   std::vector<std::string> errors;
1338   {
1339     ScopedMemoryLog log;
1340     ReadMessage(wire_buffer, &output);
1341     errors = log.GetMessages(ERROR);
1342   }
1343   ASSERT_EQ(0, errors.size());
1344   EXPECT_EQ(input.data(), output.data());
1345 }
1346 
TEST_F(Utf8ValidationTest,ParseRepeatedString)1347 TEST_F(Utf8ValidationTest, ParseRepeatedString) {
1348   protobuf_unittest::MoreBytes input;
1349   input.add_data(kValidUTF8String);
1350   input.add_data(kInvalidUTF8String);
1351   input.add_data(kInvalidUTF8String);
1352   std::string wire_buffer = input.SerializeAsString();
1353 
1354   protobuf_unittest::MoreString output;
1355   std::vector<std::string> errors;
1356   {
1357     ScopedMemoryLog log;
1358     ReadMessage(wire_buffer, &output);
1359     errors = log.GetMessages(ERROR);
1360   }
1361 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1362   ASSERT_EQ(2, errors.size());
1363 #else
1364   ASSERT_EQ(0, errors.size());
1365 #endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1366   EXPECT_EQ(wire_buffer, output.SerializeAsString());
1367 }
1368 
1369 // Test the old VerifyUTF8String() function, which may still be called by old
1370 // generated code.
TEST_F(Utf8ValidationTest,OldVerifyUTF8String)1371 TEST_F(Utf8ValidationTest, OldVerifyUTF8String) {
1372   std::string data(kInvalidUTF8String);
1373 
1374   std::vector<std::string> errors;
1375   {
1376     ScopedMemoryLog log;
1377     WireFormat::VerifyUTF8String(data.data(), data.size(),
1378                                  WireFormat::SERIALIZE);
1379     errors = log.GetMessages(ERROR);
1380   }
1381 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1382   ASSERT_EQ(1, errors.size());
1383   EXPECT_TRUE(
1384       HasPrefixString(errors[0],
1385                        "String field contains invalid UTF-8 data when "
1386                        "serializing a protocol buffer. Use the "
1387                        "'bytes' type if you intend to send raw bytes."));
1388 #else
1389   ASSERT_EQ(0, errors.size());
1390 #endif
1391 }
1392 
1393 
TEST(RepeatedVarint,Int32)1394 TEST(RepeatedVarint, Int32) {
1395   RepeatedField<int32> v;
1396 
1397   // Insert -2^n, 2^n and 2^n-1.
1398   for (int n = 0; n < 10; n++) {
1399     v.Add(-(1 << n));
1400     v.Add(1 << n);
1401     v.Add((1 << n) - 1);
1402   }
1403 
1404   // Check consistency with the scalar Int32Size.
1405   size_t expected = 0;
1406   for (int i = 0; i < v.size(); i++) {
1407     expected += WireFormatLite::Int32Size(v[i]);
1408   }
1409 
1410   EXPECT_EQ(expected, WireFormatLite::Int32Size(v));
1411 }
1412 
TEST(RepeatedVarint,Int64)1413 TEST(RepeatedVarint, Int64) {
1414   RepeatedField<int64> v;
1415 
1416   // Insert -2^n, 2^n and 2^n-1.
1417   for (int n = 0; n < 10; n++) {
1418     v.Add(-(1 << n));
1419     v.Add(1 << n);
1420     v.Add((1 << n) - 1);
1421   }
1422 
1423   // Check consistency with the scalar Int64Size.
1424   size_t expected = 0;
1425   for (int i = 0; i < v.size(); i++) {
1426     expected += WireFormatLite::Int64Size(v[i]);
1427   }
1428 
1429   EXPECT_EQ(expected, WireFormatLite::Int64Size(v));
1430 }
1431 
TEST(RepeatedVarint,SInt32)1432 TEST(RepeatedVarint, SInt32) {
1433   RepeatedField<int32> v;
1434 
1435   // Insert -2^n, 2^n and 2^n-1.
1436   for (int n = 0; n < 10; n++) {
1437     v.Add(-(1 << n));
1438     v.Add(1 << n);
1439     v.Add((1 << n) - 1);
1440   }
1441 
1442   // Check consistency with the scalar SInt32Size.
1443   size_t expected = 0;
1444   for (int i = 0; i < v.size(); i++) {
1445     expected += WireFormatLite::SInt32Size(v[i]);
1446   }
1447 
1448   EXPECT_EQ(expected, WireFormatLite::SInt32Size(v));
1449 }
1450 
TEST(RepeatedVarint,SInt64)1451 TEST(RepeatedVarint, SInt64) {
1452   RepeatedField<int64> v;
1453 
1454   // Insert -2^n, 2^n and 2^n-1.
1455   for (int n = 0; n < 10; n++) {
1456     v.Add(-(1 << n));
1457     v.Add(1 << n);
1458     v.Add((1 << n) - 1);
1459   }
1460 
1461   // Check consistency with the scalar SInt64Size.
1462   size_t expected = 0;
1463   for (int i = 0; i < v.size(); i++) {
1464     expected += WireFormatLite::SInt64Size(v[i]);
1465   }
1466 
1467   EXPECT_EQ(expected, WireFormatLite::SInt64Size(v));
1468 }
1469 
TEST(RepeatedVarint,UInt32)1470 TEST(RepeatedVarint, UInt32) {
1471   RepeatedField<uint32> v;
1472 
1473   // Insert 2^n and 2^n-1.
1474   for (int n = 0; n < 10; n++) {
1475     v.Add(1 << n);
1476     v.Add((1 << n) - 1);
1477   }
1478 
1479   // Check consistency with the scalar UInt32Size.
1480   size_t expected = 0;
1481   for (int i = 0; i < v.size(); i++) {
1482     expected += WireFormatLite::UInt32Size(v[i]);
1483   }
1484 
1485   EXPECT_EQ(expected, WireFormatLite::UInt32Size(v));
1486 }
1487 
TEST(RepeatedVarint,UInt64)1488 TEST(RepeatedVarint, UInt64) {
1489   RepeatedField<uint64> v;
1490 
1491   // Insert 2^n and 2^n-1.
1492   for (int n = 0; n < 10; n++) {
1493     v.Add(1 << n);
1494     v.Add((1 << n) - 1);
1495   }
1496 
1497   // Check consistency with the scalar UInt64Size.
1498   size_t expected = 0;
1499   for (int i = 0; i < v.size(); i++) {
1500     expected += WireFormatLite::UInt64Size(v[i]);
1501   }
1502 
1503   EXPECT_EQ(expected, WireFormatLite::UInt64Size(v));
1504 }
1505 
TEST(RepeatedVarint,Enum)1506 TEST(RepeatedVarint, Enum) {
1507   RepeatedField<int> v;
1508 
1509   // Insert 2^n and 2^n-1.
1510   for (int n = 0; n < 10; n++) {
1511     v.Add(1 << n);
1512     v.Add((1 << n) - 1);
1513   }
1514 
1515   // Check consistency with the scalar EnumSize.
1516   size_t expected = 0;
1517   for (int i = 0; i < v.size(); i++) {
1518     expected += WireFormatLite::EnumSize(v[i]);
1519   }
1520 
1521   EXPECT_EQ(expected, WireFormatLite::EnumSize(v));
1522 }
1523 
1524 
1525 }  // namespace
1526 }  // namespace internal
1527 }  // namespace protobuf
1528 }  // namespace google
1529