Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connected sockets can be passed to the library #925

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/net/schmizz/sshj/SocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -104,7 +106,9 @@ 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();
}

Expand Down
105 changes: 105 additions & 0 deletions src/test/java/net/schmizz/sshj/ConnectedSocketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import org.apache.sshd.server.SshServer;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.stream.Stream;

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();
}

private static interface Connector {
void connect(SshServerExtension fx) throws IOException;
}

private static void connectViaHostname(SshServerExtension fx) throws IOException {
SshServer server = fx.getServer();
fx.getClient().connect("localhost", server.getPort());
}

private static void connectViaAddr(SshServerExtension fx) throws IOException {
SshServer server = fx.getServer();
InetAddress addr = InetAddress.getByName(server.getHost());
fx.getClient().connect(addr, server.getPort());
}

private static Stream<Connector> connectMethods() {
return Stream.of(fx -> connectViaHostname(fx), fx -> connectViaAddr(fx));
}

@ParameterizedTest
@MethodSource("connectMethods")
public void connectsIfUnconnected(Connector connector) {
assertDoesNotThrow(() -> connector.connect(fixture));
}

@ParameterizedTest
@MethodSource("connectMethods")
public void handlesConnected(Connector connector) 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(() -> connector.connect(fixture));
}
}
Loading