GPBUnknownFieldSetTest.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "GPBField_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_];
  52. unknownFields_ = emptyMessage_.unknownFields;
  53. }
  54. - (GPBField*)getField:(int32_t)number {
  55. return [unknownFields_ getField:number];
  56. }
  57. // Constructs a protocol buffer which contains fields with all the same
  58. // numbers as allFieldsData except that each field is some other wire
  59. // type.
  60. - (NSData*)getBizarroData {
  61. GPBUnknownFieldSet* bizarroFields =
  62. [[[GPBUnknownFieldSet alloc] init] autorelease];
  63. NSUInteger count = [unknownFields_ countOfFields];
  64. int32_t tags[count];
  65. [unknownFields_ getTags:tags];
  66. for (NSUInteger i = 0; i < count; ++i) {
  67. int32_t tag = tags[i];
  68. GPBField* field = [unknownFields_ getField:tag];
  69. if (field.varintList.count == 0) {
  70. // Original field is not a varint, so use a varint.
  71. GPBField* varintField =
  72. [[[GPBField alloc] initWithNumber:tag] autorelease];
  73. [varintField addVarint:1];
  74. [bizarroFields addField:varintField];
  75. } else {
  76. // Original field *is* a varint, so use something else.
  77. GPBField* fixed32Field =
  78. [[[GPBField alloc] initWithNumber:tag] autorelease];
  79. [fixed32Field addFixed32:1];
  80. [bizarroFields addField:fixed32Field];
  81. }
  82. }
  83. return [bizarroFields data];
  84. }
  85. - (void)testSerialize {
  86. // Check that serializing the UnknownFieldSet produces the original data
  87. // again.
  88. NSData* data = [emptyMessage_ data];
  89. XCTAssertEqualObjects(allFieldsData_, data);
  90. }
  91. - (void)testCopyFrom {
  92. TestEmptyMessage* message = [TestEmptyMessage message];
  93. [message mergeFrom:emptyMessage_];
  94. XCTAssertEqualObjects(emptyMessage_.data, message.data);
  95. }
  96. - (void)testMergeFrom {
  97. GPBUnknownFieldSet* set1 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  98. GPBField* field = [[[GPBField alloc] initWithNumber:2] autorelease];
  99. [field addVarint:2];
  100. [set1 addField:field];
  101. field = [[[GPBField alloc] initWithNumber:3] autorelease];
  102. [field addVarint:4];
  103. [set1 addField:field];
  104. GPBUnknownFieldSet* set2 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  105. field = [[[GPBField alloc] initWithNumber:1] autorelease];
  106. [field addVarint:1];
  107. [set2 addField:field];
  108. field = [[[GPBField alloc] initWithNumber:3] autorelease];
  109. [field addVarint:3];
  110. [set2 addField:field];
  111. GPBUnknownFieldSet* set3 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  112. field = [[[GPBField alloc] initWithNumber:1] autorelease];
  113. [field addVarint:1];
  114. [set3 addField:field];
  115. field = [[[GPBField alloc] initWithNumber:3] autorelease];
  116. [field addVarint:4];
  117. [set3 addField:field];
  118. GPBUnknownFieldSet* set4 = [[[GPBUnknownFieldSet alloc] init] autorelease];
  119. field = [[[GPBField alloc] initWithNumber:2] autorelease];
  120. [field addVarint:2];
  121. [set4 addField:field];
  122. field = [[[GPBField alloc] initWithNumber:3] autorelease];
  123. [field addVarint:3];
  124. [set4 addField:field];
  125. TestEmptyMessage* source1 = [TestEmptyMessage message];
  126. [source1 setUnknownFields:set1];
  127. TestEmptyMessage* source2 = [TestEmptyMessage message];
  128. [source2 setUnknownFields:set2];
  129. TestEmptyMessage* source3 = [TestEmptyMessage message];
  130. [source3 setUnknownFields:set3];
  131. TestEmptyMessage* source4 = [TestEmptyMessage message];
  132. [source4 setUnknownFields:set4];
  133. TestEmptyMessage* destination1 = [TestEmptyMessage message];
  134. [destination1 mergeFrom:source1];
  135. [destination1 mergeFrom:source2];
  136. TestEmptyMessage* destination2 = [TestEmptyMessage message];
  137. [destination2 mergeFrom:source3];
  138. [destination2 mergeFrom:source4];
  139. XCTAssertEqualObjects(destination1.data, destination2.data);
  140. }
  141. - (void)testClearMessage {
  142. TestEmptyMessage* message = [TestEmptyMessage message];
  143. [message mergeFrom:emptyMessage_];
  144. [message clear];
  145. XCTAssertEqual(message.serializedSize, (size_t)0);
  146. }
  147. - (void)testParseKnownAndUnknown {
  148. // Test mixing known and unknown fields when parsing.
  149. GPBUnknownFieldSet* fields = [[unknownFields_ copy] autorelease];
  150. GPBField* field = [[[GPBField alloc] initWithNumber:123456] autorelease];
  151. [field addVarint:654321];
  152. [fields addField:field];
  153. NSData* data = fields.data;
  154. TestAllTypes* destination = [TestAllTypes parseFromData:data];
  155. [self assertAllFieldsSet:destination repeatedCount:kGPBDefaultRepeatCount];
  156. XCTAssertEqual(destination.unknownFields.countOfFields, (NSUInteger)1);
  157. GPBField* field2 = [destination.unknownFields getField:123456];
  158. XCTAssertEqual(field2.varintList.count, (NSUInteger)1);
  159. XCTAssertEqual(654321ULL, [field2.varintList valueAtIndex:0]);
  160. }
  161. - (void)testWrongTypeTreatedAsUnknown {
  162. // Test that fields of the wrong wire type are treated like unknown fields
  163. // when parsing.
  164. NSData* bizarroData = [self getBizarroData];
  165. TestAllTypes* allTypesMessage = [TestAllTypes parseFromData:bizarroData];
  166. TestEmptyMessage* emptyMessage = [TestEmptyMessage parseFromData:bizarroData];
  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_];
  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];
  186. TestEmptyMessage* emptyMessage = [TestEmptyMessage parseFromData:bizarroData];
  187. // All fields should have been interpreted as unknown, so the debug strings
  188. // should be the same.
  189. XCTAssertEqualObjects(emptyMessage.data, allExtensionsMessage.data);
  190. }
  191. - (void)testLargeVarint {
  192. GPBUnknownFieldSet* fields = [[unknownFields_ copy] autorelease];
  193. GPBField* field = [[[GPBField alloc] initWithNumber:1] autorelease];
  194. [field addVarint:0x7FFFFFFFFFFFFFFFL];
  195. [fields addField:field];
  196. NSData* data = [fields data];
  197. GPBUnknownFieldSet* parsed = [[[GPBUnknownFieldSet alloc] init] autorelease];
  198. [parsed mergeFromData:data];
  199. GPBField* field2 = [parsed getField:1];
  200. XCTAssertEqual(field2.varintList.count, (NSUInteger)1);
  201. XCTAssertEqual(0x7FFFFFFFFFFFFFFFULL, [field2.varintList valueAtIndex:0]);
  202. }
  203. - (void)testMergingFields {
  204. GPBField* field1 = [[[GPBField alloc] initWithNumber:1] autorelease];
  205. [field1 addVarint:1];
  206. [field1 addFixed32:2];
  207. [field1 addFixed64:3];
  208. [field1 addLengthDelimited:[NSData dataWithBytes:"hello" length:5]];
  209. [field1 addGroup:[[unknownFields_ copy] autorelease]];
  210. GPBField* field2 = [[[GPBField alloc] initWithNumber:2] autorelease];
  211. [field2 mergeFromField:field1];
  212. XCTAssertEqualObjects(field1, field2);
  213. }
  214. @end