GithubHelp home page GithubHelp logo

stefanbirkner / fake-sftp-server-lambda Goto Github PK

View Code? Open in Web Editor NEW
23.0 6.0 10.0 90 KB

Runs an in-memory SFTP server while your tests are running.

License: MIT License

Java 100.00%
sftp sftp-server java test unittest inmemory

fake-sftp-server-lambda's Introduction

Fake SFTP Server Lambda

Build Status

Fake SFTP Server Lambda runs an in-memory SFTP server while your tests are running. It uses the SFTP server of the Apache SSHD project.

Fake SFTP Server Rule is published under the MIT license. It requires at least Java 8.

For JUnit 4 there is an alternative to Fake SFTP Server Lambda. Its name is Fake SFTP Server Rule.

Installation

Fake SFTP Server Lambda is available from Maven Central.

Fake SFTP Server Lambda uses the SFTP server of the Apache SSHD project. The API of the server changed in version 2.6.0 in a way that required changes in Fake SFTP Server Lambda. Therefore, there are two lines of development. As long as your project does not depend on an older version of Apache SSHD you should use Fake SFTP Server Lambda 2.0.0 which is compatible with Apache SSHD 2.6.0 and later.

<dependency>
  <groupId>com.github.stefanbirkner</groupId>
  <artifactId>fake-sftp-server-lambda</artifactId>
  <version>2.0.0</version>
  <scope>test</scope>
</dependency>

Users whose projects have a dependency to Apache SSHD with a version before 2.6.0 must use Fake SFTP Server Lambda 1.0.1.

<dependency>
  <groupId>com.github.stefanbirkner</groupId>
  <artifactId>fake-sftp-server-lambda</artifactId>
  <version>1.0.1</version>
  <scope>test</scope>
</dependency>

Usage

Fake SFTP Server Lambda is used by wrapping your test code with the method withSftpServer.

import static com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.withSftpServer;

public class TestClass {
  @Test
  public void someTest() throws Exception {
    withSftpServer(server -> {
      //test code
    });
  }
}

withSftpServer starts an SFTP server before executing the test code and shuts down the server afterwards. The test code uses the provided server object to obtain information about the running server or use additional features of Fake SFTP Server Lambda.

By default the SFTP server listens on an auto-allocated port. During the test this port can be obtained by server.getPort(). It can be changed by calling setPort(int). The server is restarted whenever this method is called.

withSftpServer(server -> {
  server.setPort(1234);
  ...
});

You can interact with the SFTP server by using the SFTP protocol with password authentication. By default the server accepts every pair of username and password, but you can restrict it to specific pairs.

withSftpServer(server -> {
  server.addUser("username", "password");
  ...
});

Testing code that reads files

If you test code that reads files from an SFTP server then you need a server that provides these files. The server object has a shortcut for putting files to the server.

@Test
public void testTextFile() throws Exception {
  withSftpServer(server -> {
    server.putFile("/directory/file.txt", "content of file", UTF_8);
    //code that reads the file using the SFTP protocol
  });
}

@Test
public void testBinaryFile() throws Exception {
  withSftpServer(server -> {
    byte[] content = createContent();
    server.putFile("/directory/file.bin", content);
    //code that reads the file using the SFTP protocol
  });
}

Test data that is provided as an input stream can be put directly from that input stream. This is very handy if your test data is available as a resource.

@Test
public void testFileFromInputStream() throws Exception {
  withSftpServer(server -> {
    InputStream is = getClass().getResourceAsStream("data.bin");
    server.putFile("/directory/file.bin", is);
    //code that reads the file using the SFTP protocol
  });
}

If you need an empty directory then you can use the method createDirectory(String).

@Test
public void testDirectory() throws Exception {
  withSftpServer(server -> {
    server.createDirectory("/a/directory");
    //code that reads from or writes to that directory
  });
}

You may create multiple directories at once with createDirectories(String...).

