From 165d9893fcb49244e566bc7096be3bc7849a6aac Mon Sep 17 00:00:00 2001 From: Martin Goldhahn Date: Mon, 2 Jan 2017 03:26:01 +0100 Subject: [PATCH] #18 SFTP Server tests --- sftp/pom.xml | 9 ++- .../java/no/maddin/niofs/sftp/SFTPHost.java | 13 ++-- .../no/maddin/niofs/sftp/SFTPServerTest.java | 68 +++++++++++++++++++ 3 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 sftp/src/test/java/no/maddin/niofs/sftp/SFTPServerTest.java diff --git a/sftp/pom.xml b/sftp/pom.xml index 41cf567..7a7af74 100644 --- a/sftp/pom.xml +++ b/sftp/pom.xml @@ -35,9 +35,14 @@ org.apache.sshd - sshd + sshd-core 1.3.0 - pom + test + + + org.slf4j + slf4j-simple + 1.7.13 test diff --git a/sftp/src/main/java/no/maddin/niofs/sftp/SFTPHost.java b/sftp/src/main/java/no/maddin/niofs/sftp/SFTPHost.java index 645ff45..ddff0b8 100644 --- a/sftp/src/main/java/no/maddin/niofs/sftp/SFTPHost.java +++ b/sftp/src/main/java/no/maddin/niofs/sftp/SFTPHost.java @@ -28,14 +28,17 @@ public class SFTPHost extends FileSystem { this.host = serverUri.getHost(); this.port = serverUri.getPort(); String userInfo = serverUri.getUserInfo(); + String un = null; + String pw = null; if (userInfo != null) { String[] ui = userInfo.split(":"); - this.username = ui[0]; - this.password = ui[1]; - } else { - username = null; - password = null; + un = ui[0]; + if (ui.length > 1) { + pw = ui[1]; + } } + username = un; + password = pw; } @Override diff --git a/sftp/src/test/java/no/maddin/niofs/sftp/SFTPServerTest.java b/sftp/src/test/java/no/maddin/niofs/sftp/SFTPServerTest.java new file mode 100644 index 0000000..9f03545 --- /dev/null +++ b/sftp/src/test/java/no/maddin/niofs/sftp/SFTPServerTest.java @@ -0,0 +1,68 @@ +package no.maddin.niofs.sftp; + +import org.apache.sshd.server.SshServer; +import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; +import org.apache.sshd.server.scp.ScpCommandFactory; +import org.apache.sshd.server.shell.ProcessShellFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.net.ServerSocket; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + + +/** + * Tests that require a running SSHD server. + */ +public class SFTPServerTest { + + private SshServer sshd; + private int port; + + /** + * https://mina.apache.org/sshd-project/embedding_ssh.html + */ + @Before + public void setupSftpServer() throws Exception { + try (ServerSocket serverSocket = new ServerSocket(0)) { + sshd = SshServer.setUpDefaultServer(); + this.port = serverSocket.getLocalPort(); + serverSocket.close(); + sshd.setPort(this.port); + sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("target", "hostkey.ser"))); + + sshd.setShellFactory(new ProcessShellFactory(new String[]{"/bin/sh", "-i", "-l"})); + sshd.setCommandFactory(new ScpCommandFactory()); + sshd.start(); + } + } + + @After + public void stopServer() throws Exception { + sshd.stop(); + } + + @Test + public void createDirectories() throws Exception { + URI uri = new URI("sftp", "test", "localhost", port, "/a/b/", null, null); + Path path = Paths.get(uri); + Path newPath = Files.createDirectories(path); + assertThat(newPath, is(notNullValue())); +// File fileA = new File(rootFolder.getAbsolutePath(), "a"); +// assertThat(fileA.exists(), is(true)); +// assertThat(fileA.isDirectory(), is(true)); +// File fileB = new File(fileA.getAbsolutePath(), "b"); +// assertThat(fileB.exists(), is(true)); +// assertThat(fileB.isDirectory(), is(true)); + + } +}