Skip to content

Commit 2f0d694

Browse files
committed
Tests for Protocols
TooTallNate#612
1 parent bc865e9 commit 2f0d694

5 files changed

Lines changed: 577 additions & 66 deletions

File tree

‎src/test/java/org/java_websocket/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
@Suite.SuiteClasses({
3434
org.java_websocket.util.ByteBufferUtilsTest.class,
3535
org.java_websocket.misc.AllMiscTests.class,
36+
org.java_websocket.protocols.AllProtocolTests.class,
3637
org.java_websocket.framing.AllFramingTests.class
3738
})
3839
/**

‎src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java‎

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
package org.java_websocket.misc;
2727

28-
import org.java_websocket.WebSocketImpl;
2928
import org.java_websocket.client.WebSocketClient;
3029
import org.java_websocket.framing.CloseFrame;
3130
import org.java_websocket.handshake.ServerHandshake;
@@ -45,10 +44,10 @@
4544

4645
public class OpeningHandshakeRejectionTest {
4746

48-
private static final int testCases = 12;
4947
private static final String additionalHandshake = "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n";
5048
private static int counter = 0;
5149
private static Thread thread;
50+
private static ServerSocket serverSocket;
5251

5352
private static boolean debugPrintouts = false;
5453

@@ -58,17 +57,16 @@ public static void startServer() {
5857
new Runnable() {
5958
public void run() {
6059
try {
61-
ServerSocket server = new ServerSocket( 8887 );
62-
int count = 1;
60+
serverSocket = new ServerSocket( 8887 );
61+
serverSocket.setReuseAddress( true );
6362
while( true ) {
6463
Socket client = null;
6564
try {
66-
client = server.accept();
65+
client = serverSocket.accept();
6766
Scanner in = new Scanner( client.getInputStream() );
6867
String input = in.nextLine();
6968
String testCase = input.split( " " )[1];
7069
OutputStream os = client.getOutputStream();
71-
count++;
7270
if( "/0".equals( testCase ) ) {
7371
os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 100 Switching Protocols\r\n" + additionalHandshake ) );
7472
os.flush();
@@ -118,7 +116,7 @@ public void run() {
118116
os.flush();
119117
}
120118
} catch ( IOException e ) {
121-
fail( "There should be no exception" );
119+
//
122120
}
123121
}
124122
} catch ( Exception e ) {
@@ -130,119 +128,124 @@ public void run() {
130128
}
131129

132130
@AfterClass
133-
public static void successTests() throws InterruptedException {
131+
public static void successTests() throws InterruptedException, IOException {
132+
serverSocket.close();
134133
thread.interrupt();
135-
if (debugPrintouts)
136-
System.out.println( counter + " successful tests" );
134+
if( debugPrintouts )
135+
System.out.println( counter + " successful tests" );
137136
}
138137

139138
@Test(timeout = 5000)
140-
public void testHandshakeRejectionTestCase0() {
139+
public void testHandshakeRejectionTestCase0() throws Exception {
141140
testHandshakeRejection( 0 );
142141
}
143142

144143
@Test(timeout = 5000)
145-
public void testHandshakeRejectionTestCase1() {
144+
public void testHandshakeRejectionTestCase1() throws Exception {
146145
testHandshakeRejection( 1 );
147146
}
148147

149148
@Test(timeout = 5000)
150-
public void testHandshakeRejectionTestCase2() {
149+
public void testHandshakeRejectionTestCase2() throws Exception {
151150
testHandshakeRejection( 2 );
152151
}
153152

154153
@Test(timeout = 5000)
155-
public void testHandshakeRejectionTestCase3() {
154+
public void testHandshakeRejectionTestCase3() throws Exception {
156155
testHandshakeRejection( 3 );
157156
}
158157

159158
@Test(timeout = 5000)
160-
public void testHandshakeRejectionTestCase4() {
159+
public void testHandshakeRejectionTestCase4() throws Exception {
161160
testHandshakeRejection( 4 );
162161
}
163162

164163
@Test(timeout = 5000)
165-
public void testHandshakeRejectionTestCase5() {
164+
public void testHandshakeRejectionTestCase5() throws Exception {
166165
testHandshakeRejection( 5 );
167166
}
168167

169168
@Test(timeout = 5000)
170-
public void testHandshakeRejectionTestCase6() {
169+
public void testHandshakeRejectionTestCase6() throws Exception {
171170
testHandshakeRejection( 6 );
172171
}
173172

174173
@Test(timeout = 5000)
175-
public void testHandshakeRejectionTestCase7() {
174+
public void testHandshakeRejectionTestCase7() throws Exception {
176175
testHandshakeRejection( 7 );
177176
}
178177

179178
@Test(timeout = 5000)
180-
public void testHandshakeRejectionTestCase8() {
179+
public void testHandshakeRejectionTestCase8() throws Exception {
181180
testHandshakeRejection( 8 );
182181
}
183182

184183
@Test(timeout = 5000)
185-
public void testHandshakeRejectionTestCase9() {
184+
public void testHandshakeRejectionTestCase9() throws Exception {
186185
testHandshakeRejection( 9 );
187186
}
188187

189188
@Test(timeout = 5000)
190-
public void testHandshakeRejectionTestCase10() {
189+
public void testHandshakeRejectionTestCase10() throws Exception {
191190
testHandshakeRejection( 10 );
192191
}
193192

194193
@Test(timeout = 5000)
195-
public void testHandshakeRejectionTestCase11() {
194+
public void testHandshakeRejectionTestCase11() throws Exception {
196195
testHandshakeRejection( 11 );
197196
}
198197

199-
private void testHandshakeRejection( int i ) {
200-
try {
201-
final int finalI = i;
202-
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + finalI ) ) {
203-
@Override
204-
public void onOpen( ServerHandshake handshakedata ) {
205-
fail( "There should not be a connection!" );
206-
}
207-
208-
@Override
209-
public void onMessage( String message ) {
210-
fail( "There should not be a message!" );
211-
}
212-
213-
@Override
214-
public void onClose( int code, String reason, boolean remote ) {
215-
if( finalI != 10 && finalI != 11 ) {
216-
if( code != CloseFrame.PROTOCOL_ERROR ) {
217-
fail( "There should be a protocol error!" );
218-
} else if (reason.startsWith( "Invalid status code received:") || reason.startsWith( "Invalid status line received:")) {
219-
if (debugPrintouts)
220-
System.out.println("Protocol error for test case: " + finalI);
221-
counter++;
222-
} else {
223-
fail( "The reason should be included!" );
224-
}
198+
private void testHandshakeRejection( int i ) throws Exception {
199+
final int finalI = i;
200+
final boolean[] threadReturned = { false };
201+
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + finalI ) ) {
202+
@Override
203+
public void onOpen( ServerHandshake handshakedata ) {
204+
fail( "There should not be a connection!" );
205+
}
206+
207+
@Override
208+
public void onMessage( String message ) {
209+
fail( "There should not be a message!" );
210+
}
211+
212+
@Override
213+
public void onClose( int code, String reason, boolean remote ) {
214+
if( finalI != 10 && finalI != 11 ) {
215+
if( code != CloseFrame.PROTOCOL_ERROR ) {
216+
fail( "There should be a protocol error!" );
217+
} else if( reason.startsWith( "Invalid status code received:" ) || reason.startsWith( "Invalid status line received:" ) ) {
218+
if( debugPrintouts )
219+
System.out.println( "Protocol error for test case: " + finalI );
220+
threadReturned[0] = true;
221+
counter++;
225222
} else {
226-
//Since we do not include a correct Sec-WebSocket-Accept, onClose will be called with reason 'Draft refuses handshake'
227-
if (!reason.endsWith( "refuses handshake" )) {
228-
fail( "onClose should not be called!" );
229-
} else {
230-
if (debugPrintouts)
231-
System.out.println("Refuses handshake error for test case: " + finalI);
232-
counter++;
233-
}
223+
fail( "The reason should be included!" );
224+
}
225+
} else {
226+
//Since we do not include a correct Sec-WebSocket-Accept, onClose will be called with reason 'Draft refuses handshake'
227+
if( !reason.endsWith( "refuses handshake" ) ) {
228+
fail( "onClose should not be called!" );
229+
} else {
230+
if( debugPrintouts )
231+
System.out.println( "Refuses handshake error for test case: " + finalI );
232+
counter++;
233+
threadReturned[0] = true;
234234
}
235235
}
236-
237-
@Override
238-
public void onError( Exception ex ) {
239-
fail( "There should not be an exception" );
240-
}
241-
};
242-
webSocketClient.connectBlocking();
243-
} catch ( Exception e ) {
244-
e.printStackTrace();
245-
fail( "There should be no exception" );
236+
}
237+
238+
@Override
239+
public void onError( Exception ex ) {
240+
fail( "There should not be an exception" );
241+
}
242+
};
243+
Thread finalThread = new Thread( webSocketClient );
244+
finalThread.start();
245+
finalThread.join();
246+
247+
if( !threadReturned[0] ) {
248+
fail( "Error" );
246249
}
247250
}
248251
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2010-2017 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.protocols;
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.protocols.ProtocolTest.class,
35+
org.java_websocket.protocols.ProtoclHandshakeRejectionTest.class
36+
})
37+
/**
38+
* Start all tests for protocols
39+
*/
40+
public class AllProtocolTests {
41+
}

0 commit comments

Comments
 (0)