@Test
public void testDirectories() throws Exception {
  withSftpServer(server -> {
    server.createDirectories(
      "/a/directory",
      "/another/directory"
    );
    //code that reads from or writes to that directories
  });
}

Testing code that writes files

If you test code that writes files to an SFTP server then you need to verify the upload. The server object provides a shortcut for getting the file's content from the server.

@Test
public void testTextFile() throws Exception {
  withSftpServer(server -> {
    //code that uploads the file using the SFTP protocol
    String fileContent = server.getFileContent("/directory/file.txt", UTF_8);
    //verify file content
  });
}

@Test
public void testBinaryFile() throws Exception {
  withSftpServer(server -> {
    //code that uploads the file using the SFTP protocol
    byte[] fileContent = server.getFileContent("/directory/file.bin");
    //verify file content
  });
}

Testing existence of files

If you want to check whether a file was created or deleted then you can verify that it exists or not.

@Test
public void testFile() throws Exception {
  withSftpServer(server -> {
    //code that uploads or deletes the file
    boolean exists = server.existsFile("/directory/file.txt");
    //check value of exists variable
  });
}

The method returns true iff the file exists and it is not a directory.

Delete all files

If you want to reuse the SFTP server then you can delete all files and directories on the SFTP server. (This is rarely necessary because the method withSftpServer takes care that it starts and ends with a clean SFTP server.)

server.deleteAllFilesAndDirectories()

Contributing

You have three options if you have a feature request, found a bug or simply have a question about Fake SFTP Server Lambda.

Development Guide

Fake SFTP Server Lambda is build with Maven. If you want to contribute code then

  • Please write a test for your change.
  • Ensure that you didn't break the build by running mvn verify -Dgpg.skip.
  • Fork the repo and create a pull request. (See Understanding the GitHub Flow)

The basic coding style is described in the EditorConfig file .editorconfig.

Fake SFTP Server Lambda supports Travis CI for continuous integration. Your pull request will be automatically build by Travis CI.

Release Guide

  • Select a new version according to the Semantic Versioning 2.0.0 Standard.
  • Set the new version in pom.xml and in the Installation section of this readme.
  • Commit the modified pom.xml and README.md.
  • Run mvn clean deploy with JDK 8.
  • Add a tag for the release: git tag fake-sftp-server-lambda-X.X.X

fake-sftp-server-lambda's People

Contributors

stefanbirkner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fake-sftp-server-lambda's Issues

AbstractMethodError: missing implementation of createFileSystem

At some point within the last month our tests started failing without any code change. Does anyone know where to start troubleshooting following issue?

[           main] c.s.gcssftpadapter.service.SftpWriter    : Trying to create file <stuff> in dir </testing/stuff> on server <localhost>
[           main] o.s.i.s.s.DefaultSftpSessionFactory      : The authenticity of host 'localhost' can't be established.
RSA key fingerprint is 6a:da:7f:02:81:62:3b:d4:1d:27:85:e1:20:9d:d1:36.
Are you sure you want to continue connecting?
[           main] com.jcraft.jsch                          : Permanently added 'localhost' (RSA) to the list of known hosts.
[)-nio2-thread-5] o.a.s.server.session.ServerSessionImpl   : Session testing@/127.0.0.1:55002 authenticated
[)-nio2-thread-2] o.a.s.server.session.ServerSessionImpl   : exceptionCaught(ServerSessionImpl[testing@/127.0.0.1:55002])[state=Opened] RuntimeSshException: null
[)-nio2-thread-2] o.a.s.server.session.ServerSessionImpl   : exceptionCaught(ServerSessionImpl[testing@/127.0.0.1:55002])[state=Opened] caused by AbstractMethodError: Receiver class com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer$$Lambda$1219/0x0000000800ba5440 does not define or inherit an implementation of the resolved method 'abstract java.nio.file.FileSystem createFileSystem(org.apache.sshd.common.session.SessionContext)' of interface org.apache.sshd.common.file.FileSystemFactory.
java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.7+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode)

