Throw exception if RandomAccessData tries to read beyond EOF

Fixes gh-12986
This commit is contained in:
Madhura Bhave
2018-05-01 16:35:43 -07:00
parent d268b2102f
commit aad279208e
3 changed files with 43 additions and 0 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.loader.data;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
@@ -96,6 +97,38 @@ public class RandomAccessDataFileTests {
new RandomAccessDataFile(file);
}
@Test
public void readWithOffsetAndLengthShouldRead() throws Exception {
byte[] read = this.file.read(2, 3);
assertThat(read).isEqualTo(new byte[] { 2, 3, 4 });
}
@Test
public void readWhenOffsetIsBeyondEOFShouldThrowException() throws Exception {
this.thrown.expect(IndexOutOfBoundsException.class);
this.file.read(257, 0);
}
@Test
public void readWhenOffsetIsBeyondEndOfSubsectionShouldThrowException() throws Exception {
this.thrown.expect(IndexOutOfBoundsException.class);
RandomAccessData subsection = this.file.getSubsection(0, 10);
subsection.read(11, 0);
}
@Test
public void readWhenOffsetPlusLengthGreaterThanEOFShouldThrowException() throws Exception {
this.thrown.expect(EOFException.class);
this.file.read(256, 1);
}
@Test
public void readWhenOffsetPlusLengthGreaterThanEndOfSubsectionShouldThrowException() throws Exception {
this.thrown.expect(EOFException.class);
RandomAccessData subsection = this.file.getSubsection(0, 10);
subsection.read(10, 1);
}
@Test
public void inputStreamRead() throws Exception {
for (int i = 0; i <= 255; i++) {