1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// Author: kenton@google.com (Kenton Varda)
32//  Based on original Protocol Buffers design by
33//  Sanjay Ghemawat, Jeff Dean, and others.
34
35#include <google/protobuf/wire_format.h>
36#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>
43
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>
48
49namespace google {
50namespace protobuf {
51namespace internal {
52namespace {
53
54TEST(WireFormatTest, EnumsInSync) {
55  // Verify that WireFormatLite::FieldType and WireFormatLite::CppType match
56  // FieldDescriptor::Type and FieldDescriptor::CppType.
57
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));
62
63  for (int i = 1; i <= WireFormatLite::MAX_FIELD_TYPE; i++) {
64    EXPECT_EQ(
65      implicit_cast<int>(FieldDescriptor::TypeToCppType(
66        static_cast<FieldDescriptor::Type>(i))),
67      implicit_cast<int>(WireFormatLite::FieldTypeToCppType(
68        static_cast<WireFormatLite::FieldType>(i))));
69  }
70}
71
72TEST(WireFormatTest, MaxFieldNumber) {
73  // Make sure the max field number constant is accurate.
74  EXPECT_EQ((1 << (32 - WireFormatLite::kTagTypeBits)) - 1,
75            FieldDescriptor::kMaxNumber);
76}
77
78TEST(WireFormatTest, Parse) {
79  unittest::TestAllTypes source, dest;
80  string data;
81
82  // Serialize using the generated code.
83  TestUtil::SetAllFields(&source);
84  source.SerializeToString(&data);
85
86  // Parse using WireFormat.
87  io::ArrayInputStream raw_input(data.data(), data.size());
88  io::CodedInputStream input(&raw_input);
89  WireFormat::ParseAndMergePartial(&input, &dest);
90
91  // Check.
92  TestUtil::ExpectAllFieldsSet(dest);
93}
94
95TEST(WireFormatTest, ParseExtensions) {
96  unittest::TestAllExtensions source, dest;
97  string data;
98
99  // Serialize using the generated code.
100  TestUtil::SetAllExtensions(&source);
101  source.SerializeToString(&data);
102
103  // Parse using WireFormat.
104  io::ArrayInputStream raw_input(data.data(), data.size());
105  io::CodedInputStream input(&raw_input);
106  WireFormat::ParseAndMergePartial(&input, &dest);
107
108  // Check.
109  TestUtil::ExpectAllExtensionsSet(dest);
110}
111
112TEST(WireFormatTest, ParsePacked) {
113  unittest::TestPackedTypes source, dest;
114  string data;
115
116  // Serialize using the generated code.
117  TestUtil::SetPackedFields(&source);
118  source.SerializeToString(&data);
119
120  // Parse using WireFormat.
121  io::ArrayInputStream raw_input(data.data(), data.size());
122  io::CodedInputStream input(&raw_input);
123  WireFormat::ParseAndMergePartial(&input, &dest);
124
125  // Check.
126  TestUtil::ExpectPackedFieldsSet(dest);
127}
128
129TEST(WireFormatTest, ParsePackedFromUnpacked) {
130  // Serialize using the generated code.
131  unittest::TestUnpackedTypes source;
132  TestUtil::SetUnpackedFields(&source);
133  string data = source.SerializeAsString();
134
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);
140
141  // Check.
142  TestUtil::ExpectPackedFieldsSet(dest);
143}
144
145TEST(WireFormatTest, ParseUnpackedFromPacked) {
146  // Serialize using the generated code.
147  unittest::TestPackedTypes source;
148  TestUtil::SetPackedFields(&source);
149  string data = source.SerializeAsString();
150
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);
156
157  // Check.
158  TestUtil::ExpectUnpackedFieldsSet(dest);
159}
160
161TEST(WireFormatTest, ParsePackedExtensions) {
162  unittest::TestPackedExtensions source, dest;
163  string data;
164
165  // Serialize using the generated code.
166  TestUtil::SetPackedExtensions(&source);
167  source.SerializeToString(&data);
168
169  // Parse using WireFormat.
170  io::ArrayInputStream raw_input(data.data(), data.size());
171  io::CodedInputStream input(&raw_input);
172  WireFormat::ParseAndMergePartial(&input, &dest);
173
174  // Check.
175  TestUtil::ExpectPackedExtensionsSet(dest);
176}
177
178TEST(WireFormatTest, ByteSize) {
179  unittest::TestAllTypes message;
180  TestUtil::SetAllFields(&message);
181
182  EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
183  message.Clear();
184  EXPECT_EQ(0, message.ByteSize());
185  EXPECT_EQ(0, WireFormat::ByteSize(message));
186}
187
188TEST(WireFormatTest, ByteSizeExtensions) {
189  unittest::TestAllExtensions message;
190  TestUtil::SetAllExtensions(&message);
191
192  EXPECT_EQ(message.ByteSize(),
193            WireFormat::ByteSize(message));
194  message.Clear();
195  EXPECT_EQ(0, message.ByteSize());
196  EXPECT_EQ(0, WireFormat::ByteSize(message));
197}
198
199TEST(WireFormatTest, ByteSizePacked) {
200  unittest::TestPackedTypes message;
201  TestUtil::SetPackedFields(&message);
202
203  EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
204  message.Clear();
205  EXPECT_EQ(0, message.ByteSize());
206  EXPECT_EQ(0, WireFormat::ByteSize(message));
207}
208
209TEST(WireFormatTest, ByteSizePackedExtensions) {
210  unittest::TestPackedExtensions message;
211  TestUtil::SetPackedExtensions(&message);
212
213  EXPECT_EQ(message.ByteSize(),
214            WireFormat::ByteSize(message));
215  message.Clear();
216  EXPECT_EQ(0, message.ByteSize());
217  EXPECT_EQ(0, WireFormat::ByteSize(message));
218}
219
220TEST(WireFormatTest, Serialize) {
221  unittest::TestAllTypes message;
222  string generated_data;
223  string dynamic_data;
224
225  TestUtil::SetAllFields(&message);
226  int size = message.ByteSize();
227
228  // Serialize using the generated code.
229  {
230    io::StringOutputStream raw_output(&generated_data);
231    io::CodedOutputStream output(&raw_output);
232    message.SerializeWithCachedSizes(&output);
233    ASSERT_FALSE(output.HadError());
234  }
235
236  // Serialize using WireFormat.
237  {
238    io::StringOutputStream raw_output(&dynamic_data);
239    io::CodedOutputStream output(&raw_output);
240    WireFormat::SerializeWithCachedSizes(message, size, &output);
241    ASSERT_FALSE(output.HadError());
242  }
243
244  // Should be the same.
245  // Don't use EXPECT_EQ here because we're comparing raw binary data and
246  // we really don't want it dumped to stdout on failure.
247  EXPECT_TRUE(dynamic_data == generated_data);
248}
249
250TEST(WireFormatTest, SerializeExtensions) {
251  unittest::TestAllExtensions message;
252  string generated_data;
253  string dynamic_data;
254
255  TestUtil::SetAllExtensions(&message);
256  int size = message.ByteSize();
257
258  // Serialize using the generated code.
259  {
260    io::StringOutputStream raw_output(&generated_data);
261    io::CodedOutputStream output(&raw_output);
262    message.SerializeWithCachedSizes(&output);
263    ASSERT_FALSE(output.HadError());
264  }
265
266  // Serialize using WireFormat.
267  {
268    io::StringOutputStream raw_output(&dynamic_data);
269    io::CodedOutputStream output(&raw_output);
270    WireFormat::SerializeWithCachedSizes(message, size, &output);
271    ASSERT_FALSE(output.HadError());
272  }
273
274  // Should be the same.
275  // Don't use EXPECT_EQ here because we're comparing raw binary data and
276  // we really don't want it dumped to stdout on failure.
277  EXPECT_TRUE(dynamic_data == generated_data);
278}
279
280TEST(WireFormatTest, SerializeFieldsAndExtensions) {
281  unittest::TestFieldOrderings message;
282  string generated_data;
283  string dynamic_data;
284
285  TestUtil::SetAllFieldsAndExtensions(&message);
286  int size = message.ByteSize();
287
288  // Serialize using the generated code.
289  {
290    io::StringOutputStream raw_output(&generated_data);
291    io::CodedOutputStream output(&raw_output);
292    message.SerializeWithCachedSizes(&output);
293    ASSERT_FALSE(output.HadError());
294  }
295
296  // Serialize using WireFormat.
297  {
298    io::StringOutputStream raw_output(&dynamic_data);
299    io::CodedOutputStream output(&raw_output);
300    WireFormat::SerializeWithCachedSizes(message, size, &output);
301    ASSERT_FALSE(output.HadError());
302  }
303
304  // Should be the same.
305  // Don't use EXPECT_EQ here because we're comparing raw binary data and
306  // we really don't want it dumped to stdout on failure.
307  EXPECT_TRUE(dynamic_data == generated_data);
308
309  // Should output in canonical order.
310  TestUtil::ExpectAllFieldsAndExtensionsInOrder(dynamic_data);
311  TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data);
312}
313
314TEST(WireFormatTest, ParseMultipleExtensionRanges) {
315  // Make sure we can parse a message that contains multiple extensions ranges.
316  unittest::TestFieldOrderings source;
317  string data;
318
319  TestUtil::SetAllFieldsAndExtensions(&source);
320  source.SerializeToString(&data);
321
322  {
323    unittest::TestFieldOrderings dest;
324    EXPECT_TRUE(dest.ParseFromString(data));
325    EXPECT_EQ(source.DebugString(), dest.DebugString());
326  }
327
328  // Also test using reflection-based parsing.
329  {
330    unittest::TestFieldOrderings dest;
331    io::ArrayInputStream raw_input(data.data(), data.size());
332    io::CodedInputStream coded_input(&raw_input);
333    EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest));
334    EXPECT_EQ(source.DebugString(), dest.DebugString());
335  }
336}
337
338const int kUnknownTypeId = 1550055;
339
340TEST(WireFormatTest, SerializeMessageSet) {
341  // Set up a TestMessageSet with two known messages and an unknown one.
342  unittest::TestMessageSet message_set;
343  message_set.MutableExtension(
344    unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
345  message_set.MutableExtension(
346    unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
347  message_set.mutable_unknown_fields()->AddLengthDelimited(
348    kUnknownTypeId, "bar");
349
350  string data;
351  ASSERT_TRUE(message_set.SerializeToString(&data));
352
353  // Parse back using RawMessageSet and check the contents.
354  unittest::RawMessageSet raw;
355  ASSERT_TRUE(raw.ParseFromString(data));
356
357  EXPECT_EQ(0, raw.unknown_fields().field_count());
358
359  ASSERT_EQ(3, raw.item_size());
360  EXPECT_EQ(
361    unittest::TestMessageSetExtension1::descriptor()->extension(0)->number(),
362    raw.item(0).type_id());
363  EXPECT_EQ(
364    unittest::TestMessageSetExtension2::descriptor()->extension(0)->number(),
365    raw.item(1).type_id());
366  EXPECT_EQ(kUnknownTypeId, raw.item(2).type_id());
367
368  unittest::TestMessageSetExtension1 message1;
369  EXPECT_TRUE(message1.ParseFromString(raw.item(0).message()));
370  EXPECT_EQ(123, message1.i());
371
372  unittest::TestMessageSetExtension2 message2;
373  EXPECT_TRUE(message2.ParseFromString(raw.item(1).message()));
374  EXPECT_EQ("foo", message2.str());
375
376  EXPECT_EQ("bar", raw.item(2).message());
377}
378
379TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) {
380  // Serialize a MessageSet to a stream and to a flat array using generated
381  // code, and also using WireFormat, and check that the results are equal.
382  // Set up a TestMessageSet with two known messages and an unknown one, as
383  // above.
384
385  unittest::TestMessageSet message_set;
386  message_set.MutableExtension(
387    unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
388  message_set.MutableExtension(
389    unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
390  message_set.mutable_unknown_fields()->AddLengthDelimited(
391    kUnknownTypeId, "bar");
392
393  int size = message_set.ByteSize();
394  EXPECT_EQ(size, message_set.GetCachedSize());
395  ASSERT_EQ(size, WireFormat::ByteSize(message_set));
396
397  string flat_data;
398  string stream_data;
399  string dynamic_data;
400  flat_data.resize(size);
401  stream_data.resize(size);
402
403  // Serialize to flat array
404  {
405    uint8* target = reinterpret_cast<uint8*>(string_as_array(&flat_data));
406    uint8* end = message_set.SerializeWithCachedSizesToArray(target);
407    EXPECT_EQ(size, end - target);
408  }
409
410  // Serialize to buffer
411  {
412    io::ArrayOutputStream array_stream(string_as_array(&stream_data), size, 1);
413    io::CodedOutputStream output_stream(&array_stream);
414    message_set.SerializeWithCachedSizes(&output_stream);
415    ASSERT_FALSE(output_stream.HadError());
416  }
417
418  // Serialize to buffer with WireFormat.
419  {
420    io::StringOutputStream string_stream(&dynamic_data);
421    io::CodedOutputStream output_stream(&string_stream);
422    WireFormat::SerializeWithCachedSizes(message_set, size, &output_stream);
423    ASSERT_FALSE(output_stream.HadError());
424  }
425
426  EXPECT_TRUE(flat_data == stream_data);
427  EXPECT_TRUE(flat_data == dynamic_data);
428}
429
430TEST(WireFormatTest, ParseMessageSet) {
431  // Set up a RawMessageSet with two known messages and an unknown one.
432  unittest::RawMessageSet raw;
433
434  {
435    unittest::RawMessageSet::Item* item = raw.add_item();
436    item->set_type_id(
437      unittest::TestMessageSetExtension1::descriptor()->extension(0)->number());
438    unittest::TestMessageSetExtension1 message;
439    message.set_i(123);
440    message.SerializeToString(item->mutable_message());
441  }
442
443  {
444    unittest::RawMessageSet::Item* item = raw.add_item();
445    item->set_type_id(
446      unittest::TestMessageSetExtension2::descriptor()->extension(0)->number());
447    unittest::TestMessageSetExtension2 message;
448    message.set_str("foo");
449    message.SerializeToString(item->mutable_message());
450  }
451
452  {
453    unittest::RawMessageSet::Item* item = raw.add_item();
454    item->set_type_id(kUnknownTypeId);
455    item->set_message("bar");
456  }
457
458  string data;
459  ASSERT_TRUE(raw.SerializeToString(&data));
460
461  // Parse as a TestMessageSet and check the contents.
462  unittest::TestMessageSet message_set;
463  ASSERT_TRUE(message_set.ParseFromString(data));
464
465  EXPECT_EQ(123, message_set.GetExtension(
466    unittest::TestMessageSetExtension1::message_set_extension).i());
467  EXPECT_EQ("foo", message_set.GetExtension(
468    unittest::TestMessageSetExtension2::message_set_extension).str());
469
470  ASSERT_EQ(1, message_set.unknown_fields().field_count());
471  ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED,
472            message_set.unknown_fields().field(0).type());
473  EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited());
474
475  // Also parse using WireFormat.
476  unittest::TestMessageSet dynamic_message_set;
477  io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()),
478                             data.size());
479  ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set));
480  EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString());
481}
482
483TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) {
484  string data;
485  {
486    unittest::TestMessageSetExtension1 message;
487    message.set_i(123);
488    // Build a MessageSet manually with its message content put before its
489    // type_id.
490    io::StringOutputStream output_stream(&data);
491    io::CodedOutputStream coded_output(&output_stream);
492    coded_output.WriteTag(WireFormatLite::kMessageSetItemStartTag);
493    // Write the message content first.
494    WireFormatLite::WriteTag(WireFormatLite::kMessageSetMessageNumber,
495                             WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
496                             &coded_output);
497    coded_output.WriteVarint32(message.ByteSize());
498    message.SerializeWithCachedSizes(&coded_output);
499    // Write the type id.
500    uint32 type_id = message.GetDescriptor()->extension(0)->number();
501    WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber,
502                                type_id, &coded_output);
503    coded_output.WriteTag(WireFormatLite::kMessageSetItemEndTag);
504  }
505  {
506    unittest::TestMessageSet message_set;
507    ASSERT_TRUE(message_set.ParseFromString(data));
508
509    EXPECT_EQ(123, message_set.GetExtension(
510        unittest::TestMessageSetExtension1::message_set_extension).i());
511  }
512  {
513    // Test parse the message via Reflection.
514    unittest::TestMessageSet message_set;
515    io::CodedInputStream input(
516        reinterpret_cast<const uint8*>(data.data()), data.size());
517    EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set));
518    EXPECT_TRUE(input.ConsumedEntireMessage());
519
520    EXPECT_EQ(123, message_set.GetExtension(
521        unittest::TestMessageSetExtension1::message_set_extension).i());
522  }
523}
524
525TEST(WireFormatTest, ParseBrokenMessageSet) {
526  unittest::TestMessageSet message_set;
527  string input("goodbye");  // Invalid wire format data.
528  EXPECT_FALSE(message_set.ParseFromString(input));
529}
530
531TEST(WireFormatTest, RecursionLimit) {
532  unittest::TestRecursiveMessage message;
533  message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1);
534  string data;
535  message.SerializeToString(&data);
536
537  {
538    io::ArrayInputStream raw_input(data.data(), data.size());
539    io::CodedInputStream input(&raw_input);
540    input.SetRecursionLimit(4);
541    unittest::TestRecursiveMessage message2;
542    EXPECT_TRUE(message2.ParseFromCodedStream(&input));
543  }
544
545  {
546    io::ArrayInputStream raw_input(data.data(), data.size());
547    io::CodedInputStream input(&raw_input);
548    input.SetRecursionLimit(3);
549    unittest::TestRecursiveMessage message2;
550    EXPECT_FALSE(message2.ParseFromCodedStream(&input));
551  }
552}
553
554TEST(WireFormatTest, UnknownFieldRecursionLimit) {
555  unittest::TestEmptyMessage message;
556  message.mutable_unknown_fields()
557        ->AddGroup(1234)
558        ->AddGroup(1234)
559        ->AddGroup(1234)
560        ->AddGroup(1234)
561        ->AddVarint(1234, 123);
562  string data;
563  message.SerializeToString(&data);
564
565  {
566    io::ArrayInputStream raw_input(data.data(), data.size());
567    io::CodedInputStream input(&raw_input);
568    input.SetRecursionLimit(4);
569    unittest::TestEmptyMessage message2;
570    EXPECT_TRUE(message2.ParseFromCodedStream(&input));
571  }
572
573  {
574    io::ArrayInputStream raw_input(data.data(), data.size());
575    io::CodedInputStream input(&raw_input);
576    input.SetRecursionLimit(3);
577    unittest::TestEmptyMessage message2;
578    EXPECT_FALSE(message2.ParseFromCodedStream(&input));
579  }
580}
581
582TEST(WireFormatTest, ZigZag) {
583// avoid line-wrapping
584#define LL(x) GOOGLE_LONGLONG(x)
585#define ULL(x) GOOGLE_ULONGLONG(x)
586#define ZigZagEncode32(x) WireFormatLite::ZigZagEncode32(x)
587#define ZigZagDecode32(x) WireFormatLite::ZigZagDecode32(x)
588#define ZigZagEncode64(x) WireFormatLite::ZigZagEncode64(x)
589#define ZigZagDecode64(x) WireFormatLite::ZigZagDecode64(x)
590
591  EXPECT_EQ(0u, ZigZagEncode32( 0));
592  EXPECT_EQ(1u, ZigZagEncode32(-1));
593  EXPECT_EQ(2u, ZigZagEncode32( 1));
594  EXPECT_EQ(3u, ZigZagEncode32(-2));
595  EXPECT_EQ(0x7FFFFFFEu, ZigZagEncode32(0x3FFFFFFF));
596  EXPECT_EQ(0x7FFFFFFFu, ZigZagEncode32(0xC0000000));
597  EXPECT_EQ(0xFFFFFFFEu, ZigZagEncode32(0x7FFFFFFF));
598  EXPECT_EQ(0xFFFFFFFFu, ZigZagEncode32(0x80000000));
599
600  EXPECT_EQ( 0, ZigZagDecode32(0u));
601  EXPECT_EQ(-1, ZigZagDecode32(1u));
602  EXPECT_EQ( 1, ZigZagDecode32(2u));
603  EXPECT_EQ(-2, ZigZagDecode32(3u));
604  EXPECT_EQ(0x3FFFFFFF, ZigZagDecode32(0x7FFFFFFEu));
605  EXPECT_EQ(0xC0000000, ZigZagDecode32(0x7FFFFFFFu));
606  EXPECT_EQ(0x7FFFFFFF, ZigZagDecode32(0xFFFFFFFEu));
607  EXPECT_EQ(0x80000000, ZigZagDecode32(0xFFFFFFFFu));
608
609  EXPECT_EQ(0u, ZigZagEncode64( 0));
610  EXPECT_EQ(1u, ZigZagEncode64(-1));
611  EXPECT_EQ(2u, ZigZagEncode64( 1));
612  EXPECT_EQ(3u, ZigZagEncode64(-2));
613  EXPECT_EQ(ULL(0x000000007FFFFFFE), ZigZagEncode64(LL(0x000000003FFFFFFF)));
614  EXPECT_EQ(ULL(0x000000007FFFFFFF), ZigZagEncode64(LL(0xFFFFFFFFC0000000)));
615  EXPECT_EQ(ULL(0x00000000FFFFFFFE), ZigZagEncode64(LL(0x000000007FFFFFFF)));
616  EXPECT_EQ(ULL(0x00000000FFFFFFFF), ZigZagEncode64(LL(0xFFFFFFFF80000000)));
617  EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFE), ZigZagEncode64(LL(0x7FFFFFFFFFFFFFFF)));
618  EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFF), ZigZagEncode64(LL(0x8000000000000000)));
619
620  EXPECT_EQ( 0, ZigZagDecode64(0u));
621  EXPECT_EQ(-1, ZigZagDecode64(1u));
622  EXPECT_EQ( 1, ZigZagDecode64(2u));
623  EXPECT_EQ(-2, ZigZagDecode64(3u));
624  EXPECT_EQ(LL(0x000000003FFFFFFF), ZigZagDecode64(ULL(0x000000007FFFFFFE)));
625  EXPECT_EQ(LL(0xFFFFFFFFC0000000), ZigZagDecode64(ULL(0x000000007FFFFFFF)));
626  EXPECT_EQ(LL(0x000000007FFFFFFF), ZigZagDecode64(ULL(0x00000000FFFFFFFE)));
627  EXPECT_EQ(LL(0xFFFFFFFF80000000), ZigZagDecode64(ULL(0x00000000FFFFFFFF)));
628  EXPECT_EQ(LL(0x7FFFFFFFFFFFFFFF), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFE)));
629  EXPECT_EQ(LL(0x8000000000000000), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFF)));
630
631  // Some easier-to-verify round-trip tests.  The inputs (other than 0, 1, -1)
632  // were chosen semi-randomly via keyboard bashing.
633  EXPECT_EQ(    0, ZigZagDecode32(ZigZagEncode32(    0)));
634  EXPECT_EQ(    1, ZigZagDecode32(ZigZagEncode32(    1)));
635  EXPECT_EQ(   -1, ZigZagDecode32(ZigZagEncode32(   -1)));
636  EXPECT_EQ(14927, ZigZagDecode32(ZigZagEncode32(14927)));
637  EXPECT_EQ(-3612, ZigZagDecode32(ZigZagEncode32(-3612)));
638
639  EXPECT_EQ(    0, ZigZagDecode64(ZigZagEncode64(    0)));
640  EXPECT_EQ(    1, ZigZagDecode64(ZigZagEncode64(    1)));
641  EXPECT_EQ(   -1, ZigZagDecode64(ZigZagEncode64(   -1)));
642  EXPECT_EQ(14927, ZigZagDecode64(ZigZagEncode64(14927)));
643  EXPECT_EQ(-3612, ZigZagDecode64(ZigZagEncode64(-3612)));
644
645  EXPECT_EQ(LL(856912304801416), ZigZagDecode64(ZigZagEncode64(
646            LL(856912304801416))));
647  EXPECT_EQ(LL(-75123905439571256), ZigZagDecode64(ZigZagEncode64(
648            LL(-75123905439571256))));
649}
650
651TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) {
652  // At one point checks would trigger when parsing repeated fixed scalar
653  // fields.
654  protobuf_unittest::TestRepeatedScalarDifferentTagSizes msg1, msg2;
655  for (int i = 0; i < 100; ++i) {
656    msg1.add_repeated_fixed32(i);
657    msg1.add_repeated_int32(i);
658    msg1.add_repeated_fixed64(i);
659    msg1.add_repeated_int64(i);
660    msg1.add_repeated_float(i);
661    msg1.add_repeated_uint64(i);
662  }
663
664  // Make sure that we have a variety of tag sizes.
665  const google::protobuf::Descriptor* desc = msg1.GetDescriptor();
666  const google::protobuf::FieldDescriptor* field;
667  field = desc->FindFieldByName("repeated_fixed32");
668  ASSERT_TRUE(field != NULL);
669  ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
670  field = desc->FindFieldByName("repeated_int32");
671  ASSERT_TRUE(field != NULL);
672  ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
673  field = desc->FindFieldByName("repeated_fixed64");
674  ASSERT_TRUE(field != NULL);
675  ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
676  field = desc->FindFieldByName("repeated_int64");
677  ASSERT_TRUE(field != NULL);
678  ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
679  field = desc->FindFieldByName("repeated_float");
680  ASSERT_TRUE(field != NULL);
681  ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
682  field = desc->FindFieldByName("repeated_uint64");
683  ASSERT_TRUE(field != NULL);
684  ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
685
686  EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString()));
687  EXPECT_EQ(msg1.DebugString(), msg2.DebugString());
688}
689
690class WireFormatInvalidInputTest : public testing::Test {
691 protected:
692  // Make a serialized TestAllTypes in which the field optional_nested_message
693  // contains exactly the given bytes, which may be invalid.
694  string MakeInvalidEmbeddedMessage(const char* bytes, int size) {
695    const FieldDescriptor* field =
696      unittest::TestAllTypes::descriptor()->FindFieldByName(
697        "optional_nested_message");
698    GOOGLE_CHECK(field != NULL);
699
700    string result;
701
702    {
703      io::StringOutputStream raw_output(&result);
704      io::CodedOutputStream output(&raw_output);
705
706      WireFormatLite::WriteBytes(field->number(), string(bytes, size), &output);
707    }
708
709    return result;
710  }
711
712  // Make a serialized TestAllTypes in which the field optionalgroup
713  // contains exactly the given bytes -- which may be invalid -- and
714  // possibly no end tag.
715  string MakeInvalidGroup(const char* bytes, int size, bool include_end_tag) {
716    const FieldDescriptor* field =
717      unittest::TestAllTypes::descriptor()->FindFieldByName(
718        "optionalgroup");
719    GOOGLE_CHECK(field != NULL);
720
721    string result;
722
723    {
724      io::StringOutputStream raw_output(&result);
725      io::CodedOutputStream output(&raw_output);
726
727      output.WriteVarint32(WireFormat::MakeTag(field));
728      output.WriteString(string(bytes, size));
729      if (include_end_tag) {
730        output.WriteVarint32(WireFormatLite::MakeTag(
731          field->number(), WireFormatLite::WIRETYPE_END_GROUP));
732      }
733    }
734
735    return result;
736  }
737};
738
739TEST_F(WireFormatInvalidInputTest, InvalidSubMessage) {
740  unittest::TestAllTypes message;
741
742  // Control case.
743  EXPECT_TRUE(message.ParseFromString(MakeInvalidEmbeddedMessage("", 0)));
744
745  // The byte is a valid varint, but not a valid tag (zero).
746  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\0", 1)));
747
748  // The byte is a malformed varint.
749  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\200", 1)));
750
751  // The byte is an endgroup tag, but we aren't parsing a group.
752  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\014", 1)));
753
754  // The byte is a valid varint but not a valid tag (bad wire type).
755  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\017", 1)));
756}
757
758TEST_F(WireFormatInvalidInputTest, InvalidGroup) {
759  unittest::TestAllTypes message;
760
761  // Control case.
762  EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
763
764  // Missing end tag.  Groups cannot end at EOF.
765  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
766
767  // The byte is a valid varint, but not a valid tag (zero).
768  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
769
770  // The byte is a malformed varint.
771  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
772
773  // The byte is an endgroup tag, but not the right one for this group.
774  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
775
776  // The byte is a valid varint but not a valid tag (bad wire type).
777  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
778}
779
780TEST_F(WireFormatInvalidInputTest, InvalidUnknownGroup) {
781  // Use TestEmptyMessage so that the group made by MakeInvalidGroup will not
782  // be a known tag number.
783  unittest::TestEmptyMessage message;
784
785  // Control case.
786  EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
787
788  // Missing end tag.  Groups cannot end at EOF.
789  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
790
791  // The byte is a valid varint, but not a valid tag (zero).
792  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
793
794  // The byte is a malformed varint.
795  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
796
797  // The byte is an endgroup tag, but not the right one for this group.
798  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
799
800  // The byte is a valid varint but not a valid tag (bad wire type).
801  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
802}
803
804TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) {
805  // Test a bug fix:  SkipMessage should fail if the message contains a string
806  // whose length would extend beyond the message end.
807
808  unittest::TestAllTypes message;
809  message.set_optional_string("foo foo foo foo");
810  string data;
811  message.SerializeToString(&data);
812
813  // Chop some bytes off the end.
814  data.resize(data.size() - 4);
815
816  // Try to skip it.  Note that the bug was only present when parsing to an
817  // UnknownFieldSet.
818  io::ArrayInputStream raw_input(data.data(), data.size());
819  io::CodedInputStream coded_input(&raw_input);
820  UnknownFieldSet unknown_fields;
821  EXPECT_FALSE(WireFormat::SkipMessage(&coded_input, &unknown_fields));
822}
823
824// Test differences between string and bytes.
825// Value of a string type must be valid UTF-8 string.  When UTF-8
826// validation is enabled (GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED):
827// WriteInvalidUTF8String:  see error message.
828// ReadInvalidUTF8String:  see error message.
829// WriteValidUTF8String: fine.
830// ReadValidUTF8String:  fine.
831// WriteAnyBytes: fine.
832// ReadAnyBytes: fine.
833const char * kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0";
834// This used to be "Valid UTF-8: \x01\x02\u8C37\u6B4C", but MSVC seems to
835// interpret \u differently from GCC.
836const char * kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214";
837
838template<typename T>
839bool WriteMessage(const char *value, T *message, string *wire_buffer) {
840  message->set_data(value);
841  wire_buffer->clear();
842  message->AppendToString(wire_buffer);
843  return (wire_buffer->size() > 0);
844}
845
846template<typename T>
847bool ReadMessage(const string &wire_buffer, T *message) {
848  return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
849}
850
851bool StartsWith(const string& s, const string& prefix) {
852  return s.substr(0, prefix.length()) == prefix;
853}
854
855TEST(Utf8ValidationTest, WriteInvalidUTF8String) {
856  string wire_buffer;
857  protobuf_unittest::OneString input;
858  vector<string> errors;
859  {
860    ScopedMemoryLog log;
861    WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
862    errors = log.GetMessages(ERROR);
863  }
864#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
865  ASSERT_EQ(1, errors.size());
866  EXPECT_TRUE(StartsWith(errors[0],
867                         "String field contains invalid UTF-8 data when "
868                         "serializing a protocol buffer. Use the "
869                         "'bytes' type if you intend to send raw bytes."));
870#else
871  ASSERT_EQ(0, errors.size());
872#endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
873}
874
875TEST(Utf8ValidationTest, ReadInvalidUTF8String) {
876  string wire_buffer;
877  protobuf_unittest::OneString input;
878  WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
879  protobuf_unittest::OneString output;
880  vector<string> errors;
881  {
882    ScopedMemoryLog log;
883    ReadMessage(wire_buffer, &output);
884    errors = log.GetMessages(ERROR);
885  }
886#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
887  ASSERT_EQ(1, errors.size());
888  EXPECT_TRUE(StartsWith(errors[0],
889                         "String field contains invalid UTF-8 data when "
890                         "parsing a protocol buffer. Use the "
891                         "'bytes' type if you intend to send raw bytes."));
892
893#else
894  ASSERT_EQ(0, errors.size());
895#endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
896}
897
898TEST(Utf8ValidationTest, WriteValidUTF8String) {
899  string wire_buffer;
900  protobuf_unittest::OneString input;
901  vector<string> errors;
902  {
903    ScopedMemoryLog log;
904    WriteMessage(kValidUTF8String, &input, &wire_buffer);
905    errors = log.GetMessages(ERROR);
906  }
907  ASSERT_EQ(0, errors.size());
908}
909
910TEST(Utf8ValidationTest, ReadValidUTF8String) {
911  string wire_buffer;
912  protobuf_unittest::OneString input;
913  WriteMessage(kValidUTF8String, &input, &wire_buffer);
914  protobuf_unittest::OneString output;
915  vector<string> errors;
916  {
917    ScopedMemoryLog log;
918    ReadMessage(wire_buffer, &output);
919    errors = log.GetMessages(ERROR);
920  }
921  ASSERT_EQ(0, errors.size());
922  EXPECT_EQ(input.data(), output.data());
923}
924
925// Bytes: anything can pass as bytes, use invalid UTF-8 string to test
926TEST(Utf8ValidationTest, WriteArbitraryBytes) {
927  string wire_buffer;
928  protobuf_unittest::OneBytes input;
929  vector<string> errors;
930  {
931    ScopedMemoryLog log;
932    WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
933    errors = log.GetMessages(ERROR);
934  }
935  ASSERT_EQ(0, errors.size());
936}
937
938TEST(Utf8ValidationTest, ReadArbitraryBytes) {
939  string wire_buffer;
940  protobuf_unittest::OneBytes input;
941  WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
942  protobuf_unittest::OneBytes output;
943  vector<string> errors;
944  {
945    ScopedMemoryLog log;
946    ReadMessage(wire_buffer, &output);
947    errors = log.GetMessages(ERROR);
948  }
949  ASSERT_EQ(0, errors.size());
950  EXPECT_EQ(input.data(), output.data());
951}
952
953TEST(Utf8ValidationTest, ParseRepeatedString) {
954  protobuf_unittest::MoreBytes input;
955  input.add_data(kValidUTF8String);
956  input.add_data(kInvalidUTF8String);
957  input.add_data(kInvalidUTF8String);
958  string wire_buffer = input.SerializeAsString();
959
960  protobuf_unittest::MoreString output;
961  vector<string> errors;
962  {
963    ScopedMemoryLog log;
964    ReadMessage(wire_buffer, &output);
965    errors = log.GetMessages(ERROR);
966  }
967#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
968  ASSERT_EQ(2, errors.size());
969#else
970  ASSERT_EQ(0, errors.size());
971#endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
972  EXPECT_EQ(wire_buffer, output.SerializeAsString());
973}
974
975}  // namespace
976}  // namespace internal
977}  // namespace protobuf
978}  // namespace google
979