From 99afabdf925fa88d250ce3d915667f59475c636f Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 27 Jul 2012 10:24:35 -0700 Subject: [PATCH] Finished fleshing out TestABCase1 --- .../jetty/websocket/io/DataFrameBytes.java | 1 - .../websocket/server/ab/TestABCase1.java | 434 ++++++++++-------- .../server/blockhead/BlockheadClient.java | 5 + 3 files changed, 238 insertions(+), 202 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/DataFrameBytes.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/DataFrameBytes.java index eaabfb4aaa9..13148f19f20 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/DataFrameBytes.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/io/DataFrameBytes.java @@ -25,7 +25,6 @@ import org.eclipse.jetty.websocket.protocol.WebSocketFrame; public class DataFrameBytes extends FrameBytes { private static final Logger LOG = Log.getLogger(DataFrameBytes.class); - private int size; private ByteBuffer buffer; public DataFrameBytes(WebSocketAsyncConnection connection, Callback callback, C context, WebSocketFrame frame) diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java index 0878b265f99..f6b6e0bb1b5 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java @@ -33,11 +33,7 @@ import org.junit.Test; public class TestABCase1 extends AbstractABCase { - /** - * Echo 0 byte text message - */ - @Test - public void testCase1_1_1() throws Exception + private void assertEchoEmptyFrame(OpCode opcode) throws Exception { BlockheadClient client = new BlockheadClient(server.getServerUri()); try @@ -46,10 +42,10 @@ public class TestABCase1 extends AbstractABCase client.sendStandardRequest(); client.expectUpgradeResponse(); - ByteBuffer buf = ByteBuffer.allocate(16); + ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD); BufferUtil.clearToFill(buf); - buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); + buf.put((byte)(0x00 | FIN | opcode.getCode())); putPayloadLength(buf,0); putMask(buf); @@ -59,8 +55,89 @@ public class TestABCase1 extends AbstractABCase // Read frame Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); WebSocketFrame frame = frames.remove(); - Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); - Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(0)); + Assert.assertThat("frame should be " + opcode + " frame",frame.getOpCode(),is(opcode)); + Assert.assertThat(opcode + ".payloadLength",frame.getPayloadLength(),is(0)); + } + finally + { + client.close(); + } + } + + private void assertEchoFrame(OpCode opcode, byte[] payload) throws Exception + { + BlockheadClient client = new BlockheadClient(server.getServerUri()); + try + { + client.connect(); + client.sendStandardRequest(); + client.expectUpgradeResponse(); + + ByteBuffer buf = ByteBuffer.allocate(payload.length + Generator.OVERHEAD); + BufferUtil.clearToFill(buf); + + buf.put((byte)(0x00 | FIN | opcode.getCode())); + putPayloadLength(buf,payload.length); + putMask(buf); + buf.put(masked(payload)); + + BufferUtil.flipToFlush(buf,0); + client.writeRaw(buf); + + // Read frame + Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); + WebSocketFrame frame = frames.remove(); + Assert.assertThat("frame should be " + opcode + " frame",frame.getOpCode(),is(opcode)); + Assert.assertThat(opcode + ".payloadLength",frame.getPayloadLength(),is(payload.length)); + ByteBufferAssert.assertEquals(opcode + ".payload",payload,frame.getPayload()); + } + finally + { + client.close(); + } + } + + private void assertEchoSegmentedFrame(OpCode opcode, byte payload[], int segmentSize) throws Exception + { + BlockheadClient client = new BlockheadClient(server.getServerUri()); + try + { + client.connect(); + client.sendStandardRequest(); + client.expectUpgradeResponse(); + + ByteBuffer buf = ByteBuffer.allocate(payload.length + Generator.OVERHEAD); + BufferUtil.clearToFill(buf); + + buf.put((byte)(0x00 | FIN | opcode.getCode())); + putPayloadLength(buf,payload.length); + putMask(buf); + buf.put(masked(payload)); + + BufferUtil.flipToFlush(buf,0); + int origLimit = buf.limit(); + int limit = buf.limit(); + int len; + int pos = buf.position(); + int overallLeft = buf.remaining(); + while (overallLeft > 0) + { + buf.position(pos); + limit = Math.min(origLimit,pos + segmentSize); + buf.limit(limit); + len = buf.remaining(); + overallLeft -= len; + pos += len; + client.writeRaw(buf); + client.flush(); + } + + // Read frame + Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); + WebSocketFrame frame = frames.remove(); + Assert.assertThat("frame should be " + opcode + " frame",frame.getOpCode(),is(opcode)); + Assert.assertThat(opcode + ".payloadLength",frame.getPayloadLength(),is(payload.length)); + ByteBufferAssert.assertEquals(opcode + ".payload",payload,frame.getPayload()); } finally { @@ -69,243 +146,198 @@ public class TestABCase1 extends AbstractABCase } /** - * Echo 125 byte text message (uses small 7-bit payload length) + * Echo 0 byte TEXT message + */ + @Test + public void testCase1_1_1() throws Exception + { + assertEchoEmptyFrame(OpCode.TEXT); + } + + /** + * Echo 125 byte TEXT message (uses small 7-bit payload length) */ @Test public void testCase1_1_2() throws Exception { - BlockheadClient client = new BlockheadClient(server.getServerUri()); - try - { - client.connect(); - client.sendStandardRequest(); - client.expectUpgradeResponse(); + byte payload[] = new byte[125]; + Arrays.fill(payload,(byte)'*'); - byte msg[] = new byte[125]; - Arrays.fill(msg,(byte)'*'); - - ByteBuffer buf = ByteBuffer.allocate(msg.length + Generator.OVERHEAD); - BufferUtil.clearToFill(buf); - - buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); - putPayloadLength(buf,msg.length); - putMask(buf); - buf.put(masked(msg)); - - BufferUtil.flipToFlush(buf,0); - client.writeRaw(buf); - - // Read frame - Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); - WebSocketFrame frame = frames.remove(); - Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); - Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(msg.length)); - ByteBufferAssert.assertEquals("Text.payload",msg,frame.getPayload()); - } - finally - { - client.close(); - } + assertEchoFrame(OpCode.TEXT,payload); } /** - * Echo 126 byte text message (uses medium 2 byte payload length) + * Echo 126 byte TEXT message (uses medium 2 byte payload length) */ @Test public void testCase1_1_3() throws Exception { - BlockheadClient client = new BlockheadClient(server.getServerUri()); - try - { - client.connect(); - client.sendStandardRequest(); - client.expectUpgradeResponse(); + byte payload[] = new byte[126]; + Arrays.fill(payload,(byte)'*'); - byte msg[] = new byte[126]; - Arrays.fill(msg,(byte)'*'); - - ByteBuffer buf = ByteBuffer.allocate(msg.length + Generator.OVERHEAD); - BufferUtil.clearToFill(buf); - - buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); - putPayloadLength(buf,msg.length); - putMask(buf); - buf.put(masked(msg)); - - BufferUtil.flipToFlush(buf,0); - client.writeRaw(buf); - - // Read frame - Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); - WebSocketFrame frame = frames.remove(); - Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); - Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(msg.length)); - ByteBufferAssert.assertEquals("Text.payload",msg,frame.getPayload()); - } - finally - { - client.close(); - } + assertEchoFrame(OpCode.TEXT,payload); } /** - * Echo 127 byte text message (uses medium 2 byte payload length) + * Echo 127 byte TEXT message (uses medium 2 byte payload length) */ @Test public void testCase1_1_4() throws Exception { - BlockheadClient client = new BlockheadClient(server.getServerUri()); - try - { - client.connect(); - client.sendStandardRequest(); - client.expectUpgradeResponse(); + byte payload[] = new byte[127]; + Arrays.fill(payload,(byte)'*'); - byte msg[] = new byte[127]; - Arrays.fill(msg,(byte)'*'); - - ByteBuffer buf = ByteBuffer.allocate(msg.length + Generator.OVERHEAD); - BufferUtil.clearToFill(buf); - - buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); - putPayloadLength(buf,msg.length); - putMask(buf); - buf.put(masked(msg)); - - BufferUtil.flipToFlush(buf,0); - client.writeRaw(buf); - - // Read frame - Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); - WebSocketFrame frame = frames.remove(); - Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); - Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(msg.length)); - ByteBufferAssert.assertEquals("Text.payload",msg,frame.getPayload()); - } - finally - { - client.close(); - } + assertEchoFrame(OpCode.TEXT,payload); } /** - * Echo 128 byte text message (uses medium 2 byte payload length) + * Echo 128 byte TEXT message (uses medium 2 byte payload length) */ @Test public void testCase1_1_5() throws Exception { - BlockheadClient client = new BlockheadClient(server.getServerUri()); - try - { - client.connect(); - client.sendStandardRequest(); - client.expectUpgradeResponse(); + byte payload[] = new byte[128]; + Arrays.fill(payload,(byte)'*'); - byte msg[] = new byte[128]; - Arrays.fill(msg,(byte)'*'); - - ByteBuffer buf = ByteBuffer.allocate(msg.length + Generator.OVERHEAD); - BufferUtil.clearToFill(buf); - - buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); - putPayloadLength(buf,msg.length); - putMask(buf); - buf.put(masked(msg)); - - BufferUtil.flipToFlush(buf,0); - client.writeRaw(buf); - - // Read frame - Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); - WebSocketFrame frame = frames.remove(); - Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); - Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(msg.length)); - ByteBufferAssert.assertEquals("Text.payload",msg,frame.getPayload()); - } - finally - { - client.close(); - } + assertEchoFrame(OpCode.TEXT,payload); } /** - * Echo 65535 byte text message (uses medium 2 byte payload length) + * Echo 65535 byte TEXT message (uses medium 2 byte payload length) */ @Test public void testCase1_1_6() throws Exception { - BlockheadClient client = new BlockheadClient(server.getServerUri()); - try - { - client.connect(); - client.sendStandardRequest(); - client.expectUpgradeResponse(); + byte payload[] = new byte[65535]; + Arrays.fill(payload,(byte)'*'); - byte msg[] = new byte[65535]; - Arrays.fill(msg,(byte)'*'); - - ByteBuffer buf = ByteBuffer.allocate(msg.length + Generator.OVERHEAD); - BufferUtil.clearToFill(buf); - - buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); - putPayloadLength(buf,msg.length); - putMask(buf); - buf.put(masked(msg)); - - BufferUtil.flipToFlush(buf,0); - client.writeRaw(buf); - - // Read frame - Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); - WebSocketFrame frame = frames.remove(); - Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); - Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(msg.length)); - ByteBufferAssert.assertEquals("Text.payload",msg,frame.getPayload()); - } - finally - { - client.close(); - } + assertEchoFrame(OpCode.TEXT,payload); } /** - * Echo 65536 byte text message (uses large 8 byte payload length) + * Echo 65536 byte TEXT message (uses large 8 byte payload length) */ @Test public void testCase1_1_7() throws Exception { - BlockheadClient client = new BlockheadClient(server.getServerUri()); - try - { - client.connect(); - client.sendStandardRequest(); - client.expectUpgradeResponse(); + byte payload[] = new byte[65536]; + Arrays.fill(payload,(byte)'*'); - byte msg[] = new byte[65536]; - Arrays.fill(msg,(byte)'*'); - - ByteBuffer buf = ByteBuffer.allocate(msg.length + Generator.OVERHEAD); - BufferUtil.clearToFill(buf); - - buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); - putPayloadLength(buf,msg.length); - putMask(buf); - buf.put(masked(msg)); - - BufferUtil.flipToFlush(buf,0); - client.writeRaw(buf); - - // Read frame - Queue frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); - WebSocketFrame frame = frames.remove(); - Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); - Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(msg.length)); - ByteBufferAssert.assertEquals("Text.payload",msg,frame.getPayload()); - } - finally - { - client.close(); - } + assertEchoFrame(OpCode.TEXT,payload); } + /** + * Echo 65536 byte TEXT message (uses large 8 byte payload length). + *

+ * Only send 1 TEXT frame from client, but in small segments (flushed after each). + *

+ * This is done to test the parsing together of the frame on the server side. + */ + @Test + public void testCase1_1_8() throws Exception + { + byte payload[] = new byte[65536]; + Arrays.fill(payload,(byte)'*'); + int segmentSize = 997; + + assertEchoSegmentedFrame(OpCode.TEXT,payload,segmentSize); + } + + /** + * Echo 0 byte BINARY message + */ + @Test + public void testCase1_2_1() throws Exception + { + assertEchoEmptyFrame(OpCode.BINARY); + } + + /** + * Echo 125 byte BINARY message (uses small 7-bit payload length) + */ + @Test + public void testCase1_2_2() throws Exception + { + byte payload[] = new byte[125]; + Arrays.fill(payload,(byte)0xFE); + + assertEchoFrame(OpCode.BINARY,payload); + } + + /** + * Echo 126 byte BINARY message (uses medium 2 byte payload length) + */ + @Test + public void testCase1_2_3() throws Exception + { + byte payload[] = new byte[126]; + Arrays.fill(payload,(byte)0xFE); + + assertEchoFrame(OpCode.BINARY,payload); + } + + /** + * Echo 127 byte BINARY message (uses medium 2 byte payload length) + */ + @Test + public void testCase1_2_4() throws Exception + { + byte payload[] = new byte[127]; + Arrays.fill(payload,(byte)0xFE); + + assertEchoFrame(OpCode.BINARY,payload); + } + + /** + * Echo 128 byte BINARY message (uses medium 2 byte payload length) + */ + @Test + public void testCase1_2_5() throws Exception + { + byte payload[] = new byte[128]; + Arrays.fill(payload,(byte)0xFE); + + assertEchoFrame(OpCode.BINARY,payload); + } + + /** + * Echo 65535 byte BINARY message (uses medium 2 byte payload length) + */ + @Test + public void testCase1_2_6() throws Exception + { + byte payload[] = new byte[65535]; + Arrays.fill(payload,(byte)0xFE); + + assertEchoFrame(OpCode.BINARY,payload); + } + + /** + * Echo 65536 byte BINARY message (uses large 8 byte payload length) + */ + @Test + public void testCase1_2_7() throws Exception + { + byte payload[] = new byte[65536]; + Arrays.fill(payload,(byte)0xFE); + + assertEchoFrame(OpCode.BINARY,payload); + } + + /** + * Echo 65536 byte BINARY message (uses large 8 byte payload length). + *

+ * Only send 1 BINARY frame from client, but in small segments (flushed after each). + *

+ * This is done to test the parsing together of the frame on the server side. + */ + @Test + public void testCase1_2_8() throws Exception + { + byte payload[] = new byte[65536]; + Arrays.fill(payload,(byte)0xFE); + int segmentSize = 997; + + assertEchoSegmentedFrame(OpCode.BINARY,payload,segmentSize); + } } diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java index fcc8f0026e2..bebd28ff4ce 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java @@ -249,6 +249,11 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames return respHeader; } + public void flush() throws IOException + { + out.flush(); + } + public List getExtensions() { return extensions;