| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | 34 |
| 35 #include <google/protobuf/wire_format.h> | 35 #include <google/protobuf/wire_format.h> |
| 36 #include <google/protobuf/wire_format_lite_inl.h> | 36 #include <google/protobuf/wire_format_lite_inl.h> |
| 37 #include <google/protobuf/descriptor.h> | 37 #include <google/protobuf/descriptor.h> |
| 38 #include <google/protobuf/io/zero_copy_stream_impl.h> | 38 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 39 #include <google/protobuf/io/coded_stream.h> | 39 #include <google/protobuf/io/coded_stream.h> |
| 40 #include <google/protobuf/unittest.pb.h> | 40 #include <google/protobuf/unittest.pb.h> |
| 41 #include <google/protobuf/unittest_proto3_arena.pb.h> |
| 41 #include <google/protobuf/unittest_mset.pb.h> | 42 #include <google/protobuf/unittest_mset.pb.h> |
| 43 #include <google/protobuf/unittest_mset_wire_format.pb.h> |
| 42 #include <google/protobuf/test_util.h> | 44 #include <google/protobuf/test_util.h> |
| 43 | 45 |
| 46 #include <google/protobuf/stubs/logging.h> |
| 44 #include <google/protobuf/stubs/common.h> | 47 #include <google/protobuf/stubs/common.h> |
| 45 #include <google/protobuf/testing/googletest.h> | 48 #include <google/protobuf/testing/googletest.h> |
| 46 #include <gtest/gtest.h> | 49 #include <gtest/gtest.h> |
| 47 #include <google/protobuf/stubs/stl_util.h> | 50 #include <google/protobuf/stubs/stl_util.h> |
| 48 | 51 |
| 49 namespace google { | 52 namespace google { |
| 50 namespace protobuf { | 53 namespace protobuf { |
| 51 namespace internal { | 54 namespace internal { |
| 52 namespace { | 55 namespace { |
| 53 | 56 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 171 |
| 169 // Parse using WireFormat. | 172 // Parse using WireFormat. |
| 170 io::ArrayInputStream raw_input(data.data(), data.size()); | 173 io::ArrayInputStream raw_input(data.data(), data.size()); |
| 171 io::CodedInputStream input(&raw_input); | 174 io::CodedInputStream input(&raw_input); |
| 172 WireFormat::ParseAndMergePartial(&input, &dest); | 175 WireFormat::ParseAndMergePartial(&input, &dest); |
| 173 | 176 |
| 174 // Check. | 177 // Check. |
| 175 TestUtil::ExpectPackedExtensionsSet(dest); | 178 TestUtil::ExpectPackedExtensionsSet(dest); |
| 176 } | 179 } |
| 177 | 180 |
| 181 TEST(WireFormatTest, ParseOneof) { |
| 182 unittest::TestOneof2 source, dest; |
| 183 string data; |
| 184 |
| 185 // Serialize using the generated code. |
| 186 TestUtil::SetOneof1(&source); |
| 187 source.SerializeToString(&data); |
| 188 |
| 189 // Parse using WireFormat. |
| 190 io::ArrayInputStream raw_input(data.data(), data.size()); |
| 191 io::CodedInputStream input(&raw_input); |
| 192 WireFormat::ParseAndMergePartial(&input, &dest); |
| 193 |
| 194 // Check. |
| 195 TestUtil::ExpectOneofSet1(dest); |
| 196 } |
| 197 |
| 198 TEST(WireFormatTest, OneofOnlySetLast) { |
| 199 unittest::TestOneofBackwardsCompatible source; |
| 200 unittest::TestOneof oneof_dest; |
| 201 string data; |
| 202 |
| 203 // Set two fields |
| 204 source.set_foo_int(100); |
| 205 source.set_foo_string("101"); |
| 206 |
| 207 // Serialize and parse to oneof message. |
| 208 source.SerializeToString(&data); |
| 209 io::ArrayInputStream raw_input(data.data(), data.size()); |
| 210 io::CodedInputStream input(&raw_input); |
| 211 WireFormat::ParseAndMergePartial(&input, &oneof_dest); |
| 212 |
| 213 // Only the last field is set. |
| 214 EXPECT_FALSE(oneof_dest.has_foo_int()); |
| 215 EXPECT_TRUE(oneof_dest.has_foo_string()); |
| 216 } |
| 217 |
| 178 TEST(WireFormatTest, ByteSize) { | 218 TEST(WireFormatTest, ByteSize) { |
| 179 unittest::TestAllTypes message; | 219 unittest::TestAllTypes message; |
| 180 TestUtil::SetAllFields(&message); | 220 TestUtil::SetAllFields(&message); |
| 181 | 221 |
| 182 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message)); | 222 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message)); |
| 183 message.Clear(); | 223 message.Clear(); |
| 184 EXPECT_EQ(0, message.ByteSize()); | 224 EXPECT_EQ(0, message.ByteSize()); |
| 185 EXPECT_EQ(0, WireFormat::ByteSize(message)); | 225 EXPECT_EQ(0, WireFormat::ByteSize(message)); |
| 186 } | 226 } |
| 187 | 227 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 210 unittest::TestPackedExtensions message; | 250 unittest::TestPackedExtensions message; |
| 211 TestUtil::SetPackedExtensions(&message); | 251 TestUtil::SetPackedExtensions(&message); |
| 212 | 252 |
| 213 EXPECT_EQ(message.ByteSize(), | 253 EXPECT_EQ(message.ByteSize(), |
| 214 WireFormat::ByteSize(message)); | 254 WireFormat::ByteSize(message)); |
| 215 message.Clear(); | 255 message.Clear(); |
| 216 EXPECT_EQ(0, message.ByteSize()); | 256 EXPECT_EQ(0, message.ByteSize()); |
| 217 EXPECT_EQ(0, WireFormat::ByteSize(message)); | 257 EXPECT_EQ(0, WireFormat::ByteSize(message)); |
| 218 } | 258 } |
| 219 | 259 |
| 260 TEST(WireFormatTest, ByteSizeOneof) { |
| 261 unittest::TestOneof2 message; |
| 262 TestUtil::SetOneof1(&message); |
| 263 |
| 264 EXPECT_EQ(message.ByteSize(), |
| 265 WireFormat::ByteSize(message)); |
| 266 message.Clear(); |
| 267 |
| 268 EXPECT_EQ(0, message.ByteSize()); |
| 269 EXPECT_EQ(0, WireFormat::ByteSize(message)); |
| 270 } |
| 271 |
| 220 TEST(WireFormatTest, Serialize) { | 272 TEST(WireFormatTest, Serialize) { |
| 221 unittest::TestAllTypes message; | 273 unittest::TestAllTypes message; |
| 222 string generated_data; | 274 string generated_data; |
| 223 string dynamic_data; | 275 string dynamic_data; |
| 224 | 276 |
| 225 TestUtil::SetAllFields(&message); | 277 TestUtil::SetAllFields(&message); |
| 226 int size = message.ByteSize(); | 278 int size = message.ByteSize(); |
| 227 | 279 |
| 228 // Serialize using the generated code. | 280 // Serialize using the generated code. |
| 229 { | 281 { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // Should be the same. | 356 // Should be the same. |
| 305 // Don't use EXPECT_EQ here because we're comparing raw binary data and | 357 // Don't use EXPECT_EQ here because we're comparing raw binary data and |
| 306 // we really don't want it dumped to stdout on failure. | 358 // we really don't want it dumped to stdout on failure. |
| 307 EXPECT_TRUE(dynamic_data == generated_data); | 359 EXPECT_TRUE(dynamic_data == generated_data); |
| 308 | 360 |
| 309 // Should output in canonical order. | 361 // Should output in canonical order. |
| 310 TestUtil::ExpectAllFieldsAndExtensionsInOrder(dynamic_data); | 362 TestUtil::ExpectAllFieldsAndExtensionsInOrder(dynamic_data); |
| 311 TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data); | 363 TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data); |
| 312 } | 364 } |
| 313 | 365 |
| 366 TEST(WireFormatTest, SerializeOneof) { |
| 367 unittest::TestOneof2 message; |
| 368 string generated_data; |
| 369 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 |
| 314 TEST(WireFormatTest, ParseMultipleExtensionRanges) { | 396 TEST(WireFormatTest, ParseMultipleExtensionRanges) { |
| 315 // Make sure we can parse a message that contains multiple extensions ranges. | 397 // Make sure we can parse a message that contains multiple extensions ranges. |
| 316 unittest::TestFieldOrderings source; | 398 unittest::TestFieldOrderings source; |
| 317 string data; | 399 string data; |
| 318 | 400 |
| 319 TestUtil::SetAllFieldsAndExtensions(&source); | 401 TestUtil::SetAllFieldsAndExtensions(&source); |
| 320 source.SerializeToString(&data); | 402 source.SerializeToString(&data); |
| 321 | 403 |
| 322 { | 404 { |
| 323 unittest::TestFieldOrderings dest; | 405 unittest::TestFieldOrderings dest; |
| 324 EXPECT_TRUE(dest.ParseFromString(data)); | 406 EXPECT_TRUE(dest.ParseFromString(data)); |
| 325 EXPECT_EQ(source.DebugString(), dest.DebugString()); | 407 EXPECT_EQ(source.DebugString(), dest.DebugString()); |
| 326 } | 408 } |
| 327 | 409 |
| 328 // Also test using reflection-based parsing. | 410 // Also test using reflection-based parsing. |
| 329 { | 411 { |
| 330 unittest::TestFieldOrderings dest; | 412 unittest::TestFieldOrderings dest; |
| 331 io::ArrayInputStream raw_input(data.data(), data.size()); | 413 io::ArrayInputStream raw_input(data.data(), data.size()); |
| 332 io::CodedInputStream coded_input(&raw_input); | 414 io::CodedInputStream coded_input(&raw_input); |
| 333 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest)); | 415 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest)); |
| 334 EXPECT_EQ(source.DebugString(), dest.DebugString()); | 416 EXPECT_EQ(source.DebugString(), dest.DebugString()); |
| 335 } | 417 } |
| 336 } | 418 } |
| 337 | 419 |
| 338 const int kUnknownTypeId = 1550055; | 420 const int kUnknownTypeId = 1550055; |
| 339 | 421 |
| 340 TEST(WireFormatTest, SerializeMessageSet) { | 422 TEST(WireFormatTest, SerializeMessageSet) { |
| 341 // Set up a TestMessageSet with two known messages and an unknown one. | 423 // Set up a TestMessageSet with two known messages and an unknown one. |
| 342 unittest::TestMessageSet message_set; | 424 proto2_wireformat_unittest::TestMessageSet message_set; |
| 343 message_set.MutableExtension( | 425 message_set.MutableExtension( |
| 344 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); | 426 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); |
| 345 message_set.MutableExtension( | 427 message_set.MutableExtension( |
| 346 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo"); | 428 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo"); |
| 347 message_set.mutable_unknown_fields()->AddLengthDelimited( | 429 message_set.mutable_unknown_fields()->AddLengthDelimited( |
| 348 kUnknownTypeId, "bar"); | 430 kUnknownTypeId, "bar"); |
| 349 | 431 |
| 350 string data; | 432 string data; |
| 351 ASSERT_TRUE(message_set.SerializeToString(&data)); | 433 ASSERT_TRUE(message_set.SerializeToString(&data)); |
| 352 | 434 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 375 | 457 |
| 376 EXPECT_EQ("bar", raw.item(2).message()); | 458 EXPECT_EQ("bar", raw.item(2).message()); |
| 377 } | 459 } |
| 378 | 460 |
| 379 TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) { | 461 TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) { |
| 380 // Serialize a MessageSet to a stream and to a flat array using generated | 462 // Serialize a MessageSet to a stream and to a flat array using generated |
| 381 // code, and also using WireFormat, and check that the results are equal. | 463 // code, and also using WireFormat, and check that the results are equal. |
| 382 // Set up a TestMessageSet with two known messages and an unknown one, as | 464 // Set up a TestMessageSet with two known messages and an unknown one, as |
| 383 // above. | 465 // above. |
| 384 | 466 |
| 385 unittest::TestMessageSet message_set; | 467 proto2_wireformat_unittest::TestMessageSet message_set; |
| 386 message_set.MutableExtension( | 468 message_set.MutableExtension( |
| 387 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); | 469 unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); |
| 388 message_set.MutableExtension( | 470 message_set.MutableExtension( |
| 389 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo"); | 471 unittest::TestMessageSetExtension2::message_set_extension)->set_str("foo"); |
| 390 message_set.mutable_unknown_fields()->AddLengthDelimited( | 472 message_set.mutable_unknown_fields()->AddLengthDelimited( |
| 391 kUnknownTypeId, "bar"); | 473 kUnknownTypeId, "bar"); |
| 392 | 474 |
| 393 int size = message_set.ByteSize(); | 475 int size = message_set.ByteSize(); |
| 394 EXPECT_EQ(size, message_set.GetCachedSize()); | 476 EXPECT_EQ(size, message_set.GetCachedSize()); |
| 395 ASSERT_EQ(size, WireFormat::ByteSize(message_set)); | 477 ASSERT_EQ(size, WireFormat::ByteSize(message_set)); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 { | 534 { |
| 453 unittest::RawMessageSet::Item* item = raw.add_item(); | 535 unittest::RawMessageSet::Item* item = raw.add_item(); |
| 454 item->set_type_id(kUnknownTypeId); | 536 item->set_type_id(kUnknownTypeId); |
| 455 item->set_message("bar"); | 537 item->set_message("bar"); |
| 456 } | 538 } |
| 457 | 539 |
| 458 string data; | 540 string data; |
| 459 ASSERT_TRUE(raw.SerializeToString(&data)); | 541 ASSERT_TRUE(raw.SerializeToString(&data)); |
| 460 | 542 |
| 461 // Parse as a TestMessageSet and check the contents. | 543 // Parse as a TestMessageSet and check the contents. |
| 462 unittest::TestMessageSet message_set; | 544 proto2_wireformat_unittest::TestMessageSet message_set; |
| 463 ASSERT_TRUE(message_set.ParseFromString(data)); | 545 ASSERT_TRUE(message_set.ParseFromString(data)); |
| 464 | 546 |
| 465 EXPECT_EQ(123, message_set.GetExtension( | 547 EXPECT_EQ(123, message_set.GetExtension( |
| 466 unittest::TestMessageSetExtension1::message_set_extension).i()); | 548 unittest::TestMessageSetExtension1::message_set_extension).i()); |
| 467 EXPECT_EQ("foo", message_set.GetExtension( | 549 EXPECT_EQ("foo", message_set.GetExtension( |
| 468 unittest::TestMessageSetExtension2::message_set_extension).str()); | 550 unittest::TestMessageSetExtension2::message_set_extension).str()); |
| 469 | 551 |
| 470 ASSERT_EQ(1, message_set.unknown_fields().field_count()); | 552 ASSERT_EQ(1, message_set.unknown_fields().field_count()); |
| 471 ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED, | 553 ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED, |
| 472 message_set.unknown_fields().field(0).type()); | 554 message_set.unknown_fields().field(0).type()); |
| 473 EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited()); | 555 EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited()); |
| 474 | 556 |
| 475 // Also parse using WireFormat. | 557 // Also parse using WireFormat. |
| 476 unittest::TestMessageSet dynamic_message_set; | 558 proto2_wireformat_unittest::TestMessageSet dynamic_message_set; |
| 477 io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()), | 559 io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()), |
| 478 data.size()); | 560 data.size()); |
| 479 ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set)); | 561 ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set)); |
| 480 EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString()); | 562 EXPECT_EQ(message_set.DebugString(), dynamic_message_set.DebugString()); |
| 481 } | 563 } |
| 482 | 564 |
| 483 TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) { | 565 TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) { |
| 484 string data; | 566 string data; |
| 485 { | 567 { |
| 486 unittest::TestMessageSetExtension1 message; | 568 unittest::TestMessageSetExtension1 message; |
| 487 message.set_i(123); | 569 message.set_i(123); |
| 488 // Build a MessageSet manually with its message content put before its | 570 // Build a MessageSet manually with its message content put before its |
| 489 // type_id. | 571 // type_id. |
| 490 io::StringOutputStream output_stream(&data); | 572 io::StringOutputStream output_stream(&data); |
| 491 io::CodedOutputStream coded_output(&output_stream); | 573 io::CodedOutputStream coded_output(&output_stream); |
| 492 coded_output.WriteTag(WireFormatLite::kMessageSetItemStartTag); | 574 coded_output.WriteTag(WireFormatLite::kMessageSetItemStartTag); |
| 493 // Write the message content first. | 575 // Write the message content first. |
| 494 WireFormatLite::WriteTag(WireFormatLite::kMessageSetMessageNumber, | 576 WireFormatLite::WriteTag(WireFormatLite::kMessageSetMessageNumber, |
| 495 WireFormatLite::WIRETYPE_LENGTH_DELIMITED, | 577 WireFormatLite::WIRETYPE_LENGTH_DELIMITED, |
| 496 &coded_output); | 578 &coded_output); |
| 497 coded_output.WriteVarint32(message.ByteSize()); | 579 coded_output.WriteVarint32(message.ByteSize()); |
| 498 message.SerializeWithCachedSizes(&coded_output); | 580 message.SerializeWithCachedSizes(&coded_output); |
| 499 // Write the type id. | 581 // Write the type id. |
| 500 uint32 type_id = message.GetDescriptor()->extension(0)->number(); | 582 uint32 type_id = message.GetDescriptor()->extension(0)->number(); |
| 501 WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber, | 583 WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber, |
| 502 type_id, &coded_output); | 584 type_id, &coded_output); |
| 503 coded_output.WriteTag(WireFormatLite::kMessageSetItemEndTag); | 585 coded_output.WriteTag(WireFormatLite::kMessageSetItemEndTag); |
| 504 } | 586 } |
| 505 { | 587 { |
| 506 unittest::TestMessageSet message_set; | 588 proto2_wireformat_unittest::TestMessageSet message_set; |
| 507 ASSERT_TRUE(message_set.ParseFromString(data)); | 589 ASSERT_TRUE(message_set.ParseFromString(data)); |
| 508 | 590 |
| 509 EXPECT_EQ(123, message_set.GetExtension( | 591 EXPECT_EQ(123, message_set.GetExtension( |
| 510 unittest::TestMessageSetExtension1::message_set_extension).i()); | 592 unittest::TestMessageSetExtension1::message_set_extension).i()); |
| 511 } | 593 } |
| 512 { | 594 { |
| 513 // Test parse the message via Reflection. | 595 // Test parse the message via Reflection. |
| 514 unittest::TestMessageSet message_set; | 596 proto2_wireformat_unittest::TestMessageSet message_set; |
| 515 io::CodedInputStream input( | 597 io::CodedInputStream input( |
| 516 reinterpret_cast<const uint8*>(data.data()), data.size()); | 598 reinterpret_cast<const uint8*>(data.data()), data.size()); |
| 517 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set)); | 599 EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set)); |
| 518 EXPECT_TRUE(input.ConsumedEntireMessage()); | 600 EXPECT_TRUE(input.ConsumedEntireMessage()); |
| 519 | 601 |
| 520 EXPECT_EQ(123, message_set.GetExtension( | 602 EXPECT_EQ(123, message_set.GetExtension( |
| 521 unittest::TestMessageSetExtension1::message_set_extension).i()); | 603 unittest::TestMessageSetExtension1::message_set_extension).i()); |
| 522 } | 604 } |
| 523 } | 605 } |
| 524 | 606 |
| 525 TEST(WireFormatTest, ParseBrokenMessageSet) { | 607 TEST(WireFormatTest, ParseBrokenMessageSet) { |
| 526 unittest::TestMessageSet message_set; | 608 proto2_wireformat_unittest::TestMessageSet message_set; |
| 527 string input("goodbye"); // Invalid wire format data. | 609 string input("goodbye"); // Invalid wire format data. |
| 528 EXPECT_FALSE(message_set.ParseFromString(input)); | 610 EXPECT_FALSE(message_set.ParseFromString(input)); |
| 529 } | 611 } |
| 530 | 612 |
| 531 TEST(WireFormatTest, RecursionLimit) { | 613 TEST(WireFormatTest, RecursionLimit) { |
| 532 unittest::TestRecursiveMessage message; | 614 unittest::TestRecursiveMessage message; |
| 533 message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1); | 615 message.mutable_a()->mutable_a()->mutable_a()->mutable_a()->set_i(1); |
| 534 string data; | 616 string data; |
| 535 message.SerializeToString(&data); | 617 message.SerializeToString(&data); |
| 536 | 618 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 ASSERT_TRUE(field != NULL); | 762 ASSERT_TRUE(field != NULL); |
| 681 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type())); | 763 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type())); |
| 682 field = desc->FindFieldByName("repeated_uint64"); | 764 field = desc->FindFieldByName("repeated_uint64"); |
| 683 ASSERT_TRUE(field != NULL); | 765 ASSERT_TRUE(field != NULL); |
| 684 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type())); | 766 ASSERT_EQ(3, WireFormat::TagSize(field->number(), field->type())); |
| 685 | 767 |
| 686 EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString())); | 768 EXPECT_TRUE(msg2.ParseFromString(msg1.SerializeAsString())); |
| 687 EXPECT_EQ(msg1.DebugString(), msg2.DebugString()); | 769 EXPECT_EQ(msg1.DebugString(), msg2.DebugString()); |
| 688 } | 770 } |
| 689 | 771 |
| 772 TEST(WireFormatTest, CompatibleTypes) { |
| 773 const int64 data = 0x100000000LL; |
| 774 unittest::Int64Message msg1; |
| 775 msg1.set_data(data); |
| 776 string serialized; |
| 777 msg1.SerializeToString(&serialized); |
| 778 |
| 779 // Test int64 is compatible with bool |
| 780 unittest::BoolMessage msg2; |
| 781 ASSERT_TRUE(msg2.ParseFromString(serialized)); |
| 782 ASSERT_EQ(static_cast<bool>(data), msg2.data()); |
| 783 |
| 784 // Test int64 is compatible with uint64 |
| 785 unittest::Uint64Message msg3; |
| 786 ASSERT_TRUE(msg3.ParseFromString(serialized)); |
| 787 ASSERT_EQ(static_cast<uint64>(data), msg3.data()); |
| 788 |
| 789 // Test int64 is compatible with int32 |
| 790 unittest::Int32Message msg4; |
| 791 ASSERT_TRUE(msg4.ParseFromString(serialized)); |
| 792 ASSERT_EQ(static_cast<int32>(data), msg4.data()); |
| 793 |
| 794 // Test int64 is compatible with uint32 |
| 795 unittest::Uint32Message msg5; |
| 796 ASSERT_TRUE(msg5.ParseFromString(serialized)); |
| 797 ASSERT_EQ(static_cast<uint32>(data), msg5.data()); |
| 798 } |
| 799 |
| 800 class Proto3PrimitiveRepeatedWireFormatTest : public ::testing::Test { |
| 801 protected: |
| 802 Proto3PrimitiveRepeatedWireFormatTest() |
| 803 : packedTestAllTypes_( |
| 804 "\xFA\x01\x01\x01" |
| 805 "\x82\x02\x01\x01" |
| 806 "\x8A\x02\x01\x01" |
| 807 "\x92\x02\x01\x01" |
| 808 "\x9A\x02\x01\x02" |
| 809 "\xA2\x02\x01\x02" |
| 810 "\xAA\x02\x04\x01\x00\x00\x00" |
| 811 "\xB2\x02\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
| 812 "\xBA\x02\x04\x01\x00\x00\x00" |
| 813 "\xC2\x02\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
| 814 "\xCA\x02\x04\x00\x00\x80\x3f" |
| 815 "\xD2\x02\x08\x00\x00\x00\x00\x00\x00\xf0\x3f" |
| 816 "\xDA\x02\x01\x01" |
| 817 "\x9A\x03\x01\x01", |
| 818 86), |
| 819 packedTestUnpackedTypes_( |
| 820 "\x0A\x01\x01" |
| 821 "\x12\x01\x01" |
| 822 "\x1A\x01\x01" |
| 823 "\x22\x01\x01" |
| 824 "\x2A\x01\x02" |
| 825 "\x32\x01\x02" |
| 826 "\x3A\x04\x01\x00\x00\x00" |
| 827 "\x42\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
| 828 "\x4A\x04\x01\x00\x00\x00" |
| 829 "\x52\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
| 830 "\x5A\x04\x00\x00\x80\x3f" |
| 831 "\x62\x08\x00\x00\x00\x00\x00\x00\xf0\x3f" |
| 832 "\x6A\x01\x01" |
| 833 "\x72\x01\x01", |
| 834 72), |
| 835 unpackedTestAllTypes_( |
| 836 "\xF8\x01\x01" |
| 837 "\x80\x02\x01" |
| 838 "\x88\x02\x01" |
| 839 "\x90\x02\x01" |
| 840 "\x98\x02\x02" |
| 841 "\xA0\x02\x02" |
| 842 "\xAD\x02\x01\x00\x00\x00" |
| 843 "\xB1\x02\x01\x00\x00\x00\x00\x00\x00\x00" |
| 844 "\xBD\x02\x01\x00\x00\x00" |
| 845 "\xC1\x02\x01\x00\x00\x00\x00\x00\x00\x00" |
| 846 "\xCD\x02\x00\x00\x80\x3f" |
| 847 "\xD1\x02\x00\x00\x00\x00\x00\x00\xf0\x3f" |
| 848 "\xD8\x02\x01" |
| 849 "\x98\x03\x01", |
| 850 72), |
| 851 unpackedTestUnpackedTypes_( |
| 852 "\x08\x01" |
| 853 "\x10\x01" |
| 854 "\x18\x01" |
| 855 "\x20\x01" |
| 856 "\x28\x02" |
| 857 "\x30\x02" |
| 858 "\x3D\x01\x00\x00\x00" |
| 859 "\x41\x01\x00\x00\x00\x00\x00\x00\x00" |
| 860 "\x4D\x01\x00\x00\x00" |
| 861 "\x51\x01\x00\x00\x00\x00\x00\x00\x00" |
| 862 "\x5D\x00\x00\x80\x3f" |
| 863 "\x61\x00\x00\x00\x00\x00\x00\xf0\x3f" |
| 864 "\x68\x01" |
| 865 "\x70\x01", |
| 866 58) {} |
| 867 template <class Proto> |
| 868 void SetProto3PrimitiveRepeatedFields(Proto* message) { |
| 869 message->add_repeated_int32(1); |
| 870 message->add_repeated_int64(1); |
| 871 message->add_repeated_uint32(1); |
| 872 message->add_repeated_uint64(1); |
| 873 message->add_repeated_sint32(1); |
| 874 message->add_repeated_sint64(1); |
| 875 message->add_repeated_fixed32(1); |
| 876 message->add_repeated_fixed64(1); |
| 877 message->add_repeated_sfixed32(1); |
| 878 message->add_repeated_sfixed64(1); |
| 879 message->add_repeated_float(1.0); |
| 880 message->add_repeated_double(1.0); |
| 881 message->add_repeated_bool(true); |
| 882 message->add_repeated_nested_enum( |
| 883 proto3_arena_unittest::TestAllTypes_NestedEnum_FOO); |
| 884 } |
| 885 |
| 886 template <class Proto> |
| 887 void ExpectProto3PrimitiveRepeatedFieldsSet(const Proto& message) { |
| 888 EXPECT_EQ(1, message.repeated_int32(0)); |
| 889 EXPECT_EQ(1, message.repeated_int64(0)); |
| 890 EXPECT_EQ(1, message.repeated_uint32(0)); |
| 891 EXPECT_EQ(1, message.repeated_uint64(0)); |
| 892 EXPECT_EQ(1, message.repeated_sint32(0)); |
| 893 EXPECT_EQ(1, message.repeated_sint64(0)); |
| 894 EXPECT_EQ(1, message.repeated_fixed32(0)); |
| 895 EXPECT_EQ(1, message.repeated_fixed64(0)); |
| 896 EXPECT_EQ(1, message.repeated_sfixed32(0)); |
| 897 EXPECT_EQ(1, message.repeated_sfixed64(0)); |
| 898 EXPECT_EQ(1.0, message.repeated_float(0)); |
| 899 EXPECT_EQ(1.0, message.repeated_double(0)); |
| 900 EXPECT_EQ(true, message.repeated_bool(0)); |
| 901 EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_FOO, |
| 902 message.repeated_nested_enum(0)); |
| 903 } |
| 904 |
| 905 template <class Proto> |
| 906 void TestSerialization(Proto* message, const string& expected) { |
| 907 SetProto3PrimitiveRepeatedFields(message); |
| 908 |
| 909 int size = message->ByteSize(); |
| 910 |
| 911 // Serialize using the generated code. |
| 912 string generated_data; |
| 913 { |
| 914 io::StringOutputStream raw_output(&generated_data); |
| 915 io::CodedOutputStream output(&raw_output); |
| 916 message->SerializeWithCachedSizes(&output); |
| 917 ASSERT_FALSE(output.HadError()); |
| 918 } |
| 919 EXPECT_TRUE(expected == generated_data); |
| 920 |
| 921 // Serialize using the dynamic code. |
| 922 string dynamic_data; |
| 923 { |
| 924 io::StringOutputStream raw_output(&dynamic_data); |
| 925 io::CodedOutputStream output(&raw_output); |
| 926 WireFormat::SerializeWithCachedSizes(*message, size, &output); |
| 927 ASSERT_FALSE(output.HadError()); |
| 928 } |
| 929 EXPECT_TRUE(expected == dynamic_data); |
| 930 } |
| 931 |
| 932 template <class Proto> |
| 933 void TestParsing(Proto* message, const string& compatible_data) { |
| 934 message->Clear(); |
| 935 message->ParseFromString(compatible_data); |
| 936 ExpectProto3PrimitiveRepeatedFieldsSet(*message); |
| 937 |
| 938 message->Clear(); |
| 939 io::CodedInputStream input( |
| 940 reinterpret_cast<const uint8*>(compatible_data.data()), |
| 941 compatible_data.size()); |
| 942 WireFormat::ParseAndMergePartial(&input, message); |
| 943 ExpectProto3PrimitiveRepeatedFieldsSet(*message); |
| 944 } |
| 945 |
| 946 const string packedTestAllTypes_; |
| 947 const string packedTestUnpackedTypes_; |
| 948 const string unpackedTestAllTypes_; |
| 949 const string unpackedTestUnpackedTypes_; |
| 950 }; |
| 951 |
| 952 TEST_F(Proto3PrimitiveRepeatedWireFormatTest, Proto3PrimitiveRepeated) { |
| 953 proto3_arena_unittest::TestAllTypes packed_message; |
| 954 proto3_arena_unittest::TestUnpackedTypes unpacked_message; |
| 955 TestSerialization(&packed_message, packedTestAllTypes_); |
| 956 TestParsing(&packed_message, packedTestAllTypes_); |
| 957 TestParsing(&packed_message, unpackedTestAllTypes_); |
| 958 TestSerialization(&unpacked_message, unpackedTestUnpackedTypes_); |
| 959 TestParsing(&unpacked_message, packedTestUnpackedTypes_); |
| 960 TestParsing(&unpacked_message, unpackedTestUnpackedTypes_); |
| 961 } |
| 962 |
| 690 class WireFormatInvalidInputTest : public testing::Test { | 963 class WireFormatInvalidInputTest : public testing::Test { |
| 691 protected: | 964 protected: |
| 692 // Make a serialized TestAllTypes in which the field optional_nested_message | 965 // Make a serialized TestAllTypes in which the field optional_nested_message |
| 693 // contains exactly the given bytes, which may be invalid. | 966 // contains exactly the given bytes, which may be invalid. |
| 694 string MakeInvalidEmbeddedMessage(const char* bytes, int size) { | 967 string MakeInvalidEmbeddedMessage(const char* bytes, int size) { |
| 695 const FieldDescriptor* field = | 968 const FieldDescriptor* field = |
| 696 unittest::TestAllTypes::descriptor()->FindFieldByName( | 969 unittest::TestAllTypes::descriptor()->FindFieldByName( |
| 697 "optional_nested_message"); | 970 "optional_nested_message"); |
| 698 GOOGLE_CHECK(field != NULL); | 971 GOOGLE_CHECK(field != NULL); |
| 699 | 972 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 wire_buffer->clear(); | 1114 wire_buffer->clear(); |
| 842 message->AppendToString(wire_buffer); | 1115 message->AppendToString(wire_buffer); |
| 843 return (wire_buffer->size() > 0); | 1116 return (wire_buffer->size() > 0); |
| 844 } | 1117 } |
| 845 | 1118 |
| 846 template<typename T> | 1119 template<typename T> |
| 847 bool ReadMessage(const string &wire_buffer, T *message) { | 1120 bool ReadMessage(const string &wire_buffer, T *message) { |
| 848 return message->ParseFromArray(wire_buffer.data(), wire_buffer.size()); | 1121 return message->ParseFromArray(wire_buffer.data(), wire_buffer.size()); |
| 849 } | 1122 } |
| 850 | 1123 |
| 851 bool base::StartsWith(const string& s, const string& prefix) { | 1124 bool StartsWith(const string& s, const string& prefix) { |
| 852 return s.substr(0, prefix.length()) == prefix; | 1125 return s.substr(0, prefix.length()) == prefix; |
| 853 } | 1126 } |
| 854 | 1127 |
| 855 TEST(Utf8ValidationTest, WriteInvalidUTF8String) { | 1128 class Utf8ValidationTest : public ::testing::Test { |
| 1129 protected: |
| 1130 Utf8ValidationTest() {} |
| 1131 virtual ~Utf8ValidationTest() {} |
| 1132 virtual void SetUp() { |
| 1133 } |
| 1134 |
| 1135 }; |
| 1136 |
| 1137 TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) { |
| 856 string wire_buffer; | 1138 string wire_buffer; |
| 857 protobuf_unittest::OneString input; | 1139 protobuf_unittest::OneString input; |
| 858 vector<string> errors; | 1140 vector<string> errors; |
| 859 { | 1141 { |
| 860 ScopedMemoryLog log; | 1142 ScopedMemoryLog log; |
| 861 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1143 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
| 862 errors = log.GetMessages(ERROR); | 1144 errors = log.GetMessages(ERROR); |
| 863 } | 1145 } |
| 864 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1146 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 865 ASSERT_EQ(1, errors.size()); | 1147 ASSERT_EQ(1, errors.size()); |
| 866 EXPECT_TRUE( | 1148 EXPECT_TRUE(StartsWith(errors[0], |
| 867 base::StartsWith(errors[0], | 1149 "String field 'protobuf_unittest.OneString.data' " |
| 868 "String field contains invalid UTF-8 data when " | 1150 "contains invalid UTF-8 data when " |
| 869 "serializing a protocol buffer. Use the " | 1151 "serializing a protocol buffer. Use the " |
| 870 "'bytes' type if you intend to send raw bytes.")); | 1152 "'bytes' type if you intend to send raw bytes.")); |
| 871 #else | 1153 #else |
| 872 ASSERT_EQ(0, errors.size()); | 1154 ASSERT_EQ(0, errors.size()); |
| 873 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1155 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 874 } | 1156 } |
| 875 | 1157 |
| 876 TEST(Utf8ValidationTest, ReadInvalidUTF8String) { | 1158 |
| 1159 TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) { |
| 877 string wire_buffer; | 1160 string wire_buffer; |
| 878 protobuf_unittest::OneString input; | 1161 protobuf_unittest::OneString input; |
| 879 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1162 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
| 880 protobuf_unittest::OneString output; | 1163 protobuf_unittest::OneString output; |
| 881 vector<string> errors; | 1164 vector<string> errors; |
| 882 { | 1165 { |
| 883 ScopedMemoryLog log; | 1166 ScopedMemoryLog log; |
| 884 ReadMessage(wire_buffer, &output); | 1167 ReadMessage(wire_buffer, &output); |
| 885 errors = log.GetMessages(ERROR); | 1168 errors = log.GetMessages(ERROR); |
| 886 } | 1169 } |
| 887 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1170 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 888 ASSERT_EQ(1, errors.size()); | 1171 ASSERT_EQ(1, errors.size()); |
| 889 EXPECT_TRUE( | 1172 EXPECT_TRUE(StartsWith(errors[0], |
| 890 base::StartsWith(errors[0], | 1173 "String field 'protobuf_unittest.OneString.data' " |
| 891 "String field contains invalid UTF-8 data when " | 1174 "contains invalid UTF-8 data when " |
| 892 "parsing a protocol buffer. Use the " | 1175 "parsing a protocol buffer. Use the " |
| 893 "'bytes' type if you intend to send raw bytes.")); | 1176 "'bytes' type if you intend to send raw bytes.")); |
| 894 | 1177 |
| 895 #else | 1178 #else |
| 896 ASSERT_EQ(0, errors.size()); | 1179 ASSERT_EQ(0, errors.size()); |
| 897 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1180 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 898 } | 1181 } |
| 899 | 1182 |
| 900 TEST(Utf8ValidationTest, WriteValidUTF8String) { | 1183 |
| 1184 TEST_F(Utf8ValidationTest, WriteValidUTF8String) { |
| 901 string wire_buffer; | 1185 string wire_buffer; |
| 902 protobuf_unittest::OneString input; | 1186 protobuf_unittest::OneString input; |
| 903 vector<string> errors; | 1187 vector<string> errors; |
| 904 { | 1188 { |
| 905 ScopedMemoryLog log; | 1189 ScopedMemoryLog log; |
| 906 WriteMessage(kValidUTF8String, &input, &wire_buffer); | 1190 WriteMessage(kValidUTF8String, &input, &wire_buffer); |
| 907 errors = log.GetMessages(ERROR); | 1191 errors = log.GetMessages(ERROR); |
| 908 } | 1192 } |
| 909 ASSERT_EQ(0, errors.size()); | 1193 ASSERT_EQ(0, errors.size()); |
| 910 } | 1194 } |
| 911 | 1195 |
| 912 TEST(Utf8ValidationTest, ReadValidUTF8String) { | 1196 TEST_F(Utf8ValidationTest, ReadValidUTF8String) { |
| 913 string wire_buffer; | 1197 string wire_buffer; |
| 914 protobuf_unittest::OneString input; | 1198 protobuf_unittest::OneString input; |
| 915 WriteMessage(kValidUTF8String, &input, &wire_buffer); | 1199 WriteMessage(kValidUTF8String, &input, &wire_buffer); |
| 916 protobuf_unittest::OneString output; | 1200 protobuf_unittest::OneString output; |
| 917 vector<string> errors; | 1201 vector<string> errors; |
| 918 { | 1202 { |
| 919 ScopedMemoryLog log; | 1203 ScopedMemoryLog log; |
| 920 ReadMessage(wire_buffer, &output); | 1204 ReadMessage(wire_buffer, &output); |
| 921 errors = log.GetMessages(ERROR); | 1205 errors = log.GetMessages(ERROR); |
| 922 } | 1206 } |
| 923 ASSERT_EQ(0, errors.size()); | 1207 ASSERT_EQ(0, errors.size()); |
| 924 EXPECT_EQ(input.data(), output.data()); | 1208 EXPECT_EQ(input.data(), output.data()); |
| 925 } | 1209 } |
| 926 | 1210 |
| 927 // Bytes: anything can pass as bytes, use invalid UTF-8 string to test | 1211 // Bytes: anything can pass as bytes, use invalid UTF-8 string to test |
| 928 TEST(Utf8ValidationTest, WriteArbitraryBytes) { | 1212 TEST_F(Utf8ValidationTest, WriteArbitraryBytes) { |
| 929 string wire_buffer; | 1213 string wire_buffer; |
| 930 protobuf_unittest::OneBytes input; | 1214 protobuf_unittest::OneBytes input; |
| 931 vector<string> errors; | 1215 vector<string> errors; |
| 932 { | 1216 { |
| 933 ScopedMemoryLog log; | 1217 ScopedMemoryLog log; |
| 934 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1218 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
| 935 errors = log.GetMessages(ERROR); | 1219 errors = log.GetMessages(ERROR); |
| 936 } | 1220 } |
| 937 ASSERT_EQ(0, errors.size()); | 1221 ASSERT_EQ(0, errors.size()); |
| 938 } | 1222 } |
| 939 | 1223 |
| 940 TEST(Utf8ValidationTest, ReadArbitraryBytes) { | 1224 TEST_F(Utf8ValidationTest, ReadArbitraryBytes) { |
| 941 string wire_buffer; | 1225 string wire_buffer; |
| 942 protobuf_unittest::OneBytes input; | 1226 protobuf_unittest::OneBytes input; |
| 943 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); | 1227 WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
| 944 protobuf_unittest::OneBytes output; | 1228 protobuf_unittest::OneBytes output; |
| 945 vector<string> errors; | 1229 vector<string> errors; |
| 946 { | 1230 { |
| 947 ScopedMemoryLog log; | 1231 ScopedMemoryLog log; |
| 948 ReadMessage(wire_buffer, &output); | 1232 ReadMessage(wire_buffer, &output); |
| 949 errors = log.GetMessages(ERROR); | 1233 errors = log.GetMessages(ERROR); |
| 950 } | 1234 } |
| 951 ASSERT_EQ(0, errors.size()); | 1235 ASSERT_EQ(0, errors.size()); |
| 952 EXPECT_EQ(input.data(), output.data()); | 1236 EXPECT_EQ(input.data(), output.data()); |
| 953 } | 1237 } |
| 954 | 1238 |
| 955 TEST(Utf8ValidationTest, ParseRepeatedString) { | 1239 TEST_F(Utf8ValidationTest, ParseRepeatedString) { |
| 956 protobuf_unittest::MoreBytes input; | 1240 protobuf_unittest::MoreBytes input; |
| 957 input.add_data(kValidUTF8String); | 1241 input.add_data(kValidUTF8String); |
| 958 input.add_data(kInvalidUTF8String); | 1242 input.add_data(kInvalidUTF8String); |
| 959 input.add_data(kInvalidUTF8String); | 1243 input.add_data(kInvalidUTF8String); |
| 960 string wire_buffer = input.SerializeAsString(); | 1244 string wire_buffer = input.SerializeAsString(); |
| 961 | 1245 |
| 962 protobuf_unittest::MoreString output; | 1246 protobuf_unittest::MoreString output; |
| 963 vector<string> errors; | 1247 vector<string> errors; |
| 964 { | 1248 { |
| 965 ScopedMemoryLog log; | 1249 ScopedMemoryLog log; |
| 966 ReadMessage(wire_buffer, &output); | 1250 ReadMessage(wire_buffer, &output); |
| 967 errors = log.GetMessages(ERROR); | 1251 errors = log.GetMessages(ERROR); |
| 968 } | 1252 } |
| 969 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1253 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 970 ASSERT_EQ(2, errors.size()); | 1254 ASSERT_EQ(2, errors.size()); |
| 971 #else | 1255 #else |
| 972 ASSERT_EQ(0, errors.size()); | 1256 ASSERT_EQ(0, errors.size()); |
| 973 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED | 1257 #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 974 EXPECT_EQ(wire_buffer, output.SerializeAsString()); | 1258 EXPECT_EQ(wire_buffer, output.SerializeAsString()); |
| 975 } | 1259 } |
| 976 | 1260 |
| 1261 // Test the old VerifyUTF8String() function, which may still be called by old |
| 1262 // generated code. |
| 1263 TEST_F(Utf8ValidationTest, OldVerifyUTF8String) { |
| 1264 string data(kInvalidUTF8String); |
| 1265 |
| 1266 vector<string> errors; |
| 1267 { |
| 1268 ScopedMemoryLog log; |
| 1269 WireFormat::VerifyUTF8String(data.data(), data.size(), |
| 1270 WireFormat::SERIALIZE); |
| 1271 errors = log.GetMessages(ERROR); |
| 1272 } |
| 1273 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
| 1274 ASSERT_EQ(1, errors.size()); |
| 1275 EXPECT_TRUE(StartsWith(errors[0], |
| 1276 "String field contains invalid UTF-8 data when " |
| 1277 "serializing a protocol buffer. Use the " |
| 1278 "'bytes' type if you intend to send raw bytes.")); |
| 1279 #else |
| 1280 ASSERT_EQ(0, errors.size()); |
| 1281 #endif |
| 1282 } |
| 1283 |
| 1284 |
| 977 } // namespace | 1285 } // namespace |
| 978 } // namespace internal | 1286 } // namespace internal |
| 979 } // namespace protobuf | 1287 } // namespace protobuf |
| 980 } // namespace google | 1288 } // namespace google |
| OLD | NEW |