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