2525
2626package org .java_websocket .misc ;
2727
28- import org .java_websocket .WebSocketImpl ;
2928import org .java_websocket .client .WebSocketClient ;
3029import org .java_websocket .framing .CloseFrame ;
3130import org .java_websocket .handshake .ServerHandshake ;
4544
4645public class OpeningHandshakeRejectionTest {
4746
48- private static final int testCases = 12 ;
4947 private static final String additionalHandshake = "Upgrade: websocket\r \n Connection: 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}
0 commit comments