1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
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: kenton@google.com (Kenton Varda)
32//  Based on original Protocol Buffers design by
33//  Sanjay Ghemawat, Jeff Dean, and others.
34//
35// This test is testing a lot more than just the UnknownFieldSet class.  It
36// tests handling of unknown fields throughout the system.
37
38#include <google/protobuf/unknown_field_set.h>
39#include <google/protobuf/descriptor.h>
40#include <google/protobuf/io/coded_stream.h>
41#include <google/protobuf/io/zero_copy_stream_impl.h>
42#include <google/protobuf/wire_format.h>
43#include <google/protobuf/unittest.pb.h>
44#include <google/protobuf/test_util.h>
45
46#include <google/protobuf/stubs/common.h>
47#include <google/protobuf/testing/googletest.h>
48#include <gtest/gtest.h>
49#include <google/protobuf/stubs/stl_util-inl.h>
50
51namespace google {
52namespace protobuf {
53
54using internal::WireFormat;
55
56namespace {
57
58class UnknownFieldSetTest : public testing::Test {
59 protected:
60  virtual void SetUp() {
61    descriptor_ = unittest::TestAllTypes::descriptor();
62    TestUtil::SetAllFields(&all_fields_);
63    all_fields_.SerializeToString(&all_fields_data_);
64    ASSERT_TRUE(empty_message_.ParseFromString(all_fields_data_));
65    unknown_fields_ = empty_message_.mutable_unknown_fields();
66  }
67
68  const UnknownField* GetField(const string& name) {
69    const FieldDescriptor* field = descriptor_->FindFieldByName(name);
70    if (field == NULL) return NULL;
71    for (int i = 0; i < unknown_fields_->field_count(); i++) {
72      if (unknown_fields_->field(i).number() == field->number()) {
73        return &unknown_fields_->field(i);
74      }
75    }
76    return NULL;
77  }
78
79  // Constructs a protocol buffer which contains fields with all the same
80  // numbers as all_fields_data_ except that each field is some other wire
81  // type.
82  string GetBizarroData() {
83    unittest::TestEmptyMessage bizarro_message;
84    UnknownFieldSet* bizarro_unknown_fields =
85      bizarro_message.mutable_unknown_fields();
86    for (int i = 0; i < unknown_fields_->field_count(); i++) {
87      const UnknownField& unknown_field = unknown_fields_->field(i);
88      if (unknown_field.type() == UnknownField::TYPE_VARINT) {
89        bizarro_unknown_fields->AddFixed32(unknown_field.number(), 1);
90      } else {
91        bizarro_unknown_fields->AddVarint(unknown_field.number(), 1);
92      }
93    }
94
95    string data;
96    EXPECT_TRUE(bizarro_message.SerializeToString(&data));
97    return data;
98  }
99
100  const Descriptor* descriptor_;
101  unittest::TestAllTypes all_fields_;
102  string all_fields_data_;
103
104  // An empty message that has been parsed from all_fields_data_.  So, it has
105  // unknown fields of every type.
106  unittest::TestEmptyMessage empty_message_;
107  UnknownFieldSet* unknown_fields_;
108};
109
110TEST_F(UnknownFieldSetTest, AllFieldsPresent) {
111  // All fields of TestAllTypes should be present, in numeric order (because
112  // that's the order we parsed them in).  Fields that are not valid field
113  // numbers of TestAllTypes should NOT be present.
114
115  int pos = 0;
116
117  for (int i = 0; i < 1000; i++) {
118    const FieldDescriptor* field = descriptor_->FindFieldByNumber(i);
119    if (field != NULL) {
120      ASSERT_LT(pos, unknown_fields_->field_count());
121      EXPECT_EQ(i, unknown_fields_->field(pos++).number());
122      if (field->is_repeated()) {
123        // Should have a second instance.
124        ASSERT_LT(pos, unknown_fields_->field_count());
125        EXPECT_EQ(i, unknown_fields_->field(pos++).number());
126      }
127    }
128  }
129  EXPECT_EQ(unknown_fields_->field_count(), pos);
130}
131
132TEST_F(UnknownFieldSetTest, Varint) {
133  const UnknownField* field = GetField("optional_int32");
134  ASSERT_TRUE(field != NULL);
135
136  ASSERT_EQ(UnknownField::TYPE_VARINT, field->type());
137  EXPECT_EQ(all_fields_.optional_int32(), field->varint());
138}
139
140TEST_F(UnknownFieldSetTest, Fixed32) {
141  const UnknownField* field = GetField("optional_fixed32");
142  ASSERT_TRUE(field != NULL);
143
144  ASSERT_EQ(UnknownField::TYPE_FIXED32, field->type());
145  EXPECT_EQ(all_fields_.optional_fixed32(), field->fixed32());
146}
147
148TEST_F(UnknownFieldSetTest, Fixed64) {
149  const UnknownField* field = GetField("optional_fixed64");
150  ASSERT_TRUE(field != NULL);
151
152  ASSERT_EQ(UnknownField::TYPE_FIXED64, field->type());
153  EXPECT_EQ(all_fields_.optional_fixed64(), field->fixed64());
154}
155
156TEST_F(UnknownFieldSetTest, LengthDelimited) {
157  const UnknownField* field = GetField("optional_string");
158  ASSERT_TRUE(field != NULL);
159
160  ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED, field->type());
161  EXPECT_EQ(all_fields_.optional_string(), field->length_delimited());
162}
163
164TEST_F(UnknownFieldSetTest, Group) {
165  const UnknownField* field = GetField("optionalgroup");
166  ASSERT_TRUE(field != NULL);
167
168  ASSERT_EQ(UnknownField::TYPE_GROUP, field->type());
169  ASSERT_EQ(1, field->group().field_count());
170
171  const UnknownField& nested_field = field->group().field(0);
172  const FieldDescriptor* nested_field_descriptor =
173    unittest::TestAllTypes::OptionalGroup::descriptor()->FindFieldByName("a");
174  ASSERT_TRUE(nested_field_descriptor != NULL);
175
176  EXPECT_EQ(nested_field_descriptor->number(), nested_field.number());
177  ASSERT_EQ(UnknownField::TYPE_VARINT, nested_field.type());
178  EXPECT_EQ(all_fields_.optionalgroup().a(), nested_field.varint());
179}
180
181TEST_F(UnknownFieldSetTest, SerializeFastAndSlowAreEquivalent) {
182  int size = WireFormat::ComputeUnknownFieldsSize(
183      empty_message_.unknown_fields());
184  string slow_buffer;
185  string fast_buffer;
186  slow_buffer.resize(size);
187  fast_buffer.resize(size);
188
189  uint8* target = reinterpret_cast<uint8*>(string_as_array(&fast_buffer));
190  uint8* result = WireFormat::SerializeUnknownFieldsToArray(
191          empty_message_.unknown_fields(), target);
192  EXPECT_EQ(size, result - target);
193
194  {
195    io::ArrayOutputStream raw_stream(string_as_array(&slow_buffer), size, 1);
196    io::CodedOutputStream output_stream(&raw_stream);
197    WireFormat::SerializeUnknownFields(empty_message_.unknown_fields(),
198                                       &output_stream);
199    ASSERT_FALSE(output_stream.HadError());
200  }
201  EXPECT_TRUE(fast_buffer == slow_buffer);
202}
203
204TEST_F(UnknownFieldSetTest, Serialize) {
205  // Check that serializing the UnknownFieldSet produces the original data
206  // again.
207
208  string data;
209  empty_message_.SerializeToString(&data);
210
211  // Don't use EXPECT_EQ because we don't want to dump raw binary data to
212  // stdout.
213  EXPECT_TRUE(data == all_fields_data_);
214}
215
216TEST_F(UnknownFieldSetTest, ParseViaReflection) {
217  // Make sure fields are properly parsed to the UnknownFieldSet when parsing
218  // via reflection.
219
220  unittest::TestEmptyMessage message;
221  io::ArrayInputStream raw_input(all_fields_data_.data(),
222                                 all_fields_data_.size());
223  io::CodedInputStream input(&raw_input);
224  ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &message));
225
226  EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
227}
228
229TEST_F(UnknownFieldSetTest, SerializeViaReflection) {
230  // Make sure fields are properly written from the UnknownFieldSet when
231  // serializing via reflection.
232
233  string data;
234
235  {
236    io::StringOutputStream raw_output(&data);
237    io::CodedOutputStream output(&raw_output);
238    int size = WireFormat::ByteSize(empty_message_);
239    WireFormat::SerializeWithCachedSizes(empty_message_, size, &output);
240    ASSERT_FALSE(output.HadError());
241  }
242
243  // Don't use EXPECT_EQ because we don't want to dump raw binary data to
244  // stdout.
245  EXPECT_TRUE(data == all_fields_data_);
246}
247
248TEST_F(UnknownFieldSetTest, CopyFrom) {
249  unittest::TestEmptyMessage message;
250
251  message.CopyFrom(empty_message_);
252
253  EXPECT_EQ(empty_message_.DebugString(), message.DebugString());
254}
255
256TEST_F(UnknownFieldSetTest, Swap) {
257  unittest::TestEmptyMessage other_message;
258  ASSERT_TRUE(other_message.ParseFromString(GetBizarroData()));
259
260  EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
261  EXPECT_GT(other_message.unknown_fields().field_count(), 0);
262  const string debug_string = empty_message_.DebugString();
263  const string other_debug_string = other_message.DebugString();
264  EXPECT_NE(debug_string, other_debug_string);
265
266  empty_message_.Swap(&other_message);
267  EXPECT_EQ(debug_string, other_message.DebugString());
268  EXPECT_EQ(other_debug_string, empty_message_.DebugString());
269}
270
271TEST_F(UnknownFieldSetTest, SwapWithSelf) {
272  const string debug_string = empty_message_.DebugString();
273  EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
274
275  empty_message_.Swap(&empty_message_);
276  EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
277  EXPECT_EQ(debug_string, empty_message_.DebugString());
278}
279
280TEST_F(UnknownFieldSetTest, MergeFrom) {
281  unittest::TestEmptyMessage source, destination;
282
283  destination.mutable_unknown_fields()->AddVarint(1, 1);
284  destination.mutable_unknown_fields()->AddVarint(3, 2);
285  source.mutable_unknown_fields()->AddVarint(2, 3);
286  source.mutable_unknown_fields()->AddVarint(3, 4);
287
288  destination.MergeFrom(source);
289
290  EXPECT_EQ(
291    // Note:  The ordering of fields here depends on the ordering of adds
292    //   and merging, above.
293    "1: 1\n"
294    "3: 2\n"
295    "2: 3\n"
296    "3: 4\n",
297    destination.DebugString());
298}
299
300TEST_F(UnknownFieldSetTest, Clear) {
301  // Clear the set.
302  empty_message_.Clear();
303  EXPECT_EQ(0, unknown_fields_->field_count());
304}
305
306TEST_F(UnknownFieldSetTest, ParseKnownAndUnknown) {
307  // Test mixing known and unknown fields when parsing.
308
309  unittest::TestEmptyMessage source;
310  source.mutable_unknown_fields()->AddVarint(123456, 654321);
311  string data;
312  ASSERT_TRUE(source.SerializeToString(&data));
313
314  unittest::TestAllTypes destination;
315  ASSERT_TRUE(destination.ParseFromString(all_fields_data_ + data));
316
317  TestUtil::ExpectAllFieldsSet(destination);
318  ASSERT_EQ(1, destination.unknown_fields().field_count());
319  ASSERT_EQ(UnknownField::TYPE_VARINT,
320            destination.unknown_fields().field(0).type());
321  EXPECT_EQ(654321, destination.unknown_fields().field(0).varint());
322}
323
324TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknown) {
325  // Test that fields of the wrong wire type are treated like unknown fields
326  // when parsing.
327
328  unittest::TestAllTypes all_types_message;
329  unittest::TestEmptyMessage empty_message;
330  string bizarro_data = GetBizarroData();
331  ASSERT_TRUE(all_types_message.ParseFromString(bizarro_data));
332  ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
333
334  // All fields should have been interpreted as unknown, so the debug strings
335  // should be the same.
336  EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
337}
338
339TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknownViaReflection) {
340  // Same as WrongTypeTreatedAsUnknown but via the reflection interface.
341
342  unittest::TestAllTypes all_types_message;
343  unittest::TestEmptyMessage empty_message;
344  string bizarro_data = GetBizarroData();
345  io::ArrayInputStream raw_input(bizarro_data.data(), bizarro_data.size());
346  io::CodedInputStream input(&raw_input);
347  ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &all_types_message));
348  ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
349
350  EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
351}
352
353TEST_F(UnknownFieldSetTest, UnknownExtensions) {
354  // Make sure fields are properly parsed to the UnknownFieldSet even when
355  // they are declared as extension numbers.
356
357  unittest::TestEmptyMessageWithExtensions message;
358  ASSERT_TRUE(message.ParseFromString(all_fields_data_));
359
360  EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
361}
362
363TEST_F(UnknownFieldSetTest, UnknownExtensionsReflection) {
364  // Same as UnknownExtensions except parsing via reflection.
365
366  unittest::TestEmptyMessageWithExtensions message;
367  io::ArrayInputStream raw_input(all_fields_data_.data(),
368                                 all_fields_data_.size());
369  io::CodedInputStream input(&raw_input);
370  ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &message));
371
372  EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
373}
374
375TEST_F(UnknownFieldSetTest, WrongExtensionTypeTreatedAsUnknown) {
376  // Test that fields of the wrong wire type are treated like unknown fields
377  // when parsing extensions.
378
379  unittest::TestAllExtensions all_extensions_message;
380  unittest::TestEmptyMessage empty_message;
381  string bizarro_data = GetBizarroData();
382  ASSERT_TRUE(all_extensions_message.ParseFromString(bizarro_data));
383  ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
384
385  // All fields should have been interpreted as unknown, so the debug strings
386  // should be the same.
387  EXPECT_EQ(empty_message.DebugString(), all_extensions_message.DebugString());
388}
389
390TEST_F(UnknownFieldSetTest, UnknownEnumValue) {
391  using unittest::TestAllTypes;
392  using unittest::TestAllExtensions;
393  using unittest::TestEmptyMessage;
394
395  const FieldDescriptor* singular_field =
396    TestAllTypes::descriptor()->FindFieldByName("optional_nested_enum");
397  const FieldDescriptor* repeated_field =
398    TestAllTypes::descriptor()->FindFieldByName("repeated_nested_enum");
399  ASSERT_TRUE(singular_field != NULL);
400  ASSERT_TRUE(repeated_field != NULL);
401
402  string data;
403
404  {
405    TestEmptyMessage empty_message;
406    UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
407    unknown_fields->AddVarint(singular_field->number(), TestAllTypes::BAR);
408    unknown_fields->AddVarint(singular_field->number(), 5);  // not valid
409    unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::FOO);
410    unknown_fields->AddVarint(repeated_field->number(), 4);  // not valid
411    unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::BAZ);
412    unknown_fields->AddVarint(repeated_field->number(), 6);  // not valid
413    empty_message.SerializeToString(&data);
414  }
415
416  {
417    TestAllTypes message;
418    ASSERT_TRUE(message.ParseFromString(data));
419    EXPECT_EQ(TestAllTypes::BAR, message.optional_nested_enum());
420    ASSERT_EQ(2, message.repeated_nested_enum_size());
421    EXPECT_EQ(TestAllTypes::FOO, message.repeated_nested_enum(0));
422    EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(1));
423
424    const UnknownFieldSet& unknown_fields = message.unknown_fields();
425    ASSERT_EQ(3, unknown_fields.field_count());
426
427    EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
428    ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
429    EXPECT_EQ(5, unknown_fields.field(0).varint());
430
431    EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
432    ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
433    EXPECT_EQ(4, unknown_fields.field(1).varint());
434
435    EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
436    ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
437    EXPECT_EQ(6, unknown_fields.field(2).varint());
438  }
439
440  {
441    using unittest::optional_nested_enum_extension;
442    using unittest::repeated_nested_enum_extension;
443
444    TestAllExtensions message;
445    ASSERT_TRUE(message.ParseFromString(data));
446    EXPECT_EQ(TestAllTypes::BAR,
447              message.GetExtension(optional_nested_enum_extension));
448    ASSERT_EQ(2, message.ExtensionSize(repeated_nested_enum_extension));
449    EXPECT_EQ(TestAllTypes::FOO,
450              message.GetExtension(repeated_nested_enum_extension, 0));
451    EXPECT_EQ(TestAllTypes::BAZ,
452              message.GetExtension(repeated_nested_enum_extension, 1));
453
454    const UnknownFieldSet& unknown_fields = message.unknown_fields();
455    ASSERT_EQ(3, unknown_fields.field_count());
456
457    EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
458    ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
459    EXPECT_EQ(5, unknown_fields.field(0).varint());
460
461    EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
462    ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
463    EXPECT_EQ(4, unknown_fields.field(1).varint());
464
465    EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
466    ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
467    EXPECT_EQ(6, unknown_fields.field(2).varint());
468  }
469}
470
471TEST_F(UnknownFieldSetTest, SpaceUsed) {
472  unittest::TestEmptyMessage empty_message;
473
474  // Make sure an unknown field set has zero space used until a field is
475  // actually added.
476  int base_size = empty_message.SpaceUsed();
477  UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
478  EXPECT_EQ(base_size, empty_message.SpaceUsed());
479
480  // Make sure each thing we add to the set increases the SpaceUsed().
481  unknown_fields->AddVarint(1, 0);
482  EXPECT_LT(base_size, empty_message.SpaceUsed());
483  base_size = empty_message.SpaceUsed();
484
485  string* str = unknown_fields->AddLengthDelimited(1);
486  EXPECT_LT(base_size, empty_message.SpaceUsed());
487  base_size = empty_message.SpaceUsed();
488
489  str->assign(sizeof(string) + 1, 'x');
490  EXPECT_LT(base_size, empty_message.SpaceUsed());
491  base_size = empty_message.SpaceUsed();
492
493  UnknownFieldSet* group = unknown_fields->AddGroup(1);
494  EXPECT_LT(base_size, empty_message.SpaceUsed());
495  base_size = empty_message.SpaceUsed();
496
497  group->AddVarint(1, 0);
498  EXPECT_LT(base_size, empty_message.SpaceUsed());
499}
500
501TEST_F(UnknownFieldSetTest, Empty) {
502  UnknownFieldSet unknown_fields;
503  EXPECT_TRUE(unknown_fields.empty());
504  unknown_fields.AddVarint(6, 123);
505  EXPECT_FALSE(unknown_fields.empty());
506  unknown_fields.Clear();
507  EXPECT_TRUE(unknown_fields.empty());
508}
509
510}  // namespace
511}  // namespace protobuf
512}  // namespace google
513