~ubuntu-branches/debian/squeeze/protobuf/squeeze

« back to all changes in this revision

Viewing changes to src/google/protobuf/wire_format_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2009-06-02 16:19:00 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090602161900-vm176i3ryt35yk91
Tags: 2.0.3-2.2
* Non-maintainer upload.
* Fix FTBFS from -2.1: don't fail when we can't clean up the java build,
  such as when openjdk isn't installed.
* Disable parallel builds, because libtool is made of fail (if binary-arch
  and build-indep run concurrently, we relink a library while it's being
  used; that doesn't work so well).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/
4
4
//
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
8
 
//
9
 
//      http://www.apache.org/licenses/LICENSE-2.0
10
 
//
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
 
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.
16
30
 
17
31
// Author: kenton@google.com (Kenton Varda)
18
32
//  Based on original Protocol Buffers design by
53
67
  // Parse using WireFormat.
54
68
  io::ArrayInputStream raw_input(data.data(), data.size());
55
69
  io::CodedInputStream input(&raw_input);
56
 
  WireFormat::ParseAndMergePartial(unittest::TestAllTypes::descriptor(),
57
 
                                   &input, dest.GetReflection());
 
70
  WireFormat::ParseAndMergePartial(&input, &dest);
58
71
 
59
72
  // Check.
60
73
  TestUtil::ExpectAllFieldsSet(dest);
71
84
  // Parse using WireFormat.
72
85
  io::ArrayInputStream raw_input(data.data(), data.size());
73
86
  io::CodedInputStream input(&raw_input);
74
 
  WireFormat::ParseAndMergePartial(unittest::TestAllExtensions::descriptor(),
75
 
                                   &input, dest.GetReflection());
 
87
  WireFormat::ParseAndMergePartial(&input, &dest);
76
88
 
77
89
  // Check.
78
90
  TestUtil::ExpectAllExtensionsSet(dest);
82
94
  unittest::TestAllTypes message;
83
95
  TestUtil::SetAllFields(&message);
84
96
 
85
 
  EXPECT_EQ(message.ByteSize(),
86
 
            WireFormat::ByteSize(unittest::TestAllTypes::descriptor(),
87
 
                                 message.GetReflection()));
 
97
  EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
88
98
  message.Clear();
89
99
  EXPECT_EQ(0, message.ByteSize());
90
 
  EXPECT_EQ(0, WireFormat::ByteSize(unittest::TestAllTypes::descriptor(),
91
 
                                    message.GetReflection()));
 
100
  EXPECT_EQ(0, WireFormat::ByteSize(message));
92
101
}
93
102
 
94
103
TEST(WireFormatTest, ByteSizeExtensions) {
96
105
  TestUtil::SetAllExtensions(&message);
97
106
 
98
107
  EXPECT_EQ(message.ByteSize(),
99
 
            WireFormat::ByteSize(unittest::TestAllExtensions::descriptor(),
100
 
                                 message.GetReflection()));
 
108
            WireFormat::ByteSize(message));
101
109
  message.Clear();
102
110
  EXPECT_EQ(0, message.ByteSize());
103
 
  EXPECT_EQ(0, WireFormat::ByteSize(unittest::TestAllExtensions::descriptor(),
104
 
                                    message.GetReflection()));
 
111
  EXPECT_EQ(0, WireFormat::ByteSize(message));
105
112
}
106
113
 
107
114
TEST(WireFormatTest, Serialize) {
123
130
  {
124
131
    io::StringOutputStream raw_output(&dynamic_data);
125
132
    io::CodedOutputStream output(&raw_output);
126
 
    WireFormat::SerializeWithCachedSizes(
127
 
      unittest::TestAllTypes::descriptor(),
128
 
      message.GetReflection(), size, &output);
 
133
    WireFormat::SerializeWithCachedSizes(message, size, &output);
129
134
  }
130
135
 
131
136
  // Should be the same.
153
158
  {
154
159
    io::StringOutputStream raw_output(&dynamic_data);
155
160
    io::CodedOutputStream output(&raw_output);
156
 
    WireFormat::SerializeWithCachedSizes(
157
 
      unittest::TestAllExtensions::descriptor(),
158
 
      message.GetReflection(), size, &output);
 
161
    WireFormat::SerializeWithCachedSizes(message, size, &output);
159
162
  }
160
163
 
161
164
  // Should be the same.
183
186
  {
184
187
    io::StringOutputStream raw_output(&dynamic_data);
185
188
    io::CodedOutputStream output(&raw_output);
186
 
    WireFormat::SerializeWithCachedSizes(
187
 
      unittest::TestFieldOrderings::descriptor(),
188
 
      message.GetReflection(), size, &output);
 
189
    WireFormat::SerializeWithCachedSizes(message, size, &output);
189
190
  }
190
191
 
191
192
  // Should be the same.
198
199
  TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data);
199
200
}
200
201
 
 
202
TEST(WireFormatTest, ParseMultipleExtensionRanges) {
 
203
  // Make sure we can parse a message that contains multiple extensions ranges.
 
204
  unittest::TestFieldOrderings source;
 
205
  string data;
 
206
 
 
207
  TestUtil::SetAllFieldsAndExtensions(&source);
 
208
  source.SerializeToString(&data);
 
209
 
 
210
  {
 
211
    unittest::TestFieldOrderings dest;
 
212
    EXPECT_TRUE(dest.ParseFromString(data));
 
213
    EXPECT_EQ(source.DebugString(), dest.DebugString());
 
214
  }
 
215
 
 
216
  // Also test using reflection-based parsing.
 
217
  {
 
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());
 
223
  }
 
224
}
 
225
 
