protobuf/src/google/protobuf/unknown_field_set_unittest.cc
Go to the documentation of this file.
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 
40 #include <unordered_set>
41 
42 #include <google/protobuf/stubs/callback.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/stubs/logging.h>
45 #include <google/protobuf/test_util.h>
46 #include <google/protobuf/unittest.pb.h>
47 #include <google/protobuf/unittest_lite.pb.h>
48 #include <google/protobuf/io/coded_stream.h>
49 #include <google/protobuf/io/zero_copy_stream_impl.h>
50 #include <google/protobuf/descriptor.h>
51 #include <google/protobuf/stubs/mutex.h>
52 #include <google/protobuf/wire_format.h>
53 #include <google/protobuf/testing/googletest.h>
54 #include <gtest/gtest.h>
55 #include <google/protobuf/stubs/time.h>
56 #include <google/protobuf/stubs/stl_util.h>
57 
58 namespace google {
59 namespace protobuf {
60 
61 using internal::WireFormat;
62 
63 class UnknownFieldSetTest : public testing::Test {
64  protected:
65  virtual void SetUp() {
68  all_fields_.SerializeToString(&all_fields_data_);
70  unknown_fields_ = empty_message_.mutable_unknown_fields();
71  }
72 
75  if (field == nullptr) return nullptr;
76  for (int i = 0; i < unknown_fields_->field_count(); i++) {
77  if (unknown_fields_->field(i).number() == field->number()) {
78  return &unknown_fields_->field(i);
79  }
80  }
81  return nullptr;
82  }
83 
84  // Constructs a protocol buffer which contains fields with all the same
85  // numbers as all_fields_data_ except that each field is some other wire
86  // type.
88  unittest::TestEmptyMessage bizarro_message;
89  UnknownFieldSet* bizarro_unknown_fields =
90  bizarro_message.mutable_unknown_fields();
91  for (int i = 0; i < unknown_fields_->field_count(); i++) {
92  const UnknownField& unknown_field = unknown_fields_->field(i);
93  if (unknown_field.type() == UnknownField::TYPE_VARINT) {
94  bizarro_unknown_fields->AddFixed32(unknown_field.number(), 1);
95  } else {
96  bizarro_unknown_fields->AddVarint(unknown_field.number(), 1);
97  }
98  }
99 
101  EXPECT_TRUE(bizarro_message.SerializeToString(&data));
102  return data;
103  }
104 
105  const Descriptor* descriptor_;
106  unittest::TestAllTypes all_fields_;
108 
109  // An empty message that has been parsed from all_fields_data_. So, it has
110  // unknown fields of every type.
111  unittest::TestEmptyMessage empty_message_;
113 };
114 
115 namespace {
116 
117 TEST_F(UnknownFieldSetTest, AllFieldsPresent) {
118  // Verifies the following:
119  // --all unknown tags belong to TestAllTypes.
120  // --all fields in TestAllTypes is present in UnknownFieldSet except unset
121  // oneof fields.
122  //
123  // Should handle repeated fields that may appear multiple times in
124  // UnknownFieldSet.
125 
126  int non_oneof_count = 0;
127  for (int i = 0; i < descriptor_->field_count(); i++) {
128  if (!descriptor_->field(i)->containing_oneof()) {
129  non_oneof_count++;
130  }
131  }
132 
133  std::unordered_set<uint32> unknown_tags;
134  for (int i = 0; i < unknown_fields_->field_count(); i++) {
135  unknown_tags.insert(unknown_fields_->field(i).number());
136  }
137 
138  for (uint32 t : unknown_tags) {
139  EXPECT_NE(descriptor_->FindFieldByNumber(t), nullptr);
140  }
141 
142  EXPECT_EQ(non_oneof_count + descriptor_->oneof_decl_count(),
143  unknown_tags.size());
144 }
145 
146 TEST_F(UnknownFieldSetTest, Varint) {
147  const UnknownField* field = GetField("optional_int32");
148  ASSERT_TRUE(field != nullptr);
149 
151  EXPECT_EQ(all_fields_.optional_int32(), field->varint());
152 }
153 
154 TEST_F(UnknownFieldSetTest, Fixed32) {
155  const UnknownField* field = GetField("optional_fixed32");
156  ASSERT_TRUE(field != nullptr);
157 
159  EXPECT_EQ(all_fields_.optional_fixed32(), field->fixed32());
160 }
161 
162 TEST_F(UnknownFieldSetTest, Fixed64) {
163  const UnknownField* field = GetField("optional_fixed64");
164  ASSERT_TRUE(field != nullptr);
165 
167  EXPECT_EQ(all_fields_.optional_fixed64(), field->fixed64());
168 }
169 
170 TEST_F(UnknownFieldSetTest, LengthDelimited) {
171  const UnknownField* field = GetField("optional_string");
172  ASSERT_TRUE(field != nullptr);
173 
175  EXPECT_EQ(all_fields_.optional_string(), field->length_delimited());
176 }
177 
178 TEST_F(UnknownFieldSetTest, Group) {
179  const UnknownField* field = GetField("optionalgroup");
180  ASSERT_TRUE(field != nullptr);
181 
183  ASSERT_EQ(1, field->group().field_count());
184 
185  const UnknownField& nested_field = field->group().field(0);
186  const FieldDescriptor* nested_field_descriptor =
188  ASSERT_TRUE(nested_field_descriptor != nullptr);
189 
190  EXPECT_EQ(nested_field_descriptor->number(), nested_field.number());
191  ASSERT_EQ(UnknownField::TYPE_VARINT, nested_field.type());
192  EXPECT_EQ(all_fields_.optionalgroup().a(), nested_field.varint());
193 }
194 
195 TEST_F(UnknownFieldSetTest, SerializeFastAndSlowAreEquivalent) {
196  int size =
197  WireFormat::ComputeUnknownFieldsSize(empty_message_.unknown_fields());
198  std::string slow_buffer;
199  std::string fast_buffer;
200  slow_buffer.resize(size);
201  fast_buffer.resize(size);
202 
203  uint8* target = reinterpret_cast<uint8*>(::google::protobuf::string_as_array(&fast_buffer));
205  empty_message_.unknown_fields(), target);
207 
208  {
209  io::ArrayOutputStream raw_stream(::google::protobuf::string_as_array(&slow_buffer), size,
210  1);
211  io::CodedOutputStream output_stream(&raw_stream);
212  WireFormat::SerializeUnknownFields(empty_message_.unknown_fields(),
213  &output_stream);
214  ASSERT_FALSE(output_stream.HadError());
215  }
216  EXPECT_TRUE(fast_buffer == slow_buffer);
217 }
218 
219 TEST_F(UnknownFieldSetTest, Serialize) {
220  // Check that serializing the UnknownFieldSet produces the original data
221  // again.
222 
224  empty_message_.SerializeToString(&data);
225 
226  // Don't use EXPECT_EQ because we don't want to dump raw binary data to
227  // stdout.
228  EXPECT_TRUE(data == all_fields_data_);
229 }
230 
231 TEST_F(UnknownFieldSetTest, ParseViaReflection) {
232  // Make sure fields are properly parsed to the UnknownFieldSet when parsing
233  // via reflection.
234 
235  unittest::TestEmptyMessage message;
236  io::ArrayInputStream raw_input(all_fields_data_.data(),
237  all_fields_data_.size());
240 
241  EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
242 }
243 
244 TEST_F(UnknownFieldSetTest, SerializeViaReflection) {
245  // Make sure fields are properly written from the UnknownFieldSet when
246  // serializing via reflection.
247 
249 
250  {
251  io::StringOutputStream raw_output(&data);
252  io::CodedOutputStream output(&raw_output);
253  size_t size = WireFormat::ByteSize(empty_message_);
255  ASSERT_FALSE(output.HadError());
256  }
257 
258  // Don't use EXPECT_EQ because we don't want to dump raw binary data to
259  // stdout.
260  EXPECT_TRUE(data == all_fields_data_);
261 }
262 
263 TEST_F(UnknownFieldSetTest, CopyFrom) {
264  unittest::TestEmptyMessage message;
265 
266  message.CopyFrom(empty_message_);
267 
268  EXPECT_EQ(empty_message_.DebugString(), message.DebugString());
269 }
270 
271 TEST_F(UnknownFieldSetTest, Swap) {
272  unittest::TestEmptyMessage other_message;
273  ASSERT_TRUE(other_message.ParseFromString(GetBizarroData()));
274 
275  EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
276  EXPECT_GT(other_message.unknown_fields().field_count(), 0);
277  const std::string debug_string = empty_message_.DebugString();
278  const std::string other_debug_string = other_message.DebugString();
279  EXPECT_NE(debug_string, other_debug_string);
280 
281  empty_message_.Swap(&other_message);
282  EXPECT_EQ(debug_string, other_message.DebugString());
283  EXPECT_EQ(other_debug_string, empty_message_.DebugString());
284 }
285 
286 TEST_F(UnknownFieldSetTest, SwapWithSelf) {
287  const std::string debug_string = empty_message_.DebugString();
288  EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
289 
290  empty_message_.Swap(&empty_message_);
291  EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
292  EXPECT_EQ(debug_string, empty_message_.DebugString());
293 }
294 
295 TEST_F(UnknownFieldSetTest, MergeFrom) {
296  unittest::TestEmptyMessage source, destination;
297 
298  destination.mutable_unknown_fields()->AddVarint(1, 1);
299  destination.mutable_unknown_fields()->AddVarint(3, 2);
300  source.mutable_unknown_fields()->AddVarint(2, 3);
301  source.mutable_unknown_fields()->AddVarint(3, 4);
302 
303  destination.MergeFrom(source);
304 
305  EXPECT_EQ(
306  // Note: The ordering of fields here depends on the ordering of adds
307  // and merging, above.
308  "1: 1\n"
309  "3: 2\n"
310  "2: 3\n"
311  "3: 4\n",
312  destination.DebugString());
313 }
314 
315 TEST_F(UnknownFieldSetTest, MergeFromMessage) {
316  unittest::TestEmptyMessage source, destination;
317 
318  destination.mutable_unknown_fields()->AddVarint(1, 1);
319  destination.mutable_unknown_fields()->AddVarint(3, 2);
320  source.mutable_unknown_fields()->AddVarint(2, 3);
321  source.mutable_unknown_fields()->AddVarint(3, 4);
322 
323  destination.mutable_unknown_fields()->MergeFromMessage(source);
324 
325  EXPECT_EQ(
326  // Note: The ordering of fields here depends on the ordering of adds
327  // and merging, above.
328  "1: 1\n"
329  "3: 2\n"
330  "2: 3\n"
331  "3: 4\n",
332  destination.DebugString());
333 }
334 
335 TEST_F(UnknownFieldSetTest, MergeFromMessageLite) {
336  unittest::TestAllTypesLite source;
337  unittest::TestEmptyMessageLite destination;
338 
339  source.set_optional_fixed32(42);
340  destination.ParseFromString(source.SerializeAsString());
341 
342  UnknownFieldSet unknown_field_set;
343  EXPECT_TRUE(unknown_field_set.MergeFromMessage(destination));
344  EXPECT_EQ(unknown_field_set.field_count(), 1);
345 
346  const UnknownField& unknown_field = unknown_field_set.field(0);
347  EXPECT_EQ(unknown_field.number(), 7);
348  EXPECT_EQ(unknown_field.fixed32(), 42);
349 }
350 
351 
352 TEST_F(UnknownFieldSetTest, Clear) {
353  // Clear the set.
354  empty_message_.Clear();
355  EXPECT_EQ(0, unknown_fields_->field_count());
356 }
357 
358 TEST_F(UnknownFieldSetTest, ClearAndFreeMemory) {
359  EXPECT_GT(unknown_fields_->field_count(), 0);
360  unknown_fields_->ClearAndFreeMemory();
361  EXPECT_EQ(0, unknown_fields_->field_count());
362  unknown_fields_->AddVarint(123456, 654321);
363  EXPECT_EQ(1, unknown_fields_->field_count());
364 }
365 
366 TEST_F(UnknownFieldSetTest, ParseKnownAndUnknown) {
367  // Test mixing known and unknown fields when parsing.
368 
369  unittest::TestEmptyMessage source;
370  source.mutable_unknown_fields()->AddVarint(123456, 654321);
372  ASSERT_TRUE(source.SerializeToString(&data));
373 
374  unittest::TestAllTypes destination;
375  ASSERT_TRUE(destination.ParseFromString(all_fields_data_ + data));
376 
377  TestUtil::ExpectAllFieldsSet(destination);
378  ASSERT_EQ(1, destination.unknown_fields().field_count());
380  destination.unknown_fields().field(0).type());
381  EXPECT_EQ(654321, destination.unknown_fields().field(0).varint());
382 }
383 
384 TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknown) {
385  // Test that fields of the wrong wire type are treated like unknown fields
386  // when parsing.
387 
388  unittest::TestAllTypes all_types_message;
389  unittest::TestEmptyMessage empty_message;
390  std::string bizarro_data = GetBizarroData();
391  ASSERT_TRUE(all_types_message.ParseFromString(bizarro_data));
392  ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
393 
394  // All fields should have been interpreted as unknown, so the debug strings
395  // should be the same.
396  EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
397 }
398 
399 TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknownViaReflection) {
400  // Same as WrongTypeTreatedAsUnknown but via the reflection interface.
401 
402  unittest::TestAllTypes all_types_message;
403  unittest::TestEmptyMessage empty_message;
404  std::string bizarro_data = GetBizarroData();
405  io::ArrayInputStream raw_input(bizarro_data.data(), bizarro_data.size());
407  ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &all_types_message));
408  ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
409 
410  EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
411 }
412 
413 TEST_F(UnknownFieldSetTest, UnknownExtensions) {
414  // Make sure fields are properly parsed to the UnknownFieldSet even when
415  // they are declared as extension numbers.
416 
417  unittest::TestEmptyMessageWithExtensions message;
418  ASSERT_TRUE(message.ParseFromString(all_fields_data_));
419 
420  EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
421 }
422 
423 TEST_F(UnknownFieldSetTest, UnknownExtensionsReflection) {
424  // Same as UnknownExtensions except parsing via reflection.
425 
426  unittest::TestEmptyMessageWithExtensions message;
427  io::ArrayInputStream raw_input(all_fields_data_.data(),
428  all_fields_data_.size());
431 
432  EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
433 }
434 
435 TEST_F(UnknownFieldSetTest, WrongExtensionTypeTreatedAsUnknown) {
436  // Test that fields of the wrong wire type are treated like unknown fields
437  // when parsing extensions.
438 
439  unittest::TestAllExtensions all_extensions_message;
440  unittest::TestEmptyMessage empty_message;
441  std::string bizarro_data = GetBizarroData();
442  ASSERT_TRUE(all_extensions_message.ParseFromString(bizarro_data));
443  ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
444 
445  // All fields should have been interpreted as unknown, so the debug strings
446  // should be the same.
447  EXPECT_EQ(empty_message.DebugString(), all_extensions_message.DebugString());
448 }
449 
450 TEST_F(UnknownFieldSetTest, UnknownEnumValue) {
451  using unittest::TestAllExtensions;
452  using unittest::TestAllTypes;
453  using unittest::TestEmptyMessage;
454 
455  const FieldDescriptor* singular_field =
456  TestAllTypes::descriptor()->FindFieldByName("optional_nested_enum");
458  TestAllTypes::descriptor()->FindFieldByName("repeated_nested_enum");
459  ASSERT_TRUE(singular_field != nullptr);
460  ASSERT_TRUE(repeated_field != nullptr);
461 
463 
464  {
465  TestEmptyMessage empty_message;
466  UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
467  unknown_fields->AddVarint(singular_field->number(), TestAllTypes::BAR);
468  unknown_fields->AddVarint(singular_field->number(), 5); // not valid
469  unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::FOO);
470  unknown_fields->AddVarint(repeated_field->number(), 4); // not valid
471  unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::BAZ);
472  unknown_fields->AddVarint(repeated_field->number(), 6); // not valid
473  empty_message.SerializeToString(&data);
474  }
475 
476  {
477  TestAllTypes message;
478  ASSERT_TRUE(message.ParseFromString(data));
479  EXPECT_EQ(TestAllTypes::BAR, message.optional_nested_enum());
480  ASSERT_EQ(2, message.repeated_nested_enum_size());
481  EXPECT_EQ(TestAllTypes::FOO, message.repeated_nested_enum(0));
482  EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(1));
483 
484  const UnknownFieldSet& unknown_fields = message.unknown_fields();
485  ASSERT_EQ(3, unknown_fields.field_count());
486 
487  EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
488  ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
489  EXPECT_EQ(5, unknown_fields.field(0).varint());
490 
491  EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
492  ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
493  EXPECT_EQ(4, unknown_fields.field(1).varint());
494 
495  EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
496  ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
497  EXPECT_EQ(6, unknown_fields.field(2).varint());
498  }
499 
500  {
501  using unittest::optional_nested_enum_extension;
502  using unittest::repeated_nested_enum_extension;
503 
504  TestAllExtensions message;
505  ASSERT_TRUE(message.ParseFromString(data));
506  EXPECT_EQ(TestAllTypes::BAR,
507  message.GetExtension(optional_nested_enum_extension));
508  ASSERT_EQ(2, message.ExtensionSize(repeated_nested_enum_extension));
509  EXPECT_EQ(TestAllTypes::FOO,
510  message.GetExtension(repeated_nested_enum_extension, 0));
511  EXPECT_EQ(TestAllTypes::BAZ,
512  message.GetExtension(repeated_nested_enum_extension, 1));
513 
514  const UnknownFieldSet& unknown_fields = message.unknown_fields();
515  ASSERT_EQ(3, unknown_fields.field_count());
516 
517  EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
518  ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
519  EXPECT_EQ(5, unknown_fields.field(0).varint());
520 
521  EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
522  ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
523  EXPECT_EQ(4, unknown_fields.field(1).varint());
524 
525  EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
526  ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
527  EXPECT_EQ(6, unknown_fields.field(2).varint());
528  }
529 }
530 
531 TEST_F(UnknownFieldSetTest, SpaceUsedExcludingSelf) {
533  empty.AddVarint(1, 0);
534  EXPECT_EQ(sizeof(std::vector<UnknownField>) + sizeof(UnknownField),
535  empty.SpaceUsedExcludingSelf());
536 }
537 
538 TEST_F(UnknownFieldSetTest, SpaceUsed) {
539  unittest::TestEmptyMessage empty_message;
540 
541  // Make sure an unknown field set has zero space used until a field is
542  // actually added.
543  size_t base_size = empty_message.SpaceUsedLong();
544  UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
545  EXPECT_EQ(base_size, empty_message.SpaceUsedLong());
546 
547  // Make sure each thing we add to the set increases the SpaceUsedLong().
548  unknown_fields->AddVarint(1, 0);
549  EXPECT_LT(base_size, empty_message.SpaceUsedLong());
550  base_size = empty_message.SpaceUsedLong();
551 
552  std::string* str = unknown_fields->AddLengthDelimited(1);
553  EXPECT_LT(base_size, empty_message.SpaceUsedLong());
554  base_size = empty_message.SpaceUsedLong();
555 
556  str->assign(sizeof(std::string) + 1, 'x');
557  EXPECT_LT(base_size, empty_message.SpaceUsedLong());
558  base_size = empty_message.SpaceUsedLong();
559 
560  UnknownFieldSet* group = unknown_fields->AddGroup(1);
561  EXPECT_LT(base_size, empty_message.SpaceUsedLong());
562  base_size = empty_message.SpaceUsedLong();
563 
564  group->AddVarint(1, 0);
565  EXPECT_LT(base_size, empty_message.SpaceUsedLong());
566 }
567 
568 
569 TEST_F(UnknownFieldSetTest, Empty) {
570  UnknownFieldSet unknown_fields;
571  EXPECT_TRUE(unknown_fields.empty());
572  unknown_fields.AddVarint(6, 123);
573  EXPECT_FALSE(unknown_fields.empty());
574  unknown_fields.Clear();
575  EXPECT_TRUE(unknown_fields.empty());
576 }
577 
578 TEST_F(UnknownFieldSetTest, DeleteSubrange) {
579  // Exhaustively test the deletion of every possible subrange in arrays of all
580  // sizes from 0 through 9.
581  for (int size = 0; size < 10; ++size) {
582  for (int num = 0; num <= size; ++num) {
583  for (int start = 0; start < size - num; ++start) {
584  // Create a set with "size" fields.
585  UnknownFieldSet unknown;
586  for (int i = 0; i < size; ++i) {
587  unknown.AddFixed32(i, i);
588  }
589  // Delete the specified subrange.
590  unknown.DeleteSubrange(start, num);
591  // Make sure the resulting field values are still correct.
592  EXPECT_EQ(size - num, unknown.field_count());
593  for (int i = 0; i < unknown.field_count(); ++i) {
594  if (i < start) {
595  EXPECT_EQ(i, unknown.field(i).fixed32());
596  } else {
597  EXPECT_EQ(i + num, unknown.field(i).fixed32());
598  }
599  }
600  }
601  }
602  }
603 }
604 
605 void CheckDeleteByNumber(const std::vector<int>& field_numbers,
606  int deleted_number,
607  const std::vector<int>& expected_field_nubmers) {
608  UnknownFieldSet unknown_fields;
609  for (int i = 0; i < field_numbers.size(); ++i) {
610  unknown_fields.AddFixed32(field_numbers[i], i);
611  }
612  unknown_fields.DeleteByNumber(deleted_number);
613  ASSERT_EQ(expected_field_nubmers.size(), unknown_fields.field_count());
614  for (int i = 0; i < expected_field_nubmers.size(); ++i) {
615  EXPECT_EQ(expected_field_nubmers[i], unknown_fields.field(i).number());
616  }
617 }
618 
619 #define MAKE_VECTOR(x) std::vector<int>(x, x + GOOGLE_ARRAYSIZE(x))
620 TEST_F(UnknownFieldSetTest, DeleteByNumber) {
621  CheckDeleteByNumber(std::vector<int>(), 1, std::vector<int>());
622  static const int kTestFieldNumbers1[] = {1, 2, 3};
623  static const int kFieldNumberToDelete1 = 1;
624  static const int kExpectedFieldNumbers1[] = {2, 3};
625  CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers1), kFieldNumberToDelete1,
626  MAKE_VECTOR(kExpectedFieldNumbers1));
627  static const int kTestFieldNumbers2[] = {1, 2, 3};
628  static const int kFieldNumberToDelete2 = 2;
629  static const int kExpectedFieldNumbers2[] = {1, 3};
630  CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers2), kFieldNumberToDelete2,
631  MAKE_VECTOR(kExpectedFieldNumbers2));
632  static const int kTestFieldNumbers3[] = {1, 2, 3};
633  static const int kFieldNumberToDelete3 = 3;
634  static const int kExpectedFieldNumbers3[] = {1, 2};
635  CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers3), kFieldNumberToDelete3,
636  MAKE_VECTOR(kExpectedFieldNumbers3));
637  static const int kTestFieldNumbers4[] = {1, 2, 1, 4, 1};
638  static const int kFieldNumberToDelete4 = 1;
639  static const int kExpectedFieldNumbers4[] = {2, 4};
640  CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers4), kFieldNumberToDelete4,
641  MAKE_VECTOR(kExpectedFieldNumbers4));
642  static const int kTestFieldNumbers5[] = {1, 2, 3, 4, 5};
643  static const int kFieldNumberToDelete5 = 6;
644  static const int kExpectedFieldNumbers5[] = {1, 2, 3, 4, 5};
645  CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers5), kFieldNumberToDelete5,
646  MAKE_VECTOR(kExpectedFieldNumbers5));
647 }
648 #undef MAKE_VECTOR
649 } // namespace
650 
651 } // namespace protobuf
652 } // namespace google
google::protobuf::UnknownField::type
Type type() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:327
xds_interop_client.str
str
Definition: xds_interop_client.py:487
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
google::protobuf.internal::WireFormat::SerializeWithCachedSizes
static void SerializeWithCachedSizes(const Message &message, int size, io::CodedOutputStream *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format.h:116
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
descriptor_
string_view descriptor_
Definition: elf.cc:154
absl::swap_internal::Swap
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
Definition: abseil-cpp/absl/meta/type_traits.h:772
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::UnknownFieldSetTest::GetBizarroData
std::string GetBizarroData()
Definition: protobuf/src/google/protobuf/unknown_field_set_unittest.cc:87
google::protobuf.internal.python_message.MergeFrom
MergeFrom
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:1339
Group
TypeAndValue Group(UnknownFields nested)
Definition: upb/upb/util/compare_test.cc:101
google::protobuf::UnknownField::TYPE_GROUP
@ TYPE_GROUP
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:223
google::protobuf::uint8
uint8_t uint8
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf::UnknownFieldSetTest::descriptor_
const Descriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set_unittest.cc:101
google::protobuf::python::cmessage::CopyFrom
static PyObject * CopyFrom(CMessage *self, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:1862
UnknownField
Definition: upb/upb/util/compare_test.cc:66
EXPECT_GT
#define EXPECT_GT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2036
google::protobuf.internal::WireFormat::ByteSize
static size_t ByteSize(const Message &message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format.cc:1069
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::uint32
uint32_t uint32
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::UnknownField
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:216
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
setup.name
name
Definition: setup.py:542
google::protobuf::UnknownFieldSetTest::all_fields_
unittest::TestAllTypes all_fields_
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set_unittest.cc:102
google::protobuf::python::cmessage::UnknownFieldSet
static PyObject * UnknownFieldSet(CMessage *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:2512
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
repeated_field
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern repeated_field
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/array.c:486
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
tests.google.protobuf.internal.test_util.SetAllFields
def SetAllFields(message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:182
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
google::protobuf::UnknownFieldSet::field_count
int field_count() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:308
google::protobuf.internal::WireFormat::ComputeUnknownFieldsSize
static size_t ComputeUnknownFieldsSize(const UnknownFieldSet &unknown_fields)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format.cc:266
grpc::protobuf::io::StringOutputStream
GRPC_CUSTOM_STRINGOUTPUTSTREAM StringOutputStream
Definition: src/compiler/config.h:56
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
google::protobuf::UnknownField::TYPE_FIXED32
@ TYPE_FIXED32
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:220
start
static uint64_t start
Definition: benchmark-pound.c:74
absl::string_view::size
constexpr size_type size() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:277
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
google::protobuf.internal::WireFormat::SerializeUnknownFieldsToArray
static uint8 * SerializeUnknownFieldsToArray(const UnknownFieldSet &unknown_fields, uint8 *target)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format.h:170
grpc::protobuf::io::CodedInputStream
GRPC_CUSTOM_CODEDINPUTSTREAM CodedInputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:102
google::protobuf::UnknownFieldSetTest::GetField
const UnknownField * GetField(const std::string &name)
Definition: protobuf/src/google/protobuf/unknown_field_set_unittest.cc:73
google::protobuf::UnknownFieldSet::AddVarint
void AddVarint(int number, uint64 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.cc:135
tests.google.protobuf.internal.test_util.ExpectAllFieldsSet
def ExpectAllFieldsSet(test_case, message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:367
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
add_person.raw_input
raw_input
Definition: bloaty/third_party/protobuf/examples/add_person.py:11
grpc::protobuf::io::CodedOutputStream
GRPC_CUSTOM_CODEDOUTPUTSTREAM CodedOutputStream
Definition: src/compiler/config.h:55
google::protobuf::string_as_array
char * string_as_array(string *str)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stl_util.h:63
google::protobuf::UnknownField::TYPE_VARINT
@ TYPE_VARINT
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:219
Fixed64
TypeAndValue Fixed64(uint64_t val)
Definition: upb/upb/util/compare_test.cc:83
google::protobuf::UnknownField::TYPE_LENGTH_DELIMITED
@ TYPE_LENGTH_DELIMITED
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:222
Empty
Definition: abseil-cpp/absl/container/internal/compressed_tuple_test.cc:33
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
MAKE_VECTOR
#define MAKE_VECTOR(x)
Definition: protobuf/src/google/protobuf/unknown_field_set_unittest.cc:619
google::protobuf::UnknownFieldSetTest::all_fields_data_
std::string all_fields_data_
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set_unittest.cc:103
google::protobuf::UnknownFieldSetTest::empty_message_
unittest::TestEmptyMessage empty_message_
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set_unittest.cc:107
google::protobuf::UnknownField::TYPE_FIXED64
@ TYPE_FIXED64
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:221
google::protobuf::UnknownFieldSet::field
const UnknownField & field(int index) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:311
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
Varint
TypeAndValue Varint(uint64_t val)
Definition: upb/upb/util/compare_test.cc:71
google::protobuf::FieldDescriptor::type
Type type() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2052
upload.group
group
Definition: bloaty/third_party/googletest/googlemock/scripts/upload.py:397
google::protobuf.internal.python_message.Clear
Clear
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:1430
google::protobuf::UnknownFieldSetTest::SetUp
virtual void SetUp()
Definition: protobuf/src/google/protobuf/unknown_field_set_unittest.cc:65
google::protobuf::Descriptor::FindFieldByName
const FieldDescriptor * FindFieldByName(const std::string &name) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:1595
google::protobuf::TEST_F
TEST_F(DynamicMessageTest, Descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message_unittest.cc:126
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
google::protobuf::UnknownFieldSetTest::unknown_fields_
UnknownFieldSet * unknown_fields_
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set_unittest.cc:108
google::protobuf::UnknownFieldSet
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:81
google::protobuf.internal::WireFormat::SerializeUnknownFields
static void SerializeUnknownFields(const UnknownFieldSet &unknown_fields, io::CodedOutputStream *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format.h:160
xds_manager.num
num
Definition: xds_manager.py:56
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
google::protobuf.internal::WireFormat::ParseAndMergePartial
static bool ParseAndMergePartial(io::CodedInputStream *input, Message *message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format.cc:331
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
google::protobuf::UnknownFieldSet::AddFixed32
void AddFixed32(int number, uint32 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.cc:143
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
google::protobuf::UnknownField::number
int number() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:326
Fixed32
TypeAndValue Fixed32(uint32_t val)
Definition: upb/upb/util/compare_test.cc:89
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:44