Skip to content

Commit

Permalink
#18 SFTP Server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maddingo committed Jan 3, 2017
1 parent 9ef16d9 commit 165d989
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
9 changes: 7 additions & 2 deletions sftp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd</artifactId>
<artifactId>sshd-core</artifactId>
<version>1.3.0</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.13</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
13 changes: 8 additions & 5 deletions sftp/src/main/java/no/maddin/niofs/sftp/SFTPHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions sftp/src/test/java/no/maddin/niofs/sftp/SFTPServerTest.java
Original file line number Diff line number Diff line change
@@ -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));

}
}

0 comments on commit 165d989

Please sign in to comment.