1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 #include <google/protobuf/wire_format.h>
36 #include <google/protobuf/wire_format_lite_inl.h>
37 #include <google/protobuf/descriptor.h>
38 #include <google/protobuf/io/zero_copy_stream_impl.h>
39 #include <google/protobuf/io/coded_stream.h>
40 #include <google/protobuf/unittest.pb.h>
41 #include <google/protobuf/unittest_mset.pb.h>
42 #include <google/protobuf/test_util.h>
44 #include <google/protobuf/stubs/common.h>
45 #include <google/protobuf/testing/googletest.h>
46 #include <gtest/gtest.h>
47 #include <google/protobuf/stubs/stl_util.h>
54 TEST(WireFormatTest, EnumsInSync) {
55 // Verify that WireFormatLite::FieldType and WireFormatLite::CppType match
56 // FieldDescriptor::Type and FieldDescriptor::CppType.
58 EXPECT_EQ(implicit_cast<int>(FieldDescriptor::MAX_TYPE),
59 implicit_cast<int>(WireFormatLite::MAX_FIELD_TYPE));
60 EXPECT_EQ(implicit_cast<int>(FieldDescriptor::MAX_CPPTYPE),
61 implicit_cast<int>(WireFormatLite::MAX_CPPTYPE));
63 for (int i = 1; i <= WireFormatLite::MAX_FIELD_TYPE; i++) {
65 implicit_cast<int>(FieldDescriptor::TypeToCppType(
66 static_cast<FieldDescriptor::Type>(i))),
67 implicit_cast<int>(WireFormatLite::FieldTypeToCppType(
68 static_cast<WireFormatLite::FieldType>(i))));
72 TEST(WireFormatTest, MaxFieldNumber) {
73 // Make sure the max field number constant is accurate.
74 EXPECT_EQ((1 << (32 - WireFormatLite::kTagTypeBits)) - 1,
75 FieldDescriptor::kMaxNumber);
78 TEST(WireFormatTest, Parse) {
79 unittest::TestAllTypes source, dest;
82 // Serialize using the generated code.
83 TestUtil::SetAllFields(&source);
84 source.SerializeToString(&data);
86 // Parse using WireFormat.
87 io::ArrayInputStream raw_input(data.data(), data.size());
88 io::CodedInputStream input(&raw_input);
89 WireFormat::ParseAndMergePartial(&input, &dest);
92 TestUtil::ExpectAllFieldsSet(dest);
95 TEST(WireFormatTest, ParseExtensions) {
96 unittest::TestAllExtensions source, dest;
99 // Serialize using the generated code.
100 TestUtil::SetAllExtensions(&source);
101 source.SerializeToString(&data);
103 // Parse using WireFormat.
104 io::ArrayInputStream raw_input(data.data(), data.size());
105 io::CodedInputStream input(&raw_input);
106 WireFormat::ParseAndMergePartial(&input, &dest);
109 TestUtil::ExpectAllExtensionsSet(dest);
112 TEST(WireFormatTest, ParsePacked) {
113 unittest::TestPackedTypes source, dest;
116 // Serialize using the generated code.
117 TestUtil::SetPackedFields(&source);
118 source.SerializeToString(&data);
120 // Parse using WireFormat.
121 io::ArrayInputStream raw_input(data.data(), data.size());
122 io::CodedInputStream input(&raw_input);
123 WireFormat::ParseAndMergePartial(&input, &dest);
126 TestUtil::ExpectPackedFieldsSet(dest);
129 TEST(WireFormatTest, ParsePackedFromUnpacked) {
130 // Serialize using the generated code.
131 unittest::TestUnpackedTypes source;
132 TestUtil::SetUnpackedFields(&source);
133 string data = source.SerializeAsString();
135 // Parse using WireFormat.
136 unittest::TestPackedTypes dest;
137 io::ArrayInputStream raw_input(data.data(), data.size());
138 io::CodedInputStream input(&raw_input);
139 WireFormat::ParseAndMergePartial(&input, &dest);
142 TestUtil::ExpectPackedFieldsSet(dest);
145 TEST(WireFormatTest, ParseUnpackedFromPacked) {
146 // Serialize using the generated code.
147 unittest::TestPackedTypes source;
148 TestUtil::SetPackedFields(&source);
149 string data = source.SerializeAsString();
151 // Parse using WireFormat.
152 unittest::TestUnpackedTypes dest;
153 io::ArrayInputStream raw_input(data.data(), data.size());
154 io::CodedInputStream input(&raw_input);
155 WireFormat::ParseAndMergePartial(&input, &dest);
158 TestUtil::ExpectUnpackedFieldsSet(dest);
161 TEST(WireFormatTest, ParsePackedExtensions) {
162 unittest::TestPackedExtensions source, dest;
165 // Serialize using the generated code.
166 TestUtil::SetPackedExtensions(&source);
167 source.SerializeToString(&data);
169 // Parse using WireFormat.
170 io::ArrayInputStream raw_input(data.data(), data.size());
171 io::CodedInputStream input(&raw_input);
172 WireFormat::ParseAndMergePartial(&input, &dest);
175 TestUtil::ExpectPackedExtensionsSet(dest);
178 TEST(WireFormatTest, ParseOneof) {
179 unittest::TestOneof2 source, dest;
182 // Serialize using the generated code.
183 TestUtil::SetOneof1(&source);
184 source.SerializeToString(&data);
186 // Parse using WireFormat.
187 io::ArrayInputStream raw_input(data.data(), data.size());
188 io::CodedInputStream input(&raw_input);
189 WireFormat::ParseAndMergePartial(&input, &dest);
192 TestUtil::ExpectOneofSet1(dest);
195 TEST(WireFormatTest, OneofOnlySetLast) {
196 unittest::TestOneofBackwardsCompatible source;
197 unittest::TestOneof oneof_dest;
201 source.set_foo_int(100);
202 source.set_foo_string("101");
204 // Serialize and parse to oneof message.
205 source.SerializeToString(&data);
206 io::ArrayInputStream raw_input(data.data(), data.size());
207 io::CodedInputStream input(&raw_input);
208 WireFormat::ParseAndMergePartial(&input, &oneof_dest);
210 // Only the last field is set.
211 EXPECT_FALSE(oneof_dest.has_foo_int());
212 EXPECT_TRUE(oneof_dest.has_foo_string());
215 TEST(WireFormatTest, ByteSize) {
216 unittest::TestAllTypes message;
217 TestUtil::SetAllFields(&message);
219 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
221 EXPECT_EQ(0, message.ByteSize());
222 EXPECT_EQ(0, WireFormat::ByteSize(message));
225 TEST(WireFormatTest, ByteSizeExtensions) {
226 unittest::TestAllExtensions message;
227 TestUtil::SetAllExtensions(&message);
229 EXPECT_EQ(message.ByteSize(),
230 WireFormat::ByteSize(message));
232 EXPECT_EQ(0, message.ByteSize());
233 EXPECT_EQ(0, WireFormat::ByteSize(message));
236 TEST(WireFormatTest, ByteSizePacked) {
237 unittest::TestPackedTypes message;
238 TestUtil::SetPackedFields(&message);
240 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
242 EXPECT_EQ(0, message.ByteSize());
243 EXPECT_EQ(0, WireFormat::ByteSize(message));
246 TEST(WireFormatTest, ByteSizePackedExtensions) {
247 unittest::TestPackedExtensions message;
248 TestUtil::SetPackedExtensions(&message);
250 EXPECT_EQ(message.ByteSize(),
251 WireFormat::ByteSize(message));
253 EXPECT_EQ(0, message.ByteSize());
254 EXPECT_EQ(0, WireFormat::ByteSize(message));
257 TEST(WireFormatTest, ByteSizeOneof) {
258 unittest::TestOneof2 message;
259 TestUtil::SetOneof1(&message);
261 EXPECT_EQ(message.ByteSize(),
262 WireFormat::ByteSize(message));
265 EXPECT_EQ(0, message.ByteSize());
266 EXPECT_EQ(0, WireFormat::ByteSize(message));
269 TEST(WireFormatTest, Serialize) {
270 unittest::TestAllTypes message;
271 string generated_data;
274 TestUtil::SetAllFields(&message);
275 int size = message.ByteSize();
277 // Serialize using the generated code.
279 io::StringOutputStream raw_output(&generated_data);
280 io::CodedOutputStream output(&raw_output);
281 message.SerializeWithCachedSizes(&output);
282 ASSERT_FALSE(output.HadError());
285 // Serialize using WireFormat.
287 io::StringOutputStream raw_output(&dynamic_data);
288 io::CodedOutputStream output(&raw_output);
289 WireFormat::SerializeWithCachedSizes(message, size, &output);
290 ASSERT_FALSE(output.HadError());
293 // Should be the same.
294 // Don't use EXPECT_EQ here because we're comparing raw binary data and
295 // we really don't want it dumped to stdout on failure.
296 EXPECT_TRUE(dynamic_data == generated_data);
299 TEST(WireFormatTest, SerializeExtensions) {
300 unittest::TestAllExtensions message;
301 string generated_data;
304 TestUtil::SetAllExtensions(&message);
305 int size = message.ByteSize();
307 // Serialize using the generated code.
309 io::StringOutputStream raw_output(&generated_data);
310 io::CodedOutputStream output(&raw_output);
311 message.SerializeWithCachedSizes(&output);
312 ASSERT_FALSE(output.HadError());
315 // Serialize using WireFormat.
317 io::StringOutputStream raw_output(&dynamic_data);
318 io::CodedOutputStream output(&raw_output);
319 WireFormat::SerializeWithCachedSizes(message, size, &output);
320 ASSERT_FALSE(output.HadError());
323 // Should be the same.
324 // Don't use EXPECT_EQ here because we're comparing raw binary data and
325 // we really don't want it dumped to stdout on failure.
326 EXPECT_TRUE(dynamic_data == generated_data);
329 TEST(WireFormatTest, SerializeFieldsAndExtensions) {
330 unittest::TestFieldOrderings message;
331 string generated_data;
334 TestUtil::SetAllFieldsAndExtensions(&message);
335 int size = message.ByteSize();
337 // Serialize using the generated code.
339 io::StringOutputStream raw_output(&generated_data);
340 io::CodedOutputStream output(&raw_output);
341 message.SerializeWithCachedSizes(&output);
342 ASSERT_FALSE(output.HadError());
345 // Serialize using WireFormat.
347 io::StringOutputStream raw_output(&dynamic_data);
348 io::CodedOutputStream output(&raw_output);
349 WireFormat::SerializeWithCachedSizes(message, size, &output);
350 ASSERT_FALSE(output.HadError());
353 // Should be the same.
354 // Don't use EXPECT_EQ here because we're comparing raw binary data and
355 // we really don't want it dumped to stdout on failure.
356 EXPECT_TRUE(dynamic_data == generated_data);
358 // Should output in canonical order.
359 TestUtil::ExpectAllFieldsAndExtensionsInOrder(dynamic_data);
360 TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data);
363 TEST(WireFormatTest, SerializeOneof) {
364 unittest::TestOneof2 message;
365 string generated_data;
368 TestUtil::SetOneof1(&message);
369 int size = message.ByteSize();
371 // Serialize using the generated code.
373 io::StringOutputStream raw_output(&generated_data);
374 io::CodedOutputStream output(&raw_output);
375 message.SerializeWithCachedSizes(&output);
376 ASSERT_FALSE(output.HadError());
379 // Serialize using WireFormat.
381 io::StringOutputStream raw_output(&dynamic_data);
382 io::CodedOutputStream output(&raw_output);
383 WireFormat::SerializeWithCachedSizes(message, size, &output);
384 ASSERT_FALSE(output.HadError());
387 // Should be the same.
388 // Don't use EXPECT_EQ here because we're comparing raw binary data and
389 // we really don't want it dumped to stdout on failure.
390 EXPECT_TRUE(dynamic_data == generated_data);
393 TEST(WireFormatTest, ParseMultipleExtensionRanges) {
394 // Make sure we can parse a message that contains multiple extensions ranges.
395 unittest::TestFieldOrderings source;
398 TestUtil::SetAllFieldsAndExtensions(&source);
399 source.SerializeToString(&data);
402 unittest::TestFieldOrderings dest;
403 EXPECT_TRUE(dest.ParseFromString(data));
404 EXPECT_EQ(source.DebugString(), dest.DebugString());
407 // Also test using reflection-based parsing.
409 unittest::TestFieldOrderings dest;
410 io::ArrayInputStream raw_input(data.data(), data.size());
411 io::CodedInputStream coded_input(&raw_input);
412 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest));
413 EXPECT_EQ(source.DebugString(), dest.DebugString());
417 const int kUnknownTypeId = 1550055;
419 TEST(WireFormatTest, SerializeMessageSet) {
420 // Set up a TestMessageSet with two known messages and an unknown one.
421 unittest::TestMessageSet message_set;
422 message_set.MutableExtension(
423 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
424 message_set.MutableExtension(
425 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
426 message_set.mutable_unknown_fields()->AddLengthDelimited(
427 kUnknownTypeId, "bar");
430 ASSERT_TRUE(message_set.SerializeToString(&data));
432 // Parse back using RawMessageSet and check the contents.
433 unittest::RawMessageSet raw;
434 ASSERT_TRUE(raw.ParseFromString(data));
436 EXPECT_EQ(0, raw.unknown_fields().field_count());
438 ASSERT_EQ(3, raw.item_size());
440 unittest::TestMessageSetExtension1::descriptor()->extension(0)->number(),
441 raw.item(0).type_id());
443 unittest::TestMessageSetExtension2::descriptor()->extension(0)->number(),
444 raw.item(1).type_id());
445 EXPECT_EQ(kUnknownTypeId, raw.item(2).type_id());
447 unittest::TestMessageSetExtension1 message1;
448 EXPECT_TRUE(message1.ParseFromString(raw.item(0).message()));
449 EXPECT_EQ(123, message1.i());
451 unittest::TestMessageSetExtension2 message2;
452 EXPECT_TRUE(message2.ParseFromString(raw.item(1).message()));
453 EXPECT_EQ("foo", message2.str());
455 EXPECT_EQ("bar", raw.item(2).message());
458 TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) {
459 // Serialize a MessageSet to a stream and to a flat array using generated
460 // code, and also using WireFormat, and check that the results are equal.
461 // Set up a TestMessageSet with two known messages and an unknown one, as
464 unittest::TestMessageSet message_set;
465 message_set.MutableExtension(
466 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
467 message_set.MutableExtension(
468 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
469 message_set.mutable_unknown_fields()->AddLengthDelimited(
470 kUnknownTypeId, "bar");
472 int size = message_set.ByteSize();
473 EXPECT_EQ(size, message_set.GetCachedSize());
474 ASSERT_EQ(size, WireFormat::ByteSize(message_set));
479 flat_data.resize(size);
480 stream_data.resize(size);
482 // Serialize to flat array
484 uint8* target = reinterpret_cast<uint8*>(string_as_array(&flat_data));
485 uint8* end = message_set.SerializeWithCachedSizesToArray(target);
486 EXPECT_EQ(size, end - target);
489 // Serialize to buffer
491 io::ArrayOutputStream array_stream(string_as_array(&stream_data), size, 1);
492 io::CodedOutputStream output_stream(&array_stream);
493 message_set.SerializeWithCachedSizes(&output_stream);
494 ASSERT_FALSE(output_stream.HadError());
497 // Serialize to buffer with WireFormat.
499 io::StringOutputStream string_stream(&dynamic_data);
500 io::CodedOutputStream output_stream(&string_stream);
501 WireFormat::SerializeWithCachedSizes(message_set, size, &output_stream);
502 ASSERT_FALSE(output_stream.HadError());
505 EXPECT_TRUE(flat_data == stream_data);
506 EXPECT_TRUE(flat_data == dynamic_data);
509 TEST(WireFormatTest, ParseMessageSet) {
510 // Set up a RawMessageSet with two known messages and an unknown one.
511 unittest::RawMessageSet raw;
514 unittest::RawMessageSet::Item* item = raw.add_item();
516 unittest::TestMessageSetExtension1::descriptor()->extension(0)->number());
517 unittest::TestMessageSetExtension1 message;
519 message.SerializeToString(item->mutable_message());
523 unittest::RawMessageSet::Item* item = raw.add_item();
525 unittest::TestMessageSetExtension2::descriptor()->extension(0)->number());
526 unittest::TestMessageSetExtension2 message;
527 message.set_str("foo");
528 message.SerializeToString(item->mutable_message());
532 unittest::RawMessageSet::Item* item = raw.add_item();
533 item->set_type_id(kUnknownTypeId);
534 item->set_message("bar");
538 ASSERT_TRUE(raw.SerializeToString(&data));
540 // Parse as a TestMessageSet and check the contents.
541 unittest::TestMessageSet message_set;
542 ASSERT_TRUE(message_set.ParseFromString(data));
544 EXPECT_EQ(123, message_set.GetExtension(
545 unittest::TestMessageSetExtension1::message_set_extension).i());
546 EXPECT_EQ("foo", message_set.GetExtension(
547 unittest::TestMessageSetExtension2::message_set_extension).str());
549 ASSERT_EQ(1, message_set.unknown_fields().field_count());
550 ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED,
551 message_set.unknown_fields().field(0).type());
552 EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited());
554 // Also parse using WireFormat.
555 unittest::TestMessageSet dynamic_message_set;
556 io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()),
558 ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set));
559 EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString());
562 TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) {
565 unittest::TestMessageSetExtension1 message;
567 // Build a MessageSet manually with its message content put before its
569 io::StringOutputStream output_stream(&data);
570 io::CodedOutputStream coded_output(&output_stream);
571 coded_output.WriteTag(WireFormatLite::kMessageSetItemStartTag);
572 // Write the message content first.
573 WireFormatLite::WriteTag(WireFormatLite::kMessageSetMessageNumber,
574 WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
576 coded_output.WriteVarint32(message.ByteSize());
577 message.SerializeWithCachedSizes(&coded_output);
578 // Write the type id.
579 uint32 type_id = message.GetDescriptor()->extension(0)->number();
580 WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber,
581 type_id, &coded_output);
582 coded_output.WriteTag(WireFormatLite::kMessageSetItemEndTag);
585 unittest::TestMessageSet message_set;
586 ASSERT_TRUE(message_set.ParseFromString(data));
588 EXPECT_EQ(123, message_set.GetExtension(
589 unittest::TestMessageSetExtension1::message_set_extension).i());
592 // Test parse the message via Reflection.
593 unittest::TestMessageSet message_set;
594 io::CodedInputStream input(
595 reinterpret_cast<const uint8*>(data.data()), data.size());
596 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set));
597 EXPECT_TRUE(input.ConsumedEntireMessage());
599 EXPECT_EQ(123, message_set.GetExtension(
600 unittest::TestMessageSetExtension1::message_set_extension).i());
604 TEST(WireFormatTest, ParseBrokenMessageSet) {
605 unittest::TestMessageSet message_set;
606 string input("goodbye"); // Invalid wire format data.
607 EXPECT_FALSE(message_set.ParseFromString(input));
610 TEST(WireFormatTest, RecursionLimit) {
611 unittest::TestRecursiveMessage message;
612 message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1);
614 message.SerializeToString(&data);
617 io::ArrayInputStream raw_input(data.data(), data.size());
618 io::CodedInputStream input(&raw_input);
619 input.SetRecursionLimit(4);
620 unittest::TestRecursiveMessage message2;
621 EXPECT_TRUE(message2.ParseFromCodedStream(&input));
625 io::ArrayInputStream raw_input(data.data(), data.size());
626 io::CodedInputStream input(&raw_input);
627 input.SetRecursionLimit(3);
628 unittest::TestRecursiveMessage message2;
629 EXPECT_FALSE(message2.ParseFromCodedStream(&input));
633 TEST(WireFormatTest, UnknownFieldRecursionLimit) {
634 unittest::TestEmptyMessage message;
635 message.mutable_unknown_fields()
640 ->AddVarint(1234, 123);
642 message.SerializeToString(&data);
645 io::ArrayInputStream raw_input(data.data(), data.size());
646 io::CodedInputStream input(&raw_input);
647 input.SetRecursionLimit(4);
648 unittest::TestEmptyMessage message2;
649 EXPECT_TRUE(message2.ParseFromCodedStream(&input));
653 io::ArrayInputStream raw_input(data.data(), data.size());
654 io::CodedInputStream input(&raw_input);
655 input.SetRecursionLimit(3);
656 unittest::TestEmptyMessage message2;
657 EXPECT_FALSE(message2.ParseFromCodedStream(&input));
661 TEST(WireFormatTest, ZigZag) {
662 // avoid line-wrapping
663 #define LL(x) GOOGLE_LONGLONG(x)
664 #define ULL(x) GOOGLE_ULONGLONG(x)
665 #define ZigZagEncode32(x) WireFormatLite::ZigZagEncode32(x)
666 #define ZigZagDecode32(x) WireFormatLite::ZigZagDecode32(x)
667 #define ZigZagEncode64(x) WireFormatLite::ZigZagEncode64(x)
668 #define ZigZagDecode64(x) WireFormatLite::ZigZagDecode64(x)
670 EXPECT_EQ(0u, ZigZagEncode32( 0));
671 EXPECT_EQ(1u, ZigZagEncode32(-1));
672 EXPECT_EQ(2u, ZigZagEncode32( 1));
673 EXPECT_EQ(3u, ZigZagEncode32(-2));
674 EXPECT_EQ(0x7FFFFFFEu, ZigZagEncode32(0x3FFFFFFF));
675 EXPECT_EQ(0x7FFFFFFFu, ZigZagEncode32(0xC0000000));
676 EXPECT_EQ(0xFFFFFFFEu, ZigZagEncode32(0x7FFFFFFF));
677 EXPECT_EQ(0xFFFFFFFFu, ZigZagEncode32(0x80000000));
679 EXPECT_EQ( 0, ZigZagDecode32(0u));
680 EXPECT_EQ(-1, ZigZagDecode32(1u));
681 EXPECT_EQ( 1, ZigZagDecode32(2u));
682 EXPECT_EQ(-2, ZigZagDecode32(3u));
683 EXPECT_EQ(0x3FFFFFFF, ZigZagDecode32(0x7FFFFFFEu));
684 EXPECT_EQ(0xC0000000, ZigZagDecode32(0x7FFFFFFFu));
685 EXPECT_EQ(0x7FFFFFFF, ZigZagDecode32(0xFFFFFFFEu));
686 EXPECT_EQ(0x80000000, ZigZagDecode32(0xFFFFFFFFu));
688 EXPECT_EQ(0u, ZigZagEncode64( 0));
689 EXPECT_EQ(1u, ZigZagEncode64(-1));
690 EXPECT_EQ(2u, ZigZagEncode64( 1));
691 EXPECT_EQ(3u, ZigZagEncode64(-2));
692 EXPECT_EQ(ULL(0x000000007FFFFFFE), ZigZagEncode64(LL(0x000000003FFFFFFF)));
693 EXPECT_EQ(ULL(0x000000007FFFFFFF), ZigZagEncode64(LL(0xFFFFFFFFC0000000)));
694 EXPECT_EQ(ULL(0x00000000FFFFFFFE), ZigZagEncode64(LL(0x000000007FFFFFFF)));
695 EXPECT_EQ(ULL(0x00000000FFFFFFFF), ZigZagEncode64(LL(0xFFFFFFFF80000000)));
696 EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFE), ZigZagEncode64(LL(0x7FFFFFFFFFFFFFFF)));
697 EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFF), ZigZagEncode64(LL(0x8000000000000000)));
699 EXPECT_EQ( 0, ZigZagDecode64(0u));
700 EXPECT_EQ(-1, ZigZagDecode64(1u));
701 EXPECT_EQ( 1, ZigZagDecode64(2u));
702 EXPECT_EQ(-2, ZigZagDecode64(3u));
703 EXPECT_EQ(LL(0x000000003FFFFFFF), ZigZagDecode64(ULL(0x000000007FFFFFFE)));
704 EXPECT_EQ(LL(0xFFFFFFFFC0000000), ZigZagDecode64(ULL(0x000000007FFFFFFF)));
705 EXPECT_EQ(LL(0x000000007FFFFFFF), ZigZagDecode64(ULL(0x00000000FFFFFFFE)));
706 EXPECT_EQ(LL(0xFFFFFFFF80000000), ZigZagDecode64(ULL(0x00000000FFFFFFFF)));
707 EXPECT_EQ(LL(0x7FFFFFFFFFFFFFFF), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFE)));
708 EXPECT_EQ(LL(0x8000000000000000), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFF)));
710 // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1)
711 // were chosen semi-randomly via keyboard bashing.
712 EXPECT_EQ( 0, ZigZagDecode32(ZigZagEncode32( 0)));
713 EXPECT_EQ( 1, ZigZagDecode32(ZigZagEncode32( 1)));
714 EXPECT_EQ( -1, ZigZagDecode32(ZigZagEncode32( -1)));
715 EXPECT_EQ(14927, ZigZagDecode32(ZigZagEncode32(14927)));
716 EXPECT_EQ(-3612, ZigZagDecode32(ZigZagEncode32(-3612)));
718 EXPECT_EQ( 0, ZigZagDecode64(ZigZagEncode64( 0)));
719 EXPECT_EQ( 1, ZigZagDecode64(ZigZagEncode64( 1)));
720 EXPECT_EQ( -1, ZigZagDecode64(ZigZagEncode64( -1)));
721 EXPECT_EQ(14927, ZigZagDecode64(ZigZagEncode64(14927)));
722 EXPECT_EQ(-3612, ZigZagDecode64(ZigZagEncode64(-3612)));
724 EXPECT_EQ(LL(856912304801416), ZigZagDecode64(ZigZagEncode64(
725 LL(856912304801416))));
726 EXPECT_EQ(LL(-75123905439571256), ZigZagDecode64(ZigZagEncode64(
727 LL(-75123905439571256))));
730 TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) {
731 // At one point checks would trigger when parsing repeated fixed scalar
733 protobuf_unittest::TestRepeatedScalarDifferentTagSizes msg1, msg2;
734 for (int i = 0; i < 100; ++i) {
735 msg1.add_repeated_fixed32(i);
736 msg1.add_repeated_int32(i);
737 msg1.add_repeated_fixed64(i);
738 msg1.add_repeated_int64(i);
739 msg1.add_repeated_float(i);
740 msg1.add_repeated_uint64(i);
743 // Make sure that we have a variety of tag sizes.
744 const google::protobuf::Descriptor* desc = msg1.GetDescriptor();
745 const google::protobuf::FieldDescriptor* field;
746 field = desc->FindFieldByName("repeated_fixed32");
747 ASSERT_TRUE(field != NULL);
748 ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
749 field = desc->FindFieldByName("repeated_int32");
750 ASSERT_TRUE(field != NULL);
751 ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
752 field = desc->FindFieldByName("repeated_fixed64");
753 ASSERT_TRUE(field != NULL);
754 ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
755 field = desc->FindFieldByName("repeated_int64");
756 ASSERT_TRUE(field != NULL);
757 ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
758 field = desc->FindFieldByName("repeated_float");
759 ASSERT_TRUE(field != NULL);
760 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
761 field = desc->FindFieldByName("repeated_uint64");
762 ASSERT_TRUE(field != NULL);
763 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
765 EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString()));
766 EXPECT_EQ(msg1.DebugString(), msg2.DebugString());
769 TEST(WireFormatTest, CompatibleTypes) {
770 const int64 data = 0x100000000;
771 unittest::Int64Message msg1;
774 msg1.SerializeToString(&serialized);
776 // Test int64 is compatible with bool
777 unittest::BoolMessage msg2;
778 ASSERT_TRUE(msg2.ParseFromString(serialized));
779 ASSERT_EQ(static_cast<bool>(data), msg2.data());
781 // Test int64 is compatible with uint64
782 unittest::Uint64Message msg3;
783 ASSERT_TRUE(msg3.ParseFromString(serialized));
784 ASSERT_EQ(static_cast<uint64>(data), msg3.data());
786 // Test int64 is compatible with int32
787 unittest::Int32Message msg4;
788 ASSERT_TRUE(msg4.ParseFromString(serialized));
789 ASSERT_EQ(static_cast<int32>(data), msg4.data());
791 // Test int64 is compatible with uint32
792 unittest::Uint32Message msg5;
793 ASSERT_TRUE(msg5.ParseFromString(serialized));
794 ASSERT_EQ(static_cast<uint32>(data), msg5.data());
797 class WireFormatInvalidInputTest : public testing::Test {
799 // Make a serialized TestAllTypes in which the field optional_nested_message
800 // contains exactly the given bytes, which may be invalid.
801 string MakeInvalidEmbeddedMessage(const char* bytes, int size) {
802 const FieldDescriptor* field =
803 unittest::TestAllTypes::descriptor()->FindFieldByName(
804 "optional_nested_message");
805 GOOGLE_CHECK(field != NULL);
810 io::StringOutputStream raw_output(&result);
811 io::CodedOutputStream output(&raw_output);
813 WireFormatLite::WriteBytes(field->number(), string(bytes, size), &output);
819 // Make a serialized TestAllTypes in which the field optionalgroup
820 // contains exactly the given bytes -- which may be invalid -- and
821 // possibly no end tag.
822 string MakeInvalidGroup(const char* bytes, int size, bool include_end_tag) {
823 const FieldDescriptor* field =
824 unittest::TestAllTypes::descriptor()->FindFieldByName(
826 GOOGLE_CHECK(field != NULL);
831 io::StringOutputStream raw_output(&result);
832 io::CodedOutputStream output(&raw_output);
834 output.WriteVarint32(WireFormat::MakeTag(field));
835 output.WriteString(string(bytes, size));
836 if (include_end_tag) {
837 output.WriteVarint32(WireFormatLite::MakeTag(
838 field->number(), WireFormatLite::WIRETYPE_END_GROUP));
846 TEST_F(WireFormatInvalidInputTest, InvalidSubMessage) {
847 unittest::TestAllTypes message;
850 EXPECT_TRUE(message.ParseFromString(MakeInvalidEmbeddedMessage("", 0)));
852 // The byte is a valid varint, but not a valid tag (zero).
853 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\0", 1)));
855 // The byte is a malformed varint.
856 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\200", 1)));
858 // The byte is an endgroup tag, but we aren't parsing a group.
859 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\014", 1)));
861 // The byte is a valid varint but not a valid tag (bad wire type).
862 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\017", 1)));
865 TEST_F(WireFormatInvalidInputTest, InvalidGroup) {
866 unittest::TestAllTypes message;
869 EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
871 // Missing end tag. Groups cannot end at EOF.
872 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
874 // The byte is a valid varint, but not a valid tag (zero).
875 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
877 // The byte is a malformed varint.
878 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
880 // The byte is an endgroup tag, but not the right one for this group.
881 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
883 // The byte is a valid varint but not a valid tag (bad wire type).
884 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
887 TEST_F(WireFormatInvalidInputTest, InvalidUnknownGroup) {
888 // Use TestEmptyMessage so that the group made by MakeInvalidGroup will not
889 // be a known tag number.
890 unittest::TestEmptyMessage message;
893 EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
895 // Missing end tag. Groups cannot end at EOF.
896 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
898 // The byte is a valid varint, but not a valid tag (zero).
899 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
901 // The byte is a malformed varint.
902 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
904 // The byte is an endgroup tag, but not the right one for this group.
905 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
907 // The byte is a valid varint but not a valid tag (bad wire type).
908 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
911 TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) {
912 // Test a bug fix: SkipMessage should fail if the message contains a string
913 // whose length would extend beyond the message end.
915 unittest::TestAllTypes message;
916 message.set_optional_string("foo foo foo foo");
918 message.SerializeToString(&data);
920 // Chop some bytes off the end.
921 data.resize(data.size() - 4);
923 // Try to skip it. Note that the bug was only present when parsing to an
925 io::ArrayInputStream raw_input(data.data(), data.size());
926 io::CodedInputStream coded_input(&raw_input);
927 UnknownFieldSet unknown_fields;
928 EXPECT_FALSE(WireFormat::SkipMessage(&coded_input, &unknown_fields));
931 // Test differences between string and bytes.
932 // Value of a string type must be valid UTF-8 string. When UTF-8
933 // validation is enabled (GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED):
934 // WriteInvalidUTF8String: see error message.
935 // ReadInvalidUTF8String: see error message.
936 // WriteValidUTF8String: fine.
937 // ReadValidUTF8String: fine.
938 // WriteAnyBytes: fine.
939 // ReadAnyBytes: fine.
940 const char * kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0";
941 // This used to be "Valid UTF-8: \x01\x02\u8C37\u6B4C", but MSVC seems to
942 // interpret \u differently from GCC.
943 const char * kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214";
946 bool WriteMessage(const char *value, T *message, string *wire_buffer) {
947 message->set_data(value);
948 wire_buffer->clear();
949 message->AppendToString(wire_buffer);
950 return (wire_buffer->size() > 0);
954 bool ReadMessage(const string &wire_buffer, T *message) {
955 return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
958 bool StartsWith(const string& s, const string& prefix) {
959 return s.substr(0, prefix.length()) == prefix;
962 class Utf8ValidationTest : public ::testing::Test {
964 Utf8ValidationTest() {}
965 virtual ~Utf8ValidationTest() {}
966 virtual void SetUp() {
971 TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) {
973 protobuf_unittest::OneString input;
974 vector<string> errors;
977 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
978 errors = log.GetMessages(ERROR);
980 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
981 ASSERT_EQ(1, errors.size());
982 EXPECT_TRUE(StartsWith(errors[0],
983 "String field 'data' contains invalid UTF-8 data when "
984 "serializing a protocol buffer. Use the "
985 "'bytes' type if you intend to send raw bytes."));
987 ASSERT_EQ(0, errors.size());
988 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
992 TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) {
994 protobuf_unittest::OneString input;
995 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
996 protobuf_unittest::OneString output;
997 vector<string> errors;
1000 ReadMessage(wire_buffer, &output);
1001 errors = log.GetMessages(ERROR);
1003 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1004 ASSERT_EQ(1, errors.size());
1005 EXPECT_TRUE(StartsWith(errors[0],
1006 "String field 'data' contains invalid UTF-8 data when "
1007 "parsing a protocol buffer. Use the "
1008 "'bytes' type if you intend to send raw bytes."));
1011 ASSERT_EQ(0, errors.size());
1012 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1016 TEST_F(Utf8ValidationTest, WriteValidUTF8String) {
1018 protobuf_unittest::OneString input;
1019 vector<string> errors;
1021 ScopedMemoryLog log;
1022 WriteMessage(kValidUTF8String, &input, &wire_buffer);
1023 errors = log.GetMessages(ERROR);
1025 ASSERT_EQ(0, errors.size());
1028 TEST_F(Utf8ValidationTest, ReadValidUTF8String) {
1030 protobuf_unittest::OneString input;
1031 WriteMessage(kValidUTF8String, &input, &wire_buffer);
1032 protobuf_unittest::OneString output;
1033 vector<string> errors;
1035 ScopedMemoryLog log;
1036 ReadMessage(wire_buffer, &output);
1037 errors = log.GetMessages(ERROR);
1039 ASSERT_EQ(0, errors.size());
1040 EXPECT_EQ(input.data(), output.data());
1043 // Bytes: anything can pass as bytes, use invalid UTF-8 string to test
1044 TEST_F(Utf8ValidationTest, WriteArbitraryBytes) {
1046 protobuf_unittest::OneBytes input;
1047 vector<string> errors;
1049 ScopedMemoryLog log;
1050 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1051 errors = log.GetMessages(ERROR);
1053 ASSERT_EQ(0, errors.size());
1056 TEST_F(Utf8ValidationTest, ReadArbitraryBytes) {
1058 protobuf_unittest::OneBytes input;
1059 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1060 protobuf_unittest::OneBytes output;
1061 vector<string> errors;
1063 ScopedMemoryLog log;
1064 ReadMessage(wire_buffer, &output);
1065 errors = log.GetMessages(ERROR);
1067 ASSERT_EQ(0, errors.size());
1068 EXPECT_EQ(input.data(), output.data());
1071 TEST_F(Utf8ValidationTest, ParseRepeatedString) {
1072 protobuf_unittest::MoreBytes input;
1073 input.add_data(kValidUTF8String);
1074 input.add_data(kInvalidUTF8String);
1075 input.add_data(kInvalidUTF8String);
1076 string wire_buffer = input.SerializeAsString();
1078 protobuf_unittest::MoreString output;
1079 vector<string> errors;
1081 ScopedMemoryLog log;
1082 ReadMessage(wire_buffer, &output);
1083 errors = log.GetMessages(ERROR);
1085 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1086 ASSERT_EQ(2, errors.size());
1088 ASSERT_EQ(0, errors.size());
1089 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1090 EXPECT_EQ(wire_buffer, output.SerializeAsString());
1093 // Test the old VerifyUTF8String() function, which may still be called by old
1095 TEST_F(Utf8ValidationTest, OldVerifyUTF8String) {
1096 string data(kInvalidUTF8String);
1098 vector<string> errors;
1100 ScopedMemoryLog log;
1101 WireFormat::VerifyUTF8String(data.data(), data.size(),
1102 WireFormat::SERIALIZE);
1103 errors = log.GetMessages(ERROR);
1105 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1106 ASSERT_EQ(1, errors.size());
1107 EXPECT_TRUE(StartsWith(errors[0],
1108 "String field contains invalid UTF-8 data when "
1109 "serializing a protocol buffer. Use the "
1110 "'bytes' type if you intend to send raw bytes."));
1112 ASSERT_EQ(0, errors.size());
1118 } // namespace internal
1119 } // namespace protobuf
1120 } // namespace google