From e32249c24f16644564660dc29b5d7361c1ded212 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 26 Jun 2012 16:36:38 -0700 Subject: [PATCH] Fixing old testcase as fallout from API updates --- .../server/WebSocketMessageRFC6455Test.java | 731 +++++++----------- 1 file changed, 258 insertions(+), 473 deletions(-) diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketMessageRFC6455Test.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketMessageRFC6455Test.java index 849673c8a38..ad352dc3412 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketMessageRFC6455Test.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketMessageRFC6455Test.java @@ -178,7 +178,7 @@ public class WebSocketMessageRFC6455Test } - private static final int WSVERSION = org.eclipse.jetty.websocket.api.WebSocket.VERSION; + private static final int WSVERSION = 13; // RFC-6455 version private static Server __server; private static SelectChannelConnector __connector; private static TestWebSocket __serverWebSocket; @@ -199,13 +199,13 @@ public class WebSocketMessageRFC6455Test { __textCount.set(0); __serverWebSocket = new TestWebSocket(); - __serverWebSocket._onConnect=("onConnect".equals(protocol)); - __serverWebSocket._echo=("echo".equals(protocol)); - __serverWebSocket._aggregate=("aggregate".equals(protocol)); - __serverWebSocket._latch=("latch".equals(protocol)); + __serverWebSocket._onConnect = ("onConnect".equals(protocol)); + __serverWebSocket._echo = ("echo".equals(protocol)); + __serverWebSocket._aggregate = ("aggregate".equals(protocol)); + __serverWebSocket._latch = ("latch".equals(protocol)); if (__serverWebSocket._latch) { - __latch=new CountDownLatch(1); + __latch = new CountDownLatch(1); } return __serverWebSocket; } @@ -217,7 +217,6 @@ public class WebSocketMessageRFC6455Test __server.start(); } - @AfterClass public static void stopServer() throws Exception { @@ -225,79 +224,70 @@ public class WebSocketMessageRFC6455Test __server.join(); } - private void lookFor(String string,InputStream in) - throws IOException - { - String orig=string; - Utf8StringBuilder scanned=new Utf8StringBuilder(); + private void lookFor(String string, InputStream in) throws IOException + { + String orig = string; + Utf8StringBuilder scanned = new Utf8StringBuilder(); try { - while(true) + while (true) { int b = in.read(); - if (b<0) + if (b < 0) { throw new EOFException(); } scanned.append((byte)b); - assertEquals("looking for\""+orig+"\" in '"+scanned+"'",string.charAt(0),b); - if (string.length()==1) + assertEquals("looking for\"" + orig + "\" in '" + scanned + "'",string.charAt(0),b); + if (string.length() == 1) { break; } - string=string.substring(1); + string = string.substring(1); } } - catch(IOException e) + catch (IOException e) { - System.err.println("IOE while looking for \""+orig+"\" in '"+scanned+"'"); + System.err.println("IOE while looking for \"" + orig + "\" in '" + scanned + "'"); throw e; } - } + } - private void skipTo(String string,InputStream in) - throws IOException - { - int state=0; + private void skipTo(String string, InputStream in) throws IOException + { + int state = 0; - while(true) + while (true) { int b = in.read(); - if (b<0) + if (b < 0) { throw new EOFException(); } - if (b==string.charAt(state)) + if (b == string.charAt(state)) { state++; - if (state==string.length()) + if (state == string.length()) { break; } } else { - state=0; + state = 0; } } - } + } @Test public void testBinaryAggregate() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: aggregate\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: aggregate\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); socket.setSoTimeout(1000); @@ -318,10 +308,10 @@ public class WebSocketMessageRFC6455Test output.write(0xff); output.write(0xff); output.write(0xff); - byte[] bytes="0123456789".getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = "0123456789".getBytes(StringUtil.__ISO_8859_1); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); @@ -333,7 +323,7 @@ public class WebSocketMessageRFC6455Test output.write(0xff); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); @@ -342,38 +332,30 @@ public class WebSocketMessageRFC6455Test lookFor("01234567890123456789",input); } - @Test public void testBlockedConsumer() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - byte[] bytes="This is a long message of text that we will send again and again".getBytes(StringUtil.__ISO_8859_1); - byte[] mesg=new byte[bytes.length+6]; + byte[] bytes = "This is a long message of text that we will send again and again".getBytes(StringUtil.__ISO_8859_1); + byte[] mesg = new byte[bytes.length + 6]; mesg[0] = (byte)(0x80 + OpCode.TEXT.getCode()); - mesg[1]=(byte)(0x80+bytes.length); - mesg[2]=(byte)0xff; - mesg[3]=(byte)0xff; - mesg[4]=(byte)0xff; - mesg[5]=(byte)0xff; - for (int i=0;i>> "+i); output.flush(); - long now=System.currentTimeMillis(); - long duration=now-start; - start=now; - if (max2000); // was blocked + assertEquals(count + 1,__textCount.get()); // all messages + assertTrue(max > 2000); // was blocked } @Test public void testBlockedProducer() throws Exception { - final Socket socket = new Socket("localhost", __connector.getLocalPort()); + final Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); final int count = 100000; - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: latch\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: latch\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -484,7 +459,7 @@ public class WebSocketMessageRFC6455Test __latch.countDown(); // wait 2s and then consume messages - final AtomicLong totalB=new AtomicLong(); + final AtomicLong totalB = new AtomicLong(); new Thread() { @Override @@ -494,120 +469,86 @@ public class WebSocketMessageRFC6455Test { Thread.sleep(2000); - byte[] recv = new byte[32*1024]; + byte[] recv = new byte[32 * 1024]; - int len=0; - while (len>=0) + int len = 0; + while (len >= 0) { totalB.addAndGet(len); - len=socket.getInputStream().read(recv,0,recv.length); + len = socket.getInputStream().read(recv,0,recv.length); Thread.sleep(10); } } - catch(Exception e) + catch (Exception e) { e.printStackTrace(); } } }.start(); - // Send enough messages to fill receive buffer - long max=0; - long start=System.currentTimeMillis(); - String mesg="How Now Brown Cow"; - for (int i=0;i0) + if (code > 0) { - output.write(code/0x100); - output.write(code%0x100); + output.write(code / 0x100); + output.write(code % 0x100); output.write(m.getBytes()); } output.flush(); @@ -644,17 +585,17 @@ public class WebSocketMessageRFC6455Test byte[] buf = new byte[128]; int len = input.read(buf); - assertEquals(tst,2+tests[t][1],len); + assertEquals(tst,2 + tests[t][1],len); assertEquals(tst,(byte)0x88,buf[0]); - if (len>=4) + if (len >= 4) { - code=((0xff&buf[2])*0x100)+(0xff&buf[3]); + code = ((0xff & buf[2]) * 0x100) + (0xff & buf[3]); assertEquals(tst,tests[t][2],code); - if (len>4) + if (len > 4) { - m = new String(buf,4,len-4,"UTF-8"); + m = new String(buf,4,len - 4,"UTF-8"); assertEquals(tst,resp[t],m); } } @@ -663,7 +604,6 @@ public class WebSocketMessageRFC6455Test assertEquals(tst,tests[t][2],-1); } - len = input.read(buf); assertEquals(tst,-1,len); } @@ -673,52 +613,31 @@ public class WebSocketMessageRFC6455Test public void testCloseOut() throws Exception { int[][] tests = - { - {-1,0,-1}, - {-1,0,-1}, - {0,2,1000}, - {0,2+4,1000}, - {1000,2,1000}, - {1000,2+4,1000}, - {1005,0,-1}, - {1005,0,-1}, - {1006,0,-1}, - {1006,0,-1}, - {9000,2,9000}, - {9000,2+4,9000} - }; + { + { -1, 0, -1 }, + { -1, 0, -1 }, + { 0, 2, 1000 }, + { 0, 2 + 4, 1000 }, + { 1000, 2, 1000 }, + { 1000, 2 + 4, 1000 }, + { 1005, 0, -1 }, + { 1005, 0, -1 }, + { 1006, 0, -1 }, + { 1006, 0, -1 }, + { 9000, 2, 9000 }, + { 9000, 2 + 4, 9000 } }; String[] mesg = - { - null, - "Not Sent", - null, - "mesg", - null, - "mesg", - null, - "mesg", - null, - "mesg", - null, - "mesg" - }; + { null, "Not Sent", null, "mesg", null, "mesg", null, "mesg", null, "mesg", null, "mesg" }; - for (int t=0;t=4) + if (len >= 4) { - int code=((0xff&buf[2])*0x100)+(0xff&buf[3]); + int code = ((0xff & buf[2]) * 0x100) + (0xff & buf[3]); assertEquals(tst,tests[t][2],code); - if (len>4) + if (len > 4) { - String m = new String(buf,4,len-4,"UTF-8"); + String m = new String(buf,4,len - 4,"UTF-8"); assertEquals(tst,mesg[t],m); } } @@ -765,9 +684,9 @@ public class WebSocketMessageRFC6455Test output.write(0x00); output.flush(); } - catch(IOException e) + catch (IOException e) { - System.err.println("socket "+socket); + System.err.println("socket " + socket); throw e; } @@ -779,20 +698,12 @@ public class WebSocketMessageRFC6455Test @Test public void testDeflateFrameExtension() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: echo\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "Sec-WebSocket-Extensions: x-deflate-frame;minLength=64\r\n"+ - "Sec-WebSocket-Extensions: fragment;minFragments=2\r\n"+ - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: echo\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "Sec-WebSocket-Extensions: x-deflate-frame;minLength=64\r\n" + + "Sec-WebSocket-Extensions: fragment;minFragments=2\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -811,37 +722,36 @@ public class WebSocketMessageRFC6455Test assertTrue(__serverWebSocket.awaitConnected(1000)); assertNotNull(__serverWebSocket.connection); - // Server sends a big message String text = "0123456789ABCDEF "; - text=text+text+text+text; - text=text+text+text+text; - text=text+text+text+text+'X'; - byte[] data=text.getBytes("utf-8"); + text = text + text + text + text; + text = text + text + text + text; + text = text + text + text + text + 'X'; + byte[] data = text.getBytes("utf-8"); Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); - byte[] buf=new byte[data.length]; + byte[] buf = new byte[data.length]; - buf[0]=((byte)0x7e); - buf[1]=(byte)(data.length>>8); - buf[2]=(byte)(data.length&0xff); + buf[0] = ((byte)0x7e); + buf[1] = (byte)(data.length >> 8); + buf[2] = (byte)(data.length & 0xff); - int l=deflater.deflate(buf,3,buf.length-3); + int l = deflater.deflate(buf,3,buf.length - 3); assertTrue(deflater.finished()); output.write(0xC1); - output.write((byte)(0x80|(0xff&(l+3)))); + output.write((byte)(0x80 | (0xff & (l + 3)))); output.write(0x00); output.write(0x00); output.write(0x00); output.write(0x00); - output.write(buf,0,l+3); + output.write(buf,0,l + 3); output.flush(); assertEquals(0x40 + OpCode.TEXT.getCode(),input.read()); - assertEquals(0x20+3,input.read()); + assertEquals(0x20 + 3,input.read()); assertEquals(0x7e,input.read()); assertEquals(0x02,input.read()); assertEquals(0x20,input.read()); @@ -856,9 +766,8 @@ public class WebSocketMessageRFC6455Test assertEquals(544,inflater.inflate(result)); assertEquals(TypeUtil.toHexString(data,0,544),TypeUtil.toHexString(result)); - assertEquals((byte)0xC0,(byte)input.read()); - assertEquals(0x21+3,input.read()); + assertEquals(0x21 + 3,input.read()); assertEquals(0x7e,input.read()); assertEquals(0x02,input.read()); assertEquals(0x21,input.read()); @@ -871,25 +780,17 @@ public class WebSocketMessageRFC6455Test assertEquals(545,inflater.inflate(result)); assertEquals(TypeUtil.toHexString(data,544,545),TypeUtil.toHexString(result)); - } @Test public void testFragmentExtension() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: onConnect\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "Sec-WebSocket-Extensions: fragment;maxLength=4;minFragments=7\r\n"+ - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: onConnect\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "Sec-WebSocket-Extensions: fragment;maxLength=4;minFragments=7\r\n" + "\r\n") + .getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -937,20 +838,12 @@ public class WebSocketMessageRFC6455Test @Test public void testIdentityExtension() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: onConnect\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "Sec-WebSocket-Extensions: identity;param=0\r\n"+ - "Sec-WebSocket-Extensions: identity;param=1, identity ; param = '2' ; other = ' some = value ' \r\n"+ - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: onConnect\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "Sec-WebSocket-Extensions: identity;param=0\r\n" + + "Sec-WebSocket-Extensions: identity;param=1, identity ; param = '2' ; other = ' some = value ' \r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -976,22 +869,14 @@ public class WebSocketMessageRFC6455Test lookFor("sent on connect",input); } - @Test public void testIdle() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: onConnect\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: onConnect\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -1013,27 +898,26 @@ public class WebSocketMessageRFC6455Test assertEquals((byte)0x88,(byte)input.read()); assertEquals(26,input.read()); - assertEquals(1000/0x100,input.read()); - assertEquals(1000%0x100,input.read()); + assertEquals(1000 / 0x100,input.read()); + assertEquals(1000 % 0x100,input.read()); lookFor("Idle",input); // respond to close - output.write(0x88^0xff); - output.write(0x80^0xff); + output.write(0x88 ^ 0xff); + output.write(0x80 ^ 0xff); output.write(0xff); output.write(0xff); output.write(0xff); output.write(0xff); output.flush(); - assertTrue(__serverWebSocket.awaitDisconnected(5000)); try { __serverWebSocket.connection.sendMessage("Don't send"); assertTrue(false); } - catch(IOException e) + catch (IOException e) { assertTrue(true); } @@ -1042,18 +926,11 @@ public class WebSocketMessageRFC6455Test @Test public void testMaxBinarySize() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: other\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: other\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); socket.setSoTimeout(100000); @@ -1075,10 +952,10 @@ public class WebSocketMessageRFC6455Test output.write(0xff); output.write(0xff); output.write(0xff); - byte[] bytes="0123456789".getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = "0123456789".getBytes(StringUtil.__ISO_8859_1); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); @@ -1090,14 +967,13 @@ public class WebSocketMessageRFC6455Test output.write(0xff); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); - assertEquals(0x80 | OpCode.CLOSE.getCode(),input.read()); assertEquals(19,input.read()); - int code=((0xff&input.read())*0x100)+(0xff&input.read()); + int code = ((0xff & input.read()) * 0x100) + (0xff & input.read()); assertEquals(StatusCode.MESSAGE_TOO_LARGE,code); lookFor("Message size > 15",input); } @@ -1105,18 +981,11 @@ public class WebSocketMessageRFC6455Test @Test public void testMaxBinarySize2() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: other\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: other\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); socket.setSoTimeout(100000); @@ -1138,37 +1007,28 @@ public class WebSocketMessageRFC6455Test output.write(0xff); output.write(0xff); output.write(0xff); - byte[] bytes="01234567890123456789".getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = "01234567890123456789".getBytes(StringUtil.__ISO_8859_1); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); assertEquals(0x80 | OpCode.CLOSE.getCode(),input.read()); assertEquals(19,input.read()); - int code=((0xff&input.read())*0x100)+(0xff&input.read()); + int code = ((0xff & input.read()) * 0x100) + (0xff & input.read()); assertEquals(StatusCode.MESSAGE_TOO_LARGE,code); lookFor("Message size > 15",input); } - - @Test public void testMaxTextSize() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: other\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: other\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); socket.setSoTimeout(1000); @@ -1190,10 +1050,10 @@ public class WebSocketMessageRFC6455Test output.write(0xff); output.write(0xff); output.write(0xff); - byte[] bytes="0123456789".getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = "0123456789".getBytes(StringUtil.__ISO_8859_1); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); @@ -1205,34 +1065,25 @@ public class WebSocketMessageRFC6455Test output.write(0xff); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); assertEquals(0x80 | OpCode.CLOSE.getCode(),input.read()); assertEquals(30,input.read()); - int code=((0xff&input.read())*0x100)+(0xff&input.read()); + int code = ((0xff & input.read()) * 0x100) + (0xff & input.read()); assertEquals(StatusCode.MESSAGE_TOO_LARGE,code); lookFor("Text message size > 15 chars",input); } - - @Test public void testMaxTextSize2() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: other\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: other\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); socket.setSoTimeout(100000); @@ -1254,38 +1105,28 @@ public class WebSocketMessageRFC6455Test output.write(0xff); output.write(0xff); output.write(0xff); - byte[] bytes="01234567890123456789".getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = "01234567890123456789".getBytes(StringUtil.__ISO_8859_1); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); - - assertEquals(0x80 | OpCode.CLOSE.getCode(),input.read()); assertEquals(30,input.read()); - int code=((0xff&input.read())*0x100)+(0xff&input.read()); + int code = ((0xff & input.read()) * 0x100) + (0xff & input.read()); assertEquals(StatusCode.MESSAGE_TOO_LARGE,code); lookFor("Text message size > 15 chars",input); } - @Test public void testMaxTextSizeFalseFrag() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: other\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: other\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); socket.setSoTimeout(1000); @@ -1299,28 +1140,27 @@ public class WebSocketMessageRFC6455Test assertTrue(__serverWebSocket.awaitConnected(1000)); assertNotNull(__serverWebSocket.connection); - __serverWebSocket.getConnection().setMaxTextMessageSize(10*1024); + __serverWebSocket.getConnection().setMaxTextMessageSize(10 * 1024); __serverWebSocket.getConnection().setAllowFrameFragmentation(true); output.write(0x81); - output.write(0x80|0x7E); - output.write((byte)((16*1024)>>8)); - output.write((byte)((16*1024)&0xff)); + output.write(0x80 | 0x7E); + output.write((byte)((16 * 1024) >> 8)); + output.write((byte)((16 * 1024) & 0xff)); output.write(0x00); output.write(0x00); output.write(0x00); output.write(0x00); - for (int i=0;i<(16*1024);i++) + for (int i = 0; i < (16 * 1024); i++) { output.write('X'); } output.flush(); - assertEquals(0x80 | OpCode.CLOSE.getCode(),input.read()); assertEquals(33,input.read()); - int code=((0xff&input.read())*0x100)+(0xff&input.read()); + int code = ((0xff & input.read()) * 0x100) + (0xff & input.read()); assertEquals(StatusCode.MESSAGE_TOO_LARGE,code); lookFor("Text message size > 10240 chars",input); } @@ -1328,18 +1168,11 @@ public class WebSocketMessageRFC6455Test @Test public void testNotUTF8() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: chat\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: chat\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); socket.setSoTimeout(100000); @@ -1367,7 +1200,7 @@ public class WebSocketMessageRFC6455Test assertEquals(0x80 | OpCode.CLOSE.getCode(),input.read()); assertEquals(15,input.read()); - int code=((0xff&input.read())*0x100)+(0xff&input.read()); + int code = ((0xff & input.read()) * 0x100) + (0xff & input.read()); assertEquals(StatusCode.BAD_PAYLOAD,code); lookFor("Invalid UTF-8",input); } @@ -1375,18 +1208,11 @@ public class WebSocketMessageRFC6455Test @Test public void testServerEcho() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: echo\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: echo\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); output.write(0x84); output.write(0x8f); @@ -1394,10 +1220,10 @@ public class WebSocketMessageRFC6455Test output.write(0xff); output.write(0xff); output.write(0xff); - byte[] bytes="this is an echo".getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = "this is an echo".getBytes(StringUtil.__ISO_8859_1); for (byte b : bytes) { - output.write(b^0xff); + output.write(b ^ 0xff); } output.flush(); // Make sure the read times out if there are problems with the implementation @@ -1418,24 +1244,15 @@ public class WebSocketMessageRFC6455Test lookFor("this is an echo",input); } - - @Test public void testServerPingPong() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); // Make sure the read times out if there are problems with the implementation OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: echo\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: echo\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); output.write(0x89); output.write(0x80); @@ -1463,18 +1280,11 @@ public class WebSocketMessageRFC6455Test @Test public void testServerSendBigStringMessage() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: chat, superchat\r\n"+ - "Sec-WebSocket-Version: "+org.eclipse.jetty.websocket.api.WebSocket.VERSION+"\r\n"+ - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + + "Sec-WebSocket-Protocol: chat, superchat\r\n" + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -1497,7 +1307,7 @@ public class WebSocketMessageRFC6455Test { message.append(text); } - String data=message.toString(); + String data = message.toString(); __serverWebSocket.connection.sendMessage(data); assertEquals(OpCode.TEXT.getCode(),input.read()); @@ -1510,22 +1320,14 @@ public class WebSocketMessageRFC6455Test lookFor(data.substring(0x1ff6),input); } - @Test public void testServerSendOnConnect() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: onConnect\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: onConnect\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -1549,18 +1351,11 @@ public class WebSocketMessageRFC6455Test @Test public void testTCPClose() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: onConnect\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: onConnect\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -1573,7 +1368,6 @@ public class WebSocketMessageRFC6455Test lookFor("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",input); skipTo("\r\n\r\n",input); - assertTrue(__serverWebSocket.awaitConnected(1000)); assertNotNull(__serverWebSocket.connection); @@ -1589,28 +1383,20 @@ public class WebSocketMessageRFC6455Test __serverWebSocket.connection.sendMessage("Don't send"); assertTrue(false); } - catch(IOException e) + catch (IOException e) { assertTrue(true); } } - @Test public void testTCPHalfClose() throws Exception { - Socket socket = new Socket("localhost", __connector.getLocalPort()); + Socket socket = new Socket("localhost",__connector.getLocalPort()); OutputStream output = socket.getOutputStream(); - output.write( - ("GET /chat HTTP/1.1\r\n"+ - "Host: server.example.com\r\n"+ - "Upgrade: websocket\r\n"+ - "Connection: Upgrade\r\n"+ - "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"+ - "Sec-WebSocket-Origin: http://example.com\r\n"+ - "Sec-WebSocket-Protocol: onConnect\r\n" + - "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + - "\r\n").getBytes("ISO-8859-1")); + output.write(("GET /chat HTTP/1.1\r\n" + "Host: server.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Origin: http://example.com\r\n" + "Sec-WebSocket-Protocol: onConnect\r\n" + + "Sec-WebSocket-Version: " + WSVERSION + "\r\n" + "\r\n").getBytes("ISO-8859-1")); output.flush(); // Make sure the read times out if there are problems with the implementation @@ -1623,7 +1409,6 @@ public class WebSocketMessageRFC6455Test lookFor("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",input); skipTo("\r\n\r\n",input); - assertTrue(__serverWebSocket.awaitConnected(1000)); assertNotNull(__serverWebSocket.connection); @@ -1642,13 +1427,13 @@ public class WebSocketMessageRFC6455Test // look for broken pipe try { - for (int i=0;i<1000;i++) + for (int i = 0; i < 1000; i++) { output.write(0); } Assert.fail(); } - catch(SocketException e) + catch (SocketException e) { // expected }