From 9982e5c30eaebd5ae13b309c759d346498a9560f Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Wed, 17 Jun 2015 21:49:05 +0200 Subject: [PATCH] Added testcase for #194 --- .../channel/direct/CommandTest.java | 34 +++++++++++++++++++ .../com/hierynomus/sshj/test/SshFixture.java | 21 +++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/hierynomus/sshj/connection/channel/direct/CommandTest.java diff --git a/src/test/java/com/hierynomus/sshj/connection/channel/direct/CommandTest.java b/src/test/java/com/hierynomus/sshj/connection/channel/direct/CommandTest.java new file mode 100644 index 00000000..224a4017 --- /dev/null +++ b/src/test/java/com/hierynomus/sshj/connection/channel/direct/CommandTest.java @@ -0,0 +1,34 @@ +package com.hierynomus.sshj.connection.channel.direct; + +import com.hierynomus.sshj.test.SshFixture; +import net.schmizz.sshj.SSHClient; +import net.schmizz.sshj.connection.channel.direct.Session; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; + +import static org.hamcrest.MatcherAssert.assertThat; + +public class CommandTest { + + @Rule + public SshFixture fixture = new SshFixture(); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void shouldExecuteBackgroundCommand() throws IOException { + SSHClient sshClient = fixture.setupConnectedDefaultClient(); + sshClient.authPassword("jeroen", "jeroen"); + File file = new File(temp.getRoot(), "testdir"); + assertThat("File should not exist", !file.exists()); + Session.Command exec = sshClient.startSession().exec("mkdir " + file.getPath() + " &"); + exec.join(); + assertThat("File should exist", file.exists()); + assertThat("File should be directory", file.isDirectory()); + } +} diff --git a/src/test/java/com/hierynomus/sshj/test/SshFixture.java b/src/test/java/com/hierynomus/sshj/test/SshFixture.java index 3ec5f0d6..4f18b970 100644 --- a/src/test/java/com/hierynomus/sshj/test/SshFixture.java +++ b/src/test/java/com/hierynomus/sshj/test/SshFixture.java @@ -9,15 +9,22 @@ import net.schmizz.sshj.util.gss.BogusGSSAuthenticator; import org.apache.sshd.SshServer; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.keyprovider.FileKeyPairProvider; +import org.apache.sshd.common.util.OsUtils; import org.apache.sshd.server.Command; +import org.apache.sshd.server.CommandFactory; import org.apache.sshd.server.PasswordAuthenticator; +import org.apache.sshd.server.channel.ChannelSession; +import org.apache.sshd.server.command.ScpCommandFactory; import org.apache.sshd.server.session.ServerSession; import org.apache.sshd.server.sftp.SftpSubsystem; +import org.apache.sshd.server.shell.ProcessShellFactory; import org.junit.rules.ExternalResource; import java.io.IOException; import java.net.ServerSocket; +import java.util.Arrays; import java.util.Collections; +import java.util.EnumSet; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -97,7 +104,19 @@ public class SshFixture extends ExternalResource { } }); sshServer.setGSSAuthenticator(new BogusGSSAuthenticator()); - sshServer.setSubsystemFactories(Collections.>singletonList(new SftpSubsystem.Factory())); + sshServer.setSubsystemFactories(Arrays.>asList(new SftpSubsystem.Factory())); + sshServer.setCommandFactory(new ScpCommandFactory(new CommandFactory() { + public Command createCommand(String command) { + EnumSet ttyOptions; + if (OsUtils.isUNIX()) { + ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr); + } else { + ttyOptions = EnumSet.of(ProcessShellFactory.TtyOptions.Echo, ProcessShellFactory.TtyOptions.ICrNl, ProcessShellFactory.TtyOptions.ONlCr); + } + return new ProcessShellFactory(command.split(" "), ttyOptions).create(); + } + })); + return sshServer; }