Fix SFTP tests to use OS selection port

**Cherry-pick to 4.2.x**
This commit is contained in:
Artem Bilan
2016-01-25 12:58:30 -05:00
parent e957f43477
commit 4d87572afd
2 changed files with 14 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2015 the original author or authors. * Copyright 2014-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -30,7 +30,6 @@ import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPublicKeySpec;
import java.util.Collections; import java.util.Collections;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import org.apache.sshd.SshServer; import org.apache.sshd.SshServer;
import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory; import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
@@ -48,10 +47,13 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.file.remote.session.Session;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
import com.jcraft.jsch.ChannelSftp.LsEntry;
/** /**
* * * *
* @author Gary Russell * @author Gary Russell
* @author David Liu * @author David Liu
* @author Artem Bilan
* @since 4.1 * @since 4.1
* *
*/ */
@@ -101,7 +103,7 @@ public class SftpServerTests {
} }
private void testKeyExchange(String pubKey, String privKey, String passphrase) private void testKeyExchange(String pubKey, String privKey, String passphrase)
throws Exception, IOException, InterruptedException { throws Exception {
SshServer server = SshServer.setUpDefaultServer(); SshServer server = SshServer.setUpDefaultServer();
final PublicKey allowedKey = decodePublicKey(pubKey); final PublicKey allowedKey = decodePublicKey(pubKey);
try { try {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2015 the original author or authors. * Copyright 2014-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.integration.sftp.session; package org.springframework.integration.sftp.session;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
@@ -30,7 +31,7 @@ import static org.mockito.Mockito.when;
import java.io.IOException; import java.io.IOException;
import java.net.ConnectException; import java.net.ConnectException;
import java.security.PublicKey; import java.security.PublicKey;
import java.util.Arrays; import java.util.Collections;
import org.apache.sshd.SshServer; import org.apache.sshd.SshServer;
import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.NamedFactory;
@@ -44,7 +45,6 @@ import org.junit.Test;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.integration.test.util.SocketUtils;
import org.springframework.integration.test.util.TestUtils; import org.springframework.integration.test.util.TestUtils;
import com.jcraft.jsch.JSchException; import com.jcraft.jsch.JSchException;
@@ -52,8 +52,8 @@ import com.jcraft.jsch.UserInfo;
/** /**
* @author Gary Russell * @author Gary Russell
* @author Artem Bilan
* @since 3.0.2 * @since 3.0.2
*
*/ */
public class SftpSessionFactoryTests { public class SftpSessionFactoryTests {
@@ -63,7 +63,6 @@ public class SftpSessionFactoryTests {
*/ */
@Test @Test
public void testConnectFailSocketOpen() throws Exception { public void testConnectFailSocketOpen() throws Exception {
final int port = SocketUtils.findAvailableServerSocket();
SshServer server = SshServer.setUpDefaultServer(); SshServer server = SshServer.setUpDefaultServer();
try { try {
server.setPasswordAuthenticator(new PasswordAuthenticator() { server.setPasswordAuthenticator(new PasswordAuthenticator() {
@@ -73,13 +72,13 @@ public class SftpSessionFactoryTests {
return true; return true;
} }
}); });
server.setPort(port); server.setPort(0);
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
server.start(); server.start();
DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
f.setHost("localhost"); f.setHost("localhost");
f.setPort(port); f.setPort(server.getPort());
f.setUser("user"); f.setUser("user");
f.setPassword("pass"); f.setPassword("pass");
int n = 0; int n = 0;
@@ -122,8 +121,6 @@ public class SftpSessionFactoryTests {
@Test @Test
public void testPasswordPassPhraseViaUserInfo() throws Exception { public void testPasswordPassPhraseViaUserInfo() throws Exception {
DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
f.setHost("localhost");
f.setPort(9999);
f.setUser("user"); f.setUser("user");
f.setAllowUnknownKeys(true); f.setAllowUnknownKeys(true);
UserInfo ui = mock(UserInfo.class); UserInfo ui = mock(UserInfo.class);
@@ -224,7 +221,6 @@ public class SftpSessionFactoryTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private DefaultSftpSessionFactory createServerAndClient(SshServer server) throws IOException { private DefaultSftpSessionFactory createServerAndClient(SshServer server) throws IOException {
final int port = SocketUtils.findAvailableServerSocket();
server.setPublickeyAuthenticator(new PublickeyAuthenticator() { server.setPublickeyAuthenticator(new PublickeyAuthenticator() {
@Override @Override
@@ -232,14 +228,14 @@ public class SftpSessionFactoryTests {
return true; return true;
} }
}); });
server.setPort(port); server.setPort(0);
server.setSubsystemFactories(Arrays.<NamedFactory<Command>> asList(new SftpSubsystem.Factory())); server.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystem.Factory()));
server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
server.start(); server.start();
DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory();
f.setHost("localhost"); f.setHost("localhost");
f.setPort(port); f.setPort(server.getPort());
f.setUser("user"); f.setUser("user");
Resource privateKey = new ClassPathResource("id_rsa"); Resource privateKey = new ClassPathResource("id_rsa");
f.setPrivateKey(privateKey); f.setPrivateKey(privateKey);