• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Protocol Buffers - Google's data interchange format
2// Copyright 2015 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 <objc/runtime.h>
34
35#import "GPBMessage.h"
36
37#import "google/protobuf/MapProto2Unittest.pbobjc.h"
38#import "google/protobuf/MapUnittest.pbobjc.h"
39#import "google/protobuf/Unittest.pbobjc.h"
40#import "google/protobuf/UnittestDropUnknownFields.pbobjc.h"
41#import "google/protobuf/UnittestPreserveUnknownEnum.pbobjc.h"
42#import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
43#import "google/protobuf/UnittestRuntimeProto3.pbobjc.h"
44
45static NSData *DataFromCStr(const char *str) {
46  return [NSData dataWithBytes:str length:strlen(str)];
47}
48
49@interface MessageSerializationTests : GPBTestCase
50@end
51
52@implementation MessageSerializationTests
53
54// TODO(thomasvl): Pull tests over from GPBMessageTests that are serialization
55// specific.
56
57- (void)testProto3SerializationHandlingDefaults {
58  // Proto2 covered in other tests.
59
60  Message3 *msg = [[Message3 alloc] init];
61
62  // Add defaults, no output.
63
64  NSData *data = [msg data];
65  XCTAssertEqual([data length], 0U);
66
67  // All zeros, still nothing.
68
69  msg.optionalInt32 = 0;
70  msg.optionalInt64 = 0;
71  msg.optionalUint32 = 0;
72  msg.optionalUint64 = 0;
73  msg.optionalSint32 = 0;
74  msg.optionalSint64 = 0;
75  msg.optionalFixed32 = 0;
76  msg.optionalFixed64 = 0;
77  msg.optionalSfixed32 = 0;
78  msg.optionalSfixed64 = 0;
79  msg.optionalFloat = 0.0f;
80  msg.optionalDouble = 0.0;
81  msg.optionalBool = NO;
82  msg.optionalString = @"";
83  msg.optionalBytes = [NSData data];
84  msg.optionalEnum = Message3_Enum_Foo;  // first value
85
86  data = [msg data];
87  XCTAssertEqual([data length], 0U);
88
89  // The two that also take nil as nothing.
90
91  msg.optionalString = nil;
92  msg.optionalBytes = nil;
93
94  data = [msg data];
95  XCTAssertEqual([data length], 0U);
96
97  // Set one field...
98
99  msg.optionalInt32 = 1;
100
101  data = [msg data];
102  const uint8_t expectedBytes[] = {0x08, 0x01};
103  NSData *expected = [NSData dataWithBytes:expectedBytes length:2];
104  XCTAssertEqualObjects(data, expected);
105
106  // Back to zero...
107
108  msg.optionalInt32 = 0;
109
110  data = [msg data];
111  XCTAssertEqual([data length], 0U);
112
113  [msg release];
114}
115
116- (void)testProto3DroppingUnknownFields {
117  DropUnknownsFooWithExtraFields *fooWithExtras =
118      [[DropUnknownsFooWithExtraFields alloc] init];
119
120  fooWithExtras.int32Value = 1;
121  fooWithExtras.enumValue = DropUnknownsFooWithExtraFields_NestedEnum_Baz;
122  fooWithExtras.extraInt32Value = 2;
123
124  NSData *data = [fooWithExtras data];
125  XCTAssertNotNil(data);
126  DropUnknownsFoo *foo = [DropUnknownsFoo parseFromData:data error:NULL];
127
128  XCTAssertEqual(foo.int32Value, 1);
129  XCTAssertEqual(foo.enumValue, DropUnknownsFoo_NestedEnum_Baz);
130  // Nothing should end up in the unknowns.
131  XCTAssertEqual([foo.unknownFields countOfFields], 0U);
132
133  [fooWithExtras release];
134  data = [foo data];
135  fooWithExtras =
136      [DropUnknownsFooWithExtraFields parseFromData:data error:NULL];
137  XCTAssertEqual(fooWithExtras.int32Value, 1);
138  XCTAssertEqual(fooWithExtras.enumValue,
139                 DropUnknownsFooWithExtraFields_NestedEnum_Baz);
140  // And the extra value is gone (back to the default).
141  XCTAssertEqual(fooWithExtras.extraInt32Value, 0);
142  XCTAssertEqual([foo.unknownFields countOfFields], 0U);
143}
144
145- (void)testProto2UnknownEnumToUnknownField {
146  Message3 *orig = [[Message3 alloc] init];
147
148  orig.optionalEnum = Message3_Enum_Extra3;
149  orig.repeatedEnumArray =
150      [GPBEnumArray arrayWithValidationFunction:Message3_Enum_IsValidValue
151                                       rawValue:Message3_Enum_Extra3];
152  orig.oneofEnum = Message3_Enum_Extra3;
153
154  NSData *data = [orig data];
155  XCTAssertNotNil(data);
156  Message2 *msg = [[Message2 alloc] initWithData:data error:NULL];
157
158  // None of the fields should be set.
159
160  XCTAssertFalse(msg.hasOptionalEnum);
161  XCTAssertEqual(msg.repeatedEnumArray.count, 0U);
162  XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_GPBUnsetOneOfCase);
163
164  // All the values should be in unknown fields.
165
166  GPBUnknownFieldSet *unknownFields = msg.unknownFields;
167
168  XCTAssertEqual([unknownFields countOfFields], 3U);
169  XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OptionalEnum]);
170  XCTAssertTrue(
171      [unknownFields hasField:Message2_FieldNumber_RepeatedEnumArray]);
172  XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OneofEnum]);
173
174  GPBUnknownField *field =
175      [unknownFields getField:Message2_FieldNumber_OptionalEnum];
176  XCTAssertEqual(field.varintList.count, 1U);
177  XCTAssertEqual([field.varintList valueAtIndex:0],
178                 (uint64_t)Message3_Enum_Extra3);
179
180  field = [unknownFields getField:Message2_FieldNumber_RepeatedEnumArray];
181  XCTAssertEqual(field.varintList.count, 1U);
182  XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)Message3_Enum_Extra3);
183
184  field = [unknownFields getField:Message2_FieldNumber_OneofEnum];
185  XCTAssertEqual(field.varintList.count, 1U);
186  XCTAssertEqual([field.varintList valueAtIndex:0],
187                 (uint64_t)Message3_Enum_Extra3);
188
189  [msg release];
190  [orig release];
191}
192
193- (void)testProto3UnknownEnumPreserving {
194  UnknownEnumsMyMessagePlusExtra *orig =
195      [UnknownEnumsMyMessagePlusExtra message];
196
197  orig.e = UnknownEnumsMyEnumPlusExtra_EExtra;
198  orig.repeatedEArray = [GPBEnumArray
199      arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue
200                         rawValue:UnknownEnumsMyEnumPlusExtra_EExtra];
201  orig.repeatedPackedEArray = [GPBEnumArray
202      arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue
203                         rawValue:UnknownEnumsMyEnumPlusExtra_EExtra];
204  orig.oneofE1 = UnknownEnumsMyEnumPlusExtra_EExtra;
205
206  // Everything should be there via raw values.
207
208  NSData *data = [orig data];
209  XCTAssertNotNil(data);
210  UnknownEnumsMyMessage *msg =
211      [UnknownEnumsMyMessage parseFromData:data error:NULL];
212
213  XCTAssertEqual(msg.e, UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
214  XCTAssertEqual(UnknownEnumsMyMessage_E_RawValue(msg),
215                 UnknownEnumsMyEnumPlusExtra_EExtra);
216  XCTAssertEqual(msg.repeatedEArray.count, 1U);
217  XCTAssertEqual([msg.repeatedEArray valueAtIndex:0],
218                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
219  XCTAssertEqual([msg.repeatedEArray rawValueAtIndex:0],
220                 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra);
221  XCTAssertEqual(msg.repeatedPackedEArray.count, 1U);
222  XCTAssertEqual([msg.repeatedPackedEArray valueAtIndex:0],
223                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
224  XCTAssertEqual([msg.repeatedPackedEArray rawValueAtIndex:0],
225                 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra);
226  XCTAssertEqual(msg.oneofE1,
227                 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
228  XCTAssertEqual(UnknownEnumsMyMessage_OneofE1_RawValue(msg),
229                 UnknownEnumsMyEnumPlusExtra_EExtra);
230
231  // Everything should go out and come back.
232
233  data = [msg data];
234  orig = [UnknownEnumsMyMessagePlusExtra parseFromData:data error:NULL];
235
236  XCTAssertEqual(orig.e, UnknownEnumsMyEnumPlusExtra_EExtra);
237  XCTAssertEqual(orig.repeatedEArray.count, 1U);
238  XCTAssertEqual([orig.repeatedEArray valueAtIndex:0],
239                 UnknownEnumsMyEnumPlusExtra_EExtra);
240  XCTAssertEqual(orig.repeatedPackedEArray.count, 1U);
241  XCTAssertEqual([orig.repeatedPackedEArray valueAtIndex:0],
242                 UnknownEnumsMyEnumPlusExtra_EExtra);
243  XCTAssertEqual(orig.oneofE1, UnknownEnumsMyEnumPlusExtra_EExtra);
244}
245
246//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF(MESSAGE, FIELD, VALUE)
247//%TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, )
248//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, EQ_SUFFIX)
249//%  {  // oneof##FIELD
250//%    MESSAGE *orig = [[MESSAGE alloc] init];
251//%    orig.oneof##FIELD = VALUE;
252//%    XCTAssertEqual(orig.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD);
253//%    NSData *data = [orig data];
254//%    XCTAssertNotNil(data);
255//%    MESSAGE *msg = [MESSAGE parseFromData:data error:NULL];
256//%    XCTAssertEqual(msg.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD);
257//%    XCTAssertEqual##EQ_SUFFIX(msg.oneof##FIELD, VALUE);
258//%    [orig release];
259//%  }
260//%
261//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOFS(SYNTAX, BOOL_NON_DEFAULT)
262//%- (void)testProto##SYNTAX##RoundTripOneof {
263//%
264//%GROUP_INIT##SYNTAX()  Message##SYNTAX *subMessage = [[Message##SYNTAX alloc] init];
265//%  XCTAssertNotNil(subMessage);
266//%  subMessage.optionalInt32 = 666;
267//%
268//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int32, 1)
269//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int64, 2)
270//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint32, 3U)
271//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint64, 4U)
272//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint32, 5)
273//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint64, 6)
274//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed32, 7U)
275//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed64, 8U)
276//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed32, 9)
277//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed64, 10)
278//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Float, 11.0f)
279//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Double, 12.0)
280//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Bool, BOOL_NON_DEFAULT)
281//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, String, @"foo", Objects)
282//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Bytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding], Objects)
283//%GROUP_TEST##SYNTAX()TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Message, subMessage, Objects)
284//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Enum, Message2_Enum_Bar)
285//%GROUP_CLEANUP##SYNTAX()  [subMessage release];
286//%}
287//%
288//%PDDM-DEFINE GROUP_INIT2()
289//%  Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init];
290//%  XCTAssertNotNil(group);
291//%  group.a = 777;
292//%
293//%PDDM-DEFINE GROUP_CLEANUP2()
294//%  [group release];
295//%
296//%PDDM-DEFINE GROUP_TEST2()
297//%TEST_ROUNDTRIP_ONEOF_ADV(Message2, Group, group, Objects)
298//%
299//%PDDM-DEFINE GROUP_INIT3()
300// Empty
301//%PDDM-DEFINE GROUP_CLEANUP3()
302// Empty
303//%PDDM-DEFINE GROUP_TEST3()
304//%  // Not "group" in proto3.
305//%
306//%
307//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(2, NO)
308// This block of code is generated, do not edit it directly.
309
310- (void)testProto2RoundTripOneof {
311
312  Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init];
313  XCTAssertNotNil(group);
314  group.a = 777;
315  Message2 *subMessage = [[Message2 alloc] init];
316  XCTAssertNotNil(subMessage);
317  subMessage.optionalInt32 = 666;
318
319  {  // oneofInt32
320    Message2 *orig = [[Message2 alloc] init];
321    orig.oneofInt32 = 1;
322    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
323    NSData *data = [orig data];
324    XCTAssertNotNil(data);
325    Message2 *msg = [Message2 parseFromData:data error:NULL];
326    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
327    XCTAssertEqual(msg.oneofInt32, 1);
328    [orig release];
329  }
330
331  {  // oneofInt64
332    Message2 *orig = [[Message2 alloc] init];
333    orig.oneofInt64 = 2;
334    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
335    NSData *data = [orig data];
336    XCTAssertNotNil(data);
337    Message2 *msg = [Message2 parseFromData:data error:NULL];
338    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
339    XCTAssertEqual(msg.oneofInt64, 2);
340    [orig release];
341  }
342
343  {  // oneofUint32
344    Message2 *orig = [[Message2 alloc] init];
345    orig.oneofUint32 = 3U;
346    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
347    NSData *data = [orig data];
348    XCTAssertNotNil(data);
349    Message2 *msg = [Message2 parseFromData:data error:NULL];
350    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
351    XCTAssertEqual(msg.oneofUint32, 3U);
352    [orig release];
353  }
354
355  {  // oneofUint64
356    Message2 *orig = [[Message2 alloc] init];
357    orig.oneofUint64 = 4U;
358    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
359    NSData *data = [orig data];
360    XCTAssertNotNil(data);
361    Message2 *msg = [Message2 parseFromData:data error:NULL];
362    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
363    XCTAssertEqual(msg.oneofUint64, 4U);
364    [orig release];
365  }
366
367  {  // oneofSint32
368    Message2 *orig = [[Message2 alloc] init];
369    orig.oneofSint32 = 5;
370    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
371    NSData *data = [orig data];
372    XCTAssertNotNil(data);
373    Message2 *msg = [Message2 parseFromData:data error:NULL];
374    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
375    XCTAssertEqual(msg.oneofSint32, 5);
376    [orig release];
377  }
378
379  {  // oneofSint64
380    Message2 *orig = [[Message2 alloc] init];
381    orig.oneofSint64 = 6;
382    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
383    NSData *data = [orig data];
384    XCTAssertNotNil(data);
385    Message2 *msg = [Message2 parseFromData:data error:NULL];
386    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
387    XCTAssertEqual(msg.oneofSint64, 6);
388    [orig release];
389  }
390
391  {  // oneofFixed32
392    Message2 *orig = [[Message2 alloc] init];
393    orig.oneofFixed32 = 7U;
394    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
395    NSData *data = [orig data];
396    XCTAssertNotNil(data);
397    Message2 *msg = [Message2 parseFromData:data error:NULL];
398    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
399    XCTAssertEqual(msg.oneofFixed32, 7U);
400    [orig release];
401  }
402
403  {  // oneofFixed64
404    Message2 *orig = [[Message2 alloc] init];
405    orig.oneofFixed64 = 8U;
406    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
407    NSData *data = [orig data];
408    XCTAssertNotNil(data);
409    Message2 *msg = [Message2 parseFromData:data error:NULL];
410    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
411    XCTAssertEqual(msg.oneofFixed64, 8U);
412    [orig release];
413  }
414
415  {  // oneofSfixed32
416    Message2 *orig = [[Message2 alloc] init];
417    orig.oneofSfixed32 = 9;
418    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
419    NSData *data = [orig data];
420    XCTAssertNotNil(data);
421    Message2 *msg = [Message2 parseFromData:data error:NULL];
422    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
423    XCTAssertEqual(msg.oneofSfixed32, 9);
424    [orig release];
425  }
426
427  {  // oneofSfixed64
428    Message2 *orig = [[Message2 alloc] init];
429    orig.oneofSfixed64 = 10;
430    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
431    NSData *data = [orig data];
432    XCTAssertNotNil(data);
433    Message2 *msg = [Message2 parseFromData:data error:NULL];
434    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
435    XCTAssertEqual(msg.oneofSfixed64, 10);
436    [orig release];
437  }
438
439  {  // oneofFloat
440    Message2 *orig = [[Message2 alloc] init];
441    orig.oneofFloat = 11.0f;
442    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
443    NSData *data = [orig data];
444    XCTAssertNotNil(data);
445    Message2 *msg = [Message2 parseFromData:data error:NULL];
446    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
447    XCTAssertEqual(msg.oneofFloat, 11.0f);
448    [orig release];
449  }
450
451  {  // oneofDouble
452    Message2 *orig = [[Message2 alloc] init];
453    orig.oneofDouble = 12.0;
454    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
455    NSData *data = [orig data];
456    XCTAssertNotNil(data);
457    Message2 *msg = [Message2 parseFromData:data error:NULL];
458    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
459    XCTAssertEqual(msg.oneofDouble, 12.0);
460    [orig release];
461  }
462
463  {  // oneofBool
464    Message2 *orig = [[Message2 alloc] init];
465    orig.oneofBool = NO;
466    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBool);
467    NSData *data = [orig data];
468    XCTAssertNotNil(data);
469    Message2 *msg = [Message2 parseFromData:data error:NULL];
470    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBool);
471    XCTAssertEqual(msg.oneofBool, NO);
472    [orig release];
473  }
474
475  {  // oneofString
476    Message2 *orig = [[Message2 alloc] init];
477    orig.oneofString = @"foo";
478    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofString);
479    NSData *data = [orig data];
480    XCTAssertNotNil(data);
481    Message2 *msg = [Message2 parseFromData:data error:NULL];
482    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofString);
483    XCTAssertEqualObjects(msg.oneofString, @"foo");
484    [orig release];
485  }
486
487  {  // oneofBytes
488    Message2 *orig = [[Message2 alloc] init];
489    orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
490    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
491    NSData *data = [orig data];
492    XCTAssertNotNil(data);
493    Message2 *msg = [Message2 parseFromData:data error:NULL];
494    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
495    XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
496    [orig release];
497  }
498
499  {  // oneofGroup
500    Message2 *orig = [[Message2 alloc] init];
501    orig.oneofGroup = group;
502    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
503    NSData *data = [orig data];
504    XCTAssertNotNil(data);
505    Message2 *msg = [Message2 parseFromData:data error:NULL];
506    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
507    XCTAssertEqualObjects(msg.oneofGroup, group);
508    [orig release];
509  }
510
511  {  // oneofMessage
512    Message2 *orig = [[Message2 alloc] init];
513    orig.oneofMessage = subMessage;
514    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
515    NSData *data = [orig data];
516    XCTAssertNotNil(data);
517    Message2 *msg = [Message2 parseFromData:data error:NULL];
518    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
519    XCTAssertEqualObjects(msg.oneofMessage, subMessage);
520    [orig release];
521  }
522
523  {  // oneofEnum
524    Message2 *orig = [[Message2 alloc] init];
525    orig.oneofEnum = Message2_Enum_Bar;
526    XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
527    NSData *data = [orig data];
528    XCTAssertNotNil(data);
529    Message2 *msg = [Message2 parseFromData:data error:NULL];
530    XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
531    XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
532    [orig release];
533  }
534
535  [group release];
536  [subMessage release];
537}
538
539//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(3, YES)
540// This block of code is generated, do not edit it directly.
541
542- (void)testProto3RoundTripOneof {
543
544  Message3 *subMessage = [[Message3 alloc] init];
545  XCTAssertNotNil(subMessage);
546  subMessage.optionalInt32 = 666;
547
548  {  // oneofInt32
549    Message3 *orig = [[Message3 alloc] init];
550    orig.oneofInt32 = 1;
551    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
552    NSData *data = [orig data];
553    XCTAssertNotNil(data);
554    Message3 *msg = [Message3 parseFromData:data error:NULL];
555    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
556    XCTAssertEqual(msg.oneofInt32, 1);
557    [orig release];
558  }
559
560  {  // oneofInt64
561    Message3 *orig = [[Message3 alloc] init];
562    orig.oneofInt64 = 2;
563    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
564    NSData *data = [orig data];
565    XCTAssertNotNil(data);
566    Message3 *msg = [Message3 parseFromData:data error:NULL];
567    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
568    XCTAssertEqual(msg.oneofInt64, 2);
569    [orig release];
570  }
571
572  {  // oneofUint32
573    Message3 *orig = [[Message3 alloc] init];
574    orig.oneofUint32 = 3U;
575    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
576    NSData *data = [orig data];
577    XCTAssertNotNil(data);
578    Message3 *msg = [Message3 parseFromData:data error:NULL];
579    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
580    XCTAssertEqual(msg.oneofUint32, 3U);
581    [orig release];
582  }
583
584  {  // oneofUint64
585    Message3 *orig = [[Message3 alloc] init];
586    orig.oneofUint64 = 4U;
587    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
588    NSData *data = [orig data];
589    XCTAssertNotNil(data);
590    Message3 *msg = [Message3 parseFromData:data error:NULL];
591    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
592    XCTAssertEqual(msg.oneofUint64, 4U);
593    [orig release];
594  }
595
596  {  // oneofSint32
597    Message3 *orig = [[Message3 alloc] init];
598    orig.oneofSint32 = 5;
599    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
600    NSData *data = [orig data];
601    XCTAssertNotNil(data);
602    Message3 *msg = [Message3 parseFromData:data error:NULL];
603    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
604    XCTAssertEqual(msg.oneofSint32, 5);
605    [orig release];
606  }
607
608  {  // oneofSint64
609    Message3 *orig = [[Message3 alloc] init];
610    orig.oneofSint64 = 6;
611    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
612    NSData *data = [orig data];
613    XCTAssertNotNil(data);
614    Message3 *msg = [Message3 parseFromData:data error:NULL];
615    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
616    XCTAssertEqual(msg.oneofSint64, 6);
617    [orig release];
618  }
619
620  {  // oneofFixed32
621    Message3 *orig = [[Message3 alloc] init];
622    orig.oneofFixed32 = 7U;
623    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
624    NSData *data = [orig data];
625    XCTAssertNotNil(data);
626    Message3 *msg = [Message3 parseFromData:data error:NULL];
627    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
628    XCTAssertEqual(msg.oneofFixed32, 7U);
629    [orig release];
630  }
631
632  {  // oneofFixed64
633    Message3 *orig = [[Message3 alloc] init];
634    orig.oneofFixed64 = 8U;
635    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
636    NSData *data = [orig data];
637    XCTAssertNotNil(data);
638    Message3 *msg = [Message3 parseFromData:data error:NULL];
639    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
640    XCTAssertEqual(msg.oneofFixed64, 8U);
641    [orig release];
642  }
643
644  {  // oneofSfixed32
645    Message3 *orig = [[Message3 alloc] init];
646    orig.oneofSfixed32 = 9;
647    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
648    NSData *data = [orig data];
649    XCTAssertNotNil(data);
650    Message3 *msg = [Message3 parseFromData:data error:NULL];
651    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
652    XCTAssertEqual(msg.oneofSfixed32, 9);
653    [orig release];
654  }
655
656  {  // oneofSfixed64
657    Message3 *orig = [[Message3 alloc] init];
658    orig.oneofSfixed64 = 10;
659    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
660    NSData *data = [orig data];
661    XCTAssertNotNil(data);
662    Message3 *msg = [Message3 parseFromData:data error:NULL];
663    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
664    XCTAssertEqual(msg.oneofSfixed64, 10);
665    [orig release];
666  }
667
668  {  // oneofFloat
669    Message3 *orig = [[Message3 alloc] init];
670    orig.oneofFloat = 11.0f;
671    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
672    NSData *data = [orig data];
673    XCTAssertNotNil(data);
674    Message3 *msg = [Message3 parseFromData:data error:NULL];
675    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
676    XCTAssertEqual(msg.oneofFloat, 11.0f);
677    [orig release];
678  }
679
680  {  // oneofDouble
681    Message3 *orig = [[Message3 alloc] init];
682    orig.oneofDouble = 12.0;
683    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
684    NSData *data = [orig data];
685    XCTAssertNotNil(data);
686    Message3 *msg = [Message3 parseFromData:data error:NULL];
687    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
688    XCTAssertEqual(msg.oneofDouble, 12.0);
689    [orig release];
690  }
691
692  {  // oneofBool
693    Message3 *orig = [[Message3 alloc] init];
694    orig.oneofBool = YES;
695    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBool);
696    NSData *data = [orig data];
697    XCTAssertNotNil(data);
698    Message3 *msg = [Message3 parseFromData:data error:NULL];
699    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBool);
700    XCTAssertEqual(msg.oneofBool, YES);
701    [orig release];
702  }
703
704  {  // oneofString
705    Message3 *orig = [[Message3 alloc] init];
706    orig.oneofString = @"foo";
707    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofString);
708    NSData *data = [orig data];
709    XCTAssertNotNil(data);
710    Message3 *msg = [Message3 parseFromData:data error:NULL];
711    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofString);
712    XCTAssertEqualObjects(msg.oneofString, @"foo");
713    [orig release];
714  }
715
716  {  // oneofBytes
717    Message3 *orig = [[Message3 alloc] init];
718    orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
719    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
720    NSData *data = [orig data];
721    XCTAssertNotNil(data);
722    Message3 *msg = [Message3 parseFromData:data error:NULL];
723    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
724    XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
725    [orig release];
726  }
727
728  // Not "group" in proto3.
729
730  {  // oneofMessage
731    Message3 *orig = [[Message3 alloc] init];
732    orig.oneofMessage = subMessage;
733    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
734    NSData *data = [orig data];
735    XCTAssertNotNil(data);
736    Message3 *msg = [Message3 parseFromData:data error:NULL];
737    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
738    XCTAssertEqualObjects(msg.oneofMessage, subMessage);
739    [orig release];
740  }
741
742  {  // oneofEnum
743    Message3 *orig = [[Message3 alloc] init];
744    orig.oneofEnum = Message2_Enum_Bar;
745    XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
746    NSData *data = [orig data];
747    XCTAssertNotNil(data);
748    Message3 *msg = [Message3 parseFromData:data error:NULL];
749    XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
750    XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar);
751    [orig release];
752  }
753
754  [subMessage release];
755}
756
757//%PDDM-EXPAND-END (2 expansions)
758
759- (void)testPackedUnpackedMessageParsing {
760  // packed is optional, a repeated field should parse when packed or unpacked.
761
762  TestPackedTypes *packedOrig = [TestPackedTypes message];
763  TestUnpackedTypes *unpackedOrig = [TestUnpackedTypes message];
764  [self setPackedFields:packedOrig repeatedCount:4];
765  [self setUnpackedFields:unpackedOrig repeatedCount:4];
766
767  NSData *packedData = [packedOrig data];
768  NSData *unpackedData = [unpackedOrig data];
769  XCTAssertNotNil(packedData);
770  XCTAssertNotNil(unpackedData);
771  XCTAssertNotEqualObjects(packedData, unpackedData,
772                           @"Data should differ (packed vs unpacked) use");
773
774  NSError *error = nil;
775  TestPackedTypes *packedParse =
776      [TestPackedTypes parseFromData:unpackedData error:&error];
777  XCTAssertNotNil(packedParse);
778  XCTAssertNil(error);
779  XCTAssertEqualObjects(packedParse, packedOrig);
780
781  error = nil;
782  TestUnpackedTypes *unpackedParsed =
783      [TestUnpackedTypes parseFromData:packedData error:&error];
784  XCTAssertNotNil(unpackedParsed);
785  XCTAssertNil(error);
786  XCTAssertEqualObjects(unpackedParsed, unpackedOrig);
787}
788
789- (void)testPackedUnpackedExtensionParsing {
790  // packed is optional, a repeated extension should parse when packed or
791  // unpacked.
792
793  TestPackedExtensions *packedOrig = [TestPackedExtensions message];
794  TestUnpackedExtensions *unpackedOrig = [TestUnpackedExtensions message];
795  [self setPackedExtensions:packedOrig repeatedCount:kGPBDefaultRepeatCount];
796  [self setUnpackedExtensions:unpackedOrig repeatedCount:kGPBDefaultRepeatCount];
797
798  NSData *packedData = [packedOrig data];
799  NSData *unpackedData = [unpackedOrig data];
800  XCTAssertNotNil(packedData);
801  XCTAssertNotNil(unpackedData);
802  XCTAssertNotEqualObjects(packedData, unpackedData,
803                           @"Data should differ (packed vs unpacked) use");
804
805  NSError *error = nil;
806  TestPackedExtensions *packedParse =
807      [TestPackedExtensions parseFromData:unpackedData
808                        extensionRegistry:[UnittestRoot extensionRegistry]
809                                    error:&error];
810  XCTAssertNotNil(packedParse);
811  XCTAssertNil(error);
812  XCTAssertEqualObjects(packedParse, packedOrig);
813
814  error = nil;
815  TestUnpackedExtensions *unpackedParsed =
816      [TestUnpackedExtensions parseFromData:packedData
817                          extensionRegistry:[UnittestRoot extensionRegistry]
818                                      error:&error];
819  XCTAssertNotNil(unpackedParsed);
820  XCTAssertNil(error);
821  XCTAssertEqualObjects(unpackedParsed, unpackedOrig);
822}
823
824- (void)testPackedExtensionVsFieldParsing {
825  // Extensions and fields end up on the wire the same way, so they can parse
826  // each other.
827
828  TestPackedTypes *fieldsOrig = [TestPackedTypes message];
829  TestPackedExtensions *extsOrig = [TestPackedExtensions message];
830  [self setPackedFields:fieldsOrig repeatedCount:kGPBDefaultRepeatCount];
831  [self setPackedExtensions:extsOrig repeatedCount:kGPBDefaultRepeatCount];
832
833  NSData *fieldsData = [fieldsOrig data];
834  NSData *extsData = [extsOrig data];
835  XCTAssertNotNil(fieldsData);
836  XCTAssertNotNil(extsData);
837  XCTAssertEqualObjects(fieldsData, extsData);
838
839  NSError *error = nil;
840  TestPackedTypes *fieldsParse =
841      [TestPackedTypes parseFromData:extsData error:&error];
842  XCTAssertNotNil(fieldsParse);
843  XCTAssertNil(error);
844  XCTAssertEqualObjects(fieldsParse, fieldsOrig);
845
846  error = nil;
847  TestPackedExtensions *extsParse =
848      [TestPackedExtensions parseFromData:fieldsData
849                        extensionRegistry:[UnittestRoot extensionRegistry]
850                                    error:&error];
851  XCTAssertNotNil(extsParse);
852  XCTAssertNil(error);
853  XCTAssertEqualObjects(extsParse, extsOrig);
854}
855
856- (void)testUnpackedExtensionVsFieldParsing {
857  // Extensions and fields end up on the wire the same way, so they can parse
858  // each other.
859
860  TestUnpackedTypes *fieldsOrig = [TestUnpackedTypes message];
861  TestUnpackedExtensions *extsOrig = [TestUnpackedExtensions message];
862  [self setUnpackedFields:fieldsOrig repeatedCount:3];
863  [self setUnpackedExtensions:extsOrig repeatedCount:3];
864
865  NSData *fieldsData = [fieldsOrig data];
866  NSData *extsData = [extsOrig data];
867  XCTAssertNotNil(fieldsData);
868  XCTAssertNotNil(extsData);
869  XCTAssertEqualObjects(fieldsData, extsData);
870
871  TestUnpackedTypes *fieldsParse =
872      [TestUnpackedTypes parseFromData:extsData error:NULL];
873  XCTAssertNotNil(fieldsParse);
874  XCTAssertEqualObjects(fieldsParse, fieldsOrig);
875
876  TestUnpackedExtensions *extsParse =
877      [TestUnpackedExtensions parseFromData:fieldsData
878                          extensionRegistry:[UnittestRoot extensionRegistry]
879                                      error:NULL];
880  XCTAssertNotNil(extsParse);
881  XCTAssertEqualObjects(extsParse, extsOrig);
882}
883
884- (void)testErrorSubsectionInvalidLimit {
885  NSData *data = DataFromCStr(
886      "\x0A\x08\x0A\x07\x12\x04\x72\x02\x4B\x50\x12\x04\x72\x02\x4B\x50");
887  NSError *error = nil;
888  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
889                                                        error:&error];
890  XCTAssertNil(msg);
891  XCTAssertNotNil(error);
892  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
893  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSubsectionLimit);
894}
895
896- (void)testErrorSubsectionLimitReached {
897  NSData *data = DataFromCStr("\x0A\x06\x12\x03\x72\x02\x4B\x50");
898  NSError *error = nil;
899  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
900                                                        error:&error];
901  XCTAssertNil(msg);
902  XCTAssertNotNil(error);
903  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
904  XCTAssertEqual(error.code, GPBCodedInputStreamErrorSubsectionLimitReached);
905}
906
907- (void)testErrorInvalidVarint {
908  NSData *data = DataFromCStr("\x72\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
909  NSError *error = nil;
910  TestAllTypes *msg = [TestAllTypes parseFromData:data error:&error];
911  XCTAssertNil(msg);
912  XCTAssertNotNil(error);
913  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
914  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidVarInt);
915}
916
917- (void)testErrorInvalidUTF8 {
918  NSData *data = DataFromCStr("\x72\x04\xF4\xFF\xFF\xFF");
919  NSError *error = nil;
920  TestAllTypes *msg = [TestAllTypes parseFromData:data error:&error];
921  XCTAssertNil(msg);
922  XCTAssertNotNil(error);
923  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
924  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidUTF8);
925}
926
927- (void)testErrorInvalidSize {
928  NSData *data = DataFromCStr("\x72\x03\x4B\x50");
929  NSError *error = nil;
930  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
931                                                        error:&error];
932  XCTAssertNil(msg);
933  XCTAssertNotNil(error);
934  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
935  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSize);
936}
937
938- (void)testErrorInvalidTag {
939  NSData *data = DataFromCStr("\x0F");
940  NSError *error = nil;
941  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
942                                                        error:&error];
943  XCTAssertNil(msg);
944  XCTAssertNotNil(error);
945  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
946  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidTag);
947}
948
949- (void)testErrorRecursionDepthReached {
950  NSData *data = DataFromCStr(
951      "\x0A\x86\x01\x0A\x83\x01\x0A\x80\x01\x0A\x7E\x0A\x7C\x0A\x7A\x0A\x78"
952      "\x0A\x76\x0A\x74\x0A\x72\x0A\x70\x0A\x6E\x0A\x6C\x0A\x6A\x0A\x68"
953      "\x0A\x66\x0A\x64\x0A\x62\x0A\x60\x0A\x5E\x0A\x5C\x0A\x5A\x0A\x58"
954      "\x0A\x56\x0A\x54\x0A\x52\x0A\x50\x0A\x4E\x0A\x4C\x0A\x4A\x0A\x48"
955      "\x0A\x46\x0A\x44\x0A\x42\x0A\x40\x0A\x3E\x0A\x3C\x0A\x3A\x0A\x38"
956      "\x0A\x36\x0A\x34\x0A\x32\x0A\x30\x0A\x2E\x0A\x2C\x0A\x2A\x0A\x28"
957      "\x0A\x26\x0A\x24\x0A\x22\x0A\x20\x0A\x1E\x0A\x1C\x0A\x1A\x0A\x18"
958      "\x0A\x16\x0A\x14\x0A\x12\x0A\x10\x0A\x0E\x0A\x0C\x0A\x0A\x0A\x08"
959      "\x0A\x06\x12\x04\x72\x02\x4B\x50");
960  NSError *error = nil;
961  NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data
962                                                        error:&error];
963  XCTAssertNil(msg);
964  XCTAssertNotNil(error);
965  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
966  XCTAssertEqual(error.code, GPBCodedInputStreamErrorRecursionDepthExceeded);
967}
968
969#ifdef DEBUG
970- (void)testErrorMissingRequiredField {
971  NSData *data = DataFromCStr("");
972  NSError *error = nil;
973  TestRequired *msg = [TestRequired parseFromData:data error:&error];
974  XCTAssertNil(msg);
975  XCTAssertNotNil(error);
976  XCTAssertEqualObjects(error.domain, GPBMessageErrorDomain);
977  XCTAssertEqual(error.code, GPBMessageErrorCodeMissingRequiredField);
978}
979#endif
980
981#pragma mark - Subset from from map_tests.cc
982
983// TEST(GeneratedMapFieldTest, StandardWireFormat)
984- (void)testMap_StandardWireFormat {
985  NSData *data = DataFromCStr("\x0A\x04\x08\x01\x10\x01");
986
987  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
988  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
989  int32_t val = 666;
990  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
991  XCTAssertEqual(val, 1);
992
993  [msg release];
994}
995
996// TEST(GeneratedMapFieldTest, UnorderedWireFormat)
997- (void)testMap_UnorderedWireFormat {
998  // put value before key in wire format
999  NSData *data = DataFromCStr("\x0A\x04\x10\x01\x08\x02");
1000
1001  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1002  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1003  int32_t val = 666;
1004  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
1005  XCTAssertEqual(val, 1);
1006
1007  [msg release];
1008}
1009
1010// TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat)
1011- (void)testMap_DuplicatedKeyWireFormat {
1012  // Two key fields in wire format
1013  NSData *data = DataFromCStr("\x0A\x06\x08\x01\x08\x02\x10\x01");
1014
1015  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1016  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1017  int32_t val = 666;
1018  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
1019  XCTAssertEqual(val, 1);
1020
1021  [msg release];
1022}
1023
1024// TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat)
1025- (void)testMap_DuplicatedValueWireFormat {
1026  // Two value fields in wire format
1027  NSData *data = DataFromCStr("\x0A\x06\x08\x01\x10\x01\x10\x02");
1028
1029  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1030  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1031  int32_t val = 666;
1032  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
1033  XCTAssertEqual(val, 2);
1034
1035  [msg release];
1036}
1037
1038// TEST(GeneratedMapFieldTest, MissedKeyWireFormat)
1039- (void)testMap_MissedKeyWireFormat {
1040  // No key field in wire format
1041  NSData *data = DataFromCStr("\x0A\x02\x10\x01");
1042
1043  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1044  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1045  int32_t val = 666;
1046  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:0]);
1047  XCTAssertEqual(val, 1);
1048
1049  [msg release];
1050}
1051
1052// TEST(GeneratedMapFieldTest, MissedValueWireFormat)
1053- (void)testMap_MissedValueWireFormat {
1054  // No value field in wire format
1055  NSData *data = DataFromCStr("\x0A\x02\x08\x01");
1056
1057  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1058  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1059  int32_t val = 666;
1060  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]);
1061  XCTAssertEqual(val, 0);
1062
1063  [msg release];
1064}
1065
1066// TEST(GeneratedMapFieldTest, UnknownFieldWireFormat)
1067- (void)testMap_UnknownFieldWireFormat {
1068  // Unknown field in wire format
1069  NSData *data = DataFromCStr("\x0A\x06\x08\x02\x10\x03\x18\x01");
1070
1071  TestMap *msg = [[TestMap alloc] initWithData:data error:NULL];
1072  XCTAssertEqual(msg.mapInt32Int32.count, 1U);
1073  int32_t val = 666;
1074  XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]);
1075  XCTAssertEqual(val, 3);
1076
1077  [msg release];
1078}
1079
1080// TEST(GeneratedMapFieldTest, CorruptedWireFormat)
1081- (void)testMap_CorruptedWireFormat {
1082  // corrupted data in wire format
1083  NSData *data = DataFromCStr("\x0A\x06\x08\x02\x11\x03");
1084
1085  NSError *error = nil;
1086  TestMap *msg = [TestMap parseFromData:data error:&error];
1087  XCTAssertNil(msg);
1088  XCTAssertNotNil(error);
1089  XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain);
1090  XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSubsectionLimit);
1091}
1092
1093// TEST(GeneratedMapFieldTest, Proto2UnknownEnum)
1094- (void)testMap_Proto2UnknownEnum {
1095  TestEnumMapPlusExtra *orig = [[TestEnumMapPlusExtra alloc] init];
1096
1097  orig.knownMapField = [GPBInt32EnumDictionary
1098      dictionaryWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue];
1099  orig.unknownMapField = [GPBInt32EnumDictionary
1100      dictionaryWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue];
1101  [orig.knownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumFoo
1102                       forKey:0];
1103  [orig.unknownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumExtra
1104                         forKey:0];
1105
1106  NSData *data = [orig data];
1107  XCTAssertNotNil(data);
1108  TestEnumMap *msg1 = [TestEnumMap parseFromData:data error:NULL];
1109  XCTAssertEqual(msg1.knownMapField.count, 1U);
1110  int32_t val = -1;
1111  XCTAssertTrue([msg1.knownMapField getEnum:&val forKey:0]);
1112  XCTAssertEqual(val, Proto2MapEnum_Proto2MapEnumFoo);
1113  XCTAssertEqual(msg1.unknownFields.countOfFields, 1U);
1114
1115  data = [msg1 data];
1116  TestEnumMapPlusExtra *msg2 =
1117      [TestEnumMapPlusExtra parseFromData:data error:NULL];
1118  val = -1;
1119  XCTAssertEqual(msg2.knownMapField.count, 1U);
1120  XCTAssertTrue([msg2.knownMapField getEnum:&val forKey:0]);
1121  XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumFoo);
1122  val = -1;
1123  XCTAssertEqual(msg2.unknownMapField.count, 1U);
1124  XCTAssertTrue([msg2.unknownMapField getEnum:&val forKey:0]);
1125  XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumExtra);
1126  XCTAssertEqual(msg2.unknownFields.countOfFields, 0U);
1127
1128  XCTAssertEqualObjects(orig, msg2);
1129
1130  [orig release];
1131}
1132
1133#pragma mark - Map Round Tripping
1134
1135- (void)testProto2MapRoundTripping {
1136  Message2 *msg = [[Message2 alloc] init];
1137
1138  // Key/Value data should result in different byte lengths on wire to ensure
1139  // everything is right.
1140  [msg.mapInt32Int32 setInt32:1000 forKey:200];
1141  [msg.mapInt32Int32 setInt32:101 forKey:2001];
1142  [msg.mapInt64Int64 setInt64:1002 forKey:202];
1143  [msg.mapInt64Int64 setInt64:103 forKey:2003];
1144  [msg.mapUint32Uint32 setUInt32:1004 forKey:204];
1145  [msg.mapUint32Uint32 setUInt32:105 forKey:2005];
1146  [msg.mapUint64Uint64 setUInt64:1006 forKey:206];
1147  [msg.mapUint64Uint64 setUInt64:107 forKey:2007];
1148  [msg.mapSint32Sint32 setInt32:1008 forKey:208];
1149  [msg.mapSint32Sint32 setInt32:109 forKey:2009];
1150  [msg.mapSint64Sint64 setInt64:1010 forKey:210];
1151  [msg.mapSint64Sint64 setInt64:111 forKey:2011];
1152  [msg.mapFixed32Fixed32 setUInt32:1012 forKey:212];
1153  [msg.mapFixed32Fixed32 setUInt32:113 forKey:2013];
1154  [msg.mapFixed64Fixed64 setUInt64:1014 forKey:214];
1155  [msg.mapFixed64Fixed64 setUInt64:115 forKey:2015];
1156  [msg.mapSfixed32Sfixed32 setInt32:1016 forKey:216];
1157  [msg.mapSfixed32Sfixed32 setInt32:117 forKey:2017];
1158  [msg.mapSfixed64Sfixed64 setInt64:1018 forKey:218];
1159  [msg.mapSfixed64Sfixed64 setInt64:119 forKey:2019];
1160  [msg.mapInt32Float setFloat:1020.f forKey:220];
1161  [msg.mapInt32Float setFloat:121.f forKey:2021];
1162  [msg.mapInt32Double setDouble:1022. forKey:222];
1163  [msg.mapInt32Double setDouble:123. forKey:2023];
1164  [msg.mapBoolBool setBool:false forKey:true];
1165  [msg.mapBoolBool setBool:true forKey:false];
1166  msg.mapStringString[@"224"] = @"1024";
1167  msg.mapStringString[@"2025"] = @"125";
1168  msg.mapStringBytes[@"226"] = DataFromCStr("1026");
1169  msg.mapStringBytes[@"2027"] = DataFromCStr("127");
1170  Message2 *val1 = [[Message2 alloc] init];
1171  val1.optionalInt32 = 1028;
1172  Message2 *val2 = [[Message2 alloc] init];
1173  val2.optionalInt32 = 129;
1174  [msg.mapStringMessage setObject:val1 forKey:@"228"];
1175  [msg.mapStringMessage setObject:val2 forKey:@"2029"];
1176  [msg.mapInt32Bytes setObject:DataFromCStr("1030 bytes") forKey:230];
1177  [msg.mapInt32Bytes setObject:DataFromCStr("131") forKey:2031];
1178  [msg.mapInt32Enum setEnum:Message2_Enum_Bar forKey:232];
1179  [msg.mapInt32Enum setEnum:Message2_Enum_Baz forKey:2033];
1180  Message2 *val3 = [[Message2 alloc] init];
1181  val3.optionalInt32 = 1034;
1182  Message2 *val4 = [[Message2 alloc] init];
1183  val4.optionalInt32 = 135;
1184  [msg.mapInt32Message setObject:val3 forKey:234];
1185  [msg.mapInt32Message setObject:val4 forKey:2035];
1186
1187  NSData *data = [msg data];
1188  XCTAssertNotNil(data);
1189  Message2 *msg2 = [[Message2 alloc] initWithData:data error:NULL];
1190
1191  XCTAssertNotEqual(msg2, msg);  // Pointer comparison
1192  XCTAssertEqualObjects(msg2, msg);
1193
1194  [val4 release];
1195  [val3 release];
1196  [val2 release];
1197  [val1 release];
1198  [msg2 release];
1199  [msg release];
1200}
1201
1202@end
1203