Imported Upstream version 3.8.0
[platform/upstream/protobuf.git] / src / google / protobuf / text_format_unittest.cc
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
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/stubs/logging.h>
45 #include <google/protobuf/testing/file.h>
46 #include <google/protobuf/testing/file.h>
47 #include <google/protobuf/map_unittest.pb.h>
48 #include <google/protobuf/test_util.h>
49 #include <google/protobuf/test_util2.h>
50 #include <google/protobuf/unittest.pb.h>
51 #include <google/protobuf/unittest_mset.pb.h>
52 #include <google/protobuf/unittest_mset_wire_format.pb.h>
53 #include <google/protobuf/unittest_proto3.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
58
59 #include <google/protobuf/stubs/substitute.h>
60 #include <google/protobuf/testing/googletest.h>
61 #include <gtest/gtest.h>
62 #include <google/protobuf/stubs/mathlimits.h>
63
64
65 #include <google/protobuf/port_def.inc>
66
67 namespace google {
68 namespace protobuf {
69
70 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
71 namespace text_format_unittest {
72
73 // A basic string with different escapable characters for testing.
74 const std::string kEscapeTestString =
75     "\"A string with ' characters \n and \r newlines and \t tabs and \001 "
76     "slashes \\ and  multiple   spaces";
77
78 // A representation of the above string with all the characters escaped.
79 const std::string kEscapeTestStringEscaped =
80     "\"\\\"A string with \\' characters \\n and \\r newlines "
81     "and \\t tabs and \\001 slashes \\\\ and  multiple   spaces\"";
82
83 class TextFormatTest : public testing::Test {
84  public:
85   static void SetUpTestCase() {
86     GOOGLE_CHECK_OK(File::GetContents(
87         TestUtil::GetTestDataPath(
88             "net/proto2/internal/"
89             "testdata/text_format_unittest_data_oneof_implemented.txt"),
90         &static_proto_debug_string_, true));
91     CleanStringLineEndings(&static_proto_debug_string_, false);
92   }
93
94   TextFormatTest() : proto_debug_string_(static_proto_debug_string_) {}
95
96  protected:
97   // Debug string read from text_format_unittest_data.txt.
98   const std::string proto_debug_string_;
99   unittest::TestAllTypes proto_;
100
101  private:
102   static std::string static_proto_debug_string_;
103 };
104 std::string TextFormatTest::static_proto_debug_string_;
105
106 class TextFormatExtensionsTest : public testing::Test {
107  public:
108   static void SetUpTestCase() {
109     GOOGLE_CHECK_OK(File::GetContents(
110         TestUtil::GetTestDataPath("net/proto2/internal/testdata/"
111                                   "text_format_unittest_extensions_data.txt"),
112         &static_proto_debug_string_, true));
113     CleanStringLineEndings(&static_proto_debug_string_, false);
114   }
115
116   TextFormatExtensionsTest()
117       : proto_debug_string_(static_proto_debug_string_) {}
118
119  protected:
120   // Debug string read from text_format_unittest_data.txt.
121   const std::string proto_debug_string_;
122   unittest::TestAllExtensions proto_;
123
124  private:
125   static std::string static_proto_debug_string_;
126 };
127 std::string TextFormatExtensionsTest::static_proto_debug_string_;
128
129 TEST_F(TextFormatTest, Basic) {
130   TestUtil::SetAllFields(&proto_);
131   EXPECT_EQ(proto_debug_string_, proto_.DebugString());
132 }
133
134 TEST_F(TextFormatExtensionsTest, Extensions) {
135   TestUtil::SetAllExtensions(&proto_);
136   EXPECT_EQ(proto_debug_string_, proto_.DebugString());
137 }
138
139 TEST_F(TextFormatTest, ShortDebugString) {
140   proto_.set_optional_int32(1);
141   proto_.set_optional_string("hello");
142   proto_.mutable_optional_nested_message()->set_bb(2);
143   proto_.mutable_optional_foreign_message();
144
145   EXPECT_EQ(
146       "optional_int32: 1 optional_string: \"hello\" "
147       "optional_nested_message { bb: 2 } "
148       "optional_foreign_message { }",
149       proto_.ShortDebugString());
150 }
151
152 TEST_F(TextFormatTest, ShortPrimitiveRepeateds) {
153   proto_.set_optional_int32(123);
154   proto_.add_repeated_int32(456);
155   proto_.add_repeated_int32(789);
156   proto_.add_repeated_string("foo");
157   proto_.add_repeated_string("bar");
158   proto_.add_repeated_nested_message()->set_bb(2);
159   proto_.add_repeated_nested_message()->set_bb(3);
160   proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO);
161   proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR);
162
163   TextFormat::Printer printer;
164   printer.SetUseShortRepeatedPrimitives(true);
165   std::string text;
166   EXPECT_TRUE(printer.PrintToString(proto_, &text));
167
168   EXPECT_EQ(
169       "optional_int32: 123\n"
170       "repeated_int32: [456, 789]\n"
171       "repeated_string: \"foo\"\n"
172       "repeated_string: \"bar\"\n"
173       "repeated_nested_message {\n  bb: 2\n}\n"
174       "repeated_nested_message {\n  bb: 3\n}\n"
175       "repeated_nested_enum: [FOO, BAR]\n",
176       text);
177
178   // Verify that any existing data in the string is cleared when PrintToString()
179   // is called.
180   text = "just some data here...\n\nblah blah";
181   EXPECT_TRUE(printer.PrintToString(proto_, &text));
182
183   EXPECT_EQ(
184       "optional_int32: 123\n"
185       "repeated_int32: [456, 789]\n"
186       "repeated_string: \"foo\"\n"
187       "repeated_string: \"bar\"\n"
188       "repeated_nested_message {\n  bb: 2\n}\n"
189       "repeated_nested_message {\n  bb: 3\n}\n"
190       "repeated_nested_enum: [FOO, BAR]\n",
191       text);
192
193   // Try in single-line mode.
194   printer.SetSingleLineMode(true);
195   EXPECT_TRUE(printer.PrintToString(proto_, &text));
196
197   EXPECT_EQ(
198       "optional_int32: 123 "
199       "repeated_int32: [456, 789] "
200       "repeated_string: \"foo\" "
201       "repeated_string: \"bar\" "
202       "repeated_nested_message { bb: 2 } "
203       "repeated_nested_message { bb: 3 } "
204       "repeated_nested_enum: [FOO, BAR] ",
205       text);
206 }
207
208
209 TEST_F(TextFormatTest, StringEscape) {
210   // Set the string value to test.
211   proto_.set_optional_string(kEscapeTestString);
212
213   // Get the DebugString from the proto.
214   std::string debug_string = proto_.DebugString();
215   std::string utf8_debug_string = proto_.Utf8DebugString();
216
217   // Hardcode a correct value to test against.
218   std::string correct_string =
219       "optional_string: " + kEscapeTestStringEscaped + "\n";
220
221   // Compare.
222   EXPECT_EQ(correct_string, debug_string);
223   // UTF-8 string is the same as non-UTF-8 because
224   // the protocol buffer contains no UTF-8 text.
225   EXPECT_EQ(correct_string, utf8_debug_string);
226
227   std::string expected_short_debug_string =
228       "optional_string: " + kEscapeTestStringEscaped;
229   EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString());
230 }
231
232 TEST_F(TextFormatTest, Utf8DebugString) {
233   // Set the string value to test.
234   proto_.set_optional_string("\350\260\267\346\255\214");
235   proto_.set_optional_bytes("\350\260\267\346\255\214");
236
237   // Get the DebugString from the proto.
238   std::string debug_string = proto_.DebugString();
239   std::string utf8_debug_string = proto_.Utf8DebugString();
240
241   // Hardcode a correct value to test against.
242   std::string correct_utf8_string =
243       "optional_string: "
244       "\"\350\260\267\346\255\214\""
245       "\n"
246       "optional_bytes: "
247       "\"\\350\\260\\267\\346\\255\\214\""
248       "\n";
249   std::string correct_string =
250       "optional_string: "
251       "\"\\350\\260\\267\\346\\255\\214\""
252       "\n"
253       "optional_bytes: "
254       "\"\\350\\260\\267\\346\\255\\214\""
255       "\n";
256
257   // Compare.
258   EXPECT_EQ(correct_utf8_string, utf8_debug_string);
259   EXPECT_EQ(correct_string, debug_string);
260 }
261
262 TEST_F(TextFormatTest, PrintUnknownFields) {
263   // Test printing of unknown fields in a message.
264
265   unittest::TestEmptyMessage message;
266   UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
267
268   unknown_fields->AddVarint(5, 1);
269   unknown_fields->AddFixed32(5, 2);
270   unknown_fields->AddFixed64(5, 3);
271   unknown_fields->AddLengthDelimited(5, "4");
272   unknown_fields->AddGroup(5)->AddVarint(10, 5);
273
274   unknown_fields->AddVarint(8, 1);
275   unknown_fields->AddVarint(8, 2);
276   unknown_fields->AddVarint(8, 3);
277
278   EXPECT_EQ(
279       "5: 1\n"
280       "5: 0x00000002\n"
281       "5: 0x0000000000000003\n"
282       "5: \"4\"\n"
283       "5 {\n"
284       "  10: 5\n"
285       "}\n"
286       "8: 1\n"
287       "8: 2\n"
288       "8: 3\n",
289       message.DebugString());
290 }
291
292 TEST_F(TextFormatTest, PrintUnknownFieldsHidden) {
293   // Test printing of unknown fields in a message when suppressed.
294
295   unittest::OneString message;
296   message.set_data("data");
297   UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
298
299   unknown_fields->AddVarint(5, 1);
300   unknown_fields->AddFixed32(5, 2);
301   unknown_fields->AddFixed64(5, 3);
302   unknown_fields->AddLengthDelimited(5, "4");
303   unknown_fields->AddGroup(5)->AddVarint(10, 5);
304
305   unknown_fields->AddVarint(8, 1);
306   unknown_fields->AddVarint(8, 2);
307   unknown_fields->AddVarint(8, 3);
308
309   TextFormat::Printer printer;
310   printer.SetHideUnknownFields(true);
311   std::string output;
312   printer.PrintToString(message, &output);
313
314   EXPECT_EQ("data: \"data\"\n", output);
315 }
316
317 TEST_F(TextFormatTest, PrintUnknownMessage) {
318   // Test heuristic printing of messages in an UnknownFieldSet.
319
320   protobuf_unittest::TestAllTypes message;
321
322   // Cases which should not be interpreted as sub-messages.
323
324   // 'a' is a valid FIXED64 tag, so for the string to be parseable as a message
325   // it should be followed by 8 bytes.  Since this string only has two
326   // subsequent bytes, it should be treated as a string.
327   message.add_repeated_string("abc");
328
329   // 'd' happens to be a valid ENDGROUP tag.  So,
330   // UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but
331   // the ConsumedEntireMessage() check should fail.
332   message.add_repeated_string("def");
333
334   // A zero-length string should never be interpreted as a message even though
335   // it is technically valid as one.
336   message.add_repeated_string("");
337
338   // Case which should be interpreted as a sub-message.
339
340   // An actual nested message with content should always be interpreted as a
341   // nested message.
342   message.add_repeated_nested_message()->set_bb(123);
343
344   std::string data;
345   message.SerializeToString(&data);
346
347   std::string text;
348   UnknownFieldSet unknown_fields;
349   EXPECT_TRUE(unknown_fields.ParseFromString(data));
350   EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text));
351   EXPECT_EQ(
352       "44: \"abc\"\n"
353       "44: \"def\"\n"
354       "44: \"\"\n"
355       "48 {\n"
356       "  1: 123\n"
357       "}\n",
358       text);
359 }
360
361 TEST_F(TextFormatTest, PrintMessageWithIndent) {
362   // Test adding an initial indent to printing.
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   std::string text;
371   TextFormat::Printer printer;
372   printer.SetInitialIndentLevel(1);
373   EXPECT_TRUE(printer.PrintToString(message, &text));
374   EXPECT_EQ(
375       "  repeated_string: \"abc\"\n"
376       "  repeated_string: \"def\"\n"
377       "  repeated_nested_message {\n"
378       "    bb: 123\n"
379       "  }\n",
380       text);
381 }
382
383 TEST_F(TextFormatTest, PrintMessageSingleLine) {
384   // Test printing a message on a single line.
385
386   protobuf_unittest::TestAllTypes message;
387
388   message.add_repeated_string("abc");
389   message.add_repeated_string("def");
390   message.add_repeated_nested_message()->set_bb(123);
391
392   std::string text;
393   TextFormat::Printer printer;
394   printer.SetInitialIndentLevel(1);
395   printer.SetSingleLineMode(true);
396   EXPECT_TRUE(printer.PrintToString(message, &text));
397   EXPECT_EQ(
398       "  repeated_string: \"abc\" repeated_string: \"def\" "
399       "repeated_nested_message { bb: 123 } ",
400       text);
401 }
402
403 TEST_F(TextFormatTest, PrintBufferTooSmall) {
404   // Test printing a message to a buffer that is too small.
405
406   protobuf_unittest::TestAllTypes message;
407
408   message.add_repeated_string("abc");
409   message.add_repeated_string("def");
410
411   char buffer[1] = "";
412   io::ArrayOutputStream output_stream(buffer, 1);
413   EXPECT_FALSE(TextFormat::Print(message, &output_stream));
414   EXPECT_EQ(buffer[0], 'r');
415   EXPECT_EQ(output_stream.ByteCount(), 1);
416 }
417
418 // A printer that appends 'u' to all unsigned int32.
419 class CustomUInt32FieldValuePrinter : public TextFormat::FieldValuePrinter {
420  public:
421   virtual std::string PrintUInt32(uint32 val) const {
422     return StrCat(FieldValuePrinter::PrintUInt32(val), "u");
423   }
424 };
425
426 TEST_F(TextFormatTest, DefaultCustomFieldPrinter) {
427   protobuf_unittest::TestAllTypes message;
428
429   message.set_optional_uint32(42);
430   message.add_repeated_uint32(1);
431   message.add_repeated_uint32(2);
432   message.add_repeated_uint32(3);
433
434   TextFormat::Printer printer;
435   printer.SetDefaultFieldValuePrinter(new CustomUInt32FieldValuePrinter());
436   // Let's see if that works well together with the repeated primitives:
437   printer.SetUseShortRepeatedPrimitives(true);
438   std::string text;
439   printer.PrintToString(message, &text);
440   EXPECT_EQ("optional_uint32: 42u\nrepeated_uint32: [1u, 2u, 3u]\n", text);
441 }
442
443 class CustomInt32FieldValuePrinter : public TextFormat::FieldValuePrinter {
444  public:
445   virtual std::string PrintInt32(int32 val) const {
446     return StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")");
447   }
448 };
449
450 TEST_F(TextFormatTest, FieldSpecificCustomPrinter) {
451   protobuf_unittest::TestAllTypes message;
452
453   message.set_optional_int32(42);  // This will be handled by our Printer.
454   message.add_repeated_int32(42);  // This will be printed as number.
455
456   TextFormat::Printer printer;
457   EXPECT_TRUE(printer.RegisterFieldValuePrinter(
458       message.GetDescriptor()->FindFieldByName("optional_int32"),
459       new CustomInt32FieldValuePrinter()));
460   std::string text;
461   printer.PrintToString(message, &text);
462   EXPECT_EQ("optional_int32: value-is(42)\nrepeated_int32: 42\n", text);
463 }
464
465 TEST_F(TextFormatTest, FieldSpecificCustomPrinterRegisterSameFieldTwice) {
466   protobuf_unittest::TestAllTypes message;
467   TextFormat::Printer printer;
468   const FieldDescriptor* const field =
469       message.GetDescriptor()->FindFieldByName("optional_int32");
470   ASSERT_TRUE(printer.RegisterFieldValuePrinter(
471       field, new CustomInt32FieldValuePrinter()));
472   const TextFormat::FieldValuePrinter* const rejected =
473       new CustomInt32FieldValuePrinter();
474   ASSERT_FALSE(printer.RegisterFieldValuePrinter(field, rejected));
475   delete rejected;
476 }
477
478 TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) {
479   protobuf_unittest::TestAllTypes message;
480   TextFormat::Printer printer;
481   // NULL printer.
482   EXPECT_FALSE(printer.RegisterFieldValuePrinter(
483       message.GetDescriptor()->FindFieldByName("optional_int32"),
484       static_cast<const TextFormat::FieldValuePrinter*>(nullptr)));
485   EXPECT_FALSE(printer.RegisterFieldValuePrinter(
486       message.GetDescriptor()->FindFieldByName("optional_int32"),
487       static_cast<const TextFormat::FastFieldValuePrinter*>(nullptr)));
488   // Because registration fails, the ownership of this printer is never taken.
489   TextFormat::FieldValuePrinter my_field_printer;
490   // NULL field
491   EXPECT_FALSE(printer.RegisterFieldValuePrinter(nullptr, &my_field_printer));
492 }
493
494 class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter {
495  public:
496   virtual std::string PrintInt32(int32 v) const {
497     return StrCat(FieldValuePrinter::PrintInt32(v), "  # x",
498                         strings::Hex(v));
499   }
500
501   virtual std::string PrintMessageStart(const Message& message, int field_index,
502                                         int field_count,
503                                         bool single_line_mode) const {
504     if (single_line_mode) {
505       return " { ";
506     }
507     return StrCat(" {  # ", message.GetDescriptor()->name(), ": ",
508                         field_index, "\n");
509   }
510 };
511
512 TEST_F(TextFormatTest, CustomPrinterForComments) {
513   protobuf_unittest::TestAllTypes message;
514   message.mutable_optional_nested_message();
515   message.mutable_optional_import_message()->set_d(42);
516   message.add_repeated_nested_message();
517   message.add_repeated_nested_message();
518   message.add_repeated_import_message()->set_d(43);
519   message.add_repeated_import_message()->set_d(44);
520   TextFormat::Printer printer;
521   CustomMessageFieldValuePrinter my_field_printer;
522   printer.SetDefaultFieldValuePrinter(new CustomMessageFieldValuePrinter());
523   std::string text;
524   printer.PrintToString(message, &text);
525   EXPECT_EQ(
526       "optional_nested_message {  # NestedMessage: -1\n"
527       "}\n"
528       "optional_import_message {  # ImportMessage: -1\n"
529       "  d: 42  # x2a\n"
530       "}\n"
531       "repeated_nested_message {  # NestedMessage: 0\n"
532       "}\n"
533       "repeated_nested_message {  # NestedMessage: 1\n"
534       "}\n"
535       "repeated_import_message {  # ImportMessage: 0\n"
536       "  d: 43  # x2b\n"
537       "}\n"
538       "repeated_import_message {  # ImportMessage: 1\n"
539       "  d: 44  # x2c\n"
540       "}\n",
541       text);
542 }
543
544 class CustomMultilineCommentPrinter : public TextFormat::FieldValuePrinter {
545  public:
546   virtual std::string PrintMessageStart(const Message& message, int field_index,
547                                         int field_count,
548                                         bool single_line_comment) const {
549     return StrCat(" {  # 1\n", "  # 2\n");
550   }
551 };
552
553 TEST_F(TextFormatTest, CustomPrinterForMultilineComments) {
554   protobuf_unittest::TestAllTypes message;
555   message.mutable_optional_nested_message();
556   message.mutable_optional_import_message()->set_d(42);
557   TextFormat::Printer printer;
558   CustomMessageFieldValuePrinter my_field_printer;
559   printer.SetDefaultFieldValuePrinter(new CustomMultilineCommentPrinter());
560   std::string text;
561   printer.PrintToString(message, &text);
562   EXPECT_EQ(
563       "optional_nested_message {  # 1\n"
564       "  # 2\n"
565       "}\n"
566       "optional_import_message {  # 1\n"
567       "  # 2\n"
568       "  d: 42\n"
569       "}\n",
570       text);
571 }
572
573 // Achieve effects similar to SetUseShortRepeatedPrimitives for messages, using
574 // RegisterFieldValuePrinter. Use this to test the version of PrintFieldName
575 // that accepts repeated field index and count.
576 class CompactRepeatedFieldPrinter : public TextFormat::FastFieldValuePrinter {
577  public:
578   void PrintFieldName(const Message& message, int field_index, int field_count,
579                       const Reflection* reflection,
580                       const FieldDescriptor* field,
581                       TextFormat::BaseTextGenerator* generator) const override {
582     if (field_index == 0 || field_index == -1) {
583       generator->PrintString(field->name());
584     }
585   }
586   // To prevent compiler complaining about Woverloaded-virtual
587   void PrintFieldName(const Message& message, const Reflection* reflection,
588                       const FieldDescriptor* field,
589                       TextFormat::BaseTextGenerator* generator) const override {
590   }
591   void PrintMessageStart(
592       const Message& message, int field_index, int field_count,
593       bool single_line_mode,
594       TextFormat::BaseTextGenerator* generator) const override {
595     if (field_index == 0 || field_index == -1) {
596       if (single_line_mode) {
597         generator->PrintLiteral(" { ");
598       } else {
599         generator->PrintLiteral(" {\n");
600       }
601     }
602   }
603   void PrintMessageEnd(
604       const Message& message, int field_index, int field_count,
605       bool single_line_mode,
606       TextFormat::BaseTextGenerator* generator) const override {
607     if (field_index == field_count - 1 || field_index == -1) {
608       if (single_line_mode) {
609         generator->PrintLiteral("} ");
610       } else {
611         generator->PrintLiteral("}\n");
612       }
613     }
614   }
615 };
616
617 TEST_F(TextFormatTest, CompactRepeatedFieldPrinter) {
618   TextFormat::Printer printer;
619   ASSERT_TRUE(printer.RegisterFieldValuePrinter(
620       unittest::TestAllTypes::default_instance()
621           .descriptor()
622           ->FindFieldByNumber(
623               unittest::TestAllTypes::kRepeatedNestedMessageFieldNumber),
624       new CompactRepeatedFieldPrinter));
625
626   protobuf_unittest::TestAllTypes message;
627   message.add_repeated_nested_message()->set_bb(1);
628   message.add_repeated_nested_message()->set_bb(2);
629   message.add_repeated_nested_message()->set_bb(3);
630
631   std::string text;
632   ASSERT_TRUE(printer.PrintToString(message, &text));
633   EXPECT_EQ(
634       "repeated_nested_message {\n"
635       "  bb: 1\n"
636       "  bb: 2\n"
637       "  bb: 3\n"
638       "}\n",
639       text);
640 }
641
642 // Print strings into multiple line, with indention. Use this to test
643 // BaseTextGenerator::Indent and BaseTextGenerator::Outdent.
644 class MultilineStringPrinter : public TextFormat::FastFieldValuePrinter {
645  public:
646   void PrintString(const std::string& val,
647                    TextFormat::BaseTextGenerator* generator) const override {
648     generator->Indent();
649     int last_pos = 0;
650     int newline_pos = val.find('\n');
651     while (newline_pos != std::string::npos) {
652       generator->PrintLiteral("\n");
653       TextFormat::FastFieldValuePrinter::PrintString(
654           val.substr(last_pos, newline_pos + 1 - last_pos), generator);
655       last_pos = newline_pos + 1;
656       newline_pos = val.find('\n', last_pos);
657     }
658     if (last_pos < val.size()) {
659       generator->PrintLiteral("\n");
660       TextFormat::FastFieldValuePrinter::PrintString(val.substr(last_pos),
661                                                      generator);
662     }
663     generator->Outdent();
664   }
665 };
666
667 TEST_F(TextFormatTest, MultilineStringPrinter) {
668   TextFormat::Printer printer;
669   ASSERT_TRUE(printer.RegisterFieldValuePrinter(
670       unittest::TestAllTypes::default_instance()
671           .descriptor()
672           ->FindFieldByNumber(
673               unittest::TestAllTypes::kOptionalStringFieldNumber),
674       new MultilineStringPrinter));
675
676   protobuf_unittest::TestAllTypes message;
677   message.set_optional_string("first line\nsecond line\nthird line");
678
679   std::string text;
680   ASSERT_TRUE(printer.PrintToString(message, &text));
681   EXPECT_EQ(
682       "optional_string: \n"
683       "  \"first line\\n\"\n"
684       "  \"second line\\n\"\n"
685       "  \"third line\"\n",
686       text);
687 }
688
689 class CustomNestedMessagePrinter : public TextFormat::MessagePrinter {
690  public:
691   CustomNestedMessagePrinter() {}
692   ~CustomNestedMessagePrinter() override {}
693   void Print(const Message& message, bool single_line_mode,
694              TextFormat::BaseTextGenerator* generator) const override {
695     generator->PrintLiteral("custom");
696   }
697 };
698
699 TEST_F(TextFormatTest, CustomMessagePrinter) {
700   TextFormat::Printer printer;
701   printer.RegisterMessagePrinter(
702       unittest::TestAllTypes::NestedMessage::default_instance().descriptor(),
703       new CustomNestedMessagePrinter);
704
705   unittest::TestAllTypes message;
706   std::string text;
707   EXPECT_TRUE(printer.PrintToString(message, &text));
708   EXPECT_EQ("", text);
709
710   message.mutable_optional_nested_message()->set_bb(1);
711   EXPECT_TRUE(printer.PrintToString(message, &text));
712   EXPECT_EQ("optional_nested_message {\n  custom}\n", text);
713 }
714
715 TEST_F(TextFormatTest, ParseBasic) {
716   io::ArrayInputStream input_stream(proto_debug_string_.data(),
717                                     proto_debug_string_.size());
718   TextFormat::Parse(&input_stream, &proto_);
719   TestUtil::ExpectAllFieldsSet(proto_);
720 }
721
722 TEST_F(TextFormatExtensionsTest, ParseExtensions) {
723   io::ArrayInputStream input_stream(proto_debug_string_.data(),
724                                     proto_debug_string_.size());
725   TextFormat::Parse(&input_stream, &proto_);
726   TestUtil::ExpectAllExtensionsSet(proto_);
727 }
728
729 TEST_F(TextFormatTest, ParseEnumFieldFromNumber) {
730   // Create a parse string with a numerical value for an enum field.
731   std::string parse_string = strings::Substitute("optional_nested_enum: $0",
732                                                  unittest::TestAllTypes::BAZ);
733   EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
734   EXPECT_TRUE(proto_.has_optional_nested_enum());
735   EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum());
736 }
737
738 TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) {
739   ASSERT_LT(unittest::SPARSE_E, 0);
740   std::string parse_string =
741       strings::Substitute("sparse_enum: $0", unittest::SPARSE_E);
742   unittest::SparseEnumMessage proto;
743   EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
744   EXPECT_TRUE(proto.has_sparse_enum());
745   EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum());
746 }
747
748 TEST_F(TextFormatTest, PrintUnknownEnumFieldProto3) {
749   proto3_unittest::TestAllTypes proto;
750
751   proto.add_repeated_nested_enum(
752       static_cast<proto3_unittest::TestAllTypes::NestedEnum>(10));
753   proto.add_repeated_nested_enum(
754       static_cast<proto3_unittest::TestAllTypes::NestedEnum>(-10));
755   proto.add_repeated_nested_enum(
756       static_cast<proto3_unittest::TestAllTypes::NestedEnum>(2147483647));
757   proto.add_repeated_nested_enum(
758       static_cast<proto3_unittest::TestAllTypes::NestedEnum>(-2147483648));
759
760   EXPECT_EQ(
761       "repeated_nested_enum: 10\n"
762       "repeated_nested_enum: -10\n"
763       "repeated_nested_enum: 2147483647\n"
764       "repeated_nested_enum: -2147483648\n",
765       proto.DebugString());
766 }
767
768 TEST_F(TextFormatTest, ParseUnknownEnumFieldProto3) {
769   proto3_unittest::TestAllTypes proto;
770   std::string parse_string =
771       "repeated_nested_enum: [10, -10, 2147483647, -2147483648]";
772   EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
773   ASSERT_EQ(4, proto.repeated_nested_enum_size());
774   EXPECT_EQ(10, proto.repeated_nested_enum(0));
775   EXPECT_EQ(-10, proto.repeated_nested_enum(1));
776   EXPECT_EQ(2147483647, proto.repeated_nested_enum(2));
777   EXPECT_EQ(-2147483648, proto.repeated_nested_enum(3));
778 }
779
780 TEST_F(TextFormatTest, ParseStringEscape) {
781   // Create a parse string with escpaed characters in it.
782   std::string parse_string =
783       "optional_string: " + kEscapeTestStringEscaped + "\n";
784
785   io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
786   TextFormat::Parse(&input_stream, &proto_);
787
788   // Compare.
789   EXPECT_EQ(kEscapeTestString, proto_.optional_string());
790 }
791
792 TEST_F(TextFormatTest, ParseConcatenatedString) {
793   // Create a parse string with multiple parts on one line.
794   std::string parse_string = "optional_string: \"foo\" \"bar\"\n";
795
796   io::ArrayInputStream input_stream1(parse_string.data(), parse_string.size());
797   TextFormat::Parse(&input_stream1, &proto_);
798
799   // Compare.
800   EXPECT_EQ("foobar", proto_.optional_string());
801
802   // Create a parse string with multiple parts on separate lines.
803   parse_string =
804       "optional_string: \"foo\"\n"
805       "\"bar\"\n";
806
807   io::ArrayInputStream input_stream2(parse_string.data(), parse_string.size());
808   TextFormat::Parse(&input_stream2, &proto_);
809
810   // Compare.
811   EXPECT_EQ("foobar", proto_.optional_string());
812 }
813
814 TEST_F(TextFormatTest, ParseFloatWithSuffix) {
815   // Test that we can parse a floating-point value with 'f' appended to the
816   // end.  This is needed for backwards-compatibility with proto1.
817
818   // Have it parse a float with the 'f' suffix.
819   std::string parse_string = "optional_float: 1.0f\n";
820
821   io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
822
823   TextFormat::Parse(&input_stream, &proto_);
824
825   // Compare.
826   EXPECT_EQ(1.0, proto_.optional_float());
827 }
828
829 TEST_F(TextFormatTest, ParseShortRepeatedForm) {
830   std::string parse_string =
831       // Mixed short-form and long-form are simply concatenated.
832       "repeated_int32: 1\n"
833       "repeated_int32: [456, 789]\n"
834       "repeated_nested_enum: [  FOO ,BAR, # comment\n"
835       "                         3]\n"
836       // Note that while the printer won't print repeated strings in short-form,
837       // the parser will accept them.
838       "repeated_string: [ \"foo\", 'bar' ]\n"
839       // Repeated message
840       "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
841       // Repeated group
842       "RepeatedGroup [{ a: 3 },{ a: 4 }]\n";
843
844   ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
845
846   ASSERT_EQ(3, proto_.repeated_int32_size());
847   EXPECT_EQ(1, proto_.repeated_int32(0));
848   EXPECT_EQ(456, proto_.repeated_int32(1));
849   EXPECT_EQ(789, proto_.repeated_int32(2));
850
851   ASSERT_EQ(3, proto_.repeated_nested_enum_size());
852   EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
853   EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
854   EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
855
856   ASSERT_EQ(2, proto_.repeated_string_size());
857   EXPECT_EQ("foo", proto_.repeated_string(0));
858   EXPECT_EQ("bar", proto_.repeated_string(1));
859
860   ASSERT_EQ(2, proto_.repeated_nested_message_size());
861   EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
862   EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
863
864   ASSERT_EQ(2, proto_.repeatedgroup_size());
865   EXPECT_EQ(3, proto_.repeatedgroup(0).a());
866   EXPECT_EQ(4, proto_.repeatedgroup(1).a());
867 }
868
869 TEST_F(TextFormatTest, ParseShortRepeatedWithTrailingComma) {
870   std::string parse_string = "repeated_int32: [456,]\n";
871   ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
872   parse_string = "repeated_nested_enum: [  FOO , ]";
873   ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
874   parse_string = "repeated_string: [ \"foo\", ]";
875   ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
876   parse_string = "repeated_nested_message: [ { bb: 1 }, ]";
877   ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
878   parse_string = "RepeatedGroup [{ a: 3 },]\n";
879 }
880
881 TEST_F(TextFormatTest, ParseShortRepeatedEmpty) {
882   std::string parse_string =
883       "repeated_int32: []\n"
884       "repeated_nested_enum: []\n"
885       "repeated_string: []\n"
886       "repeated_nested_message: []\n"
887       "RepeatedGroup []\n";
888
889   ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
890
891   EXPECT_EQ(0, proto_.repeated_int32_size());
892   EXPECT_EQ(0, proto_.repeated_nested_enum_size());
893   EXPECT_EQ(0, proto_.repeated_string_size());
894   EXPECT_EQ(0, proto_.repeated_nested_message_size());
895   EXPECT_EQ(0, proto_.repeatedgroup_size());
896 }
897
898 TEST_F(TextFormatTest, ParseShortRepeatedConcatenatedWithEmpty) {
899   std::string parse_string =
900       // Starting with empty [] should have no impact.
901       "repeated_int32: []\n"
902       "repeated_nested_enum: []\n"
903       "repeated_string: []\n"
904       "repeated_nested_message: []\n"
905       "RepeatedGroup []\n"
906       // Mixed short-form and long-form are simply concatenated.
907       "repeated_int32: 1\n"
908       "repeated_int32: [456, 789]\n"
909       "repeated_nested_enum: [  FOO ,BAR, # comment\n"
910       "                         3]\n"
911       // Note that while the printer won't print repeated strings in short-form,
912       // the parser will accept them.
913       "repeated_string: [ \"foo\", 'bar' ]\n"
914       // Repeated message
915       "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
916       // Repeated group
917       "RepeatedGroup [{ a: 3 },{ a: 4 }]\n"
918       // Adding empty [] should have no impact.
919       "repeated_int32: []\n"
920       "repeated_nested_enum: []\n"
921       "repeated_string: []\n"
922       "repeated_nested_message: []\n"
923       "RepeatedGroup []\n";
924
925   ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
926
927   ASSERT_EQ(3, proto_.repeated_int32_size());
928   EXPECT_EQ(1, proto_.repeated_int32(0));
929   EXPECT_EQ(456, proto_.repeated_int32(1));
930   EXPECT_EQ(789, proto_.repeated_int32(2));
931
932   ASSERT_EQ(3, proto_.repeated_nested_enum_size());
933   EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
934   EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
935   EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
936
937   ASSERT_EQ(2, proto_.repeated_string_size());
938   EXPECT_EQ("foo", proto_.repeated_string(0));
939   EXPECT_EQ("bar", proto_.repeated_string(1));
940
941   ASSERT_EQ(2, proto_.repeated_nested_message_size());
942   EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
943   EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
944
945   ASSERT_EQ(2, proto_.repeatedgroup_size());
946   EXPECT_EQ(3, proto_.repeatedgroup(0).a());
947   EXPECT_EQ(4, proto_.repeatedgroup(1).a());
948 }
949
950
951 TEST_F(TextFormatTest, Comments) {
952   // Test that comments are ignored.
953
954   std::string parse_string =
955       "optional_int32: 1  # a comment\n"
956       "optional_int64: 2  # another comment";
957
958   io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
959
960   TextFormat::Parse(&input_stream, &proto_);
961
962   // Compare.
963   EXPECT_EQ(1, proto_.optional_int32());
964   EXPECT_EQ(2, proto_.optional_int64());
965 }
966
967 TEST_F(TextFormatTest, OptionalColon) {
968   // Test that we can place a ':' after the field name of a nested message,
969   // even though we don't have to.
970
971   std::string parse_string = "optional_nested_message: { bb: 1}\n";
972
973   io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
974
975   TextFormat::Parse(&input_stream, &proto_);
976
977   // Compare.
978   EXPECT_TRUE(proto_.has_optional_nested_message());
979   EXPECT_EQ(1, proto_.optional_nested_message().bb());
980 }
981
982 // Some platforms (e.g. Windows) insist on padding the exponent to three
983 // digits when one or two would be just fine.
984 static std::string RemoveRedundantZeros(std::string text) {
985   text = StringReplace(text, "e+0", "e+", true);
986   text = StringReplace(text, "e-0", "e-", true);
987   return text;
988 }
989
990 TEST_F(TextFormatTest, PrintExotic) {
991   unittest::TestAllTypes message;
992
993   // Note:  In C, a negative integer literal is actually the unary negation
994   //   operator being applied to a positive integer literal, and
995   //   9223372036854775808 is outside the range of int64.  However, it is not
996   //   outside the range of uint64.  Confusingly, this means that everything
997   //   works if we make the literal unsigned, even though we are negating it.
998   message.add_repeated_int64(-PROTOBUF_ULONGLONG(9223372036854775808));
999   message.add_repeated_uint64(PROTOBUF_ULONGLONG(18446744073709551615));
1000   message.add_repeated_double(123.456);
1001   message.add_repeated_double(1.23e21);
1002   message.add_repeated_double(1.23e-18);
1003   message.add_repeated_double(std::numeric_limits<double>::infinity());
1004   message.add_repeated_double(-std::numeric_limits<double>::infinity());
1005   message.add_repeated_double(std::numeric_limits<double>::quiet_NaN());
1006   message.add_repeated_string(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12));
1007
1008   // Fun story:  We used to use 1.23e22 instead of 1.23e21 above, but this
1009   //   seemed to trigger an odd case on MinGW/GCC 3.4.5 where GCC's parsing of
1010   //   the value differed from strtod()'s parsing.  That is to say, the
1011   //   following assertion fails on MinGW:
1012   //     assert(1.23e22 == strtod("1.23e22", NULL));
1013   //   As a result, SimpleDtoa() would print the value as
1014   //   "1.2300000000000001e+22" to make sure strtod() produce the exact same
1015   //   result.  Our goal is to test runtime parsing, not compile-time parsing,
1016   //   so this wasn't our problem.  It was found that using 1.23e21 did not
1017   //   have this problem, so we switched to that instead.
1018
1019   EXPECT_EQ(
1020       "repeated_int64: -9223372036854775808\n"
1021       "repeated_uint64: 18446744073709551615\n"
1022       "repeated_double: 123.456\n"
1023       "repeated_double: 1.23e+21\n"
1024       "repeated_double: 1.23e-18\n"
1025       "repeated_double: inf\n"
1026       "repeated_double: -inf\n"
1027       "repeated_double: nan\n"
1028       "repeated_string: "
1029       "\"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n",
1030       RemoveRedundantZeros(message.DebugString()));
1031 }
1032
1033 TEST_F(TextFormatTest, PrintFloatPrecision) {
1034   unittest::TestAllTypes message;
1035
1036   message.add_repeated_float(1.0);
1037   message.add_repeated_float(1.2);
1038   message.add_repeated_float(1.23);
1039   message.add_repeated_float(1.234);
1040   message.add_repeated_float(1.2345);
1041   message.add_repeated_float(1.23456);
1042   message.add_repeated_float(1.2e10);
1043   message.add_repeated_float(1.23e10);
1044   message.add_repeated_float(1.234e10);
1045   message.add_repeated_float(1.2345e10);
1046   message.add_repeated_float(1.23456e10);
1047   message.add_repeated_double(1.2);
1048   message.add_repeated_double(1.23);
1049   message.add_repeated_double(1.234);
1050   message.add_repeated_double(1.2345);
1051   message.add_repeated_double(1.23456);
1052   message.add_repeated_double(1.234567);
1053   message.add_repeated_double(1.2345678);
1054   message.add_repeated_double(1.23456789);
1055   message.add_repeated_double(1.234567898);
1056   message.add_repeated_double(1.2345678987);
1057   message.add_repeated_double(1.23456789876);
1058   message.add_repeated_double(1.234567898765);
1059   message.add_repeated_double(1.2345678987654);
1060   message.add_repeated_double(1.23456789876543);
1061   message.add_repeated_double(1.2e100);
1062   message.add_repeated_double(1.23e100);
1063   message.add_repeated_double(1.234e100);
1064   message.add_repeated_double(1.2345e100);
1065   message.add_repeated_double(1.23456e100);
1066   message.add_repeated_double(1.234567e100);
1067   message.add_repeated_double(1.2345678e100);
1068   message.add_repeated_double(1.23456789e100);
1069   message.add_repeated_double(1.234567898e100);
1070   message.add_repeated_double(1.2345678987e100);
1071   message.add_repeated_double(1.23456789876e100);
1072   message.add_repeated_double(1.234567898765e100);
1073   message.add_repeated_double(1.2345678987654e100);
1074   message.add_repeated_double(1.23456789876543e100);
1075
1076   EXPECT_EQ(
1077       "repeated_float: 1\n"
1078       "repeated_float: 1.2\n"
1079       "repeated_float: 1.23\n"
1080       "repeated_float: 1.234\n"
1081       "repeated_float: 1.2345\n"
1082       "repeated_float: 1.23456\n"
1083       "repeated_float: 1.2e+10\n"
1084       "repeated_float: 1.23e+10\n"
1085       "repeated_float: 1.234e+10\n"
1086       "repeated_float: 1.2345e+10\n"
1087       "repeated_float: 1.23456e+10\n"
1088       "repeated_double: 1.2\n"
1089       "repeated_double: 1.23\n"
1090       "repeated_double: 1.234\n"
1091       "repeated_double: 1.2345\n"
1092       "repeated_double: 1.23456\n"
1093       "repeated_double: 1.234567\n"
1094       "repeated_double: 1.2345678\n"
1095       "repeated_double: 1.23456789\n"
1096       "repeated_double: 1.234567898\n"
1097       "repeated_double: 1.2345678987\n"
1098       "repeated_double: 1.23456789876\n"
1099       "repeated_double: 1.234567898765\n"
1100       "repeated_double: 1.2345678987654\n"
1101       "repeated_double: 1.23456789876543\n"
1102       "repeated_double: 1.2e+100\n"
1103       "repeated_double: 1.23e+100\n"
1104       "repeated_double: 1.234e+100\n"
1105       "repeated_double: 1.2345e+100\n"
1106       "repeated_double: 1.23456e+100\n"
1107       "repeated_double: 1.234567e+100\n"
1108       "repeated_double: 1.2345678e+100\n"
1109       "repeated_double: 1.23456789e+100\n"
1110       "repeated_double: 1.234567898e+100\n"
1111       "repeated_double: 1.2345678987e+100\n"
1112       "repeated_double: 1.23456789876e+100\n"
1113       "repeated_double: 1.234567898765e+100\n"
1114       "repeated_double: 1.2345678987654e+100\n"
1115       "repeated_double: 1.23456789876543e+100\n",
1116       RemoveRedundantZeros(message.DebugString()));
1117 }
1118
1119 TEST_F(TextFormatTest, AllowPartial) {
1120   unittest::TestRequired message;
1121   TextFormat::Parser parser;
1122   parser.AllowPartialMessage(true);
1123   EXPECT_TRUE(parser.ParseFromString("a: 1", &message));
1124   EXPECT_EQ(1, message.a());
1125   EXPECT_FALSE(message.has_b());
1126   EXPECT_FALSE(message.has_c());
1127 }
1128
1129 TEST_F(TextFormatTest, ParseExotic) {
1130   unittest::TestAllTypes message;
1131   ASSERT_TRUE(TextFormat::ParseFromString(
1132       "repeated_int32: -1\n"
1133       "repeated_int32: -2147483648\n"
1134       "repeated_int64: -1\n"
1135       "repeated_int64: -9223372036854775808\n"
1136       "repeated_uint32: 4294967295\n"
1137       "repeated_uint32: 2147483648\n"
1138       "repeated_uint64: 18446744073709551615\n"
1139       "repeated_uint64: 9223372036854775808\n"
1140       "repeated_double: 123.0\n"
1141       "repeated_double: 123.5\n"
1142       "repeated_double: 0.125\n"
1143       "repeated_double: 1.23E17\n"
1144       "repeated_double: 1.235E+22\n"
1145       "repeated_double: 1.235e-18\n"
1146       "repeated_double: 123.456789\n"
1147       "repeated_double: inf\n"
1148       "repeated_double: Infinity\n"
1149       "repeated_double: -inf\n"
1150       "repeated_double: -Infinity\n"
1151       "repeated_double: nan\n"
1152       "repeated_double: NaN\n"
1153       "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n",
1154       &message));
1155
1156   ASSERT_EQ(2, message.repeated_int32_size());
1157   EXPECT_EQ(-1, message.repeated_int32(0));
1158   // Note:  In C, a negative integer literal is actually the unary negation
1159   //   operator being applied to a positive integer literal, and 2147483648 is
1160   //   outside the range of int32.  However, it is not outside the range of
1161   //   uint32.  Confusingly, this means that everything works if we make the
1162   //   literal unsigned, even though we are negating it.
1163   EXPECT_EQ(-2147483648u, message.repeated_int32(1));
1164
1165   ASSERT_EQ(2, message.repeated_int64_size());
1166   EXPECT_EQ(-1, message.repeated_int64(0));
1167   // Note:  In C, a negative integer literal is actually the unary negation
1168   //   operator being applied to a positive integer literal, and
1169   //   9223372036854775808 is outside the range of int64.  However, it is not
1170   //   outside the range of uint64.  Confusingly, this means that everything
1171   //   works if we make the literal unsigned, even though we are negating it.
1172   EXPECT_EQ(-PROTOBUF_ULONGLONG(9223372036854775808),
1173             message.repeated_int64(1));
1174
1175   ASSERT_EQ(2, message.repeated_uint32_size());
1176   EXPECT_EQ(4294967295u, message.repeated_uint32(0));
1177   EXPECT_EQ(2147483648u, message.repeated_uint32(1));
1178
1179   ASSERT_EQ(2, message.repeated_uint64_size());
1180   EXPECT_EQ(PROTOBUF_ULONGLONG(18446744073709551615),
1181             message.repeated_uint64(0));
1182   EXPECT_EQ(PROTOBUF_ULONGLONG(9223372036854775808),
1183             message.repeated_uint64(1));
1184
1185   ASSERT_EQ(13, message.repeated_double_size());
1186   EXPECT_EQ(123.0, message.repeated_double(0));
1187   EXPECT_EQ(123.5, message.repeated_double(1));
1188   EXPECT_EQ(0.125, message.repeated_double(2));
1189   EXPECT_EQ(1.23E17, message.repeated_double(3));
1190   EXPECT_EQ(1.235E22, message.repeated_double(4));
1191   EXPECT_EQ(1.235E-18, message.repeated_double(5));
1192   EXPECT_EQ(123.456789, message.repeated_double(6));
1193   EXPECT_EQ(message.repeated_double(7),
1194             std::numeric_limits<double>::infinity());
1195   EXPECT_EQ(message.repeated_double(8),
1196             std::numeric_limits<double>::infinity());
1197   EXPECT_EQ(message.repeated_double(9),
1198             -std::numeric_limits<double>::infinity());
1199   EXPECT_EQ(message.repeated_double(10),
1200             -std::numeric_limits<double>::infinity());
1201   EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(11)));
1202   EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(12)));
1203
1204   // Note:  Since these string literals have \0's in them, we must explicitly
1205   // pass their sizes to string's constructor.
1206   ASSERT_EQ(1, message.repeated_string_size());
1207   EXPECT_EQ(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12),
1208             message.repeated_string(0));
1209
1210   ASSERT_TRUE(
1211       TextFormat::ParseFromString("repeated_float: 3.4028235e+38\n"
1212                                   "repeated_float: -3.4028235e+38\n"
1213                                   "repeated_float: 3.402823567797337e+38\n"
1214                                   "repeated_float: -3.402823567797337e+38\n",
1215                                   &message));
1216   EXPECT_EQ(message.repeated_float(0), std::numeric_limits<float>::max());
1217   EXPECT_EQ(message.repeated_float(1), -std::numeric_limits<float>::max());
1218   EXPECT_EQ(message.repeated_float(2), std::numeric_limits<float>::infinity());
1219   EXPECT_EQ(message.repeated_float(3), -std::numeric_limits<float>::infinity());
1220
1221 }
1222
1223 TEST_F(TextFormatTest, PrintFieldsInIndexOrder) {
1224   protobuf_unittest::TestFieldOrderings message;
1225   // Fields are listed in index order instead of field number.
1226   message.set_my_string("str");  // Field number 11
1227   message.set_my_int(12345);     // Field number 1
1228   message.set_my_float(0.999);   // Field number 101
1229   // Extensions are listed based on the order of extension number.
1230   // Extension number 12.
1231   message
1232       .MutableExtension(
1233           protobuf_unittest::TestExtensionOrderings2::test_ext_orderings2)
1234       ->set_my_string("ext_str2");
1235   // Extension number 13.
1236   message
1237       .MutableExtension(
1238           protobuf_unittest::TestExtensionOrderings1::test_ext_orderings1)
1239       ->set_my_string("ext_str1");
1240   // Extension number 14.
1241   message
1242       .MutableExtension(protobuf_unittest::TestExtensionOrderings2::
1243                             TestExtensionOrderings3::test_ext_orderings3)
1244       ->set_my_string("ext_str3");
1245   // Extension number 50.
1246   *message.MutableExtension(protobuf_unittest::my_extension_string) = "ext_str0";
1247
1248   TextFormat::Printer printer;
1249   std::string text;
1250
1251   // By default, print in field number order.
1252   // my_int: 12345
1253   // my_string: "str"
1254   // [protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
1255   //   my_string: "ext_str2"
1256   // }
1257   // [protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
1258   //   my_string: "ext_str1"
1259   // }
1260   // [protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3]
1261   // {
1262   //   my_string: "ext_str3"
1263   // }
1264   // [protobuf_unittest.my_extension_string]: "ext_str0"
1265   // my_float: 0.999
1266   printer.PrintToString(message, &text);
1267   EXPECT_EQ(
1268       "my_int: 12345\nmy_string: "
1269       "\"str\"\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1270       "{\n  my_string: "
1271       "\"ext_str2\"\n}\n[protobuf_unittest.TestExtensionOrderings1.test_ext_"
1272       "orderings1] {\n  my_string: "
1273       "\"ext_str1\"\n}\n[protobuf_unittest.TestExtensionOrderings2."
1274       "TestExtensionOrderings3.test_ext_orderings3] {\n  my_string: "
1275       "\"ext_str3\"\n}\n[protobuf_unittest.my_extension_string]: "
1276       "\"ext_str0\"\nmy_float: 0.999\n",
1277       text);
1278
1279   // Print in index order.
1280   // my_string: "str"
1281   // my_int: 12345
1282   // my_float: 0.999
1283   // [protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
1284   //   my_string: "ext_str2"
1285   // }
1286   // [protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
1287   //   my_string: "ext_str1"
1288   // }
1289   // [protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3]
1290   // {
1291   //   my_string: "ext_str3"
1292   // }
1293   // [protobuf_unittest.my_extension_string]: "ext_str0"
1294   printer.SetPrintMessageFieldsInIndexOrder(true);
1295   printer.PrintToString(message, &text);
1296   EXPECT_EQ(
1297       "my_string: \"str\"\nmy_int: 12345\nmy_float: "
1298       "0.999\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1299       "{\n  my_string: "
1300       "\"ext_str2\"\n}\n[protobuf_unittest.TestExtensionOrderings1.test_ext_"
1301       "orderings1] {\n  my_string: "
1302       "\"ext_str1\"\n}\n[protobuf_unittest.TestExtensionOrderings2."
1303       "TestExtensionOrderings3.test_ext_orderings3] {\n  my_string: "
1304       "\"ext_str3\"\n}\n[protobuf_unittest.my_extension_string]: \"ext_str0\"\n",
1305       text);
1306 }
1307
1308 class TextFormatParserTest : public testing::Test {
1309  protected:
1310   void ExpectFailure(const std::string& input, const std::string& message,
1311                      int line, int col) {
1312     std::unique_ptr<unittest::TestAllTypes> proto(new unittest::TestAllTypes);
1313     ExpectFailure(input, message, line, col, proto.get());
1314   }
1315
1316   void ExpectFailure(const std::string& input, const std::string& message,
1317                      int line, int col, Message* proto) {
1318     ExpectMessage(input, message, line, col, proto, false);
1319   }
1320
1321   void ExpectMessage(const std::string& input, const std::string& message,
1322                      int line, int col, Message* proto, bool expected_result) {
1323     MockErrorCollector error_collector;
1324     parser_.RecordErrorsTo(&error_collector);
1325     EXPECT_EQ(expected_result, parser_.ParseFromString(input, proto))
1326         << input << " -> " << proto->DebugString();
1327     EXPECT_EQ(
1328         StrCat(line) + ":" + StrCat(col) + ": " + message + "\n",
1329         error_collector.text_);
1330     parser_.RecordErrorsTo(nullptr);
1331   }
1332
1333   void ExpectSuccessAndTree(const std::string& input, Message* proto,
1334                             TextFormat::ParseInfoTree* info_tree) {
1335     MockErrorCollector error_collector;
1336     parser_.RecordErrorsTo(&error_collector);
1337     parser_.WriteLocationsTo(info_tree);
1338     EXPECT_TRUE(parser_.ParseFromString(input, proto));
1339     parser_.WriteLocationsTo(nullptr);
1340     parser_.RecordErrorsTo(nullptr);
1341   }
1342
1343   void ExpectLocation(TextFormat::ParseInfoTree* tree, const Descriptor* d,
1344                       const std::string& field_name, int index, int line,
1345                       int column) {
1346     TextFormat::ParseLocation location =
1347         tree->GetLocation(d->FindFieldByName(field_name), index);
1348     EXPECT_EQ(line, location.line);
1349     EXPECT_EQ(column, location.column);
1350   }
1351
1352   // An error collector which simply concatenates all its errors into a big
1353   // block of text which can be checked.
1354   class MockErrorCollector : public io::ErrorCollector {
1355    public:
1356     MockErrorCollector() {}
1357     ~MockErrorCollector() {}
1358
1359     std::string text_;
1360
1361     // implements ErrorCollector -------------------------------------
1362     void AddError(int line, int column, const std::string& message) {
1363       strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line + 1, column + 1,
1364                                    message);
1365     }
1366
1367     void AddWarning(int line, int column, const std::string& message) {
1368       AddError(line, column, "WARNING:" + message);
1369     }
1370   };
1371
1372   TextFormat::Parser parser_;
1373 };
1374
1375 TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) {
1376   std::unique_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes);
1377   const Descriptor* d = message->GetDescriptor();
1378
1379   std::string stringData =
1380       "optional_int32: 1\n"
1381       "optional_int64: 2\n"
1382       "  optional_double: 2.4\n"
1383       "repeated_int32: 5\n"
1384       "repeated_int32: 10\n"
1385       "optional_nested_message <\n"
1386       "  bb: 78\n"
1387       ">\n"
1388       "repeated_nested_message <\n"
1389       "  bb: 79\n"
1390       ">\n"
1391       "repeated_nested_message <\n"
1392       "  bb: 80\n"
1393       ">";
1394
1395   TextFormat::ParseInfoTree tree;
1396   ExpectSuccessAndTree(stringData, message.get(), &tree);
1397
1398   // Verify that the tree has the correct positions.
1399   ExpectLocation(&tree, d, "optional_int32", -1, 0, 0);
1400   ExpectLocation(&tree, d, "optional_int64", -1, 1, 0);
1401   ExpectLocation(&tree, d, "optional_double", -1, 2, 2);
1402
1403   ExpectLocation(&tree, d, "repeated_int32", 0, 3, 0);
1404   ExpectLocation(&tree, d, "repeated_int32", 1, 4, 0);
1405
1406   ExpectLocation(&tree, d, "optional_nested_message", -1, 5, 0);
1407   ExpectLocation(&tree, d, "repeated_nested_message", 0, 8, 0);
1408   ExpectLocation(&tree, d, "repeated_nested_message", 1, 11, 0);
1409
1410   // Check for fields not set. For an invalid field, the location returned
1411   // should be -1, -1.
1412   ExpectLocation(&tree, d, "repeated_int64", 0, -1, -1);
1413   ExpectLocation(&tree, d, "repeated_int32", 6, -1, -1);
1414   ExpectLocation(&tree, d, "some_unknown_field", -1, -1, -1);
1415
1416   // Verify inside the nested message.
1417   const FieldDescriptor* nested_field =
1418       d->FindFieldByName("optional_nested_message");
1419
1420   TextFormat::ParseInfoTree* nested_tree =
1421       tree.GetTreeForNested(nested_field, -1);
1422   ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 6, 2);
1423
1424   // Verify inside another nested message.
1425   nested_field = d->FindFieldByName("repeated_nested_message");
1426   nested_tree = tree.GetTreeForNested(nested_field, 0);
1427   ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 9, 2);
1428
1429   nested_tree = tree.GetTreeForNested(nested_field, 1);
1430   ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 12, 2);
1431
1432   // Verify a NULL tree for an unknown nested field.
1433   TextFormat::ParseInfoTree* unknown_nested_tree =
1434       tree.GetTreeForNested(nested_field, 2);
1435
1436   EXPECT_EQ(NULL, unknown_nested_tree);
1437 }
1438
1439 TEST_F(TextFormatParserTest, ParseFieldValueFromString) {
1440   std::unique_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes);
1441   const Descriptor* d = message->GetDescriptor();
1442
1443 #define EXPECT_FIELD(name, value, valuestring)                             \
1444   EXPECT_TRUE(TextFormat::ParseFieldValueFromString(                       \
1445       valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1446   EXPECT_EQ(value, message->optional_##name());                            \
1447   EXPECT_TRUE(message->has_optional_##name());
1448
1449 #define EXPECT_BOOL_FIELD(name, value, valuestring)                        \
1450   EXPECT_TRUE(TextFormat::ParseFieldValueFromString(                       \
1451       valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1452   EXPECT_TRUE(message->optional_##name() == value);                        \
1453   EXPECT_TRUE(message->has_optional_##name());
1454
1455 #define EXPECT_FLOAT_FIELD(name, value, valuestring)                       \
1456   EXPECT_TRUE(TextFormat::ParseFieldValueFromString(                       \
1457       valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1458   EXPECT_FLOAT_EQ(value, message->optional_##name());                      \
1459   EXPECT_TRUE(message->has_optional_##name());
1460
1461 #define EXPECT_DOUBLE_FIELD(name, value, valuestring)                      \
1462   EXPECT_TRUE(TextFormat::ParseFieldValueFromString(                       \
1463       valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1464   EXPECT_DOUBLE_EQ(value, message->optional_##name());                     \
1465   EXPECT_TRUE(message->has_optional_##name());
1466
1467 #define EXPECT_INVALID(name, valuestring)             \
1468   EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \
1469       valuestring, d->FindFieldByName("optional_" #name), message.get()));
1470
1471   // int32
1472   EXPECT_FIELD(int32, 1, "1");
1473   EXPECT_FIELD(int32, -1, "-1");
1474   EXPECT_FIELD(int32, 0x1234, "0x1234");
1475   EXPECT_INVALID(int32, "a");
1476   EXPECT_INVALID(int32, "999999999999999999999999999999999999");
1477   EXPECT_INVALID(int32, "1,2");
1478
1479   // int64
1480   EXPECT_FIELD(int64, 1, "1");
1481   EXPECT_FIELD(int64, -1, "-1");
1482   EXPECT_FIELD(int64, 0x1234567812345678LL, "0x1234567812345678");
1483   EXPECT_INVALID(int64, "a");
1484   EXPECT_INVALID(int64, "999999999999999999999999999999999999");
1485   EXPECT_INVALID(int64, "1,2");
1486
1487   // uint64
1488   EXPECT_FIELD(uint64, 1, "1");
1489   EXPECT_FIELD(uint64, 0xf234567812345678ULL, "0xf234567812345678");
1490   EXPECT_INVALID(uint64, "-1");
1491   EXPECT_INVALID(uint64, "a");
1492   EXPECT_INVALID(uint64, "999999999999999999999999999999999999");
1493   EXPECT_INVALID(uint64, "1,2");
1494
1495   // fixed32
1496   EXPECT_FIELD(fixed32, 1, "1");
1497   EXPECT_FIELD(fixed32, 0x12345678, "0x12345678");
1498   EXPECT_INVALID(fixed32, "-1");
1499   EXPECT_INVALID(fixed32, "a");
1500   EXPECT_INVALID(fixed32, "999999999999999999999999999999999999");
1501   EXPECT_INVALID(fixed32, "1,2");
1502
1503   // fixed64
1504   EXPECT_FIELD(fixed64, 1, "1");
1505   EXPECT_FIELD(fixed64, 0x1234567812345678ULL, "0x1234567812345678");
1506   EXPECT_INVALID(fixed64, "-1");
1507   EXPECT_INVALID(fixed64, "a");
1508   EXPECT_INVALID(fixed64, "999999999999999999999999999999999999");
1509   EXPECT_INVALID(fixed64, "1,2");
1510
1511   // bool
1512   EXPECT_BOOL_FIELD(bool, true, "true");
1513   EXPECT_BOOL_FIELD(bool, false, "false");
1514   EXPECT_BOOL_FIELD(bool, true, "1");
1515   EXPECT_BOOL_FIELD(bool, true, "t");
1516   EXPECT_BOOL_FIELD(bool, false, "0");
1517   EXPECT_BOOL_FIELD(bool, false, "f");
1518   EXPECT_FIELD(bool, true, "True");
1519   EXPECT_FIELD(bool, false, "False");
1520   EXPECT_INVALID(bool, "tRue");
1521   EXPECT_INVALID(bool, "faLse");
1522   EXPECT_INVALID(bool, "2");
1523   EXPECT_INVALID(bool, "-0");
1524   EXPECT_INVALID(bool, "on");
1525   EXPECT_INVALID(bool, "a");
1526
1527   // float
1528   EXPECT_FIELD(float, 1, "1");
1529   EXPECT_FLOAT_FIELD(float, 1.5, "1.5");
1530   EXPECT_FLOAT_FIELD(float, 1.5e3, "1.5e3");
1531   EXPECT_FLOAT_FIELD(float, -4.55, "-4.55");
1532   EXPECT_INVALID(float, "a");
1533   EXPECT_INVALID(float, "1,2");
1534
1535   // double
1536   EXPECT_FIELD(double, 1, "1");
1537   EXPECT_FIELD(double, -1, "-1");
1538   EXPECT_DOUBLE_FIELD(double, 2.3, "2.3");
1539   EXPECT_DOUBLE_FIELD(double, 3e5, "3e5");
1540   EXPECT_INVALID(double, "a");
1541   EXPECT_INVALID(double, "1,2");
1542   // Rejects hex and oct numbers for a double field.
1543   EXPECT_INVALID(double, "0xf");
1544   EXPECT_INVALID(double, "012");
1545
1546   // string
1547   EXPECT_FIELD(string, "hello", "\"hello\"");
1548   EXPECT_FIELD(string, "-1.87", "'-1.87'");
1549   EXPECT_INVALID(string, "hello");  // without quote for value
1550
1551   // enum
1552   EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR");
1553   EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ,
1554                StrCat(unittest::TestAllTypes::BAZ));
1555   EXPECT_INVALID(nested_enum, "FOOBAR");
1556
1557   // message
1558   EXPECT_TRUE(TextFormat::ParseFieldValueFromString(
1559       "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get()));
1560   EXPECT_EQ(12, message->optional_nested_message().bb());
1561   EXPECT_TRUE(message->has_optional_nested_message());
1562   EXPECT_INVALID(nested_message, "any");
1563
1564 #undef EXPECT_FIELD
1565 #undef EXPECT_BOOL_FIELD
1566 #undef EXPECT_FLOAT_FIELD
1567 #undef EXPECT_DOUBLE_FIELD
1568 #undef EXPECT_INVALID
1569 }
1570
1571 TEST_F(TextFormatParserTest, InvalidToken) {
1572   ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", 2,
1573                 1);
1574
1575   ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, 20);
1576   ExpectFailure("\"some string\"", "Expected identifier, got: \"some string\"",
1577                 1, 1);
1578 }
1579
1580 TEST_F(TextFormatParserTest, InvalidFieldName) {
1581   ExpectFailure(
1582       "invalid_field: somevalue\n",
1583       "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1584       "\"invalid_field\".",
1585       1, 14);
1586 }
1587
1588 TEST_F(TextFormatParserTest, InvalidCapitalization) {
1589   // We require that group names be exactly as they appear in the .proto.
1590   ExpectFailure(
1591       "optionalgroup {\na: 15\n}\n",
1592       "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1593       "\"optionalgroup\".",
1594       1, 15);
1595   ExpectFailure(
1596       "OPTIONALgroup {\na: 15\n}\n",
1597       "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1598       "\"OPTIONALgroup\".",
1599       1, 15);
1600   ExpectFailure(
1601       "Optional_Double: 10.0\n",
1602       "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1603       "\"Optional_Double\".",
1604       1, 16);
1605 }
1606
1607 TEST_F(TextFormatParserTest, AllowIgnoreCapitalizationError) {
1608   TextFormat::Parser parser;
1609   protobuf_unittest::TestAllTypes proto;
1610
1611   // These fields have a mismatching case.
1612   EXPECT_FALSE(parser.ParseFromString("Optional_Double: 10.0", &proto));
1613   EXPECT_FALSE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto));
1614
1615   // ... but are parsed correctly if we match case insensitive.
1616   parser.AllowCaseInsensitiveField(true);
1617   EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto));
1618   EXPECT_EQ(10.0, proto.optional_double());
1619   EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto));
1620   EXPECT_EQ(15, proto.optionalgroup().a());
1621 }
1622
1623 TEST_F(TextFormatParserTest, InvalidFieldValues) {
1624   // Invalid values for a double/float field.
1625   ExpectFailure("optional_double: \"hello\"\n",
1626                 "Expected double, got: \"hello\"", 1, 18);
1627   ExpectFailure("optional_double: true\n", "Expected double, got: true", 1, 18);
1628   ExpectFailure("optional_double: !\n", "Expected double, got: !", 1, 18);
1629   ExpectFailure("optional_double {\n  \n}\n", "Expected \":\", found \"{\".", 1,
1630                 17);
1631
1632   // Invalid values for a signed integer field.
1633   ExpectFailure("optional_int32: \"hello\"\n",
1634                 "Expected integer, got: \"hello\"", 1, 17);
1635   ExpectFailure("optional_int32: true\n", "Expected integer, got: true", 1, 17);
1636   ExpectFailure("optional_int32: 4.5\n", "Expected integer, got: 4.5", 1, 17);
1637   ExpectFailure("optional_int32: !\n", "Expected integer, got: !", 1, 17);
1638   ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", 1,
1639                 16);
1640   ExpectFailure("optional_int32: 0x80000000\n",
1641                 "Integer out of range (0x80000000)", 1, 17);
1642   ExpectFailure("optional_int64: 0x8000000000000000\n",
1643                 "Integer out of range (0x8000000000000000)", 1, 17);
1644   ExpectFailure("optional_int32: -0x80000001\n",
1645                 "Integer out of range (0x80000001)", 1, 18);
1646   ExpectFailure("optional_int64: -0x8000000000000001\n",
1647                 "Integer out of range (0x8000000000000001)", 1, 18);
1648
1649   // Invalid values for an unsigned integer field.
1650   ExpectFailure("optional_uint64: \"hello\"\n",
1651                 "Expected integer, got: \"hello\"", 1, 18);
1652   ExpectFailure("optional_uint64: true\n", "Expected integer, got: true", 1,
1653                 18);
1654   ExpectFailure("optional_uint64: 4.5\n", "Expected integer, got: 4.5", 1, 18);
1655   ExpectFailure("optional_uint64: -5\n", "Expected integer, got: -", 1, 18);
1656   ExpectFailure("optional_uint64: !\n", "Expected integer, got: !", 1, 18);
1657   ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", 1,
1658                 17);
1659   ExpectFailure("optional_uint32: 0x100000000\n",
1660                 "Integer out of range (0x100000000)", 1, 18);
1661   ExpectFailure("optional_uint64: 0x10000000000000000\n",
1662                 "Integer out of range (0x10000000000000000)", 1, 18);
1663
1664   // Invalid values for a boolean field.
1665   ExpectFailure("optional_bool: \"hello\"\n",
1666                 "Expected identifier, got: \"hello\"", 1, 16);
1667   ExpectFailure("optional_bool: 5\n", "Integer out of range (5)", 1, 16);
1668   ExpectFailure("optional_bool: -7.5\n", "Expected identifier, got: -", 1, 16);
1669   ExpectFailure("optional_bool: !\n", "Expected identifier, got: !", 1, 16);
1670
1671   ExpectFailure(
1672       "optional_bool: meh\n",
1673       "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 2,
1674       1);
1675
1676   ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", 1,
1677                 15);
1678
1679   // Invalid values for a string field.
1680   ExpectFailure("optional_string: true\n", "Expected string, got: true", 1, 18);
1681   ExpectFailure("optional_string: 5\n", "Expected string, got: 5", 1, 18);
1682   ExpectFailure("optional_string: -7.5\n", "Expected string, got: -", 1, 18);
1683   ExpectFailure("optional_string: !\n", "Expected string, got: !", 1, 18);
1684   ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", 1,
1685                 17);
1686
1687   // Invalid values for an enumeration field.
1688   ExpectFailure("optional_nested_enum: \"hello\"\n",
1689                 "Expected integer or identifier, got: \"hello\"", 1, 23);
1690
1691   // Valid token, but enum value is not defined.
1692   ExpectFailure("optional_nested_enum: 5\n",
1693                 "Unknown enumeration value of \"5\" for field "
1694                 "\"optional_nested_enum\".",
1695                 2, 1);
1696   // We consume the negative sign, so the error position starts one character
1697   // later.
1698   ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer, got: 7.5", 1,
1699                 24);
1700   ExpectFailure("optional_nested_enum: !\n",
1701                 "Expected integer or identifier, got: !", 1, 23);
1702
1703   ExpectFailure("optional_nested_enum: grah\n",
1704                 "Unknown enumeration value of \"grah\" for field "
1705                 "\"optional_nested_enum\".",
1706                 2, 1);
1707
1708   ExpectFailure("optional_nested_enum {\n \n}\n",
1709                 "Expected \":\", found \"{\".", 1, 22);
1710 }
1711
1712 TEST_F(TextFormatParserTest, MessageDelimiters) {
1713   // Non-matching delimiters.
1714   ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", 3,
1715                 1);
1716
1717   // Invalid delimiters.
1718   ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", 1,
1719                 15);
1720
1721   // Unending message.
1722   ExpectFailure("optional_nested_message {\n \nbb: 118\n",
1723                 "Expected identifier, got: ", 4, 1);
1724 }
1725
1726 TEST_F(TextFormatParserTest, UnknownExtension) {
1727   // Non-matching delimiters.
1728   ExpectFailure("[blahblah]: 123",
1729                 "Extension \"blahblah\" is not defined or is not an "
1730                 "extension of \"protobuf_unittest.TestAllTypes\".",
1731                 1, 11);
1732 }
1733
1734 TEST_F(TextFormatParserTest, MissingRequired) {
1735   unittest::TestRequired message;
1736   ExpectFailure("a: 1", "Message missing required fields: b, c", 0, 1,
1737                 &message);
1738 }
1739
1740 TEST_F(TextFormatParserTest, ParseDuplicateRequired) {
1741   unittest::TestRequired message;
1742   ExpectFailure("a: 1 b: 2 c: 3 a: 1",
1743                 "Non-repeated field \"a\" is specified multiple times.", 1, 17,
1744                 &message);
1745 }
1746
1747 TEST_F(TextFormatParserTest, ParseDuplicateOptional) {
1748   unittest::ForeignMessage message;
1749   ExpectFailure("c: 1 c: 2",
1750                 "Non-repeated field \"c\" is specified multiple times.", 1, 7,
1751                 &message);
1752 }
1753
1754 TEST_F(TextFormatParserTest, MergeDuplicateRequired) {
1755   unittest::TestRequired message;
1756   TextFormat::Parser parser;
1757   EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message));
1758   EXPECT_EQ(4, message.a());
1759 }
1760
1761 TEST_F(TextFormatParserTest, MergeDuplicateOptional) {
1762   unittest::ForeignMessage message;
1763   TextFormat::Parser parser;
1764   EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message));
1765   EXPECT_EQ(2, message.c());
1766 }
1767
1768 TEST_F(TextFormatParserTest, ExplicitDelimiters) {
1769   unittest::TestRequired message;
1770   EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message));
1771   EXPECT_EQ(1, message.a());
1772   EXPECT_EQ(2, message.b());
1773   EXPECT_EQ(3, message.c());
1774 }
1775
1776 TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
1777   std::vector<std::string> errors;
1778
1779   {
1780     ScopedMemoryLog log;
1781     unittest::TestAllTypes proto;
1782     EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto));
1783     errors = log.GetMessages(ERROR);
1784   }
1785
1786   ASSERT_EQ(1, errors.size());
1787   EXPECT_EQ(
1788       "Error parsing text-format protobuf_unittest.TestAllTypes: "
1789       "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field "
1790       "named \"no_such_field\".",
1791       errors[0]);
1792 }
1793
1794 TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
1795   std::vector<std::string> errors;
1796
1797   {
1798     ScopedMemoryLog log;
1799     unittest::TestAllTypes proto;
1800     EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto));
1801     errors = log.GetMessages(ERROR);
1802   }
1803
1804   ASSERT_EQ(1, errors.size());
1805   EXPECT_EQ(
1806       "Error parsing text-format protobuf_unittest.TestAllTypes: "
1807       "1:1: Invalid control characters encountered in text.",
1808       errors[0]);
1809 }
1810
1811 TEST_F(TextFormatParserTest, ParseDeprecatedField) {
1812   unittest::TestDeprecatedFields message;
1813   ExpectMessage("deprecated_int32: 42",
1814                 "WARNING:text format contains deprecated field "
1815                 "\"deprecated_int32\"",
1816                 1, 21, &message, true);
1817 }
1818
1819 TEST_F(TextFormatParserTest, SetRecursionLimit) {
1820   const char* format = "child: { $0 }";
1821   std::string input;
1822   for (int i = 0; i < 100; ++i) input = strings::Substitute(format, input);
1823
1824   unittest::NestedTestAllTypes message;
1825   ExpectSuccessAndTree(input, &message, nullptr);
1826
1827   input = strings::Substitute(format, input);
1828   parser_.SetRecursionLimit(100);
1829   ExpectMessage(input, "Message is too deep", 1, 908, &message, false);
1830
1831   parser_.SetRecursionLimit(101);
1832   ExpectSuccessAndTree(input, &message, nullptr);
1833 }
1834
1835 class TextFormatMessageSetTest : public testing::Test {
1836  protected:
1837   static const char proto_debug_string_[];
1838 };
1839 const char TextFormatMessageSetTest::proto_debug_string_[] =
1840     "message_set {\n"
1841     "  [protobuf_unittest.TestMessageSetExtension1] {\n"
1842     "    i: 23\n"
1843     "  }\n"
1844     "  [protobuf_unittest.TestMessageSetExtension2] {\n"
1845     "    str: \"foo\"\n"
1846     "  }\n"
1847     "}\n";
1848
1849 TEST_F(TextFormatMessageSetTest, Serialize) {
1850   protobuf_unittest::TestMessageSetContainer proto;
1851   protobuf_unittest::TestMessageSetExtension1* item_a =
1852       proto.mutable_message_set()->MutableExtension(
1853           protobuf_unittest::TestMessageSetExtension1::message_set_extension);
1854   item_a->set_i(23);
1855   protobuf_unittest::TestMessageSetExtension2* item_b =
1856       proto.mutable_message_set()->MutableExtension(
1857           protobuf_unittest::TestMessageSetExtension2::message_set_extension);
1858   item_b->set_str("foo");
1859   EXPECT_EQ(proto_debug_string_, proto.DebugString());
1860 }
1861
1862 TEST_F(TextFormatMessageSetTest, Deserialize) {
1863   protobuf_unittest::TestMessageSetContainer proto;
1864   ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto));
1865   EXPECT_EQ(
1866       23,
1867       proto.message_set()
1868           .GetExtension(
1869               protobuf_unittest::TestMessageSetExtension1::message_set_extension)
1870           .i());
1871   EXPECT_EQ(
1872       "foo",
1873       proto.message_set()
1874           .GetExtension(
1875               protobuf_unittest::TestMessageSetExtension2::message_set_extension)
1876           .str());
1877
1878   // Ensure that these are the only entries present.
1879   std::vector<const FieldDescriptor*> descriptors;
1880   proto.message_set().GetReflection()->ListFields(proto.message_set(),
1881                                                   &descriptors);
1882   EXPECT_EQ(2, descriptors.size());
1883 }
1884
1885 TEST(TextFormatUnknownFieldTest, TestUnknownField) {
1886   protobuf_unittest::TestAllTypes proto;
1887   TextFormat::Parser parser;
1888   // Unknown field is not permitted by default.
1889   EXPECT_FALSE(parser.ParseFromString("unknown_field: 12345", &proto));
1890   EXPECT_FALSE(parser.ParseFromString("12345678: 12345", &proto));
1891
1892   parser.AllowUnknownField(true);
1893   EXPECT_TRUE(parser.ParseFromString("unknown_field: 12345", &proto));
1894   EXPECT_TRUE(parser.ParseFromString("unknown_field: -12345", &proto));
1895   EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345", &proto));
1896   EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345", &proto));
1897   EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345f", &proto));
1898   EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345f", &proto));
1899   EXPECT_TRUE(parser.ParseFromString("unknown_field: inf", &proto));
1900   EXPECT_TRUE(parser.ParseFromString("unknown_field: -inf", &proto));
1901   EXPECT_TRUE(parser.ParseFromString("unknown_field: TYPE_STRING", &proto));
1902   EXPECT_TRUE(
1903       parser.ParseFromString("unknown_field: \"string value\"", &proto));
1904   // Invalid field value
1905   EXPECT_FALSE(parser.ParseFromString("unknown_field: -TYPE_STRING", &proto));
1906   // Two or more unknown fields
1907   EXPECT_TRUE(
1908       parser.ParseFromString("unknown_field1: TYPE_STRING\n"
1909                              "unknown_field2: 12345",
1910                              &proto));
1911   // Unknown nested message
1912   EXPECT_TRUE(
1913       parser.ParseFromString("unknown_message1: {}\n"
1914                              "unknown_message2 {\n"
1915                              "  unknown_field: 12345\n"
1916                              "}\n"
1917                              "unknown_message3 <\n"
1918                              "  unknown_nested_message {\n"
1919                              "    unknown_field: 12345\n"
1920                              "  }\n"
1921                              ">",
1922                              &proto));
1923   // Unmatched delimeters for message body
1924   EXPECT_FALSE(parser.ParseFromString("unknown_message: {>", &proto));
1925   // Unknown extension
1926   EXPECT_TRUE(
1927       parser.ParseFromString("[somewhere.unknown_extension1]: 12345\n"
1928                              "[somewhere.unknown_extension2] {\n"
1929                              "  unknown_field: 12345\n"
1930                              "}",
1931                              &proto));
1932   // Unknown fields between known fields
1933   ASSERT_TRUE(
1934       parser.ParseFromString("optional_int32: 1\n"
1935                              "unknown_field: 12345\n"
1936                              "optional_string: \"string\"\n"
1937                              "unknown_message { unknown: 0 }\n"
1938                              "optional_nested_message { bb: 2 }",
1939                              &proto));
1940   EXPECT_EQ(1, proto.optional_int32());
1941   EXPECT_EQ("string", proto.optional_string());
1942   EXPECT_EQ(2, proto.optional_nested_message().bb());
1943
1944   // Unknown field with numeric tag number instead of identifier.
1945   EXPECT_TRUE(parser.ParseFromString("12345678: 12345", &proto));
1946
1947   // Nested unknown extensions.
1948   EXPECT_TRUE(
1949       parser.ParseFromString("[test.extension1] <\n"
1950                              "  unknown_nested_message <\n"
1951                              "    [test.extension2] <\n"
1952                              "      unknown_field: 12345\n"
1953                              "    >\n"
1954                              "  >\n"
1955                              ">",
1956                              &proto));
1957   EXPECT_TRUE(
1958       parser.ParseFromString("[test.extension1] {\n"
1959                              "  unknown_nested_message {\n"
1960                              "    [test.extension2] {\n"
1961                              "      unknown_field: 12345\n"
1962                              "    }\n"
1963                              "  }\n"
1964                              "}",
1965                              &proto));
1966   EXPECT_TRUE(
1967       parser.ParseFromString("[test.extension1] <\n"
1968                              "  some_unknown_fields: <\n"
1969                              "    unknown_field: 12345\n"
1970                              "  >\n"
1971                              ">",
1972                              &proto));
1973   EXPECT_TRUE(
1974       parser.ParseFromString("[test.extension1] {\n"
1975                              "  some_unknown_fields: {\n"
1976                              "    unknown_field: 12345\n"
1977                              "  }\n"
1978                              "}",
1979                              &proto));
1980
1981   // Unknown field with compact repetition.
1982   EXPECT_TRUE(parser.ParseFromString("unknown_field: [1, 2]", &proto));
1983   // Unknown field with compact repetition of some unknown enum.
1984   EXPECT_TRUE(parser.ParseFromString("unknown_field: [VAL1, VAL2]", &proto));
1985   // Unknown field with compact repetition with sub-message.
1986   EXPECT_TRUE(parser.ParseFromString("unknown_field: [{a:1}, <b:2>]", &proto));
1987 }
1988
1989 TEST(TextFormatUnknownFieldTest, TestAnyInUnknownField) {
1990   protobuf_unittest::TestAllTypes proto;
1991   TextFormat::Parser parser;
1992   parser.AllowUnknownField(true);
1993   EXPECT_TRUE(
1994       parser.ParseFromString("unknown {\n"
1995                              "  [type.googleapis.com/foo.bar] {\n"
1996                              "  }\n"
1997                              "}",
1998                              &proto));
1999 }
2000
2001 TEST(TextFormatUnknownFieldTest, TestUnknownExtension) {
2002   protobuf_unittest::TestAllTypes proto;
2003   TextFormat::Parser parser;
2004   std::string message_with_ext =
2005       "[test.extension1] {\n"
2006       "  some_unknown_fields: {\n"
2007       "    unknown_field: 12345\n"
2008       "  }\n"
2009       "}";
2010   // Unknown extensions are not permitted by default.
2011   EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto));
2012   // AllowUnknownField implies AllowUnknownExtension.
2013   parser.AllowUnknownField(true);
2014   EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto));
2015
2016   parser.AllowUnknownField(false);
2017   EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto));
2018   parser.AllowUnknownExtension(true);
2019   EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto));
2020   // Unknown fields are still not accepted.
2021   EXPECT_FALSE(parser.ParseFromString("unknown_field: 1", &proto));
2022 }
2023
2024
2025 }  // namespace text_format_unittest
2026 }  // namespace protobuf
2027 }  // namespace google