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