/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/unknown_field_set_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 512 lines · 343 code · 96 blank · 73 comment · 24 complexity · c9341bcaab9531f5e2a38fb43b6dd156 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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/common.h>
  44. #include <google/protobuf/testing/googletest.h>
  45. #include <gtest/gtest.h>
  46. #include <google/protobuf/stubs/stl_util-inl.h>
  47. namespace google {
  48. namespace protobuf {
  49. using internal::WireFormat;
  50. namespace {
  51. class UnknownFieldSetTest : public testing::Test {
  52. protected:
  53. virtual void SetUp() {
  54. descriptor_ = unittest::TestAllTypes::descriptor();
  55. TestUtil::SetAllFields(&all_fields_);
  56. all_fields_.SerializeToString(&all_fields_data_);
  57. ASSERT_TRUE(empty_message_.ParseFromString(all_fields_data_));
  58. unknown_fields_ = empty_message_.mutable_unknown_fields();
  59. }
  60. const UnknownField* GetField(const string& name) {
  61. const FieldDescriptor* field = descriptor_->FindFieldByName(name);
  62. if (field == NULL) return NULL;
  63. for (int i = 0; i < unknown_fields_->field_count(); i++) {
  64. if (unknown_fields_->field(i).number() == field->number()) {
  65. return &unknown_fields_->field(i);
  66. }
  67. }
  68. return NULL;
  69. }
  70. // Constructs a protocol buffer which contains fields with all the same
  71. // numbers as all_fields_data_ except that each field is some other wire
  72. // type.
  73. string GetBizarroData() {
  74. unittest::TestEmptyMessage bizarro_message;
  75. UnknownFieldSet* bizarro_unknown_fields =
  76. bizarro_message.mutable_unknown_fields();
  77. for (int i = 0; i < unknown_fields_->field_count(); i++) {
  78. const UnknownField& unknown_field = unknown_fields_->field(i);
  79. if (unknown_field.type() == UnknownField::TYPE_VARINT) {
  80. bizarro_unknown_fields->AddFixed32(unknown_field.number(), 1);
  81. } else {
  82. bizarro_unknown_fields->AddVarint(unknown_field.number(), 1);
  83. }
  84. }
  85. string data;
  86. EXPECT_TRUE(bizarro_message.SerializeToString(&data));
  87. return data;
  88. }
  89. const Descriptor* descriptor_;
  90. unittest::TestAllTypes all_fields_;
  91. string all_fields_data_;
  92. // An empty message that has been parsed from all_fields_data_. So, it has
  93. // unknown fields of every type.
  94. unittest::TestEmptyMessage empty_message_;
  95. UnknownFieldSet* unknown_fields_;
  96. };
  97. TEST_F(UnknownFieldSetTest, AllFieldsPresent) {
  98. // All fields of TestAllTypes should be present, in numeric order (because
  99. // that's the order we parsed them in). Fields that are not valid field
  100. // numbers of TestAllTypes should NOT be present.
  101. int pos = 0;
  102. for (int i = 0; i < 1000; i++) {
  103. const FieldDescriptor* field = descriptor_->FindFieldByNumber(i);
  104. if (field != NULL) {
  105. ASSERT_LT(pos, unknown_fields_->field_count());
  106. EXPECT_EQ(i, unknown_fields_->field(pos++).number());
  107. if (field->is_repeated()) {
  108. // Should have a second instance.
  109. ASSERT_LT(pos, unknown_fields_->field_count());
  110. EXPECT_EQ(i, unknown_fields_->field(pos++).number());
  111. }
  112. }
  113. }
  114. EXPECT_EQ(unknown_fields_->field_count(), pos);
  115. }
  116. TEST_F(UnknownFieldSetTest, Varint) {
  117. const UnknownField* field = GetField("optional_int32");
  118. ASSERT_TRUE(field != NULL);
  119. ASSERT_EQ(UnknownField::TYPE_VARINT, field->type());
  120. EXPECT_EQ(all_fields_.optional_int32(), field->varint());
  121. }
  122. TEST_F(UnknownFieldSetTest, Fixed32) {
  123. const UnknownField* field = GetField("optional_fixed32");
  124. ASSERT_TRUE(field != NULL);
  125. ASSERT_EQ(UnknownField::TYPE_FIXED32, field->type());
  126. EXPECT_EQ(all_fields_.optional_fixed32(), field->fixed32());
  127. }
  128. TEST_F(UnknownFieldSetTest, Fixed64) {
  129. const UnknownField* field = GetField("optional_fixed64");
  130. ASSERT_TRUE(field != NULL);
  131. ASSERT_EQ(UnknownField::TYPE_FIXED64, field->type());
  132. EXPECT_EQ(all_fields_.optional_fixed64(), field->fixed64());
  133. }
  134. TEST_F(UnknownFieldSetTest, LengthDelimited) {
  135. const UnknownField* field = GetField("optional_string");
  136. ASSERT_TRUE(field != NULL);
  137. ASSERT_EQ(UnknownField::TYPE_LENGTH_DELIMITED, field->type());
  138. EXPECT_EQ(all_fields_.optional_string(), field->length_delimited());
  139. }
  140. TEST_F(UnknownFieldSetTest, Group) {
  141. const UnknownField* field = GetField("optionalgroup");
  142. ASSERT_TRUE(field != NULL);
  143. ASSERT_EQ(UnknownField::TYPE_GROUP, field->type());
  144. ASSERT_EQ(1, field->group().field_count());
  145. const UnknownField& nested_field = field->group().field(0);
  146. const FieldDescriptor* nested_field_descriptor =
  147. unittest::TestAllTypes::OptionalGroup::descriptor()->FindFieldByName("a");
  148. ASSERT_TRUE(nested_field_descriptor != NULL);
  149. EXPECT_EQ(nested_field_descriptor->number(), nested_field.number());
  150. ASSERT_EQ(UnknownField::TYPE_VARINT, nested_field.type());
  151. EXPECT_EQ(all_fields_.optionalgroup().a(), nested_field.varint());
  152. }
  153. TEST_F(UnknownFieldSetTest, SerializeFastAndSlowAreEquivalent) {
  154. int size = WireFormat::ComputeUnknownFieldsSize(
  155. empty_message_.unknown_fields());
  156. string slow_buffer;
  157. string fast_buffer;
  158. slow_buffer.resize(size);
  159. fast_buffer.resize(size);
  160. uint8* target = reinterpret_cast<uint8*>(string_as_array(&fast_buffer));
  161. uint8* result = WireFormat::SerializeUnknownFieldsToArray(
  162. empty_message_.unknown_fields(), target);
  163. EXPECT_EQ(size, result - target);
  164. {
  165. io::ArrayOutputStream raw_stream(string_as_array(&slow_buffer), size, 1);
  166. io::CodedOutputStream output_stream(&raw_stream);
  167. WireFormat::SerializeUnknownFields(empty_message_.unknown_fields(),
  168. &output_stream);
  169. ASSERT_FALSE(output_stream.HadError());
  170. }
  171. EXPECT_TRUE(fast_buffer == slow_buffer);
  172. }
  173. TEST_F(UnknownFieldSetTest, Serialize) {
  174. // Check that serializing the UnknownFieldSet produces the original data
  175. // again.
  176. string data;
  177. empty_message_.SerializeToString(&data);
  178. // Don't use EXPECT_EQ because we don't want to dump raw binary data to
  179. // stdout.
  180. EXPECT_TRUE(data == all_fields_data_);
  181. }
  182. TEST_F(UnknownFieldSetTest, ParseViaReflection) {
  183. // Make sure fields are properly parsed to the UnknownFieldSet when parsing
  184. // via reflection.
  185. unittest::TestEmptyMessage message;
  186. io::ArrayInputStream raw_input(all_fields_data_.data(),
  187. all_fields_data_.size());
  188. io::CodedInputStream input(&raw_input);
  189. ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &message));
  190. EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
  191. }
  192. TEST_F(UnknownFieldSetTest, SerializeViaReflection) {
  193. // Make sure fields are properly written from the UnknownFieldSet when
  194. // serializing via reflection.
  195. string data;
  196. {
  197. io::StringOutputStream raw_output(&data);
  198. io::CodedOutputStream output(&raw_output);
  199. int size = WireFormat::ByteSize(empty_message_);
  200. WireFormat::SerializeWithCachedSizes(empty_message_, size, &output);
  201. ASSERT_FALSE(output.HadError());
  202. }
  203. // Don't use EXPECT_EQ because we don't want to dump raw binary data to
  204. // stdout.
  205. EXPECT_TRUE(data == all_fields_data_);
  206. }
  207. TEST_F(UnknownFieldSetTest, CopyFrom) {
  208. unittest::TestEmptyMessage message;
  209. message.CopyFrom(empty_message_);
  210. EXPECT_EQ(empty_message_.DebugString(), message.DebugString());
  211. }
  212. TEST_F(UnknownFieldSetTest, Swap) {
  213. unittest::TestEmptyMessage other_message;
  214. ASSERT_TRUE(other_message.ParseFromString(GetBizarroData()));
  215. EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
  216. EXPECT_GT(other_message.unknown_fields().field_count(), 0);
  217. const string debug_string = empty_message_.DebugString();
  218. const string other_debug_string = other_message.DebugString();
  219. EXPECT_NE(debug_string, other_debug_string);
  220. empty_message_.Swap(&other_message);
  221. EXPECT_EQ(debug_string, other_message.DebugString());
  222. EXPECT_EQ(other_debug_string, empty_message_.DebugString());
  223. }
  224. TEST_F(UnknownFieldSetTest, SwapWithSelf) {
  225. const string debug_string = empty_message_.DebugString();
  226. EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
  227. empty_message_.Swap(&empty_message_);
  228. EXPECT_GT(empty_message_.unknown_fields().field_count(), 0);
  229. EXPECT_EQ(debug_string, empty_message_.DebugString());
  230. }
  231. TEST_F(UnknownFieldSetTest, MergeFrom) {
  232. unittest::TestEmptyMessage source, destination;
  233. destination.mutable_unknown_fields()->AddVarint(1, 1);
  234. destination.mutable_unknown_fields()->AddVarint(3, 2);
  235. source.mutable_unknown_fields()->AddVarint(2, 3);
  236. source.mutable_unknown_fields()->AddVarint(3, 4);
  237. destination.MergeFrom(source);
  238. EXPECT_EQ(
  239. // Note: The ordering of fields here depends on the ordering of adds
  240. // and merging, above.
  241. "1: 1\n"
  242. "3: 2\n"
  243. "2: 3\n"
  244. "3: 4\n",
  245. destination.DebugString());
  246. }
  247. TEST_F(UnknownFieldSetTest, Clear) {
  248. // Clear the set.
  249. empty_message_.Clear();
  250. EXPECT_EQ(0, unknown_fields_->field_count());
  251. }
  252. TEST_F(UnknownFieldSetTest, ParseKnownAndUnknown) {
  253. // Test mixing known and unknown fields when parsing.
  254. unittest::TestEmptyMessage source;
  255. source.mutable_unknown_fields()->AddVarint(123456, 654321);
  256. string data;
  257. ASSERT_TRUE(source.SerializeToString(&data));
  258. unittest::TestAllTypes destination;
  259. ASSERT_TRUE(destination.ParseFromString(all_fields_data_ + data));
  260. TestUtil::ExpectAllFieldsSet(destination);
  261. ASSERT_EQ(1, destination.unknown_fields().field_count());
  262. ASSERT_EQ(UnknownField::TYPE_VARINT,
  263. destination.unknown_fields().field(0).type());
  264. EXPECT_EQ(654321, destination.unknown_fields().field(0).varint());
  265. }
  266. TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknown) {
  267. // Test that fields of the wrong wire type are treated like unknown fields
  268. // when parsing.
  269. unittest::TestAllTypes all_types_message;
  270. unittest::TestEmptyMessage empty_message;
  271. string bizarro_data = GetBizarroData();
  272. ASSERT_TRUE(all_types_message.ParseFromString(bizarro_data));
  273. ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
  274. // All fields should have been interpreted as unknown, so the debug strings
  275. // should be the same.
  276. EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
  277. }
  278. TEST_F(UnknownFieldSetTest, WrongTypeTreatedAsUnknownViaReflection) {
  279. // Same as WrongTypeTreatedAsUnknown but via the reflection interface.
  280. unittest::TestAllTypes all_types_message;
  281. unittest::TestEmptyMessage empty_message;
  282. string bizarro_data = GetBizarroData();
  283. io::ArrayInputStream raw_input(bizarro_data.data(), bizarro_data.size());
  284. io::CodedInputStream input(&raw_input);
  285. ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &all_types_message));
  286. ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
  287. EXPECT_EQ(empty_message.DebugString(), all_types_message.DebugString());
  288. }
  289. TEST_F(UnknownFieldSetTest, UnknownExtensions) {
  290. // Make sure fields are properly parsed to the UnknownFieldSet even when
  291. // they are declared as extension numbers.
  292. unittest::TestEmptyMessageWithExtensions message;
  293. ASSERT_TRUE(message.ParseFromString(all_fields_data_));
  294. EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
  295. }
  296. TEST_F(UnknownFieldSetTest, UnknownExtensionsReflection) {
  297. // Same as UnknownExtensions except parsing via reflection.
  298. unittest::TestEmptyMessageWithExtensions message;
  299. io::ArrayInputStream raw_input(all_fields_data_.data(),
  300. all_fields_data_.size());
  301. io::CodedInputStream input(&raw_input);
  302. ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &message));
  303. EXPECT_EQ(message.DebugString(), empty_message_.DebugString());
  304. }
  305. TEST_F(UnknownFieldSetTest, WrongExtensionTypeTreatedAsUnknown) {
  306. // Test that fields of the wrong wire type are treated like unknown fields
  307. // when parsing extensions.
  308. unittest::TestAllExtensions all_extensions_message;
  309. unittest::TestEmptyMessage empty_message;
  310. string bizarro_data = GetBizarroData();
  311. ASSERT_TRUE(all_extensions_message.ParseFromString(bizarro_data));
  312. ASSERT_TRUE(empty_message.ParseFromString(bizarro_data));
  313. // All fields should have been interpreted as unknown, so the debug strings
  314. // should be the same.
  315. EXPECT_EQ(empty_message.DebugString(), all_extensions_message.DebugString());
  316. }
  317. TEST_F(UnknownFieldSetTest, UnknownEnumValue) {
  318. using unittest::TestAllTypes;
  319. using unittest::TestAllExtensions;
  320. using unittest::TestEmptyMessage;
  321. const FieldDescriptor* singular_field =
  322. TestAllTypes::descriptor()->FindFieldByName("optional_nested_enum");
  323. const FieldDescriptor* repeated_field =
  324. TestAllTypes::descriptor()->FindFieldByName("repeated_nested_enum");
  325. ASSERT_TRUE(singular_field != NULL);
  326. ASSERT_TRUE(repeated_field != NULL);
  327. string data;
  328. {
  329. TestEmptyMessage empty_message;
  330. UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
  331. unknown_fields->AddVarint(singular_field->number(), TestAllTypes::BAR);
  332. unknown_fields->AddVarint(singular_field->number(), 5); // not valid
  333. unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::FOO);
  334. unknown_fields->AddVarint(repeated_field->number(), 4); // not valid
  335. unknown_fields->AddVarint(repeated_field->number(), TestAllTypes::BAZ);
  336. unknown_fields->AddVarint(repeated_field->number(), 6); // not valid
  337. empty_message.SerializeToString(&data);
  338. }
  339. {
  340. TestAllTypes message;
  341. ASSERT_TRUE(message.ParseFromString(data));
  342. EXPECT_EQ(TestAllTypes::BAR, message.optional_nested_enum());
  343. ASSERT_EQ(2, message.repeated_nested_enum_size());
  344. EXPECT_EQ(TestAllTypes::FOO, message.repeated_nested_enum(0));
  345. EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(1));
  346. const UnknownFieldSet& unknown_fields = message.unknown_fields();
  347. ASSERT_EQ(3, unknown_fields.field_count());
  348. EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
  349. ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
  350. EXPECT_EQ(5, unknown_fields.field(0).varint());
  351. EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
  352. ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
  353. EXPECT_EQ(4, unknown_fields.field(1).varint());
  354. EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
  355. ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
  356. EXPECT_EQ(6, unknown_fields.field(2).varint());
  357. }
  358. {
  359. using unittest::optional_nested_enum_extension;
  360. using unittest::repeated_nested_enum_extension;
  361. TestAllExtensions message;
  362. ASSERT_TRUE(message.ParseFromString(data));
  363. EXPECT_EQ(TestAllTypes::BAR,
  364. message.GetExtension(optional_nested_enum_extension));
  365. ASSERT_EQ(2, message.ExtensionSize(repeated_nested_enum_extension));
  366. EXPECT_EQ(TestAllTypes::FOO,
  367. message.GetExtension(repeated_nested_enum_extension, 0));
  368. EXPECT_EQ(TestAllTypes::BAZ,
  369. message.GetExtension(repeated_nested_enum_extension, 1));
  370. const UnknownFieldSet& unknown_fields = message.unknown_fields();
  371. ASSERT_EQ(3, unknown_fields.field_count());
  372. EXPECT_EQ(singular_field->number(), unknown_fields.field(0).number());
  373. ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(0).type());
  374. EXPECT_EQ(5, unknown_fields.field(0).varint());
  375. EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number());
  376. ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type());
  377. EXPECT_EQ(4, unknown_fields.field(1).varint());
  378. EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number());
  379. ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type());
  380. EXPECT_EQ(6, unknown_fields.field(2).varint());
  381. }
  382. }
  383. TEST_F(UnknownFieldSetTest, SpaceUsed) {
  384. unittest::TestEmptyMessage empty_message;
  385. // Make sure an unknown field set has zero space used until a field is
  386. // actually added.
  387. int base_size = empty_message.SpaceUsed();
  388. UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields();
  389. EXPECT_EQ(base_size, empty_message.SpaceUsed());
  390. // Make sure each thing we add to the set increases the SpaceUsed().
  391. unknown_fields->AddVarint(1, 0);
  392. EXPECT_LT(base_size, empty_message.SpaceUsed());
  393. base_size = empty_message.SpaceUsed();
  394. string* str = unknown_fields->AddLengthDelimited(1);
  395. EXPECT_LT(base_size, empty_message.SpaceUsed());
  396. base_size = empty_message.SpaceUsed();
  397. str->assign(sizeof(string) + 1, 'x');
  398. EXPECT_LT(base_size, empty_message.SpaceUsed());
  399. base_size = empty_message.SpaceUsed();
  400. UnknownFieldSet* group = unknown_fields->AddGroup(1);
  401. EXPECT_LT(base_size, empty_message.SpaceUsed());
  402. base_size = empty_message.SpaceUsed();
  403. group->AddVarint(1, 0);
  404. EXPECT_LT(base_size, empty_message.SpaceUsed());
  405. }
  406. TEST_F(UnknownFieldSetTest, Empty) {
  407. UnknownFieldSet unknown_fields;
  408. EXPECT_TRUE(unknown_fields.empty());
  409. unknown_fields.AddVarint(6, 123);
  410. EXPECT_FALSE(unknown_fields.empty());
  411. unknown_fields.Clear();
  412. EXPECT_TRUE(unknown_fields.empty());
  413. }
  414. } // namespace
  415. } // namespace protobuf
  416. } // namespace google