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


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:02