ironing out generator issues at the byte lvl, so nice bit of progress

This commit is contained in:
Jesse McConnell 2012-06-20 18:42:24 -05:00
parent 8893b9c96a
commit a73d057578
4 changed files with 116 additions and 28 deletions

View File

@ -28,10 +28,10 @@ import org.eclipse.jetty.websocket.api.OpCode;
*/ */
public class BaseFrame public class BaseFrame
{ {
private boolean fin; private boolean fin = false;
private boolean rsv1; private boolean rsv1 = false;
private boolean rsv2; private boolean rsv2 = false;
private boolean rsv3; private boolean rsv3 = false;
private OpCode opcode = null; private OpCode opcode = null;
private boolean masked = false; private boolean masked = false;
private int payloadLength; private int payloadLength;

View File

@ -31,11 +31,6 @@ public abstract class ControlFrame extends BaseFrame
public void setPayload(ByteBuffer payload) public void setPayload(ByteBuffer payload)
{ {
this.payload = payload; this.payload = payload;
if (payload.position() != 0) setPayloadLength(this.payload.position());
{
// Make buffer ready for reading?
payload.flip();
}
setPayloadLength(payload.array().length);
} }
} }

View File

@ -100,6 +100,8 @@ public abstract class FrameGenerator<T extends BaseFrame>
// generate payload // generate payload
ByteBuffer payloadBuffer = payload(frame); ByteBuffer payloadBuffer = payload(frame);
payloadBuffer.flip(); // flip to ensure its ready to put pushed into final buffer
// insert framing // insert framing
buffer.put(framing); buffer.put(framing);

View File

