OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #library("protobuftest"); |
| 6 #import('../../../lib/protobuf/runtime/Protobuf_client.dart'); |
| 7 #source('../../../lib/protobuf/plugin/protoc/descriptor.pb.dart'); |
| 8 #source('test_util.dart'); |
| 9 #source('unittest.pb.dart'); |
| 10 #source('unittest_import.pb.dart'); |
| 11 |
| 12 void main() { |
| 13 // CodedBufferReader tests |
| 14 testReadVarint(); |
| 15 testReadWholeMessage(); |
| 16 testSkipWholeMessage(); |
| 17 testReadHugeBlob(); |
| 18 testReadMaliciouslyLargeBlob(); |
| 19 testMaliciousRecursion(); |
| 20 testSizeLimit(); |
| 21 testReadInvalidUtf8(); |
| 22 testInvalidTag(); |
| 23 |
| 24 // CodedBufferWriter tests |
| 25 testWriteWholeMessage(); |
| 26 testWriteWholePackedFieldsMessage(); |
| 27 testWriteMessageWithNegativeEnumValue(); |
| 28 |
| 29 // Wire protocol tests |
| 30 testBuffer(); |
| 31 testSerialization(); |
| 32 testSerializationPacked(); |
| 33 testSerializeExtensions(); |
| 34 testSerializePackedExtensions(); |
| 35 testSerializationPackedWithoutGetSerializedSize(); |
| 36 testParseExtensions(); |
| 37 testParsePackedExtensions(); |
| 38 testExtensionsSerializedSize(); |
| 39 |
| 40 // GeneratedMessage tests |
| 41 testMessageOrBuilder(); |
| 42 testUsingBuilderMultipleTimes(); |
| 43 testProtosShareRepeatedArraysIfDidntChange(); |
| 44 testRepeatedArraysAreImmutable(); |
| 45 testSettersRejectNull(); |
| 46 testRepeatedSetters(); |
| 47 testRepeatedSettersRejectNull(); |
| 48 testRepeatedAppend(); |
| 49 testRepeatedAppendRejectsNull(); |
| 50 testSettingForeignMessageUsingBuilder(); |
| 51 testSettingRepeatedForeignMessageUsingBuilder(); |
| 52 testDefaults(); |
| 53 testClear(); |
| 54 testEnumInterface(); |
| 55 testEnumMap(); |
| 56 testParsePackedToUnpacked(); |
| 57 testParseUnpackedToPacked(); |
| 58 testExtensionMessageOrBuilder(); |
| 59 testExtensionRepeatedSetters(); |
| 60 testExtensionDefaults(); |
| 61 testClearExtension(); |
| 62 testExtensionCopy(); |
| 63 testExtensionMergeFrom(); |
| 64 testToBuilder(); |
| 65 testRecursiveMessageDefaultInstance(); |
| 66 testSerialize(); |
| 67 testSerializePartial(); |
| 68 testEnumValues(); |
| 69 testBadExtension(); |
| 70 |
| 71 // JSON tests |
| 72 testOutput(); |
| 73 testBase64Encode(); |
| 74 testBase64Decode(); |
| 75 testParse(); |
| 76 testExtensionsOutput(); |
| 77 testExtensionsParse(); |
| 78 |
| 79 // Message tests |
| 80 setUpMessageTests(); |
| 81 testMergeFrom(); |
| 82 testRequired(); |
| 83 testRequiredForeign(); |
| 84 testRequiredExtension(); |
| 85 testUninitializedException(); |
| 86 testBuildPartial(); |
| 87 testNestedUninitializedException(); |
| 88 testBuildNestedPartial(); |
| 89 testParseUnititialized(); |
| 90 testParseNestedUnititialized(); |
| 91 |
| 92 // UnknownFieldSet tests |
| 93 setUpUnknownFieldSetTests(); |
| 94 testVarint(); |
| 95 testFixed32(); |
| 96 testFixed64(); |
| 97 testLengthDelimited(); |
| 98 testGroup(); |
| 99 testSerialize2(); |
| 100 testCopyFrom(); |
| 101 testMergeFrom2(); |
| 102 testClear2(); |
| 103 testEmpty(); |
| 104 testClearMessage(); |
| 105 testParseKnownAndUnknown(); |
| 106 testWrongTypeTreatedAsUnknown(); |
| 107 testUnknownExtensions(); |
| 108 testWrongExtensionTypeTreatedAsUnknown(); |
| 109 testParseUnknownEnumValue(); |
| 110 testLargeVarint(); |
| 111 testEqualsAndHashCode(); |
| 112 } |
| 113 |
| 114 void testReadVarint() { |
| 115 _assertReadRawVarint([0x00], [0x00, 0x00, 0x00, 0x00]); |
| 116 _assertReadRawVarint([0x01], [0x01, 0x00, 0x00, 0x00]); |
| 117 _assertReadRawVarint([0x7f], [0x7f, 0x00, 0x00, 0x00]); |
| 118 _assertReadRawVarint([0xa2, 0x74], [0xa2, 0x74, 0x00, 0x00]); |
| 119 _assertReadRawVarint([0xbe, 0xf7, 0x92, 0x84, 0x0b], [0xbe, 0xf7, 0x92, |
| 120 0x84, 0x0b, 0x00, 0x00, 0x00, 0x00]); |
| 121 _assertReadRawVarint( |
| 122 [0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01], |
| 123 [0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01, 0x00]); |
| 124 _assertReadVarintFailure(<int>[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, |
| 125 0x80, 0x80, 0x80, 0x00]); |
| 126 _assertReadVarintFailure(<int>[0x80]); |
| 127 } |
| 128 |
| 129 void _assertReadRawVarint(List<int> expected, List<int> src) { |
| 130 CodedBufferReader reader = new CodedBufferReader.fromBuffer(src); |
| 131 Expect.listEquals(expected, reader.readRawVarint()); |
| 132 } |
| 133 |
| 134 void _assertReadVarintFailure(List<int> src) { |
| 135 try { |
| 136 CodedBufferReader reader = new CodedBufferReader.fromBuffer(src); |
| 137 reader.readRawVarint(); |
| 138 Expect.fail("Should throw exception"); |
| 139 } catch (InvalidProtocolBufferException e) { |
| 140 // success |
| 141 } |
| 142 } |
| 143 |
| 144 void testReadWholeMessage() { |
| 145 Protobuf_Unittest_TestAllTypes message = getAllSet(); |
| 146 List<int> rawBytes = message.writeToBuffer(); |
| 147 Expect.equals(rawBytes.length, message.getSerializedSize()); |
| 148 |
| 149 Protobuf_Unittest_TestAllTypes message2 = |
| 150 Protobuf_Unittest_TestAllTypes.parseFromBuffer(rawBytes); |
| 151 assertAllFieldsSet(message2); |
| 152 } |
| 153 |
| 154 /** Tests skipField(). */ |
| 155 void testSkipWholeMessage() { |
| 156 Protobuf_Unittest_TestAllTypes message = getAllSet(); |
| 157 List<int> rawBytes = message.writeToBuffer(); |
| 158 |
| 159 // Create two parallel inputs. Parse one as unknown fields while using |
| 160 // skipField() to skip each field on the other. Expect the same tags. |
| 161 CodedBufferReader input1 = new CodedBufferReader.fromBuffer(rawBytes); |
| 162 CodedBufferReader input2 = new CodedBufferReader.fromBuffer(rawBytes); |
| 163 UnknownFieldSet_Builder unknownFields = new UnknownFieldSet_Builder(); |
| 164 |
| 165 while (true) { |
| 166 int input1Tag = input1.readTag(); |
| 167 int input2Tag = input2.readTag(); |
| 168 Expect.equals(input1Tag, input2Tag); |
| 169 if (input1Tag == 0) { |
| 170 break; |
| 171 } |
| 172 unknownFields.mergeFieldFromBuffer(input1Tag, input1); |
| 173 input2.skipField(input2Tag); |
| 174 } |
| 175 } |
| 176 |
| 177 void testReadHugeBlob() { |
| 178 // Allocate and initialize a 1MB blob. |
| 179 List<int> blob = new List<int>(1 << 20); |
| 180 for (int i = 0; i < blob.length; i++) { |
| 181 blob[i] = i % 256; |
| 182 } |
| 183 |
| 184 // Make a message containing it. |
| 185 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 186 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 187 setAllFields(builder); |
| 188 builder.optionalBytes = blob; |
| 189 Protobuf_Unittest_TestAllTypes message = builder.build(); |
| 190 |
| 191 Protobuf_Unittest_TestAllTypes message2 = |
| 192 Protobuf_Unittest_TestAllTypes.parseFromBuffer(message.writeToBuffer()); |
| 193 |
| 194 Expect.listEquals(message.optionalBytes, message2.optionalBytes); |
| 195 } |
| 196 |
| 197 void testReadMaliciouslyLargeBlob() { |
| 198 CodedBufferWriter output = new CodedBufferWriter(38); |
| 199 |
| 200 int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED); |
| 201 output.writeRawBytes(PbCodec.int32ToBytes(tag)); |
| 202 output.writeInt32NoTag(0x7FFFFFFF); |
| 203 // Pad with a few random bytes. |
| 204 List<int> randomBits = <int>[]; |
| 205 randomBits.insertRange(0, 32, 47); |
| 206 output.writeRawBytes(randomBits); |
| 207 |
| 208 CodedBufferReader input = new CodedBufferReader.fromBuffer(output.buffer); |
| 209 Expect.equals(tag, input.readTag()); |
| 210 |
| 211 try { |
| 212 input.readBytes(); |
| 213 Expect.fail("Should have thrown an exception!"); |
| 214 } catch (InvalidProtocolBufferException e) { |
| 215 // success. |
| 216 } |
| 217 } |
| 218 |
| 219 void testMaliciousRecursion() { |
| 220 List<int> data64 = _makeRecursiveMessage(64).writeToBuffer(); |
| 221 List<int> data65 = _makeRecursiveMessage(65).writeToBuffer(); |
| 222 |
| 223 _assertMessageDepth( |
| 224 Protobuf_Unittest_TestRecursiveMessage.parseFromBuffer(data64), 64); |
| 225 |
| 226 try { |
| 227 Protobuf_Unittest_TestRecursiveMessage.parseFromBuffer(data65); |
| 228 Expect.fail("Should have thrown an exception!"); |
| 229 } catch (InvalidProtocolBufferException e) { |
| 230 // success. |
| 231 } |
| 232 |
| 233 CodedBufferReader input = new CodedBufferReader.fromBuffer(data64, 8); |
| 234 try { |
| 235 // uncomfortable alternative to below... |
| 236 new Protobuf_Unittest_TestRecursiveMessage_Builder() |
| 237 .mergeFromCodedBufferReader(input); |
| 238 Expect.fail("Should have thrown an exception!"); |
| 239 } catch (InvalidProtocolBufferException e) { |
| 240 // success. |
| 241 } |
| 242 } |
| 243 |
| 244 Protobuf_Unittest_TestRecursiveMessage _makeRecursiveMessage(int depth) { |
| 245 if (depth == 0) { |
| 246 Protobuf_Unittest_TestRecursiveMessage_Builder builder = |
| 247 new Protobuf_Unittest_TestRecursiveMessage_Builder(); |
| 248 builder.i = 5; |
| 249 return builder.build(); |
| 250 } else { |
| 251 Protobuf_Unittest_TestRecursiveMessage_Builder builder = |
| 252 new Protobuf_Unittest_TestRecursiveMessage_Builder(); |
| 253 builder.a = _makeRecursiveMessage(depth - 1); |
| 254 return builder.build(); |
| 255 } |
| 256 } |
| 257 |
| 258 void _assertMessageDepth(Protobuf_Unittest_TestRecursiveMessage message, |
| 259 int depth) { |
| 260 if (depth == 0) { |
| 261 Expect.isFalse(message.hasA()); |
| 262 Expect.equals(5, message.i); |
| 263 } else { |
| 264 Expect.isTrue(message.hasA()); |
| 265 _assertMessageDepth(message.a, depth - 1); |
| 266 } |
| 267 } |
| 268 |
| 269 void testSizeLimit() { |
| 270 CodedBufferReader input = new CodedBufferReader.fromBuffer( |
| 271 getAllSet().writeToBuffer(), sizeLimit: 16); |
| 272 |
| 273 try { |
| 274 // uncomfortable alternative to below... |
| 275 new Protobuf_Unittest_TestAllTypes_Builder() |
| 276 .mergeFromCodedBufferReader(input); |
| 277 Expect.fail("Size limit too small, should have thrown an exception!"); |
| 278 } catch (InvalidProtocolBufferException e) { |
| 279 // success. |
| 280 } |
| 281 } |
| 282 |
| 283 /** |
| 284 * Tests that if we read an string that contains invalid UTF-8, no exception |
| 285 * is thrown. Instead, the invalid bytes are replaced with the Unicode |
| 286 * "replacement character" U+FFFD. |
| 287 */ |
| 288 void testReadInvalidUtf8() { |
| 289 CodedBufferWriter writer = new CodedBufferWriter(3); |
| 290 writer.writeBytes(1, [0x80]); |
| 291 |
| 292 CodedBufferReader input = new CodedBufferReader.fromBuffer(writer.buffer); |
| 293 Expect.equals( |
| 294 WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), |
| 295 input.readTag()); |
| 296 String text = input.readString(); |
| 297 Expect.equals(0xfffd, text.charCodeAt(0)); |
| 298 } |
| 299 |
| 300 void testInvalidTag() { |
| 301 // Any tag number which corresponds to field number zero is invalid and |
| 302 // should throw InvalidProtocolBufferException. |
| 303 for (int i = 0; i < 8; i++) { |
| 304 try { |
| 305 new CodedBufferReader.fromBuffer([i]).readTag(); |
| 306 Expect.fail("Should have thrown an exception."); |
| 307 } catch (InvalidProtocolBufferException e) { |
| 308 // success |
| 309 } |
| 310 } |
| 311 } |
| 312 |
| 313 void testWriteWholeMessage() { |
| 314 Protobuf_Unittest_TestAllTypes message = getAllSet(); |
| 315 |
| 316 List<int> rawBytes = message.writeToBuffer(); |
| 317 Expect.listEquals(goldenMessage, rawBytes); |
| 318 } |
| 319 |
| 320 void testWriteWholePackedFieldsMessage() { |
| 321 List<int> rawBytes = getPackedSet().writeToBuffer(); |
| 322 Expect.listEquals(goldenPackedMessage, rawBytes); |
| 323 } |
| 324 |
| 325 void testWriteMessageWithNegativeEnumValue() { |
| 326 Protobuf_Unittest_SparseEnumMessage_Builder builder = |
| 327 new Protobuf_Unittest_SparseEnumMessage_Builder(); |
| 328 builder.sparseEnum = Protobuf_Unittest_TestSparseEnum.SPARSE_E; |
| 329 Protobuf_Unittest_SparseEnumMessage message = builder.build(); |
| 330 Expect.isTrue(message.sparseEnum.value < 0, "enum.value should be -53452"); |
| 331 List<int> buffer = message.writeToBuffer(); |
| 332 Protobuf_Unittest_SparseEnumMessage message2 = |
| 333 Protobuf_Unittest_SparseEnumMessage.parseFromBuffer(buffer); |
| 334 Expect.equals(Protobuf_Unittest_TestSparseEnum.SPARSE_E, |
| 335 message2.sparseEnum, "should resolve back to SPARSE_E"); |
| 336 |
| 337 } |
| 338 |
| 339 void testBuffer() { |
| 340 GoogleProtobuf_DescriptorProto_ExtensionRange_Builder builder = |
| 341 new GoogleProtobuf_DescriptorProto_ExtensionRange_Builder(); |
| 342 builder.start = 1; |
| 343 builder.end = 10000; |
| 344 GoogleProtobuf_DescriptorProto_ExtensionRange range = builder.build(); |
| 345 |
| 346 List<int> wireFormat = range.writeToBuffer(); |
| 347 Expect.equals(8, wireFormat[0]); // tag 1, type 0 (varint) |
| 348 Expect.equals(1, wireFormat[1]); // value 1 |
| 349 Expect.equals(16, wireFormat[2]); // tag 2, type 0 (varint) |
| 350 Expect.equals(144, wireFormat[3]); // value 10000 = (78 << 7) + (144 & 0x7f); |
| 351 Expect.equals(78, wireFormat[4]); |
| 352 |
| 353 // Set values manually |
| 354 wireFormat[1] = 120; |
| 355 wireFormat[3] = 185; |
| 356 wireFormat[4] = 96; |
| 357 |
| 358 GoogleProtobuf_DescriptorProto_ExtensionRange range2 = |
| 359 GoogleProtobuf_DescriptorProto_ExtensionRange.parseFromBuffer(wireFormat); |
| 360 Expect.equals(120, range2.start); |
| 361 Expect.equals(12345, range2.end); |
| 362 } |
| 363 |
| 364 void testSerialization() { |
| 365 Protobuf_Unittest_TestAllTypes message = getAllSet(); |
| 366 |
| 367 List<int> rawBytes = message.writeToBuffer(); |
| 368 Expect.equals(rawBytes.length, message.getSerializedSize()); |
| 369 |
| 370 Protobuf_Unittest_TestAllTypes message2 = |
| 371 Protobuf_Unittest_TestAllTypes.parseFromBuffer(rawBytes); |
| 372 assertAllFieldsSet(message2); |
| 373 } |
| 374 |
| 375 |
| 376 void testSerializationPacked() { |
| 377 Protobuf_Unittest_TestPackedTypes message = getPackedSet(); |
| 378 |
| 379 List<int> rawBytes = message.writeToBuffer(); |
| 380 Expect.equals(rawBytes.length, message.getSerializedSize()); |
| 381 |
| 382 Protobuf_Unittest_TestPackedTypes message2 = |
| 383 Protobuf_Unittest_TestPackedTypes.parseFromBuffer(rawBytes); |
| 384 assertPackedFieldsSet(message2); |
| 385 } |
| 386 |
| 387 void testSerializeExtensions() { |
| 388 Protobuf_Unittest_TestAllExtensions message = getAllExtensionsSet(); |
| 389 List<int> rawBytes = message.writeToBuffer(); |
| 390 Expect.equals(rawBytes.length, message.getSerializedSize()); |
| 391 |
| 392 Protobuf_Unittest_TestAllTypes message2 = |
| 393 Protobuf_Unittest_TestAllTypes.parseFromBuffer(rawBytes); |
| 394 assertAllFieldsSet(message2); |
| 395 } |
| 396 |
| 397 void testSerializePackedExtensions() { |
| 398 Protobuf_Unittest_TestPackedExtensions message = getPackedExtensionsSet(); |
| 399 List<int> rawBytes = message.writeToBuffer(); |
| 400 |
| 401 Protobuf_Unittest_TestPackedTypes message2 = getPackedSet(); |
| 402 List<int> rawBytes2 = message2.writeToBuffer(); |
| 403 |
| 404 Expect.listEquals(rawBytes, rawBytes2); |
| 405 } |
| 406 |
| 407 void testSerializationPackedWithoutGetSerializedSize() { |
| 408 // At one point, the Java impl. had a bug where size was not computed |
| 409 // unless the getSerializedSize() was called on the message outside |
| 410 // of the writing of the message. |
| 411 Protobuf_Unittest_TestPackedTypes message = getPackedSet(); |
| 412 |
| 413 // Directly construct a CodedBufferWriter around a buffer, in case |
| 414 // writeToBuffer() invokes getSerializedSize(); |
| 415 CodedBufferWriter writer = new CodedBufferWriter(10000); |
| 416 message.writeToCodedBufferWriter(writer); |
| 417 |
| 418 List<int> buffer = writer.buffer.getRange(0, writer.length); |
| 419 Protobuf_Unittest_TestPackedTypes message2 = |
| 420 Protobuf_Unittest_TestPackedTypes.parseFromBuffer(buffer); |
| 421 |
| 422 assertPackedFieldsSet(message2); |
| 423 } |
| 424 |
| 425 void testParseExtensions() { |
| 426 // TestAllTypes and TestAllExtensions should have compatible wire formats, |
| 427 // so if we serialize a TestAllTypes then parse it as TestAllExtensions |
| 428 // it should work. |
| 429 |
| 430 Protobuf_Unittest_TestAllTypes message = getAllSet(); |
| 431 List<int> rawBytes = message.writeToBuffer(); |
| 432 |
| 433 ExtensionRegistry registry = getExtensionRegistry(); |
| 434 |
| 435 Protobuf_Unittest_TestAllExtensions message2 = |
| 436 Protobuf_Unittest_TestAllExtensions.parseFromBuffer(rawBytes, registry); |
| 437 |
| 438 assertAllExtensionsSet(message2); |
| 439 } |
| 440 |
| 441 void testParsePackedExtensions() { |
| 442 // Ensure that packed extensions can be properly parsed. |
| 443 Protobuf_Unittest_TestPackedExtensions message = getPackedExtensionsSet(); |
| 444 List<int> rawBytes = message.writeToBuffer(); |
| 445 |
| 446 ExtensionRegistry registry = getExtensionRegistry(); |
| 447 |
| 448 Protobuf_Unittest_TestPackedExtensions message2 = |
| 449 Protobuf_Unittest_TestPackedExtensions.parseFromBuffer(rawBytes, |
| 450 registry); |
| 451 |
| 452 assertPackedExtensionsSet(message2); |
| 453 } |
| 454 |
| 455 void testExtensionsSerializedSize() { |
| 456 Expect.equals(getAllSet().getSerializedSize(), |
| 457 getAllExtensionsSet().getSerializedSize()); |
| 458 } |
| 459 |
| 460 void testMessageOrBuilder() { |
| 461 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 462 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 463 setAllFields(builder); |
| 464 Protobuf_Unittest_TestAllTypes message = builder.build(); |
| 465 assertAllFieldsSet(message); |
| 466 } |
| 467 |
| 468 void testUsingBuilderMultipleTimes() { |
| 469 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 470 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 471 // primitive field scalar and repeated |
| 472 builder.optionalSfixed64 = 100; |
| 473 builder.repeatedInt32.add(100); |
| 474 // enum field scalar and repeated |
| 475 builder.optionalImportEnum = Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR; |
| 476 builder.repeatedImportEnum.add( |
| 477 Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR); |
| 478 // proto field scalar and repeated |
| 479 Protobuf_Unittest_ForeignMessage_Builder optionalForeignMessageBuilder = |
| 480 new Protobuf_Unittest_ForeignMessage_Builder(); |
| 481 optionalForeignMessageBuilder.c = 1; |
| 482 builder.optionalForeignMessage = optionalForeignMessageBuilder.build(); |
| 483 builder.repeatedForeignMessage.add(optionalForeignMessageBuilder.build()); |
| 484 |
| 485 Protobuf_Unittest_TestAllTypes value1 = builder.build(); |
| 486 |
| 487 Expect.equals(100, value1.optionalSfixed64); |
| 488 Expect.equals(100, value1.repeatedInt32[0]); |
| 489 Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR, |
| 490 value1.optionalImportEnum); |
| 491 Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR, |
| 492 value1.repeatedImportEnum[0]); |
| 493 Expect.equals(1, value1.optionalForeignMessage.c); |
| 494 Expect.equals(1, value1.repeatedForeignMessage[0].c); |
| 495 |
| 496 // Make sure that builder didn't update previously created values |
| 497 builder.optionalSfixed64 = 200; |
| 498 builder.repeatedInt32[0] = 200; |
| 499 builder.optionalImportEnum = Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO; |
| 500 builder.repeatedImportEnum[0] = |
| 501 Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO; |
| 502 optionalForeignMessageBuilder.c = 2; |
| 503 builder.optionalForeignMessage = optionalForeignMessageBuilder.build(); |
| 504 builder.repeatedForeignMessage[0] = optionalForeignMessageBuilder.build(); |
| 505 |
| 506 Protobuf_Unittest_TestAllTypes value2 = builder.build(); |
| 507 |
| 508 // Make sure value1 didn't change. |
| 509 Expect.equals(100, value1.optionalSfixed64); |
| 510 Expect.equals(100, value1.repeatedInt32[0]); |
| 511 Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR, |
| 512 value1.optionalImportEnum); |
| 513 Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR, |
| 514 value1.repeatedImportEnum[0]); |
| 515 Expect.equals(1, value1.optionalForeignMessage.c); |
| 516 Expect.equals(1, value1.repeatedForeignMessage[0].c); |
| 517 |
| 518 // Make sure value2 is correct |
| 519 Expect.equals(200, value2.optionalSfixed64); |
| 520 Expect.equals(200, value2.repeatedInt32[0]); |
| 521 Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO, |
| 522 value2.optionalImportEnum); |
| 523 Expect.equals(Protobuf_Unittest_Import_ImportEnum.IMPORT_FOO, |
| 524 value2.repeatedImportEnum[0]); |
| 525 Expect.equals(2, value2.optionalForeignMessage.c); |
| 526 Expect.equals(2, value2.repeatedForeignMessage[0].c); |
| 527 } |
| 528 |
| 529 void testProtosShareRepeatedArraysIfDidntChange() { |
| 530 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 531 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 532 builder.repeatedInt32.add(100); |
| 533 builder.repeatedImportEnum |
| 534 .add(Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR); |
| 535 builder.repeatedForeignMessage |
| 536 .add(Protobuf_Unittest_ForeignMessage.defaultInstance); |
| 537 |
| 538 Protobuf_Unittest_TestAllTypes value1 = builder.build(); |
| 539 Protobuf_Unittest_TestAllTypes value2 = value1.toBuilder().build(); |
| 540 |
| 541 Expect.listEquals(value1.repeatedInt32, value2.repeatedInt32); |
| 542 Expect.listEquals(value1.repeatedImportEnum, value2.repeatedImportEnum); |
| 543 Expect.listEquals(value1.repeatedForeignMessage, |
| 544 value2.repeatedForeignMessage); |
| 545 } |
| 546 |
| 547 void testRepeatedArraysAreImmutable() { |
| 548 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 549 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 550 builder.repeatedInt32.add(100); |
| 551 builder.repeatedImportEnum.add( |
| 552 Protobuf_Unittest_Import_ImportEnum.IMPORT_BAR); |
| 553 builder.repeatedForeignMessage.add( |
| 554 Protobuf_Unittest_ForeignMessage.defaultInstance); |
| 555 |
| 556 Protobuf_Unittest_TestAllTypes value = builder.build(); |
| 557 _assertIsUnmodifiable(value.repeatedInt32); |
| 558 _assertIsUnmodifiable(value.repeatedImportEnum); |
| 559 _assertIsUnmodifiable(value.repeatedForeignMessage); |
| 560 // Following line fails. Default repeating values are not properly being |
| 561 // converted into immutable lists (could be a generic empty list). |
| 562 _assertIsUnmodifiable(value.repeatedFloat); |
| 563 } |
| 564 |
| 565 void _assertIsUnmodifiable(List list) { |
| 566 try { |
| 567 list.clear(); |
| 568 Expect.fail("List wasn't immutable"); |
| 569 } catch (UnsupportedOperationException e) { |
| 570 // good |
| 571 } |
| 572 } |
| 573 |
| 574 void testSettersRejectNull() { |
| 575 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 576 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 577 try { |
| 578 builder.optionalString = null; |
| 579 Expect.fail("Exception was not thrown"); |
| 580 } catch (NullPointerException e) { |
| 581 // We expect this exception. |
| 582 } |
| 583 try { |
| 584 builder.optionalBytes = null; |
| 585 Expect.fail("Exception was not thrown"); |
| 586 } catch (NullPointerException e) { |
| 587 // We expect this exception. |
| 588 } |
| 589 try { |
| 590 builder.optionalNestedMessage = null; |
| 591 Expect.fail("Exception was not thrown"); |
| 592 } catch (NullPointerException e) { |
| 593 // We expect this exception. |
| 594 } |
| 595 try { |
| 596 builder.optionalNestedMessage =null; |
| 597 Expect.fail("Exception was not thrown"); |
| 598 } catch (NullPointerException e) { |
| 599 // We expect this exception. |
| 600 } |
| 601 try { |
| 602 builder.optionalNestedEnum = null; |
| 603 Expect.fail("Exception was not thrown"); |
| 604 } catch (NullPointerException e) { |
| 605 // We expect this exception. |
| 606 } |
| 607 try { |
| 608 builder.repeatedString.add(null); |
| 609 Expect.fail("Exception was not thrown"); |
| 610 } catch (NullPointerException e) { |
| 611 // We expect this exception. |
| 612 } |
| 613 try { |
| 614 builder.repeatedBytes.add(null); |
| 615 Expect.fail("Exception was not thrown"); |
| 616 } catch (NullPointerException e) { |
| 617 // We expect this exception. |
| 618 } |
| 619 try { |
| 620 builder.repeatedNestedMessage.add(null); |
| 621 Expect.fail("Exception was not thrown"); |
| 622 } catch (NullPointerException e) { |
| 623 // We expect this exception. |
| 624 } |
| 625 try { |
| 626 builder.repeatedNestedMessage.add(null); |
| 627 Expect.fail("Exception was not thrown"); |
| 628 } catch (NullPointerException e) { |
| 629 // We expect this exception. |
| 630 } |
| 631 try { |
| 632 builder.repeatedNestedEnum.add(null); |
| 633 Expect.fail("Exception was not thrown"); |
| 634 } catch (NullPointerException e) { |
| 635 // We expect this exception. |
| 636 } |
| 637 } |
| 638 |
| 639 void testRepeatedSetters() { |
| 640 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 641 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 642 setAllFields(builder); |
| 643 modifyRepeatedFields(builder); |
| 644 Protobuf_Unittest_TestAllTypes message = builder.build(); |
| 645 assertRepeatedFieldsModified(message); |
| 646 } |
| 647 |
| 648 void testRepeatedSettersRejectNull() { |
| 649 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 650 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 651 |
| 652 builder.repeatedString.add("one"); |
| 653 builder.repeatedString.add("two"); |
| 654 try { |
| 655 builder.repeatedString[1] = null; |
| 656 Expect.fail("Exception was not thrown"); |
| 657 } catch (NullPointerException e) { |
| 658 // We expect this exception. |
| 659 } |
| 660 |
| 661 builder.repeatedBytes.add("one".charCodes()); |
| 662 builder.repeatedBytes.add("two".charCodes()); |
| 663 try { |
| 664 builder.repeatedBytes[1] = null; |
| 665 Expect.fail("Exception was not thrown"); |
| 666 } catch (NullPointerException e) { |
| 667 // We expect this exception. |
| 668 } |
| 669 |
| 670 Protobuf_Unittest_TestAllTypes_NestedMessage_Builder repeatedNestedBuilder = |
| 671 new Protobuf_Unittest_TestAllTypes_NestedMessage_Builder(); |
| 672 repeatedNestedBuilder.bb = 318; |
| 673 builder.repeatedNestedMessage.add(repeatedNestedBuilder.build()); |
| 674 repeatedNestedBuilder.bb = 456; |
| 675 builder.repeatedNestedMessage.add(repeatedNestedBuilder.build()); |
| 676 |
| 677 try { |
| 678 builder.repeatedNestedMessage[1] = null; |
| 679 Expect.fail("Exception was not thrown"); |
| 680 } catch (NullPointerException e) { |
| 681 // We expect this exception. |
| 682 } |
| 683 try { |
| 684 builder.repeatedNestedMessage[1] = null; |
| 685 Expect.fail("Exception was not thrown"); |
| 686 } catch (NullPointerException e) { |
| 687 // We expect this exception. |
| 688 } |
| 689 |
| 690 builder.repeatedNestedEnum |
| 691 .add(Protobuf_Unittest_TestAllTypes_NestedEnum.FOO); |
| 692 builder.repeatedNestedEnum |
| 693 .add(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR); |
| 694 try { |
| 695 builder.repeatedNestedEnum[1] = null; |
| 696 Expect.fail("Exception was not thrown"); |
| 697 } catch (NullPointerException e) { |
| 698 // We expect this exception. |
| 699 } |
| 700 } |
| 701 |
| 702 void testRepeatedAppend() { |
| 703 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 704 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 705 |
| 706 builder.repeatedInt32.addAll([1, 2, 3, 4]); |
| 707 builder.repeatedForeignEnum.addAll([ |
| 708 Protobuf_Unittest_ForeignEnum.FOREIGN_BAZ]); |
| 709 |
| 710 Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder = |
| 711 new Protobuf_Unittest_ForeignMessage_Builder(); |
| 712 foreignMessageBuilder.c = 12; |
| 713 builder.repeatedForeignMessage.addAll([foreignMessageBuilder.build()]); |
| 714 |
| 715 Protobuf_Unittest_TestAllTypes message = builder.build(); |
| 716 Expect.listEquals([1, 2, 3, 4], message.repeatedInt32, "s.b. [1,2,3,4]"); |
| 717 Expect.listEquals([Protobuf_Unittest_ForeignEnum.FOREIGN_BAZ], |
| 718 message.repeatedForeignEnum); |
| 719 Expect.equals(1, message.repeatedForeignMessage.length); |
| 720 Expect.equals(12, message.repeatedForeignMessage[0].c); |
| 721 } |
| 722 |
| 723 void testRepeatedAppendRejectsNull() { |
| 724 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 725 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 726 |
| 727 Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder = |
| 728 new Protobuf_Unittest_ForeignMessage_Builder(); |
| 729 foreignMessageBuilder.c = 12; |
| 730 |
| 731 try { |
| 732 builder.repeatedForeignMessage.addAll([foreignMessageBuilder.build(), |
| 733 null]); |
| 734 Expect.fail("Exception was not thrown"); |
| 735 } catch (NullPointerException e) { |
| 736 // We expect this exception. |
| 737 } |
| 738 |
| 739 try { |
| 740 builder.repeatedForeignEnum.addAll([ |
| 741 Protobuf_Unittest_ForeignEnum.FOREIGN_BAZ, null]); |
| 742 Expect.fail("Exception was not thrown"); |
| 743 } catch (NullPointerException e) { |
| 744 // We expect this exception. |
| 745 } |
| 746 |
| 747 try { |
| 748 builder.repeatedString.addAll(["one", null]); |
| 749 Expect.fail("Exception was not thrown"); |
| 750 } catch (NullPointerException e) { |
| 751 // We expect this exception. |
| 752 } |
| 753 |
| 754 try { |
| 755 builder.repeatedBytes.addAll(["one".charCodes(), null]); |
| 756 Expect.fail("Exception was not thrown"); |
| 757 } catch (NullPointerException e) { |
| 758 // We expect this exception. |
| 759 } |
| 760 } |
| 761 |
| 762 void testSettingForeignMessageUsingBuilder() { |
| 763 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 764 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 765 // Pass builder for foreign message instance. |
| 766 Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder = |
| 767 new Protobuf_Unittest_ForeignMessage_Builder(); |
| 768 foreignMessageBuilder.c = 123; |
| 769 builder.optionalForeignMessage = foreignMessageBuilder.build(); |
| 770 Protobuf_Unittest_TestAllTypes message = builder.build(); |
| 771 |
| 772 builder = new Protobuf_Unittest_TestAllTypes_Builder(); |
| 773 // Pass builder for foreign message instance. |
| 774 foreignMessageBuilder = new Protobuf_Unittest_ForeignMessage_Builder(); |
| 775 foreignMessageBuilder.c = 123; |
| 776 builder.optionalForeignMessage = foreignMessageBuilder.build(); |
| 777 Protobuf_Unittest_TestAllTypes expectedMessage = builder.build(); |
| 778 |
| 779 // TODO: In Java version, ngd says: |
| 780 // Upgrade to using real #equals method once implemented |
| 781 Expect.stringEquals(expectedMessage.toString(), message.toString()); |
| 782 } |
| 783 |
| 784 void testSettingRepeatedForeignMessageUsingBuilder() { |
| 785 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 786 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 787 // Pass builder for foreign message instance. |
| 788 Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder = |
| 789 new Protobuf_Unittest_ForeignMessage_Builder(); |
| 790 foreignMessageBuilder.c = 456; |
| 791 builder.repeatedForeignMessage.add(foreignMessageBuilder.build()); |
| 792 Protobuf_Unittest_TestAllTypes message = builder.build(); |
| 793 |
| 794 builder = new Protobuf_Unittest_TestAllTypes_Builder(); |
| 795 // Pass builder for foreign message instance. |
| 796 foreignMessageBuilder = new Protobuf_Unittest_ForeignMessage_Builder(); |
| 797 foreignMessageBuilder.c = 456; |
| 798 builder.repeatedForeignMessage.add(foreignMessageBuilder.build()); |
| 799 Protobuf_Unittest_TestAllTypes expectedMessage = builder.build(); |
| 800 |
| 801 // TODO(ngd): Upgrade to using real #equals method once implemented |
| 802 Expect.stringEquals(expectedMessage.toString(), message.toString()); |
| 803 } |
| 804 |
| 805 void testDefaults() { |
| 806 assertClear(Protobuf_Unittest_TestAllTypes.defaultInstance); |
| 807 assertClear((new Protobuf_Unittest_TestAllTypes_Builder()).build()); |
| 808 |
| 809 Protobuf_Unittest_TestExtremeDefaultValues message = |
| 810 Protobuf_Unittest_TestExtremeDefaultValues.defaultInstance; |
| 811 |
| 812 Expect.stringEquals("\u1234", message.utf8String); |
| 813 Expect.identical(double.INFINITY, message.infDouble); |
| 814 Expect.identical(double.NEGATIVE_INFINITY, message.negInfDouble); |
| 815 Expect.isTrue(message.nanDouble.isNaN()); |
| 816 Expect.identical(double.INFINITY, message.infFloat); |
| 817 Expect.identical(double.NEGATIVE_INFINITY, message.negInfFloat); |
| 818 Expect.isTrue(message.nanFloat.isNaN()); |
| 819 Expect.stringEquals("? ? ?? ?? ??? ??/ ??-", message.cppTrigraph); |
| 820 } |
| 821 |
| 822 void testClear() { |
| 823 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 824 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 825 |
| 826 assertClear(builder.build()); |
| 827 setAllFields(builder); |
| 828 builder.clear(); |
| 829 assertClear(builder.build()); |
| 830 } |
| 831 |
| 832 void testEnumInterface() { |
| 833 Expect.isTrue( |
| 834 Protobuf_Unittest_TestAllTypes.defaultInstance.defaultNestedEnum |
| 835 is ProtobufEnum); |
| 836 } |
| 837 |
| 838 void testEnumMap() { |
| 839 for (Protobuf_Unittest_ForeignEnum value in |
| 840 Protobuf_Unittest_ForeignEnum.values) { |
| 841 Expect.equals(value, Protobuf_Unittest_ForeignEnum.valueOf(value.value)); |
| 842 } |
| 843 Expect.isNull(Protobuf_Unittest_ForeignEnum.valueOf(12345)); |
| 844 } |
| 845 |
| 846 void testParsePackedToUnpacked() { |
| 847 Protobuf_Unittest_TestUnpackedTypes_Builder builder = |
| 848 new Protobuf_Unittest_TestUnpackedTypes_Builder(); |
| 849 Protobuf_Unittest_TestUnpackedTypes message = |
| 850 builder.mergeFromBuffer(getPackedSet().writeToBuffer()).build(); |
| 851 assertUnpackedFieldsSet(message); |
| 852 } |
| 853 |
| 854 void testParseUnpackedToPacked() { |
| 855 Protobuf_Unittest_TestPackedTypes_Builder builder = |
| 856 new Protobuf_Unittest_TestPackedTypes_Builder(); |
| 857 Protobuf_Unittest_TestPackedTypes message = |
| 858 builder.mergeFromBuffer(getUnpackedSet().writeToBuffer()).build(); |
| 859 assertPackedFieldsSet(message); |
| 860 } |
| 861 |
| 862 // ================================================================= |
| 863 // Extensions. |
| 864 void testExtensionMessageOrBuilder() { |
| 865 Protobuf_Unittest_TestAllExtensions_Builder builder = |
| 866 new Protobuf_Unittest_TestAllExtensions_Builder(); |
| 867 setAllExtensions(builder); |
| 868 Protobuf_Unittest_TestAllExtensions message = builder.build(); |
| 869 assertAllExtensionsSet(message); |
| 870 } |
| 871 |
| 872 void testExtensionRepeatedSetters() { |
| 873 Protobuf_Unittest_TestAllExtensions_Builder builder = |
| 874 new Protobuf_Unittest_TestAllExtensions_Builder(); |
| 875 setAllExtensions(builder); |
| 876 modifyRepeatedExtensions(builder); |
| 877 Protobuf_Unittest_TestAllExtensions message = builder.build(); |
| 878 assertRepeatedExtensionsModified(message); |
| 879 } |
| 880 |
| 881 void testExtensionDefaults() { |
| 882 assertExtensionsClear(Protobuf_Unittest_TestAllExtensions |
| 883 .defaultInstance); |
| 884 assertExtensionsClear(new Protobuf_Unittest_TestAllExtensions_Builder() |
| 885 .build()); |
| 886 } |
| 887 |
| 888 void testClearExtension() { |
| 889 // clearExtension() is not actually used in TestUtil, so try it manually. |
| 890 var builder = new Protobuf_Unittest_TestAllExtensions_Builder(); |
| 891 builder.setExtension(Unittest.optionalInt32Extension, 1); |
| 892 builder.clearExtension(Unittest.optionalInt32Extension); |
| 893 Expect.isFalse(builder.hasExtension( |
| 894 Unittest.optionalInt32Extension)); |
| 895 |
| 896 builder = new Protobuf_Unittest_TestAllExtensions_Builder(); |
| 897 builder.addExtension(Unittest.repeatedInt32Extension, 1); |
| 898 builder.clearExtension(Unittest.repeatedInt32Extension); |
| 899 Expect.equals(0, builder.getExtensionCount( |
| 900 Unittest.repeatedInt32Extension)); |
| 901 } |
| 902 |
| 903 void testExtensionCopy() { |
| 904 Protobuf_Unittest_TestAllExtensions original = getAllExtensionsSet(); |
| 905 |
| 906 Protobuf_Unittest_TestAllExtensions_Builder builder = |
| 907 new Protobuf_Unittest_TestAllExtensions_Builder(); |
| 908 builder.mergeFromMessage(original); |
| 909 Protobuf_Unittest_TestAllExtensions copy = builder.build(); |
| 910 assertAllExtensionsSet(copy); |
| 911 } |
| 912 |
| 913 void testExtensionMergeFrom() { |
| 914 Protobuf_Unittest_TestAllExtensions_Builder builder = |
| 915 new Protobuf_Unittest_TestAllExtensions_Builder(); |
| 916 builder.setExtension(Unittest.optionalInt32Extension, 1); |
| 917 Protobuf_Unittest_TestAllExtensions original = builder.build(); |
| 918 Protobuf_Unittest_TestAllExtensions merged = |
| 919 new Protobuf_Unittest_TestAllExtensions_Builder(). |
| 920 mergeFromMessage(original).build(); |
| 921 Expect.isTrue(merged.hasExtension( |
| 922 Unittest.optionalInt32Extension)); |
| 923 Expect.equals( |
| 924 1, merged.getExtension( |
| 925 Unittest.optionalInt32Extension)); |
| 926 } |
| 927 |
| 928 void testToBuilder() { |
| 929 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 930 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 931 setAllFields(builder); |
| 932 Protobuf_Unittest_TestAllTypes message = builder.build(); |
| 933 assertAllFieldsSet(message); |
| 934 assertAllFieldsSet(message.toBuilder().build()); |
| 935 } |
| 936 |
| 937 void testRecursiveMessageDefaultInstance() { |
| 938 Protobuf_Unittest_TestRecursiveMessage message = |
| 939 Protobuf_Unittest_TestRecursiveMessage.defaultInstance; |
| 940 Expect.isNotNull(message); |
| 941 Expect.isNotNull(message.a); |
| 942 Expect.equals(message.a, message); |
| 943 } |
| 944 |
| 945 void testSerialize() { |
| 946 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 947 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 948 setAllFields(builder); |
| 949 Protobuf_Unittest_TestAllTypes expected = builder.build(); |
| 950 List<int> out = expected.writeToBuffer(); |
| 951 Protobuf_Unittest_TestAllTypes actual = |
| 952 Protobuf_Unittest_TestAllTypes.parseFromBuffer(out); |
| 953 Expect.equals(expected, actual); |
| 954 } |
| 955 |
| 956 void testSerializePartial() { |
| 957 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 958 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 959 Protobuf_Unittest_TestAllTypes expected = builder.buildPartial(); |
| 960 List<int> out = expected.writeToBuffer(); |
| 961 Protobuf_Unittest_TestAllTypes actual = |
| 962 Protobuf_Unittest_TestAllTypes.parseFromBuffer(out); |
| 963 Expect.equals(expected, actual); |
| 964 } |
| 965 |
| 966 void testEnumValues() { |
| 967 Expect.setEquals([ |
| 968 Protobuf_Unittest_TestAllTypes_NestedEnum.FOO, |
| 969 Protobuf_Unittest_TestAllTypes_NestedEnum.BAR, |
| 970 Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ], |
| 971 Protobuf_Unittest_TestAllTypes_NestedEnum.values); |
| 972 Expect.equals(1, Protobuf_Unittest_TestAllTypes_NestedEnum.FOO.value); |
| 973 Expect.equals(2, Protobuf_Unittest_TestAllTypes_NestedEnum.BAR.value); |
| 974 Expect.equals(3, Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ.value); |
| 975 } |
| 976 |
| 977 void testBadExtension() { |
| 978 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 979 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 980 try { |
| 981 builder.setExtension(Unittest.optionalInt32Extension, |
| 982 101); |
| 983 Expect.fail("Extension range not set. Should throw exception"); |
| 984 } catch (IllegalArgumentException e) { |
| 985 } |
| 986 |
| 987 try { |
| 988 builder.getExtension(Unittest.optionalInt32Extension); |
| 989 Expect.fail("Extension range not set. Should throw exception"); |
| 990 } catch (IllegalArgumentException e) { |
| 991 } |
| 992 } |
| 993 |
| 994 final String TEST_ALL_TYPES_JSON = '{"1":101,"2":"102","3":103,"4":"104",' |
| 995 '"5":105,"6":"106","7":107,"8":"108","9":109,"10":"110","11":111.0,' |
| 996 '"12":112.0,"13":true,"14":"115","15":"MTE2","16":{"17":117},' |
| 997 '"18":{"1":118},"19":{"1":119},"20":{"1":120},"21":3,"22":6,"23":9,' |
| 998 '"24":"124","25":"125","31":[201,301],"32":["202","302"],' |
| 999 '"33":[203,303],"34":["204","304"],"35":[205,305],"36":["206","306"],' |
| 1000 '"37":[207,307],"38":["208","308"],"39":[209,309],"40":["210","310"],' |
| 1001 '"41":[211.0,311.0],"42":[212.0,312.0],"43":[true,false],' |
| 1002 '"44":["215","315"],"45":["MjE2","MzE2"],"46":[{"47":217},{"47":317}],' |
| 1003 '"48":[{"1":218},{"1":318}],"49":[{"1":219},{"1":319}],' |
| 1004 '"50":[{"1":220},{"1":320}],"51":[2,3],"52":[5,6],"53":[8,9],' |
| 1005 '"54":["224","324"],"55":["225","325"],"61":401,"62":"402","63":403,' |
| 1006 '"64":"404","65":405,"66":"406","67":407,"68":"408","69":409,' |
| 1007 '"70":"410","71":411.0,"72":412.0,"73":false,"74":"415","75":"NDE2",' |
| 1008 '"81":1,"82":4,"83":7,"84":"424","85":"425"}'; |
| 1009 |
| 1010 void testOutput() { |
| 1011 Protobuf_Unittest_TestAllTypes message = getAllSet(); |
| 1012 String json = message.writeToJson(); |
| 1013 Expect.stringEquals(TEST_ALL_TYPES_JSON, json); |
| 1014 |
| 1015 // Test empty list |
| 1016 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 1017 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1018 builder.repeatedBool.clear(); |
| 1019 json = builder.build().writeToJson(); |
| 1020 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll('[true,false]', '[]'), |
| 1021 json); |
| 1022 |
| 1023 // test negative number |
| 1024 builder = |
| 1025 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1026 builder.optionalInt32 = -1234567; |
| 1027 json = builder.build().writeToJson(); |
| 1028 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':101,', ':-1234567,'), |
| 1029 json); |
| 1030 |
| 1031 // 64-bit numbers are quoted |
| 1032 builder = |
| 1033 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1034 builder.optionalInt64 = 9007199254740992; |
| 1035 json = builder.build().writeToJson(); |
| 1036 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"102",', |
| 1037 ':"9007199254740992",'), json); |
| 1038 |
| 1039 // Quotes, backslashes, and control characters in strings are quoted |
| 1040 builder = |
| 1041 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1042 builder.optionalString = "a\u0000b\u0001cd\\e\"fg"; |
| 1043 json = builder.build().writeToJson(); |
| 1044 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"115",', |
| 1045 ':"a\\u0000b\\u0001cd\\\\e\\"fg",'), json); |
| 1046 } |
| 1047 |
| 1048 void testBase64Encode() { |
| 1049 Protobuf_Unittest_TestAllTypes message = getAllSet(); |
| 1050 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 1051 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1052 builder.optionalBytes = "Hello, world".charCodes(); |
| 1053 String json = builder.build().writeToJson(); |
| 1054 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1055 ':"SGVsbG8sIHdvcmxk",'), json); |
| 1056 |
| 1057 builder = |
| 1058 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1059 builder.optionalBytes = "Hello, world!".charCodes(); |
| 1060 json = builder.build().writeToJson(); |
| 1061 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1062 ':"SGVsbG8sIHdvcmxkIQ==",'), json); |
| 1063 |
| 1064 builder = |
| 1065 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1066 builder.optionalBytes = "Hello, world!!".charCodes(); |
| 1067 json = builder.build().writeToJson(); |
| 1068 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1069 ':"SGVsbG8sIHdvcmxkISE=",'), json); |
| 1070 |
| 1071 // An empty list should not appear in the output |
| 1072 builder = |
| 1073 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1074 builder.optionalBytes = []; |
| 1075 json = builder.build().writeToJson(); |
| 1076 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll('"15":"MTE2",', |
| 1077 ''), json); |
| 1078 |
| 1079 builder = |
| 1080 Protobuf_Unittest_TestAllTypes.newBuilder().mergeFromMessage(message); |
| 1081 builder.optionalBytes = "a".charCodes(); |
| 1082 json = builder.build().writeToJson(); |
| 1083 Expect.stringEquals(TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1084 ':"YQ==",'), json); |
| 1085 } |
| 1086 |
| 1087 void testBase64Decode() { |
| 1088 String json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1089 ':"SGVsbG8sIHdvcmxk",'); |
| 1090 Protobuf_Unittest_TestAllTypes message = |
| 1091 Protobuf_Unittest_TestAllTypes.parseFromJson(json); |
| 1092 Expect.stringEquals("Hello, world", new String.fromCharCodes(message.optionalB
ytes)); |
| 1093 |
| 1094 json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1095 ':"SGVsbG8sIHdvcmxkIQ==",'); |
| 1096 message = |
| 1097 Protobuf_Unittest_TestAllTypes.parseFromJson(json); |
| 1098 Expect.stringEquals("Hello, world!", new String.fromCharCodes(message.optional
Bytes)); |
| 1099 |
| 1100 json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1101 ':"SGVsbG8sIHdvcmxkISE=",'); |
| 1102 message = |
| 1103 Protobuf_Unittest_TestAllTypes.parseFromJson(json); |
| 1104 Expect.stringEquals("Hello, world!!", new String.fromCharCodes(message.optiona
lBytes)); |
| 1105 |
| 1106 // Remove optionalBytes tag, reads back as empty list |
| 1107 json = TEST_ALL_TYPES_JSON.replaceAll('"15":"MTE2",', |
| 1108 ''); |
| 1109 message = |
| 1110 Protobuf_Unittest_TestAllTypes.parseFromJson(json); |
| 1111 Expect.listEquals([], message.optionalBytes); |
| 1112 |
| 1113 // Keep optionalBytes tag, set data to empty string, get back empty list |
| 1114 json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1115 ':"",'); |
| 1116 message = |
| 1117 Protobuf_Unittest_TestAllTypes.parseFromJson(json); |
| 1118 Expect.listEquals([], message.optionalBytes); |
| 1119 |
| 1120 json = TEST_ALL_TYPES_JSON.replaceAll(':"MTE2",', |
| 1121 ':"YQ==",'); |
| 1122 message = |
| 1123 Protobuf_Unittest_TestAllTypes.parseFromJson(json); |
| 1124 Expect.stringEquals("a", new String.fromCharCodes(message.optionalBytes)); |
| 1125 } |
| 1126 |
| 1127 void testParse() { |
| 1128 Protobuf_Unittest_TestAllTypes message = |
| 1129 Protobuf_Unittest_TestAllTypes.parseFromJson(TEST_ALL_TYPES_JSON); |
| 1130 Protobuf_Unittest_TestAllTypes expected = getAllSet(); |
| 1131 Expect.equals(expected, message); |
| 1132 } |
| 1133 |
| 1134 void testExtensionsOutput() { |
| 1135 Protobuf_Unittest_TestAllExtensions message = getAllExtensionsSet(); |
| 1136 String json = message.writeToJson(); |
| 1137 Expect.equals(TEST_ALL_TYPES_JSON, json); |
| 1138 } |
| 1139 |
| 1140 void testExtensionsParse() { |
| 1141 ExtensionRegistry registry = getExtensionRegistry(); |
| 1142 Protobuf_Unittest_TestAllExtensions message = |
| 1143 Protobuf_Unittest_TestAllExtensions.parseFromJson(TEST_ALL_TYPES_JSON, |
| 1144 registry); |
| 1145 Protobuf_Unittest_TestAllExtensions expected = getAllExtensionsSet(); |
| 1146 Expect.equals(expected, message); |
| 1147 } |
| 1148 |
| 1149 Protobuf_Unittest_TestAllTypes MERGE_SOURCE; |
| 1150 Protobuf_Unittest_TestAllTypes MERGE_DEST; |
| 1151 String MERGE_RESULT_TEXT = """ |
| 1152 optionalInt32: 1 |
| 1153 optionalInt64: 2 |
| 1154 optionalString: baz |
| 1155 optionalForeignMessage: { |
| 1156 c: 3 |
| 1157 } |
| 1158 repeatedString: bar |
| 1159 repeatedString: qux |
| 1160 """; |
| 1161 |
| 1162 void setUpMessageTests() { |
| 1163 Protobuf_Unittest_TestAllTypes_Builder mergeSourceBuilder = |
| 1164 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 1165 mergeSourceBuilder.optionalInt32 = 1; |
| 1166 mergeSourceBuilder.optionalString = "foo"; |
| 1167 mergeSourceBuilder.optionalForeignMessage = |
| 1168 Protobuf_Unittest_ForeignMessage.defaultInstance; |
| 1169 mergeSourceBuilder.repeatedString.add("bar"); |
| 1170 MERGE_SOURCE = mergeSourceBuilder.build(); |
| 1171 |
| 1172 Protobuf_Unittest_TestAllTypes_Builder mergeDestBuilder = |
| 1173 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 1174 mergeDestBuilder.optionalInt64 = 2; |
| 1175 mergeDestBuilder.optionalString = "baz"; |
| 1176 mergeDestBuilder.optionalForeignMessage = |
| 1177 Protobuf_Unittest_ForeignMessage.defaultInstance; |
| 1178 Protobuf_Unittest_ForeignMessage_Builder foreignMessageBuilder = |
| 1179 new Protobuf_Unittest_ForeignMessage_Builder(); |
| 1180 foreignMessageBuilder.c = 3; |
| 1181 mergeDestBuilder.optionalForeignMessage = foreignMessageBuilder.build(); |
| 1182 mergeDestBuilder.repeatedString.add("qux"); |
| 1183 MERGE_DEST = mergeDestBuilder.build(); |
| 1184 |
| 1185 TEST_REQUIRED_UNINITIALIZED = |
| 1186 Protobuf_Unittest_TestRequired.defaultInstance; |
| 1187 |
| 1188 Protobuf_Unittest_TestRequired_Builder testRequiredBuilder = |
| 1189 new Protobuf_Unittest_TestRequired_Builder(); |
| 1190 testRequiredBuilder.a = 1; |
| 1191 testRequiredBuilder.b = 2; |
| 1192 testRequiredBuilder.c = 3; |
| 1193 TEST_REQUIRED_INITIALIZED = testRequiredBuilder.build(); |
| 1194 } |
| 1195 |
| 1196 void testMergeFrom() { |
| 1197 // was: |
| 1198 //Protobuf_Unittest_TestAllTypes result = |
| 1199 // TestAllTypes.newBuilder(MERGE_DEST) |
| 1200 // .mergeFrom(MERGE_SOURCE).build(); |
| 1201 // less succinct now... |
| 1202 Protobuf_Unittest_TestAllTypes_Builder builder = |
| 1203 new Protobuf_Unittest_TestAllTypes_Builder(); |
| 1204 // TODO add (builder construction) to the message class? |
| 1205 Protobuf_Unittest_TestAllTypes result = |
| 1206 builder.mergeFromMessage(MERGE_SOURCE) |
| 1207 // TODO can we create a builder from a message instance? |
| 1208 .mergeFromMessage(MERGE_DEST).build(); |
| 1209 |
| 1210 Expect.equals(MERGE_RESULT_TEXT, result.toString()); |
| 1211 } |
| 1212 |
| 1213 // void testMergeFromDynamic() {} // UNSUPPORTED no Dynamic implementation |
| 1214 // void testDynamicMergeFrom() {} // UNSUPPORTED no Dynamic implementation |
| 1215 |
| 1216 Protobuf_Unittest_TestRequired TEST_REQUIRED_UNINITIALIZED; |
| 1217 Protobuf_Unittest_TestRequired TEST_REQUIRED_INITIALIZED; |
| 1218 |
| 1219 void testRequired() { |
| 1220 Protobuf_Unittest_TestRequired_Builder builder = |
| 1221 new Protobuf_Unittest_TestRequired_Builder(); |
| 1222 |
| 1223 Expect.isFalse(builder.isInitialized(), "no required fields set"); |
| 1224 builder.a = 1; |
| 1225 Expect.isFalse(builder.isInitialized(), "single required field set"); |
| 1226 builder.b = 1; |
| 1227 Expect.isFalse(builder.isInitialized(), "all but one required field set"); |
| 1228 builder.c = 1; |
| 1229 Expect.isTrue(builder.isInitialized(), "required fields set"); |
| 1230 } |
| 1231 |
| 1232 void testRequiredForeign() { |
| 1233 Protobuf_Unittest_TestRequiredForeign_Builder builder = |
| 1234 new Protobuf_Unittest_TestRequiredForeign_Builder(); |
| 1235 Expect.isTrue(builder.isInitialized(), |
| 1236 "TestRequiredForeign without children should be initialized"); |
| 1237 |
| 1238 builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED; |
| 1239 Expect.isFalse(builder.isInitialized(), |
| 1240 "TestRequiredForeign with optional TEST_REQUIRED_UNINITIALIZED " |
| 1241 "should not be initialized"); |
| 1242 |
| 1243 builder.optionalMessage = TEST_REQUIRED_INITIALIZED; |
| 1244 Expect.isTrue(builder.isInitialized(), |
| 1245 "TestRequiredForeign with optional TEST_REQUIRED_INITIALIZED " |
| 1246 "should be initialized"); |
| 1247 |
| 1248 builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED); |
| 1249 Expect.isFalse(builder.isInitialized(), |
| 1250 "TestRequiredForeign with repeating TEST_REQUIRED_UNINITIALIZED " |
| 1251 "should not be initialized"); |
| 1252 |
| 1253 builder.repeatedMessage[0] = TEST_REQUIRED_INITIALIZED; |
| 1254 Expect.isTrue(builder.isInitialized(), |
| 1255 "TestRequiredForeign with repeating TEST_REQUIRED_INITIALIZED " |
| 1256 "should be initialized"); |
| 1257 } |
| 1258 |
| 1259 void testRequiredExtension() { |
| 1260 Protobuf_Unittest_TestAllExtensions_Builder builder = |
| 1261 new Protobuf_Unittest_TestAllExtensions_Builder(); |
| 1262 Expect.isTrue(builder.isInitialized()); |
| 1263 |
| 1264 builder.setExtension(Protobuf_Unittest_TestRequired.single, |
| 1265 TEST_REQUIRED_UNINITIALIZED); |
| 1266 Expect.isFalse(builder.isInitialized()); |
| 1267 |
| 1268 builder.setExtension(Protobuf_Unittest_TestRequired.single, |
| 1269 TEST_REQUIRED_INITIALIZED); |
| 1270 Expect.isTrue(builder.isInitialized()); |
| 1271 |
| 1272 builder.addExtension(Protobuf_Unittest_TestRequired.multi, |
| 1273 TEST_REQUIRED_UNINITIALIZED); |
| 1274 Expect.isFalse(builder.isInitialized()); |
| 1275 |
| 1276 builder.getExtension(Protobuf_Unittest_TestRequired.multi)[0] = |
| 1277 TEST_REQUIRED_INITIALIZED; |
| 1278 Expect.isTrue(builder.isInitialized()); |
| 1279 } |
| 1280 |
| 1281 // void testRequiredDynamic() {} // UNSUPPORTED no Dynamic implementation |
| 1282 // void testRequiredDynamicForeign() {} // UNSUPPORTED no Dynamic impl |
| 1283 |
| 1284 void testUninitializedException() { |
| 1285 try { |
| 1286 new Protobuf_Unittest_TestRequired_Builder().build(); |
| 1287 Expect.fail("Should have thrown an exception."); |
| 1288 } catch (UninitializedMessageException e) { |
| 1289 Expect.equals("Message missing required fields: a, b, c", e.message); |
| 1290 } |
| 1291 } |
| 1292 |
| 1293 void testBuildPartial() { |
| 1294 // We're mostly testing that no exception is thrown. |
| 1295 Protobuf_Unittest_TestRequired message = |
| 1296 new Protobuf_Unittest_TestRequired_Builder().buildPartial(); |
| 1297 Expect.isFalse(message.isInitialized()); |
| 1298 } |
| 1299 |
| 1300 void testNestedUninitializedException() { |
| 1301 try { |
| 1302 Protobuf_Unittest_TestRequiredForeign_Builder builder = |
| 1303 new Protobuf_Unittest_TestRequiredForeign_Builder(); |
| 1304 builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED; |
| 1305 builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED); |
| 1306 builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED); |
| 1307 builder.build(); |
| 1308 Expect.fail("Should have thrown an exception."); |
| 1309 } catch (UninitializedMessageException e) { |
| 1310 // NOTE: error message differs from Java in that |
| 1311 // fields are referenced using Dart fieldnames r.t. |
| 1312 // proto field names. |
| 1313 Expect.equals( |
| 1314 "Message missing required fields: " |
| 1315 "optionalMessage.a, " |
| 1316 "optionalMessage.b, " |
| 1317 "optionalMessage.c, " |
| 1318 "repeatedMessage[0].a, " |
| 1319 "repeatedMessage[0].b, " |
| 1320 "repeatedMessage[0].c, " |
| 1321 "repeatedMessage[1].a, " |
| 1322 "repeatedMessage[1].b, " |
| 1323 "repeatedMessage[1].c", |
| 1324 e.message); |
| 1325 } |
| 1326 } |
| 1327 |
| 1328 void testBuildNestedPartial() { |
| 1329 // We're mostly testing that no exception is thrown. |
| 1330 Protobuf_Unittest_TestRequiredForeign_Builder builder = |
| 1331 new Protobuf_Unittest_TestRequiredForeign_Builder(); |
| 1332 builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED; |
| 1333 builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED); |
| 1334 builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED); |
| 1335 Protobuf_Unittest_TestRequiredForeign message = builder.buildPartial(); |
| 1336 Expect.isFalse(message.isInitialized()); |
| 1337 } |
| 1338 |
| 1339 void testParseUnititialized() { |
| 1340 try { |
| 1341 Protobuf_Unittest_TestRequired.parseFromBuffer([]); |
| 1342 Expect.fail("Should have thrown an exception."); |
| 1343 } catch (UninitializedMessageException e) { |
| 1344 Expect.equals("Message missing required fields: a, b, c", e.message); |
| 1345 } |
| 1346 } |
| 1347 |
| 1348 void testParseNestedUnititialized() { |
| 1349 Protobuf_Unittest_TestRequiredForeign_Builder builder = |
| 1350 new Protobuf_Unittest_TestRequiredForeign_Builder(); |
| 1351 builder.optionalMessage = TEST_REQUIRED_UNINITIALIZED; |
| 1352 builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED); |
| 1353 builder.repeatedMessage.add(TEST_REQUIRED_UNINITIALIZED); |
| 1354 List<int> buffer = builder.buildPartial().writeToBuffer(); |
| 1355 |
| 1356 try { |
| 1357 Protobuf_Unittest_TestRequiredForeign.parseFromBuffer(buffer); |
| 1358 Expect.fail("Should have thrown an exception."); |
| 1359 } catch (UninitializedMessageException e) { |
| 1360 // NOTE: error message differs from Java in that |
| 1361 // fields are referenced using Dart fieldnames r.t. |
| 1362 // proto field names. |
| 1363 Expect.equals( |
| 1364 "Message missing required fields: " |
| 1365 "optionalMessage.a, " |
| 1366 "optionalMessage.b, " |
| 1367 "optionalMessage.c, " |
| 1368 "repeatedMessage[0].a, " |
| 1369 "repeatedMessage[0].b, " |
| 1370 "repeatedMessage[0].c, " |
| 1371 "repeatedMessage[1].a, " |
| 1372 "repeatedMessage[1].b, " |
| 1373 "repeatedMessage[1].c", |
| 1374 e.message); |
| 1375 } |
| 1376 } |
| 1377 |
| 1378 Protobuf_Unittest_TestEmptyMessage emptyMessage; |
| 1379 Protobuf_Unittest_TestAllTypes testAllTypes; |
| 1380 UnknownFieldSet unknownFields; |
| 1381 List<int> allFieldsData; |
| 1382 |
| 1383 void setUpUnknownFieldSetTests() { |
| 1384 testAllTypes = getAllSet(); |
| 1385 allFieldsData = testAllTypes.writeToBuffer(); |
| 1386 emptyMessage = |
| 1387 Protobuf_Unittest_TestEmptyMessage.parseFromBuffer(allFieldsData); |
| 1388 unknownFields = emptyMessage.unknownFields; |
| 1389 } |
| 1390 |
| 1391 UnknownFieldSet_Field getField(String name) { |
| 1392 int tagNumber = testAllTypes.getTagNumber(name); |
| 1393 assert(unknownFields.hasField(tagNumber)); |
| 1394 return unknownFields.getField(tagNumber); |
| 1395 } |
| 1396 |
| 1397 void testVarint() { |
| 1398 UnknownFieldSet_Field optionalInt32 = getField("optionalInt32"); |
| 1399 Expect.equals(testAllTypes.optionalInt32, |
| 1400 optionalInt32.varintList[0].toInt()); |
| 1401 } |
| 1402 |
| 1403 void testFixed32() { |
| 1404 UnknownFieldSet_Field optionalFixed32 = getField("optionalFixed32"); |
| 1405 Expect.equals(testAllTypes.optionalFixed32, optionalFixed32.fixed32List[0]); |
| 1406 } |
| 1407 |
| 1408 void testFixed64() { |
| 1409 UnknownFieldSet_Field optionalFixed64 = getField("optionalFixed64"); |
| 1410 Expect.equals(testAllTypes.optionalFixed64, |
| 1411 optionalFixed64.fixed64List[0].toInt()); |
| 1412 } |
| 1413 |
| 1414 void testLengthDelimited() { |
| 1415 UnknownFieldSet_Field optionalBytes = getField("optionalBytes"); |
| 1416 Expect.listEquals(testAllTypes.optionalBytes, |
| 1417 optionalBytes.lengthDelimitedList[0]); |
| 1418 } |
| 1419 |
| 1420 void testGroup() { |
| 1421 int tagNumberA = Protobuf_Unittest_TestAllTypes_OptionalGroup |
| 1422 .defaultInstance.getTagNumber("a"); |
| 1423 Expect.isTrue(tagNumberA != null); |
| 1424 |
| 1425 UnknownFieldSet_Field optionalGroupField = getField("optionalGroup"); |
| 1426 Expect.equals(1, optionalGroupField.groupList.length); |
| 1427 UnknownFieldSet group = optionalGroupField.groupList[0]; |
| 1428 Expect.isTrue(group.hasField(tagNumberA)); |
| 1429 Expect.equals(testAllTypes.optionalGroup.a, group.getField(tagNumberA) |
| 1430 .varintList[0].toInt()); |
| 1431 } |
| 1432 |
| 1433 void testSerialize2() { |
| 1434 List<int> data = emptyMessage.writeToBuffer(); |
| 1435 Expect.listEquals(allFieldsData, data); |
| 1436 } |
| 1437 |
| 1438 void testCopyFrom() { |
| 1439 Protobuf_Unittest_TestEmptyMessage message = |
| 1440 Protobuf_Unittest_TestEmptyMessage.newBuilder() |
| 1441 .mergeFromMessage(emptyMessage).build(); |
| 1442 Expect.stringEquals(emptyMessage.toString(), message.toString()); |
| 1443 Expect.isFalse(emptyMessage.toString().isEmpty()); |
| 1444 } |
| 1445 |
| 1446 void testMergeFrom2() { |
| 1447 // source |
| 1448 UnknownFieldSet sourceFieldSet = new UnknownFieldSet_Builder() |
| 1449 .addField(2, new UnknownFieldSet_Field_Builder().addVarint(2).build()) |
| 1450 .addField(3, new UnknownFieldSet_Field_Builder().addVarint(3).build()) |
| 1451 .build(); |
| 1452 |
| 1453 Protobuf_Unittest_TestEmptyMessage_Builder sourceBuilder = |
| 1454 Protobuf_Unittest_TestEmptyMessage.newBuilder(); |
| 1455 sourceBuilder.unknownFields = sourceFieldSet; |
| 1456 Protobuf_Unittest_TestEmptyMessage source = sourceBuilder.build(); |
| 1457 |
| 1458 // destination |
| 1459 UnknownFieldSet destinationFieldSet = new UnknownFieldSet_Builder() |
| 1460 .addField(1, new UnknownFieldSet_Field_Builder().addVarint(1).build()) |
| 1461 .addField(3, new UnknownFieldSet_Field_Builder().addVarint(4).build()) |
| 1462 .build(); |
| 1463 |
| 1464 Protobuf_Unittest_TestEmptyMessage_Builder destinationBuilder = |
| 1465 Protobuf_Unittest_TestEmptyMessage.newBuilder(); |
| 1466 destinationBuilder.unknownFields = destinationFieldSet; |
| 1467 destinationBuilder.mergeFromMessage(source); |
| 1468 Protobuf_Unittest_TestEmptyMessage destination = destinationBuilder.build(); |
| 1469 |
| 1470 Expect.stringEquals( |
| 1471 "1: 1\n" |
| 1472 "2: 2\n" |
| 1473 "3: 4\n" |
| 1474 "3: 3\n", |
| 1475 destination.toString()); |
| 1476 } |
| 1477 |
| 1478 void testClear2() { |
| 1479 UnknownFieldSet_Builder fsb = new UnknownFieldSet_Builder(unknownFields); |
| 1480 fsb.clear(); |
| 1481 UnknownFieldSet fields = fsb.build(); |
| 1482 Expect.isTrue(fields.asMap().isEmpty()); |
| 1483 } |
| 1484 |
| 1485 void testEmpty() { |
| 1486 UnknownFieldSet fields = (new UnknownFieldSet_Builder()).build(); |
| 1487 Expect.isTrue(fields.asMap().isEmpty()); |
| 1488 } |
| 1489 |
| 1490 void testClearMessage() { |
| 1491 Protobuf_Unittest_TestEmptyMessage_Builder messageBuilder = |
| 1492 Protobuf_Unittest_TestEmptyMessage.newBuilder(); |
| 1493 messageBuilder.mergeFromMessage(emptyMessage); |
| 1494 messageBuilder.clear(); |
| 1495 Protobuf_Unittest_TestEmptyMessage message = messageBuilder.build(); |
| 1496 Expect.equals(0, message.getSerializedSize()); |
| 1497 } |
| 1498 |
| 1499 void testParseKnownAndUnknown() { |
| 1500 // Test mixing known and unknown fields when parsing. |
| 1501 UnknownFieldSet fields = (new UnknownFieldSet_Builder(unknownFields)) |
| 1502 .addField(123456, |
| 1503 (new UnknownFieldSet_Field_Builder()).addVarint(654321).build()) |
| 1504 .build(); |
| 1505 |
| 1506 CodedBufferWriter writer = |
| 1507 new CodedBufferWriter(fields.getSerializedSize()); |
| 1508 fields.writeToCodedBufferWriter(writer); |
| 1509 |
| 1510 Protobuf_Unittest_TestAllTypes destination = |
| 1511 Protobuf_Unittest_TestAllTypes.parseFromBuffer(writer.buffer); |
| 1512 |
| 1513 assertAllFieldsSet(destination); |
| 1514 Expect.equals(1, destination.unknownFields.asMap().length); |
| 1515 |
| 1516 UnknownFieldSet_Field field = |
| 1517 destination.unknownFields.getField(123456); |
| 1518 Expect.equals(1, field.varintList.length); |
| 1519 Expect.equals(654321, field.varintList[0].toInt()); |
| 1520 } |
| 1521 |
| 1522 // Constructs a protocol buffer which contains fields with all the same |
| 1523 // numbers as allFieldsData except that each field is some other wire |
| 1524 // type. |
| 1525 List<int> getBizarroData() { |
| 1526 UnknownFieldSet_Builder bizarroFieldsBuilder = |
| 1527 new UnknownFieldSet_Builder(); |
| 1528 |
| 1529 UnknownFieldSet_Field_Builder varintField = |
| 1530 (new UnknownFieldSet_Field_Builder()).addVarint(1); |
| 1531 |
| 1532 UnknownFieldSet_Field_Builder fixed32Field = |
| 1533 (new UnknownFieldSet_Field_Builder()).addFixed32(1); |
| 1534 |
| 1535 unknownFields.asMap().forEach(void _(int tag, UnknownFieldSet_Field value) { |
| 1536 if (value.varintList.isEmpty()) { |
| 1537 // Original field is not a varint, so use a varint. |
| 1538 bizarroFieldsBuilder.addField(tag, varintField.build()); |
| 1539 } else { |
| 1540 // Original field *is* a varint, so use something else. |
| 1541 bizarroFieldsBuilder.addField(tag, fixed32Field.build()); |
| 1542 } |
| 1543 }); |
| 1544 UnknownFieldSet bizarroFields = bizarroFieldsBuilder.build(); |
| 1545 CodedBufferWriter writer = |
| 1546 new CodedBufferWriter(bizarroFields.getSerializedSize()); |
| 1547 bizarroFields.writeToCodedBufferWriter(writer); |
| 1548 return writer.buffer; |
| 1549 } |
| 1550 |
| 1551 void testWrongTypeTreatedAsUnknown() { |
| 1552 // Test that fields of the wrong wire type are treated like unknown fields |
| 1553 // when parsing. |
| 1554 List<int> bizarroData = getBizarroData(); |
| 1555 Protobuf_Unittest_TestAllTypes allTypesMessage = |
| 1556 Protobuf_Unittest_TestAllTypes.parseFromBuffer(bizarroData); |
| 1557 Protobuf_Unittest_TestEmptyMessage emptyMessage_ = |
| 1558 Protobuf_Unittest_TestEmptyMessage.parseFromBuffer(bizarroData); |
| 1559 // All fields should have been interpreted as unknown, so the debug strings |
| 1560 // should be the same. |
| 1561 Expect.stringEquals(emptyMessage_.toString(), allTypesMessage.toString()); |
| 1562 } |
| 1563 |
| 1564 void testUnknownExtensions() { |
| 1565 // Make sure fields are properly parsed to the UnknownFieldSet even when |
| 1566 // they are declared as extension numbers. |
| 1567 Protobuf_Unittest_TestEmptyMessageWithExtensions message = |
| 1568 Protobuf_Unittest_TestEmptyMessageWithExtensions |
| 1569 .parseFromBuffer(allFieldsData); |
| 1570 |
| 1571 Expect.equals(unknownFields.asMap().length, |
| 1572 message.unknownFields.asMap().length); |
| 1573 Expect.listEquals(allFieldsData, message.writeToBuffer()); |
| 1574 } |
| 1575 |
| 1576 void testWrongExtensionTypeTreatedAsUnknown() { |
| 1577 // Test that fields of the wrong wire type are treated like unknown fields |
| 1578 // when parsing extensions. |
| 1579 |
| 1580 List<int> bizarroData = getBizarroData(); |
| 1581 Protobuf_Unittest_TestAllExtensions allExtensionsMessage = |
| 1582 Protobuf_Unittest_TestAllExtensions.parseFromBuffer(bizarroData); |
| 1583 Protobuf_Unittest_TestEmptyMessage emptyMessage_ = |
| 1584 Protobuf_Unittest_TestEmptyMessage.parseFromBuffer(bizarroData); |
| 1585 |
| 1586 // All fields should have been interpreted as unknown, so the debug strings |
| 1587 // should be the same. |
| 1588 Expect.stringEquals(emptyMessage_.toString(), |
| 1589 allExtensionsMessage.toString()); |
| 1590 } |
| 1591 |
| 1592 void testParseUnknownEnumValue() { |
| 1593 int singularFieldNum = testAllTypes.getTagNumber("optionalNestedEnum"); |
| 1594 int repeatedFieldNum = testAllTypes.getTagNumber("repeatedNestedEnum"); |
| 1595 Expect.isNotNull(singularFieldNum); |
| 1596 Expect.isNotNull(repeatedFieldNum); |
| 1597 |
| 1598 UnknownFieldSet fieldSet = (new UnknownFieldSet_Builder()) |
| 1599 .addField(singularFieldNum, (new UnknownFieldSet_Field_Builder()) |
| 1600 .addVarint(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR.value) |
| 1601 .addVarint(5).build()) |
| 1602 .addField(repeatedFieldNum, (new UnknownFieldSet_Field_Builder()) |
| 1603 .addVarint(Protobuf_Unittest_TestAllTypes_NestedEnum.FOO.value) |
| 1604 .addVarint(4) |
| 1605 .addVarint(Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ.value) |
| 1606 .addVarint(6).build()) |
| 1607 .build(); |
| 1608 |
| 1609 CodedBufferWriter writer = |
| 1610 new CodedBufferWriter(fieldSet.getSerializedSize()); |
| 1611 fieldSet.writeToCodedBufferWriter(writer); |
| 1612 { |
| 1613 Protobuf_Unittest_TestAllTypes message = |
| 1614 Protobuf_Unittest_TestAllTypes.parseFromBuffer(writer.buffer); |
| 1615 Expect.equals(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR, |
| 1616 message.optionalNestedEnum); |
| 1617 Expect.listEquals([Protobuf_Unittest_TestAllTypes_NestedEnum.FOO, |
| 1618 Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ], |
| 1619 message.repeatedNestedEnum); |
| 1620 Expect.equals(1, |
| 1621 message.unknownFields.getField(singularFieldNum).varintList.length); |
| 1622 Expect.equals(5, |
| 1623 message.unknownFields.getField(singularFieldNum).varintList[0].toInt()); |
| 1624 Expect.equals(2, |
| 1625 message.unknownFields.getField(repeatedFieldNum).varintList.length); |
| 1626 Expect.equals(4, |
| 1627 message.unknownFields.getField(repeatedFieldNum).varintList[0].toInt()); |
| 1628 Expect.equals(6, |
| 1629 message.unknownFields.getField(repeatedFieldNum).varintList[1].toInt()); |
| 1630 } |
| 1631 { |
| 1632 Protobuf_Unittest_TestAllExtensions message = |
| 1633 Protobuf_Unittest_TestAllExtensions.parseFromBuffer(writer.buffer, |
| 1634 getExtensionRegistry()); |
| 1635 Expect.equals(Protobuf_Unittest_TestAllTypes_NestedEnum.BAR, |
| 1636 message.getExtension( |
| 1637 Unittest.optionalNestedEnumExtension)); |
| 1638 |
| 1639 Expect.listEquals([Protobuf_Unittest_TestAllTypes_NestedEnum.FOO, |
| 1640 Protobuf_Unittest_TestAllTypes_NestedEnum.BAZ], |
| 1641 message.getExtension( |
| 1642 Unittest.repeatedNestedEnumExtension)); |
| 1643 Expect.equals(1, |
| 1644 message.unknownFields.getField(singularFieldNum).varintList.length); |
| 1645 Expect.equals(5, |
| 1646 message.unknownFields.getField(singularFieldNum).varintList[0].toInt()); |
| 1647 Expect.equals(2, |
| 1648 message.unknownFields.getField(repeatedFieldNum).varintList.length); |
| 1649 Expect.equals(4, |
| 1650 message.unknownFields.getField(repeatedFieldNum).varintList[0].toInt()); |
| 1651 Expect.equals(6, |
| 1652 message.unknownFields.getField(repeatedFieldNum).varintList[1].toInt()); |
| 1653 } |
| 1654 } |
| 1655 |
| 1656 void testLargeVarint() { |
| 1657 Packed64 maxint = new Packed64(0x7FFFFFFF, 0xFFFFFFFF); |
| 1658 UnknownFieldSet unknownFieldSet = (new UnknownFieldSet_Builder()) |
| 1659 .addField(1, (new UnknownFieldSet_Field_Builder()) |
| 1660 .addVarint(maxint).build()) |
| 1661 .build(); |
| 1662 CodedBufferWriter writer = |
| 1663 new CodedBufferWriter(unknownFieldSet.getSerializedSize()); |
| 1664 unknownFieldSet.writeToCodedBufferWriter(writer); |
| 1665 |
| 1666 var parsed = new UnknownFieldSet_Builder(); |
| 1667 parsed.mergeFromCodedBufferReader( |
| 1668 new CodedBufferReader.fromBuffer(writer.buffer)); |
| 1669 var field = parsed.build().getField(1); |
| 1670 Expect.equals(1, field.varintList.length); |
| 1671 Expect.equals(new Packed64(0x7FFFFFFF, 0xFFFFFFFF).toString(), |
| 1672 field.varintList[0].toString()); |
| 1673 } |
| 1674 |
| 1675 void testEqualsAndHashCode() { |
| 1676 UnknownFieldSet a = (new UnknownFieldSet_Builder()) |
| 1677 .addField(1, (new UnknownFieldSet_Field_Builder()) |
| 1678 .addFixed32(1).build()).build(); |
| 1679 |
| 1680 UnknownFieldSet b = (new UnknownFieldSet_Builder()) |
| 1681 .addField(1, (new UnknownFieldSet_Field_Builder()) |
| 1682 .addFixed64(new Packed64.fromInt(1)).build()).build(); |
| 1683 |
| 1684 UnknownFieldSet c = (new UnknownFieldSet_Builder()) |
| 1685 .addField(1, (new UnknownFieldSet_Field_Builder()).addVarint(1).build()) |
| 1686 .build(); |
| 1687 |
| 1688 UnknownFieldSet d = (new UnknownFieldSet_Builder()) |
| 1689 .addField(1, (new UnknownFieldSet_Field_Builder()) |
| 1690 .addLengthDelimited([]).build()).build(); |
| 1691 |
| 1692 UnknownFieldSet e = (new UnknownFieldSet_Builder()) |
| 1693 .addField(1, (new UnknownFieldSet_Field_Builder()) |
| 1694 .addGroup(unknownFields).build()).build(); |
| 1695 |
| 1696 _checkEqualsIsConsistent(a); |
| 1697 _checkEqualsIsConsistent(b); |
| 1698 _checkEqualsIsConsistent(c); |
| 1699 _checkEqualsIsConsistent(d); |
| 1700 _checkEqualsIsConsistent(e); |
| 1701 |
| 1702 _checkNotEqual(a, b); |
| 1703 _checkNotEqual(a, c); |
| 1704 _checkNotEqual(a, d); |
| 1705 _checkNotEqual(a, e); |
| 1706 _checkNotEqual(b, c); |
| 1707 _checkNotEqual(b, d); |
| 1708 _checkNotEqual(b, e); |
| 1709 _checkNotEqual(c, d); |
| 1710 _checkNotEqual(c, e); |
| 1711 _checkNotEqual(d, e); |
| 1712 |
| 1713 UnknownFieldSet f1 = (new UnknownFieldSet_Builder()) |
| 1714 .addField(1, |
| 1715 (new UnknownFieldSet_Field_Builder()).addLengthDelimited([1, 2]) |
| 1716 .build()).build(); |
| 1717 UnknownFieldSet f2 = (new UnknownFieldSet_Builder()) |
| 1718 .addField(1, |
| 1719 (new UnknownFieldSet_Field_Builder()).addLengthDelimited([2, 1]) |
| 1720 .build()).build(); |
| 1721 |
| 1722 _checkEqualsIsConsistent(f1); |
| 1723 _checkEqualsIsConsistent(f2); |
| 1724 |
| 1725 _checkNotEqual(f1, f2); |
| 1726 } |
| 1727 |
| 1728 /** |
| 1729 * Asserts that the given field sets are not equal and have different |
| 1730 * hash codes. |
| 1731 * |
| 1732 * N.B.: It is valid for non-equal objects to have the same hash code, so |
| 1733 * this test is more strict than necessary. However, in the test cases |
| 1734 * identifies, the hash codes should differ, and as a matter of principle |
| 1735 * hash collisions should be relatively rare. |
| 1736 */ |
| 1737 void _checkNotEqual(UnknownFieldSet s1, UnknownFieldSet s2) { |
| 1738 String equalsError = |
| 1739 "${s1.toString()} should not be equal to ${s2.toString}"; |
| 1740 Expect.isFalse(s1 == s2, equalsError); |
| 1741 Expect.isFalse(s2 == s1, equalsError); |
| 1742 |
| 1743 Expect.isFalse( |
| 1744 s1.hashCode() == s2.hashCode(), |
| 1745 "${s1.toString()} should have a different hash code " |
| 1746 "from ${s2.toString()}"); |
| 1747 } |
| 1748 |
| 1749 /** |
| 1750 * Asserts that the given field sets are equal and have identical hash codes. |
| 1751 */ |
| 1752 void _checkEqualsIsConsistent(UnknownFieldSet set) { |
| 1753 // Object should be equal to itself. |
| 1754 Expect.equals(set, set); |
| 1755 |
| 1756 // Object should be equal to a copy of itself. |
| 1757 UnknownFieldSet copy = (new UnknownFieldSet_Builder(set)).build(); |
| 1758 Expect.equals(set, copy); |
| 1759 Expect.equals(copy, set); |
| 1760 Expect.equals(set.hashCode(), copy.hashCode()); |
| 1761 } |
| 1762 |
| 1763 |
OLD | NEW |