1
1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.
2
// Copyright 2008 Google Inc. All rights reserved.
3
3
// http://code.google.com/p/protobuf/
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
17
31
// Author: jschorr@google.com (Joseph Schorr)
18
32
// Based on original Protocol Buffers design by
48
64
// A basic string with different escapable characters for testing.
49
65
const string kEscapeTestString =
50
66
"\"A string with ' characters \n and \r newlines and \t tabs and \001 "
67
"slashes \\ and multiple spaces";
53
69
// A representation of the above string with all the characters escaped.
54
70
const string kEscapeTestStringEscaped =
55
71
"\"\\\"A string with \\' characters \\n and \\r newlines "
56
"and \\t tabs and \\001 slashes \\\\\"";
72
"and \\t tabs and \\001 slashes \\\\ and multiple spaces\"";
58
74
class TextFormatTest : public testing::Test {
110
126
EXPECT_EQ(proto_debug_string_, proto_.DebugString());
129
TEST_F(TextFormatTest, ShortDebugString) {
130
proto_.set_optional_int32(1);
131
proto_.set_optional_string("hello");
132
proto_.mutable_optional_nested_message()->set_bb(2);
133
proto_.mutable_optional_foreign_message();
135
EXPECT_EQ("optional_int32: 1 optional_string: \"hello\" "
136
"optional_nested_message { bb: 2 } "
137
"optional_foreign_message { }",
138
proto_.ShortDebugString());
113
141
TEST_F(TextFormatTest, StringEscape) {
114
142
// Set the string value to test.
115
143
proto_.set_optional_string(kEscapeTestString);
158
190
message.DebugString());
193
TEST_F(TextFormatTest, PrintUnknownMessage) {
194
// Test heuristic printing of messages in an UnknownFieldSet.
196
protobuf_unittest::TestAllTypes message;
198
// Cases which should not be interpreted as sub-messages.
200
// 'a' is a valid FIXED64 tag, so for the string to be parseable as a message
201
// it should be followed by 8 bytes. Since this string only has two
202
// subsequent bytes, it should be treated as a string.
203
message.add_repeated_string("abc");
205
// 'd' happens to be a valid ENDGROUP tag. So,
206
// UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but
207
// the ConsumedEntireMessage() check should fail.
208
message.add_repeated_string("def");
210
// A zero-length string should never be interpreted as a message even though
211
// it is technically valid as one.
212
message.add_repeated_string("");
214
// Case which should be interpreted as a sub-message.
216
// An actual nested message with content should always be interpreted as a
218
message.add_repeated_nested_message()->set_bb(123);
221
message.SerializeToString(&data);
224
UnknownFieldSet unknown_fields;
225
EXPECT_TRUE(unknown_fields.ParseFromString(data));
226
EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text));
161
237
TEST_F(TextFormatTest, ParseBasic) {
162
238
io::ArrayInputStream input_stream(proto_debug_string_.data(),
163
239
proto_debug_string_.size());
710
TEST_F(TextFormatParserTest, ParseDuplicateRequired) {
711
unittest::TestRequired message;
712
ExpectFailure("a: 1 b: 2 c: 3 a: 1",
713
"Non-repeated field \"a\" is specified multiple times.",
717
TEST_F(TextFormatParserTest, ParseDuplicateOptional) {
718
unittest::ForeignMessage message;
719
ExpectFailure("c: 1 c: 2",
720
"Non-repeated field \"c\" is specified multiple times.",
724
TEST_F(TextFormatParserTest, MergeDuplicateRequired) {
725
unittest::TestRequired message;
726
TextFormat::Parser parser;
727
EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message));
728
EXPECT_EQ(4, message.a());
731
TEST_F(TextFormatParserTest, MergeDuplicateOptional) {
732
unittest::ForeignMessage message;
733
TextFormat::Parser parser;
734
EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message));
735
EXPECT_EQ(2, message.c());
634
738
TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
635
739
vector<string> errors;
755
TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
756
vector<string> errors;
760
unittest::TestAllTypes proto;
761
EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto));
762
errors = log.GetMessages(ERROR);
765
ASSERT_EQ(1, errors.size());
766
EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: "
767
"1:1: Invalid control characters encountered in text.",
652
772
class TextFormatMessageSetTest : public testing::Test {
688
808
// Ensure that these are the only entries present.
689
809
vector<const FieldDescriptor*> descriptors;
690
proto.message_set().GetReflection()->ListFields(&descriptors);
810
proto.message_set().GetReflection()->ListFields(
811
proto.message_set(), &descriptors);
691
812
EXPECT_EQ(2, descriptors.size());
815
} // namespace text_format_unittest
695
816
} // namespace protobuf
697
818
} // namespace google