Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/test/java/org/java_websocket/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions src/test/java/org/java_websocket/SSLSocketChannel2Test.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
97 changes: 97 additions & 0 deletions src/test/java/org/java_websocket/SSLSocketChannelTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/java_websocket/client/AllClientTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
177 changes: 177 additions & 0 deletions src/test/java/org/java_websocket/client/WebSocketClientTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> httpHeaders = new HashMap<String, String>();
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<String, String> httpHeaders = new HashMap<String, String>();
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<String, String> httpHeaders) {
super(serverUri, httpHeaders);
}

public MyWebSocketClient(URI serverUri, Draft protocolDraft, Map<String, String> 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) {

}
}
}
Loading