|
20 | 20 | import net.schmizz.sshj.sftp.OpenMode;
|
21 | 21 | import net.schmizz.sshj.sftp.RemoteFile;
|
22 | 22 | import net.schmizz.sshj.sftp.SFTPEngine;
|
| 23 | +import net.schmizz.sshj.sftp.SFTPException; |
23 | 24 | import org.junit.Rule;
|
24 | 25 | import org.junit.Test;
|
25 | 26 | import org.junit.rules.TemporaryFolder;
|
|
32 | 33 |
|
33 | 34 | import static org.hamcrest.CoreMatchers.equalTo;
|
34 | 35 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 36 | +import static org.junit.Assert.fail; |
35 | 37 |
|
36 | 38 | public class RemoteFileTest {
|
37 | 39 | @Rule
|
@@ -84,4 +86,92 @@ public void shouldNotGoOutOfBoundsInReadAheadInputStream() throws IOException {
|
84 | 86 |
|
85 | 87 | assertThat("The written and received data should match", data, equalTo(test2));
|
86 | 88 | }
|
| 89 | + |
| 90 | + @Test |
| 91 | + public void shouldNotReadAheadAfterLimitInputStream() throws IOException { |
| 92 | + SSHClient ssh = fixture.setupConnectedDefaultClient(); |
| 93 | + ssh.authPassword("test", "test"); |
| 94 | + SFTPEngine sftp = new SFTPEngine(ssh).init(); |
| 95 | + |
| 96 | + RemoteFile rf; |
| 97 | + File file = temp.newFile("SftpReadAheadLimitTest.bin"); |
| 98 | + rf = sftp.open(file.getPath(), EnumSet.of(OpenMode.WRITE, OpenMode.CREAT)); |
| 99 | + byte[] data = new byte[8192]; |
| 100 | + new Random(53).nextBytes(data); |
| 101 | + data[3072] = 1; |
| 102 | + rf.write(0, data, 0, data.length); |
| 103 | + rf.close(); |
| 104 | + |
| 105 | + assertThat("The file should exist", file.exists()); |
| 106 | + |
| 107 | + rf = sftp.open(file.getPath()); |
| 108 | + InputStream rs = rf.new ReadAheadRemoteFileInputStream(16 /*maxUnconfirmedReads*/,0, 3072); |
| 109 | + |
| 110 | + byte[] test = new byte[4097]; |
| 111 | + int n = 0; |
| 112 | + |
| 113 | + while (n < 2048) { |
| 114 | + n += rs.read(test, n, 2048 - n); |
| 115 | + } |
| 116 | + |
| 117 | + rf.close(); |
| 118 | + |
| 119 | + while (n < 3072) { |
| 120 | + n += rs.read(test, n, 3072 - n); |
| 121 | + } |
| 122 | + |
| 123 | + assertThat("buffer overrun", test[3072] == 0); |
| 124 | + |
| 125 | + try { |
| 126 | + rs.read(test, n, test.length - n); |
| 127 | + fail("Content must not be buffered"); |
| 128 | + } catch (SFTPException e){ |
| 129 | + // expected |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void limitedReadAheadInputStream() throws IOException { |
| 135 | + SSHClient ssh = fixture.setupConnectedDefaultClient(); |
| 136 | + ssh.authPassword("test", "test"); |
| 137 | + SFTPEngine sftp = new SFTPEngine(ssh).init(); |
| 138 | + |
| 139 | + RemoteFile rf; |
| 140 | + File file = temp.newFile("SftpReadAheadLimitedTest.bin"); |
| 141 | + rf = sftp.open(file.getPath(), EnumSet.of(OpenMode.WRITE, OpenMode.CREAT)); |
| 142 | + byte[] data = new byte[8192]; |
| 143 | + new Random(53).nextBytes(data); |
| 144 | + data[3072] = 1; |
| 145 | + rf.write(0, data, 0, data.length); |
| 146 | + rf.close(); |
| 147 | + |
| 148 | + assertThat("The file should exist", file.exists()); |
| 149 | + |
| 150 | + rf = sftp.open(file.getPath()); |
| 151 | + InputStream rs = rf.new ReadAheadRemoteFileInputStream(16 /*maxUnconfirmedReads*/,0, 3072); |
| 152 | + |
| 153 | + byte[] test = new byte[4097]; |
| 154 | + int n = 0; |
| 155 | + |
| 156 | + while (n < 2048) { |
| 157 | + n += rs.read(test, n, 2048 - n); |
| 158 | + } |
| 159 | + |
| 160 | + while (n < 3072) { |
| 161 | + n += rs.read(test, n, 3072 - n); |
| 162 | + } |
| 163 | + |
| 164 | + assertThat("buffer overrun", test[3072] == 0); |
| 165 | + |
| 166 | + n += rs.read(test, n, test.length - n); // --> ArrayIndexOutOfBoundsException |
| 167 | + |
| 168 | + byte[] test2 = new byte[data.length]; |
| 169 | + System.arraycopy(test, 0, test2, 0, test.length); |
| 170 | + |
| 171 | + while (n < data.length) { |
| 172 | + n += rs.read(test2, n, data.length - n); |
| 173 | + } |
| 174 | + |
| 175 | + assertThat("The written and received data should match", data, equalTo(test2)); |
| 176 | + } |
87 | 177 | }
|
0 commit comments