mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
Added RemoteFileTest
This commit is contained in:
@@ -3,15 +3,21 @@ package com.hierynomus.sshj;
|
|||||||
import net.schmizz.sshj.Config;
|
import net.schmizz.sshj.Config;
|
||||||
import net.schmizz.sshj.DefaultConfig;
|
import net.schmizz.sshj.DefaultConfig;
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
|
import net.schmizz.sshj.transport.TransportException;
|
||||||
|
import net.schmizz.sshj.userauth.UserAuthException;
|
||||||
import net.schmizz.sshj.util.gss.BogusGSSAuthenticator;
|
import net.schmizz.sshj.util.gss.BogusGSSAuthenticator;
|
||||||
import org.apache.sshd.SshServer;
|
import org.apache.sshd.SshServer;
|
||||||
|
import org.apache.sshd.common.NamedFactory;
|
||||||
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
|
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
|
||||||
|
import org.apache.sshd.server.Command;
|
||||||
import org.apache.sshd.server.PasswordAuthenticator;
|
import org.apache.sshd.server.PasswordAuthenticator;
|
||||||
import org.apache.sshd.server.session.ServerSession;
|
import org.apache.sshd.server.session.ServerSession;
|
||||||
|
import org.apache.sshd.server.sftp.SftpSubsystem;
|
||||||
import org.junit.rules.ExternalResource;
|
import org.junit.rules.ExternalResource;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,6 +97,7 @@ public class SshFixture extends ExternalResource {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
sshServer.setGSSAuthenticator(new BogusGSSAuthenticator());
|
sshServer.setGSSAuthenticator(new BogusGSSAuthenticator());
|
||||||
|
sshServer.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystem.Factory()));
|
||||||
return sshServer;
|
return sshServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
75
src/test/java/com/hierynomus/sshj/sftp/RemoteFileTest.java
Normal file
75
src/test/java/com/hierynomus/sshj/sftp/RemoteFileTest.java
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package com.hierynomus.sshj.sftp;
|
||||||
|
|
||||||
|
import com.hierynomus.sshj.SshFixture;
|
||||||
|
import net.schmizz.sshj.SSHClient;
|
||||||
|
import net.schmizz.sshj.sftp.OpenMode;
|
||||||
|
import net.schmizz.sshj.sftp.RemoteFile;
|
||||||
|
import net.schmizz.sshj.sftp.SFTPEngine;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|
||||||
|
public class RemoteFileTest {
|
||||||
|
@Rule
|
||||||
|
public SshFixture fixture = new SshFixture();
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temp = new TemporaryFolder();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotGoOutOfBoundsInReadAheadInputStream() throws IOException {
|
||||||
|
SSHClient ssh = fixture.setupConnectedDefaultClient();
|
||||||
|
ssh.authPassword("test", "test");
|
||||||
|
SFTPEngine sftp = new SFTPEngine(ssh).init();
|
||||||
|
|
||||||
|
RemoteFile rf;
|
||||||
|
File file = temp.newFile("SftpReadAheadTest.bin");
|
||||||
|
rf = sftp.open(file.getPath(), EnumSet.of(OpenMode.WRITE, OpenMode.CREAT));
|
||||||
|
byte[] data = new byte[8192];
|
||||||
|
new Random(53).nextBytes(data);
|
||||||
|
data[3072] = 1;
|
||||||
|
rf.write(0, data, 0, data.length);
|
||||||
|
rf.close();
|
||||||
|
|
||||||
|
assertThat("The file should exist", file.exists());
|
||||||
|
|
||||||
|
rf = sftp.open(file.getPath());
|
||||||
|
InputStream rs = rf.new ReadAheadRemoteFileInputStream(16 /*maxUnconfirmedReads*/);
|
||||||
|
|
||||||
|
byte[] test = new byte[4097];
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
while (n < 2048) {
|
||||||
|
n += rs.read(test, n, 2048 - n);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (n < 3072) {
|
||||||
|
n += rs.read(test, n, 3072 - n);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (test[3072] != 0) {
|
||||||
|
System.err.println("buffer overrun!");
|
||||||
|
}
|
||||||
|
|
||||||
|
n += rs.read(test, n, test.length - n); // --> ArrayIndexOutOfBoundsException
|
||||||
|
|
||||||
|
byte[] test2 = new byte[data.length];
|
||||||
|
System.arraycopy(test, 0, test2, 0, test.length);
|
||||||
|
|
||||||
|
while (n < data.length) {
|
||||||
|
n += rs.read(test2, n, data.length - n);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThat("The written and received data should match", data, equalTo(test2));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user