diff --git a/src/main/java/net/schmizz/sshj/sftp/PathComponents.java b/src/main/java/net/schmizz/sshj/sftp/PathComponents.java index 37ad0bed..c57d3024 100644 --- a/src/main/java/net/schmizz/sshj/sftp/PathComponents.java +++ b/src/main/java/net/schmizz/sshj/sftp/PathComponents.java @@ -18,8 +18,15 @@ package net.schmizz.sshj.sftp; public class PathComponents { static String adjustForParent(String parent, String path, String pathSep) { - return (path.startsWith(pathSep)) ? path // Absolute path, nothing to adjust - : (parent + (parent.endsWith(pathSep) ? "" : pathSep) + path); // Relative path + if (path.startsWith(pathSep)) { // Absolute path + return path; +// } else if (parent.isEmpty()) { // Relative path +// return path; + } else if (parent.endsWith(pathSep)) { + return parent + path; + } else { + return parent + pathSep + path; + } } static String trimTrailingSeparator(String somePath, String pathSep) { diff --git a/src/test/java/com/hierynomus/sshj/IntegrationTest.java b/src/test/java/com/hierynomus/sshj/IntegrationTest.java index 52af7e7f..b745ed0a 100644 --- a/src/test/java/com/hierynomus/sshj/IntegrationTest.java +++ b/src/test/java/com/hierynomus/sshj/IntegrationTest.java @@ -17,23 +17,37 @@ package com.hierynomus.sshj; import net.schmizz.sshj.DefaultConfig; import net.schmizz.sshj.SSHClient; +import net.schmizz.sshj.connection.channel.direct.Session; +import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder; +import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener; import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts; +import net.schmizz.sshj.transport.verification.PromiscuousVerifier; import org.junit.Ignore; import org.junit.Test; import java.io.File; import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.CountDownLatch; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; public class IntegrationTest { @Test @Ignore // Should only be enabled for testing against VM - public void shouldConnect() throws IOException { + public void shouldConnect() throws IOException, InterruptedException { SSHClient sshClient = new SSHClient(new DefaultConfig()); - sshClient.addHostKeyVerifier(new OpenSSHKnownHosts(new File("/Users/ajvanerp/.ssh/known_hosts"))); + sshClient.addHostKeyVerifier(new PromiscuousVerifier()); sshClient.connect("172.16.37.129"); sshClient.authPassword("jeroen", "jeroen"); assertThat("Is connected", sshClient.isAuthenticated()); + sshClient.getRemotePortForwarder().bind( + // where the server should listen + new RemotePortForwarder.Forward("0.0.0.0", 8080), + // what we do with incoming connections that are forwarded to us + new SocketForwardingConnectListener(new InetSocketAddress("localhost", 8000))); + new CountDownLatch(1).await(); + } } diff --git a/src/test/java/com/hierynomus/sshj/sftp/SFTPClientTest.java b/src/test/java/com/hierynomus/sshj/sftp/SFTPClientTest.java index a44716f0..90d80b60 100644 --- a/src/test/java/com/hierynomus/sshj/sftp/SFTPClientTest.java +++ b/src/test/java/com/hierynomus/sshj/sftp/SFTPClientTest.java @@ -18,14 +18,21 @@ package com.hierynomus.sshj.sftp; import com.hierynomus.sshj.test.SshFixture; import com.hierynomus.sshj.test.util.FileUtil; import net.schmizz.sshj.SSHClient; +import net.schmizz.sshj.sftp.FileAttributes; import net.schmizz.sshj.sftp.SFTPClient; +import org.hamcrest.CoreMatchers; import org.junit.Rule; import org.junit.Test; +import org.junit.experimental.theories.DataPoint; import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; + public class SFTPClientTest { @Rule @@ -51,4 +58,21 @@ public class SFTPClientTest { sshClient.disconnect(); } } + + @Test + @DataPoint + public void shouldNotCreateAbsoluteDirectoryWhenPathIsRelative() throws IOException { + SSHClient sshClient = fixture.setupConnectedDefaultClient(); + sshClient.authPassword("test", "test"); + SFTPClient sftpClient = sshClient.newSFTPClient(); + String foo = sftpClient.canonicalize("foo"); + String fooAbs = sftpClient.canonicalize("/foo"); + assertThat(sftpClient.statExistence("foo"), nullValue()); + sftpClient.mkdirs("foo"); + assertThat(sftpClient.statExistence("foo"), notNullValue()); + sftpClient.rmdir("foo"); + assertThat(sftpClient.statExistence("foo"), nullValue()); + sftpClient.close(); + sshClient.disconnect(); + } }