|
10 | 10 | import static com.google.common.truth.Truth.assertThat;
|
11 | 11 | import static com.google.common.truth.Truth.assertWithMessage;
|
12 | 12 | import static org.junit.Assert.assertArrayEquals;
|
| 13 | +import static org.junit.Assert.assertThrows; |
13 | 14 | import protobuf_unittest.UnittestProto.BoolMessage;
|
14 | 15 | import protobuf_unittest.UnittestProto.Int32Message;
|
15 | 16 | import protobuf_unittest.UnittestProto.Int64Message;
|
@@ -534,6 +535,86 @@ public void testReadMaliciouslyLargeBlob() throws Exception {
|
534 | 535 | }
|
535 | 536 | }
|
536 | 537 |
|
| 538 | + @Test |
| 539 | + public void testReadStringWithSizeOverflow_throwsInvalidProtocolBufferException() |
| 540 | + throws Exception { |
| 541 | + ByteString.Output rawOutput = ByteString.newOutput(); |
| 542 | + CodedOutputStream output = CodedOutputStream.newInstance(rawOutput); |
| 543 | + |
| 544 | + output.writeUInt32NoTag(0xFFFFFFFF); // Larger than Integer.MAX_VALUE. |
| 545 | + output.writeRawBytes(new byte[32]); // Pad with a few random bytes. |
| 546 | + output.flush(); |
| 547 | + byte[] data = rawOutput.toByteString().toByteArray(); |
| 548 | + for (InputType inputType : InputType.values()) { |
| 549 | + CodedInputStream input = inputType.newDecoder(data); |
| 550 | + assertThrows(InvalidProtocolBufferException.class, input::readString); |
| 551 | + } |
| 552 | + } |
| 553 | + |
| 554 | + @Test |
| 555 | + public void testReadStringRequireUtf8WithSizeOverflow_throwsInvalidProtocolBufferException() |
| 556 | + throws Exception { |
| 557 | + ByteString.Output rawOutput = ByteString.newOutput(); |
| 558 | + CodedOutputStream output = CodedOutputStream.newInstance(rawOutput); |
| 559 | + |
| 560 | + output.writeUInt32NoTag(0xFFFFFFFF); // Larger than Integer.MAX_VALUE. |
| 561 | + output.writeRawBytes(new byte[32]); // Pad with a few random bytes. |
| 562 | + output.flush(); |
| 563 | + byte[] data = rawOutput.toByteString().toByteArray(); |
| 564 | + for (InputType inputType : InputType.values()) { |
| 565 | + CodedInputStream input = inputType.newDecoder(data); |
| 566 | + assertThrows(InvalidProtocolBufferException.class, input::readStringRequireUtf8); |
| 567 | + } |
| 568 | + } |
| 569 | + |
| 570 | + @Test |
| 571 | + public void testReadBytesWithHugeSizeOverflow_throwsInvalidProtocolBufferException() |
| 572 | + throws Exception { |
| 573 | + ByteString.Output rawOutput = ByteString.newOutput(); |
| 574 | + CodedOutputStream output = CodedOutputStream.newInstance(rawOutput); |
| 575 | + |
| 576 | + output.writeUInt32NoTag(0xFFFFFFFF); // Larger than Integer.MAX_VALUE. |
| 577 | + output.writeRawBytes(new byte[32]); // Pad with a few random bytes. |
| 578 | + output.flush(); |
| 579 | + byte[] data = rawOutput.toByteString().toByteArray(); |
| 580 | + for (InputType inputType : InputType.values()) { |
| 581 | + CodedInputStream input = inputType.newDecoder(data); |
| 582 | + assertThrows(InvalidProtocolBufferException.class, input::readBytes); |
| 583 | + } |
| 584 | + } |
| 585 | + |
| 586 | + @Test |
| 587 | + public void testReadByteArrayWithHugeSizeOverflow_throwsInvalidProtocolBufferException() |
| 588 | + throws Exception { |
| 589 | + ByteString.Output rawOutput = ByteString.newOutput(); |
| 590 | + CodedOutputStream output = CodedOutputStream.newInstance(rawOutput); |
| 591 | + |
| 592 | + output.writeUInt32NoTag(0xFFFFFFFF); // Larger than Integer.MAX_VALUE. |
| 593 | + output.writeRawBytes(new byte[32]); // Pad with a few random bytes. |
| 594 | + output.flush(); |
| 595 | + byte[] data = rawOutput.toByteString().toByteArray(); |
| 596 | + for (InputType inputType : InputType.values()) { |
| 597 | + CodedInputStream input = inputType.newDecoder(data); |
| 598 | + assertThrows(InvalidProtocolBufferException.class, input::readByteArray); |
| 599 | + } |
| 600 | + } |
| 601 | + |
| 602 | + @Test |
| 603 | + public void testReadByteBufferWithSizeOverflow_throwsInvalidProtocolBufferException() |
| 604 | + throws Exception { |
| 605 | + ByteString.Output rawOutput = ByteString.newOutput(); |
| 606 | + CodedOutputStream output = CodedOutputStream.newInstance(rawOutput); |
| 607 | + |
| 608 | + output.writeUInt32NoTag(0xFFFFFFFF); // Larger than Integer.MAX_VALUE. |
| 609 | + output.writeRawBytes(new byte[32]); // Pad with a few random bytes. |
| 610 | + output.flush(); |
| 611 | + byte[] data = rawOutput.toByteString().toByteArray(); |
| 612 | + for (InputType inputType : InputType.values()) { |
| 613 | + CodedInputStream input = inputType.newDecoder(data); |
| 614 | + assertThrows(InvalidProtocolBufferException.class, input::readByteBuffer); |
| 615 | + } |
| 616 | + } |
| 617 | + |
537 | 618 | /**
|
538 | 619 | * Test we can do messages that are up to CodedInputStream#DEFAULT_SIZE_LIMIT in size (2G or
|
539 | 620 | * Integer#MAX_SIZE).
|
|
0 commit comments