2525
2626package org .java_websocket .misc ;
2727
28+ import org .java_websocket .WebSocketImpl ;
2829import org .java_websocket .client .WebSocketClient ;
2930import org .java_websocket .framing .CloseFrame ;
3031import org .java_websocket .handshake .ServerHandshake ;
3132import org .java_websocket .util .Charsetfunctions ;
33+ import org .junit .AfterClass ;
34+ import org .junit .BeforeClass ;
3235import org .junit .Test ;
3336
3437import java .io .IOException ;
3740import java .net .Socket ;
3841import java .net .URI ;
3942import java .util .Scanner ;
40- import java .util .concurrent .CountDownLatch ;
4143
4244import static org .junit .Assert .fail ;
4345
4446public class OpeningHandshakeRejectionTest {
4547
46- /**
47- * How many testcases do we have
48- */
49- private static final int testCases = 10 ;
48+ private static final int testCases = 12 ;
49+ private static final String additionalHandshake = "Upgrade: websocket \r \n Connection: Upgrade \r \n \r \n " ;
50+ private static int counter = 0 ;
51+ private static Thread thread ;
5052
51- public OpeningHandshakeRejectionTest () {
52- Thread thread = new Thread (
53+ private static boolean debugPrintouts = false ;
54+
55+ @ BeforeClass
56+ public static void startServer () {
57+ thread = new Thread (
5358 new Runnable () {
5459 public void run () {
5560 try {
5661 ServerSocket server = new ServerSocket ( 8887 );
62+ int count = 1 ;
5763 while ( true ) {
5864 Socket client = null ;
5965 try {
@@ -62,96 +68,180 @@ public void run() {
6268 String input = in .nextLine ();
6369 String testCase = input .split ( " " )[1 ];
6470 OutputStream os = client .getOutputStream ();
71+ count ++;
6572 if ( "/0" .equals ( testCase ) ) {
66- os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 100 Switching Protocols\r \n " ) );
73+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 100 Switching Protocols\r \n " + additionalHandshake ) );
6774 os .flush ();
6875 }
6976 if ( "/1" .equals ( testCase ) ) {
70- os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.0 100 Switching Protocols\r \n " ) );
77+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.0 100 Switching Protocols\r \n " + additionalHandshake ) );
7178 os .flush ();
7279 }
7380 if ( "/2" .equals ( testCase ) ) {
74- os .write ( Charsetfunctions .asciiBytes ( "HTTP 100 Switching Protocols\r \n " ) );
81+ os .write ( Charsetfunctions .asciiBytes ( "HTTP 100 Switching Protocols\r \n " + additionalHandshake ) );
7582 os .flush ();
7683 }
7784 if ( "/3" .equals ( testCase ) ) {
78- os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 200 Switching Protocols\r \n " ) );
85+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 200 Switching Protocols\r \n " + additionalHandshake ) );
7986 os .flush ();
8087 }
8188 if ( "/4" .equals ( testCase ) ) {
82- os .write ( Charsetfunctions .asciiBytes ( "HTTP 101 Switching Protocols\r \n " ) );
89+ os .write ( Charsetfunctions .asciiBytes ( "HTTP 101 Switching Protocols\r \n " + additionalHandshake ) );
8390 os .flush ();
8491 }
8592 if ( "/5" .equals ( testCase ) ) {
86- os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 404 Switching Protocols\r \n " ) );
93+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 404 Switching Protocols\r \n " + additionalHandshake ) );
8794 os .flush ();
8895 }
8996 if ( "/6" .equals ( testCase ) ) {
90- os .write ( Charsetfunctions .asciiBytes ( "HTTP/2.0 404 Switching Protocols\r \n " ) );
97+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/2.0 404 Switching Protocols\r \n " + additionalHandshake ) );
9198 os .flush ();
9299 }
93100 if ( "/7" .equals ( testCase ) ) {
94- os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 500 Switching Protocols\r \n " ) );
101+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 500 Switching Protocols\r \n " + additionalHandshake ) );
95102 os .flush ();
96103 }
97104 if ( "/8" .equals ( testCase ) ) {
98- os .write ( Charsetfunctions .asciiBytes ( "GET 302 Switching Protocols\r \n " ) );
105+ os .write ( Charsetfunctions .asciiBytes ( "GET 302 Switching Protocols\r \n " + additionalHandshake ) );
99106 os .flush ();
100107 }
101108 if ( "/9" .equals ( testCase ) ) {
102- os .write ( Charsetfunctions .asciiBytes ( "GET HTTP/1.1 101 Switching Protocols\r \n " ) );
109+ os .write ( Charsetfunctions .asciiBytes ( "GET HTTP/1.1 101 Switching Protocols\r \n " + additionalHandshake ) );
110+ os .flush ();
111+ }
112+ if ( "/10" .equals ( testCase ) ) {
113+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 101 Switching Protocols\r \n " + additionalHandshake ) );
114+ os .flush ();
115+ }
116+ if ( "/11" .equals ( testCase ) ) {
117+ os .write ( Charsetfunctions .asciiBytes ( "HTTP/1.1 101 Websocket Connection Upgrade\r \n " + additionalHandshake ) );
103118 os .flush ();
104119 }
105- client .close ();
106120 } catch ( IOException e ) {
107121 fail ( "There should be no exception" );
108- } finally {
109- if ( client != null )
110- client .close ();
111122 }
112123 }
113124 } catch ( Exception e ) {
114- e .printStackTrace ();
115125 fail ( "There should be no exception" );
116126 }
117127 }
118128 } );
119129 thread .start ();
120130 }
121131
122- @ Test (timeout =10000 )
123- public void testClient () {
132+ @ AfterClass
133+ public static void successTests () throws InterruptedException {
134+ thread .interrupt ();
135+ if (debugPrintouts )
136+ System .out .println ( counter + " successful tests" );
137+ }
138+
139+ @ Test (timeout = 5000 )
140+ public void testHandshakeRejectionTestCase0 () {
141+ testHandshakeRejection ( 0 );
142+ }
143+
144+ @ Test (timeout = 5000 )
145+ public void testHandshakeRejectionTestCase1 () {
146+ testHandshakeRejection ( 1 );
147+ }
148+
149+ @ Test (timeout = 5000 )
150+ public void testHandshakeRejectionTestCase2 () {
151+ testHandshakeRejection ( 2 );
152+ }
153+
154+ @ Test (timeout = 5000 )
155+ public void testHandshakeRejectionTestCase3 () {
156+ testHandshakeRejection ( 3 );
157+ }
158+
159+ @ Test (timeout = 5000 )
160+ public void testHandshakeRejectionTestCase4 () {
161+ testHandshakeRejection ( 4 );
162+ }
163+
164+ @ Test (timeout = 5000 )
165+ public void testHandshakeRejectionTestCase5 () {
166+ testHandshakeRejection ( 5 );
167+ }
168+
169+ @ Test (timeout = 5000 )
170+ public void testHandshakeRejectionTestCase6 () {
171+ testHandshakeRejection ( 6 );
172+ }
173+
174+ @ Test (timeout = 5000 )
175+ public void testHandshakeRejectionTestCase7 () {
176+ testHandshakeRejection ( 7 );
177+ }
178+
179+ @ Test (timeout = 5000 )
180+ public void testHandshakeRejectionTestCase8 () {
181+ testHandshakeRejection ( 8 );
182+ }
183+
184+ @ Test (timeout = 5000 )
185+ public void testHandshakeRejectionTestCase9 () {
186+ testHandshakeRejection ( 9 );
187+ }
188+
189+ @ Test (timeout = 5000 )
190+ public void testHandshakeRejectionTestCase10 () {
191+ testHandshakeRejection ( 10 );
192+ }
193+
194+ @ Test (timeout = 5000 )
195+ public void testHandshakeRejectionTestCase11 () {
196+ testHandshakeRejection ( 11 );
197+ }
198+
199+ private void testHandshakeRejection ( int i ) {
124200 try {
125- for ( int i = 0 ; i < testCases ; i ++ ) {
126- final CountDownLatch latch = new CountDownLatch ( 1 );
127- WebSocketClient webSocketClient = new WebSocketClient ( new URI ( "ws://localhost:8887/" + i ) ) {
128- @ Override
129- public void onOpen ( ServerHandshake handshakedata ) {
130- fail ( "There should not be a connection" );
131- latch .countDown ();
132- }
133- @ Override
134- public void onMessage ( String message ) {
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+ }
135207
136- }
137- @ Override
138- public void onClose ( int code , String reason , boolean remote ) {
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 ) {
139216 if ( code != CloseFrame .PROTOCOL_ERROR ) {
140- fail ( "There should be a 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+ }
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 ++;
141233 }
142- close ();
143- latch .countDown ();
144234 }
235+ }
145236
146- @ Override
147- public void onError ( Exception ex ) {
148- fail ( "There should not be an exception" );
149- }
150- };
151- webSocketClient .connect ();
152- latch .await ();
153- }
237+ @ Override
238+ public void onError ( Exception ex ) {
239+ fail ( "There should not be an exception" );
240+ }
241+ };
242+ webSocketClient .connectBlocking ();
154243 } catch ( Exception e ) {
244+ e .printStackTrace ();
155245 fail ( "There should be no exception" );
156246 }
157247 }
0 commit comments