unknown_field_set_unittest.cc 22 KB

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