Cleanup OpenSSHKeyFile and add Disconnection test

This commit is contained in:
Jeroen van Erp
2018-07-10 16:29:09 +02:00
parent df5e73f1e8
commit adc0451b3f
3 changed files with 31 additions and 17 deletions

View File

@@ -75,7 +75,12 @@ public class OpenSSHKeyFile
@Override @Override
public void init(String privateKey, String publicKey) { public void init(String privateKey, String publicKey) {
if (publicKey != null) { if (publicKey != null) {
initPubKey(new StringReader(publicKey)); try {
initPubKey(new StringReader(publicKey));
} catch (IOException e) {
// let super provide both public & private key
log.warn("Error reading public key: {}", e.toString());
}
} }
super.init(privateKey, null); super.init(privateKey, null);
} }
@@ -85,23 +90,18 @@ public class OpenSSHKeyFile
* *
* @param publicKey Public key accessible through a {@code Reader} * @param publicKey Public key accessible through a {@code Reader}
*/ */
private void initPubKey(Reader publicKey) { private void initPubKey(Reader publicKey) throws IOException {
final BufferedReader br = new BufferedReader(publicKey);
try { try {
final BufferedReader br = new BufferedReader(publicKey); final String keydata = br.readLine();
try { if (keydata != null) {
final String keydata = br.readLine(); String[] parts = keydata.trim().split(" ");
if (keydata != null) { assert parts.length >= 2;
String[] parts = keydata.trim().split(" "); type = KeyType.fromString(parts[0]);
assert parts.length >= 2; pubKey = new Buffer.PlainBuffer(Base64.decode(parts[1])).readPublicKey();
type = KeyType.fromString(parts[0]);
pubKey = new Buffer.PlainBuffer(Base64.decode(parts[1])).readPublicKey();
}
} finally {
br.close();
} }
} catch (IOException e) { } finally {
// let super provide both public & private key br.close();
log.warn("Error reading public key: {}", e.toString());
} }
} }
} }

View File

@@ -127,7 +127,7 @@ public class SshFixture extends ExternalResource {
} }
}); });
sshServer.setCommandFactory(commandFactory); sshServer.setCommandFactory(commandFactory);
sshServer.setShellFactory(new ProcessShellFactory("ls"));
return sshServer; return sshServer;
} }

View File

@@ -16,13 +16,17 @@
package com.hierynomus.sshj.transport; package com.hierynomus.sshj.transport;
import com.hierynomus.sshj.test.SshFixture; import com.hierynomus.sshj.test.SshFixture;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.DisconnectReason; import net.schmizz.sshj.common.DisconnectReason;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.DisconnectListener; import net.schmizz.sshj.transport.DisconnectListener;
import net.schmizz.sshj.transport.TransportException; import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -89,4 +93,14 @@ public class DisconnectionTest {
assertFalse(joinToClientTransport(2)); assertFalse(joinToClientTransport(2));
} }
@Test
public void shouldNotThrowTimeoutOnDisconnect() throws IOException {
fixture.getClient().authPassword("u", "u");
Session session = fixture.getClient().startSession();
session.allocateDefaultPTY();
Session.Shell shell = session.startShell();
session.close();
fixture.getClient().disconnect();
}
} }