From c1ff1edad797d35214e69c3af002fc9fe2002d73 Mon Sep 17 00:00:00 2001 From: marci4 Date: Wed, 1 Nov 2017 17:10:57 +0100 Subject: [PATCH] Imrpoved OpeningHandshakeRejection test Also removed assert() in WebSocketClient runnable since it has no understandable purpose! --- .../client/WebSocketClient.java | 3 +- .../misc/OpeningHandshakeRejectionTest.java | 186 +++++++++++++----- 2 files changed, 140 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 29d598fe2..775492bfa 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -285,7 +285,8 @@ public void run() { onError( e ); engine.closeConnection( CloseFrame.ABNORMAL_CLOSE, e.getMessage() ); } - assert ( socket.isClosed() ); + //I have no idea why this was added. + //assert ( socket.isClosed() ); } private int getPort() { diff --git a/src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java b/src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java index a513d77da..01b0f87a7 100644 --- a/src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java +++ b/src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java @@ -25,10 +25,13 @@ package org.java_websocket.misc; +import org.java_websocket.WebSocketImpl; import org.java_websocket.client.WebSocketClient; import org.java_websocket.framing.CloseFrame; import org.java_websocket.handshake.ServerHandshake; import org.java_websocket.util.Charsetfunctions; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; @@ -37,23 +40,26 @@ import java.net.Socket; import java.net.URI; import java.util.Scanner; -import java.util.concurrent.CountDownLatch; import static org.junit.Assert.fail; public class OpeningHandshakeRejectionTest { - /** - * How many testcases do we have - */ - private static final int testCases = 10; + private static final int testCases = 12; + private static final String additionalHandshake = "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n"; + private static int counter = 0; + private static Thread thread; - public OpeningHandshakeRejectionTest() { - Thread thread = new Thread( + private static boolean debugPrintouts = false; + + @BeforeClass + public static void startServer() { + thread = new Thread( new Runnable() { public void run() { try { ServerSocket server = new ServerSocket( 8887 ); + int count = 1; while( true ) { Socket client = null; try { @@ -62,56 +68,60 @@ public void run() { String input = in.nextLine(); String testCase = input.split( " " )[1]; OutputStream os = client.getOutputStream(); + count++; if( "/0".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 100 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 100 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/1".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP/1.0 100 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP/1.0 100 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/2".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP 100 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP 100 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/3".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 200 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 200 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/4".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP 101 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP 101 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/5".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 404 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 404 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/6".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP/2.0 404 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP/2.0 404 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/7".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 500 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 500 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/8".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "GET 302 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "GET 302 Switching Protocols\r\n" + additionalHandshake ) ); os.flush(); } if( "/9".equals( testCase ) ) { - os.write( Charsetfunctions.asciiBytes( "GET HTTP/1.1 101 Switching Protocols\r\n" ) ); + os.write( Charsetfunctions.asciiBytes( "GET HTTP/1.1 101 Switching Protocols\r\n" + additionalHandshake ) ); + os.flush(); + } + if( "/10".equals( testCase ) ) { + os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 101 Switching Protocols\r\n" + additionalHandshake ) ); + os.flush(); + } + if( "/11".equals( testCase ) ) { + os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 101 Websocket Connection Upgrade\r\n" + additionalHandshake ) ); os.flush(); } - client.close(); } catch ( IOException e ) { fail( "There should be no exception" ); - } finally { - if( client != null ) - client.close(); } } } catch ( Exception e ) { - e.printStackTrace(); fail( "There should be no exception" ); } } @@ -119,39 +129,119 @@ public void run() { thread.start(); } - @Test(timeout=10000) - public void testClient() { + @AfterClass + public static void successTests() throws InterruptedException { + thread.interrupt(); + if (debugPrintouts) + System.out.println( counter + " successful tests" ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase0() { + testHandshakeRejection( 0 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase1() { + testHandshakeRejection( 1 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase2() { + testHandshakeRejection( 2 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase3() { + testHandshakeRejection( 3 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase4() { + testHandshakeRejection( 4 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase5() { + testHandshakeRejection( 5 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase6() { + testHandshakeRejection( 6 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase7() { + testHandshakeRejection( 7 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase8() { + testHandshakeRejection( 8 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase9() { + testHandshakeRejection( 9 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase10() { + testHandshakeRejection( 10 ); + } + + @Test(timeout = 5000) + public void testHandshakeRejectionTestCase11() { + testHandshakeRejection( 11 ); + } + + private void testHandshakeRejection( int i ) { try { - for( int i = 0; i < testCases; i++ ) { - final CountDownLatch latch = new CountDownLatch( 1 ); - WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + i ) ) { - @Override - public void onOpen( ServerHandshake handshakedata ) { - fail( "There should not be a connection" ); - latch.countDown(); - } - @Override - public void onMessage( String message ) { + final int finalI = i; + WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + finalI ) ) { + @Override + public void onOpen( ServerHandshake handshakedata ) { + fail( "There should not be a connection!" ); + } - } - @Override - public void onClose( int code, String reason, boolean remote ) { + @Override + public void onMessage( String message ) { + fail( "There should not be a message!" ); + } + + @Override + public void onClose( int code, String reason, boolean remote ) { + if( finalI != 10 && finalI != 11 ) { if( code != CloseFrame.PROTOCOL_ERROR ) { - fail( "There should be a protocol error" ); + fail( "There should be a protocol error!" ); + } else if (reason.startsWith( "Invalid status code received:") || reason.startsWith( "Invalid status line received:")) { + if (debugPrintouts) + System.out.println("Protocol error for test case: " + finalI); + counter++; + } else { + fail( "The reason should be included!" ); + } + } else { + //Since we do not include a correct Sec-WebSocket-Accept, onClose will be called with reason 'Draft refuses handshake' + if (!reason.endsWith( "refuses handshake" )) { + fail( "onClose should not be called!" ); + } else { + if (debugPrintouts) + System.out.println("Refuses handshake error for test case: " + finalI); + counter++; } - close(); - latch.countDown(); } + } - @Override - public void onError( Exception ex ) { - fail( "There should not be an exception" ); - } - }; - webSocketClient.connect(); - latch.await(); - } + @Override + public void onError( Exception ex ) { + fail( "There should not be an exception" ); + } + }; + webSocketClient.connectBlocking(); } catch ( Exception e ) { + e.printStackTrace(); fail( "There should be no exception" ); } }