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