Skip to content

Commit

Permalink
connected sockets can be passed to the library
Browse files Browse the repository at this point in the history
fixes #924

Signed-off-by: Martin Volf <vlci.doupe@gmail.com>
  • Loading branch information
martin-volf committed Jan 22, 2024
1 parent f94444b commit 0ea7932
Showing 2 changed files with 92 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/main/java/net/schmizz/sshj/SocketClient.java
Original file line number Diff line number Diff line change
@@ -65,7 +65,9 @@ public void connect(String hostname, int port) throws IOException {
this.hostname = hostname;
this.port = port;
socket = socketFactory.createSocket();
socket.connect(makeInetSocketAddress(hostname, port), connectTimeout);
if (! socket.isConnected()) {
socket.connect(makeInetSocketAddress(hostname, port), connectTimeout);
}
onConnect();
}
}
@@ -77,8 +79,10 @@ public void connect(String hostname, int port, InetAddress localAddr, int localP
this.hostname = hostname;
this.port = port;
socket = socketFactory.createSocket();
socket.bind(new InetSocketAddress(localAddr, localPort));
socket.connect(makeInetSocketAddress(hostname, port), connectTimeout);
if (! socket.isConnected()) {
socket.bind(new InetSocketAddress(localAddr, localPort));
socket.connect(makeInetSocketAddress(hostname, port), connectTimeout);
}
onConnect();
}
}
@@ -104,16 +108,20 @@ public void connect(InetAddress host) throws IOException {
public void connect(InetAddress host, int port) throws IOException {
this.port = port;
socket = socketFactory.createSocket();
socket.connect(new InetSocketAddress(host, port), connectTimeout);
if (! socket.isConnected()) {
socket.connect(new InetSocketAddress(host, port), connectTimeout);
}
onConnect();
}

public void connect(InetAddress host, int port, InetAddress localAddr, int localPort)
throws IOException {
this.port = port;
socket = socketFactory.createSocket();
socket.bind(new InetSocketAddress(localAddr, localPort));
socket.connect(new InetSocketAddress(host, port), connectTimeout);
if (! socket.isConnected()) {
socket.bind(new InetSocketAddress(localAddr, localPort));
socket.connect(new InetSocketAddress(host, port), connectTimeout);
}
onConnect();
}

78 changes: 78 additions & 0 deletions src/test/java/net/schmizz/sshj/ConnectedSocketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj;

import com.hierynomus.sshj.test.SshServerExtension;
import net.schmizz.sshj.SSHClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.net.SocketFactory;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;


public class ConnectedSocketTest {
@RegisterExtension
public SshServerExtension fixture = new SshServerExtension();

@BeforeEach
public void setupClient() throws IOException {
SSHClient defaultClient = fixture.setupDefaultClient();
}

@Test
public void connectsIfUnconnected() {
assertDoesNotThrow(() -> fixture.connectClient(fixture.getClient()));
}

@Test
public void handlesConnected() throws IOException {
Socket socket = SocketFactory.getDefault().createSocket();
SocketFactory factory = new SocketFactory() {
@Override
public Socket createSocket() {
return socket;
}
@Override
public Socket createSocket(InetAddress host, int port) {
return socket;
}
@Override
public Socket createSocket(InetAddress address, int port,
InetAddress localAddress, int localPort) {
return socket;
}
@Override
public Socket createSocket(String host, int port) {
return socket;
}
@Override
public Socket createSocket(String host, int port,
InetAddress localHost, int localPort) {
return socket;
}
};
socket.connect(new InetSocketAddress("localhost", fixture.getServer().getPort()));
fixture.getClient().setSocketFactory(factory);
assertDoesNotThrow(() -> fixture.connectClient(fixture.getClient()));
}
}

0 comments on commit 0ea7932

Please sign in to comment.