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