@ -3,6 +3,8 @@ package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import junit.framework.Assert;
import org.eclipse.jetty.io.StandardByteBufferPool; import org.eclipse.jetty.io.StandardByteBufferPool;
import org.eclipse.jetty.websocket.ByteBufferAssert; import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.Debug; import org.eclipse.jetty.websocket.Debug;
@ -10,7 +12,9 @@ import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.PingFrame; import org.eclipse.jetty.websocket.frames.PingFrame;
import org.eclipse.jetty.websocket.frames.PongFrame; import org.eclipse.jetty.websocket.frames.PongFrame;
import org.eclipse.jetty.websocket.frames.TextFrame;
import org.eclipse.jetty.websocket.masks.FixedMasker; import org.eclipse.jetty.websocket.masks.FixedMasker;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public class RFC6455ExamplesGeneratorTest public class RFC6455ExamplesGeneratorTest
@ -18,30 +22,83 @@ public class RFC6455ExamplesGeneratorTest
StandardByteBufferPool bufferPool = new StandardByteBufferPool(); StandardByteBufferPool bufferPool = new StandardByteBufferPool();
@Test @Test
@Ignore ("need to fix still")
public void testFragmentedUnmaskedTextMessage()
{
ByteBuffer b1 = ByteBuffer.allocate(5);
b1.put(new byte[]
{ (byte)0x01, (byte)0x03, (byte)0x48, (byte)0x65, (byte)0x6c });
ByteBuffer b2 = ByteBuffer.allocate(4);
b2.put(new byte[]
{ (byte)0x80, (byte)0x02, (byte)0x6c, (byte)0x6f });
TextFrame t1 = new TextFrame();
TextFrame t2 = new TextFrame();
t1.setFin(false);
t2.setFin(true);
t1.setData("Hel");
t2.setData("lo");
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
TextFrameGenerator generator = new TextFrameGenerator(bufferPool,policy);
ByteBuffer g1 = generator.generate(t1);
ByteBuffer g2 = generator.generate(t2);
//Debug.dumpState(b1);
//Debug.dumpState(g1);
b1.flip();
g1.flip();
b2.flip();
g2.flip();
Assert.assertEquals(b1.get(),g1.get());
ByteBufferAssert.assertEquals("t1 buffers are not equal", b1, g1);
ByteBufferAssert.assertEquals("t2 buffers are not equal", b2, g2);
}
@Test
@Ignore ("need to fix still")
public void testSingleMaskedPongRequest() public void testSingleMaskedPongRequest()
{ {
ByteBuffer buf = ByteBuffer.allocate(11); ByteBuffer b1 = ByteBuffer.allocate(11);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples // Raw bytes as found in RFC 6455, Section 5.7 - Examples
// Unmasked Ping request // Unmasked Ping request
buf.put(new byte[] b1.put(new byte[]
{ (byte)0x8a, (byte)0x85, (byte)0x37, (byte)0xfa, (byte)0x21, (byte)0x3d, (byte)0x7f, (byte)0x9f, (byte)0x4d, (byte)0x51, (byte)0x58 }); { (byte)0x8a, (byte)0x85, (byte)0x37, (byte)0xfa, (byte)0x21, (byte)0x3d, (byte)0x7f, (byte)0x9f, (byte)0x4d, (byte)0x51, (byte)0x58 });
//buf.flip();
PongFrame pong = new PongFrame(); PongFrame pong = new PongFrame();
ByteBuffer payload = ByteBuffer.allocate(5); ByteBuffer payload = ByteBuffer.allocate(5);
payload.put("Hello".getBytes(), 0, 5); payload.put("Hello".getBytes(), 0, 5);
pong.setPayload(payload); pong.setPayload(payload);
pong.setFin(true);
pong.setMasked(true);
pong.setMask(new byte[]{(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff});
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMasker(new FixedMasker()); policy.setMasker(new FixedMasker());
PongFrameGenerator generator = new PongFrameGenerator(bufferPool,policy); PongFrameGenerator t1 = new PongFrameGenerator(bufferPool,policy);
ByteBuffer generatedPong = generator.generate(pong); ByteBuffer g1 = t1.generate(pong);
Debug.dumpState(buf);
Debug.dumpState(generatedPong);
ByteBufferAssert.assertEquals("pong buffers are not equal", buf, generatedPong); b1.flip();
g1.flip();
Debug.dumpState(b1);
Debug.dumpState(g1);
ByteBufferAssert.assertEquals("pong buffers are not equal", b1, g1);
} }
@ -50,31 +107,65 @@ public class RFC6455ExamplesGeneratorTest
public void testSingleUnmaskedPingRequest() throws Exception public void testSingleUnmaskedPingRequest() throws Exception
{ {
ByteBuffer buf = ByteBuffer.allocate(7); ByteBuffer b1 = ByteBuffer.allocate(7);
buf.put(new byte[] { b1.put(new byte[] {
(byte)0x89, (byte)0x05, (byte)0x48, (byte)0x65, (byte)0x6c, (byte)0x6c, (byte)0x6f (byte)0x89, (byte)0x05, (byte)0x48, (byte)0x65, (byte)0x6c, (byte)0x6c, (byte)0x6f
}); });
//buffer.flip();
PingFrame ping = new PingFrame(); PingFrame ping = new PingFrame();
ping.setFin(true);
ByteBuffer payload = ByteBuffer.allocate(5); ByteBuffer payload = ByteBuffer.allocate(5);
payload.put("Hello".getBytes(), 0, 5); payload.put("Hello".getBytes(), 0, 5);
ping.setPayload(payload); ping.setPayload(payload);
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
PingFrameGenerator generator = new PingFrameGenerator(bufferPool, policy); PingFrameGenerator t1 = new PingFrameGenerator(bufferPool, policy);
ByteBuffer generatedPing = generator.generate(ping); ByteBuffer g1 = t1.generate(ping);
Debug.dumpState(buf); b1.flip();
Debug.dumpState(generatedPing); g1.flip();
ByteBufferAssert.assertEquals("ping buffers are not equal",buf,generatedPing); Debug.dumpState(b1);
Debug.dumpState(g1);
ByteBufferAssert.assertEquals("ping buffers are not equal",b1,g1);
} }
@Test
@Ignore ("need to fix still")
public void testSingleMaskedTextMessage()
{
ByteBuffer b1 = ByteBuffer.allocate(11);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// A single-frame masked text message
b1.put(new byte[]
{ (byte)0x81, (byte)0x85, 0x37, (byte)0xfa, 0x21, 0x3d, 0x7f, (byte)0x9f, 0x4d, 0x51, 0x58 });
TextFrame t1 = new TextFrame();
t1.setData("Hello");
t1.setFin(true);
t1.setMasked(true);
t1.setMask(new byte[]{(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff});
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
TextFrameGenerator generator = new TextFrameGenerator(bufferPool,policy);
ByteBuffer g1 = generator.generate(t1);
Debug.dumpState(b1);
Debug.dumpState(g1);
b1.flip();
g1.flip();
ByteBufferAssert.assertEquals("masked text buffers are not equal",b1,g1);
}
} }