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)testProto3SerializationHandlingOptionals { 113 // 114 // Proto3 optionals should be just like proto2, zero values also get serialized. 115 // 116 117//%PDDM-DEFINE PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(FIELD, ZERO_VALUE, EXPECTED_LEN) 118//% { // optional##FIELD 119//% Message3Optional *msg = [[Message3Optional alloc] init]; 120//% NSData *data = [msg data]; 121//% XCTAssertEqual([data length], 0U); 122//% msg.optional##FIELD = ZERO_VALUE; 123//% data = [msg data]; 124//% XCTAssertEqual(data.length, EXPECTED_LEN); 125//% NSError *err = nil; 126//% Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 127//% XCTAssertNotNil(msg2); 128//% XCTAssertNil(err); 129//% XCTAssertTrue(msg2.hasOptional##FIELD); 130//% XCTAssertEqualObjects(msg, msg2); 131//% [msg release]; 132//% } 133//% 134//%PDDM-DEFINE PROTO3_TEST_SERIALIZE_OPTIONAL_FIELDS() 135//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Int32, 0, 2) 136//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Int64, 0, 2) 137//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Uint32, 0, 2) 138//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Uint64, 0, 2) 139//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sint32, 0, 2) 140//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sint64, 0, 2) 141//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Fixed32, 0, 5) 142//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Fixed64, 0, 9) 143//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sfixed32, 0, 5) 144//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Sfixed64, 0, 9) 145//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Float, 0.0f, 5) 146//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Double, 0.0, 9) 147//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Bool, NO, 2) 148//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(String, @"", 2) 149//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Bytes, [NSData data], 2) 150//% // 151//% // Test doesn't apply to optionalMessage (no groups in proto3). 152//% // 153//% 154//%PROTO3_TEST_SERIALIZE_OPTIONAL_FIELD(Enum, Message3Optional_Enum_Foo, 3) 155//%PDDM-EXPAND PROTO3_TEST_SERIALIZE_OPTIONAL_FIELDS() 156// This block of code is generated, do not edit it directly. 157// clang-format off 158 159 { // optionalInt32 160 Message3Optional *msg = [[Message3Optional alloc] init]; 161 NSData *data = [msg data]; 162 XCTAssertEqual([data length], 0U); 163 msg.optionalInt32 = 0; 164 data = [msg data]; 165 XCTAssertEqual(data.length, 2); 166 NSError *err = nil; 167 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 168 XCTAssertNotNil(msg2); 169 XCTAssertNil(err); 170 XCTAssertTrue(msg2.hasOptionalInt32); 171 XCTAssertEqualObjects(msg, msg2); 172 [msg release]; 173 } 174 175 { // optionalInt64 176 Message3Optional *msg = [[Message3Optional alloc] init]; 177 NSData *data = [msg data]; 178 XCTAssertEqual([data length], 0U); 179 msg.optionalInt64 = 0; 180 data = [msg data]; 181 XCTAssertEqual(data.length, 2); 182 NSError *err = nil; 183 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 184 XCTAssertNotNil(msg2); 185 XCTAssertNil(err); 186 XCTAssertTrue(msg2.hasOptionalInt64); 187 XCTAssertEqualObjects(msg, msg2); 188 [msg release]; 189 } 190 191 { // optionalUint32 192 Message3Optional *msg = [[Message3Optional alloc] init]; 193 NSData *data = [msg data]; 194 XCTAssertEqual([data length], 0U); 195 msg.optionalUint32 = 0; 196 data = [msg data]; 197 XCTAssertEqual(data.length, 2); 198 NSError *err = nil; 199 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 200 XCTAssertNotNil(msg2); 201 XCTAssertNil(err); 202 XCTAssertTrue(msg2.hasOptionalUint32); 203 XCTAssertEqualObjects(msg, msg2); 204 [msg release]; 205 } 206 207 { // optionalUint64 208 Message3Optional *msg = [[Message3Optional alloc] init]; 209 NSData *data = [msg data]; 210 XCTAssertEqual([data length], 0U); 211 msg.optionalUint64 = 0; 212 data = [msg data]; 213 XCTAssertEqual(data.length, 2); 214 NSError *err = nil; 215 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 216 XCTAssertNotNil(msg2); 217 XCTAssertNil(err); 218 XCTAssertTrue(msg2.hasOptionalUint64); 219 XCTAssertEqualObjects(msg, msg2); 220 [msg release]; 221 } 222 223 { // optionalSint32 224 Message3Optional *msg = [[Message3Optional alloc] init]; 225 NSData *data = [msg data]; 226 XCTAssertEqual([data length], 0U); 227 msg.optionalSint32 = 0; 228 data = [msg data]; 229 XCTAssertEqual(data.length, 2); 230 NSError *err = nil; 231 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 232 XCTAssertNotNil(msg2); 233 XCTAssertNil(err); 234 XCTAssertTrue(msg2.hasOptionalSint32); 235 XCTAssertEqualObjects(msg, msg2); 236 [msg release]; 237 } 238 239 { // optionalSint64 240 Message3Optional *msg = [[Message3Optional alloc] init]; 241 NSData *data = [msg data]; 242 XCTAssertEqual([data length], 0U); 243 msg.optionalSint64 = 0; 244 data = [msg data]; 245 XCTAssertEqual(data.length, 2); 246 NSError *err = nil; 247 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 248 XCTAssertNotNil(msg2); 249 XCTAssertNil(err); 250 XCTAssertTrue(msg2.hasOptionalSint64); 251 XCTAssertEqualObjects(msg, msg2); 252 [msg release]; 253 } 254 255 { // optionalFixed32 256 Message3Optional *msg = [[Message3Optional alloc] init]; 257 NSData *data = [msg data]; 258 XCTAssertEqual([data length], 0U); 259 msg.optionalFixed32 = 0; 260 data = [msg data]; 261 XCTAssertEqual(data.length, 5); 262 NSError *err = nil; 263 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 264 XCTAssertNotNil(msg2); 265 XCTAssertNil(err); 266 XCTAssertTrue(msg2.hasOptionalFixed32); 267 XCTAssertEqualObjects(msg, msg2); 268 [msg release]; 269 } 270 271 { // optionalFixed64 272 Message3Optional *msg = [[Message3Optional alloc] init]; 273 NSData *data = [msg data]; 274 XCTAssertEqual([data length], 0U); 275 msg.optionalFixed64 = 0; 276 data = [msg data]; 277 XCTAssertEqual(data.length, 9); 278 NSError *err = nil; 279 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 280 XCTAssertNotNil(msg2); 281 XCTAssertNil(err); 282 XCTAssertTrue(msg2.hasOptionalFixed64); 283 XCTAssertEqualObjects(msg, msg2); 284 [msg release]; 285 } 286 287 { // optionalSfixed32 288 Message3Optional *msg = [[Message3Optional alloc] init]; 289 NSData *data = [msg data]; 290 XCTAssertEqual([data length], 0U); 291 msg.optionalSfixed32 = 0; 292 data = [msg data]; 293 XCTAssertEqual(data.length, 5); 294 NSError *err = nil; 295 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 296 XCTAssertNotNil(msg2); 297 XCTAssertNil(err); 298 XCTAssertTrue(msg2.hasOptionalSfixed32); 299 XCTAssertEqualObjects(msg, msg2); 300 [msg release]; 301 } 302 303 { // optionalSfixed64 304 Message3Optional *msg = [[Message3Optional alloc] init]; 305 NSData *data = [msg data]; 306 XCTAssertEqual([data length], 0U); 307 msg.optionalSfixed64 = 0; 308 data = [msg data]; 309 XCTAssertEqual(data.length, 9); 310 NSError *err = nil; 311 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 312 XCTAssertNotNil(msg2); 313 XCTAssertNil(err); 314 XCTAssertTrue(msg2.hasOptionalSfixed64); 315 XCTAssertEqualObjects(msg, msg2); 316 [msg release]; 317 } 318 319 { // optionalFloat 320 Message3Optional *msg = [[Message3Optional alloc] init]; 321 NSData *data = [msg data]; 322 XCTAssertEqual([data length], 0U); 323 msg.optionalFloat = 0.0f; 324 data = [msg data]; 325 XCTAssertEqual(data.length, 5); 326 NSError *err = nil; 327 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 328 XCTAssertNotNil(msg2); 329 XCTAssertNil(err); 330 XCTAssertTrue(msg2.hasOptionalFloat); 331 XCTAssertEqualObjects(msg, msg2); 332 [msg release]; 333 } 334 335 { // optionalDouble 336 Message3Optional *msg = [[Message3Optional alloc] init]; 337 NSData *data = [msg data]; 338 XCTAssertEqual([data length], 0U); 339 msg.optionalDouble = 0.0; 340 data = [msg data]; 341 XCTAssertEqual(data.length, 9); 342 NSError *err = nil; 343 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 344 XCTAssertNotNil(msg2); 345 XCTAssertNil(err); 346 XCTAssertTrue(msg2.hasOptionalDouble); 347 XCTAssertEqualObjects(msg, msg2); 348 [msg release]; 349 } 350 351 { // optionalBool 352 Message3Optional *msg = [[Message3Optional alloc] init]; 353 NSData *data = [msg data]; 354 XCTAssertEqual([data length], 0U); 355 msg.optionalBool = NO; 356 data = [msg data]; 357 XCTAssertEqual(data.length, 2); 358 NSError *err = nil; 359 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 360 XCTAssertNotNil(msg2); 361 XCTAssertNil(err); 362 XCTAssertTrue(msg2.hasOptionalBool); 363 XCTAssertEqualObjects(msg, msg2); 364 [msg release]; 365 } 366 367 { // optionalString 368 Message3Optional *msg = [[Message3Optional alloc] init]; 369 NSData *data = [msg data]; 370 XCTAssertEqual([data length], 0U); 371 msg.optionalString = @""; 372 data = [msg data]; 373 XCTAssertEqual(data.length, 2); 374 NSError *err = nil; 375 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 376 XCTAssertNotNil(msg2); 377 XCTAssertNil(err); 378 XCTAssertTrue(msg2.hasOptionalString); 379 XCTAssertEqualObjects(msg, msg2); 380 [msg release]; 381 } 382 383 { // optionalBytes 384 Message3Optional *msg = [[Message3Optional alloc] init]; 385 NSData *data = [msg data]; 386 XCTAssertEqual([data length], 0U); 387 msg.optionalBytes = [NSData data]; 388 data = [msg data]; 389 XCTAssertEqual(data.length, 2); 390 NSError *err = nil; 391 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 392 XCTAssertNotNil(msg2); 393 XCTAssertNil(err); 394 XCTAssertTrue(msg2.hasOptionalBytes); 395 XCTAssertEqualObjects(msg, msg2); 396 [msg release]; 397 } 398 399 // 400 // Test doesn't apply to optionalMessage (no groups in proto3). 401 // 402 403 { // optionalEnum 404 Message3Optional *msg = [[Message3Optional alloc] init]; 405 NSData *data = [msg data]; 406 XCTAssertEqual([data length], 0U); 407 msg.optionalEnum = Message3Optional_Enum_Foo; 408 data = [msg data]; 409 XCTAssertEqual(data.length, 3); 410 NSError *err = nil; 411 Message3Optional *msg2 = [Message3Optional parseFromData:data error:&err]; 412 XCTAssertNotNil(msg2); 413 XCTAssertNil(err); 414 XCTAssertTrue(msg2.hasOptionalEnum); 415 XCTAssertEqualObjects(msg, msg2); 416 [msg release]; 417 } 418 419// clang-format on 420//%PDDM-EXPAND-END PROTO3_TEST_SERIALIZE_OPTIONAL_FIELDS() 421} 422 423- (void)testProto2UnknownEnumToUnknownField { 424 Message3 *orig = [[Message3 alloc] init]; 425 426 orig.optionalEnum = Message3_Enum_Extra3; 427 orig.repeatedEnumArray = 428 [GPBEnumArray arrayWithValidationFunction:Message3_Enum_IsValidValue 429 rawValue:Message3_Enum_Extra3]; 430 orig.oneofEnum = Message3_Enum_Extra3; 431 432 NSData *data = [orig data]; 433 XCTAssertNotNil(data); 434 Message2 *msg = [[Message2 alloc] initWithData:data error:NULL]; 435 436 // None of the fields should be set. 437 438 XCTAssertFalse(msg.hasOptionalEnum); 439 XCTAssertEqual(msg.repeatedEnumArray.count, 0U); 440 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_GPBUnsetOneOfCase); 441 442 // All the values should be in unknown fields. 443 444 GPBUnknownFieldSet *unknownFields = msg.unknownFields; 445 446 XCTAssertEqual([unknownFields countOfFields], 3U); 447 XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OptionalEnum]); 448 XCTAssertTrue( 449 [unknownFields hasField:Message2_FieldNumber_RepeatedEnumArray]); 450 XCTAssertTrue([unknownFields hasField:Message2_FieldNumber_OneofEnum]); 451 452 GPBUnknownField *field = 453 [unknownFields getField:Message2_FieldNumber_OptionalEnum]; 454 XCTAssertEqual(field.varintList.count, 1U); 455 XCTAssertEqual([field.varintList valueAtIndex:0], 456 (uint64_t)Message3_Enum_Extra3); 457 458 field = [unknownFields getField:Message2_FieldNumber_RepeatedEnumArray]; 459 XCTAssertEqual(field.varintList.count, 1U); 460 XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)Message3_Enum_Extra3); 461 462 field = [unknownFields getField:Message2_FieldNumber_OneofEnum]; 463 XCTAssertEqual(field.varintList.count, 1U); 464 XCTAssertEqual([field.varintList valueAtIndex:0], 465 (uint64_t)Message3_Enum_Extra3); 466 467 [msg release]; 468 [orig release]; 469} 470 471- (void)testProto3UnknownEnumPreserving { 472 UnknownEnumsMyMessagePlusExtra *orig = 473 [UnknownEnumsMyMessagePlusExtra message]; 474 475 orig.e = UnknownEnumsMyEnumPlusExtra_EExtra; 476 orig.repeatedEArray = [GPBEnumArray 477 arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue 478 rawValue:UnknownEnumsMyEnumPlusExtra_EExtra]; 479 orig.repeatedPackedEArray = [GPBEnumArray 480 arrayWithValidationFunction:UnknownEnumsMyEnumPlusExtra_IsValidValue 481 rawValue:UnknownEnumsMyEnumPlusExtra_EExtra]; 482 orig.oneofE1 = UnknownEnumsMyEnumPlusExtra_EExtra; 483 484 // Everything should be there via raw values. 485 486 NSData *data = [orig data]; 487 XCTAssertNotNil(data); 488 UnknownEnumsMyMessage *msg = 489 [UnknownEnumsMyMessage parseFromData:data error:NULL]; 490 491 XCTAssertEqual(msg.e, UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue); 492 XCTAssertEqual(UnknownEnumsMyMessage_E_RawValue(msg), 493 UnknownEnumsMyEnumPlusExtra_EExtra); 494 XCTAssertEqual(msg.repeatedEArray.count, 1U); 495 XCTAssertEqual([msg.repeatedEArray valueAtIndex:0], 496 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue); 497 XCTAssertEqual([msg.repeatedEArray rawValueAtIndex:0], 498 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra); 499 XCTAssertEqual(msg.repeatedPackedEArray.count, 1U); 500 XCTAssertEqual([msg.repeatedPackedEArray valueAtIndex:0], 501 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue); 502 XCTAssertEqual([msg.repeatedPackedEArray rawValueAtIndex:0], 503 (UnknownEnumsMyEnum)UnknownEnumsMyEnumPlusExtra_EExtra); 504 XCTAssertEqual(msg.oneofE1, 505 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue); 506 XCTAssertEqual(UnknownEnumsMyMessage_OneofE1_RawValue(msg), 507 UnknownEnumsMyEnumPlusExtra_EExtra); 508 509 // Everything should go out and come back. 510 511 data = [msg data]; 512 orig = [UnknownEnumsMyMessagePlusExtra parseFromData:data error:NULL]; 513 514 XCTAssertEqual(orig.e, UnknownEnumsMyEnumPlusExtra_EExtra); 515 XCTAssertEqual(orig.repeatedEArray.count, 1U); 516 XCTAssertEqual([orig.repeatedEArray valueAtIndex:0], 517 UnknownEnumsMyEnumPlusExtra_EExtra); 518 XCTAssertEqual(orig.repeatedPackedEArray.count, 1U); 519 XCTAssertEqual([orig.repeatedPackedEArray valueAtIndex:0], 520 UnknownEnumsMyEnumPlusExtra_EExtra); 521 XCTAssertEqual(orig.oneofE1, UnknownEnumsMyEnumPlusExtra_EExtra); 522} 523 524//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF(MESSAGE, FIELD, VALUE) 525//%TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, ) 526//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOF_ADV(MESSAGE, FIELD, VALUE, EQ_SUFFIX) 527//% { // oneof##FIELD 528//% MESSAGE *orig = [[MESSAGE alloc] init]; 529//% orig.oneof##FIELD = VALUE; 530//% XCTAssertEqual(orig.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD); 531//% NSData *data = [orig data]; 532//% XCTAssertNotNil(data); 533//% MESSAGE *msg = [MESSAGE parseFromData:data error:NULL]; 534//% XCTAssertEqual(msg.oOneOfCase, MESSAGE##_O_OneOfCase_Oneof##FIELD); 535//% XCTAssertEqual##EQ_SUFFIX(msg.oneof##FIELD, VALUE); 536//% [orig release]; 537//% } 538//% 539//%PDDM-DEFINE TEST_ROUNDTRIP_ONEOFS(SYNTAX, BOOL_NON_DEFAULT) 540//%- (void)testProto##SYNTAX##RoundTripOneof { 541//% 542//%GROUP_INIT##SYNTAX() Message##SYNTAX *subMessage = [[Message##SYNTAX alloc] init]; 543//% XCTAssertNotNil(subMessage); 544//% subMessage.optionalInt32 = 666; 545//% 546//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int32, 1) 547//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Int64, 2) 548//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint32, 3U) 549//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Uint64, 4U) 550//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint32, 5) 551//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sint64, 6) 552//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed32, 7U) 553//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Fixed64, 8U) 554//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed32, 9) 555//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Sfixed64, 10) 556//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Float, 11.0f) 557//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Double, 12.0) 558//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Bool, BOOL_NON_DEFAULT) 559//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, String, @"foo", Objects) 560//%TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Bytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding], Objects) 561//%GROUP_TEST##SYNTAX()TEST_ROUNDTRIP_ONEOF_ADV(Message##SYNTAX, Message, subMessage, Objects) 562//%TEST_ROUNDTRIP_ONEOF(Message##SYNTAX, Enum, Message2_Enum_Bar) 563//%GROUP_CLEANUP##SYNTAX() [subMessage release]; 564//%} 565//% 566//%PDDM-DEFINE GROUP_INIT2() 567//% Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init]; 568//% XCTAssertNotNil(group); 569//% group.a = 777; 570//% 571//%PDDM-DEFINE GROUP_CLEANUP2() 572//% [group release]; 573//% 574//%PDDM-DEFINE GROUP_TEST2() 575//%TEST_ROUNDTRIP_ONEOF_ADV(Message2, Group, group, Objects) 576//% 577//%PDDM-DEFINE GROUP_INIT3() 578// Empty 579//%PDDM-DEFINE GROUP_CLEANUP3() 580// Empty 581//%PDDM-DEFINE GROUP_TEST3() 582//% // Not "group" in proto3. 583//% 584//% 585//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(2, NO) 586// This block of code is generated, do not edit it directly. 587// clang-format off 588 589- (void)testProto2RoundTripOneof { 590 591 Message2_OneofGroup *group = [[Message2_OneofGroup alloc] init]; 592 XCTAssertNotNil(group); 593 group.a = 777; 594 Message2 *subMessage = [[Message2 alloc] init]; 595 XCTAssertNotNil(subMessage); 596 subMessage.optionalInt32 = 666; 597 598 { // oneofInt32 599 Message2 *orig = [[Message2 alloc] init]; 600 orig.oneofInt32 = 1; 601 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt32); 602 NSData *data = [orig data]; 603 XCTAssertNotNil(data); 604 Message2 *msg = [Message2 parseFromData:data error:NULL]; 605 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt32); 606 XCTAssertEqual(msg.oneofInt32, 1); 607 [orig release]; 608 } 609 610 { // oneofInt64 611 Message2 *orig = [[Message2 alloc] init]; 612 orig.oneofInt64 = 2; 613 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofInt64); 614 NSData *data = [orig data]; 615 XCTAssertNotNil(data); 616 Message2 *msg = [Message2 parseFromData:data error:NULL]; 617 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofInt64); 618 XCTAssertEqual(msg.oneofInt64, 2); 619 [orig release]; 620 } 621 622 { // oneofUint32 623 Message2 *orig = [[Message2 alloc] init]; 624 orig.oneofUint32 = 3U; 625 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint32); 626 NSData *data = [orig data]; 627 XCTAssertNotNil(data); 628 Message2 *msg = [Message2 parseFromData:data error:NULL]; 629 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint32); 630 XCTAssertEqual(msg.oneofUint32, 3U); 631 [orig release]; 632 } 633 634 { // oneofUint64 635 Message2 *orig = [[Message2 alloc] init]; 636 orig.oneofUint64 = 4U; 637 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofUint64); 638 NSData *data = [orig data]; 639 XCTAssertNotNil(data); 640 Message2 *msg = [Message2 parseFromData:data error:NULL]; 641 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofUint64); 642 XCTAssertEqual(msg.oneofUint64, 4U); 643 [orig release]; 644 } 645 646 { // oneofSint32 647 Message2 *orig = [[Message2 alloc] init]; 648 orig.oneofSint32 = 5; 649 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint32); 650 NSData *data = [orig data]; 651 XCTAssertNotNil(data); 652 Message2 *msg = [Message2 parseFromData:data error:NULL]; 653 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint32); 654 XCTAssertEqual(msg.oneofSint32, 5); 655 [orig release]; 656 } 657 658 { // oneofSint64 659 Message2 *orig = [[Message2 alloc] init]; 660 orig.oneofSint64 = 6; 661 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSint64); 662 NSData *data = [orig data]; 663 XCTAssertNotNil(data); 664 Message2 *msg = [Message2 parseFromData:data error:NULL]; 665 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSint64); 666 XCTAssertEqual(msg.oneofSint64, 6); 667 [orig release]; 668 } 669 670 { // oneofFixed32 671 Message2 *orig = [[Message2 alloc] init]; 672 orig.oneofFixed32 = 7U; 673 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed32); 674 NSData *data = [orig data]; 675 XCTAssertNotNil(data); 676 Message2 *msg = [Message2 parseFromData:data error:NULL]; 677 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed32); 678 XCTAssertEqual(msg.oneofFixed32, 7U); 679 [orig release]; 680 } 681 682 { // oneofFixed64 683 Message2 *orig = [[Message2 alloc] init]; 684 orig.oneofFixed64 = 8U; 685 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFixed64); 686 NSData *data = [orig data]; 687 XCTAssertNotNil(data); 688 Message2 *msg = [Message2 parseFromData:data error:NULL]; 689 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFixed64); 690 XCTAssertEqual(msg.oneofFixed64, 8U); 691 [orig release]; 692 } 693 694 { // oneofSfixed32 695 Message2 *orig = [[Message2 alloc] init]; 696 orig.oneofSfixed32 = 9; 697 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32); 698 NSData *data = [orig data]; 699 XCTAssertNotNil(data); 700 Message2 *msg = [Message2 parseFromData:data error:NULL]; 701 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32); 702 XCTAssertEqual(msg.oneofSfixed32, 9); 703 [orig release]; 704 } 705 706 { // oneofSfixed64 707 Message2 *orig = [[Message2 alloc] init]; 708 orig.oneofSfixed64 = 10; 709 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64); 710 NSData *data = [orig data]; 711 XCTAssertNotNil(data); 712 Message2 *msg = [Message2 parseFromData:data error:NULL]; 713 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64); 714 XCTAssertEqual(msg.oneofSfixed64, 10); 715 [orig release]; 716 } 717 718 { // oneofFloat 719 Message2 *orig = [[Message2 alloc] init]; 720 orig.oneofFloat = 11.0f; 721 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofFloat); 722 NSData *data = [orig data]; 723 XCTAssertNotNil(data); 724 Message2 *msg = [Message2 parseFromData:data error:NULL]; 725 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofFloat); 726 XCTAssertEqual(msg.oneofFloat, 11.0f); 727 [orig release]; 728 } 729 730 { // oneofDouble 731 Message2 *orig = [[Message2 alloc] init]; 732 orig.oneofDouble = 12.0; 733 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofDouble); 734 NSData *data = [orig data]; 735 XCTAssertNotNil(data); 736 Message2 *msg = [Message2 parseFromData:data error:NULL]; 737 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofDouble); 738 XCTAssertEqual(msg.oneofDouble, 12.0); 739 [orig release]; 740 } 741 742 { // oneofBool 743 Message2 *orig = [[Message2 alloc] init]; 744 orig.oneofBool = NO; 745 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBool); 746 NSData *data = [orig data]; 747 XCTAssertNotNil(data); 748 Message2 *msg = [Message2 parseFromData:data error:NULL]; 749 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBool); 750 XCTAssertEqual(msg.oneofBool, NO); 751 [orig release]; 752 } 753 754 { // oneofString 755 Message2 *orig = [[Message2 alloc] init]; 756 orig.oneofString = @"foo"; 757 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofString); 758 NSData *data = [orig data]; 759 XCTAssertNotNil(data); 760 Message2 *msg = [Message2 parseFromData:data error:NULL]; 761 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofString); 762 XCTAssertEqualObjects(msg.oneofString, @"foo"); 763 [orig release]; 764 } 765 766 { // oneofBytes 767 Message2 *orig = [[Message2 alloc] init]; 768 orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding]; 769 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofBytes); 770 NSData *data = [orig data]; 771 XCTAssertNotNil(data); 772 Message2 *msg = [Message2 parseFromData:data error:NULL]; 773 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofBytes); 774 XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]); 775 [orig release]; 776 } 777 778 { // oneofGroup 779 Message2 *orig = [[Message2 alloc] init]; 780 orig.oneofGroup = group; 781 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofGroup); 782 NSData *data = [orig data]; 783 XCTAssertNotNil(data); 784 Message2 *msg = [Message2 parseFromData:data error:NULL]; 785 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofGroup); 786 XCTAssertEqualObjects(msg.oneofGroup, group); 787 [orig release]; 788 } 789 790 { // oneofMessage 791 Message2 *orig = [[Message2 alloc] init]; 792 orig.oneofMessage = subMessage; 793 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofMessage); 794 NSData *data = [orig data]; 795 XCTAssertNotNil(data); 796 Message2 *msg = [Message2 parseFromData:data error:NULL]; 797 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofMessage); 798 XCTAssertEqualObjects(msg.oneofMessage, subMessage); 799 [orig release]; 800 } 801 802 { // oneofEnum 803 Message2 *orig = [[Message2 alloc] init]; 804 orig.oneofEnum = Message2_Enum_Bar; 805 XCTAssertEqual(orig.oOneOfCase, Message2_O_OneOfCase_OneofEnum); 806 NSData *data = [orig data]; 807 XCTAssertNotNil(data); 808 Message2 *msg = [Message2 parseFromData:data error:NULL]; 809 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase_OneofEnum); 810 XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar); 811 [orig release]; 812 } 813 814 [group release]; 815 [subMessage release]; 816} 817 818// clang-format on 819//%PDDM-EXPAND TEST_ROUNDTRIP_ONEOFS(3, YES) 820// This block of code is generated, do not edit it directly. 821// clang-format off 822 823- (void)testProto3RoundTripOneof { 824 825 Message3 *subMessage = [[Message3 alloc] init]; 826 XCTAssertNotNil(subMessage); 827 subMessage.optionalInt32 = 666; 828 829 { // oneofInt32 830 Message3 *orig = [[Message3 alloc] init]; 831 orig.oneofInt32 = 1; 832 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt32); 833 NSData *data = [orig data]; 834 XCTAssertNotNil(data); 835 Message3 *msg = [Message3 parseFromData:data error:NULL]; 836 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt32); 837 XCTAssertEqual(msg.oneofInt32, 1); 838 [orig release]; 839 } 840 841 { // oneofInt64 842 Message3 *orig = [[Message3 alloc] init]; 843 orig.oneofInt64 = 2; 844 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofInt64); 845 NSData *data = [orig data]; 846 XCTAssertNotNil(data); 847 Message3 *msg = [Message3 parseFromData:data error:NULL]; 848 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofInt64); 849 XCTAssertEqual(msg.oneofInt64, 2); 850 [orig release]; 851 } 852 853 { // oneofUint32 854 Message3 *orig = [[Message3 alloc] init]; 855 orig.oneofUint32 = 3U; 856 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint32); 857 NSData *data = [orig data]; 858 XCTAssertNotNil(data); 859 Message3 *msg = [Message3 parseFromData:data error:NULL]; 860 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint32); 861 XCTAssertEqual(msg.oneofUint32, 3U); 862 [orig release]; 863 } 864 865 { // oneofUint64 866 Message3 *orig = [[Message3 alloc] init]; 867 orig.oneofUint64 = 4U; 868 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofUint64); 869 NSData *data = [orig data]; 870 XCTAssertNotNil(data); 871 Message3 *msg = [Message3 parseFromData:data error:NULL]; 872 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofUint64); 873 XCTAssertEqual(msg.oneofUint64, 4U); 874 [orig release]; 875 } 876 877 { // oneofSint32 878 Message3 *orig = [[Message3 alloc] init]; 879 orig.oneofSint32 = 5; 880 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint32); 881 NSData *data = [orig data]; 882 XCTAssertNotNil(data); 883 Message3 *msg = [Message3 parseFromData:data error:NULL]; 884 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint32); 885 XCTAssertEqual(msg.oneofSint32, 5); 886 [orig release]; 887 } 888 889 { // oneofSint64 890 Message3 *orig = [[Message3 alloc] init]; 891 orig.oneofSint64 = 6; 892 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSint64); 893 NSData *data = [orig data]; 894 XCTAssertNotNil(data); 895 Message3 *msg = [Message3 parseFromData:data error:NULL]; 896 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSint64); 897 XCTAssertEqual(msg.oneofSint64, 6); 898 [orig release]; 899 } 900 901 { // oneofFixed32 902 Message3 *orig = [[Message3 alloc] init]; 903 orig.oneofFixed32 = 7U; 904 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed32); 905 NSData *data = [orig data]; 906 XCTAssertNotNil(data); 907 Message3 *msg = [Message3 parseFromData:data error:NULL]; 908 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed32); 909 XCTAssertEqual(msg.oneofFixed32, 7U); 910 [orig release]; 911 } 912 913 { // oneofFixed64 914 Message3 *orig = [[Message3 alloc] init]; 915 orig.oneofFixed64 = 8U; 916 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFixed64); 917 NSData *data = [orig data]; 918 XCTAssertNotNil(data); 919 Message3 *msg = [Message3 parseFromData:data error:NULL]; 920 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFixed64); 921 XCTAssertEqual(msg.oneofFixed64, 8U); 922 [orig release]; 923 } 924 925 { // oneofSfixed32 926 Message3 *orig = [[Message3 alloc] init]; 927 orig.oneofSfixed32 = 9; 928 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32); 929 NSData *data = [orig data]; 930 XCTAssertNotNil(data); 931 Message3 *msg = [Message3 parseFromData:data error:NULL]; 932 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32); 933 XCTAssertEqual(msg.oneofSfixed32, 9); 934 [orig release]; 935 } 936 937 { // oneofSfixed64 938 Message3 *orig = [[Message3 alloc] init]; 939 orig.oneofSfixed64 = 10; 940 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64); 941 NSData *data = [orig data]; 942 XCTAssertNotNil(data); 943 Message3 *msg = [Message3 parseFromData:data error:NULL]; 944 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64); 945 XCTAssertEqual(msg.oneofSfixed64, 10); 946 [orig release]; 947 } 948 949 { // oneofFloat 950 Message3 *orig = [[Message3 alloc] init]; 951 orig.oneofFloat = 11.0f; 952 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofFloat); 953 NSData *data = [orig data]; 954 XCTAssertNotNil(data); 955 Message3 *msg = [Message3 parseFromData:data error:NULL]; 956 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofFloat); 957 XCTAssertEqual(msg.oneofFloat, 11.0f); 958 [orig release]; 959 } 960 961 { // oneofDouble 962 Message3 *orig = [[Message3 alloc] init]; 963 orig.oneofDouble = 12.0; 964 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofDouble); 965 NSData *data = [orig data]; 966 XCTAssertNotNil(data); 967 Message3 *msg = [Message3 parseFromData:data error:NULL]; 968 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofDouble); 969 XCTAssertEqual(msg.oneofDouble, 12.0); 970 [orig release]; 971 } 972 973 { // oneofBool 974 Message3 *orig = [[Message3 alloc] init]; 975 orig.oneofBool = YES; 976 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBool); 977 NSData *data = [orig data]; 978 XCTAssertNotNil(data); 979 Message3 *msg = [Message3 parseFromData:data error:NULL]; 980 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBool); 981 XCTAssertEqual(msg.oneofBool, YES); 982 [orig release]; 983 } 984 985 { // oneofString 986 Message3 *orig = [[Message3 alloc] init]; 987 orig.oneofString = @"foo"; 988 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofString); 989 NSData *data = [orig data]; 990 XCTAssertNotNil(data); 991 Message3 *msg = [Message3 parseFromData:data error:NULL]; 992 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofString); 993 XCTAssertEqualObjects(msg.oneofString, @"foo"); 994 [orig release]; 995 } 996 997 { // oneofBytes 998 Message3 *orig = [[Message3 alloc] init]; 999 orig.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding]; 1000 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofBytes); 1001 NSData *data = [orig data]; 1002 XCTAssertNotNil(data); 1003 Message3 *msg = [Message3 parseFromData:data error:NULL]; 1004 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofBytes); 1005 XCTAssertEqualObjects(msg.oneofBytes, [@"bar" dataUsingEncoding:NSUTF8StringEncoding]); 1006 [orig release]; 1007 } 1008 1009 // Not "group" in proto3. 1010 1011 { // oneofMessage 1012 Message3 *orig = [[Message3 alloc] init]; 1013 orig.oneofMessage = subMessage; 1014 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofMessage); 1015 NSData *data = [orig data]; 1016 XCTAssertNotNil(data); 1017 Message3 *msg = [Message3 parseFromData:data error:NULL]; 1018 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofMessage); 1019 XCTAssertEqualObjects(msg.oneofMessage, subMessage); 1020 [orig release]; 1021 } 1022 1023 { // oneofEnum 1024 Message3 *orig = [[Message3 alloc] init]; 1025 orig.oneofEnum = Message2_Enum_Bar; 1026 XCTAssertEqual(orig.oOneOfCase, Message3_O_OneOfCase_OneofEnum); 1027 NSData *data = [orig data]; 1028 XCTAssertNotNil(data); 1029 Message3 *msg = [Message3 parseFromData:data error:NULL]; 1030 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase_OneofEnum); 1031 XCTAssertEqual(msg.oneofEnum, Message2_Enum_Bar); 1032 [orig release]; 1033 } 1034 1035 [subMessage release]; 1036} 1037 1038// clang-format on 1039//%PDDM-EXPAND-END (2 expansions) 1040 1041- (void)testPackedUnpackedMessageParsing { 1042 // packed is optional, a repeated field should parse when packed or unpacked. 1043 1044 TestPackedTypes *packedOrig = [TestPackedTypes message]; 1045 TestUnpackedTypes *unpackedOrig = [TestUnpackedTypes message]; 1046 [self setPackedFields:packedOrig repeatedCount:4]; 1047 [self setUnpackedFields:unpackedOrig repeatedCount:4]; 1048 1049 NSData *packedData = [packedOrig data]; 1050 NSData *unpackedData = [unpackedOrig data]; 1051 XCTAssertNotNil(packedData); 1052 XCTAssertNotNil(unpackedData); 1053 XCTAssertNotEqualObjects(packedData, unpackedData, 1054 @"Data should differ (packed vs unpacked) use"); 1055 1056 NSError *error = nil; 1057 TestPackedTypes *packedParse = 1058 [TestPackedTypes parseFromData:unpackedData error:&error]; 1059 XCTAssertNotNil(packedParse); 1060 XCTAssertNil(error); 1061 XCTAssertEqualObjects(packedParse, packedOrig); 1062 1063 error = nil; 1064 TestUnpackedTypes *unpackedParsed = 1065 [TestUnpackedTypes parseFromData:packedData error:&error]; 1066 XCTAssertNotNil(unpackedParsed); 1067 XCTAssertNil(error); 1068 XCTAssertEqualObjects(unpackedParsed, unpackedOrig); 1069} 1070 1071- (void)testPackedUnpackedExtensionParsing { 1072 // packed is optional, a repeated extension should parse when packed or 1073 // unpacked. 1074 1075 TestPackedExtensions *packedOrig = [TestPackedExtensions message]; 1076 TestUnpackedExtensions *unpackedOrig = [TestUnpackedExtensions message]; 1077 [self setPackedExtensions:packedOrig repeatedCount:kGPBDefaultRepeatCount]; 1078 [self setUnpackedExtensions:unpackedOrig repeatedCount:kGPBDefaultRepeatCount]; 1079 1080 NSData *packedData = [packedOrig data]; 1081 NSData *unpackedData = [unpackedOrig data]; 1082 XCTAssertNotNil(packedData); 1083 XCTAssertNotNil(unpackedData); 1084 XCTAssertNotEqualObjects(packedData, unpackedData, 1085 @"Data should differ (packed vs unpacked) use"); 1086 1087 NSError *error = nil; 1088 TestPackedExtensions *packedParse = 1089 [TestPackedExtensions parseFromData:unpackedData 1090 extensionRegistry:[UnittestRoot extensionRegistry] 1091 error:&error]; 1092 XCTAssertNotNil(packedParse); 1093 XCTAssertNil(error); 1094 XCTAssertEqualObjects(packedParse, packedOrig); 1095 1096 error = nil; 1097 TestUnpackedExtensions *unpackedParsed = 1098 [TestUnpackedExtensions parseFromData:packedData 1099 extensionRegistry:[UnittestRoot extensionRegistry] 1100 error:&error]; 1101 XCTAssertNotNil(unpackedParsed); 1102 XCTAssertNil(error); 1103 XCTAssertEqualObjects(unpackedParsed, unpackedOrig); 1104} 1105 1106- (void)testPackedExtensionVsFieldParsing { 1107 // Extensions and fields end up on the wire the same way, so they can parse 1108 // each other. 1109 1110 TestPackedTypes *fieldsOrig = [TestPackedTypes message]; 1111 TestPackedExtensions *extsOrig = [TestPackedExtensions message]; 1112 [self setPackedFields:fieldsOrig repeatedCount:kGPBDefaultRepeatCount]; 1113 [self setPackedExtensions:extsOrig repeatedCount:kGPBDefaultRepeatCount]; 1114 1115 NSData *fieldsData = [fieldsOrig data]; 1116 NSData *extsData = [extsOrig data]; 1117 XCTAssertNotNil(fieldsData); 1118 XCTAssertNotNil(extsData); 1119 XCTAssertEqualObjects(fieldsData, extsData); 1120 1121 NSError *error = nil; 1122 TestPackedTypes *fieldsParse = 1123 [TestPackedTypes parseFromData:extsData error:&error]; 1124 XCTAssertNotNil(fieldsParse); 1125 XCTAssertNil(error); 1126 XCTAssertEqualObjects(fieldsParse, fieldsOrig); 1127 1128 error = nil; 1129 TestPackedExtensions *extsParse = 1130 [TestPackedExtensions parseFromData:fieldsData 1131 extensionRegistry:[UnittestRoot extensionRegistry] 1132 error:&error]; 1133 XCTAssertNotNil(extsParse); 1134 XCTAssertNil(error); 1135 XCTAssertEqualObjects(extsParse, extsOrig); 1136} 1137 1138- (void)testUnpackedExtensionVsFieldParsing { 1139 // Extensions and fields end up on the wire the same way, so they can parse 1140 // each other. 1141 1142 TestUnpackedTypes *fieldsOrig = [TestUnpackedTypes message]; 1143 TestUnpackedExtensions *extsOrig = [TestUnpackedExtensions message]; 1144 [self setUnpackedFields:fieldsOrig repeatedCount:3]; 1145 [self setUnpackedExtensions:extsOrig repeatedCount:3]; 1146 1147 NSData *fieldsData = [fieldsOrig data]; 1148 NSData *extsData = [extsOrig data]; 1149 XCTAssertNotNil(fieldsData); 1150 XCTAssertNotNil(extsData); 1151 XCTAssertEqualObjects(fieldsData, extsData); 1152 1153 TestUnpackedTypes *fieldsParse = 1154 [TestUnpackedTypes parseFromData:extsData error:NULL]; 1155 XCTAssertNotNil(fieldsParse); 1156 XCTAssertEqualObjects(fieldsParse, fieldsOrig); 1157 1158 TestUnpackedExtensions *extsParse = 1159 [TestUnpackedExtensions parseFromData:fieldsData 1160 extensionRegistry:[UnittestRoot extensionRegistry] 1161 error:NULL]; 1162 XCTAssertNotNil(extsParse); 1163 XCTAssertEqualObjects(extsParse, extsOrig); 1164} 1165 1166- (void)testErrorSubsectionInvalidLimit { 1167 NSData *data = DataFromCStr( 1168 "\x0A\x08\x0A\x07\x12\x04\x72\x02\x4B\x50\x12\x04\x72\x02\x4B\x50"); 1169 NSError *error = nil; 1170 NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data 1171 error:&error]; 1172 XCTAssertNil(msg); 1173 XCTAssertNotNil(error); 1174 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1175 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSubsectionLimit); 1176} 1177 1178- (void)testErrorSubsectionLimitReached { 1179 NSData *data = DataFromCStr("\x0A\x06\x12\x03\x72\x02\x4B\x50"); 1180 NSError *error = nil; 1181 NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data 1182 error:&error]; 1183 XCTAssertNil(msg); 1184 XCTAssertNotNil(error); 1185 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1186 XCTAssertEqual(error.code, GPBCodedInputStreamErrorSubsectionLimitReached); 1187} 1188 1189- (void)testErrorInvalidVarint { 1190 NSData *data = DataFromCStr("\x72\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"); 1191 NSError *error = nil; 1192 TestAllTypes *msg = [TestAllTypes parseFromData:data error:&error]; 1193 XCTAssertNil(msg); 1194 XCTAssertNotNil(error); 1195 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1196 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidVarInt); 1197} 1198 1199- (void)testErrorInvalidUTF8 { 1200 NSData *data = DataFromCStr("\x72\x04\xF4\xFF\xFF\xFF"); 1201 NSError *error = nil; 1202 TestAllTypes *msg = [TestAllTypes parseFromData:data error:&error]; 1203 XCTAssertNil(msg); 1204 XCTAssertNotNil(error); 1205 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1206 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidUTF8); 1207} 1208 1209- (void)testErrorInvalidSize { 1210 NSData *data = DataFromCStr("\x72\x03\x4B\x50"); 1211 NSError *error = nil; 1212 NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data 1213 error:&error]; 1214 XCTAssertNil(msg); 1215 XCTAssertNotNil(error); 1216 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1217 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSize); 1218} 1219 1220- (void)testErrorInvalidTag { 1221 NSData *data = DataFromCStr("\x0F"); 1222 NSError *error = nil; 1223 NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data 1224 error:&error]; 1225 XCTAssertNil(msg); 1226 XCTAssertNotNil(error); 1227 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1228 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidTag); 1229} 1230 1231- (void)testZeroFieldNum { 1232 // These are ConformanceTestSuite::TestIllegalTags. 1233 1234 const char *tests[] = { 1235 "\1DEADBEEF", 1236 "\2\1\1", 1237 "\3\4", 1238 "\5DEAD" 1239 }; 1240 1241 for (size_t i = 0; i < GPBARRAYSIZE(tests); ++i) { 1242 NSData *data = DataFromCStr(tests[i]); 1243 1244 { 1245 // Message from proto2 syntax file 1246 NSError *error = nil; 1247 Message2 *msg = [Message2 parseFromData:data error:&error]; 1248 XCTAssertNil(msg, @"i = %zd", i); 1249 XCTAssertNotNil(error, @"i = %zd", i); 1250 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain, @"i = %zd", i); 1251 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidTag, @"i = %zd", i); 1252 } 1253 1254 { 1255 // Message from proto3 syntax file 1256 NSError *error = nil; 1257 Message3 *msg = [Message3 parseFromData:data error:&error]; 1258 XCTAssertNil(msg, @"i = %zd", i); 1259 XCTAssertNotNil(error, @"i = %zd", i); 1260 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain, @"i = %zd", i); 1261 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidTag, @"i = %zd", i); 1262 } 1263 } 1264} 1265 1266- (void)testErrorRecursionDepthReached { 1267 NSData *data = DataFromCStr( 1268 "\x0A\xF2\x01\x0A\xEF\x01\x0A\xEC\x01\x0A\xE9\x01\x0A\xE6\x01" 1269 "\x0A\xE3\x01\x0A\xE0\x01\x0A\xDD\x01\x0A\xDA\x01\x0A\xD7\x01" 1270 "\x0A\xD4\x01\x0A\xD1\x01\x0A\xCE\x01\x0A\xCB\x01\x0A\xC8\x01" 1271 "\x0A\xC5\x01\x0A\xC2\x01\x0A\xBF\x01\x0A\xBC\x01\x0A\xB9\x01" 1272 "\x0A\xB6\x01\x0A\xB3\x01\x0A\xB0\x01\x0A\xAD\x01\x0A\xAA\x01" 1273 "\x0A\xA7\x01\x0A\xA4\x01\x0A\xA1\x01\x0A\x9E\x01\x0A\x9B\x01" 1274 "\x0A\x98\x01\x0A\x95\x01\x0A\x92\x01\x0A\x8F\x01\x0A\x8C\x01" 1275 "\x0A\x89\x01\x0A\x86\x01\x0A\x83\x01\x0A\x80\x01\x0A\x7E" 1276 "\x0A\x7C\x0A\x7A\x0A\x78\x0A\x76\x0A\x74\x0A\x72\x0A\x70" 1277 "\x0A\x6E\x0A\x6C\x0A\x6A\x0A\x68\x0A\x66\x0A\x64\x0A\x62" 1278 "\x0A\x60\x0A\x5E\x0A\x5C\x0A\x5A\x0A\x58\x0A\x56\x0A\x54" 1279 "\x0A\x52\x0A\x50\x0A\x4E\x0A\x4C\x0A\x4A\x0A\x48\x0A\x46" 1280 "\x0A\x44\x0A\x42\x0A\x40\x0A\x3E\x0A\x3C\x0A\x3A\x0A\x38" 1281 "\x0A\x36\x0A\x34\x0A\x32\x0A\x30\x0A\x2E\x0A\x2C\x0A\x2A" 1282 "\x0A\x28\x0A\x26\x0A\x24\x0A\x22\x0A\x20\x0A\x1E\x0A\x1C" 1283 "\x0A\x1A\x0A\x18\x0A\x16\x0A\x14\x0A\x12\x0A\x10\x0A\x0E" 1284 "\x0A\x0C\x0A\x0A\x0A\x08\x0A\x06\x12\x04\x72\x02\x4B\x50"); 1285 NSError *error = nil; 1286 NestedTestAllTypes *msg = [NestedTestAllTypes parseFromData:data 1287 error:&error]; 1288 XCTAssertNil(msg); 1289 XCTAssertNotNil(error); 1290 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1291 XCTAssertEqual(error.code, GPBCodedInputStreamErrorRecursionDepthExceeded); 1292} 1293 1294- (void)testParseDelimitedDataWithNegativeSize { 1295 NSData *data = DataFromCStr("\xFF\xFF\xFF\xFF\x0F"); 1296 GPBCodedInputStream *input = [GPBCodedInputStream streamWithData:data]; 1297 NSError *error; 1298 [GPBMessage parseDelimitedFromCodedInputStream:input 1299 extensionRegistry:nil 1300 error:&error]; 1301 XCTAssertNil(error); 1302} 1303 1304#ifdef DEBUG 1305- (void)testErrorMissingRequiredField { 1306 NSData *data = DataFromCStr(""); 1307 NSError *error = nil; 1308 TestRequired *msg = [TestRequired parseFromData:data error:&error]; 1309 XCTAssertNil(msg); 1310 XCTAssertNotNil(error); 1311 XCTAssertEqualObjects(error.domain, GPBMessageErrorDomain); 1312 XCTAssertEqual(error.code, GPBMessageErrorCodeMissingRequiredField); 1313} 1314#endif 1315 1316#pragma mark - Subset from from map_tests.cc 1317 1318// TEST(GeneratedMapFieldTest, StandardWireFormat) 1319- (void)testMap_StandardWireFormat { 1320 NSData *data = DataFromCStr("\x0A\x04\x08\x01\x10\x01"); 1321 1322 TestMap *msg = [[TestMap alloc] initWithData:data error:NULL]; 1323 XCTAssertEqual(msg.mapInt32Int32.count, 1U); 1324 int32_t val = 666; 1325 XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]); 1326 XCTAssertEqual(val, 1); 1327 1328 [msg release]; 1329} 1330 1331// TEST(GeneratedMapFieldTest, UnorderedWireFormat) 1332- (void)testMap_UnorderedWireFormat { 1333 // put value before key in wire format 1334 NSData *data = DataFromCStr("\x0A\x04\x10\x01\x08\x02"); 1335 1336 TestMap *msg = [[TestMap alloc] initWithData:data error:NULL]; 1337 XCTAssertEqual(msg.mapInt32Int32.count, 1U); 1338 int32_t val = 666; 1339 XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]); 1340 XCTAssertEqual(val, 1); 1341 1342 [msg release]; 1343} 1344 1345// TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) 1346- (void)testMap_DuplicatedKeyWireFormat { 1347 // Two key fields in wire format 1348 NSData *data = DataFromCStr("\x0A\x06\x08\x01\x08\x02\x10\x01"); 1349 1350 TestMap *msg = [[TestMap alloc] initWithData:data error:NULL]; 1351 XCTAssertEqual(msg.mapInt32Int32.count, 1U); 1352 int32_t val = 666; 1353 XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]); 1354 XCTAssertEqual(val, 1); 1355 1356 [msg release]; 1357} 1358 1359// TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) 1360- (void)testMap_DuplicatedValueWireFormat { 1361 // Two value fields in wire format 1362 NSData *data = DataFromCStr("\x0A\x06\x08\x01\x10\x01\x10\x02"); 1363 1364 TestMap *msg = [[TestMap alloc] initWithData:data error:NULL]; 1365 XCTAssertEqual(msg.mapInt32Int32.count, 1U); 1366 int32_t val = 666; 1367 XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]); 1368 XCTAssertEqual(val, 2); 1369 1370 [msg release]; 1371} 1372 1373// TEST(GeneratedMapFieldTest, MissedKeyWireFormat) 1374- (void)testMap_MissedKeyWireFormat { 1375 // No key field in wire format 1376 NSData *data = DataFromCStr("\x0A\x02\x10\x01"); 1377 1378 TestMap *msg = [[TestMap alloc] initWithData:data error:NULL]; 1379 XCTAssertEqual(msg.mapInt32Int32.count, 1U); 1380 int32_t val = 666; 1381 XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:0]); 1382 XCTAssertEqual(val, 1); 1383 1384 [msg release]; 1385} 1386 1387// TEST(GeneratedMapFieldTest, MissedValueWireFormat) 1388- (void)testMap_MissedValueWireFormat { 1389 // No value field in wire format 1390 NSData *data = DataFromCStr("\x0A\x02\x08\x01"); 1391 1392 TestMap *msg = [[TestMap alloc] initWithData:data error:NULL]; 1393 XCTAssertEqual(msg.mapInt32Int32.count, 1U); 1394 int32_t val = 666; 1395 XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:1]); 1396 XCTAssertEqual(val, 0); 1397 1398 [msg release]; 1399} 1400 1401// TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) 1402- (void)testMap_UnknownFieldWireFormat { 1403 // Unknown field in wire format 1404 NSData *data = DataFromCStr("\x0A\x06\x08\x02\x10\x03\x18\x01"); 1405 1406 TestMap *msg = [[TestMap alloc] initWithData:data error:NULL]; 1407 XCTAssertEqual(msg.mapInt32Int32.count, 1U); 1408 int32_t val = 666; 1409 XCTAssertTrue([msg.mapInt32Int32 getInt32:&val forKey:2]); 1410 XCTAssertEqual(val, 3); 1411 1412 [msg release]; 1413} 1414 1415// TEST(GeneratedMapFieldTest, CorruptedWireFormat) 1416- (void)testMap_CorruptedWireFormat { 1417 // corrupted data in wire format 1418 NSData *data = DataFromCStr("\x0A\x06\x08\x02\x11\x03"); 1419 1420 NSError *error = nil; 1421 TestMap *msg = [TestMap parseFromData:data error:&error]; 1422 XCTAssertNil(msg); 1423 XCTAssertNotNil(error); 1424 XCTAssertEqualObjects(error.domain, GPBCodedInputStreamErrorDomain); 1425 XCTAssertEqual(error.code, GPBCodedInputStreamErrorInvalidSubsectionLimit); 1426} 1427 1428// TEST(GeneratedMapFieldTest, Proto2UnknownEnum) 1429- (void)testMap_Proto2UnknownEnum { 1430 TestEnumMapPlusExtra *orig = [[TestEnumMapPlusExtra alloc] init]; 1431 1432 orig.knownMapField = [[[GPBInt32EnumDictionary alloc] 1433 initWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue] autorelease]; 1434 orig.unknownMapField = [[[GPBInt32EnumDictionary alloc] 1435 initWithValidationFunction:Proto2MapEnumPlusExtra_IsValidValue] autorelease]; 1436 [orig.knownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumFoo 1437 forKey:0]; 1438 [orig.unknownMapField setEnum:Proto2MapEnumPlusExtra_EProto2MapEnumExtra 1439 forKey:0]; 1440 1441 NSData *data = [orig data]; 1442 XCTAssertNotNil(data); 1443 TestEnumMap *msg1 = [TestEnumMap parseFromData:data error:NULL]; 1444 XCTAssertEqual(msg1.knownMapField.count, 1U); 1445 int32_t val = -1; 1446 XCTAssertTrue([msg1.knownMapField getEnum:&val forKey:0]); 1447 XCTAssertEqual(val, Proto2MapEnum_Proto2MapEnumFoo); 1448 XCTAssertEqual(msg1.unknownFields.countOfFields, 1U); 1449 1450 data = [msg1 data]; 1451 TestEnumMapPlusExtra *msg2 = 1452 [TestEnumMapPlusExtra parseFromData:data error:NULL]; 1453 val = -1; 1454 XCTAssertEqual(msg2.knownMapField.count, 1U); 1455 XCTAssertTrue([msg2.knownMapField getEnum:&val forKey:0]); 1456 XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumFoo); 1457 val = -1; 1458 XCTAssertEqual(msg2.unknownMapField.count, 1U); 1459 XCTAssertTrue([msg2.unknownMapField getEnum:&val forKey:0]); 1460 XCTAssertEqual(val, Proto2MapEnumPlusExtra_EProto2MapEnumExtra); 1461 XCTAssertEqual(msg2.unknownFields.countOfFields, 0U); 1462 1463 XCTAssertEqualObjects(orig, msg2); 1464 1465 [orig release]; 1466} 1467 1468#pragma mark - Map Round Tripping 1469 1470- (void)testProto2MapRoundTripping { 1471 Message2 *msg = [[Message2 alloc] init]; 1472 1473 // Key/Value data should result in different byte lengths on wire to ensure 1474 // everything is right. 1475 [msg.mapInt32Int32 setInt32:1000 forKey:200]; 1476 [msg.mapInt32Int32 setInt32:101 forKey:2001]; 1477 [msg.mapInt64Int64 setInt64:1002 forKey:202]; 1478 [msg.mapInt64Int64 setInt64:103 forKey:2003]; 1479 [msg.mapInt64Int64 setInt64:4294967296 forKey:4294967297]; 1480 [msg.mapUint32Uint32 setUInt32:1004 forKey:204]; 1481 [msg.mapUint32Uint32 setUInt32:105 forKey:2005]; 1482 [msg.mapUint64Uint64 setUInt64:1006 forKey:206]; 1483 [msg.mapUint64Uint64 setUInt64:107 forKey:2007]; 1484 [msg.mapUint64Uint64 setUInt64:4294967298 forKey:4294967299]; 1485 [msg.mapSint32Sint32 setInt32:1008 forKey:208]; 1486 [msg.mapSint32Sint32 setInt32:109 forKey:2009]; 1487 [msg.mapSint64Sint64 setInt64:1010 forKey:210]; 1488 [msg.mapSint64Sint64 setInt64:111 forKey:2011]; 1489 [msg.mapSint64Sint64 setInt64:4294967300 forKey:4294967301]; 1490 [msg.mapFixed32Fixed32 setUInt32:1012 forKey:212]; 1491 [msg.mapFixed32Fixed32 setUInt32:113 forKey:2013]; 1492 [msg.mapFixed64Fixed64 setUInt64:1014 forKey:214]; 1493 [msg.mapFixed64Fixed64 setUInt64:115 forKey:2015]; 1494 [msg.mapFixed64Fixed64 setUInt64:4294967302 forKey:4294967303]; 1495 [msg.mapSfixed32Sfixed32 setInt32:1016 forKey:216]; 1496 [msg.mapSfixed32Sfixed32 setInt32:117 forKey:2017]; 1497 [msg.mapSfixed64Sfixed64 setInt64:1018 forKey:218]; 1498 [msg.mapSfixed64Sfixed64 setInt64:119 forKey:2019]; 1499 [msg.mapSfixed64Sfixed64 setInt64:4294967304 forKey:4294967305]; 1500 [msg.mapInt32Float setFloat:1020.f forKey:220]; 1501 [msg.mapInt32Float setFloat:121.f forKey:2021]; 1502 [msg.mapInt32Double setDouble:1022. forKey:222]; 1503 [msg.mapInt32Double setDouble:123. forKey:2023]; 1504 [msg.mapBoolBool setBool:false forKey:true]; 1505 [msg.mapBoolBool setBool:true forKey:false]; 1506 msg.mapStringString[@"224"] = @"1024"; 1507 msg.mapStringString[@"2025"] = @"125"; 1508 msg.mapStringBytes[@"226"] = DataFromCStr("1026"); 1509 msg.mapStringBytes[@"2027"] = DataFromCStr("127"); 1510 Message2 *val1 = [[Message2 alloc] init]; 1511 val1.optionalInt32 = 1028; 1512 Message2 *val2 = [[Message2 alloc] init]; 1513 val2.optionalInt32 = 129; 1514 [msg.mapStringMessage setObject:val1 forKey:@"228"]; 1515 [msg.mapStringMessage setObject:val2 forKey:@"2029"]; 1516 [msg.mapInt32Bytes setObject:DataFromCStr("1030 bytes") forKey:230]; 1517 [msg.mapInt32Bytes setObject:DataFromCStr("131") forKey:2031]; 1518 [msg.mapInt32Enum setEnum:Message2_Enum_Bar forKey:232]; 1519 [msg.mapInt32Enum setEnum:Message2_Enum_Baz forKey:2033]; 1520 Message2 *val3 = [[Message2 alloc] init]; 1521 val3.optionalInt32 = 1034; 1522 Message2 *val4 = [[Message2 alloc] init]; 1523 val4.optionalInt32 = 135; 1524 [msg.mapInt32Message setObject:val3 forKey:234]; 1525 [msg.mapInt32Message setObject:val4 forKey:2035]; 1526 1527 NSData *data = [msg data]; 1528 XCTAssertNotNil(data); 1529 Message2 *msg2 = [[Message2 alloc] initWithData:data error:NULL]; 1530 1531 XCTAssertNotEqual(msg2, msg); // Pointer comparison 1532 XCTAssertEqualObjects(msg2, msg); 1533 1534 [val4 release]; 1535 [val3 release]; 1536 [val2 release]; 1537 [val1 release]; 1538 [msg2 release]; 1539 [msg release]; 1540} 1541 1542@end 1543