BAEL-6560: How to solve SocketException Connection reset (#14636)

This commit is contained in:
ACHRAF TAITAI 2023-08-22 10:10:05 +02:00 committed by GitHub
parent 12a678018f
commit 420305fe3b
2 changed files with 60 additions and 2 deletions

View File

@ -0,0 +1,32 @@
package com.baeldung.socketexception;
import java.io.IOException;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
public class SslServer {
public static void createSSLSocketWithEnabledProtocols(SSLServerSocketFactory socketFactory, int port, String[] enabledProtocols) {
SSLServerSocket serverSocket = null;
try {
serverSocket = (SSLServerSocket) socketFactory.createServerSocket(port);
// Set the enabled protocols
serverSocket.setEnabledProtocols(enabledProtocols);
System.out.println("Server is running on port " + port);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

View File

@ -1,25 +1,33 @@
package com.baeldung.socketexception;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.net.SocketException;
import java.util.concurrent.Executors;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import org.junit.BeforeClass;
import org.junit.Test;
public class SocketExceptionHandlingUnitTest {
private static final int PORT = 6699;
@BeforeClass
public static void runServer() throws IOException, InterruptedException {
Executors.newSingleThreadExecutor()
.submit(() -> new SocketServer().start(6699));
.submit(() -> new SocketServer().start(PORT));
Thread.sleep(100);
}
@Test
public void givenRunningServer_whenConnectToClosedSocket_thenHandleException() throws IOException {
SocketClient client = new SocketClient();
client.startConnection("127.0.0.1", 6699);
client.startConnection("127.0.0.1", PORT);
try {
client.sendMessage("hi");
client.sendMessage("hi again");
@ -28,4 +36,22 @@ public class SocketExceptionHandlingUnitTest {
}
}
@Test
public void givenRunningServer_whenConnectToSocket_thenHandleConnectionResetException() throws IOException {
// Enable multiple SSL/TLS protocols
String[] enabledProtocols = { "TLSv1.2", "TLSv1.3", "TLSv1.1", "TLSv1", "SSLv3", "SSLv3" };
SSLServerSocketFactory mockFactory = mock(SSLServerSocketFactory.class);
SSLServerSocket mockServerSocket = mock(SSLServerSocket.class);
// Set up the mock factory to return the mock server socket
when(mockFactory.createServerSocket(PORT)).thenReturn(mockServerSocket);
// Call the method being tested
SslServer.createSSLSocketWithEnabledProtocols(mockFactory, PORT, enabledProtocols);
// Verify that setEnabledProtocols and close were called
verify(mockServerSocket).setEnabledProtocols(enabledProtocols);
verify(mockServerSocket).close();
}
}