201
226
const int kUnknownTypeId = 1550055;
202
227
 
203
228
TEST(WireFormatTest, SerializeMessageSet) {
420
445
      io::StringOutputStream raw_output(&result);
421
446
      io::CodedOutputStream output(&raw_output);
422
447
 
423
 
      EXPECT_TRUE(WireFormat::WriteString(
 
448
      EXPECT_TRUE(WireFormat::WriteBytes(
424
449
        field->number(), string(bytes, size), &output));
425
450
    }
426
451
 
497
522
}
498
523
 
499
524
TEST_F(WireFormatInvalidInputTest, InvalidUnknownGroup) {
500
 
  // Use ForeignMessage so that the group made by MakeInvalidGroup will not
 
525
  // Use TestEmptyMessage so that the group made by MakeInvalidGroup will not
501
526
  // be a known tag number.
502
 
  unittest::ForeignMessage message;
 
527
  unittest::TestEmptyMessage message;
503
528
 
504
529
  // Control case.
505
530
  EXPECT_TRUE(message.ParseFromString(MakeInvalidGroup("", 0, true)));
520
545
  EXPECT_FALSE(message.ParseFromString(MakeInvalidGroup("\017", 1, true)));
521
546
}
522
547
 
 
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.
 
551
 
 
552
  unittest::TestAllTypes message;
 
553
  message.set_optional_string("foo foo foo foo");
 
554
  string data;
 
555
  message.SerializeToString(&data);
 
556
 
 
557
  // Chop some bytes off the end.
 
558
  data.resize(data.size() - 4);
 
559
 
 
560
  // Try to skip it.  Note that the bug was only present when parsing to an
 
561
  // UnknownFieldSet.
 
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));
 
566
}
 
567
 
 
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";
 
579
 
 
580
template<typename T>
 
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);
 
586
}
 
587
 
 
588
template<typename T>
 
589
bool ReadMessage(const string &wire_buffer, T *message) {
 
590
  return message->ParseFromArray(wire_buffer.data(), wire_buffer.size());
 
591
}
 
592
 
 
593
TEST(Utf8ValidationTest, WriteInvalidUTF8String) {
 
594
  string wire_buffer;
 
595
  protobuf_unittest::OneString input;
 
596
  vector<string> errors;
 
597
  {
 
598
    ScopedMemoryLog log;
 
599
    WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
 
600
    errors = log.GetMessages(ERROR);
 
601
  }
 
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.",
 
607
            errors[0]);
 
608
 
 
609
#else
 
610
  ASSERT_EQ(0, errors.size());
 
611
#endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
 
612
}
 
613
 
 
614
TEST(Utf8ValidationTest, ReadInvalidUTF8String) {
 
615
  string wire_buffer;
 
616
  protobuf_unittest::OneString input;
 
617
  WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
 
618
  protobuf_unittest::OneString output;
 
619
  vector<string> errors;
 
620
  {
 
621
    ScopedMemoryLog log;
 
622
    ReadMessage(wire_buffer, &output);
 
623
    errors = log.GetMessages(ERROR);
 
624
  }
 
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.",
 
630
            errors[0]);
 
631
 
 
632
#else
 
633
  ASSERT_EQ(0, errors.size());
 
634
#endif  // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
 
635
}
 
636
 
 
637
TEST(Utf8ValidationTest, WriteValidUTF8String) {
 
638
  string wire_buffer;
 
639
  protobuf_unittest::OneString input;
 
640
  vector<string> errors;
 
641
  {
 
642
    ScopedMemoryLog log;
 
643
    WriteMessage(kValidUTF8String, &input, &wire_buffer);
 
644
    errors = log.GetMessages(ERROR);
 
645
  }
 
646
  ASSERT_EQ(0, errors.size());
 
647
}
 
648
 
 
649
TEST(Utf8ValidationTest, ReadValidUTF8String) {
 
650
  string wire_buffer;
 
651
  protobuf_unittest::OneString input;
 
652
  WriteMessage(kValidUTF8String, &input, &wire_buffer);
 
653
  protobuf_unittest::OneString output;
 
654
  vector<string> errors;
 
655
  {
 
656
    ScopedMemoryLog log;
 
657
    ReadMessage(wire_buffer, &output);
 
658
    errors = log.GetMessages(ERROR);
 
659
  }
 
660
  ASSERT_EQ(0, errors.size());
 
661
  EXPECT_EQ(input.data(), output.data());
 
662
}
 
663
 
 
664
// Bytes: anything can pass as bytes, use invalid UTF-8 string to test
 
665
TEST(Utf8ValidationTest, WriteArbitraryBytes) {
 
666
  string wire_buffer;
 
667
  protobuf_unittest::OneBytes input;
 
668
  vector<string> errors;
 
669
  {
 
670
    ScopedMemoryLog log;
 
671
    WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
 
672
    errors = log.GetMessages(ERROR);
 
673
  }
 
674
  ASSERT_EQ(0, errors.size());
 
675
}
 
676
 
 
677
TEST(Utf8ValidationTest, ReadArbitraryBytes) {
 
678
  string wire_buffer;
 
679
  protobuf_unittest::OneBytes input;
 
680
  WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
 
681
  protobuf_unittest::OneBytes output;
 
682
  vector<string> errors;
 
683
  {
 
684
    ScopedMemoryLog log;
 
685
    ReadMessage(wire_buffer, &output);
 
686
    errors = log.GetMessages(ERROR);
 
687
  }
 
688
  ASSERT_EQ(0, errors.size());
 
689
  EXPECT_EQ(input.data(), output.data());
 
690
}
 
691
 
523
692
}  // namespace
524
693
}  // namespace internal
525
694
}  // namespace protobuf