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