PageRenderTime 156ms CodeModel.GetById 62ms app.highlight 87ms RepoModel.GetById 2ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/wire_format_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 905 lines | 628 code | 147 blank | 130 comment | 16 complexity | d2d00f5b1f5164c44ae8de5ac0e4c28a MD5 | raw file
  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-inl.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, RecursionLimit) {
484  unittest::TestRecursiveMessage message;
485  message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1);
486  string data;
487  message.SerializeToString(&data);
488
489  {
490    io::ArrayInputStream raw_input(data.data(), data.size());
491    io::CodedInputStream input(&raw_input);
492    input.SetRecursionLimit(4);
493    unittest::TestRecursiveMessage message2;
494    EXPECT_TRUE(message2.ParseFromCodedStream(&input));
495  }
496
497  {
498    io::ArrayInputStream raw_input(data.data(), data.size());
499    io::CodedInputStream input(&raw_input);
500    input.SetRecursionLimit(3);
501    unittest::TestRecursiveMessage message2;
502    EXPECT_FALSE(message2.ParseFromCodedStream(&input));
503  }
504}
505
506TEST(WireFormatTest, UnknownFieldRecursionLimit) {
507  unittest::TestEmptyMessage message;
508  message.mutable_unknown_fields()
509        ->AddGroup(1234)
510        ->AddGroup(1234)
511        ->AddGroup(1234)
512        ->AddGroup(1234)
513        ->AddVarint(1234, 123);
514  string data;
515  message.SerializeToString(&data);
516
517  {
518    io::ArrayInputStream raw_input(data.data(), data.size());
519    io::CodedInputStream input(&raw_input);
520    input.SetRecursionLimit(4);
521    unittest::TestEmptyMessage message2;
522    EXPECT_TRUE(message2.ParseFromCodedStream(&input));
523  }
524
525  {
526    io::ArrayInputStream raw_input(data.data(), data.size());
527    io::CodedInputStream input(&raw_input);
528    input.SetRecursionLimit(3);
529    unittest::TestEmptyMessage message2;
530    EXPECT_FALSE(message2.ParseFromCodedStream(&input));
531  }
532}
533
534TEST(WireFormatTest, ZigZag) {
535// avoid line-wrapping
536#define LL(x) GOOGLE_LONGLONG(x)
537#define ULL(x) GOOGLE_ULONGLONG(x)
538#define ZigZagEncode32(x) WireFormatLite::ZigZagEncode32(x)
539#define ZigZagDecode32(x) WireFormatLite::ZigZagDecode32(x)
540#define ZigZagEncode64(x) WireFormatLite::ZigZagEncode64(x)
541#define ZigZagDecode64(x) WireFormatLite::ZigZagDecode64(x)
542
543  EXPECT_EQ(0u, ZigZagEncode32( 0));
544  EXPECT_EQ(1u, ZigZagEncode32(-1));
545  EXPECT_EQ(2u, ZigZagEncode32( 1));
546  EXPECT_EQ(3u, ZigZagEncode32(-2));
547  EXPECT_EQ(0x7FFFFFFEu, ZigZagEncode32(0x3FFFFFFF));
548  EXPECT_EQ(0x7FFFFFFFu, ZigZagEncode32(0xC0000000));
549  EXPECT_EQ(0xFFFFFFFEu, ZigZagEncode32(0x7FFFFFFF));
550  EXPECT_EQ(0xFFFFFFFFu, ZigZagEncode32(0x80000000));
551
552  EXPECT_EQ( 0, ZigZagDecode32(0u));
553  EXPECT_EQ(-1, ZigZagDecode32(1u));
554  EXPECT_EQ( 1, ZigZagDecode32(2u));
555  EXPECT_EQ(-2, ZigZagDecode32(3u));
556  EXPECT_EQ(0x3FFFFFFF, ZigZagDecode32(0x7FFFFFFEu));
557  EXPECT_EQ(0xC0000000, ZigZagDecode32(0x7FFFFFFFu));
558  EXPECT_EQ(0x7FFFFFFF, ZigZagDecode32(0xFFFFFFFEu));
559  EXPECT_EQ(0x80000000, ZigZagDecode32(0xFFFFFFFFu));
560
561  EXPECT_EQ(0u, ZigZagEncode64( 0));
562  EXPECT_EQ(1u, ZigZagEncode64(-1));
563  EXPECT_EQ(2u, ZigZagEncode64( 1));
564  EXPECT_EQ(3u, ZigZagEncode64(-2));
565  EXPECT_EQ(ULL(0x000000007FFFFFFE), ZigZagEncode64(LL(0x000000003FFFFFFF)));
566  EXPECT_EQ(ULL(0x000000007FFFFFFF), ZigZagEncode64(LL(0xFFFFFFFFC0000000)));
567  EXPECT_EQ(ULL(0x00000000FFFFFFFE), ZigZagEncode64(LL(0x000000007FFFFFFF)));
568  EXPECT_EQ(ULL(0x00000000FFFFFFFF), ZigZagEncode64(LL(0xFFFFFFFF80000000)));
569  EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFE), ZigZagEncode64(LL(0x7FFFFFFFFFFFFFFF)));
570  EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFF), ZigZagEncode64(LL(0x8000000000000000)));
571
572  EXPECT_EQ( 0, ZigZagDecode64(0u));
573  EXPECT_EQ(-1, ZigZagDecode64(1u));
574  EXPECT_EQ( 1, ZigZagDecode64(2u));
575  EXPECT_EQ(-2, ZigZagDecode64(3u));
576  EXPECT_EQ(LL(0x000000003FFFFFFF), ZigZagDecode64(ULL(0x000000007FFFFFFE)));
577  EXPECT_EQ(LL(0xFFFFFFFFC0000000), ZigZagDecode64(ULL(0x000000007FFFFFFF)));
578  EXPECT_EQ(LL(0x000000007FFFFFFF), ZigZagDecode64(ULL(0x00000000FFFFFFFE)));
579  EXPECT_EQ(LL(0xFFFFFFFF80000000), ZigZagDecode64(ULL(0x00000000FFFFFFFF)));
580  EXPECT_EQ(LL(0x7FFFFFFFFFFFFFFF), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFE)));
581  EXPECT_EQ(LL(0x8000000000000000), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFF)));
582
583  // Some easier-to-verify round-trip tests.  The inputs (other than 0, 1, -1)
584  // were chosen semi-randomly via keyboard bashing.
585  EXPECT_EQ(    0, ZigZagDecode32(ZigZagEncode32(    0)));
586  EXPECT_EQ(    1, ZigZagDecode32(ZigZagEncode32(    1)));
587  EXPECT_EQ(   -1, ZigZagDecode32(ZigZagEncode32(   -1)));
588  EXPECT_EQ(14927, ZigZagDecode32(ZigZagEncode32(14927)));
589  EXPECT_EQ(-3612, ZigZagDecode32(ZigZagEncode32(-3612)));
590
591  EXPECT_EQ(    0, ZigZagDecode64(ZigZagEncode64(    0)));
592  EXPECT_EQ(    1, ZigZagDecode64(ZigZagEncode64(    1)));
593  EXPECT_EQ(   -1, ZigZagDecode64(ZigZagEncode64(   -1)));
594  EXPECT_EQ(14927, ZigZagDecode64(ZigZagEncode64(14927)));
595  EXPECT_EQ(-3612, ZigZagDecode64(ZigZagEncode64(-3612)));
596
597  EXPECT_EQ(LL(856912304801416), ZigZagDecode64(ZigZagEncode64(
598            LL(856912304801416))));
599  EXPECT_EQ(LL(-75123905439571256), ZigZagDecode64(ZigZagEncode64(
600            LL(-75123905439571256))));
601}
602
603TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) {
604  // At one point checks would trigger when parsing repeated fixed scalar
605  // fields.
606  protobuf_unittest::TestRepeatedScalarDifferentTagSizes msg1, msg2;
607  for (int i = 0; i < 100; ++i) {
608    msg1.add_repeated_fixed32(i);
609    msg1.add_repeated_int32(i);
610    msg1.add_repeated_fixed64(i);
611    msg1.add_repeated_int64(i);
612    msg1.add_repeated_float(i);
613    msg1.add_repeated_uint64(i);
614  }
615
616  // Make sure that we have a variety of tag sizes.
617  const google::protobuf::Descriptor* desc = msg1.GetDescriptor();
618  const google::protobuf::FieldDescriptor* field;
619  field = desc->FindFieldByName("repeated_fixed32");
620  ASSERT_TRUE(field != NULL);
621  ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
622  field = desc->FindFieldByName("repeated_int32");
623  ASSERT_TRUE(field != NULL);
624  ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
625  field = desc->FindFieldByName("repeated_fixed64");
626  ASSERT_TRUE(field != NULL);
627  ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
628  field = desc->FindFieldByName("repeated_int64");
629  ASSERT_TRUE(field != NULL);
630  ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
631  field = desc->FindFieldByName("repeated_float");
632  ASSERT_TRUE(field != NULL);
633  ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
634  field = desc->FindFieldByName("repeated_uint64");
635  ASSERT_TRUE(field != NULL);
636  ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
637
638  EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString()));
639  EXPECT_EQ(msg1.DebugString(), msg2.DebugString());
640}
641
642class WireFormatInvalidInputTest : public testing::Test {
643 protected:
644  // Make a serialized TestAllTypes in which the field optional_nested_message
645  // contains exactly the given bytes, which may be invalid.
646  string MakeInvalidEmbeddedMessage(const char* bytes, int size) {
647    const FieldDescriptor* field =
648      unittest::TestAllTypes::descriptor()->FindFieldByName(
649        "optional_nested_message");
650    GOOGLE_CHECK(field != NULL);
651
652    string result;
653
654    {
655      io::StringOutputStream raw_output(&result);
656      io::CodedOutputStream output(&raw_output);
657
658      WireFormatLite::WriteBytes(field->number(), string(bytes, size), &output);
659    }
660
661    return result;
662  }
663
664  // Make a serialized TestAllTypes in which the field optionalgroup
665  // contains exactly the given bytes -- which may be invalid -- and
666  // possibly no end tag.
667  string MakeInvalidGroup(const char* bytes, int size, bool include_end_tag) {
668    const FieldDescriptor* field =
669      unittest::TestAllTypes::descriptor()->FindFieldByName(
670        "optionalgroup");
671    GOOGLE_CHECK(field != NULL);
672
673    string result;
674
675    {
676      io::StringOutputStream raw_output(&result);
677      io::CodedOutputStream output(&raw_output);
678
679      output.WriteVarint32(WireFormat::MakeTag(field));
680      output.WriteString(string(bytes, size));
681      if (include_end_tag) {
682        output.WriteVarint32(WireFormatLite::MakeTag(
683          field->number(), WireFormatLite::WIRETYPE_END_GROUP));
684      }
685    }
686
687    return result;
688  }
689};
690
691TEST_F(WireFormatInvalidInputTest, InvalidSubMessage) {
692  unittest::TestAllTypes message;
693
694  // Control case.
695  EXPECT_TRUE(message.ParseFromString(MakeInvalidEmbeddedMessage("", 0)));
696
697  // The byte is a valid varint, but not a valid tag (zero).
698  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\0", 1)));
699
700  // The byte is a malformed varint.
701  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\200", 1)));
702
703  // The byte is an endgroup tag, but we aren't parsing a group.
704  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\014", 1)));
705
706  // The byte is a valid varint but not a valid tag (bad wire type).
707  EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\017", 1)));
708}
709
710TEST_F(WireFormatInvalidInputTest, InvalidGroup) {
711  unittest::TestAllTypes message;
712
713  // Control case.
714  EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
715
716  // Missing end tag.  Groups cannot end at EOF.
717  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
718
719  // The byte is a valid varint, but not a valid tag (zero).
720  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
721
722  // The byte is a malformed varint.
723  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
724
725  // The byte is an endgroup tag, but not the right one for this group.
726  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
727
728  // The byte is a valid varint but not a valid tag (bad wire type).
729  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
730}
731
732TEST_F(WireFormatInvalidInputTest, InvalidUnknownGroup) {
733  // Use TestEmptyMessage so that the group made by MakeInvalidGroup will not
734  // be a known tag number.
735  unittest::TestEmptyMessage message;
736
737  // Control case.
738  EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
739
740  // Missing end tag.  Groups cannot end at EOF.
741  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
742
743  // The byte is a valid varint, but not a valid tag (zero).
744  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
745
746  // The byte is a malformed varint.
747  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
748
749  // The byte is an endgroup tag, but not the right one for this group.
750  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
751
752  // The byte is a valid varint but not a valid tag (bad wire type).
753  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
754}
755
756TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) {
757  // Test a bug fix:  SkipMessage should fail if the message contains a string
758  // whose length would extend beyond the message end.
759
760  unittest::TestAllTypes message;
761  message.set_optional_string("foo foo foo foo");
762  string data;
763  message.SerializeToString(&data);
764
765  // Chop some bytes off the end.
766  data.resize(data.size() - 4);
767
768  // Try to skip it.  Note that the bug was only present when parsing to an
769  // UnknownFieldSet.
770  io::ArrayInputStream raw_input(data.data(), data.size());
771  io::CodedInputStream coded_input(&raw_input);
772  UnknownFieldSet unknown_fields;
773  EXPECT_FALSE(WireFormat::SkipMessage(&coded_input, &unknown_fields));
774}
775
776// Test differences between string and bytes.
777// Value of a string type must be valid UTF-8 string.  When UTF-8
778// validation is enabled (GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED):
779// WriteInvalidUTF8String:  see error message.
780// ReadInvalidUTF8String:  see error message.
781// WriteValidUTF8String: fine.
782// ReadValidUTF8String:  fine.
783// WriteAnyBytes: fine.
784// ReadAnyBytes: fine.
785const char * kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0";
786// This used to be "Valid UTF-8: \x01\x02\u8C37\u6B4C", but MSVC seems to
787// interpret \u differently from GCC.
788const char * kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214";
789
790template<typename T>
791bool WriteMessage(const char *value, T *message, string *wire_buffer) {
792  message->set_data(value);
793  wire_buffer->clear();
794  message->AppendToString(wire_buffer);
795  return (wire_buffer->size() > 0);
796}
797
798template<typename T>
799bool ReadMessage(const string &wire_buffer, T *message) {
800  return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
801}
802
803TEST(Utf8ValidationTest, WriteInvalidUTF8String) {
804  string wire_buffer;
805  protobuf_unittest::OneString input;
806  vector<string> errors;
807  {
808    ScopedMemoryLog log;
809    WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
810    errors = log.GetMessages(ERROR);
811  }
812#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
813  ASSERT_EQ(1, errors.size());
814  EXPECT_EQ("Encountered string containing invalid UTF-8 data while "
815            "serializing protocol buffer. Strings must contain only UTF-8; "
816            "use the 'bytes' type for raw bytes.",
817            errors[0]);
818
819#else
820  ASSERT_EQ(0, errors.size());
821#endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
822}
823
824TEST(Utf8ValidationTest, ReadInvalidUTF8String) {
825  string wire_buffer;
826  protobuf_unittest::OneString input;
827  WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
828  protobuf_unittest::OneString output;
829  vector<string> errors;
830  {
831    ScopedMemoryLog log;
832    ReadMessage(wire_buffer, &output);
833    errors = log.GetMessages(ERROR);
834  }
835#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
836  ASSERT_EQ(1, errors.size());
837  EXPECT_EQ("Encountered string containing invalid UTF-8 data while "
838            "parsing protocol buffer. Strings must contain only UTF-8; "
839            "use the 'bytes' type for raw bytes.",
840            errors[0]);
841
842#else
843  ASSERT_EQ(0, errors.size());
844#endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
845}
846
847TEST(Utf8ValidationTest, WriteValidUTF8String) {
848  string wire_buffer;
849  protobuf_unittest::OneString input;
850  vector<string> errors;
851  {
852    ScopedMemoryLog log;
853    WriteMessage(kValidUTF8String, &input, &wire_buffer);
854    errors = log.GetMessages(ERROR);
855  }
856  ASSERT_EQ(0, errors.size());
857}
858
859TEST(Utf8ValidationTest, ReadValidUTF8String) {
860  string wire_buffer;
861  protobuf_unittest::OneString input;
862  WriteMessage(kValidUTF8String, &input, &wire_buffer);
863  protobuf_unittest::OneString output;
864  vector<string> errors;
865  {
866    ScopedMemoryLog log;
867    ReadMessage(wire_buffer, &output);
868    errors = log.GetMessages(ERROR);
869  }
870  ASSERT_EQ(0, errors.size());
871  EXPECT_EQ(input.data(), output.data());
872}
873
874// Bytes: anything can pass as bytes, use invalid UTF-8 string to test
875TEST(Utf8ValidationTest, WriteArbitraryBytes) {
876  string wire_buffer;
877  protobuf_unittest::OneBytes input;
878  vector<string> errors;
879  {
880    ScopedMemoryLog log;
881    WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
882    errors = log.GetMessages(ERROR);
883  }
884  ASSERT_EQ(0, errors.size());
885}
886
887TEST(Utf8ValidationTest, ReadArbitraryBytes) {
888  string wire_buffer;
889  protobuf_unittest::OneBytes input;
890  WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
891  protobuf_unittest::OneBytes output;
892  vector<string> errors;
893  {
894    ScopedMemoryLog log;
895    ReadMessage(wire_buffer, &output);
896    errors = log.GetMessages(ERROR);
897  }
898  ASSERT_EQ(0, errors.size());
899  EXPECT_EQ(input.data(), output.data());
900}
901
902}  // namespace
903}  // namespace internal
904}  // namespace protobuf
905}  // namespace google