Finished fleshing out TestABCase1

This commit is contained in:
Joakim Erdfelt 2012-07-27 10:24:35 -07:00
parent 47f882e6dc
commit 99afabdf92
3 changed files with 238 additions and 202 deletions

View File

@ -25,7 +25,6 @@ import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
public class DataFrameBytes<C> extends FrameBytes<C> public class DataFrameBytes<C> extends FrameBytes<C>
{ {
private static final Logger LOG = Log.getLogger(DataFrameBytes.class); private static final Logger LOG = Log.getLogger(DataFrameBytes.class);
private int size;
private ByteBuffer buffer; private ByteBuffer buffer;
public DataFrameBytes(WebSocketAsyncConnection connection, Callback<C> callback, C context, WebSocketFrame frame) public DataFrameBytes(WebSocketAsyncConnection connection, Callback<C> callback, C context, WebSocketFrame frame)

View File

@ -33,11 +33,7 @@ import org.junit.Test;
public class TestABCase1 extends AbstractABCase public class TestABCase1 extends AbstractABCase
{ {
/** private void assertEchoEmptyFrame(OpCode opcode) throws Exception
* Echo 0 byte text message
*/
@Test
public void testCase1_1_1() throws Exception
{ {
BlockheadClient client = new BlockheadClient(server.getServerUri()); BlockheadClient client = new BlockheadClient(server.getServerUri());
try try
@ -46,10 +42,10 @@ public class TestABCase1 extends AbstractABCase
client.sendStandardRequest(); client.sendStandardRequest();
client.expectUpgradeResponse(); client.expectUpgradeResponse();
ByteBuffer buf = ByteBuffer.allocate(16); ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD);
BufferUtil.clearToFill(buf); BufferUtil.clearToFill(buf);
buf.put((byte)(0x00 | FIN | OpCode.TEXT.getCode())); buf.put((byte)(0x00 | FIN | opcode.getCode()));
putPayloadLength(buf,0); putPayloadLength(buf,0);
putMask(buf); putMask(buf);
@ -59,8 +55,89 @@ public class TestABCase1 extends AbstractABCase
// Read frame // Read frame
Queue<WebSocketFrame> frames = client.readFrames(1,TimeUnit.MILLISECONDS,500); Queue<WebSocketFrame> frames = client.readFrames(1,TimeUnit.MILLISECONDS,500);
WebSocketFrame frame = frames.remove(); WebSocketFrame frame = frames.remove();
Assert.assertThat("frame should be TEXT frame",frame.getOpCode(),is(OpCode.TEXT)); Assert.assertThat("frame should be " + opcode + " frame",frame.getOpCode(),is(opcode));
Assert.assertThat("Text.payloadLength",frame.getPayloadLength(),is(0)); 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<WebSocketFrame> 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<WebSocketFrame> 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 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 @Test
public void testCase1_1_2() throws Exception public void testCase1_1_2() throws Exception
{ {
BlockheadClient client = new BlockheadClient(server.getServerUri()); byte payload[] = new byte[125];
try Arrays.fill(payload,(byte)'*');
{
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
byte msg[] = new byte[125]; assertEchoFrame(OpCode.TEXT,payload);
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<WebSocketFrame> 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();
}
} }
/** /**
* Echo 126 byte text message (uses medium 2 byte payload length) * Echo 126 byte TEXT message (uses medium 2 byte payload length)
*/ */
@Test @Test
public void testCase1_1_3() throws Exception public void testCase1_1_3() throws Exception
{ {
BlockheadClient client = new BlockheadClient(server.getServerUri()); byte payload[] = new byte[126];
try Arrays.fill(payload,(byte)'*');
{
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
byte msg[] = new byte[126]; assertEchoFrame(OpCode.TEXT,payload);
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<WebSocketFrame> 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();
}
} }
/** /**
* Echo 127 byte text message (uses medium 2 byte payload length) * Echo 127 byte TEXT message (uses medium 2 byte payload length)
*/ */
@Test @Test
public void testCase1_1_4() throws Exception public void testCase1_1_4() throws Exception
{ {
BlockheadClient client = new BlockheadClient(server.getServerUri()); byte payload[] = new byte[127];
try Arrays.fill(payload,(byte)'*');
{
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
byte msg[] = new byte[127]; assertEchoFrame(OpCode.TEXT,payload);
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<WebSocketFrame> 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();
}
} }
/** /**
* Echo 128 byte text message (uses medium 2 byte payload length) * Echo 128 byte TEXT message (uses medium 2 byte payload length)
*/ */
@Test @Test
public void testCase1_1_5() throws Exception public void testCase1_1_5() throws Exception
{ {
BlockheadClient client = new BlockheadClient(server.getServerUri()); byte payload[] = new byte[128];
try Arrays.fill(payload,(byte)'*');
{
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
byte msg[] = new byte[128]; assertEchoFrame(OpCode.TEXT,payload);
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<WebSocketFrame> 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();
}
} }
/** /**
* Echo 65535 byte text message (uses medium 2 byte payload length) * Echo 65535 byte TEXT message (uses medium 2 byte payload length)
*/ */
@Test @Test
public void testCase1_1_6() throws Exception public void testCase1_1_6() throws Exception
{ {
BlockheadClient client = new BlockheadClient(server.getServerUri()); byte payload[] = new byte[65535];
try Arrays.fill(payload,(byte)'*');
{
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
byte msg[] = new byte[65535]; assertEchoFrame(OpCode.TEXT,payload);
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<WebSocketFrame> 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();
}
} }
/** /**
* Echo 65536 byte text message (uses large 8 byte payload length) * Echo 65536 byte TEXT message (uses large 8 byte payload length)
*/ */
@Test @Test
public void testCase1_1_7() throws Exception public void testCase1_1_7() throws Exception
{ {
BlockheadClient client = new BlockheadClient(server.getServerUri()); byte payload[] = new byte[65536];
try Arrays.fill(payload,(byte)'*');
{
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
byte msg[] = new byte[65536]; assertEchoFrame(OpCode.TEXT,payload);
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<WebSocketFrame> 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();
}
} }
/**
* Echo 65536 byte TEXT message (uses large 8 byte payload length).
* <p>
* Only send 1 TEXT frame from client, but in small segments (flushed after each).
* <p>
* 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).
* <p>
* Only send 1 BINARY frame from client, but in small segments (flushed after each).
* <p>
* 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);
}
} }

View File

@ -249,6 +249,11 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames
return respHeader; return respHeader;
} }
public void flush() throws IOException
{
out.flush();
}
public List<String> getExtensions() public List<String> getExtensions()
{ {
return extensions; return extensions;