PageRenderTime 73ms CodeModel.GetById 11ms RepoModel.GetById 2ms app.codeStats 0ms

/objectivec/Tests/GPBUnknownFieldSetTest.m

https://gitlab.com/admin-github-cloud/protobuf
Objective C | 255 lines | 166 code | 40 blank | 49 comment | 4 complexity | ff9106569ef83f08f5a4fb61f97082e1 MD5 | raw 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. #import "GPBTestUtilities.h"
  31. #import "GPBUnknownField_PackagePrivate.h"
  32. #import "GPBUnknownFieldSet_PackagePrivate.h"
  33. #import "google/protobuf/Unittest.pbobjc.h"
  34. @interface GPBUnknownFieldSet (GPBUnknownFieldSetTest)
  35. - (void)getTags:(int32_t*)tags;
  36. @end
  37. @interface UnknownFieldSetTest : GPBTestCase {
  38. @private
  39. TestAllTypes* allFields_;
  40. NSData* allFieldsData_;
  41. // An empty message that has been parsed from allFieldsData. So, it has
  42. // unknown fields of every type.
  43. TestEmptyMessage* emptyMessage_;
  44. GPBUnknownFieldSet* unknownFields_;
  45. }
  46. @end
  47. @implementation UnknownFieldSetTest
  48. - (void)setUp {
  49. allFields_ = [self allSetRepeatedCount:kGPBDefaultRepeatCount];
  50. allFieldsData_ = [allFields_ data];
  51. emptyMessage_ = [TestEmptyMessage parseFromData:allFieldsData_ error:NULL];
  52. unknownFields_ = emptyMessage_.unknownFields;
  53. }
  54. // Constructs a protocol buffer which contains fields with all the same
  55. // numbers as allFieldsData except that each field is some other wire
  56. // type.
  57. - (NSData*)getBizarroData {
  58. GPBUnknownFieldSet* bizarroFields =
  59. [[[GPBUnknownFieldSet alloc] init] autorelease];
  60. NSUInteger count = [unknownFields_ countOfFields];
  61. int32_t tags[count];
  62. [unknownFields_ getTags:tags];
  63. for (NSUInteger i = 0; i < count; ++i) {
  64. int32_t tag = tags[i];
  65. GPBUnknownField* field = [unknownFields_ getField:tag];
  66. if (field.varintList.count == 0) {
  67. // Original field is not a varint, so use a varint.
  68. GPBUnknownField* varintField =
  69. [[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
  70. [varintField addVarint:1];
  71. [bizarroFields addField:varintField];
  72. } else {
  73. // Original field *is* a varint, so use something else.
  74. GPBUnknownField* fixed32Field =
  75. [[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
  76. [fixed32Field addFixed32:1];
  77. [bizarroFields addField:fixed32Field];
  78. }
  79. }
  80. return [bizarroFields data];
  81. }
  82. - (void)testSerialize {
  83. // Check that serializing the UnknownFieldSet produces the original data
  84. // again.
  85. NSData* data = [emptyMessage_ data];
  86. XCTAssertEqualObjects(allFieldsData_, data);
  87. }
  88. - (void)testCopyFrom {
  89. TestEmptyMessage* message = [TestEmptyMessage message];
  90. [message mergeFrom:emptyMessage_];
  91. XCTAssertEqualObjects(emptyMessage_.data, message.data);
  92. }
  93. - (void)testMergeFrom {
  94. GPBUnknownFieldSet* set1 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  95. GPBUnknownField* field = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
  96. [field addVarint:2];
  97. [set1 addField:field];
  98. field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
  99. [field addVarint:4];
  100. [set1 addField:field];
  101. GPBUnknownFieldSet* set2 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  102. field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
  103. [field addVarint:1];
  104. [set2 addField:field];
  105. field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
  106. [field addVarint:3];
  107. [set2 addField:field];
  108. GPBUnknownFieldSet* set3 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  109. field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
  110. [field addVarint:1];
  111. [set3 addField:field];
  112. field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
  113. [field addVarint:4];
  114. [set3 addField:field];
  115. GPBUnknownFieldSet* set4 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  116. field = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
  117. [field addVarint:2];
  118. [set4 addField:field];
  119. field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
  120. [field addVarint:3];
  121. [set4 addField:field];
  122. TestEmptyMessage* source1 = [TestEmptyMessage message];
  123. [source1 setUnknownFields:set1];
  124. TestEmptyMessage* source2 = [TestEmptyMessage message];
  125. [source2 setUnknownFields:set2];
  126. TestEmptyMessage* source3 = [TestEmptyMessage message];
  127. [source3 setUnknownFields:set3];
  128. TestEmptyMessage* source4 = [TestEmptyMessage message];
  129. [source4 setUnknownFields:set4];
  130. TestEmptyMessage* destination1 = [TestEmptyMessage message];
  131. [destination1 mergeFrom:source1];
  132. [destination1 mergeFrom:source2];
  133. TestEmptyMessage* destination2 = [TestEmptyMessage message];
  134. [destination2 mergeFrom:source3];
  135. [destination2 mergeFrom:source4];
  136. XCTAssertEqualObjects(destination1.data, destination2.data);
  137. }
  138. - (void)testClearMessage {
  139. TestEmptyMessage *message = [TestEmptyMessage message];
  140. [message mergeFrom:emptyMessage_];
  141. [message clear];
  142. XCTAssertEqual(message.serializedSize, (size_t)0);
  143. }
  144. - (void)testParseKnownAndUnknown {
  145. // Test mixing known and unknown fields when parsing.
  146. GPBUnknownFieldSet *fields = [[unknownFields_ copy] autorelease];
  147. GPBUnknownField *field =
  148. [[[GPBUnknownField alloc] initWithNumber:123456] autorelease];
  149. [field addVarint:654321];
  150. [fields addField:field];
  151. NSData* data = fields.data;
  152. TestAllTypes* destination = [TestAllTypes parseFromData:data error:NULL];
  153. [self assertAllFieldsSet:destination repeatedCount:kGPBDefaultRepeatCount];
  154. XCTAssertEqual(destination.unknownFields.countOfFields, (NSUInteger)1);
  155. GPBUnknownField* field2 = [destination.unknownFields getField:123456];
  156. XCTAssertEqual(field2.varintList.count, (NSUInteger)1);
  157. XCTAssertEqual(654321ULL, [field2.varintList valueAtIndex:0]);
  158. }
  159. - (void)testWrongTypeTreatedAsUnknown {
  160. // Test that fields of the wrong wire type are treated like unknown fields
  161. // when parsing.
  162. NSData* bizarroData = [self getBizarroData];
  163. TestAllTypes* allTypesMessage =
  164. [TestAllTypes parseFromData:bizarroData error:NULL];
  165. TestEmptyMessage* emptyMessage =
  166. [TestEmptyMessage parseFromData:bizarroData error:NULL];
  167. // All fields should have been interpreted as unknown, so the debug strings
  168. // should be the same.
  169. XCTAssertEqualObjects(emptyMessage.data, allTypesMessage.data);
  170. }
  171. - (void)testUnknownExtensions {
  172. // Make sure fields are properly parsed to the UnknownFieldSet even when
  173. // they are declared as extension numbers.
  174. TestEmptyMessageWithExtensions* message =
  175. [TestEmptyMessageWithExtensions parseFromData:allFieldsData_ error:NULL];
  176. XCTAssertEqual(unknownFields_.countOfFields,
  177. message.unknownFields.countOfFields);
  178. XCTAssertEqualObjects(allFieldsData_, message.data);
  179. }
  180. - (void)testWrongExtensionTypeTreatedAsUnknown {
  181. // Test that fields of the wrong wire type are treated like unknown fields
  182. // when parsing extensions.
  183. NSData* bizarroData = [self getBizarroData];
  184. TestAllExtensions* allExtensionsMessage =
  185. [TestAllExtensions parseFromData:bizarroData error:NULL];
  186. TestEmptyMessage* emptyMessage =
  187. [TestEmptyMessage parseFromData:bizarroData error:NULL];
  188. // All fields should have been interpreted as unknown, so the debug strings
  189. // should be the same.
  190. XCTAssertEqualObjects(emptyMessage.data, allExtensionsMessage.data);
  191. }
  192. - (void)testLargeVarint {
  193. GPBUnknownFieldSet* fields = [[unknownFields_ copy] autorelease];
  194. GPBUnknownField* field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
  195. [field addVarint:0x7FFFFFFFFFFFFFFFL];
  196. [fields addField:field];
  197. NSData* data = [fields data];
  198. GPBUnknownFieldSet* parsed = [[[GPBUnknownFieldSet alloc] init] autorelease];
  199. [parsed mergeFromData:data];
  200. GPBUnknownField* field2 = [parsed getField:1];
  201. XCTAssertEqual(field2.varintList.count, (NSUInteger)1);
  202. XCTAssertEqual(0x7FFFFFFFFFFFFFFFULL, [field2.varintList valueAtIndex:0]);
  203. }
  204. - (void)testMergingFields {
  205. GPBUnknownField* field1 = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
  206. [field1 addVarint:1];
  207. [field1 addFixed32:2];
  208. [field1 addFixed64:3];
  209. [field1 addLengthDelimited:[NSData dataWithBytes:"hello" length:5]];
  210. [field1 addGroup:[[unknownFields_ copy] autorelease]];
  211. GPBUnknownField* field2 = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
  212. [field2 mergeFromField:field1];
  213. XCTAssertEqualObjects(field1, field2);
  214. }
  215. @end