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