From 8b38d4481b3e104189f6b388cb7de6c615c19b89 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Fri, 22 Jan 2021 22:23:02 +0100 Subject: [PATCH] Test SOCKS4 client: connect with faulty Socket --- .../junit/net/i2p/socks/SOCKS4ClientTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/java/test/junit/net/i2p/socks/SOCKS4ClientTest.java b/core/java/test/junit/net/i2p/socks/SOCKS4ClientTest.java index f2211ac674..a7daa52f8c 100644 --- a/core/java/test/junit/net/i2p/socks/SOCKS4ClientTest.java +++ b/core/java/test/junit/net/i2p/socks/SOCKS4ClientTest.java @@ -109,6 +109,28 @@ public class SOCKS4ClientTest { 80); }); } + + /** + * Run into IOException while trying to connect due to closed input stream + */ + @Test + public void connect__ioExceptionWithSocket() { + assertThrows(IOException.class, () -> { + // Create an IOException by closing input stream before it can used + ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[]{}); + inputStream.close(); + + Socket socket = Mockito.mock(Socket.class); + Mockito.when(socket.getInputStream()).thenReturn(inputStream); + Mockito.when(socket.getOutputStream()).thenReturn(new ByteArrayOutputStream()); + + SOCKS4Client.connect( + socket, + "127.0.0.1", + 80 + ); + }); + } /** * Check that CONNECTION_REFUSED throws exception */