1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import "GPBTestUtilities.h"
33 #import "GPBUnknownField_PackagePrivate.h"
34 #import "GPBUnknownFieldSet_PackagePrivate.h"
35 #import "google/protobuf/Unittest.pbobjc.h"
37 @interface GPBUnknownFieldSet (GPBUnknownFieldSetTest)
38 - (void)getTags:(int32_t*)tags;
41 @interface UnknownFieldSetTest : GPBTestCase {
43 TestAllTypes* allFields_;
44 NSData* allFieldsData_;
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_;
54 @implementation UnknownFieldSetTest
57 allFields_ = [self allSetRepeatedCount:kGPBDefaultRepeatCount];
58 allFieldsData_ = [allFields_ data];
59 emptyMessage_ = [TestEmptyMessage parseFromData:allFieldsData_ error:NULL];
60 unknownFields_ = emptyMessage_.unknownFields;
63 - (GPBUnknownField *)getField:(int32_t)number {
64 return [unknownFields_ getField:number];
67 // Constructs a protocol buffer which contains fields with all the same
68 // numbers as allFieldsData except that each field is some other wire
70 - (NSData*)getBizarroData {
71 GPBUnknownFieldSet* bizarroFields =
72 [[[GPBUnknownFieldSet alloc] init] autorelease];
73 NSUInteger count = [unknownFields_ countOfFields];
75 [unknownFields_ getTags:tags];
76 for (NSUInteger i = 0; i < count; ++i) {
77 int32_t tag = tags[i];
78 GPBUnknownField* field = [unknownFields_ getField:tag];
79 if (field.varintList.count == 0) {
80 // Original field is not a varint, so use a varint.
81 GPBUnknownField* varintField =
82 [[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
83 [varintField addVarint:1];
84 [bizarroFields addField:varintField];
86 // Original field *is* a varint, so use something else.
87 GPBUnknownField* fixed32Field =
88 [[[GPBUnknownField alloc] initWithNumber:tag] autorelease];
89 [fixed32Field addFixed32:1];
90 [bizarroFields addField:fixed32Field];
94 return [bizarroFields data];
97 - (void)testSerialize {
98 // Check that serializing the UnknownFieldSet produces the original data
100 NSData* data = [emptyMessage_ data];
101 XCTAssertEqualObjects(allFieldsData_, data);
104 - (void)testCopyFrom {
105 TestEmptyMessage* message = [TestEmptyMessage message];
106 [message mergeFrom:emptyMessage_];
108 XCTAssertEqualObjects(emptyMessage_.data, message.data);
111 - (void)testMergeFrom {
112 GPBUnknownFieldSet* set1 = [[[GPBUnknownFieldSet alloc] init] autorelease];
113 GPBUnknownField* field = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
115 [set1 addField:field];
116 field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
118 [set1 addField:field];
120 GPBUnknownFieldSet* set2 = [[[GPBUnknownFieldSet alloc] init] autorelease];
121 field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
123 [set2 addField:field];
124 field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
126 [set2 addField:field];
128 GPBUnknownFieldSet* set3 = [[[GPBUnknownFieldSet alloc] init] autorelease];
129 field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
131 [set3 addField:field];
132 field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
134 [set3 addField:field];
136 GPBUnknownFieldSet* set4 = [[[GPBUnknownFieldSet alloc] init] autorelease];
137 field = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
139 [set4 addField:field];
140 field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
142 [set4 addField:field];
144 TestEmptyMessage* source1 = [TestEmptyMessage message];
145 [source1 setUnknownFields:set1];
146 TestEmptyMessage* source2 = [TestEmptyMessage message];
147 [source2 setUnknownFields:set2];
148 TestEmptyMessage* source3 = [TestEmptyMessage message];
149 [source3 setUnknownFields:set3];
150 TestEmptyMessage* source4 = [TestEmptyMessage message];
151 [source4 setUnknownFields:set4];
153 TestEmptyMessage* destination1 = [TestEmptyMessage message];
154 [destination1 mergeFrom:source1];
155 [destination1 mergeFrom:source2];
157 TestEmptyMessage* destination2 = [TestEmptyMessage message];
158 [destination2 mergeFrom:source3];
159 [destination2 mergeFrom:source4];
161 XCTAssertEqualObjects(destination1.data, destination2.data);
164 - (void)testClearMessage {
165 TestEmptyMessage *message = [TestEmptyMessage message];
166 [message mergeFrom:emptyMessage_];
168 XCTAssertEqual(message.serializedSize, (size_t)0);
171 - (void)testParseKnownAndUnknown {
172 // Test mixing known and unknown fields when parsing.
173 GPBUnknownFieldSet *fields = [[unknownFields_ copy] autorelease];
174 GPBUnknownField *field =
175 [[[GPBUnknownField alloc] initWithNumber:123456] autorelease];
176 [field addVarint:654321];
177 [fields addField:field];
179 NSData* data = fields.data;
180 TestAllTypes* destination = [TestAllTypes parseFromData:data error:NULL];
182 [self assertAllFieldsSet:destination repeatedCount:kGPBDefaultRepeatCount];
183 XCTAssertEqual(destination.unknownFields.countOfFields, (NSUInteger)1);
185 GPBUnknownField* field2 = [destination.unknownFields getField:123456];
186 XCTAssertEqual(field2.varintList.count, (NSUInteger)1);
187 XCTAssertEqual(654321ULL, [field2.varintList valueAtIndex:0]);
190 - (void)testWrongTypeTreatedAsUnknown {
191 // Test that fields of the wrong wire type are treated like unknown fields
194 NSData* bizarroData = [self getBizarroData];
195 TestAllTypes* allTypesMessage =
196 [TestAllTypes parseFromData:bizarroData error:NULL];
197 TestEmptyMessage* emptyMessage =
198 [TestEmptyMessage parseFromData:bizarroData error:NULL];
200 // All fields should have been interpreted as unknown, so the debug strings
201 // should be the same.
202 XCTAssertEqualObjects(emptyMessage.data, allTypesMessage.data);
205 - (void)testUnknownExtensions {
206 // Make sure fields are properly parsed to the UnknownFieldSet even when
207 // they are declared as extension numbers.
209 TestEmptyMessageWithExtensions* message =
210 [TestEmptyMessageWithExtensions parseFromData:allFieldsData_ error:NULL];
212 XCTAssertEqual(unknownFields_.countOfFields,
213 message.unknownFields.countOfFields);
214 XCTAssertEqualObjects(allFieldsData_, message.data);
217 - (void)testWrongExtensionTypeTreatedAsUnknown {
218 // Test that fields of the wrong wire type are treated like unknown fields
219 // when parsing extensions.
221 NSData* bizarroData = [self getBizarroData];
222 TestAllExtensions* allExtensionsMessage =
223 [TestAllExtensions parseFromData:bizarroData error:NULL];
224 TestEmptyMessage* emptyMessage =
225 [TestEmptyMessage parseFromData:bizarroData error:NULL];
227 // All fields should have been interpreted as unknown, so the debug strings
228 // should be the same.
229 XCTAssertEqualObjects(emptyMessage.data, allExtensionsMessage.data);
232 - (void)testLargeVarint {
233 GPBUnknownFieldSet* fields = [[unknownFields_ copy] autorelease];
234 GPBUnknownField* field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
235 [field addVarint:0x7FFFFFFFFFFFFFFFL];
236 [fields addField:field];
238 NSData* data = [fields data];
240 GPBUnknownFieldSet* parsed = [[[GPBUnknownFieldSet alloc] init] autorelease];
241 [parsed mergeFromData:data];
242 GPBUnknownField* field2 = [parsed getField:1];
243 XCTAssertEqual(field2.varintList.count, (NSUInteger)1);
244 XCTAssertEqual(0x7FFFFFFFFFFFFFFFULL, [field2.varintList valueAtIndex:0]);
247 - (void)testMergingFields {
248 GPBUnknownField* field1 = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
249 [field1 addVarint:1];
250 [field1 addFixed32:2];
251 [field1 addFixed64:3];
252 [field1 addLengthDelimited:[NSData dataWithBytes:"hello" length:5]];
253 [field1 addGroup:[[unknownFields_ copy] autorelease]];
254 GPBUnknownField* field2 = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
255 [field2 mergeFromField:field1];
256 XCTAssertEqualObjects(field1, field2);