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/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
49 namespace google {
50 namespace protobuf {
51 namespace internal {
52 namespace {
53
TEST(WireFormatTest,EnumsInSync)54 TEST(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
TEST(WireFormatTest,MaxFieldNumber)72 TEST(WireFormatTest, MaxFieldNumber) {
73 // Make sure the max field number constant is accurate.
74 EXPECT_EQ((1 << (32 - WireFormatLite::kTagTypeBits)) - 1,
75 FieldDescriptor::kMaxNumber);
76 }
77
TEST(WireFormatTest,Parse)78 TEST(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
TEST(WireFormatTest,ParseExtensions)95 TEST(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
TEST(WireFormatTest,ParsePacked)112 TEST(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
TEST(WireFormatTest,ParsePackedFromUnpacked)129 TEST(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
TEST(WireFormatTest,ParseUnpackedFromPacked)145 TEST(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
TEST(WireFormatTest,ParsePackedExtensions)161 TEST(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
TEST(WireFormatTest,ParseOneof)178 TEST(WireFormatTest, ParseOneof) {
179 unittest::TestOneof2 source, dest;
180 string data;
181
182 // Serialize using the generated code.
183 TestUtil::SetOneof1(&source);
184 source.SerializeToString(&data);
185
186 // Parse using WireFormat.
187 io::ArrayInputStream raw_input(data.data(), data.size());
188 io::CodedInputStream input(&raw_input);
189 WireFormat::ParseAndMergePartial(&input, &dest);
190
191 // Check.
192 TestUtil::ExpectOneofSet1(dest);
193 }
194
TEST(WireFormatTest,OneofOnlySetLast)195 TEST(WireFormatTest, OneofOnlySetLast) {
196 unittest::TestOneofBackwardsCompatible source;
197 unittest::TestOneof oneof_dest;
198 string data;
199
200 // Set two fields
201 source.set_foo_int(100);
202 source.set_foo_string("101");
203
204 // Serialize and parse to oneof message.
205 source.SerializeToString(&data);
206 io::ArrayInputStream raw_input(data.data(), data.size());
207 io::CodedInputStream input(&raw_input);
208 WireFormat::ParseAndMergePartial(&input, &oneof_dest);
209
210 // Only the last field is set.
211 EXPECT_FALSE(oneof_dest.has_foo_int());
212 EXPECT_TRUE(oneof_dest.has_foo_string());
213 }
214
TEST(WireFormatTest,ByteSize)215 TEST(WireFormatTest, ByteSize) {
216 unittest::TestAllTypes message;
217 TestUtil::SetAllFields(&message);
218
219 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
220 message.Clear();
221 EXPECT_EQ(0, message.ByteSize());
222 EXPECT_EQ(0, WireFormat::ByteSize(message));
223 }
224
TEST(WireFormatTest,ByteSizeExtensions)225 TEST(WireFormatTest, ByteSizeExtensions) {
226 unittest::TestAllExtensions message;
227 TestUtil::SetAllExtensions(&message);
228
229 EXPECT_EQ(message.ByteSize(),
230 WireFormat::ByteSize(message));
231 message.Clear();
232 EXPECT_EQ(0, message.ByteSize());
233 EXPECT_EQ(0, WireFormat::ByteSize(message));
234 }
235
TEST(WireFormatTest,ByteSizePacked)236 TEST(WireFormatTest, ByteSizePacked) {
237 unittest::TestPackedTypes message;
238 TestUtil::SetPackedFields(&message);
239
240 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
241 message.Clear();
242 EXPECT_EQ(0, message.ByteSize());
243 EXPECT_EQ(0, WireFormat::ByteSize(message));
244 }
245
TEST(WireFormatTest,ByteSizePackedExtensions)246 TEST(WireFormatTest, ByteSizePackedExtensions) {
247 unittest::TestPackedExtensions message;
248 TestUtil::SetPackedExtensions(&message);
249
250 EXPECT_EQ(message.ByteSize(),
251 WireFormat::ByteSize(message));
252 message.Clear();
253 EXPECT_EQ(0, message.ByteSize());
254 EXPECT_EQ(0, WireFormat::ByteSize(message));
255 }
256
TEST(WireFormatTest,ByteSizeOneof)257 TEST(WireFormatTest, ByteSizeOneof) {
258 unittest::TestOneof2 message;
259 TestUtil::SetOneof1(&message);
260
261 EXPECT_EQ(message.ByteSize(),
262 WireFormat::ByteSize(message));
263 message.Clear();
264
265 EXPECT_EQ(0, message.ByteSize());
266 EXPECT_EQ(0, WireFormat::ByteSize(message));
267 }
268
TEST(WireFormatTest,Serialize)269 TEST(WireFormatTest, Serialize) {
270 unittest::TestAllTypes message;
271 string generated_data;
272 string dynamic_data;
273
274 TestUtil::SetAllFields(&message);
275 int size = message.ByteSize();
276
277 // Serialize using the generated code.
278 {
279 io::StringOutputStream raw_output(&generated_data);
280 io::CodedOutputStream output(&raw_output);
281 message.SerializeWithCachedSizes(&output);
282 ASSERT_FALSE(output.HadError());
283 }
284
285 // Serialize using WireFormat.
286 {
287 io::StringOutputStream raw_output(&dynamic_data);
288 io::CodedOutputStream output(&raw_output);
289 WireFormat::SerializeWithCachedSizes(message, size, &output);
290 ASSERT_FALSE(output.HadError());
291 }
292
293 // Should be the same.
294 // Don't use EXPECT_EQ here because we're comparing raw binary data and
295 // we really don't want it dumped to stdout on failure.
296 EXPECT_TRUE(dynamic_data == generated_data);
297 }
298
TEST(WireFormatTest,SerializeExtensions)299 TEST(WireFormatTest, SerializeExtensions) {
300 unittest::TestAllExtensions message;
301 string generated_data;
302 string dynamic_data;
303
304 TestUtil::SetAllExtensions(&message);
305 int size = message.ByteSize();
306
307 // Serialize using the generated code.
308 {
309 io::StringOutputStream raw_output(&generated_data);
310 io::CodedOutputStream output(&raw_output);
311 message.SerializeWithCachedSizes(&output);
312 ASSERT_FALSE(output.HadError());
313 }
314
315 // Serialize using WireFormat.
316 {
317 io::StringOutputStream raw_output(&dynamic_data);
318 io::CodedOutputStream output(&raw_output);
319 WireFormat::SerializeWithCachedSizes(message, size, &output);
320 ASSERT_FALSE(output.HadError());
321 }
322
323 // Should be the same.
324 // Don't use EXPECT_EQ here because we're comparing raw binary data and
325 // we really don't want it dumped to stdout on failure.
326 EXPECT_TRUE(dynamic_data == generated_data);
327 }
328
TEST(WireFormatTest,SerializeFieldsAndExtensions)329 TEST(WireFormatTest, SerializeFieldsAndExtensions) {
330 unittest::TestFieldOrderings message;
331 string generated_data;
332 string dynamic_data;
333
334 TestUtil::SetAllFieldsAndExtensions(&message);
335 int size = message.ByteSize();
336
337 // Serialize using the generated code.
338 {
339 io::StringOutputStream raw_output(&generated_data);
340 io::CodedOutputStream output(&raw_output);
341 message.SerializeWithCachedSizes(&output);
342 ASSERT_FALSE(output.HadError());
343 }
344
345 // Serialize using WireFormat.
346 {
347 io::StringOutputStream raw_output(&dynamic_data);
348 io::CodedOutputStream output(&raw_output);
349 WireFormat::SerializeWithCachedSizes(message, size, &output);
350 ASSERT_FALSE(output.HadError());
351 }
352
353 // Should be the same.
354 // Don't use EXPECT_EQ here because we're comparing raw binary data and
355 // we really don't want it dumped to stdout on failure.
356 EXPECT_TRUE(dynamic_data == generated_data);
357
358 // Should output in canonical order.
359 TestUtil::ExpectAllFieldsAndExtensionsInOrder(dynamic_data);
360 TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data);
361 }
362
TEST(WireFormatTest,SerializeOneof)363 TEST(WireFormatTest, SerializeOneof) {
364 unittest::TestOneof2 message;
365 string generated_data;
366 string dynamic_data;
367
368 TestUtil::SetOneof1(&message);
369 int size = message.ByteSize();
370
371 // Serialize using the generated code.
372 {
373 io::StringOutputStream raw_output(&generated_data);
374 io::CodedOutputStream output(&raw_output);
375 message.SerializeWithCachedSizes(&output);
376 ASSERT_FALSE(output.HadError());
377 }
378
379 // Serialize using WireFormat.
380 {
381 io::StringOutputStream raw_output(&dynamic_data);
382 io::CodedOutputStream output(&raw_output);
383 WireFormat::SerializeWithCachedSizes(message, size, &output);
384 ASSERT_FALSE(output.HadError());
385 }
386
387 // Should be the same.
388 // Don't use EXPECT_EQ here because we're comparing raw binary data and
389 // we really don't want it dumped to stdout on failure.
390 EXPECT_TRUE(dynamic_data == generated_data);
391 }
392
TEST(WireFormatTest,ParseMultipleExtensionRanges)393 TEST(WireFormatTest, ParseMultipleExtensionRanges) {
394 // Make sure we can parse a message that contains multiple extensions ranges.
395 unittest::TestFieldOrderings source;
396 string data;
397
398 TestUtil::SetAllFieldsAndExtensions(&source);
399 source.SerializeToString(&data);
400
401 {
402 unittest::TestFieldOrderings dest;
403 EXPECT_TRUE(dest.ParseFromString(data));
404 EXPECT_EQ(source.DebugString(), dest.DebugString());
405 }
406
407 // Also test using reflection-based parsing.
408 {
409 unittest::TestFieldOrderings dest;
410 io::ArrayInputStream raw_input(data.data(), data.size());
411 io::CodedInputStream coded_input(&raw_input);
412 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest));
413 EXPECT_EQ(source.DebugString(), dest.DebugString());
414 }
415 }
416
417 const int kUnknownTypeId = 1550055;
418
TEST(WireFormatTest,SerializeMessageSet)419 TEST(WireFormatTest, SerializeMessageSet) {
420 // Set up a TestMessageSet with two known messages and an unknown one.
421 unittest::TestMessageSet message_set;
422 message_set.MutableExtension(
423 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
424 message_set.MutableExtension(
425 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
426 message_set.mutable_unknown_fields()->AddLengthDelimited(
427 kUnknownTypeId, "bar");
428
429 string data;
430 ASSERT_TRUE(message_set.SerializeToString(&data));
431
432 // Parse back using RawMessageSet and check the contents.
433 unittest::RawMessageSet raw;
434 ASSERT_TRUE(raw.ParseFromString(data));
435
436 EXPECT_EQ(0, raw.unknown_fields().field_count());
437
438 ASSERT_EQ(3, raw.item_size());
439 EXPECT_EQ(
440 unittest::TestMessageSetExtension1::descriptor()->extension(0)->number(),
441 raw.item(0).type_id());
442 EXPECT_EQ(
443 unittest::TestMessageSetExtension2::descriptor()->extension(0)->number(),
444 raw.item(1).type_id());
445 EXPECT_EQ(kUnknownTypeId, raw.item(2).type_id());
446
447 unittest::TestMessageSetExtension1 message1;
448 EXPECT_TRUE(message1.ParseFromString(raw.item(0).message()));
449 EXPECT_EQ(123, message1.i());
450
451 unittest::TestMessageSetExtension2 message2;
452 EXPECT_TRUE(message2.ParseFromString(raw.item(1).message()));
453 EXPECT_EQ("foo", message2.str());
454
455 EXPECT_EQ("bar", raw.item(2).message());
456 }
457
TEST(WireFormatTest,SerializeMessageSetVariousWaysAreEqual)458 TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) {
459 // Serialize a MessageSet to a stream and to a flat array using generated
460 // code, and also using WireFormat, and check that the results are equal.
461 // Set up a TestMessageSet with two known messages and an unknown one, as
462 // above.
463
464 unittest::TestMessageSet message_set;
465 message_set.MutableExtension(
466 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123);
467 message_set.MutableExtension(
468 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo");
469 message_set.mutable_unknown_fields()->AddLengthDelimited(
470 kUnknownTypeId, "bar");
471
472 int size = message_set.ByteSize();
473 EXPECT_EQ(size, message_set.GetCachedSize());
474 ASSERT_EQ(size, WireFormat::ByteSize(message_set));
475
476 string flat_data;
477 string stream_data;
478 string dynamic_data;
479 flat_data.resize(size);
480 stream_data.resize(size);
481
482 // Serialize to flat array
483 {
484 uint8* target = reinterpret_cast<uint8*>(string_as_array(&flat_data));
485 uint8* end = message_set.SerializeWithCachedSizesToArray(target);
486 EXPECT_EQ(size, end - target);
487 }
488
489 // Serialize to buffer
490 {
491 io::ArrayOutputStream array_stream(string_as_array(&stream_data), size, 1);
492 io::CodedOutputStream output_stream(&array_stream);
493 message_set.SerializeWithCachedSizes(&output_stream);
494 ASSERT_FALSE(output_stream.HadError());
495 }
496
497 // Serialize to buffer with WireFormat.
498 {
499 io::StringOutputStream string_stream(&dynamic_data);
500 io::CodedOutputStream output_stream(&string_stream);
501 WireFormat::SerializeWithCachedSizes(message_set, size, &output_stream);
502 ASSERT_FALSE(output_stream.HadError());
503 }
504
505 EXPECT_TRUE(flat_data == stream_data);
506 EXPECT_TRUE(flat_data == dynamic_data);
507 }
508
TEST(WireFormatTest,ParseMessageSet)509 TEST(WireFormatTest, ParseMessageSet) {
510 // Set up a RawMessageSet with two known messages and an unknown one.
511 unittest::RawMessageSet raw;
512
513 {
514 unittest::RawMessageSet::Item* item = raw.add_item();
515 item->set_type_id(
516 unittest::TestMessageSetExtension1::descriptor()->extension(0)->number());
517 unittest::TestMessageSetExtension1 message;
518 message.set_i(123);
519 message.SerializeToString(item->mutable_message());
520 }
521
522 {
523 unittest::RawMessageSet::Item* item = raw.add_item();
524 item->set_type_id(
525 unittest::TestMessageSetExtension2::descriptor()->extension(0)->number());
526 unittest::TestMessageSetExtension2 message;
527 message.set_str("foo");
528 message.SerializeToString(item->mutable_message());
529 }
530
531 {
532 unittest::RawMessageSet::Item* item = raw.add_item();
533 item->set_type_id(kUnknownTypeId);
534 item->set_message("bar");
535 }
536
537 string data;
538 ASSERT_TRUE(raw.SerializeToString(&data));
539
540 // Parse as a TestMessageSet and check the contents.
541 unittest::TestMessageSet message_set;
542 ASSERT_TRUE(message_set.ParseFromString(data));
543
544 EXPECT_EQ(123, message_set.GetExtension(
545 unittest::TestMessageSetExtension1::message_set_extension).i());
546 EXPECT_EQ("foo", message_set.GetExtension(
547 unittest::TestMessageSetExtension2::message_set_extension).str());
548
549 ASSERT_EQ(1, message_set.unknown_fields().field_count());
550 ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED,
551 message_set.unknown_fields().field(0).type());
552 EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited());
553
554 // Also parse using WireFormat.
555 unittest::TestMessageSet dynamic_message_set;
556 io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()),
557 data.size());
558 ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set));
559 EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString());
560 }
561
TEST(WireFormatTest,ParseMessageSetWithReverseTagOrder)562 TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) {
563 string data;
564 {
565 unittest::TestMessageSetExtension1 message;
566 message.set_i(123);
567 // Build a MessageSet manually with its message content put before its
568 // type_id.
569 io::StringOutputStream output_stream(&data);
570 io::CodedOutputStream coded_output(&output_stream);
571 coded_output.WriteTag(WireFormatLite::kMessageSetItemStartTag);
572 // Write the message content first.
573 WireFormatLite::WriteTag(WireFormatLite::kMessageSetMessageNumber,
574 WireFormatLite::WIRETYPE_LENGTH_DELIMITED,
575 &coded_output);
576 coded_output.WriteVarint32(message.ByteSize());
577 message.SerializeWithCachedSizes(&coded_output);
578 // Write the type id.
579 uint32 type_id = message.GetDescriptor()->extension(0)->number();
580 WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber,
581 type_id, &coded_output);
582 coded_output.WriteTag(WireFormatLite::kMessageSetItemEndTag);
583 }
584 {
585 unittest::TestMessageSet message_set;
586 ASSERT_TRUE(message_set.ParseFromString(data));
587
588 EXPECT_EQ(123, message_set.GetExtension(
589 unittest::TestMessageSetExtension1::message_set_extension).i());
590 }
591 {
592 // Test parse the message via Reflection.
593 unittest::TestMessageSet message_set;
594 io::CodedInputStream input(
595 reinterpret_cast<const uint8*>(data.data()), data.size());
596 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set));
597 EXPECT_TRUE(input.ConsumedEntireMessage());
598
599 EXPECT_EQ(123, message_set.GetExtension(
600 unittest::TestMessageSetExtension1::message_set_extension).i());
601 }
602 }
603
TEST(WireFormatTest,ParseBrokenMessageSet)604 TEST(WireFormatTest, ParseBrokenMessageSet) {
605 unittest::TestMessageSet message_set;
606 string input("goodbye"); // Invalid wire format data.
607 EXPECT_FALSE(message_set.ParseFromString(input));
608 }
609
TEST(WireFormatTest,RecursionLimit)610 TEST(WireFormatTest, RecursionLimit) {
611 unittest::TestRecursiveMessage message;
612 message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1);
613 string data;
614 message.SerializeToString(&data);
615
616 {
617 io::ArrayInputStream raw_input(data.data(), data.size());
618 io::CodedInputStream input(&raw_input);
619 input.SetRecursionLimit(4);
620 unittest::TestRecursiveMessage message2;
621 EXPECT_TRUE(message2.ParseFromCodedStream(&input));
622 }
623
624 {
625 io::ArrayInputStream raw_input(data.data(), data.size());
626 io::CodedInputStream input(&raw_input);
627 input.SetRecursionLimit(3);
628 unittest::TestRecursiveMessage message2;
629 EXPECT_FALSE(message2.ParseFromCodedStream(&input));
630 }
631 }
632
TEST(WireFormatTest,UnknownFieldRecursionLimit)633 TEST(WireFormatTest, UnknownFieldRecursionLimit) {
634 unittest::TestEmptyMessage message;
635 message.mutable_unknown_fields()
636 ->AddGroup(1234)
637 ->AddGroup(1234)
638 ->AddGroup(1234)
639 ->AddGroup(1234)
640 ->AddVarint(1234, 123);
641 string data;
642 message.SerializeToString(&data);
643
644 {
645 io::ArrayInputStream raw_input(data.data(), data.size());
646 io::CodedInputStream input(&raw_input);
647 input.SetRecursionLimit(4);
648 unittest::TestEmptyMessage message2;
649 EXPECT_TRUE(message2.ParseFromCodedStream(&input));
650 }
651
652 {
653 io::ArrayInputStream raw_input(data.data(), data.size());
654 io::CodedInputStream input(&raw_input);
655 input.SetRecursionLimit(3);
656 unittest::TestEmptyMessage message2;
657 EXPECT_FALSE(message2.ParseFromCodedStream(&input));
658 }
659 }
660
TEST(WireFormatTest,ZigZag)661 TEST(WireFormatTest, ZigZag) {
662 // avoid line-wrapping
663 #define LL(x) GOOGLE_LONGLONG(x)
664 #define ULL(x) GOOGLE_ULONGLONG(x)
665 #define ZigZagEncode32(x) WireFormatLite::ZigZagEncode32(x)
666 #define ZigZagDecode32(x) WireFormatLite::ZigZagDecode32(x)
667 #define ZigZagEncode64(x) WireFormatLite::ZigZagEncode64(x)
668 #define ZigZagDecode64(x) WireFormatLite::ZigZagDecode64(x)
669
670 EXPECT_EQ(0u, ZigZagEncode32( 0));
671 EXPECT_EQ(1u, ZigZagEncode32(-1));
672 EXPECT_EQ(2u, ZigZagEncode32( 1));
673 EXPECT_EQ(3u, ZigZagEncode32(-2));
674 EXPECT_EQ(0x7FFFFFFEu, ZigZagEncode32(0x3FFFFFFF));
675 EXPECT_EQ(0x7FFFFFFFu, ZigZagEncode32(0xC0000000));
676 EXPECT_EQ(0xFFFFFFFEu, ZigZagEncode32(0x7FFFFFFF));
677 EXPECT_EQ(0xFFFFFFFFu, ZigZagEncode32(0x80000000));
678
679 EXPECT_EQ( 0, ZigZagDecode32(0u));
680 EXPECT_EQ(-1, ZigZagDecode32(1u));
681 EXPECT_EQ( 1, ZigZagDecode32(2u));
682 EXPECT_EQ(-2, ZigZagDecode32(3u));
683 EXPECT_EQ(0x3FFFFFFF, ZigZagDecode32(0x7FFFFFFEu));
684 EXPECT_EQ(0xC0000000, ZigZagDecode32(0x7FFFFFFFu));
685 EXPECT_EQ(0x7FFFFFFF, ZigZagDecode32(0xFFFFFFFEu));
686 EXPECT_EQ(0x80000000, ZigZagDecode32(0xFFFFFFFFu));
687
688 EXPECT_EQ(0u, ZigZagEncode64( 0));
689 EXPECT_EQ(1u, ZigZagEncode64(-1));
690 EXPECT_EQ(2u, ZigZagEncode64( 1));
691 EXPECT_EQ(3u, ZigZagEncode64(-2));
692 EXPECT_EQ(ULL(0x000000007FFFFFFE), ZigZagEncode64(LL(0x000000003FFFFFFF)));
693 EXPECT_EQ(ULL(0x000000007FFFFFFF), ZigZagEncode64(LL(0xFFFFFFFFC0000000)));
694 EXPECT_EQ(ULL(0x00000000FFFFFFFE), ZigZagEncode64(LL(0x000000007FFFFFFF)));
695 EXPECT_EQ(ULL(0x00000000FFFFFFFF), ZigZagEncode64(LL(0xFFFFFFFF80000000)));
696 EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFE), ZigZagEncode64(LL(0x7FFFFFFFFFFFFFFF)));
697 EXPECT_EQ(ULL(0xFFFFFFFFFFFFFFFF), ZigZagEncode64(LL(0x8000000000000000)));
698
699 EXPECT_EQ( 0, ZigZagDecode64(0u));
700 EXPECT_EQ(-1, ZigZagDecode64(1u));
701 EXPECT_EQ( 1, ZigZagDecode64(2u));
702 EXPECT_EQ(-2, ZigZagDecode64(3u));
703 EXPECT_EQ(LL(0x000000003FFFFFFF), ZigZagDecode64(ULL(0x000000007FFFFFFE)));
704 EXPECT_EQ(LL(0xFFFFFFFFC0000000), ZigZagDecode64(ULL(0x000000007FFFFFFF)));
705 EXPECT_EQ(LL(0x000000007FFFFFFF), ZigZagDecode64(ULL(0x00000000FFFFFFFE)));
706 EXPECT_EQ(LL(0xFFFFFFFF80000000), ZigZagDecode64(ULL(0x00000000FFFFFFFF)));
707 EXPECT_EQ(LL(0x7FFFFFFFFFFFFFFF), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFE)));
708 EXPECT_EQ(LL(0x8000000000000000), ZigZagDecode64(ULL(0xFFFFFFFFFFFFFFFF)));
709
710 // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1)
711 // were chosen semi-randomly via keyboard bashing.
712 EXPECT_EQ( 0, ZigZagDecode32(ZigZagEncode32( 0)));
713 EXPECT_EQ( 1, ZigZagDecode32(ZigZagEncode32( 1)));
714 EXPECT_EQ( -1, ZigZagDecode32(ZigZagEncode32( -1)));
715 EXPECT_EQ(14927, ZigZagDecode32(ZigZagEncode32(14927)));
716 EXPECT_EQ(-3612, ZigZagDecode32(ZigZagEncode32(-3612)));
717
718 EXPECT_EQ( 0, ZigZagDecode64(ZigZagEncode64( 0)));
719 EXPECT_EQ( 1, ZigZagDecode64(ZigZagEncode64( 1)));
720 EXPECT_EQ( -1, ZigZagDecode64(ZigZagEncode64( -1)));
721 EXPECT_EQ(14927, ZigZagDecode64(ZigZagEncode64(14927)));
722 EXPECT_EQ(-3612, ZigZagDecode64(ZigZagEncode64(-3612)));
723
724 EXPECT_EQ(LL(856912304801416), ZigZagDecode64(ZigZagEncode64(
725 LL(856912304801416))));
726 EXPECT_EQ(LL(-75123905439571256), ZigZagDecode64(ZigZagEncode64(
727 LL(-75123905439571256))));
728 }
729
TEST(WireFormatTest,RepeatedScalarsDifferentTagSizes)730 TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) {
731 // At one point checks would trigger when parsing repeated fixed scalar
732 // fields.
733 protobuf_unittest::TestRepeatedScalarDifferentTagSizes msg1, msg2;
734 for (int i = 0; i < 100; ++i) {
735 msg1.add_repeated_fixed32(i);
736 msg1.add_repeated_int32(i);
737 msg1.add_repeated_fixed64(i);
738 msg1.add_repeated_int64(i);
739 msg1.add_repeated_float(i);
740 msg1.add_repeated_uint64(i);
741 }
742
743 // Make sure that we have a variety of tag sizes.
744 const google::protobuf::Descriptor* desc = msg1.GetDescriptor();
745 const google::protobuf::FieldDescriptor* field;
746 field = desc->FindFieldByName("repeated_fixed32");
747 ASSERT_TRUE(field != NULL);
748 ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
749 field = desc->FindFieldByName("repeated_int32");
750 ASSERT_TRUE(field != NULL);
751 ASSERT_EQ(1, WireFormat::TagSize(field->number(), field->type()));
752 field = desc->FindFieldByName("repeated_fixed64");
753 ASSERT_TRUE(field != NULL);
754 ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
755 field = desc->FindFieldByName("repeated_int64");
756 ASSERT_TRUE(field != NULL);
757 ASSERT_EQ(2, WireFormat::TagSize(field->number(), field->type()));
758 field = desc->FindFieldByName("repeated_float");
759 ASSERT_TRUE(field != NULL);
760 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
761 field = desc->FindFieldByName("repeated_uint64");
762 ASSERT_TRUE(field != NULL);
763 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type()));
764
765 EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString()));
766 EXPECT_EQ(msg1.DebugString(), msg2.DebugString());
767 }
768
TEST(WireFormatTest,CompatibleTypes)769 TEST(WireFormatTest, CompatibleTypes) {
770 const int64 data = 0x100000000;
771 unittest::Int64Message msg1;
772 msg1.set_data(data);
773 string serialized;
774 msg1.SerializeToString(&serialized);
775
776 // Test int64 is compatible with bool
777 unittest::BoolMessage msg2;
778 ASSERT_TRUE(msg2.ParseFromString(serialized));
779 ASSERT_EQ(static_cast<bool>(data), msg2.data());
780
781 // Test int64 is compatible with uint64
782 unittest::Uint64Message msg3;
783 ASSERT_TRUE(msg3.ParseFromString(serialized));
784 ASSERT_EQ(static_cast<uint64>(data), msg3.data());
785
786 // Test int64 is compatible with int32
787 unittest::Int32Message msg4;
788 ASSERT_TRUE(msg4.ParseFromString(serialized));
789 ASSERT_EQ(static_cast<int32>(data), msg4.data());
790
791 // Test int64 is compatible with uint32
792 unittest::Uint32Message msg5;
793 ASSERT_TRUE(msg5.ParseFromString(serialized));
794 ASSERT_EQ(static_cast<uint32>(data), msg5.data());
795 }
796
797 class WireFormatInvalidInputTest : public testing::Test {
798 protected:
799 // Make a serialized TestAllTypes in which the field optional_nested_message
800 // contains exactly the given bytes, which may be invalid.
MakeInvalidEmbeddedMessage(const char * bytes,int size)801 string MakeInvalidEmbeddedMessage(const char* bytes, int size) {
802 const FieldDescriptor* field =
803 unittest::TestAllTypes::descriptor()->FindFieldByName(
804 "optional_nested_message");
805 GOOGLE_CHECK(field != NULL);
806
807 string result;
808
809 {
810 io::StringOutputStream raw_output(&result);
811 io::CodedOutputStream output(&raw_output);
812
813 WireFormatLite::WriteBytes(field->number(), string(bytes, size), &output);
814 }
815
816 return result;
817 }
818
819 // Make a serialized TestAllTypes in which the field optionalgroup
820 // contains exactly the given bytes -- which may be invalid -- and
821 // possibly no end tag.
MakeInvalidGroup(const char * bytes,int size,bool include_end_tag)822 string MakeInvalidGroup(const char* bytes, int size, bool include_end_tag) {
823 const FieldDescriptor* field =
824 unittest::TestAllTypes::descriptor()->FindFieldByName(
825 "optionalgroup");
826 GOOGLE_CHECK(field != NULL);
827
828 string result;
829
830 {
831 io::StringOutputStream raw_output(&result);
832 io::CodedOutputStream output(&raw_output);
833
834 output.WriteVarint32(WireFormat::MakeTag(field));
835 output.WriteString(string(bytes, size));
836 if (include_end_tag) {
837 output.WriteVarint32(WireFormatLite::MakeTag(
838 field->number(), WireFormatLite::WIRETYPE_END_GROUP));
839 }
840 }
841
842 return result;
843 }
844 };
845
TEST_F(WireFormatInvalidInputTest,InvalidSubMessage)846 TEST_F(WireFormatInvalidInputTest, InvalidSubMessage) {
847 unittest::TestAllTypes message;
848
849 // Control case.
850 EXPECT_TRUE(message.ParseFromString(MakeInvalidEmbeddedMessage("", 0)));
851
852 // The byte is a valid varint, but not a valid tag (zero).
853 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\0", 1)));
854
855 // The byte is a malformed varint.
856 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\200", 1)));
857
858 // The byte is an endgroup tag, but we aren't parsing a group.
859 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\014", 1)));
860
861 // The byte is a valid varint but not a valid tag (bad wire type).
862 EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\017", 1)));
863 }
864
TEST_F(WireFormatInvalidInputTest,InvalidGroup)865 TEST_F(WireFormatInvalidInputTest, InvalidGroup) {
866 unittest::TestAllTypes message;
867
868 // Control case.
869 EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
870
871 // Missing end tag. Groups cannot end at EOF.
872 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
873
874 // The byte is a valid varint, but not a valid tag (zero).
875 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
876
877 // The byte is a malformed varint.
878 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
879
880 // The byte is an endgroup tag, but not the right one for this group.
881 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
882
883 // The byte is a valid varint but not a valid tag (bad wire type).
884 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
885 }
886
TEST_F(WireFormatInvalidInputTest,InvalidUnknownGroup)887 TEST_F(WireFormatInvalidInputTest, InvalidUnknownGroup) {
888 // Use TestEmptyMessage so that the group made by MakeInvalidGroup will not
889 // be a known tag number.
890 unittest::TestEmptyMessage message;
891
892 // Control case.
893 EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
894
895 // Missing end tag. Groups cannot end at EOF.
896 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("", 0, false)));
897
898 // The byte is a valid varint, but not a valid tag (zero).
899 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\0", 1, false)));
900
901 // The byte is a malformed varint.
902 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\200", 1, false)));
903
904 // The byte is an endgroup tag, but not the right one for this group.
905 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\014", 1, false)));
906
907 // The byte is a valid varint but not a valid tag (bad wire type).
908 EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
909 }
910
TEST_F(WireFormatInvalidInputTest,InvalidStringInUnknownGroup)911 TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) {
912 // Test a bug fix: SkipMessage should fail if the message contains a string
913 // whose length would extend beyond the message end.
914
915 unittest::TestAllTypes message;
916 message.set_optional_string("foo foo foo foo");
917 string data;
918 message.SerializeToString(&data);
919
920 // Chop some bytes off the end.
921 data.resize(data.size() - 4);
922
923 // Try to skip it. Note that the bug was only present when parsing to an
924 // UnknownFieldSet.
925 io::ArrayInputStream raw_input(data.data(), data.size());
926 io::CodedInputStream coded_input(&raw_input);
927 UnknownFieldSet unknown_fields;
928 EXPECT_FALSE(WireFormat::SkipMessage(&coded_input, &unknown_fields));
929 }
930
931 // Test differences between string and bytes.
932 // Value of a string type must be valid UTF-8 string. When UTF-8
933 // validation is enabled (GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED):
934 // WriteInvalidUTF8String: see error message.
935 // ReadInvalidUTF8String: see error message.
936 // WriteValidUTF8String: fine.
937 // ReadValidUTF8String: fine.
938 // WriteAnyBytes: fine.
939 // ReadAnyBytes: fine.
940 const char * kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0";
941 // This used to be "Valid UTF-8: \x01\x02\u8C37\u6B4C", but MSVC seems to
942 // interpret \u differently from GCC.
943 const char * kValidUTF8String = "Valid UTF-8: \x01\x02\350\260\267\346\255\214";
944
945 template<typename T>
WriteMessage(const char * value,T * message,string * wire_buffer)946 bool WriteMessage(const char *value, T *message, string *wire_buffer) {
947 message->set_data(value);
948 wire_buffer->clear();
949 message->AppendToString(wire_buffer);
950 return (wire_buffer->size() > 0);
951 }
952
953 template<typename T>
ReadMessage(const string & wire_buffer,T * message)954 bool ReadMessage(const string &wire_buffer, T *message) {
955 return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
956 }
957
StartsWith(const string & s,const string & prefix)958 bool StartsWith(const string& s, const string& prefix) {
959 return s.substr(0, prefix.length()) == prefix;
960 }
961
962 class Utf8ValidationTest : public ::testing::Test {
963 protected:
Utf8ValidationTest()964 Utf8ValidationTest() {}
~Utf8ValidationTest()965 virtual ~Utf8ValidationTest() {}
SetUp()966 virtual void SetUp() {
967 }
968
969 };
970
TEST_F(Utf8ValidationTest,WriteInvalidUTF8String)971 TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) {
972 string wire_buffer;
973 protobuf_unittest::OneString input;
974 vector<string> errors;
975 {
976 ScopedMemoryLog log;
977 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
978 errors = log.GetMessages(ERROR);
979 }
980 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
981 ASSERT_EQ(1, errors.size());
982 EXPECT_TRUE(StartsWith(errors[0],
983 "String field 'data' contains invalid UTF-8 data when "
984 "serializing a protocol buffer. Use the "
985 "'bytes' type if you intend to send raw bytes."));
986 #else
987 ASSERT_EQ(0, errors.size());
988 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
989 }
990
991
TEST_F(Utf8ValidationTest,ReadInvalidUTF8String)992 TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) {
993 string wire_buffer;
994 protobuf_unittest::OneString input;
995 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
996 protobuf_unittest::OneString output;
997 vector<string> errors;
998 {
999 ScopedMemoryLog log;
1000 ReadMessage(wire_buffer, &output);
1001 errors = log.GetMessages(ERROR);
1002 }
1003 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1004 ASSERT_EQ(1, errors.size());
1005 EXPECT_TRUE(StartsWith(errors[0],
1006 "String field 'data' contains invalid UTF-8 data when "
1007 "parsing a protocol buffer. Use the "
1008 "'bytes' type if you intend to send raw bytes."));
1009
1010 #else
1011 ASSERT_EQ(0, errors.size());
1012 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1013 }
1014
1015
TEST_F(Utf8ValidationTest,WriteValidUTF8String)1016 TEST_F(Utf8ValidationTest, WriteValidUTF8String) {
1017 string wire_buffer;
1018 protobuf_unittest::OneString input;
1019 vector<string> errors;
1020 {
1021 ScopedMemoryLog log;
1022 WriteMessage(kValidUTF8String, &input, &wire_buffer);
1023 errors = log.GetMessages(ERROR);
1024 }
1025 ASSERT_EQ(0, errors.size());
1026 }
1027
TEST_F(Utf8ValidationTest,ReadValidUTF8String)1028 TEST_F(Utf8ValidationTest, ReadValidUTF8String) {
1029 string wire_buffer;
1030 protobuf_unittest::OneString input;
1031 WriteMessage(kValidUTF8String, &input, &wire_buffer);
1032 protobuf_unittest::OneString output;
1033 vector<string> errors;
1034 {
1035 ScopedMemoryLog log;
1036 ReadMessage(wire_buffer, &output);
1037 errors = log.GetMessages(ERROR);
1038 }
1039 ASSERT_EQ(0, errors.size());
1040 EXPECT_EQ(input.data(), output.data());
1041 }
1042
1043 // Bytes: anything can pass as bytes, use invalid UTF-8 string to test
TEST_F(Utf8ValidationTest,WriteArbitraryBytes)1044 TEST_F(Utf8ValidationTest, WriteArbitraryBytes) {
1045 string wire_buffer;
1046 protobuf_unittest::OneBytes input;
1047 vector<string> errors;
1048 {
1049 ScopedMemoryLog log;
1050 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1051 errors = log.GetMessages(ERROR);
1052 }
1053 ASSERT_EQ(0, errors.size());
1054 }
1055
TEST_F(Utf8ValidationTest,ReadArbitraryBytes)1056 TEST_F(Utf8ValidationTest, ReadArbitraryBytes) {
1057 string wire_buffer;
1058 protobuf_unittest::OneBytes input;
1059 WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
1060 protobuf_unittest::OneBytes output;
1061 vector<string> errors;
1062 {
1063 ScopedMemoryLog log;
1064 ReadMessage(wire_buffer, &output);
1065 errors = log.GetMessages(ERROR);
1066 }
1067 ASSERT_EQ(0, errors.size());
1068 EXPECT_EQ(input.data(), output.data());
1069 }
1070
TEST_F(Utf8ValidationTest,ParseRepeatedString)1071 TEST_F(Utf8ValidationTest, ParseRepeatedString) {
1072 protobuf_unittest::MoreBytes input;
1073 input.add_data(kValidUTF8String);
1074 input.add_data(kInvalidUTF8String);
1075 input.add_data(kInvalidUTF8String);
1076 string wire_buffer = input.SerializeAsString();
1077
1078 protobuf_unittest::MoreString output;
1079 vector<string> errors;
1080 {
1081 ScopedMemoryLog log;
1082 ReadMessage(wire_buffer, &output);
1083 errors = log.GetMessages(ERROR);
1084 }
1085 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1086 ASSERT_EQ(2, errors.size());
1087 #else
1088 ASSERT_EQ(0, errors.size());
1089 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1090 EXPECT_EQ(wire_buffer, output.SerializeAsString());
1091 }
1092
1093 // Test the old VerifyUTF8String() function, which may still be called by old
1094 // generated code.
TEST_F(Utf8ValidationTest,OldVerifyUTF8String)1095 TEST_F(Utf8ValidationTest, OldVerifyUTF8String) {
1096 string data(kInvalidUTF8String);
1097
1098 vector<string> errors;
1099 {
1100 ScopedMemoryLog log;
1101 WireFormat::VerifyUTF8String(data.data(), data.size(),
1102 WireFormat::SERIALIZE);
1103 errors = log.GetMessages(ERROR);
1104 }
1105 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
1106 ASSERT_EQ(1, errors.size());
1107 EXPECT_TRUE(StartsWith(errors[0],
1108 "String field contains invalid UTF-8 data when "
1109 "serializing a protocol buffer. Use the "
1110 "'bytes' type if you intend to send raw bytes."));
1111 #else
1112 ASSERT_EQ(0, errors.size());
1113 #endif
1114 }
1115
1116
1117 } // namespace
1118 } // namespace internal
1119 } // namespace protobuf
1120 } // namespace google
1121