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: kenton@google.com (Kenton Varda)
18
32
// Based on original Protocol Buffers design by
198
199
TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data);
202
TEST(WireFormatTest, ParseMultipleExtensionRanges) {
203
// Make sure we can parse a message that contains multiple extensions ranges.
204
unittest::TestFieldOrderings source;
207
TestUtil::SetAllFieldsAndExtensions(&source);
208
source.SerializeToString(&data);
211
unittest::TestFieldOrderings dest;
212
EXPECT_TRUE(dest.ParseFromString(data));
213
EXPECT_EQ(source.DebugString(), dest.DebugString());
216
// Also test using reflection-based parsing.
218
unittest::TestFieldOrderings dest;
219
io::ArrayInputStream raw_input(data.data(), data.size());
220
io::CodedInputStream coded_input(&raw_input);
221
EXPECT_TRUE(WireFormat::ParseAndMergePartial(&coded_input, &dest));
222
EXPECT_EQ(source.DebugString(), dest.DebugString());
201
226
const int kUnknownTypeId = 1550055;
203
228
TEST(WireFormatTest, SerializeMessageSet) {
520
545
EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
548
TEST_F(WireFormatInvalidInputTest, InvalidStringInUnknownGroup) {
549
// Test a bug fix: SkipMessage should fail if the message contains a string
550
// whose length would extend beyond the message end.
552
unittest::TestAllTypes message;
553
message.set_optional_string("foo foo foo foo");
555
message.SerializeToString(&data);
557
// Chop some bytes off the end.
558
data.resize(data.size() - 4);
560
// Try to skip it. Note that the bug was only present when parsing to an
562
io::ArrayInputStream raw_input(data.data(), data.size());
563
io::CodedInputStream coded_input(&raw_input);
564
UnknownFieldSet unknown_fields;
565
EXPECT_FALSE(WireFormat::SkipMessage(&coded_input, &unknown_fields));
568
// Test differences between string and bytes.
569
// Value of a string type must be valid UTF-8 string. When UTF-8
570
// validation is enabled (GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED):
571
// WriteInvalidUTF8String: see error message.
572
// ReadInvalidUTF8String: see error message.
573
// WriteValidUTF8String: fine.
574
// ReadValidUTF8String: fine.
575
// WriteAnyBytes: fine.
576
// ReadAnyBytes: fine.
577
const char * kInvalidUTF8String = "Invalid UTF-8: \xA0\xB0\xC0\xD0";
578
const char * kValidUTF8String = "Valid UTF-8: \x01\x02\u8C37\u6B4C";
581
bool WriteMessage(const char *value, T *message, string *wire_buffer) {
582
message->set_data(value);
583
wire_buffer->clear();
584
message->AppendToString(wire_buffer);
585
return (wire_buffer->size() > 0);
589
bool ReadMessage(const string &wire_buffer, T *message) {
590
return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
593
TEST(Utf8ValidationTest, WriteInvalidUTF8String) {
595
protobuf_unittest::OneString input;
596
vector<string> errors;
599
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
600
errors = log.GetMessages(ERROR);
602
#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
603
ASSERT_EQ(1, errors.size());
604
EXPECT_EQ("Encountered string containing invalid UTF-8 data while "
605
"serializing protocol buffer. Strings must contain only UTF-8; "
606
"use the 'bytes' type for raw bytes.",
610
ASSERT_EQ(0, errors.size());
611
#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
614
TEST(Utf8ValidationTest, ReadInvalidUTF8String) {
616
protobuf_unittest::OneString input;
617
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
618
protobuf_unittest::OneString output;
619
vector<string> errors;
622
ReadMessage(wire_buffer, &output);
623
errors = log.GetMessages(ERROR);
625
#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
626
ASSERT_EQ(1, errors.size());
627
EXPECT_EQ("Encountered string containing invalid UTF-8 data while "
628
"parsing protocol buffer. Strings must contain only UTF-8; "
629
"use the 'bytes' type for raw bytes.",
633
ASSERT_EQ(0, errors.size());
634
#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
637
TEST(Utf8ValidationTest, WriteValidUTF8String) {
639
protobuf_unittest::OneString input;
640
vector<string> errors;
643
WriteMessage(kValidUTF8String, &input, &wire_buffer);
644
errors = log.GetMessages(ERROR);
646
ASSERT_EQ(0, errors.size());
649
TEST(Utf8ValidationTest, ReadValidUTF8String) {
651
protobuf_unittest::OneString input;
652
WriteMessage(kValidUTF8String, &input, &wire_buffer);
653
protobuf_unittest::OneString output;
654
vector<string> errors;
657
ReadMessage(wire_buffer, &output);
658
errors = log.GetMessages(ERROR);
660
ASSERT_EQ(0, errors.size());
661
EXPECT_EQ(input.data(), output.data());
664
// Bytes: anything can pass as bytes, use invalid UTF-8 string to test
665
TEST(Utf8ValidationTest, WriteArbitraryBytes) {
667
protobuf_unittest::OneBytes input;
668
vector<string> errors;
671
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
672
errors = log.GetMessages(ERROR);
674
ASSERT_EQ(0, errors.size());
677
TEST(Utf8ValidationTest, ReadArbitraryBytes) {
679
protobuf_unittest::OneBytes input;
680
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
681
protobuf_unittest::OneBytes output;
682
vector<string> errors;
685
ReadMessage(wire_buffer, &output);
686
errors = log.GetMessages(ERROR);
688
ASSERT_EQ(0, errors.size());
689
EXPECT_EQ(input.data(), output.data());
524
693
} // namespace internal
525
694
} // namespace protobuf