Skip to content

Commit c0d1519

Browse files
authored
connected sockets can be passed to the library (#925)
* connected sockets can be passed to the library fixes #924 Signed-off-by: Martin Volf <[email protected]> * removed pointless socket check; test coverage improved Signed-off-by: Martin Volf <[email protected]> * better test coverage Signed-off-by: Martin Volf <[email protected]> --------- Signed-off-by: Martin Volf <[email protected]>
1 parent 03f8b22 commit c0d1519

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

src/main/java/net/schmizz/sshj/SocketClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public void connect(String hostname, int port) throws IOException {
6565
this.hostname = hostname;
6666
this.port = port;
6767
socket = socketFactory.createSocket();
68-
socket.connect(makeInetSocketAddress(hostname, port), connectTimeout);
68+
if (! socket.isConnected()) {
69+
socket.connect(makeInetSocketAddress(hostname, port), connectTimeout);
70+
}
6971
onConnect();
7072
}
7173
}
@@ -104,7 +106,9 @@ public void connect(InetAddress host) throws IOException {
104106
public void connect(InetAddress host, int port) throws IOException {
105107
this.port = port;
106108
socket = socketFactory.createSocket();
107-
socket.connect(new InetSocketAddress(host, port), connectTimeout);
109+
if (! socket.isConnected()) {
110+
socket.connect(new InetSocketAddress(host, port), connectTimeout);
111+
}
108112
onConnect();
109113
}
110114

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.schmizz.sshj;
17+
18+
import com.hierynomus.sshj.test.SshServerExtension;
19+
import net.schmizz.sshj.SSHClient;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.MethodSource;
25+
26+
import org.apache.sshd.server.SshServer;
27+
28+
import java.io.IOException;
29+
import java.net.InetAddress;
30+
import java.net.InetSocketAddress;
31+
import java.net.Socket;
32+
import java.util.stream.Stream;
33+
34+
import javax.net.SocketFactory;
35+
36+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
37+
38+
39+
public class ConnectedSocketTest {
40+
@RegisterExtension
41+
public SshServerExtension fixture = new SshServerExtension();
42+
43+
@BeforeEach
44+
public void setupClient() throws IOException {
45+
SSHClient defaultClient = fixture.setupDefaultClient();
46+
}
47+
48+
private static interface Connector {
49+
void connect(SshServerExtension fx) throws IOException;
50+
}
51+
52+
private static void connectViaHostname(SshServerExtension fx) throws IOException {
53+
SshServer server = fx.getServer();
54+
fx.getClient().connect("localhost", server.getPort());
55+
}
56+
57+
private static void connectViaAddr(SshServerExtension fx) throws IOException {
58+
SshServer server = fx.getServer();
59+
InetAddress addr = InetAddress.getByName(server.getHost());
60+
fx.getClient().connect(addr, server.getPort());
61+
}
62+
63+
private static Stream<Connector> connectMethods() {
64+
return Stream.of(fx -> connectViaHostname(fx), fx -> connectViaAddr(fx));
65+
}
66+
67+
@ParameterizedTest
68+
@MethodSource("connectMethods")
69+
public void connectsIfUnconnected(Connector connector) {
70+
assertDoesNotThrow(() -> connector.connect(fixture));
71+
}
72+
73+
@ParameterizedTest
74+
@MethodSource("connectMethods")
75+
public void handlesConnected(Connector connector) throws IOException {
76+
Socket socket = SocketFactory.getDefault().createSocket();
77+
SocketFactory factory = new SocketFactory() {
78+
@Override
79+
public Socket createSocket() {
80+
return socket;
81+
}
82+
@Override
83+
public Socket createSocket(InetAddress host, int port) {
84+
return socket;
85+
}
86+
@Override
87+
public Socket createSocket(InetAddress address, int port,
88+
InetAddress localAddress, int localPort) {
89+
return socket;
90+
}
91+
@Override
92+
public Socket createSocket(String host, int port) {
93+
return socket;
94+
}
95+
@Override
96+
public Socket createSocket(String host, int port,
97+
InetAddress localHost, int localPort) {
98+
return socket;
99+
}
100+
};
101+
socket.connect(new InetSocketAddress("localhost", fixture.getServer().getPort()));
102+
fixture.getClient().setSocketFactory(factory);
103+
assertDoesNotThrow(() -> connector.connect(fixture));
104+
}
105+
}

0 commit comments

Comments
 (0)