1// Protocol Buffers - Google's data interchange format 2// Copyright 2008 Google Inc. All rights reserved. 3// http://code.google.com/p/protobuf/ 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are 7// met: 8// 9// * Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above 12// copyright notice, this list of conditions and the following disclaimer 13// in the documentation and/or other materials provided with the 14// distribution. 15// * Neither the name of Google Inc. nor the names of its 16// contributors may be used to endorse or promote products derived from 17// this software without specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31// Author: jschorr@google.com (Joseph Schorr) 32// Based on original Protocol Buffers design by 33// Sanjay Ghemawat, Jeff Dean, and others. 34 35#include <math.h> 36#include <stdlib.h> 37#include <limits> 38 39#include <google/protobuf/text_format.h> 40#include <google/protobuf/io/zero_copy_stream_impl.h> 41#include <google/protobuf/io/tokenizer.h> 42#include <google/protobuf/unittest.pb.h> 43#include <google/protobuf/unittest_mset.pb.h> 44#include <google/protobuf/test_util.h> 45 46#include <google/protobuf/stubs/common.h> 47#include <google/protobuf/testing/file.h> 48#include <google/protobuf/testing/googletest.h> 49#include <gtest/gtest.h> 50#include <google/protobuf/stubs/strutil.h> 51#include <google/protobuf/stubs/substitute.h> 52 53namespace google { 54namespace protobuf { 55 56// Can't use an anonymous namespace here due to brokenness of Tru64 compiler. 57namespace text_format_unittest { 58 59inline bool IsNaN(double value) { 60 // NaN is never equal to anything, even itself. 61 return value != value; 62} 63 64// A basic string with different escapable characters for testing. 65const string kEscapeTestString = 66 "\"A string with ' characters \n and \r newlines and \t tabs and \001 " 67 "slashes \\ and multiple spaces"; 68 69// A representation of the above string with all the characters escaped. 70const string kEscapeTestStringEscaped = 71 "\"\\\"A string with \\' characters \\n and \\r newlines " 72 "and \\t tabs and \\001 slashes \\\\ and multiple spaces\""; 73 74class TextFormatTest : public testing::Test { 75 public: 76 static void SetUpTestCase() { 77 File::ReadFileToStringOrDie( 78 TestSourceDir() 79 + "/google/protobuf/testdata/text_format_unittest_data.txt", 80 &static_proto_debug_string_); 81 } 82 83 TextFormatTest() : proto_debug_string_(static_proto_debug_string_) {} 84 85 protected: 86 // Debug string read from text_format_unittest_data.txt. 87 const string proto_debug_string_; 88 unittest::TestAllTypes proto_; 89 90 private: 91 static string static_proto_debug_string_; 92}; 93string TextFormatTest::static_proto_debug_string_; 94 95class TextFormatExtensionsTest : public testing::Test { 96 public: 97 static void SetUpTestCase() { 98 File::ReadFileToStringOrDie( 99 TestSourceDir() 100 + "/google/protobuf/testdata/" 101 "text_format_unittest_extensions_data.txt", 102 &static_proto_debug_string_); 103 } 104 105 TextFormatExtensionsTest() 106 : proto_debug_string_(static_proto_debug_string_) {} 107 108 protected: 109 // Debug string read from text_format_unittest_data.txt. 110 const string proto_debug_string_; 111 unittest::TestAllExtensions proto_; 112 113 private: 114 static string static_proto_debug_string_; 115}; 116string TextFormatExtensionsTest::static_proto_debug_string_; 117 118 119TEST_F(TextFormatTest, Basic) { 120 TestUtil::SetAllFields(&proto_); 121 EXPECT_EQ(proto_debug_string_, proto_.DebugString()); 122} 123 124TEST_F(TextFormatExtensionsTest, Extensions) { 125 TestUtil::SetAllExtensions(&proto_); 126 EXPECT_EQ(proto_debug_string_, proto_.DebugString()); 127} 128 129TEST_F(TextFormatTest, ShortDebugString) { 130 proto_.set_optional_int32(1); 131 proto_.set_optional_string("hello"); 132 proto_.mutable_optional_nested_message()->set_bb(2); 133 proto_.mutable_optional_foreign_message(); 134 135 EXPECT_EQ("optional_int32: 1 optional_string: \"hello\" " 136 "optional_nested_message { bb: 2 } " 137 "optional_foreign_message { }", 138 proto_.ShortDebugString()); 139} 140 141TEST_F(TextFormatTest, ShortPrimitiveRepeateds) { 142 proto_.set_optional_int32(123); 143 proto_.add_repeated_int32(456); 144 proto_.add_repeated_int32(789); 145 proto_.add_repeated_string("foo"); 146 proto_.add_repeated_string("bar"); 147 proto_.add_repeated_nested_message()->set_bb(2); 148 proto_.add_repeated_nested_message()->set_bb(3); 149 proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO); 150 proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR); 151 152 TextFormat::Printer printer; 153 printer.SetUseShortRepeatedPrimitives(true); 154 string text; 155 printer.PrintToString(proto_, &text); 156 157 EXPECT_EQ("optional_int32: 123\n" 158 "repeated_int32: [456, 789]\n" 159 "repeated_string: \"foo\"\n" 160 "repeated_string: \"bar\"\n" 161 "repeated_nested_message {\n bb: 2\n}\n" 162 "repeated_nested_message {\n bb: 3\n}\n" 163 "repeated_nested_enum: [FOO, BAR]\n", 164 text); 165 166 // Try in single-line mode. 167 printer.SetSingleLineMode(true); 168 printer.PrintToString(proto_, &text); 169 170 EXPECT_EQ("optional_int32: 123 " 171 "repeated_int32: [456, 789] " 172 "repeated_string: \"foo\" " 173 "repeated_string: \"bar\" " 174 "repeated_nested_message { bb: 2 } " 175 "repeated_nested_message { bb: 3 } " 176 "repeated_nested_enum: [FOO, BAR] ", 177 text); 178} 179 180 181TEST_F(TextFormatTest, StringEscape) { 182 // Set the string value to test. 183 proto_.set_optional_string(kEscapeTestString); 184 185 // Get the DebugString from the proto. 186 string debug_string = proto_.DebugString(); 187 string utf8_debug_string = proto_.Utf8DebugString(); 188 189 // Hardcode a correct value to test against. 190 string correct_string = "optional_string: " 191 + kEscapeTestStringEscaped 192 + "\n"; 193 194 // Compare. 195 EXPECT_EQ(correct_string, debug_string); 196 // UTF-8 string is the same as non-UTF-8 because 197 // the protocol buffer contains no UTF-8 text. 198 EXPECT_EQ(correct_string, utf8_debug_string); 199 200 string expected_short_debug_string = "optional_string: " 201 + kEscapeTestStringEscaped; 202 EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString()); 203} 204 205TEST_F(TextFormatTest, Utf8DebugString) { 206 // Set the string value to test. 207 proto_.set_optional_string("\350\260\267\346\255\214"); 208 209 // Get the DebugString from the proto. 210 string debug_string = proto_.DebugString(); 211 string utf8_debug_string = proto_.Utf8DebugString(); 212 213 // Hardcode a correct value to test against. 214 string correct_utf8_string = "optional_string: " 215 "\"\350\260\267\346\255\214\"" 216 "\n"; 217 string correct_string = "optional_string: " 218 "\"\\350\\260\\267\\346\\255\\214\"" 219 "\n"; 220 221 // Compare. 222 EXPECT_EQ(correct_utf8_string, utf8_debug_string); 223 EXPECT_EQ(correct_string, debug_string); 224} 225 226TEST_F(TextFormatTest, PrintUnknownFields) { 227 // Test printing of unknown fields in a message. 228 229 unittest::TestEmptyMessage message; 230 UnknownFieldSet* unknown_fields = message.mutable_unknown_fields(); 231 232 unknown_fields->AddVarint(5, 1); 233 unknown_fields->AddFixed32(5, 2); 234 unknown_fields->AddFixed64(5, 3); 235 unknown_fields->AddLengthDelimited(5, "4"); 236 unknown_fields->AddGroup(5)->AddVarint(10, 5); 237 238 unknown_fields->AddVarint(8, 1); 239 unknown_fields->AddVarint(8, 2); 240 unknown_fields->AddVarint(8, 3); 241 242 EXPECT_EQ( 243 "5: 1\n" 244 "5: 0x00000002\n" 245 "5: 0x0000000000000003\n" 246 "5: \"4\"\n" 247 "5 {\n" 248 " 10: 5\n" 249 "}\n" 250 "8: 1\n" 251 "8: 2\n" 252 "8: 3\n", 253 message.DebugString()); 254} 255 256TEST_F(TextFormatTest, PrintUnknownMessage) { 257 // Test heuristic printing of messages in an UnknownFieldSet. 258 259 protobuf_unittest::TestAllTypes message; 260 261 // Cases which should not be interpreted as sub-messages. 262 263 // 'a' is a valid FIXED64 tag, so for the string to be parseable as a message 264 // it should be followed by 8 bytes. Since this string only has two 265 // subsequent bytes, it should be treated as a string. 266 message.add_repeated_string("abc"); 267 268 // 'd' happens to be a valid ENDGROUP tag. So, 269 // UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but 270 // the ConsumedEntireMessage() check should fail. 271 message.add_repeated_string("def"); 272 273 // A zero-length string should never be interpreted as a message even though 274 // it is technically valid as one. 275 message.add_repeated_string(""); 276 277 // Case which should be interpreted as a sub-message. 278 279 // An actual nested message with content should always be interpreted as a 280 // nested message. 281 message.add_repeated_nested_message()->set_bb(123); 282 283 string data; 284 message.SerializeToString(&data); 285 286 string text; 287 UnknownFieldSet unknown_fields; 288 EXPECT_TRUE(unknown_fields.ParseFromString(data)); 289 EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text)); 290 EXPECT_EQ( 291 "44: \"abc\"\n" 292 "44: \"def\"\n" 293 "44: \"\"\n" 294 "48 {\n" 295 " 1: 123\n" 296 "}\n", 297 text); 298} 299 300TEST_F(TextFormatTest, PrintMessageWithIndent) { 301 // Test adding an initial indent to printing. 302 303 protobuf_unittest::TestAllTypes message; 304 305 message.add_repeated_string("abc"); 306 message.add_repeated_string("def"); 307 message.add_repeated_nested_message()->set_bb(123); 308 309 string text; 310 TextFormat::Printer printer; 311 printer.SetInitialIndentLevel(1); 312 EXPECT_TRUE(printer.PrintToString(message, &text)); 313 EXPECT_EQ( 314 " repeated_string: \"abc\"\n" 315 " repeated_string: \"def\"\n" 316 " repeated_nested_message {\n" 317 " bb: 123\n" 318 " }\n", 319 text); 320} 321 322TEST_F(TextFormatTest, PrintMessageSingleLine) { 323 // Test printing a message on a single line. 324 325 protobuf_unittest::TestAllTypes message; 326 327 message.add_repeated_string("abc"); 328 message.add_repeated_string("def"); 329 message.add_repeated_nested_message()->set_bb(123); 330 331 string text; 332 TextFormat::Printer printer; 333 printer.SetInitialIndentLevel(1); 334 printer.SetSingleLineMode(true); 335 EXPECT_TRUE(printer.PrintToString(message, &text)); 336 EXPECT_EQ( 337 " repeated_string: \"abc\" repeated_string: \"def\" " 338 "repeated_nested_message { bb: 123 } ", 339 text); 340} 341 342TEST_F(TextFormatTest, PrintBufferTooSmall) { 343 // Test printing a message to a buffer that is too small. 344 345 protobuf_unittest::TestAllTypes message; 346 347 message.add_repeated_string("abc"); 348 message.add_repeated_string("def"); 349 350 char buffer[1] = ""; 351 io::ArrayOutputStream output_stream(buffer, 1); 352 EXPECT_FALSE(TextFormat::Print(message, &output_stream)); 353 EXPECT_EQ(buffer[0], 'r'); 354 EXPECT_EQ(output_stream.ByteCount(), 1); 355} 356 357TEST_F(TextFormatTest, ParseBasic) { 358 io::ArrayInputStream input_stream(proto_debug_string_.data(), 359 proto_debug_string_.size()); 360 TextFormat::Parse(&input_stream, &proto_); 361 TestUtil::ExpectAllFieldsSet(proto_); 362} 363 364TEST_F(TextFormatExtensionsTest, ParseExtensions) { 365 io::ArrayInputStream input_stream(proto_debug_string_.data(), 366 proto_debug_string_.size()); 367 TextFormat::Parse(&input_stream, &proto_); 368 TestUtil::ExpectAllExtensionsSet(proto_); 369} 370 371TEST_F(TextFormatTest, ParseEnumFieldFromNumber) { 372 // Create a parse string with a numerical value for an enum field. 373 string parse_string = strings::Substitute("optional_nested_enum: $0", 374 unittest::TestAllTypes::BAZ); 375 EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_)); 376 EXPECT_TRUE(proto_.has_optional_nested_enum()); 377 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum()); 378} 379 380TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) { 381 ASSERT_LT(unittest::SPARSE_E, 0); 382 string parse_string = strings::Substitute("sparse_enum: $0", 383 unittest::SPARSE_E); 384 unittest::SparseEnumMessage proto; 385 EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto)); 386 EXPECT_TRUE(proto.has_sparse_enum()); 387 EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum()); 388} 389 390TEST_F(TextFormatTest, ParseStringEscape) { 391 // Create a parse string with escpaed characters in it. 392 string parse_string = "optional_string: " 393 + kEscapeTestStringEscaped 394 + "\n"; 395 396 io::ArrayInputStream input_stream(parse_string.data(), 397 parse_string.size()); 398 TextFormat::Parse(&input_stream, &proto_); 399 400 // Compare. 401 EXPECT_EQ(kEscapeTestString, proto_.optional_string()); 402} 403 404TEST_F(TextFormatTest, ParseConcatenatedString) { 405 // Create a parse string with multiple parts on one line. 406 string parse_string = "optional_string: \"foo\" \"bar\"\n"; 407 408 io::ArrayInputStream input_stream1(parse_string.data(), 409 parse_string.size()); 410 TextFormat::Parse(&input_stream1, &proto_); 411 412 // Compare. 413 EXPECT_EQ("foobar", proto_.optional_string()); 414 415 // Create a parse string with multiple parts on seperate lines. 416 parse_string = "optional_string: \"foo\"\n" 417 "\"bar\"\n"; 418 419 io::ArrayInputStream input_stream2(parse_string.data(), 420 parse_string.size()); 421 TextFormat::Parse(&input_stream2, &proto_); 422 423 // Compare. 424 EXPECT_EQ("foobar", proto_.optional_string()); 425} 426 427TEST_F(TextFormatTest, ParseFloatWithSuffix) { 428 // Test that we can parse a floating-point value with 'f' appended to the 429 // end. This is needed for backwards-compatibility with proto1. 430 431 // Have it parse a float with the 'f' suffix. 432 string parse_string = "optional_float: 1.0f\n"; 433 434 io::ArrayInputStream input_stream(parse_string.data(), 435 parse_string.size()); 436 437 TextFormat::Parse(&input_stream, &proto_); 438 439 // Compare. 440 EXPECT_EQ(1.0, proto_.optional_float()); 441} 442 443TEST_F(TextFormatTest, ParseShortRepeatedForm) { 444 string parse_string = 445 // Mixed short-form and long-form are simply concatenated. 446 "repeated_int32: 1\n" 447 "repeated_int32: [456, 789]\n" 448 "repeated_nested_enum: [ FOO ,BAR, # comment\n" 449 " 3]\n" 450 // Note that while the printer won't print repeated strings in short-form, 451 // the parser will accept them. 452 "repeated_string: [ \"foo\", 'bar' ]\n"; 453 454 ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_)); 455 456 ASSERT_EQ(3, proto_.repeated_int32_size()); 457 EXPECT_EQ(1, proto_.repeated_int32(0)); 458 EXPECT_EQ(456, proto_.repeated_int32(1)); 459 EXPECT_EQ(789, proto_.repeated_int32(2)); 460 461 ASSERT_EQ(3, proto_.repeated_nested_enum_size()); 462 EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0)); 463 EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1)); 464 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2)); 465 466 ASSERT_EQ(2, proto_.repeated_string_size()); 467 EXPECT_EQ("foo", proto_.repeated_string(0)); 468 EXPECT_EQ("bar", proto_.repeated_string(1)); 469} 470 471TEST_F(TextFormatTest, Comments) { 472 // Test that comments are ignored. 473 474 string parse_string = "optional_int32: 1 # a comment\n" 475 "optional_int64: 2 # another comment"; 476 477 io::ArrayInputStream input_stream(parse_string.data(), 478 parse_string.size()); 479 480 TextFormat::Parse(&input_stream, &proto_); 481 482 // Compare. 483 EXPECT_EQ(1, proto_.optional_int32()); 484 EXPECT_EQ(2, proto_.optional_int64()); 485} 486 487TEST_F(TextFormatTest, OptionalColon) { 488 // Test that we can place a ':' after the field name of a nested message, 489 // even though we don't have to. 490 491 string parse_string = "optional_nested_message: { bb: 1}\n"; 492 493 io::ArrayInputStream input_stream(parse_string.data(), 494 parse_string.size()); 495 496 TextFormat::Parse(&input_stream, &proto_); 497 498 // Compare. 499 EXPECT_TRUE(proto_.has_optional_nested_message()); 500 EXPECT_EQ(1, proto_.optional_nested_message().bb()); 501} 502 503// Some platforms (e.g. Windows) insist on padding the exponent to three 504// digits when one or two would be just fine. 505static string RemoveRedundantZeros(string text) { 506 text = StringReplace(text, "e+0", "e+", true); 507 text = StringReplace(text, "e-0", "e-", true); 508 return text; 509} 510 511TEST_F(TextFormatTest, PrintExotic) { 512 unittest::TestAllTypes message; 513 514 // Note: In C, a negative integer literal is actually the unary negation 515 // operator being applied to a positive integer literal, and 516 // 9223372036854775808 is outside the range of int64. However, it is not 517 // outside the range of uint64. Confusingly, this means that everything 518 // works if we make the literal unsigned, even though we are negating it. 519 message.add_repeated_int64(-GOOGLE_ULONGLONG(9223372036854775808)); 520 message.add_repeated_uint64(GOOGLE_ULONGLONG(18446744073709551615)); 521 message.add_repeated_double(123.456); 522 message.add_repeated_double(1.23e21); 523 message.add_repeated_double(1.23e-18); 524 message.add_repeated_double(std::numeric_limits<double>::infinity()); 525 message.add_repeated_double(-std::numeric_limits<double>::infinity()); 526 message.add_repeated_double(std::numeric_limits<double>::quiet_NaN()); 527 message.add_repeated_string(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12)); 528 529 // Fun story: We used to use 1.23e22 instead of 1.23e21 above, but this 530 // seemed to trigger an odd case on MinGW/GCC 3.4.5 where GCC's parsing of 531 // the value differed from strtod()'s parsing. That is to say, the 532 // following assertion fails on MinGW: 533 // assert(1.23e22 == strtod("1.23e22", NULL)); 534 // As a result, SimpleDtoa() would print the value as 535 // "1.2300000000000001e+22" to make sure strtod() produce the exact same 536 // result. Our goal is to test runtime parsing, not compile-time parsing, 537 // so this wasn't our problem. It was found that using 1.23e21 did not 538 // have this problem, so we switched to that instead. 539 540 EXPECT_EQ( 541 "repeated_int64: -9223372036854775808\n" 542 "repeated_uint64: 18446744073709551615\n" 543 "repeated_double: 123.456\n" 544 "repeated_double: 1.23e+21\n" 545 "repeated_double: 1.23e-18\n" 546 "repeated_double: inf\n" 547 "repeated_double: -inf\n" 548 "repeated_double: nan\n" 549 "repeated_string: \"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n", 550 RemoveRedundantZeros(message.DebugString())); 551} 552 553TEST_F(TextFormatTest, PrintFloatPrecision) { 554 unittest::TestAllTypes message; 555 556 message.add_repeated_float(1.2); 557 message.add_repeated_float(1.23); 558 message.add_repeated_float(1.234); 559 message.add_repeated_float(1.2345); 560 message.add_repeated_float(1.23456); 561 message.add_repeated_float(1.2e10); 562 message.add_repeated_float(1.23e10); 563 message.add_repeated_float(1.234e10); 564 message.add_repeated_float(1.2345e10); 565 message.add_repeated_float(1.23456e10); 566 message.add_repeated_double(1.2); 567 message.add_repeated_double(1.23); 568 message.add_repeated_double(1.234); 569 message.add_repeated_double(1.2345); 570 message.add_repeated_double(1.23456); 571 message.add_repeated_double(1.234567); 572 message.add_repeated_double(1.2345678); 573 message.add_repeated_double(1.23456789); 574 message.add_repeated_double(1.234567898); 575 message.add_repeated_double(1.2345678987); 576 message.add_repeated_double(1.23456789876); 577 message.add_repeated_double(1.234567898765); 578 message.add_repeated_double(1.2345678987654); 579 message.add_repeated_double(1.23456789876543); 580 message.add_repeated_double(1.2e100); 581 message.add_repeated_double(1.23e100); 582 message.add_repeated_double(1.234e100); 583 message.add_repeated_double(1.2345e100); 584 message.add_repeated_double(1.23456e100); 585 message.add_repeated_double(1.234567e100); 586 message.add_repeated_double(1.2345678e100); 587 message.add_repeated_double(1.23456789e100); 588 message.add_repeated_double(1.234567898e100); 589 message.add_repeated_double(1.2345678987e100); 590 message.add_repeated_double(1.23456789876e100); 591 message.add_repeated_double(1.234567898765e100); 592 message.add_repeated_double(1.2345678987654e100); 593 message.add_repeated_double(1.23456789876543e100); 594 595 EXPECT_EQ( 596 "repeated_float: 1.2\n" 597 "repeated_float: 1.23\n" 598 "repeated_float: 1.234\n" 599 "repeated_float: 1.2345\n" 600 "repeated_float: 1.23456\n" 601 "repeated_float: 1.2e+10\n" 602 "repeated_float: 1.23e+10\n" 603 "repeated_float: 1.234e+10\n" 604 "repeated_float: 1.2345e+10\n" 605 "repeated_float: 1.23456e+10\n" 606 "repeated_double: 1.2\n" 607 "repeated_double: 1.23\n" 608 "repeated_double: 1.234\n" 609 "repeated_double: 1.2345\n" 610 "repeated_double: 1.23456\n" 611 "repeated_double: 1.234567\n" 612 "repeated_double: 1.2345678\n" 613 "repeated_double: 1.23456789\n" 614 "repeated_double: 1.234567898\n" 615 "repeated_double: 1.2345678987\n" 616 "repeated_double: 1.23456789876\n" 617 "repeated_double: 1.234567898765\n" 618 "repeated_double: 1.2345678987654\n" 619 "repeated_double: 1.23456789876543\n" 620 "repeated_double: 1.2e+100\n" 621 "repeated_double: 1.23e+100\n" 622 "repeated_double: 1.234e+100\n" 623 "repeated_double: 1.2345e+100\n" 624 "repeated_double: 1.23456e+100\n" 625 "repeated_double: 1.234567e+100\n" 626 "repeated_double: 1.2345678e+100\n" 627 "repeated_double: 1.23456789e+100\n" 628 "repeated_double: 1.234567898e+100\n" 629 "repeated_double: 1.2345678987e+100\n" 630 "repeated_double: 1.23456789876e+100\n" 631 "repeated_double: 1.234567898765e+100\n" 632 "repeated_double: 1.2345678987654e+100\n" 633 "repeated_double: 1.23456789876543e+100\n", 634 RemoveRedundantZeros(message.DebugString())); 635} 636 637 638TEST_F(TextFormatTest, AllowPartial) { 639 unittest::TestRequired message; 640 TextFormat::Parser parser; 641 parser.AllowPartialMessage(true); 642 EXPECT_TRUE(parser.ParseFromString("a: 1", &message)); 643 EXPECT_EQ(1, message.a()); 644 EXPECT_FALSE(message.has_b()); 645 EXPECT_FALSE(message.has_c()); 646} 647 648TEST_F(TextFormatTest, ParseExotic) { 649 unittest::TestAllTypes message; 650 ASSERT_TRUE(TextFormat::ParseFromString( 651 "repeated_int32: -1\n" 652 "repeated_int32: -2147483648\n" 653 "repeated_int64: -1\n" 654 "repeated_int64: -9223372036854775808\n" 655 "repeated_uint32: 4294967295\n" 656 "repeated_uint32: 2147483648\n" 657 "repeated_uint64: 18446744073709551615\n" 658 "repeated_uint64: 9223372036854775808\n" 659 "repeated_double: 123.0\n" 660 "repeated_double: 123.5\n" 661 "repeated_double: 0.125\n" 662 "repeated_double: 1.23E17\n" 663 "repeated_double: 1.235E+22\n" 664 "repeated_double: 1.235e-18\n" 665 "repeated_double: 123.456789\n" 666 "repeated_double: inf\n" 667 "repeated_double: Infinity\n" 668 "repeated_double: -inf\n" 669 "repeated_double: -Infinity\n" 670 "repeated_double: nan\n" 671 "repeated_double: NaN\n" 672 "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n", 673 &message)); 674 675 ASSERT_EQ(2, message.repeated_int32_size()); 676 EXPECT_EQ(-1, message.repeated_int32(0)); 677 // Note: In C, a negative integer literal is actually the unary negation 678 // operator being applied to a positive integer literal, and 2147483648 is 679 // outside the range of int32. However, it is not outside the range of 680 // uint32. Confusingly, this means that everything works if we make the 681 // literal unsigned, even though we are negating it. 682 EXPECT_EQ(-2147483648u, message.repeated_int32(1)); 683 684 ASSERT_EQ(2, message.repeated_int64_size()); 685 EXPECT_EQ(-1, message.repeated_int64(0)); 686 // Note: In C, a negative integer literal is actually the unary negation 687 // operator being applied to a positive integer literal, and 688 // 9223372036854775808 is outside the range of int64. However, it is not 689 // outside the range of uint64. Confusingly, this means that everything 690 // works if we make the literal unsigned, even though we are negating it. 691 EXPECT_EQ(-GOOGLE_ULONGLONG(9223372036854775808), message.repeated_int64(1)); 692 693 ASSERT_EQ(2, message.repeated_uint32_size()); 694 EXPECT_EQ(4294967295u, message.repeated_uint32(0)); 695 EXPECT_EQ(2147483648u, message.repeated_uint32(1)); 696 697 ASSERT_EQ(2, message.repeated_uint64_size()); 698 EXPECT_EQ(GOOGLE_ULONGLONG(18446744073709551615), message.repeated_uint64(0)); 699 EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1)); 700 701 ASSERT_EQ(13, message.repeated_double_size()); 702 EXPECT_EQ(123.0 , message.repeated_double(0)); 703 EXPECT_EQ(123.5 , message.repeated_double(1)); 704 EXPECT_EQ(0.125 , message.repeated_double(2)); 705 EXPECT_EQ(1.23E17 , message.repeated_double(3)); 706 EXPECT_EQ(1.235E22 , message.repeated_double(4)); 707 EXPECT_EQ(1.235E-18 , message.repeated_double(5)); 708 EXPECT_EQ(123.456789, message.repeated_double(6)); 709 EXPECT_EQ(message.repeated_double(7), numeric_limits<double>::infinity()); 710 EXPECT_EQ(message.repeated_double(8), numeric_limits<double>::infinity()); 711 EXPECT_EQ(message.repeated_double(9), -numeric_limits<double>::infinity()); 712 EXPECT_EQ(message.repeated_double(10), -numeric_limits<double>::infinity()); 713 EXPECT_TRUE(IsNaN(message.repeated_double(11))); 714 EXPECT_TRUE(IsNaN(message.repeated_double(12))); 715 716 // Note: Since these string literals have \0's in them, we must explicitly 717 // pass their sizes to string's constructor. 718 ASSERT_EQ(1, message.repeated_string_size()); 719 EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), 720 message.repeated_string(0)); 721} 722 723class TextFormatParserTest : public testing::Test { 724 protected: 725 void ExpectFailure(const string& input, const string& message, int line, 726 int col) { 727 scoped_ptr<unittest::TestAllTypes> proto(new unittest::TestAllTypes); 728 ExpectFailure(input, message, line, col, proto.get()); 729 } 730 731 void ExpectFailure(const string& input, const string& message, int line, 732 int col, Message* proto) { 733 ExpectMessage(input, message, line, col, proto, false); 734 } 735 736 void ExpectMessage(const string& input, const string& message, int line, 737 int col, Message* proto, bool expected_result) { 738 TextFormat::Parser parser; 739 MockErrorCollector error_collector; 740 parser.RecordErrorsTo(&error_collector); 741 EXPECT_EQ(parser.ParseFromString(input, proto), expected_result); 742 EXPECT_EQ(SimpleItoa(line) + ":" + SimpleItoa(col) + ": " + message + "\n", 743 error_collector.text_); 744 } 745 746 void ExpectSuccessAndTree(const string& input, Message* proto, 747 TextFormat::ParseInfoTree* info_tree) { 748 TextFormat::Parser parser; 749 MockErrorCollector error_collector; 750 parser.RecordErrorsTo(&error_collector); 751 parser.WriteLocationsTo(info_tree); 752 753 EXPECT_TRUE(parser.ParseFromString(input, proto)); 754 } 755 756 void ExpectLocation(TextFormat::ParseInfoTree* tree, 757 const Descriptor* d, const string& field_name, 758 int index, int line, int column) { 759 TextFormat::ParseLocation location = tree->GetLocation( 760 d->FindFieldByName(field_name), index); 761 EXPECT_EQ(line, location.line); 762 EXPECT_EQ(column, location.column); 763 } 764 765 // An error collector which simply concatenates all its errors into a big 766 // block of text which can be checked. 767 class MockErrorCollector : public io::ErrorCollector { 768 public: 769 MockErrorCollector() {} 770 ~MockErrorCollector() {} 771 772 string text_; 773 774 // implements ErrorCollector ------------------------------------- 775 void AddError(int line, int column, const string& message) { 776 strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", 777 line + 1, column + 1, message); 778 } 779 780 void AddWarning(int line, int column, const string& message) { 781 AddError(line, column, "WARNING:" + message); 782 } 783 }; 784}; 785 786TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) { 787 scoped_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes); 788 const Descriptor* d = message->GetDescriptor(); 789 790 string stringData = 791 "optional_int32: 1\n" 792 "optional_int64: 2\n" 793 " optional_double: 2.4\n" 794 "repeated_int32: 5\n" 795 "repeated_int32: 10\n" 796 "optional_nested_message <\n" 797 " bb: 78\n" 798 ">\n" 799 "repeated_nested_message <\n" 800 " bb: 79\n" 801 ">\n" 802 "repeated_nested_message <\n" 803 " bb: 80\n" 804 ">"; 805 806 807 TextFormat::ParseInfoTree tree; 808 ExpectSuccessAndTree(stringData, message.get(), &tree); 809 810 // Verify that the tree has the correct positions. 811 ExpectLocation(&tree, d, "optional_int32", -1, 0, 0); 812 ExpectLocation(&tree, d, "optional_int64", -1, 1, 0); 813 ExpectLocation(&tree, d, "optional_double", -1, 2, 2); 814 815 ExpectLocation(&tree, d, "repeated_int32", 0, 3, 0); 816 ExpectLocation(&tree, d, "repeated_int32", 1, 4, 0); 817 818 ExpectLocation(&tree, d, "optional_nested_message", -1, 5, 0); 819 ExpectLocation(&tree, d, "repeated_nested_message", 0, 8, 0); 820 ExpectLocation(&tree, d, "repeated_nested_message", 1, 11, 0); 821 822 // Check for fields not set. For an invalid field, the location returned 823 // should be -1, -1. 824 ExpectLocation(&tree, d, "repeated_int64", 0, -1, -1); 825 ExpectLocation(&tree, d, "repeated_int32", 6, -1, -1); 826 ExpectLocation(&tree, d, "some_unknown_field", -1, -1, -1); 827 828 // Verify inside the nested message. 829 const FieldDescriptor* nested_field = 830 d->FindFieldByName("optional_nested_message"); 831 832 TextFormat::ParseInfoTree* nested_tree = 833 tree.GetTreeForNested(nested_field, -1); 834 ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 6, 2); 835 836 // Verify inside another nested message. 837 nested_field = d->FindFieldByName("repeated_nested_message"); 838 nested_tree = tree.GetTreeForNested(nested_field, 0); 839 ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 9, 2); 840 841 nested_tree = tree.GetTreeForNested(nested_field, 1); 842 ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 12, 2); 843 844 // Verify a NULL tree for an unknown nested field. 845 TextFormat::ParseInfoTree* unknown_nested_tree = 846 tree.GetTreeForNested(nested_field, 2); 847 848 EXPECT_EQ(NULL, unknown_nested_tree); 849} 850 851TEST_F(TextFormatParserTest, ParseFieldValueFromString) { 852 scoped_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes); 853 const Descriptor* d = message->GetDescriptor(); 854 855#define EXPECT_FIELD(name, value, valuestring) \ 856 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ 857 valuestring, d->FindFieldByName("optional_" #name), message.get())); \ 858 EXPECT_EQ(value, message->optional_##name()); \ 859 EXPECT_TRUE(message->has_optional_##name()); 860 861#define EXPECT_BOOL_FIELD(name, value, valuestring) \ 862 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ 863 valuestring, d->FindFieldByName("optional_" #name), message.get())); \ 864 EXPECT_TRUE(message->optional_##name() == value); \ 865 EXPECT_TRUE(message->has_optional_##name()); 866 867#define EXPECT_FLOAT_FIELD(name, value, valuestring) \ 868 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ 869 valuestring, d->FindFieldByName("optional_" #name), message.get())); \ 870 EXPECT_FLOAT_EQ(value, message->optional_##name()); \ 871 EXPECT_TRUE(message->has_optional_##name()); 872 873#define EXPECT_DOUBLE_FIELD(name, value, valuestring) \ 874 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ 875 valuestring, d->FindFieldByName("optional_" #name), message.get())); \ 876 EXPECT_DOUBLE_EQ(value, message->optional_##name()); \ 877 EXPECT_TRUE(message->has_optional_##name()); 878 879#define EXPECT_INVALID(name, valuestring) \ 880 EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \ 881 valuestring, d->FindFieldByName("optional_" #name), message.get())); 882 883 // int32 884 EXPECT_FIELD(int32, 1, "1"); 885 EXPECT_FIELD(int32, -1, "-1"); 886 EXPECT_FIELD(int32, 0x1234, "0x1234"); 887 EXPECT_INVALID(int32, "a"); 888 EXPECT_INVALID(int32, "999999999999999999999999999999999999"); 889 EXPECT_INVALID(int32, "1,2"); 890 891 // int64 892 EXPECT_FIELD(int64, 1, "1"); 893 EXPECT_FIELD(int64, -1, "-1"); 894 EXPECT_FIELD(int64, 0x1234567812345678LL, "0x1234567812345678"); 895 EXPECT_INVALID(int64, "a"); 896 EXPECT_INVALID(int64, "999999999999999999999999999999999999"); 897 EXPECT_INVALID(int64, "1,2"); 898 899 // uint64 900 EXPECT_FIELD(uint64, 1, "1"); 901 EXPECT_FIELD(uint64, 0xf234567812345678ULL, "0xf234567812345678"); 902 EXPECT_INVALID(uint64, "-1"); 903 EXPECT_INVALID(uint64, "a"); 904 EXPECT_INVALID(uint64, "999999999999999999999999999999999999"); 905 EXPECT_INVALID(uint64, "1,2"); 906 907 // fixed32 908 EXPECT_FIELD(fixed32, 1, "1"); 909 EXPECT_FIELD(fixed32, 0x12345678, "0x12345678"); 910 EXPECT_INVALID(fixed32, "-1"); 911 EXPECT_INVALID(fixed32, "a"); 912 EXPECT_INVALID(fixed32, "999999999999999999999999999999999999"); 913 EXPECT_INVALID(fixed32, "1,2"); 914 915 // fixed64 916 EXPECT_FIELD(fixed64, 1, "1"); 917 EXPECT_FIELD(fixed64, 0x1234567812345678ULL, "0x1234567812345678"); 918 EXPECT_INVALID(fixed64, "-1"); 919 EXPECT_INVALID(fixed64, "a"); 920 EXPECT_INVALID(fixed64, "999999999999999999999999999999999999"); 921 EXPECT_INVALID(fixed64, "1,2"); 922 923 // bool 924 EXPECT_BOOL_FIELD(bool, true, "true"); 925 EXPECT_BOOL_FIELD(bool, false, "false"); 926 EXPECT_BOOL_FIELD(bool, true, "1"); 927 EXPECT_BOOL_FIELD(bool, true, "t"); 928 EXPECT_BOOL_FIELD(bool, false, "0"); 929 EXPECT_BOOL_FIELD(bool, false, "f"); 930 EXPECT_INVALID(bool, "2"); 931 EXPECT_INVALID(bool, "-0"); 932 EXPECT_INVALID(bool, "on"); 933 EXPECT_INVALID(bool, "a"); 934 EXPECT_INVALID(bool, "True"); 935 936 // float 937 EXPECT_FIELD(float, 1, "1"); 938 EXPECT_FLOAT_FIELD(float, 1.5, "1.5"); 939 EXPECT_FLOAT_FIELD(float, 1.5e3, "1.5e3"); 940 EXPECT_FLOAT_FIELD(float, -4.55, "-4.55"); 941 EXPECT_INVALID(float, "a"); 942 EXPECT_INVALID(float, "1,2"); 943 944 // double 945 EXPECT_FIELD(double, 1, "1"); 946 EXPECT_FIELD(double, -1, "-1"); 947 EXPECT_DOUBLE_FIELD(double, 2.3, "2.3"); 948 EXPECT_DOUBLE_FIELD(double, 3e5, "3e5"); 949 EXPECT_INVALID(double, "a"); 950 EXPECT_INVALID(double, "1,2"); 951 952 // string 953 EXPECT_FIELD(string, "hello", "\"hello\""); 954 EXPECT_FIELD(string, "-1.87", "'-1.87'"); 955 EXPECT_INVALID(string, "hello"); // without quote for value 956 957 // enum 958 EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR"); 959 EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ, 960 SimpleItoa(unittest::TestAllTypes::BAZ)); 961 EXPECT_INVALID(nested_enum, "FOOBAR"); 962 963 // message 964 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( 965 "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get())); 966 EXPECT_EQ(12, message->optional_nested_message().bb()); \ 967 EXPECT_TRUE(message->has_optional_nested_message()); 968 EXPECT_INVALID(nested_message, "any"); 969 970#undef EXPECT_FIELD 971#undef EXPECT_BOOL_FIELD 972#undef EXPECT_FLOAT_FIELD 973#undef EXPECT_DOUBLE_FIELD 974#undef EXPECT_INVALID 975} 976 977 978TEST_F(TextFormatParserTest, InvalidToken) { 979 ExpectFailure("optional_bool: true\n-5\n", "Expected identifier.", 980 2, 1); 981 982 ExpectFailure("optional_bool: true!\n", "Expected identifier.", 1, 20); 983 ExpectFailure("\"some string\"", "Expected identifier.", 1, 1); 984} 985 986TEST_F(TextFormatParserTest, InvalidFieldName) { 987 ExpectFailure( 988 "invalid_field: somevalue\n", 989 "Message type \"protobuf_unittest.TestAllTypes\" has no field named " 990 "\"invalid_field\".", 991 1, 14); 992} 993 994TEST_F(TextFormatParserTest, InvalidCapitalization) { 995 // We require that group names be exactly as they appear in the .proto. 996 ExpectFailure( 997 "optionalgroup {\na: 15\n}\n", 998 "Message type \"protobuf_unittest.TestAllTypes\" has no field named " 999 "\"optionalgroup\".", 1000 1, 15); 1001 ExpectFailure( 1002 "OPTIONALgroup {\na: 15\n}\n", 1003 "Message type \"protobuf_unittest.TestAllTypes\" has no field named " 1004 "\"OPTIONALgroup\".", 1005 1, 15); 1006 ExpectFailure( 1007 "Optional_Double: 10.0\n", 1008 "Message type \"protobuf_unittest.TestAllTypes\" has no field named " 1009 "\"Optional_Double\".", 1010 1, 16); 1011} 1012 1013TEST_F(TextFormatParserTest, InvalidFieldValues) { 1014 // Invalid values for a double/float field. 1015 ExpectFailure("optional_double: \"hello\"\n", "Expected double.", 1, 18); 1016 ExpectFailure("optional_double: true\n", "Expected double.", 1, 18); 1017 ExpectFailure("optional_double: !\n", "Expected double.", 1, 18); 1018 ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", 1019 1, 17); 1020 1021 // Invalid values for a signed integer field. 1022 ExpectFailure("optional_int32: \"hello\"\n", "Expected integer.", 1, 17); 1023 ExpectFailure("optional_int32: true\n", "Expected integer.", 1, 17); 1024 ExpectFailure("optional_int32: 4.5\n", "Expected integer.", 1, 17); 1025 ExpectFailure("optional_int32: !\n", "Expected integer.", 1, 17); 1026 ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", 1027 1, 16); 1028 ExpectFailure("optional_int32: 0x80000000\n", 1029 "Integer out of range.", 1, 17); 1030 ExpectFailure("optional_int64: 0x8000000000000000\n", 1031 "Integer out of range.", 1, 17); 1032 ExpectFailure("optional_int32: -0x80000001\n", 1033 "Integer out of range.", 1, 18); 1034 ExpectFailure("optional_int64: -0x8000000000000001\n", 1035 "Integer out of range.", 1, 18); 1036 1037 // Invalid values for an unsigned integer field. 1038 ExpectFailure("optional_uint64: \"hello\"\n", "Expected integer.", 1, 18); 1039 ExpectFailure("optional_uint64: true\n", "Expected integer.", 1, 18); 1040 ExpectFailure("optional_uint64: 4.5\n", "Expected integer.", 1, 18); 1041 ExpectFailure("optional_uint64: -5\n", "Expected integer.", 1, 18); 1042 ExpectFailure("optional_uint64: !\n", "Expected integer.", 1, 18); 1043 ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", 1044 1, 17); 1045 ExpectFailure("optional_uint32: 0x100000000\n", 1046 "Integer out of range.", 1, 18); 1047 ExpectFailure("optional_uint64: 0x10000000000000000\n", 1048 "Integer out of range.", 1, 18); 1049 1050 // Invalid values for a boolean field. 1051 ExpectFailure("optional_bool: \"hello\"\n", "Expected identifier.", 1, 16); 1052 ExpectFailure("optional_bool: 5\n", "Integer out of range.", 1, 16); 1053 ExpectFailure("optional_bool: -7.5\n", "Expected identifier.", 1, 16); 1054 ExpectFailure("optional_bool: !\n", "Expected identifier.", 1, 16); 1055 1056 ExpectFailure( 1057 "optional_bool: meh\n", 1058 "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 1059 2, 1); 1060 1061 ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", 1062 1, 15); 1063 1064 // Invalid values for a string field. 1065 ExpectFailure("optional_string: true\n", "Expected string.", 1, 18); 1066 ExpectFailure("optional_string: 5\n", "Expected string.", 1, 18); 1067 ExpectFailure("optional_string: -7.5\n", "Expected string.", 1, 18); 1068 ExpectFailure("optional_string: !\n", "Expected string.", 1, 18); 1069 ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", 1070 1, 17); 1071 1072 // Invalid values for an enumeration field. 1073 ExpectFailure("optional_nested_enum: \"hello\"\n", 1074 "Expected integer or identifier.", 1, 23); 1075 1076 // Valid token, but enum value is not defined. 1077 ExpectFailure("optional_nested_enum: 5\n", 1078 "Unknown enumeration value of \"5\" for field " 1079 "\"optional_nested_enum\".", 2, 1); 1080 // We consume the negative sign, so the error position starts one character 1081 // later. 1082 ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer.", 1, 24); 1083 ExpectFailure("optional_nested_enum: !\n", 1084 "Expected integer or identifier.", 1, 23); 1085 1086 ExpectFailure( 1087 "optional_nested_enum: grah\n", 1088 "Unknown enumeration value of \"grah\" for field " 1089 "\"optional_nested_enum\".", 2, 1); 1090 1091 ExpectFailure( 1092 "optional_nested_enum {\n \n}\n", 1093 "Expected \":\", found \"{\".", 1, 22); 1094} 1095 1096TEST_F(TextFormatParserTest, MessageDelimeters) { 1097 // Non-matching delimeters. 1098 ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", 1099 3, 1); 1100 1101 // Invalid delimeters. 1102 ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", 1103 1, 15); 1104 1105 // Unending message. 1106 ExpectFailure("optional_nested_message {\n \nbb: 118\n", 1107 "Expected identifier.", 1108 4, 1); 1109} 1110 1111TEST_F(TextFormatParserTest, UnknownExtension) { 1112 // Non-matching delimeters. 1113 ExpectFailure("[blahblah]: 123", 1114 "Extension \"blahblah\" is not defined or is not an " 1115 "extension of \"protobuf_unittest.TestAllTypes\".", 1116 1, 11); 1117} 1118 1119TEST_F(TextFormatParserTest, MissingRequired) { 1120 unittest::TestRequired message; 1121 ExpectFailure("a: 1", 1122 "Message missing required fields: b, c", 1123 0, 1, &message); 1124} 1125 1126TEST_F(TextFormatParserTest, ParseDuplicateRequired) { 1127 unittest::TestRequired message; 1128 ExpectFailure("a: 1 b: 2 c: 3 a: 1", 1129 "Non-repeated field \"a\" is specified multiple times.", 1130 1, 17, &message); 1131} 1132 1133TEST_F(TextFormatParserTest, ParseDuplicateOptional) { 1134 unittest::ForeignMessage message; 1135 ExpectFailure("c: 1 c: 2", 1136 "Non-repeated field \"c\" is specified multiple times.", 1137 1, 7, &message); 1138} 1139 1140TEST_F(TextFormatParserTest, MergeDuplicateRequired) { 1141 unittest::TestRequired message; 1142 TextFormat::Parser parser; 1143 EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message)); 1144 EXPECT_EQ(4, message.a()); 1145} 1146 1147TEST_F(TextFormatParserTest, MergeDuplicateOptional) { 1148 unittest::ForeignMessage message; 1149 TextFormat::Parser parser; 1150 EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message)); 1151 EXPECT_EQ(2, message.c()); 1152} 1153 1154TEST_F(TextFormatParserTest, ExplicitDelimiters) { 1155 unittest::TestRequired message; 1156 EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message)); 1157 EXPECT_EQ(1, message.a()); 1158 EXPECT_EQ(2, message.b()); 1159 EXPECT_EQ(3, message.c()); 1160} 1161 1162TEST_F(TextFormatParserTest, PrintErrorsToStderr) { 1163 vector<string> errors; 1164 1165 { 1166 ScopedMemoryLog log; 1167 unittest::TestAllTypes proto; 1168 EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto)); 1169 errors = log.GetMessages(ERROR); 1170 } 1171 1172 ASSERT_EQ(1, errors.size()); 1173 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " 1174 "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field " 1175 "named \"no_such_field\".", 1176 errors[0]); 1177} 1178 1179TEST_F(TextFormatParserTest, FailsOnTokenizationError) { 1180 vector<string> errors; 1181 1182 { 1183 ScopedMemoryLog log; 1184 unittest::TestAllTypes proto; 1185 EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto)); 1186 errors = log.GetMessages(ERROR); 1187 } 1188 1189 ASSERT_EQ(1, errors.size()); 1190 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " 1191 "1:1: Invalid control characters encountered in text.", 1192 errors[0]); 1193} 1194 1195TEST_F(TextFormatParserTest, ParseDeprecatedField) { 1196 unittest::TestDeprecatedFields message; 1197 ExpectMessage("deprecated_int32: 42", 1198 "WARNING:text format contains deprecated field " 1199 "\"deprecated_int32\"", 1, 21, &message, true); 1200} 1201 1202class TextFormatMessageSetTest : public testing::Test { 1203 protected: 1204 static const char proto_debug_string_[]; 1205}; 1206const char TextFormatMessageSetTest::proto_debug_string_[] = 1207"message_set {\n" 1208" [protobuf_unittest.TestMessageSetExtension1] {\n" 1209" i: 23\n" 1210" }\n" 1211" [protobuf_unittest.TestMessageSetExtension2] {\n" 1212" str: \"foo\"\n" 1213" }\n" 1214"}\n"; 1215 1216 1217TEST_F(TextFormatMessageSetTest, Serialize) { 1218 protobuf_unittest::TestMessageSetContainer proto; 1219 protobuf_unittest::TestMessageSetExtension1* item_a = 1220 proto.mutable_message_set()->MutableExtension( 1221 protobuf_unittest::TestMessageSetExtension1::message_set_extension); 1222 item_a->set_i(23); 1223 protobuf_unittest::TestMessageSetExtension2* item_b = 1224 proto.mutable_message_set()->MutableExtension( 1225 protobuf_unittest::TestMessageSetExtension2::message_set_extension); 1226 item_b->set_str("foo"); 1227 EXPECT_EQ(proto_debug_string_, proto.DebugString()); 1228} 1229 1230TEST_F(TextFormatMessageSetTest, Deserialize) { 1231 protobuf_unittest::TestMessageSetContainer proto; 1232 ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto)); 1233 EXPECT_EQ(23, proto.message_set().GetExtension( 1234 protobuf_unittest::TestMessageSetExtension1::message_set_extension).i()); 1235 EXPECT_EQ("foo", proto.message_set().GetExtension( 1236 protobuf_unittest::TestMessageSetExtension2::message_set_extension).str()); 1237 1238 // Ensure that these are the only entries present. 1239 vector<const FieldDescriptor*> descriptors; 1240 proto.message_set().GetReflection()->ListFields( 1241 proto.message_set(), &descriptors); 1242 EXPECT_EQ(2, descriptors.size()); 1243} 1244 1245 1246} // namespace text_format_unittest 1247} // namespace protobuf 1248} // namespace google 1249