当前位置:   article > 正文

Springboot 利用Apache sshd 模拟sftp server进行Junit 测试_springboot apache sshd

springboot apache sshd
使用初衷

        最近在做Junit测试,因而需要对代码进行挡泥板操作,但是在编写过程中遇到了难题,sftp这个东西没法mock出来。很是头痛!后来想的是那可以模拟临时启动一个,结束后在删掉。因而在选型时采用了sshd+jsch的模式

项目配置
 <dependency>
     <groupId>org.apache.sshd</groupId>
     <artifactId>sshd-core</artifactId>
     <version>1.7.0</version>
 </dependency>
  <dependency>
     <groupId>com.jcraft</groupId>
     <artifactId>jsch</artifactId>
     <version>0.1.55</version>
 </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
测试用例

package com.juneyaoair;

import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
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.subsystem.sftp.SftpSubsystemFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Properties;
import java.util.Vector;


@Slf4j
public class SFTPMockTest {


    private SshServer sshd;


    @Before
    public void beforeTestSetup() throws Exception {
        sshd = SshServer.setUpDefaultServer();
        sshd.setPort(2294);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("host.ser")));
        sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
        sshd.setPasswordAuthenticator((username, password, session) ->
                username.equals("test") && password.equals("password"));
        sshd.setCommandFactory(new ScpCommandFactory());
        sshd.start();
        log.info("SFTP server started");

    }

    @After
    public void teardown() throws Exception {
        sshd.stop();
    }


    @Test
    public void test() throws JSchException {
        Channel channel = null;
        ChannelSftp sftp = null;
        try {
            JSch jSch = new JSch();
            //以test身份登录,默认在2294端口
            Session session = jSch.getSession("test", "localhost", 2294);
            session.setPassword("password");
            Properties config = new Properties();
            //设置不用检查HostKey
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            //使用sftp操作
            channel = session.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            //操作-----------------------------------------------
            sftp.cd("/");
            InputStream is = new InputStream() {
                @Override
                public int read() throws IOException {
                    return -1;
                }
            };
            sftp.put(is, "emptyFile.txt");
            Vector<ChannelSftp.LsEntry> files = sftp.ls("/");
            files.forEach(r -> System.out.println(r.getFilename()));
            //操作-----------------------------------------------
            Assert.assertTrue(true);
        } catch (Exception e) {
            log.error("错误{}", e.getMessage());
        } finally {
            assert sftp != null;
            sftp.disconnect();
            channel.disconnect();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
编写中遇到的问题
1、jar版本原因导致的部分参数无法导入

最新版为2.5.0 一般google出来的都是1.7.0版本。变化较大,官网给的还是老版本使用方式。

VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
//该处参数为path  而不是String
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
  • 1
  • 2
  • 3
  • 4
//SftpSubsystem.Factory这个内部类根本就不存在
sshd.setSubsystemFactories( Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
  • 1
  • 2
//CommandFactory  Command这个类高版本已经不存在了
CommandFactory myCommandFactory = new CommandFactory() {
	
	public Command createCommand(String command) {
		System.out.println("Command: " + command);
		return null;
	}
};
sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));
   List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
	    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/57679?site
推荐阅读
相关标签
  

闽ICP备14008679号