Using com.github.stefanbirkner.fake-sftp-server-lambda:1.0.0, com.jcraft.jsch:0.1.55

Doesn't work with sshd-sftp 2.5

The library doesn't work with sshd-sftp 2.5:

 16:54:51.029 [warn] - [SessionHelper.java:1172] - exceptionCaught(ServerSessionImpl[ssh-user@/127.0.0.1:35184])[state=Opened] RuntimeSshException: null
16:54:51.029 [warn] - [SessionHelper.java:1179] - exceptionCaught(ServerSessionImpl[ssh-user@/127.0.0.1:35184])[state=Opened] caused by AbstractMethodError: Receiver class com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer$$Lambda$12324/0x00000008036c1440 does not define or inherit an implementation of the resolved method abstract createFileSystem(Lorg/apache/sshd/common/session/SessionContext;)Ljava/nio/file/FileSystem; of interface org.apache.sshd.common.file.FileSystemFactory.

In general, the dependency range (any version >= 2) is too loose. Clearly breaking changes are happening to the dependency and such a loose dependency version shunts the work of having to find a compatible version onto the user.

use stable library versions

I get

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project jfr-srv-batch: Could not resolve dependencies for project com.edb.fs.tac.jfr.srv:jfr-srv-batch:jar:8.0.3-SNAPSHOT: Failed to collect dependencies at com.github.stefanbirkner:fake-sftp-server-lambda:jar:1.0.0 -> com.github.marschall:memoryfilesystem:jar:[0.8.0,)

would it be netter to just depend on a fixed version instead of using ranges?

Apache sshd-sftp:2.9.0 breaks the server

sshd-sftp:2.9.0 was recently released and the fake-sftp-server stopped working.
Pinning the sshd-sftp to 2.8.0 fixes the problem.

Thank you for the useful library!

It gives Auth fail when try to connect via jsch

My code is as bellow. The server is creating properly and i can put files as well. But when i try to connect to server using jsch, it always gives "Auth fail" in session.connect()

    withSftpServer(server -> {
        server.addUser("username", "password1").setPort(1234);

        JSch jsch = new JSch();
        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;
        final int rPort = Integer.parseInt("22");
        String knownHostPublicKey = "127.0.0.1" + " ssh-rsa " + <<HOST-KEY>>;
        jsch.setKnownHosts(new ByteArrayInputStream(knownHostPublicKey.getBytes()));
        session = jsch.getSession("username", "127.0.0.1", rPort);
        session.setPassword("password1");
        session.setTimeout(25000);
        session.connect(25000); **//Auth fail here**
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;

    });

Tests fails only on github actions

Running the following test in local machine runs fine but not on github actions (linux java 11) kotlin project
I get

java.lang.IllegalStateException at withSftpServer
  Caused by: java.net.SocketException at withSftpServer

Code:

@SpringBootTest
@DirtiesContext
class SFTPClient {
    @Test
    @Throws(Exception::class)
    fun test() {
        withSftpServer { server ->
    server.setPort(22)
            server.addUser("username","")
            server.createDirectory("myDir")
        }
    }
}

Unable to start SFTP Server on ubuntu

Hi,
With the given package I am unable to start the SFTP server on Ubuntu system.
I am seeing server port up message. But actually server is not getting up

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
The server is started on the port 40745

Enhancement: Split Start and Stop of Server

I've got a couple of detailed test cases where we do multiple sftp related calls. They look very odd when I have to wrap all of the test rows inside the withSftpServer lambda. It would be much more preferable if start and stop of the server was independent of each other.

Is this project still alive

hello @stefanbirkner

First of all thank you for this lib we've been using for a few years !

are you accepting contribution (mainly dependencies upgrades) ?
are you willing to make a new release based on those PRs ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.