如何在Junit测试中绕过jsch.addIdentity()?

如何在Junit测试中绕过jsch.addIdentity()?,junit,jsch,apache-mina,Junit,Jsch,Apache Mina,我使用ApacheMina sshd为Junit测试目的设置模拟ssh服务器。由于ApacheMina的文档非常不清楚,我无法理解如何在测试中处理身份验证问题 我想测试的代码,基本上是使用Jsch将文件从本地传输到远程 public static void scpTo(String rfilePath, String lfilePath, String user, String host, String keyPath, int port) { FileInputStream fis=n

我使用ApacheMina sshd为Junit测试目的设置模拟ssh服务器。由于ApacheMina的文档非常不清楚,我无法理解如何在测试中处理身份验证问题

我想测试的代码,基本上是使用Jsch将文件从本地传输到远程

public static void scpTo(String rfilePath, String lfilePath, String user, String host, String keyPath, int port) {
    FileInputStream fis=null;
    try {
        while(true) {
            String rfile = rfilePath;
            String lfile = lfilePath;

            JSch jsch = new JSch();
            jsch.addIdentity(keyPath);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");

            Session session = jsch.getSession(user, host, port);
            session.setConfig(config);

            session.connect();

            // exec 'scp -t rfile' remotely
            String command = "scp " + " -t " + rfile;
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            // get I/O streams for remote scp
            OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            channel.connect();

            if (checkAck(in) != 0) {
                System.exit(0);
            }

            File _lfile = new File(lfile);
            if(!_lfile.exists()) {
                System.err.println("Local file not existing");
            }

            //check file existing
            File _rfile = new File(rfile);
            if (_rfile.exists()) {
                System.out.println("Remote file already existed");
                break;
            }

            // send "C0644 filesize filename", where filename should not include '/'
            long filesize = _lfile.length();
            command = "C0644 " + filesize + " ";
            if (lfile.lastIndexOf('/') > 0) {
                command += lfile.substring(lfile.lastIndexOf('/') + 1);
            } else {
                command += lfile;
            }
            command += "\n";
            out.write(command.getBytes());
            out.flush();
            if (checkAck(in) != 0) {
                System.exit(0);
            }

            // send a content of lfile
            fis = new FileInputStream(lfile);
            byte[] buf = new byte[1024];
            while (true) {
                int len = fis.read(buf, 0, buf.length);
                if (len <= 0) break;
                out.write(buf, 0, len); //out.flush();
            }
            fis.close();
            fis = null;
            // send '\0'
            buf[0] = 0;
            out.write(buf, 0, 1);
            out.flush();
            if (checkAck(in) != 0) {
                System.exit(0);
            }
            out.close();

            channel.disconnect();
            session.disconnect();

            //System.exit(0);

            Thread.sleep(2000);
        }
    } catch (Exception e) {
        System.out.println(e);
        try {
            if (fis != null) fis.close();
        } catch (Exception ee) {
        }
    }
}
publicstaticvoidscpto(stringrfilepath、stringlfilepath、stringuser、stringhost、stringkeypath、int端口){
FileInputStream fis=null;
试一试{
while(true){
字符串rfile=rfilePath;
字符串lfile=lfilePath;
JSch JSch=新的JSch();
jsch.额外性(关键路径);
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
Session Session=jsch.getSession(用户、主机、端口);
session.setConfig(config);
session.connect();
//远程执行“scp-t文件”
String command=“scp”+“-t”+rfile;
Channel=session.openChannel(“exec”);
((ChannelExec)channel).setCommand(command);
//获取远程scp的I/O流
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
如果(检查确认(in)!=0){
系统出口(0);
}
文件_lfile=新文件(lfile);
如果(!\u lfile.exists()){
System.err.println(“本地文件不存在”);
}
//检查文件是否存在
文件_rfile=新文件(rfile);
如果(_rfile.exists()){
System.out.println(“远程文件已存在”);
打破
}
//发送“C0644文件大小文件名”,其中文件名不应包括“/”
长文件大小=_lfile.length();
command=“C0644”+文件大小+”;
如果(lfile.lastIndexOf('/')>0){
command+=lfile.substring(lfile.lastIndexOf('/')+1);
}否则{
command+=lfile;
}
命令+=“\n”;
out.write(command.getBytes());
out.flush();
如果(检查确认(in)!=0){
系统出口(0);
}
//发送lfile的内容
fis=新文件输入流(lfile);
字节[]buf=新字节[1024];
while(true){
int len=fis.read(buf,0,buf.length);

如果(len您需要更改此行: 添加(新的UserAuthPassword.Factory()); 到
UserAuthFactorys.add(新的UserAuthPublicKey.Factory());

您需要更改此行: 添加(新的UserAuthPassword.Factory()); 到
userauthfactures.add(new UserAuthPublicKey.Factory());

提示:您的方法scpTo对许多事情都有帮助。我郑重建议您研究“干净的代码”由Robert Martin编写…或花一些时间观看…您的代码可以从中受益匪浅…提示:您的方法scpTo正在做许多事情。我郑重建议您研究由Robert Martin编写的“干净代码”…或花一些时间观看…您的代码可以从中受益匪浅。。。
@Before
public void setup() {
    user = "siyangli";
    host = "localhost";
    //pick a port not occupied for tesing
    port = 22998;
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(port);
    keyPath = "/Users/siyangli/.ssh/id_rsa";
    //it will change the key file??? do not know why, how to work around the authentication key??
    sshd.setKeyPairProvider(new FileKeyPairProvider(new String[]{"/Users/siyangli/.ssh/id_rsa"}));
    //sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(keyPath));
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
        public boolean authenticate(String username, String password, ServerSession session) {
            return true;
        }
    });
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return true;
        }
    });
    CommandFactory myCommandFactory = new CommandFactory() {
        public Command createCommand(String command) {
            System.out.println("Command: " + command);
            return null;
        }
    };
    sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));
    List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
    userAuthFactories.add(new UserAuthPassword.Factory());
    sshd.setUserAuthFactories(userAuthFactories);
    List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
    namedFactoryList.add(new SftpSubsystem.Factory()); sshd.setSubsystemFactories(namedFactoryList);
    try {
        sshd.start();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    System.out.println("Finished setup !!! ");
}