Skip to content

Commit b26515b

Browse files
committed
Additional tests
For WebSocketFactory and Compression Extension
1 parent d9ec613 commit b26515b

File tree

6 files changed

+514
-1
lines changed

6 files changed

+514
-1
lines changed

src/test/java/org/java_websocket/extensions/AllExtensionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
@RunWith(Suite.class)
3232
@Suite.SuiteClasses({
33-
org.java_websocket.extensions.DefaultExtensionTest.class
33+
org.java_websocket.extensions.DefaultExtensionTest.class,
34+
org.java_websocket.extensions.CompressionExtensionTest.class
3435
})
3536
/**
3637
* Start all tests for extensuins
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.java_websocket.extensions;
2+
3+
import org.java_websocket.framing.PingFrame;
4+
import org.java_websocket.framing.TextFrame;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.fail;
8+
9+
public class CompressionExtensionTest {
10+
11+
12+
@Test
13+
public void testIsFrameValid() {
14+
CustomCompressionExtension customCompressionExtension = new CustomCompressionExtension();
15+
TextFrame textFrame = new TextFrame();
16+
try {
17+
customCompressionExtension.isFrameValid( textFrame );
18+
} catch ( Exception e ) {
19+
fail( "This frame is valid" );
20+
}
21+
textFrame.setRSV1( true );
22+
try {
23+
customCompressionExtension.isFrameValid( textFrame );
24+
} catch ( Exception e ) {
25+
fail( "This frame is valid" );
26+
}
27+
textFrame.setRSV1( false );
28+
textFrame.setRSV2( true );
29+
try {
30+
customCompressionExtension.isFrameValid( textFrame );
31+
fail( "This frame is not valid" );
32+
} catch ( Exception e ) {
33+
//
34+
}
35+
textFrame.setRSV2( false );
36+
textFrame.setRSV3( true );
37+
try {
38+
customCompressionExtension.isFrameValid( textFrame );
39+
fail( "This frame is not valid" );
40+
} catch ( Exception e ) {
41+
//
42+
}
43+
PingFrame pingFrame = new PingFrame();
44+
try {
45+
customCompressionExtension.isFrameValid( pingFrame );
46+
} catch ( Exception e ) {
47+
fail( "This frame is valid" );
48+
}
49+
pingFrame.setRSV1( true );
50+
try {
51+
customCompressionExtension.isFrameValid( pingFrame );
52+
fail( "This frame is not valid" );
53+
} catch ( Exception e ) {
54+
//
55+
}
56+
pingFrame.setRSV1( false );
57+
pingFrame.setRSV2( true );
58+
try {
59+
customCompressionExtension.isFrameValid( pingFrame );
60+
fail( "This frame is not valid" );
61+
} catch ( Exception e ) {
62+
//
63+
}
64+
pingFrame.setRSV2( false );
65+
pingFrame.setRSV3( true );
66+
try {
67+
customCompressionExtension.isFrameValid( pingFrame );
68+
fail( "This frame is not valid" );
69+
} catch ( Exception e ) {
70+
//
71+
}
72+
}
73+
74+
private static class CustomCompressionExtension extends CompressionExtension {
75+
}
76+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2010-2018 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.server;
27+
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.Suite;
30+
31+
32+
@RunWith(Suite.class)
33+
@Suite.SuiteClasses({
34+
org.java_websocket.server.DefaultWebSocketServerFactoryTest.class,
35+
org.java_websocket.protocols.ProtoclHandshakeRejectionTest.class
36+
})
37+
/**
38+
* Start all tests for the server
39+
*/
40+
public class AllServerTests {
41+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package org.java_websocket.server;
2+
3+
import org.java_websocket.WebSocket;
4+
import org.java_websocket.WebSocketAdapter;
5+
import org.java_websocket.WebSocketImpl;
6+
import org.java_websocket.drafts.Draft;
7+
import org.java_websocket.drafts.Draft_6455;
8+
import org.java_websocket.handshake.Handshakedata;
9+
import org.junit.Test;
10+
11+
import javax.net.ssl.SSLContext;
12+
import java.io.IOException;
13+
import java.net.InetSocketAddress;
14+
import java.nio.ByteBuffer;
15+
import java.nio.channels.ByteChannel;
16+
import java.nio.channels.NotYetConnectedException;
17+
import java.nio.channels.SocketChannel;
18+
import java.security.NoSuchAlgorithmException;
19+
import java.util.Collections;
20+
import java.util.concurrent.Executors;
21+
22+
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.fail;
24+
25+
public class CustomSSLWebSocketServerFactoryTest {
26+
27+
final String[] emptyArray = new String[0];
28+
@Test
29+
public void testConstructor() throws NoSuchAlgorithmException {
30+
try {
31+
new CustomSSLWebSocketServerFactory(null, null, null);
32+
fail("IllegalArgumentException should be thrown");
33+
} catch (IllegalArgumentException e) {
34+
// Good
35+
}
36+
try {
37+
new CustomSSLWebSocketServerFactory(null, null, null, null);
38+
fail("IllegalArgumentException should be thrown");
39+
} catch (IllegalArgumentException e) {
40+
// Good
41+
}
42+
try {
43+
new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null, null);
44+
fail("IllegalArgumentException should be thrown");
45+
} catch (IllegalArgumentException e) {
46+
}
47+
try {
48+
new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null);
49+
} catch (IllegalArgumentException e) {
50+
fail("IllegalArgumentException should not be thrown");
51+
}
52+
try {
53+
new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), null, null);
54+
} catch (IllegalArgumentException e) {
55+
fail("IllegalArgumentException should not be thrown");
56+
}
57+
try {
58+
new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), emptyArray, null);
59+
} catch (IllegalArgumentException e) {
60+
fail("IllegalArgumentException should not be thrown");
61+
}
62+
try {
63+
new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), null, emptyArray);
64+
} catch (IllegalArgumentException e) {
65+
fail("IllegalArgumentException should not be thrown");
66+
}
67+
try {
68+
new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), emptyArray, emptyArray);
69+
} catch (IllegalArgumentException e) {
70+
fail("IllegalArgumentException should not be thrown");
71+
}
72+
}
73+
@Test
74+
public void testCreateWebSocket() throws NoSuchAlgorithmException {
75+
CustomSSLWebSocketServerFactory webSocketServerFactory = new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null);
76+
CustomWebSocketAdapter webSocketAdapter = new CustomWebSocketAdapter();
77+
WebSocketImpl webSocketImpl = webSocketServerFactory.createWebSocket(webSocketAdapter, new Draft_6455());
78+
assertNotNull("webSocketImpl != null", webSocketImpl);
79+
webSocketImpl = webSocketServerFactory.createWebSocket(webSocketAdapter, Collections.<Draft>singletonList(new Draft_6455()));
80+
assertNotNull("webSocketImpl != null", webSocketImpl);
81+
}
82+
83+
@Test
84+
public void testWrapChannel() throws IOException, NoSuchAlgorithmException {
85+
CustomSSLWebSocketServerFactory webSocketServerFactory = new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null);
86+
SocketChannel channel = SocketChannel.open();
87+
try {
88+
ByteChannel result = webSocketServerFactory.wrapChannel(channel, null);
89+
} catch (NotYetConnectedException e) {
90+
//We do not really connect
91+
}
92+
channel.close();
93+
webSocketServerFactory = new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), new String[]{"TLSv1.2"}, new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA"});
94+
channel = SocketChannel.open();
95+
try {
96+
ByteChannel result = webSocketServerFactory.wrapChannel(channel, null);
97+
} catch (NotYetConnectedException e) {
98+
//We do not really connect
99+
}
100+
channel.close();
101+
}
102+
103+
@Test
104+
public void testClose() {
105+
DefaultWebSocketServerFactory webSocketServerFactory = new DefaultWebSocketServerFactory();
106+
webSocketServerFactory.close();
107+
}
108+
109+
private static class CustomWebSocketAdapter extends WebSocketAdapter {
110+
@Override
111+
public void onWebsocketMessage(WebSocket conn, String message) {
112+
113+
}
114+
115+
@Override
116+
public void onWebsocketMessage(WebSocket conn, ByteBuffer blob) {
117+
118+
}
119+
120+
@Override
121+
public void onWebsocketOpen(WebSocket conn, Handshakedata d) {
122+
123+
}
124+
125+
@Override
126+
public void onWebsocketClose(WebSocket ws, int code, String reason, boolean remote) {
127+
128+
}
129+
130+
@Override
131+
public void onWebsocketClosing(WebSocket ws, int code, String reason, boolean remote) {
132+
133+
}
134+
135+
@Override
136+
public void onWebsocketCloseInitiated(WebSocket ws, int code, String reason) {
137+
138+
}
139+
140+
@Override
141+
public void onWebsocketError(WebSocket conn, Exception ex) {
142+
143+
}
144+
145+
@Override
146+
public void onWriteDemand(WebSocket conn) {
147+
148+
}
149+
150+
@Override
151+
public InetSocketAddress getLocalSocketAddress(WebSocket conn) {
152+
return null;
153+
}
154+
155+
@Override
156+
public InetSocketAddress getRemoteSocketAddress(WebSocket conn) {
157+
return null;
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)