• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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- (GPBUnknownField *)getField:(int32_t)number {
64  return [unknownFields_ getField:number];
65}
66
67// Constructs a protocol buffer which contains fields with all the same
68// numbers as allFieldsData except that each field is some other wire
69// type.
70- (NSData*)getBizarroData {
71  GPBUnknownFieldSet* bizarroFields =
72      [[[GPBUnknownFieldSet alloc] init] autorelease];
73  NSUInteger count = [unknownFields_ countOfFields];
74  int32_t tags[count];
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];
85    } else {
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];
91    }
92  }
93
94  return [bizarroFields data];
95}
96
97- (void)testSerialize {
98  // Check that serializing the UnknownFieldSet produces the original data
99  // again.
100  NSData* data = [emptyMessage_ data];
101  XCTAssertEqualObjects(allFieldsData_, data);
102}
103
104- (void)testCopyFrom {
105  TestEmptyMessage* message = [TestEmptyMessage message];
106  [message mergeFrom:emptyMessage_];
107
108  XCTAssertEqualObjects(emptyMessage_.data, message.data);
109}
110
111- (void)testMergeFrom {
112  GPBUnknownFieldSet* set1 = [[[GPBUnknownFieldSet alloc] init] autorelease];
113  GPBUnknownField* field = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
114  [field addVarint:2];
115  [set1 addField:field];
116  field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
117  [field addVarint:4];
118  [set1 addField:field];
119
120  GPBUnknownFieldSet* set2 = [[[GPBUnknownFieldSet alloc] init] autorelease];
121  field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
122  [field addVarint:1];
123  [set2 addField:field];
124  field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
125  [field addVarint:3];
126  [set2 addField:field];
127
128  GPBUnknownFieldSet* set3 = [[[GPBUnknownFieldSet alloc] init] autorelease];
129  field = [[[GPBUnknownField alloc] initWithNumber:1] autorelease];
130  [field addVarint:1];
131  [set3 addField:field];
132  field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
133  [field addVarint:4];
134  [set3 addField:field];
135
136  GPBUnknownFieldSet* set4 = [[[GPBUnknownFieldSet alloc] init] autorelease];
137  field = [[[GPBUnknownField alloc] initWithNumber:2] autorelease];
138  [field addVarint:2];
139  [set4 addField:field];
140  field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
141  [field addVarint:3];
142  [set4 addField:field];
143
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];
152
153  TestEmptyMessage* destination1 = [TestEmptyMessage message];
154  [destination1 mergeFrom:source1];
155  [destination1 mergeFrom:source2];
156
157  TestEmptyMessage* destination2 = [TestEmptyMessage message];
158  [destination2 mergeFrom:source3];
159  [destination2 mergeFrom:source4];
160
161  XCTAssertEqualObjects(destination1.data, destination2.data);
162}
163
164- (void)testClearMessage {
165  TestEmptyMessage *message = [TestEmptyMessage message];
166  [message mergeFrom:emptyMessage_];
167  [message clear];
168  XCTAssertEqual(message.serializedSize, (size_t)0);
169}
170
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];
178
179  NSData* data = fields.data;
180  TestAllTypes* destination = [TestAllTypes parseFromData:data error:NULL];
181
182  [self assertAllFieldsSet:destination repeatedCount:kGPBDefaultRepeatCount];
183  XCTAssertEqual(destination.unknownFields.countOfFields, (NSUInteger)1);
184
185  GPBUnknownField* field2 = [destination.unknownFields getField:123456];
186  XCTAssertEqual(field2.varintList.count, (NSUInteger)1);
187  XCTAssertEqual(654321ULL, [field2.varintList valueAtIndex:0]);
188}
189
190- (void)testWrongTypeTreatedAsUnknown {
191  // Test that fields of the wrong wire type are treated like unknown fields
192  // when parsing.
193
194  NSData* bizarroData = [self getBizarroData];
195  TestAllTypes* allTypesMessage =
196      [TestAllTypes parseFromData:bizarroData error:NULL];
197  TestEmptyMessage* emptyMessage =
198      [TestEmptyMessage parseFromData:bizarroData error:NULL];
199
200  // All fields should have been interpreted as unknown, so the debug strings
201  // should be the same.
202  XCTAssertEqualObjects(emptyMessage.data, allTypesMessage.data);
203}
204
205- (void)testUnknownExtensions {
206  // Make sure fields are properly parsed to the UnknownFieldSet even when
207  // they are declared as extension numbers.
208
209  TestEmptyMessageWithExtensions* message =
210      [TestEmptyMessageWithExtensions parseFromData:allFieldsData_ error:NULL];
211
212  XCTAssertEqual(unknownFields_.countOfFields,
213                 message.unknownFields.countOfFields);
214  XCTAssertEqualObjects(allFieldsData_, message.data);
215}
216
217- (void)testWrongExtensionTypeTreatedAsUnknown {
218  // Test that fields of the wrong wire type are treated like unknown fields
219  // when parsing extensions.
220
221  NSData* bizarroData = [self getBizarroData];
222  TestAllExtensions* allExtensionsMessage =
223      [TestAllExtensions parseFromData:bizarroData error:NULL];
224  TestEmptyMessage* emptyMessage =
225      [TestEmptyMessage parseFromData:bizarroData error:NULL];
226
227  // All fields should have been interpreted as unknown, so the debug strings
228  // should be the same.
229  XCTAssertEqualObjects(emptyMessage.data, allExtensionsMessage.data);
230}
231
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];
237
238  NSData* data = [fields data];
239
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]);
245}
246
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);
257}
258
259@end
260