diff --git a/src/test/java/org/java_websocket/AllTests.java b/src/test/java/org/java_websocket/AllTests.java index 9fae8d634..dd41e39ef 100644 --- a/src/test/java/org/java_websocket/AllTests.java +++ b/src/test/java/org/java_websocket/AllTests.java @@ -39,7 +39,9 @@ org.java_websocket.exceptions.AllExceptionsTests.class, org.java_websocket.misc.AllMiscTests.class, org.java_websocket.protocols.AllProtocolTests.class, - org.java_websocket.framing.AllFramingTests.class + org.java_websocket.framing.AllFramingTests.class, + org.java_websocket.SSLSocketChannelTest.class, + org.java_websocket.SSLSocketChannel2Test.class }) /** * Start all tests diff --git a/src/test/java/org/java_websocket/SSLSocketChannel2Test.java b/src/test/java/org/java_websocket/SSLSocketChannel2Test.java new file mode 100644 index 000000000..1403686d8 --- /dev/null +++ b/src/test/java/org/java_websocket/SSLSocketChannel2Test.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2010-2020 Nathan Rajlich + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.java_websocket; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLParameters; +import java.io.IOException; +import java.nio.channels.NotYetConnectedException; +import java.nio.channels.SocketChannel; +import java.security.NoSuchAlgorithmException; +import java.util.concurrent.Executors; + +import org.junit.Test; +import static org.junit.Assert.fail; + +public class SSLSocketChannel2Test { + + @Test + public void testConstructor() throws IOException, NoSuchAlgorithmException { + + SocketChannel inputSocketChannel = SocketChannel.open(); + SSLContext sslContext = SSLContext.getDefault(); + SSLEngine engine = sslContext.createSSLEngine(); + + try { + new SSLSocketChannel2(null, null, null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new SSLSocketChannel2(inputSocketChannel, null, null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + inputSocketChannel.close(); + } + + try { + inputSocketChannel = SocketChannel.open(); + new SSLSocketChannel2(inputSocketChannel, engine, null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + inputSocketChannel.close(); + } + + try { + inputSocketChannel = SocketChannel.open(); + new SSLSocketChannel2(inputSocketChannel, null, Executors.newSingleThreadScheduledExecutor(), null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + inputSocketChannel.close(); + } + + try { + new SSLSocketChannel2(null, engine, Executors.newSingleThreadScheduledExecutor(), null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + + try { + inputSocketChannel = SocketChannel.open(); + engine.setUseClientMode(true); + engine.setSSLParameters(new SSLParameters()); + new SSLSocketChannel2(inputSocketChannel, engine, Executors.newSingleThreadScheduledExecutor(), null); + fail("NotYetConnectedException should be thrown"); + } catch (NotYetConnectedException e) { + inputSocketChannel.close(); + } + } +} diff --git a/src/test/java/org/java_websocket/SSLSocketChannelTest.java b/src/test/java/org/java_websocket/SSLSocketChannelTest.java new file mode 100644 index 000000000..55391b9bd --- /dev/null +++ b/src/test/java/org/java_websocket/SSLSocketChannelTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2010-2020 Nathan Rajlich + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.java_websocket; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLParameters; +import java.io.IOException; +import java.nio.channels.NotYetConnectedException; +import java.nio.channels.SocketChannel; +import java.security.NoSuchAlgorithmException; +import java.util.concurrent.Executors; + +import org.junit.Test; +import static org.junit.Assert.fail; + +public class SSLSocketChannelTest { + + @Test + public void testConstructor() throws IOException, NoSuchAlgorithmException { + + SocketChannel inputSocketChannel = SocketChannel.open(); + SSLContext sslContext = SSLContext.getDefault(); + SSLEngine engine = sslContext.createSSLEngine(); + + try { + new SSLSocketChannel(null, null, null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new SSLSocketChannel(inputSocketChannel, null, null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + inputSocketChannel.close(); + } + + try { + inputSocketChannel = SocketChannel.open(); + new SSLSocketChannel(inputSocketChannel, engine, null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + inputSocketChannel.close(); + } + + try { + inputSocketChannel = SocketChannel.open(); + new SSLSocketChannel(inputSocketChannel, null, Executors.newSingleThreadScheduledExecutor(), null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + inputSocketChannel.close(); + } + + try { + new SSLSocketChannel(null, engine, Executors.newSingleThreadScheduledExecutor(), null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + + try { + inputSocketChannel = SocketChannel.open(); + engine.setUseClientMode(true); + engine.setSSLParameters(new SSLParameters()); + new SSLSocketChannel(inputSocketChannel, engine, Executors.newSingleThreadScheduledExecutor(), null); + fail("NotYetConnectedException should be thrown"); + } catch (NotYetConnectedException e) { + inputSocketChannel.close(); + } + } +} diff --git a/src/test/java/org/java_websocket/client/AllClientTests.java b/src/test/java/org/java_websocket/client/AllClientTests.java index 9be07eefe..0bcca7c82 100644 --- a/src/test/java/org/java_websocket/client/AllClientTests.java +++ b/src/test/java/org/java_websocket/client/AllClientTests.java @@ -33,7 +33,8 @@ @Suite.SuiteClasses({ org.java_websocket.client.AttachmentTest.class, org.java_websocket.client.SchemaCheckTest.class, - org.java_websocket.client.HeadersTest.class + org.java_websocket.client.HeadersTest.class, + org.java_websocket.client.WebSocketClientTest.class }) /** * Start all tests for the client diff --git a/src/test/java/org/java_websocket/client/WebSocketClientTest.java b/src/test/java/org/java_websocket/client/WebSocketClientTest.java new file mode 100644 index 000000000..765d8ce68 --- /dev/null +++ b/src/test/java/org/java_websocket/client/WebSocketClientTest.java @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2010-2020 Nathan Rajlich + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.java_websocket.client; + +import org.java_websocket.WebSocketImpl; +import org.java_websocket.drafts.Draft; +import org.java_websocket.drafts.Draft_6455; +import org.java_websocket.exceptions.WebsocketNotConnectedException; +import org.java_websocket.handshake.ServerHandshake; +import org.junit.Test; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +public class WebSocketClientTest { + + @Test + public void testMyWebSocketClientConstructor() throws URISyntaxException { + Draft draft = new Draft_6455(); + URI uri = new URI("ws://localhost"); + Map httpHeaders = new HashMap(); + httpHeaders.put("Cache-Control", "only-if-cached"); + httpHeaders.put("Keep-Alive", "1000"); + + try { + new MyWebSocketClient(null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new MyWebSocketClient(uri); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should be thrown"); + } + + try { + new MyWebSocketClient(uri, null); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should be thrown"); + } + + try { + new MyWebSocketClient(null, httpHeaders); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new MyWebSocketClient(uri, httpHeaders); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should be thrown"); + } + + try { + new MyWebSocketClient(null, httpHeaders); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new MyWebSocketClient(null, null, null); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new MyWebSocketClient(null, draft, httpHeaders); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new MyWebSocketClient(uri, null, httpHeaders); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + + } + + try { + new MyWebSocketClient(uri, draft, null); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should be thrown"); + } + + try { + MyWebSocketClient client = new MyWebSocketClient(uri, draft, httpHeaders); + assertEquals(draft, client.getDraft()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should be thrown"); + } + } + + @Test + public void testGetLocalSocketAddress() throws URISyntaxException { + Draft draft = new Draft_6455(); + URI uri = new URI("ws://localhost"); + Map httpHeaders = new HashMap(); + httpHeaders.put("Cache-Control", "only-if-cached"); + httpHeaders.put("Keep-Alive", "1000"); + + MyWebSocketClient client = new MyWebSocketClient(uri, draft, httpHeaders); + assertEquals(draft, client.getDraft()); + assertNotNull(client.getConnection()); + assertNull(client.getLocalSocketAddress()); + assertNull(client.getLocalSocketAddress(client.getConnection())); + assertNull(client.getRemoteSocketAddress()); + assertNull(client.getRemoteSocketAddress(client.getConnection())); + } + + private static class MyWebSocketClient extends WebSocketClient { + + public MyWebSocketClient(URI serverUri) { + super(serverUri); + } + + public MyWebSocketClient(URI serverUri, Map httpHeaders) { + super(serverUri, httpHeaders); + } + + public MyWebSocketClient(URI serverUri, Draft protocolDraft, Map httpHeaders) { + super(serverUri, protocolDraft, httpHeaders); + } + + @Override + public void onOpen(ServerHandshake handshakedata) { + + } + + @Override + public void onMessage(String message) { + + } + + @Override + public void onClose(int code, String reason, boolean remote) { + + } + + @Override + public void onError(Exception ex) { + + } + } +} diff --git a/src/test/java/org/java_websocket/drafts/Draft_6455Test.java b/src/test/java/org/java_websocket/drafts/Draft_6455Test.java index 1f8959767..7e784dd8e 100644 --- a/src/test/java/org/java_websocket/drafts/Draft_6455Test.java +++ b/src/test/java/org/java_websocket/drafts/Draft_6455Test.java @@ -112,6 +112,7 @@ public void testConstructor() throws Exception { Draft_6455 draft_6455 = new Draft_6455( Collections.emptyList(), Collections.emptyList() ); assertEquals( 1, draft_6455.getKnownExtensions().size() ); assertEquals( 0, draft_6455.getKnownProtocols().size() ); + assertEquals(Integer.MAX_VALUE, draft_6455.getMaxFrameSize()); } @Test @@ -137,10 +138,23 @@ public void testGetProtocol() throws Exception { assertNull( draft_6455.getProtocol() ); draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ); assertNull( draft_6455.getProtocol() ); + + handshakedataProtocolExtension.put("Sec-WebSocket-Version", "12"); + draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ); + assertNull( draft_6455.getProtocol() ); + + handshakedataProtocolExtension.put("Sec-WebSocket-Version", "13"); draft_6455 = new Draft_6455( Collections.emptyList(), Collections.singletonList( new Protocol( "chat" ) ) ); assertNull( draft_6455.getProtocol() ); draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ); assertNotNull( draft_6455.getProtocol() ); + + handshakedataProtocolExtension.put("Sec-WebSocket-Protocol", "test"); + draft_6455 = new Draft_6455( Collections.emptyList(), Collections.singletonList( new Protocol( "chat" ) ) ); + assertNull( draft_6455.getProtocol() ); + draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ); + assertNull( draft_6455.getProtocol() ); + handshakedataProtocolExtension.put("Sec-WebSocket-Protocol", "chat, test"); } @Test diff --git a/src/test/java/org/java_websocket/server/WebSocketServerTest.java b/src/test/java/org/java_websocket/server/WebSocketServerTest.java index d7ab804f6..2b4060ea0 100644 --- a/src/test/java/org/java_websocket/server/WebSocketServerTest.java +++ b/src/test/java/org/java_websocket/server/WebSocketServerTest.java @@ -102,6 +102,14 @@ public void testConstructor() { } catch (IllegalArgumentException e) { fail("Should not fail"); } + + WebSocketServer server = new MyWebSocketServer(inetAddress, 1); + assertEquals(0, server.getDraft().size()); + assertEquals(0, server.getConnections().size()); + assertEquals(-1, server.getMaxPendingConnections()); + + server.setMaxPendingConnections(1); + assertEquals(1, server.getMaxPendingConnections()); } @@ -184,7 +192,10 @@ public void testBroadcast() { } private static class MyWebSocketServer extends WebSocketServer { private CountDownLatch serverLatch = null; - + + public MyWebSocketServer(InetSocketAddress address , int decodercount) { + super(address, decodercount); + } public MyWebSocketServer(InetSocketAddress address , int decodercount , List drafts , Collection connectionscontainer) { super(address, decodercount, drafts, connectionscontainer); }