• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: 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/callback.h>
47 #include <google/protobuf/stubs/common.h>
48 #include <google/protobuf/stubs/logging.h>
49 #include <google/protobuf/stubs/mutex.h>
50 #include <google/protobuf/testing/googletest.h>
51 #include <gtest/gtest.h>
52 #include <google/protobuf/stubs/stl_util.h>
53 
54 namespace google {
55 namespace protobuf {
56 
57 using internal::WireFormat;
58 
59 class UnknownFieldSetTest : public testing::Test {
60  protected:
SetUp()61   virtual void SetUp() {
62     descriptor_ = unittest::TestAllTypes::descriptor();
63     TestUtil::SetAllFields(&all_fields_);
64     all_fields_.SerializeToString(&all_fields_data_);
65     ASSERT_TRUE(empty_message_.ParseFromString(all_fields_data_));
66     unknown_fields_ = empty_message_.mutable_unknown_fields();
67   }
68 
GetField(const string & name)69   const UnknownField* GetField(const string& name) {
70     const FieldDescriptor* field = descriptor_->FindFieldByName(name);
71     if (field == NULL) return NULL;
72     for (int i = 0; i < unknown_fields_->field_count(); i++) {
73       if (unknown_fields_->field(i).number() == field->number()) {
74         return &unknown_fields_->field(i);
75       }
76     }
77     return NULL;
78   }
79 
80   // Constructs a protocol buffer which contains fields with all the same
81   // numbers as all_fields_data_ except that each field is some other wire
82   // type.
GetBizarroData()83   string GetBizarroData() {
84     unittest::TestEmptyMessage bizarro_message;
85     UnknownFieldSet* bizarro_unknown_fields =
86       bizarro_message.mutable_unknown_fields();
87     for (int i = 0; i < unknown_fields_->field_count(); i++) {
88       const UnknownField& unknown_field = unknown_fields_->field(i);
89       if (unknown_field.type() == UnknownField::TYPE_VARINT) {
90         bizarro_unknown_fields->AddFixed32(unknown_field.number(), 1);
91       } else {
92         bizarro_unknown_fields->AddVarint(unknown_field.number(), 1);
93       }
94     }
95 
96     string data;
97     EXPECT_TRUE(bizarro_message.SerializeToString(&data));
98     return data;
99   }
100 
101   const Descriptor* descriptor_;
102   unittest::TestAllTypes all_fields_;
103   string all_fields_data_;
104 
105   // An empty message that has been parsed from all_fields_data_.  So, it has
106   // unknown fields of every type.
107   unittest::TestEmptyMessage empty_message_;
108   UnknownFieldSet* unknown_fields_;
109 };
110 
111 namespace {
112 
TEST_F(UnknownFieldSetTest,AllFieldsPresent)113 TEST_F(UnknownFieldSetTest, AllFieldsPresent) {
114   // All fields of TestAllTypes should be present, in numeric order (because
115   // that's the order we parsed them in).  Fields that are not valid field
116   // numbers of TestAllTypes should NOT be present.
117 
118   int pos = 0;
119 
120   for (int i = 0; i < 1000; i++) {
121     const FieldDescriptor* field = descriptor_->FindFieldByNumber(i);
122     if (field != NULL) {
123       ASSERT_LT(pos, unknown_fields_->field_count());
124       // Do not check oneof field if it is not set.
125       if (field->containing_oneof() == NULL) {
126         EXPECT_EQ(i, unknown_fields_->field(pos++).number());
127       } else if (i == unknown_fields_->field(pos).number()) {
128         pos++;
129       }
130       if (field->is_repeated()) {
131         // Should have a second instance.
132         ASSERT_LT(pos, unknown_fields_->field_count());
133         EXPECT_EQ(i, unknown_fields_->field(pos++).number());
134       }
135     }
136   }
137   EXPECT_EQ(unknown_fields_->field_count(), pos);
138 }
139 
TEST_F(UnknownFieldSetTest,Varint)140 TEST_F(UnknownFieldSetTest, Varint) {
141   const UnknownField* field = GetField("optional_int32");
142   ASSERT_TRUE(field != NULL);
143 
144   ASSERT_EQ(UnknownField::TYPE_VARINT, field->type());
145   EXPECT_EQ(all_fields_.optional_int32(), field->varint());
146 }
147 
TEST_F(UnknownFieldSetTest,Fixed32)148 TEST_F(UnknownFieldSetTest, Fixed32) {
149   const UnknownField* field = GetField("optional_fixed32");
150   ASSERT_TRUE(field != NULL);
151 
152   ASSERT_EQ(UnknownField::TYPE_FIXED32, field->type());
153   EXPECT_EQ(all_fields_.optional_fixed32(), field->fixed32());
154 }
155 
TEST_F(UnknownFieldSetTest,Fixed64)156 TEST_F(UnknownFieldSetTest, Fixed64) {
157   const UnknownField* field = GetField("optional_fixed64");
158   ASSERT_TRUE(field != NULL);
159 
160   ASSERT_EQ(UnknownField::TYPE_FIXED64, field->type());
161   EXPECT_EQ(all_fields_.optional_fixed64(), field->fixed64());
162 }
163 
TEST_F(UnknownFieldSetTest,LengthDelimited)164 TEST_F(UnknownFieldSetTest, LengthDelimited) {
165   const UnknownField* field = GetField("optional_string");
166   ASSERT_TRUE(field != NULL);
167 
168   ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED, field->type());
169   EXPECT_EQ(all_fields_.optional_string(), field->length_delimited());
170 }
171 
TEST_F(UnknownFieldSetTest,Group)172 TEST_F(UnknownFieldSetTest, Group) {
173   const UnknownField* field = GetField("optionalgroup");
174   ASSERT_TRUE(field != NULL);
175 
176   ASSERT_EQ(UnknownField::TYPE_GROUP, field->type());
177   ASSERT_EQ(1, field->group().field_count());
178 
179   const UnknownField& nested_field = field->group().field(0);
180   const FieldDescriptor* nested_field_descriptor =
181     unittest::TestAllTypes::OptionalGroup::descriptor()->FindFieldByName("a");
182   ASSERT_TRUE(nested_field_descriptor != NULL);
183 
184   EXPECT_EQ(nested_field_descriptor->number(), nested_field.number());
185   ASSERT_EQ(UnknownField::TYPE_VARINT, nested_field.type());
186   EXPECT_EQ(all_fields_.optionalgroup().a(), nested_field.varint());
187 }
188 
TEST_F(UnknownFieldSetTest,SerializeFastAndSlowAreEquivalent)189 TEST_F(UnknownFieldSetTest, SerializeFastAndSlowAreEquivalent) {
190   int size = WireFormat::ComputeUnknownFieldsSize(
191       empty_message_.unknown_fields());
192   string slow_buffer;
193   string fast_buffer;
194   slow_buffer.resize(size);
195   fast_buffer.resize(size);
196 
197   uint8* target = reinterpret_cast<uint8*>(string_as_array(&fast_buffer));
198   uint8* result = WireFormat::SerializeUnknownFieldsToArray(
199           empty_message_.unknown_fields(), target);
200   EXPECT_EQ(size, result - target);
201 
202   {
203     io::ArrayOutputStream raw_stream(string_as_array(&slow_buffer), size, 1);
204     io::CodedOutputStream output_stream(&raw_stream);
205     WireFormat::SerializeUnknownFields(empty_message_.unknown_fields(),
206                                        &output_stream);
207     ASSERT_FALSE(output_stream.HadError());
208   }
209   EXPECT_TRUE(fast_buffer == slow_buffer);
210 }
211 
TEST_F(UnknownFieldSetTest,Serialize)212 TEST_F(UnknownFieldSetTest, Serialize) {
213   // Check that serializing the UnknownFieldSet produces the original data
214   // again.
215 
216   string data;
217   empty_message_.SerializeToString(&data);
218 
219   // Don't use EXPECT_EQ because we don't want to dump raw binary data to
220   // stdout.
221   EXPECT_TRUE(data == all_fields_data_);
222 }
223 
TEST_F(UnknownFieldSetTest,ParseViaReflection)224 TEST_F(UnknownFieldSetTest, ParseViaReflection) {
225   // Make sure fields are properly parsed to the UnknownFieldSet when parsing
226   // via reflection.
227 
228   unittest::TestEmptyMessage message;
229   io::ArrayInputStream raw_input(all_fields_data_.data(),
230                                  all_fields_data_.size());
231   io::CodedInputStream input(&raw_input);
232   ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &message));
233 
234   EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
235 }
236 
TEST_F(UnknownFieldSetTest,SerializeViaReflection)237 TEST_F(UnknownFieldSetTest, SerializeViaReflection) {
238   // Make sure fields are properly written from the UnknownFieldSet when
239   // serializing via reflection.
240 
241   string data;
242 
243   {
244     io::StringOutputStream raw_output(&data);
245     io::CodedOutputStream output(&raw_output);
246     int size = WireFormat::ByteSize(empty_message_);
247     WireFormat::SerializeWithCachedSizes(empty_message_, size, &output);
248     ASSERT_FALSE(output.HadError());
249   }
250 
251   // Don't use EXPECT_EQ because we don't want to dump raw binary data to
252   // stdout.
253   EXPECT_TRUE(data == all_fields_data_);
254 }
255 
TEST_F(UnknownFieldSetTest,CopyFrom)256 TEST_F(UnknownFieldSetTest, CopyFrom) {
257   unittest::TestEmptyMessage message;
258 
259   message.CopyFrom(empty_message_);
260 
261   EXPECT_EQ(empty_message_.DebugString(), message.DebugString());
262 }
263 
TEST_F(UnknownFieldSetTest,Swap)264 TEST_F(UnknownFieldSetTest, Swap) {
265   unittest::TestEmptyMessage other_message;
266   ASSERT_TRUE(other_message.ParseFromString(GetBizarroData()));
267 
268   EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
269   EXPECT_GT(other_message.unknown_fields().field_count(), 0);
270   const string debug_string = empty_message_.DebugString();
271   const string other_debug_string = other_message.DebugString();
272   EXPECT_NE(debug_string, other_debug_string);
273 
274   empty_message_.Swap(&other_message);
275   EXPECT_EQ(debug_string, other_message.DebugString());
276   EXPECT_EQ(other_debug_string, empty_message_.DebugString());
277 }
278 
TEST_F(UnknownFieldSetTest,SwapWithSelf)279 TEST_F(UnknownFieldSetTest, SwapWithSelf) {
280   const string debug_string = empty_message_.DebugString();
281   EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
282 
283   empty_message_.Swap(&empty_message_);
284   EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
285   EXPECT_EQ(debug_string, empty_message_.DebugString());
286 }
287 
TEST_F(UnknownFieldSetTest,MergeFrom)288 TEST_F(UnknownFieldSetTest, MergeFrom) {
289   unittest::TestEmptyMessage source, destination;
290 
291   destination.mutable_unknown_fields()->AddVarint(1, 1);
292   destination.mutable_unknown_fields()->AddVarint(3, 2);
293   source.mutable_unknown_fields()->AddVarint(2, 3);
294   source.mutable_unknown_fields()->AddVarint(3, 4);
295 
296   destination.MergeFrom(source);
297 
298   EXPECT_EQ(
299     // Note:  The ordering of fields here depends on the ordering of adds
300     //   and merging, above.
301     "1: 1\n"
302     "3: 2\n"
303     "2: 3\n"
304     "3: 4\n",
305     destination.DebugString());
306 }
307 
308 
TEST_F(UnknownFieldSetTest,Clear)309 TEST_F(UnknownFieldSetTest, Clear) {
310   // Clear the set.
311   empty_message_.Clear();
312   EXPECT_EQ(0, unknown_fields_->field_count());
313 }
314 
TEST_F(UnknownFieldSetTest,ClearAndFreeMemory)315 TEST_F(UnknownFieldSetTest, ClearAndFreeMemory) {
316   EXPECT_GT(unknown_fields_->field_count(), 0);
317   unknown_fields_->ClearAndFreeMemory();
318   EXPECT_EQ(0, unknown_fields_->field_count());
319   unknown_fields_->AddVarint(123456, 654321);
320   EXPECT_EQ(1, unknown_fields_->field_count());
321 }
322 
TEST_F(UnknownFieldSetTest,ParseKnownAndUnknown)323 TEST_F(UnknownFieldSetTest, ParseKnownAndUnknown) {
324   // Test mixing known and unknown fields when parsing.
325 
326   unittest::TestEmptyMessage source;
327   source.mutable_unknown_fields()->AddVarint(123456, 654321);
328   string data;
329   ASSERT_TRUE(source.SerializeToString(&data));
330 
331   unittest::TestAllTypes destination;
332   ASSERT_TRUE(destination.ParseFromString(all_fields_data_ + data));
333 
334   TestUtil::ExpectAllFieldsSet(destination);
335   ASSERT_EQ(1, destination.unknown_fields().field_count());
336   ASSERT_EQ(UnknownField::TYPE_VARINT,
337             destination.unknown_fields().field(0).type());
338   EXPECT_EQ(654321, destination.unknown_fields().field(0).varint());
339 }
340 
TEST_F(UnknownFieldSetTest,WrongTypeTreatedAsUnknown)341 TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknown) {
342   // Test that fields of the wrong wire type are treated like unknown fields
343   // when parsing.
344 
345   unittest::TestAllTypes all_types_message;
346   unittest::TestEmptyMessage empty_message;
347   string bizarro_data = GetBizarroData();
348   ASSERT_TRUE(all_types_message.ParseFromString(bizarro_data));
349   ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
350 
351   // All fields should have been interpreted as unknown, so the debug strings
352   // should be the same.
353   EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
354 }
355 
TEST_F(UnknownFieldSetTest,WrongTypeTreatedAsUnknownViaReflection)356 TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknownViaReflection) {
357   // Same as WrongTypeTreatedAsUnknown but via the reflection interface.
358 
359   unittest::TestAllTypes all_types_message;
360   unittest::TestEmptyMessage empty_message;
361   string bizarro_data = GetBizarroData();
362   io::ArrayInputStream raw_input(bizarro_data.data(), bizarro_data.size());
363   io::CodedInputStream input(&raw_input);
364   ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &all_types_message));
365   ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
366 
367   EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
368 }
369 
TEST_F(UnknownFieldSetTest,UnknownExtensions)370 TEST_F(UnknownFieldSetTest, UnknownExtensions) {
371   // Make sure fields are properly parsed to the UnknownFieldSet even when
372   // they are declared as extension numbers.
373 
374   unittest::TestEmptyMessageWithExtensions message;
375   ASSERT_TRUE(message.ParseFromString(all_fields_data_));
376 
377   EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
378 }
379 
TEST_F(UnknownFieldSetTest,UnknownExtensionsReflection)380 TEST_F(UnknownFieldSetTest, UnknownExtensionsReflection) {
381   // Same as UnknownExtensions except parsing via reflection.
382 
383   unittest::TestEmptyMessageWithExtensions message;
384   io::ArrayInputStream raw_input(all_fields_data_.data(),
385                                  all_fields_data_.size());
386   io::CodedInputStream input(&raw_input);
387   ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &message));
388 
389   EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
390 }
391 
TEST_F(UnknownFieldSetTest,WrongExtensionTypeTreatedAsUnknown)392 TEST_F(UnknownFieldSetTest, WrongExtensionTypeTreatedAsUnknown) {
393   // Test that fields of the wrong wire type are treated like unknown fields
394   // when parsing extensions.
395 
396   unittest::TestAllExtensions all_extensions_message;
397   unittest::TestEmptyMessage empty_message;
398   string bizarro_data = GetBizarroData();
399   ASSERT_TRUE(all_extensions_message.ParseFromString(bizarro_data));
400   ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
401 
402   // All fields should have been interpreted as unknown, so the debug strings
403   // should be the same.
404   EXPECT_EQ(empty_message.DebugString(), all_extensions_message.DebugString());
405 }
406 
TEST_F(UnknownFieldSetTest,UnknownEnumValue)407 TEST_F(UnknownFieldSetTest, UnknownEnumValue) {
408   using unittest::TestAllTypes;
409   using unittest::TestAllExtensions;
410   using unittest::TestEmptyMessage;
411 
412   const FieldDescriptor* singular_field =
413     TestAllTypes::descriptor()->FindFieldByName("optional_nested_enum");
414   const FieldDescriptor* repeated_field =
415     TestAllTypes::descriptor()->FindFieldByName("repeated_nested_enum");
416   ASSERT_TRUE(singular_field != NULL);
417   ASSERT_TRUE(repeated_field != NULL);
418 
419   string data;
420 
421   {
422     TestEmptyMessage empty_message;
423     UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
424     unknown_fields->AddVarint(singular_field->number(), TestAllTypes::BAR);
425     unknown_fields->AddVarint(singular_field->number(), 5);  // not valid
426     unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::FOO);
427     unknown_fields->AddVarint(repeated_field->number(), 4);  // not valid
428     unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::BAZ);
429     unknown_fields->AddVarint(repeated_field->number(), 6);  // not valid
430     empty_message.SerializeToString(&data);
431   }
432 
433   {
434     TestAllTypes message;
435     ASSERT_TRUE(message.ParseFromString(data));
436     EXPECT_EQ(TestAllTypes::BAR, message.optional_nested_enum());
437     ASSERT_EQ(2, message.repeated_nested_enum_size());
438     EXPECT_EQ(TestAllTypes::FOO, message.repeated_nested_enum(0));
439     EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(1));
440 
441     const UnknownFieldSet& unknown_fields = message.unknown_fields();
442     ASSERT_EQ(3, unknown_fields.field_count());
443 
444     EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
445     ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
446     EXPECT_EQ(5, unknown_fields.field(0).varint());
447 
448     EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
449     ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
450     EXPECT_EQ(4, unknown_fields.field(1).varint());
451 
452     EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
453     ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
454     EXPECT_EQ(6, unknown_fields.field(2).varint());
455   }
456 
457   {
458     using unittest::optional_nested_enum_extension;
459     using unittest::repeated_nested_enum_extension;
460 
461     TestAllExtensions message;
462     ASSERT_TRUE(message.ParseFromString(data));
463     EXPECT_EQ(TestAllTypes::BAR,
464               message.GetExtension(optional_nested_enum_extension));
465     ASSERT_EQ(2, message.ExtensionSize(repeated_nested_enum_extension));
466     EXPECT_EQ(TestAllTypes::FOO,
467               message.GetExtension(repeated_nested_enum_extension, 0));
468     EXPECT_EQ(TestAllTypes::BAZ,
469               message.GetExtension(repeated_nested_enum_extension, 1));
470 
471     const UnknownFieldSet& unknown_fields = message.unknown_fields();
472     ASSERT_EQ(3, unknown_fields.field_count());
473 
474     EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
475     ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
476     EXPECT_EQ(5, unknown_fields.field(0).varint());
477 
478     EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
479     ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
480     EXPECT_EQ(4, unknown_fields.field(1).varint());
481 
482     EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
483     ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
484     EXPECT_EQ(6, unknown_fields.field(2).varint());
485   }
486 }
487 
TEST_F(UnknownFieldSetTest,SpaceUsedExcludingSelf)488 TEST_F(UnknownFieldSetTest, SpaceUsedExcludingSelf) {
489   UnknownFieldSet empty;
490   empty.AddVarint(1, 0);
491   EXPECT_EQ(sizeof(vector<UnknownField>) + sizeof(UnknownField),
492             empty.SpaceUsedExcludingSelf());
493 }
494 
TEST_F(UnknownFieldSetTest,SpaceUsed)495 TEST_F(UnknownFieldSetTest, SpaceUsed) {
496   unittest::TestEmptyMessage empty_message;
497 
498   // Make sure an unknown field set has zero space used until a field is
499   // actually added.
500   int base_size = empty_message.SpaceUsed();
501   UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
502   EXPECT_EQ(base_size, empty_message.SpaceUsed());
503 
504   // Make sure each thing we add to the set increases the SpaceUsed().
505   unknown_fields->AddVarint(1, 0);
506   EXPECT_LT(base_size, empty_message.SpaceUsed());
507   base_size = empty_message.SpaceUsed();
508 
509   string* str = unknown_fields->AddLengthDelimited(1);
510   EXPECT_LT(base_size, empty_message.SpaceUsed());
511   base_size = empty_message.SpaceUsed();
512 
513   str->assign(sizeof(string) + 1, 'x');
514   EXPECT_LT(base_size, empty_message.SpaceUsed());
515   base_size = empty_message.SpaceUsed();
516 
517   UnknownFieldSet* group = unknown_fields->AddGroup(1);
518   EXPECT_LT(base_size, empty_message.SpaceUsed());
519   base_size = empty_message.SpaceUsed();
520 
521   group->AddVarint(1, 0);
522   EXPECT_LT(base_size, empty_message.SpaceUsed());
523 }
524 
525 
TEST_F(UnknownFieldSetTest,Empty)526 TEST_F(UnknownFieldSetTest, Empty) {
527   UnknownFieldSet unknown_fields;
528   EXPECT_TRUE(unknown_fields.empty());
529   unknown_fields.AddVarint(6, 123);
530   EXPECT_FALSE(unknown_fields.empty());
531   unknown_fields.Clear();
532   EXPECT_TRUE(unknown_fields.empty());
533 }
534 
TEST_F(UnknownFieldSetTest,DeleteSubrange)535 TEST_F(UnknownFieldSetTest, DeleteSubrange) {
536   // Exhaustively test the deletion of every possible subrange in arrays of all
537   // sizes from 0 through 9.
538   for (int size = 0; size < 10; ++size) {
539     for (int num = 0; num <= size; ++num) {
540       for (int start = 0; start < size - num; ++start) {
541         // Create a set with "size" fields.
542         UnknownFieldSet unknown;
543         for (int i = 0; i < size; ++i) {
544           unknown.AddFixed32(i, i);
545         }
546         // Delete the specified subrange.
547         unknown.DeleteSubrange(start, num);
548         // Make sure the resulting field values are still correct.
549         EXPECT_EQ(size - num, unknown.field_count());
550         for (int i = 0; i < unknown.field_count(); ++i) {
551           if (i < start) {
552             EXPECT_EQ(i, unknown.field(i).fixed32());
553           } else {
554             EXPECT_EQ(i + num, unknown.field(i).fixed32());
555           }
556         }
557       }
558     }
559   }
560 }
561 
CheckDeleteByNumber(const vector<int> & field_numbers,int deleted_number,const vector<int> & expected_field_nubmers)562 void CheckDeleteByNumber(const vector<int>& field_numbers, int deleted_number,
563                         const vector<int>& expected_field_nubmers) {
564   UnknownFieldSet unknown_fields;
565   for (int i = 0; i < field_numbers.size(); ++i) {
566     unknown_fields.AddFixed32(field_numbers[i], i);
567   }
568   unknown_fields.DeleteByNumber(deleted_number);
569   ASSERT_EQ(expected_field_nubmers.size(), unknown_fields.field_count());
570   for (int i = 0; i < expected_field_nubmers.size(); ++i) {
571     EXPECT_EQ(expected_field_nubmers[i],
572               unknown_fields.field(i).number());
573   }
574 }
575 
576 #define MAKE_VECTOR(x) vector<int>(x, x + GOOGLE_ARRAYSIZE(x))
TEST_F(UnknownFieldSetTest,DeleteByNumber)577 TEST_F(UnknownFieldSetTest, DeleteByNumber) {
578   CheckDeleteByNumber(vector<int>(), 1, vector<int>());
579   static const int kTestFieldNumbers1[] = {1, 2, 3};
580   static const int kFieldNumberToDelete1 = 1;
581   static const int kExpectedFieldNumbers1[] = {2, 3};
582   CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers1), kFieldNumberToDelete1,
583                       MAKE_VECTOR(kExpectedFieldNumbers1));
584   static const int kTestFieldNumbers2[] = {1, 2, 3};
585   static const int kFieldNumberToDelete2 = 2;
586   static const int kExpectedFieldNumbers2[] = {1, 3};
587   CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers2), kFieldNumberToDelete2,
588                       MAKE_VECTOR(kExpectedFieldNumbers2));
589   static const int kTestFieldNumbers3[] = {1, 2, 3};
590   static const int kFieldNumberToDelete3 = 3;
591   static const int kExpectedFieldNumbers3[] = {1, 2};
592   CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers3), kFieldNumberToDelete3,
593                       MAKE_VECTOR(kExpectedFieldNumbers3));
594   static const int kTestFieldNumbers4[] = {1, 2, 1, 4, 1};
595   static const int kFieldNumberToDelete4 = 1;
596   static const int kExpectedFieldNumbers4[] = {2, 4};
597   CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers4), kFieldNumberToDelete4,
598                       MAKE_VECTOR(kExpectedFieldNumbers4));
599   static const int kTestFieldNumbers5[] = {1, 2, 3, 4, 5};
600   static const int kFieldNumberToDelete5 = 6;
601   static const int kExpectedFieldNumbers5[] = {1, 2, 3, 4, 5};
602   CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers5), kFieldNumberToDelete5,
603                       MAKE_VECTOR(kExpectedFieldNumbers5));
604 }
605 #undef MAKE_VECTOR
606 }  // namespace
607 
608 }  // namespace protobuf
609 }  // namespace google
610